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 }
lzma_index ** i
Definition: index.h:629
#define RZ_API
voidpf void uLong size
Definition: ioapi.h:138
RZ_API bool rz_calculate_luhn_value(const char *data, ut64 *result)
Definition: luhn.c:6
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define IS_DIGIT(x)
Definition: rz_str_util.h:11
int ssize_t
Definition: sftypes.h:39
ut64(WINAPI *w32_GetEnabledXStateFeatures)()