Rizin
unix-like reverse engineering framework and cli tools
fletcher.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 "fletcher.h"
5 #include <rz_util.h>
6 
7 // Fletcher 8
8 
10  rz_return_val_if_fail(ctx, false);
11  memset(ctx, 0, sizeof(RzFletcher8));
12  return true;
13 }
14 
15 bool rz_fletcher8_update(RzFletcher8 *ctx, const ut8 *data, size_t len) {
16  rz_return_val_if_fail(ctx && data, false);
17  for (size_t i = 0; i < len; i++) {
18  ctx->low += data[i];
19  ctx->low = (ctx->low & 0xff) + (ctx->low >> 8);
20  ctx->high += ctx->low;
21  ctx->high = (ctx->high & 0xff) + (ctx->high >> 8);
22  }
23  return true;
24 }
25 
27  rz_return_val_if_fail(ctx && digest, false);
28  rz_write_le8(digest, (ctx->low & 0xff));
29  return true;
30 }
31 
32 // Fletcher 16
33 
35  rz_return_val_if_fail(ctx, false);
36  memset(ctx, 0, sizeof(RzFletcher16));
37  return true;
38 }
39 
40 bool rz_fletcher16_update(RzFletcher16 *ctx, const ut8 *data, size_t len) {
41  rz_return_val_if_fail(ctx && data, false);
42  size_t i;
43  for (; len >= 5802; len -= 5802) {
44  for (i = 0; i < 5802; i++) {
45  ctx->low = ctx->low + *data++;
46  ctx->high = ctx->high + ctx->low;
47  }
48  ctx->low %= 0xff;
49  ctx->high %= 0xff;
50  }
51  for (i = 0; i < len; i++) {
52  ctx->low += *data++;
53  ctx->high += ctx->low;
54  }
55  return true;
56 }
57 
59  rz_return_val_if_fail(ctx && digest, false);
60  ctx->low %= 0xff;
61  ctx->high %= 0xff;
62  rz_write_le16(digest, (ctx->high << 8 | ctx->low));
63  return true;
64 }
65 
66 // Fletcher 32
67 
69  rz_return_val_if_fail(ctx, false);
70  memset(ctx, 0, sizeof(RzFletcher32));
71  return true;
72 }
73 
74 bool rz_fletcher32_update(RzFletcher32 *ctx, const ut8 *data, size_t len) {
75  rz_return_val_if_fail(ctx && data, false);
76  size_t i;
77  ut8 word[sizeof(ut16)];
78  for (; len >= 360; len -= 360) {
79  for (i = 0; i < 360; i += 2) {
80  size_t left = 360 - i;
81  memset(word, 0, sizeof(word));
82  memcpy(word, data, RZ_MIN(sizeof(word), left));
83  ctx->low += rz_read_le16(word);
84  ctx->high += ctx->low;
85  data += 2;
86  }
87  ctx->low %= UT16_MAX;
88  ctx->high %= UT16_MAX;
89  }
90  for (i = 0; i < len; i += 2) {
91  size_t left = len - i;
92  memset(word, 0, sizeof(word));
93  memcpy(word, data, RZ_MIN(sizeof(word), left));
94  ctx->low += rz_read_le16(word);
95  ctx->high += ctx->low;
96  data += 2;
97  }
98  return true;
99 }
100 
102  rz_return_val_if_fail(ctx && digest, false);
103  ctx->low %= UT16_MAX;
104  ctx->high %= UT16_MAX;
105  rz_write_le32(digest, ctx->high << 16 | ctx->low);
106  return true;
107 }
108 
109 // Fletcher 64
110 
112  rz_return_val_if_fail(ctx, false);
113  memset(ctx, 0, sizeof(RzFletcher64));
114  return true;
115 }
116 
117 bool rz_fletcher64_update(RzFletcher64 *ctx, const ut8 *data, size_t len) {
118  rz_return_val_if_fail(ctx && data, false);
119 
120  ut8 word[sizeof(ut32)];
121  for (size_t i = 0; i < len; i += sizeof(ut32)) {
122  size_t left = RZ_MIN(sizeof(ut32), len - i);
123  memset(word, 0, sizeof(word));
124  memcpy(word, &data[i], left);
125  ut32 value = rz_read_le32(word);
126  ctx->low += value;
127  ctx->high += ctx->low;
128  }
129  return true;
130 }
131 
133  rz_return_val_if_fail(ctx && digest, false);
134  rz_write_le64(digest, ((ut64)ctx->high << 32) | ctx->low);
135  return true;
136 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static int value
Definition: cmd_api.c:93
uint16_t ut16
uint32_t ut32
bool rz_fletcher64_final(ut8 *digest, RzFletcher64 *ctx)
Definition: fletcher.c:132
bool rz_fletcher64_update(RzFletcher64 *ctx, const ut8 *data, size_t len)
Definition: fletcher.c:117
bool rz_fletcher32_final(ut8 *digest, RzFletcher32 *ctx)
Definition: fletcher.c:101
bool rz_fletcher16_final(ut8 *digest, RzFletcher16 *ctx)
Definition: fletcher.c:58
bool rz_fletcher8_final(ut8 *digest, RzFletcher8 *ctx)
Definition: fletcher.c:26
bool rz_fletcher16_init(RzFletcher16 *ctx)
Definition: fletcher.c:34
bool rz_fletcher32_init(RzFletcher32 *ctx)
Definition: fletcher.c:68
bool rz_fletcher16_update(RzFletcher16 *ctx, const ut8 *data, size_t len)
Definition: fletcher.c:40
bool rz_fletcher8_update(RzFletcher8 *ctx, const ut8 *data, size_t len)
Definition: fletcher.c:15
bool rz_fletcher64_init(RzFletcher64 *ctx)
Definition: fletcher.c:111
bool rz_fletcher8_init(RzFletcher8 *ctx)
Definition: fletcher.c:9
bool rz_fletcher32_update(RzFletcher32 *ctx, const ut8 *data, size_t len)
Definition: fletcher.c:74
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static void rz_write_le32(void *dest, ut32 val)
Definition: rz_endian.h:256
static void rz_write_le16(void *dest, ut16 val)
Definition: rz_endian.h:222
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
static ut32 rz_read_le32(const void *src)
Definition: rz_endian.h:239
static void rz_write_le64(void *dest, ut64 val)
Definition: rz_endian.h:277
static void rz_write_le8(void *dest, ut8 val)
Definition: rz_endian.h:198
#define RZ_MIN(x, y)
#define UT16_MAX
ut64(WINAPI *w32_GetEnabledXStateFeatures)()