Rizin
unix-like reverse engineering framework and cli tools
luhn.c
Go to the documentation of this file.
1
// SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2
// SPDX-License-Identifier: LGPL-3.0-only
3
4
#include <
rz_util.h
>
5
6
RZ_API
bool
rz_calculate_luhn_value
(
const
char
*data,
ut64
*result) {
7
rz_return_val_if_fail
(data && result,
false
);
8
ssize_t
size
= strlen(data);
9
if
(
size
< 1) {
10
return
false
;
11
}
12
13
int
digit;
14
ut64
sum = 0;
15
bool
parity =
false
;
16
for
(
ssize_t
i
=
size
- 1;
i
>= 0; --
i
) {
17
if
(!
IS_DIGIT
(data[
i
])) {
18
return
false
;
19
}
20
21
digit = data[
i
] -
'0'
;
22
if
(parity) {
23
digit *= 2;
24
}
25
digit = (digit / 10) + (digit % 10);
26
sum += digit;
27
parity = !parity;
28
}
29
30
*result = sum % 10;
31
return
true
;
32
}
i
lzma_index ** i
Definition:
index.h:629
RZ_API
#define RZ_API
Definition:
core_plugin_example.c:36
size
voidpf void uLong size
Definition:
ioapi.h:138
rz_calculate_luhn_value
RZ_API bool rz_calculate_luhn_value(const char *data, ut64 *result)
Definition:
luhn.c:6
rz_return_val_if_fail
#define rz_return_val_if_fail(expr, val)
Definition:
rz_assert.h:108
IS_DIGIT
#define IS_DIGIT(x)
Definition:
rz_str_util.h:11
rz_util.h
ssize_t
int ssize_t
Definition:
sftypes.h:39
ut64
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
librz
util
luhn.c
Generated by
1.9.1