-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrc.c
77 lines (59 loc) · 1.74 KB
/
crc.c
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
/*
* DESFire-Shell: Modify MIFARE DESFire Cards
*
* Copyright (C) 2015-2021 Mario Haustein
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see https://www.gnu.org/licenses/.
*/
#include <stdint.h>
#include <lua.h>
#include <lauxlib.h>
#include <zlib.h>
#include "buffer.h"
#include "desflua.h"
#include "fn.h"
static int crc_crc32(lua_State *l);
FN_ALIAS(crc_crc32) = { "crc32", NULL };
FN_PARAM(crc_crc32) =
{
FNPARAM("input", "Input Buffer", 0),
FNPARAMEND
};
FN_RET(crc_crc32) =
{
FNPARAM("crc32", "Checksum", 0),
FNPARAMEND
};
FN("crc", crc_crc32, "Calculate a CRC-32 checksum",
"Calculates the CRC-32 checksum of the input buffer <input>. The checksum\n" \
"is returned as a buffer.\n");
static int crc_crc32(lua_State *l)
{
int result;
uint8_t *input, crcbuf[4];
unsigned int inputlen;
uint32_t crc;
result = buffer_get(l, 1, &input, &inputlen);
if(result)
desflua_argerror(l, 1, "input");
crc = crc32(0, Z_NULL, 0);
crc = crc32(crc, input, inputlen);
crcbuf[0] = (crc >> 24) & 0xff;
crcbuf[1] = (crc >> 16) & 0xff;
crcbuf[2] = (crc >> 8) & 0xff;
crcbuf[3] = crc & 0xff;
lua_settop(l, 0);
buffer_push(l, crcbuf, 4);
return lua_gettop(l);
}