Rizin
unix-like reverse engineering framework and cli tools
rz_luhn.h File Reference
#include <rz_types.h>

Go to the source code of this file.

Functions

RZ_API bool rz_calculate_luhn_value (const char *data, ut64 *result)
 

Function Documentation

◆ rz_calculate_luhn_value()

RZ_API bool rz_calculate_luhn_value ( const char *  data,
ut64 result 
)

Definition at line 6 of file luhn.c.

6  {
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
voidpf void uLong size
Definition: ioapi.h:138
#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)()

References i, IS_DIGIT, rz_return_val_if_fail, and ut64().

Referenced by calculate_luhn().