aboutsummaryrefslogtreecommitdiff
path: root/src/hash/crc.c
blob: c3be7f32a64bf3cb18a320f7fd552b88764c5b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "../crypto.h"
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdint.h>

int crc32_free_l(lua_State* L){return 0;}
int crc16_free_l(lua_State* L){return 0;}
int crc8_free_l(lua_State* L){return 0;}

struct crc32_hash crc32_init(){
  return (struct crc32_hash){.crc = 0xFFFFFFFF};
}

void crc32_update(uint8_t* data, size_t len, struct crc32_hash* hash){
  for(int i = 0; i < len; i++){
    uint32_t extract = data[i];
    for(int z = 0; z < 8; z++){
      uint32_t b = (extract^hash->crc)&1;
      hash->crc>>=1;
      if(b) hash->crc^=0xEDB88320;
      extract>>=1;
    }
  }
}

uint32_t crc32_final(struct crc32_hash* hash){
  return -(hash->crc+1);
}

uint32_t crc32(uint8_t* data, size_t len){
  struct crc32_hash a = crc32_init();
  crc32_update(data, len, &a);
  return crc32_final(&a);
}

struct crc16_hash crc16_init(){
  return (struct crc16_hash){.crc = 0x0};
}

void crc16_update(uint8_t *aa, size_t len, struct crc16_hash *hash){
  for(int i = 0; i != len; i++){
    uint8_t a = aa[i];
    hash->crc ^= a;
    for (int z = 0; z < 8; z++){
      if (hash->crc & 1) hash->crc = (hash->crc >> 1) ^ 0xA001;
      else hash->crc = (hash->crc >> 1);
    }
  }
}

uint16_t crc16_final(struct crc16_hash *hash){
  return hash->crc;
}

uint16_t crc16(uint8_t *aa, size_t len){
  struct crc16_hash a = crc16_init();
  crc16_update(aa, len, &a);
  return crc16_final(&a);
}

struct crc8_hash crc8_init(){
  return (struct crc8_hash){.crc = 0x00};
}

void crc8_update(uint8_t *aa, size_t len, struct crc8_hash *hash){
  for(int i = 0; i != len; i++){
    uint8_t a = aa[i];

    for (int z = 0; z < 8; z++){
      uint8_t b = (hash->crc ^ a) & 1;
      hash->crc >>= 1;
      if(b) hash->crc ^= 0x8c;
      a >>=1;
    }
  }
}

uint8_t crc8_final(struct crc8_hash *hash){
  return hash->crc;
}

uint8_t crc8(uint8_t *aa, size_t len){
  struct crc8_hash a = crc8_init();
  crc8_update(aa, len, &a);
  return crc8_final(&a);
}

common_hash_clone(crc32);
common_hash_clone(crc16);
common_hash_clone(crc8);

common_hash_init_update(crc32);
common_hash_init_update(crc16);
common_hash_init_update(crc8);

int l_crc8_final(lua_State* L){
  struct crc8_hash* a = (struct crc8_hash*)lua_touserdata(L, 1);
  uint32_t u = crc8_final(a);
  char digest[8];
  sprintf(digest,"%x",u);
  lua_pushstring(L, digest);
  return 1;
}

int l_crc16_final(lua_State* L){
  struct crc16_hash* a = (struct crc16_hash*)lua_touserdata(L, 1);
  uint32_t u = crc16_final(a);
  char digest[16];
  sprintf(digest,"%04x",u);
  lua_pushstring(L, digest);
  return 1;
}

int l_crc32_final(lua_State* L){
  struct crc32_hash* a = (struct crc32_hash*)lua_touserdata(L, 1);
  uint32_t u = crc32_final(a);
  char digest[32];
  sprintf(digest,"%08x",u);
  lua_pushstring(L, digest);
  return 1;
}

int l_crc8(lua_State* L){
  if(lua_gettop(L) == 0) return l_crc8_init(L);
  size_t len = 0;
  uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);

  char digest[8];

  uint8_t u = crc8(a, len);
  sprintf(digest,"%x",u);
  lua_pushstring(L, digest);

  return 1;
}

int l_crc16(lua_State* L){
  if(lua_gettop(L) == 0) return l_crc16_init(L);
  size_t len = 0;
  uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);

  char digest[16];

  uint16_t u = crc16(a, len);
  sprintf(digest,"%x",u);
  lua_pushstring(L, digest);

  return 1;
}

int l_crc32(lua_State* L){
  if(lua_gettop(L) == 0) return l_crc32_init(L);
  size_t len = 0;
  uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);

  char digest[32];

  uint32_t u = crc32(a, len);
  sprintf(digest,"%x",u);
  lua_pushstring(L, digest);

  return 1;
}