-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.c
120 lines (103 loc) · 3.86 KB
/
test.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
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
/******************************************************************************
* *
* Copyright 2020-2021 Meng-Shan Jiang *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*****************************************************************************/
#include "test.h"
/* 0->'0', 1->'1', ... , 15->'F' */
static const u8 ascii_table[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};
static const u8 inv_ascii_table[128] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 10, 11, 12, 13, 14, 15, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 10, 11, 12, 13, 14, 15, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
int u8_to_hex(u8 *out, const u8 *in, size_t inlen)
{
size_t i;
if (out == NULL)
return -1;
for (i = 0; i < inlen; i++) {
out[0] = ascii_table[in[i] >> 4];
out[1] = ascii_table[in[i] & 0xf];
out += 2;
}
return 0;
}
int hex_to_u8(u8 *out, const u8 *in, size_t inlen)
{
size_t i;
if (out == NULL)
return -1;
i = 0;
if (inlen % 2 == 1) {
out[0] = inv_ascii_table[in[i]];
if (out[0] == 0xff)
return -1;
out++;
i++;
}
for (; i < inlen; i += 2) {
out[0] = (inv_ascii_table[in[i]] << 4) | inv_ascii_table[in[i+1]];
// if (out[0] == 0xff)
// return FP256_ERR;
out++;
}
return 0;
}
void print_hex(const char *desp, const u8 *s, size_t slen)
{
size_t i;
for(i = 0; i < strlen(desp); i++)
printf("%c", desp[i]);
u8 *hex = (u8*)malloc(2*slen);
u8_to_hex(hex, s, slen);
for(i = 0; i < 2*slen; i++)
printf("%c", hex[i]);
printf("\n");
free(hex);
}
/* bad rng, but good for test */
void random_string(u8 *s, size_t len)
{
static int en = 0;
size_t i;
srand((unsigned)time(NULL) + en);
for (i = 0; i < len; i++)
s[len] = rand() % 256;
en++;
}
size_t random_number(void)
{
static int en = 0;
srand((unsigned)time(NULL) + en);
en++;
return (size_t)rand();
}