forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
162 lines (142 loc) · 2.38 KB
/
common.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
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
#include "common.h"
S32 CharToByte(const S8 *pCharBuf, S32 charlen, U8 *pByteBuf, S32 *bytelen)
{
S32 i = 0;
U8 hdata = 0;
U8 ldata = 0;
S32 charlen_tmp = charlen;
if (charlen_tmp & LSBOfWord)
{
charlen_tmp += 1;
*bytelen = charlen_tmp >> 1;
if (ConvertHexChar(pCharBuf[0], &ldata) == 1)
{
pByteBuf[0] = ldata;
}
for (i = 1; i < *bytelen; i++)
{
if (ConvertHexChar(pCharBuf[2 * i - 1], &hdata) == 1)
{
if (ConvertHexChar(pCharBuf[2 * i], &ldata) == 1)
{
pByteBuf[i] = (hdata << 4) | ldata;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
else
{
*bytelen = charlen_tmp >> 1;
for (i = 0; i < *bytelen; i++)
{
if (ConvertHexChar(pCharBuf[2 * i], &hdata) == 1)
{
if (ConvertHexChar(pCharBuf[2 * i + 1], &ldata) == 1)
{
pByteBuf[i] = (hdata << 4) | ldata;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
return 1;
}
S32 ConvertHexChar(S8 ch, U8 *ch_byte)
{
if ((ch >= '0') && (ch <= '9'))
{
*ch_byte = (U8)(ch - 0x30);
return 1;
}
else
{
if ((ch >= 'A') && (ch <= 'F'))
{
*ch_byte = (U8)(ch - 'A' + 0x0a);
return 1;
}
else
{
if ((ch >= 'a') && (ch <= 'f'))
{
*ch_byte = (U8)(ch - 'a' + 0x0a);
return 1;
}
}
}
return 0;
}
S32 ByteToBN(U8 *pByteBuf, S32 bytelen, U32 *pwBN, S32 iBNWordLen)
{
S32 ExpLen = bytelen >> 2;
S32 Rem = bytelen & 0x00000003;
if (Rem != 0)
{
ExpLen += 1;
}
if (ExpLen > iBNWordLen)
{
return 0;
}
S32 i = bytelen - 1;
S32 j = 0;
while (i >= Rem)
{
pwBN[j] = ((U32)pByteBuf[i]) | ((U32)pByteBuf[i - 1] << 8) | ((U32)pByteBuf[i - 2] << 16) | ((U32)pByteBuf[i - 3] << 24);
i -= 4;
j++;
}
i = 0;
while (i < Rem)
{
pwBN[j] = (pwBN[j] << 8) | ((U32)pByteBuf[i]);
i++;
}
return 1;
}
S32 BNToByte(U32 *pwBN,S32 iBNWordLen,U8 *pByteBuf,S32 *bytelen)
{
U8 * P = pByteBuf;
for(S32 i = iBNWordLen - 1; i >= 0; i--)
{
U32 W = pwBN[i];
*P++=(U8) ((W & 0xFF000000) >> 24);
*P++=(U8) ((W & 0x00FF0000) >> 16);
*P++=(U8) ((W & 0x0000FF00) >> 8);
*P++=(U8) (W & 0x000000FF) ;
}
*bytelen = iBNWordLen << 2;
return 1;
}
void U8_Print(U8* pwSource,S32 len)
{
for(S32 i = 0;i<len;i++)
{
printf("%02X",pwSource[i]);
}
printf("\n");
}
S32 U8_JE(U8* pwX,U8* pwY,S32 len)
{
for(S32 i = 0;i<len;i++)
{
if(pwX[i]!=pwY[i])
return 0;
}
return 1;
}