]> Git Repo - VerusCoin.git/blame - src/komodo_utils.h
test
[VerusCoin.git] / src / komodo_utils.h
CommitLineData
d019c447 1/******************************************************************************
2 * Copyright © 2014-2016 The SuperNET Developers. *
3 * *
4 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
5 * the top-level directory of this distribution for the individual copyright *
6 * holder information and the developer policies on copyright and licensing. *
7 * *
8 * Unless otherwise agreed in a custom licensing agreement, no part of the *
9 * SuperNET software, including this file may be copied, modified, propagated *
10 * or distributed except according to the terms contained in the LICENSE file *
11 * *
12 * Removal or modification of this copyright notice is prohibited. *
13 * *
14 ******************************************************************************/
3eea72f2 15
453e1052 16#define SATOSHIDEN ((uint64_t)100000000L)
32b1b443 17#define dstr(x) ((double)(x) / SATOSHIDEN)
b67b154f 18#define portable_mutex_t pthread_mutex_t
19#define portable_mutex_init(ptr) pthread_mutex_init(ptr,NULL)
20#define portable_mutex_lock pthread_mutex_lock
21#define portable_mutex_unlock pthread_mutex_unlock
32b1b443 22
2552dbb4 23struct allocitem { uint32_t allocsize,type; };
0a912c89 24struct queueitem { struct queueitem *next,*prev; uint32_t allocsize,type; };
25
a18fbdc5 26typedef struct queue
27{
28 struct queueitem *list;
0a912c89 29 pthread_mutex_t mutex;
a18fbdc5 30 char name[64],initflag;
31} queue_t;
32
48fa27d9 33union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; };
34typedef union _bits256 bits256;
35
d019c447 36#include "mini-gmp.c"
37#include "uthash.h"
429dabb5 38#include "utlist.h"
3eea72f2 39
40#define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9"
b62d7030 41#define CRYPTO777_KMDADDR "RXL3YXG2ceaB6C5hfJcN4fvmLH2C34knhA"
878412df 42#define CRYPTO777_RMD160STR "f1dce4182fce875748c4986b240ff7d7bc3fffb0"
b62d7030 43
d019c447 44#define KOMODO_PUBTYPE 60
45
d019c447 46struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; };
47struct rmd160_vstate { uint64_t length; uint8_t buf[64]; uint32_t curlen, state[5]; };
48
49// following is ported from libtom
50
51#define STORE32L(x, y) \
52{ (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \
53(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
54
55#define LOAD32L(x, y) \
56{ x = (uint32_t)(((uint64_t)((y)[3] & 255)<<24) | \
57((uint32_t)((y)[2] & 255)<<16) | \
58((uint32_t)((y)[1] & 255)<<8) | \
59((uint32_t)((y)[0] & 255))); }
60
61#define STORE64L(x, y) \
62{ (y)[7] = (uint8_t)(((x)>>56)&255); (y)[6] = (uint8_t)(((x)>>48)&255); \
63(y)[5] = (uint8_t)(((x)>>40)&255); (y)[4] = (uint8_t)(((x)>>32)&255); \
64(y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \
65(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
66
67#define LOAD64L(x, y) \
68{ x = (((uint64_t)((y)[7] & 255))<<56)|(((uint64_t)((y)[6] & 255))<<48)| \
69(((uint64_t)((y)[5] & 255))<<40)|(((uint64_t)((y)[4] & 255))<<32)| \
70(((uint64_t)((y)[3] & 255))<<24)|(((uint64_t)((y)[2] & 255))<<16)| \
71(((uint64_t)((y)[1] & 255))<<8)|(((uint64_t)((y)[0] & 255))); }
72
73#define STORE32H(x, y) \
74{ (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255); \
75(y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); }
76
77#define LOAD32H(x, y) \
78{ x = (uint32_t)(((uint64_t)((y)[0] & 255)<<24) | \
79((uint32_t)((y)[1] & 255)<<16) | \
80((uint32_t)((y)[2] & 255)<<8) | \
81((uint32_t)((y)[3] & 255))); }
82
83#define STORE64H(x, y) \
84{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
85(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
86(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
87(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
88
89#define LOAD64H(x, y) \
90{ x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \
91(((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \
92(((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \
93(((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); }
94
95// Various logical functions
96#define RORc(x, y) ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)(32-((y)&31)))) & 0xFFFFFFFFUL)
97#define Ch(x,y,z) (z ^ (x & (y ^ z)))
98#define Maj(x,y,z) (((x | y) & z) | (x & y))
99#define S(x, n) RORc((x),(n))
100#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
101#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
102#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
103#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
104#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
105#define MIN(x, y) ( ((x)<(y))?(x):(y) )
106
107static inline int32_t sha256_vcompress(struct sha256_vstate * md,uint8_t *buf)
108{
109 uint32_t S[8],W[64],t0,t1,i;
110 for (i=0; i<8; i++) // copy state into S
111 S[i] = md->state[i];
112 for (i=0; i<16; i++) // copy the state into 512-bits into W[0..15]
113 LOAD32H(W[i],buf + (4*i));
114 for (i=16; i<64; i++) // fill W[16..63]
115 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
116
117#define RND(a,b,c,d,e,f,g,h,i,ki) \
118t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
119t1 = Sigma0(a) + Maj(a, b, c); \
120d += t0; \
121h = t0 + t1;
122
123 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
124 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
125 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
126 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
127 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
128 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
129 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
130 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
131 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
132 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
133 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
134 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
135 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
136 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
137 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
138 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
139 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
140 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
141 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
142 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
143 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
144 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
145 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
146 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
147 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
148 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
149 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
150 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
151 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
152 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
153 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
154 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
155 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
156 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
157 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
158 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
159 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
160 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
161 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
162 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
163 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
164 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
165 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
166 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
167 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
168 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
169 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
170 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
171 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
172 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
173 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
174 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
175 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
176 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
177 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
178 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
179 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
180 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
181 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
182 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
183 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
184 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
185 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
186 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
187#undef RND
188 for (i=0; i<8; i++) // feedback
189 md->state[i] = md->state[i] + S[i];
190 return(0);
191}
192
193#undef RORc
194#undef Ch
195#undef Maj
196#undef S
197#undef R
198#undef Sigma0
199#undef Sigma1
200#undef Gamma0
201#undef Gamma1
202
203static inline void sha256_vinit(struct sha256_vstate * md)
204{
205 md->curlen = 0;
206 md->length = 0;
207 md->state[0] = 0x6A09E667UL;
208 md->state[1] = 0xBB67AE85UL;
209 md->state[2] = 0x3C6EF372UL;
210 md->state[3] = 0xA54FF53AUL;
211 md->state[4] = 0x510E527FUL;
212 md->state[5] = 0x9B05688CUL;
213 md->state[6] = 0x1F83D9ABUL;
214 md->state[7] = 0x5BE0CD19UL;
215}
216
217static inline int32_t sha256_vprocess(struct sha256_vstate *md,const uint8_t *in,uint64_t inlen)
218{
219 uint64_t n; int32_t err;
220 if ( md->curlen > sizeof(md->buf) )
221 return(-1);
222 while ( inlen > 0 )
223 {
224 if ( md->curlen == 0 && inlen >= 64 )
225 {
226 if ( (err= sha256_vcompress(md,(uint8_t *)in)) != 0 )
227 return(err);
228 md->length += 64 * 8, in += 64, inlen -= 64;
229 }
230 else
231 {
232 n = MIN(inlen,64 - md->curlen);
233 memcpy(md->buf + md->curlen,in,(size_t)n);
234 md->curlen += n, in += n, inlen -= n;
235 if ( md->curlen == 64 )
236 {
237 if ( (err= sha256_vcompress(md,md->buf)) != 0 )
238 return(err);
239 md->length += 8*64;
240 md->curlen = 0;
241 }
242 }
243 }
244 return(0);
245}
246
247static inline int32_t sha256_vdone(struct sha256_vstate *md,uint8_t *out)
248{
249 int32_t i;
250 if ( md->curlen >= sizeof(md->buf) )
251 return(-1);
252 md->length += md->curlen * 8; // increase the length of the message
253 md->buf[md->curlen++] = (uint8_t)0x80; // append the '1' bit
254 // if len > 56 bytes we append zeros then compress. Then we can fall back to padding zeros and length encoding like normal.
255 if ( md->curlen > 56 )
256 {
257 while ( md->curlen < 64 )
258 md->buf[md->curlen++] = (uint8_t)0;
259 sha256_vcompress(md,md->buf);
260 md->curlen = 0;
261 }
262 while ( md->curlen < 56 ) // pad upto 56 bytes of zeroes
263 md->buf[md->curlen++] = (uint8_t)0;
264 STORE64H(md->length,md->buf+56); // store length
265 sha256_vcompress(md,md->buf);
266 for (i=0; i<8; i++) // copy output
267 STORE32H(md->state[i],out+(4*i));
268 return(0);
269}
270
271void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len)
272{
273 struct sha256_vstate md;
274 sha256_vinit(&md);
275 sha256_vprocess(&md,src,len);
276 sha256_vdone(&md,hash);
277}
278
279bits256 bits256_doublesha256(char *deprecated,uint8_t *data,int32_t datalen)
280{
281 bits256 hash,hash2; int32_t i;
282 vcalc_sha256(0,hash.bytes,data,datalen);
283 vcalc_sha256(0,hash2.bytes,hash.bytes,sizeof(hash));
284 for (i=0; i<sizeof(hash); i++)
285 hash.bytes[i] = hash2.bytes[sizeof(hash) - 1 - i];
286 return(hash);
287}
288
289// rmd160: the five basic functions F(), G() and H()
290#define F(x, y, z) ((x) ^ (y) ^ (z))
291#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
292#define H(x, y, z) (((x) | ~(y)) ^ (z))
293#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
294#define J(x, y, z) ((x) ^ ((y) | ~(z)))
295#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
296
297/* the ten basic operations FF() through III() */
298#define FF(a, b, c, d, e, x, s) \
299(a) += F((b), (c), (d)) + (x);\
300(a) = ROLc((a), (s)) + (e);\
301(c) = ROLc((c), 10);
302
303#define GG(a, b, c, d, e, x, s) \
304(a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
305(a) = ROLc((a), (s)) + (e);\
306(c) = ROLc((c), 10);
307
308#define HH(a, b, c, d, e, x, s) \
309(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
310(a) = ROLc((a), (s)) + (e);\
311(c) = ROLc((c), 10);
312
313#define II(a, b, c, d, e, x, s) \
314(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
315(a) = ROLc((a), (s)) + (e);\
316(c) = ROLc((c), 10);
317
318#define JJ(a, b, c, d, e, x, s) \
319(a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
320(a) = ROLc((a), (s)) + (e);\
321(c) = ROLc((c), 10);
322
323#define FFF(a, b, c, d, e, x, s) \
324(a) += F((b), (c), (d)) + (x);\
325(a) = ROLc((a), (s)) + (e);\
326(c) = ROLc((c), 10);
327
328#define GGG(a, b, c, d, e, x, s) \
329(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
330(a) = ROLc((a), (s)) + (e);\
331(c) = ROLc((c), 10);
332
333#define HHH(a, b, c, d, e, x, s) \
334(a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
335(a) = ROLc((a), (s)) + (e);\
336(c) = ROLc((c), 10);
337
338#define III(a, b, c, d, e, x, s) \
339(a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
340(a) = ROLc((a), (s)) + (e);\
341(c) = ROLc((c), 10);
342
343#define JJJ(a, b, c, d, e, x, s) \
344(a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
345(a) = ROLc((a), (s)) + (e);\
346(c) = ROLc((c), 10);
347
348static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf)
349{
350 uint32_t aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16];
351 int i;
352
353 /* load words X */
354 for (i = 0; i < 16; i++){
355 LOAD32L(X[i], buf + (4 * i));
356 }
357
358 /* load state */
359 aa = aaa = md->state[0];
360 bb = bbb = md->state[1];
361 cc = ccc = md->state[2];
362 dd = ddd = md->state[3];
363 ee = eee = md->state[4];
364
365 /* round 1 */
366 FF(aa, bb, cc, dd, ee, X[ 0], 11);
367 FF(ee, aa, bb, cc, dd, X[ 1], 14);
368 FF(dd, ee, aa, bb, cc, X[ 2], 15);
369 FF(cc, dd, ee, aa, bb, X[ 3], 12);
370 FF(bb, cc, dd, ee, aa, X[ 4], 5);
371 FF(aa, bb, cc, dd, ee, X[ 5], 8);
372 FF(ee, aa, bb, cc, dd, X[ 6], 7);
373 FF(dd, ee, aa, bb, cc, X[ 7], 9);
374 FF(cc, dd, ee, aa, bb, X[ 8], 11);
375 FF(bb, cc, dd, ee, aa, X[ 9], 13);
376 FF(aa, bb, cc, dd, ee, X[10], 14);
377 FF(ee, aa, bb, cc, dd, X[11], 15);
378 FF(dd, ee, aa, bb, cc, X[12], 6);
379 FF(cc, dd, ee, aa, bb, X[13], 7);
380 FF(bb, cc, dd, ee, aa, X[14], 9);
381 FF(aa, bb, cc, dd, ee, X[15], 8);
382
383 /* round 2 */
384 GG(ee, aa, bb, cc, dd, X[ 7], 7);
385 GG(dd, ee, aa, bb, cc, X[ 4], 6);
386 GG(cc, dd, ee, aa, bb, X[13], 8);
387 GG(bb, cc, dd, ee, aa, X[ 1], 13);
388 GG(aa, bb, cc, dd, ee, X[10], 11);
389 GG(ee, aa, bb, cc, dd, X[ 6], 9);
390 GG(dd, ee, aa, bb, cc, X[15], 7);
391 GG(cc, dd, ee, aa, bb, X[ 3], 15);
392 GG(bb, cc, dd, ee, aa, X[12], 7);
393 GG(aa, bb, cc, dd, ee, X[ 0], 12);
394 GG(ee, aa, bb, cc, dd, X[ 9], 15);
395 GG(dd, ee, aa, bb, cc, X[ 5], 9);
396 GG(cc, dd, ee, aa, bb, X[ 2], 11);
397 GG(bb, cc, dd, ee, aa, X[14], 7);
398 GG(aa, bb, cc, dd, ee, X[11], 13);
399 GG(ee, aa, bb, cc, dd, X[ 8], 12);
400
401 /* round 3 */
402 HH(dd, ee, aa, bb, cc, X[ 3], 11);
403 HH(cc, dd, ee, aa, bb, X[10], 13);
404 HH(bb, cc, dd, ee, aa, X[14], 6);
405 HH(aa, bb, cc, dd, ee, X[ 4], 7);
406 HH(ee, aa, bb, cc, dd, X[ 9], 14);
407 HH(dd, ee, aa, bb, cc, X[15], 9);
408 HH(cc, dd, ee, aa, bb, X[ 8], 13);
409 HH(bb, cc, dd, ee, aa, X[ 1], 15);
410 HH(aa, bb, cc, dd, ee, X[ 2], 14);
411 HH(ee, aa, bb, cc, dd, X[ 7], 8);
412 HH(dd, ee, aa, bb, cc, X[ 0], 13);
413 HH(cc, dd, ee, aa, bb, X[ 6], 6);
414 HH(bb, cc, dd, ee, aa, X[13], 5);
415 HH(aa, bb, cc, dd, ee, X[11], 12);
416 HH(ee, aa, bb, cc, dd, X[ 5], 7);
417 HH(dd, ee, aa, bb, cc, X[12], 5);
418
419 /* round 4 */
420 II(cc, dd, ee, aa, bb, X[ 1], 11);
421 II(bb, cc, dd, ee, aa, X[ 9], 12);
422 II(aa, bb, cc, dd, ee, X[11], 14);
423 II(ee, aa, bb, cc, dd, X[10], 15);
424 II(dd, ee, aa, bb, cc, X[ 0], 14);
425 II(cc, dd, ee, aa, bb, X[ 8], 15);
426 II(bb, cc, dd, ee, aa, X[12], 9);
427 II(aa, bb, cc, dd, ee, X[ 4], 8);
428 II(ee, aa, bb, cc, dd, X[13], 9);
429 II(dd, ee, aa, bb, cc, X[ 3], 14);
430 II(cc, dd, ee, aa, bb, X[ 7], 5);
431 II(bb, cc, dd, ee, aa, X[15], 6);
432 II(aa, bb, cc, dd, ee, X[14], 8);
433 II(ee, aa, bb, cc, dd, X[ 5], 6);
434 II(dd, ee, aa, bb, cc, X[ 6], 5);
435 II(cc, dd, ee, aa, bb, X[ 2], 12);
436
437 /* round 5 */
438 JJ(bb, cc, dd, ee, aa, X[ 4], 9);
439 JJ(aa, bb, cc, dd, ee, X[ 0], 15);
440 JJ(ee, aa, bb, cc, dd, X[ 5], 5);
441 JJ(dd, ee, aa, bb, cc, X[ 9], 11);
442 JJ(cc, dd, ee, aa, bb, X[ 7], 6);
443 JJ(bb, cc, dd, ee, aa, X[12], 8);
444 JJ(aa, bb, cc, dd, ee, X[ 2], 13);
445 JJ(ee, aa, bb, cc, dd, X[10], 12);
446 JJ(dd, ee, aa, bb, cc, X[14], 5);
447 JJ(cc, dd, ee, aa, bb, X[ 1], 12);
448 JJ(bb, cc, dd, ee, aa, X[ 3], 13);
449 JJ(aa, bb, cc, dd, ee, X[ 8], 14);
450 JJ(ee, aa, bb, cc, dd, X[11], 11);
451 JJ(dd, ee, aa, bb, cc, X[ 6], 8);
452 JJ(cc, dd, ee, aa, bb, X[15], 5);
453 JJ(bb, cc, dd, ee, aa, X[13], 6);
454
455 /* parallel round 1 */
456 JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
457 JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
458 JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
459 JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
460 JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
461 JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
462 JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
463 JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
464 JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
465 JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
466 JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
467 JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
468 JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
469 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
470 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
471 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
472
473 /* parallel round 2 */
474 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
475 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
476 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
477 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
478 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
479 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
480 III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
481 III(ccc, ddd, eee, aaa, bbb, X[10], 11);
482 III(bbb, ccc, ddd, eee, aaa, X[14], 7);
483 III(aaa, bbb, ccc, ddd, eee, X[15], 7);
484 III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
485 III(ddd, eee, aaa, bbb, ccc, X[12], 7);
486 III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
487 III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
488 III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
489 III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
490
491 /* parallel round 3 */
492 HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
493 HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
494 HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
495 HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
496 HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
497 HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
498 HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
499 HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
500 HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
501 HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
502 HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
503 HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
504 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
505 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
506 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
507 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
508
509 /* parallel round 4 */
510 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
511 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
512 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
513 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
514 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
515 GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
516 GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
517 GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
518 GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
519 GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
520 GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
521 GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
522 GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
523 GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
524 GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
525 GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
526
527 /* parallel round 5 */
528 FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
529 FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
530 FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
531 FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
532 FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
533 FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
534 FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
535 FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
536 FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
537 FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
538 FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
539 FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
540 FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
541 FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
542 FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
543 FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
544
545 /* combine results */
546 ddd += cc + md->state[1]; /* final result for md->state[0] */
547 md->state[1] = md->state[2] + dd + eee;
548 md->state[2] = md->state[3] + ee + aaa;
549 md->state[3] = md->state[4] + aa + bbb;
550 md->state[4] = md->state[0] + bb + ccc;
551 md->state[0] = ddd;
552
553 return 0;
554}
555
556/**
557 Initialize the hash state
558 @param md The hash state you wish to initialize
559 @return 0 if successful
560 */
561int rmd160_vinit(struct rmd160_vstate * md)
562{
563 md->state[0] = 0x67452301UL;
564 md->state[1] = 0xefcdab89UL;
565 md->state[2] = 0x98badcfeUL;
566 md->state[3] = 0x10325476UL;
567 md->state[4] = 0xc3d2e1f0UL;
568 md->curlen = 0;
569 md->length = 0;
570 return 0;
571}
572#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
573int func_name (struct rmd160_vstate * md, const unsigned char *in, unsigned long inlen) \
574{ \
575unsigned long n; \
576int err; \
577if (md->curlen > sizeof(md->buf)) { \
578return -1; \
579} \
580while (inlen > 0) { \
581if (md->curlen == 0 && inlen >= block_size) { \
582if ((err = compress_name (md, (unsigned char *)in)) != 0) { \
583return err; \
584} \
585md->length += block_size * 8; \
586in += block_size; \
587inlen -= block_size; \
588} else { \
589n = MIN(inlen, (block_size - md->curlen)); \
590memcpy(md->buf + md->curlen, in, (size_t)n); \
591md->curlen += n; \
592in += n; \
593inlen -= n; \
594if (md->curlen == block_size) { \
595if ((err = compress_name (md, md->buf)) != 0) { \
596return err; \
597} \
598md->length += 8*block_size; \
599md->curlen = 0; \
600} \
601} \
602} \
603return 0; \
604}
605
606/**
607 Process a block of memory though the hash
608 @param md The hash state
609 @param in The data to hash
610 @param inlen The length of the data (octets)
611 @return 0 if successful
612 */
613HASH_PROCESS(rmd160_vprocess, rmd160_vcompress, rmd160, 64)
614
615/**
616 Terminate the hash to get the digest
617 @param md The hash state
618 @param out [out] The destination of the hash (20 bytes)
619 @return 0 if successful
620 */
621int rmd160_vdone(struct rmd160_vstate * md, unsigned char *out)
622{
623 int i;
624 if (md->curlen >= sizeof(md->buf)) {
625 return -1;
626 }
627 /* increase the length of the message */
628 md->length += md->curlen * 8;
629
630 /* append the '1' bit */
631 md->buf[md->curlen++] = (unsigned char)0x80;
632
633 /* if the length is currently above 56 bytes we append zeros
634 * then compress. Then we can fall back to padding zeros and length
635 * encoding like normal.
636 */
637 if (md->curlen > 56) {
638 while (md->curlen < 64) {
639 md->buf[md->curlen++] = (unsigned char)0;
640 }
641 rmd160_vcompress(md, md->buf);
642 md->curlen = 0;
643 }
644 /* pad upto 56 bytes of zeroes */
645 while (md->curlen < 56) {
646 md->buf[md->curlen++] = (unsigned char)0;
647 }
648 /* store length */
649 STORE64L(md->length, md->buf+56);
650 rmd160_vcompress(md, md->buf);
651 /* copy output */
652 for (i = 0; i < 5; i++) {
653 STORE32L(md->state[i], out+(4*i));
654 }
655 return 0;
656}
657
658void calc_rmd160(char deprecated[41],uint8_t buf[20],uint8_t *msg,int32_t len)
659{
660 struct rmd160_vstate md;
661 rmd160_vinit(&md);
662 rmd160_vprocess(&md,msg,len);
663 rmd160_vdone(&md, buf);
664}
3eea72f2 665
1e81ccb7 666static const uint32_t crc32_tab[] = {
667 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
668 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
669 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
670 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
671 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
672 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
673 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
674 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
675 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
676 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
677 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
678 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
679 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
680 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
681 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
682 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
683 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
684 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
685 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
686 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
687 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
688 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
689 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
690 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
691 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
692 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
693 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
694 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
695 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
696 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
697 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
698 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
699 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
700 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
701 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
702 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
703 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
704 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
705 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
706 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
707 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
708 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
709 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
710};
711
712uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size)
713{
714 const uint8_t *p;
715
716 p = (const uint8_t *)buf;
717 crc = crc ^ ~0U;
718
719 while (size--)
720 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
721
722 return crc ^ ~0U;
723}
724
d019c447 725void calc_rmd160_sha256(uint8_t rmd160[20],uint8_t *data,int32_t datalen)
726{
727 bits256 hash;
728 vcalc_sha256(0,hash.bytes,data,datalen);
729 calc_rmd160(0,rmd160,hash.bytes,sizeof(hash));
730}
731
732int32_t bitcoin_addr2rmd160(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr)
733{
734 bits256 hash; uint8_t *buf,_buf[25]; int32_t len;
735 memset(rmd160,0,20);
736 *addrtypep = 0;
737 buf = _buf;
738 if ( (len= bitcoin_base58decode(buf,coinaddr)) >= 4 )
739 {
740 // validate with trailing hash, then remove hash
741 hash = bits256_doublesha256(0,buf,21);
742 *addrtypep = *buf;
743 memcpy(rmd160,buf+1,20);
744 if ( (buf[21]&0xff) == hash.bytes[31] && (buf[22]&0xff) == hash.bytes[30] &&(buf[23]&0xff) == hash.bytes[29] && (buf[24]&0xff) == hash.bytes[28] )
745 {
746 //printf("coinaddr.(%s) valid checksum addrtype.%02x\n",coinaddr,*addrtypep);
747 return(20);
748 }
749 else
750 {
751 int32_t i;
752 if ( len > 20 )
753 {
754 hash = bits256_doublesha256(0,buf,len);
755 }
756 for (i=0; i<len; i++)
757 printf("%02x ",buf[i]);
7637aa7f 758 printf("\nhex checkhash.(%s) len.%d mismatch %02x %02x %02x %02x vs %02x %02x %02x %02x\n",coinaddr,len,buf[len-1]&0xff,buf[len-2]&0xff,buf[len-3]&0xff,buf[len-4]&0xff,hash.bytes[31],hash.bytes[30],hash.bytes[29],hash.bytes[28]);
d019c447 759 }
760 }
761 return(0);
762}
763
764char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len)
765{
766 int32_t i; uint8_t data[25]; bits256 hash;// char checkaddr[65];
767 if ( len != 20 )
768 calc_rmd160_sha256(data+1,pubkey_or_rmd160,len);
769 else memcpy(data+1,pubkey_or_rmd160,20);
770 //btc_convrmd160(checkaddr,addrtype,data+1);
771 data[0] = addrtype;
772 hash = bits256_doublesha256(0,data,21);
773 for (i=0; i<4; i++)
774 data[21+i] = hash.bytes[31-i];
775 if ( (coinaddr= bitcoin_base58encode(coinaddr,data,25)) != 0 )
776 {
777 //uint8_t checktype,rmd160[20];
778 //bitcoin_addr2rmd160(&checktype,rmd160,coinaddr);
779 //if ( strcmp(checkaddr,coinaddr) != 0 )
780 // printf("checkaddr.(%s) vs coinaddr.(%s) %02x vs [%02x] memcmp.%d\n",checkaddr,coinaddr,addrtype,checktype,memcmp(rmd160,data+1,20));
781 }
782 return(coinaddr);
783}
784
1e81ccb7 785int32_t _unhex(char c)
786{
787 if ( c >= '0' && c <= '9' )
788 return(c - '0');
789 else if ( c >= 'a' && c <= 'f' )
790 return(c - 'a' + 10);
791 else if ( c >= 'A' && c <= 'F' )
792 return(c - 'A' + 10);
793 return(-1);
794}
795
796int32_t is_hexstr(char *str,int32_t n)
797{
798 int32_t i;
799 if ( str == 0 || str[0] == 0 )
800 return(0);
801 for (i=0; str[i]!=0; i++)
802 {
803 if ( n > 0 && i >= n )
804 break;
805 if ( _unhex(str[i]) < 0 )
806 break;
807 }
808 if ( n == 0 )
809 return(i);
810 return(i == n);
811}
812
813int32_t unhex(char c)
814{
815 int32_t hex;
816 if ( (hex= _unhex(c)) < 0 )
817 {
818 //printf("unhex: illegal hexchar.(%c)\n",c);
819 }
820 return(hex);
821}
822
823unsigned char _decode_hex(char *hex) { return((unhex(hex[0])<<4) | unhex(hex[1])); }
824
825int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex)
826{
827 int32_t adjust,i = 0;
828 //printf("decode.(%s)\n",hex);
829 if ( is_hexstr(hex,n) == 0 )
830 {
831 memset(bytes,0,n);
832 return(n);
833 }
834 if ( n == 0 || (hex[n*2+1] == 0 && hex[n*2] != 0) )
835 {
836 if ( n > 0 )
837 {
838 bytes[0] = unhex(hex[0]);
839 printf("decode_hex n.%d hex[0] (%c) -> %d hex.(%s) [n*2+1: %d] [n*2: %d %c] len.%ld\n",n,hex[0],bytes[0],hex,hex[n*2+1],hex[n*2],hex[n*2],(long)strlen(hex));
840 }
841 bytes++;
842 hex++;
843 adjust = 1;
844 } else adjust = 0;
845 if ( n > 0 )
846 {
847 for (i=0; i<n; i++)
848 bytes[i] = _decode_hex(&hex[i*2]);
849 }
850 //bytes[i] = 0;
851 return(n + adjust);
852}
853
32b1b443 854char hexbyte(int32_t c)
855{
856 c &= 0xf;
857 if ( c < 10 )
858 return('0'+c);
859 else if ( c < 16 )
860 return('a'+c-10);
861 else return(0);
862}
863
864int32_t init_hexbytes_noT(char *hexbytes,unsigned char *message,long len)
865{
866 int32_t i;
867 if ( len <= 0 )
868 {
869 hexbytes[0] = 0;
870 return(1);
871 }
872 for (i=0; i<len; i++)
873 {
874 hexbytes[i*2] = hexbyte((message[i]>>4) & 0xf);
875 hexbytes[i*2 + 1] = hexbyte(message[i] & 0xf);
876 //printf("i.%d (%02x) [%c%c]\n",i,message[i],hexbytes[i*2],hexbytes[i*2+1]);
877 }
878 hexbytes[len*2] = 0;
879 //printf("len.%ld\n",len*2+1);
880 return((int32_t)len*2+1);
881}
882
883char *bits256_str(char hexstr[65],bits256 x)
884{
885 init_hexbytes_noT(hexstr,x.bytes,sizeof(x));
886 return(hexstr);
887}
888
1e81ccb7 889int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp)
890{
891 int32_t i; uint64_t x;
892 if ( rwflag == 0 )
893 {
894 x = 0;
895 for (i=len-1; i>=0; i--)
896 {
897 x <<= 8;
898 x |= serialized[i];
899 }
900 switch ( len )
901 {
902 case 1: *(uint8_t *)endianedp = (uint8_t)x; break;
903 case 2: *(uint16_t *)endianedp = (uint16_t)x; break;
904 case 4: *(uint32_t *)endianedp = (uint32_t)x; break;
905 case 8: *(uint64_t *)endianedp = (uint64_t)x; break;
906 }
907 }
908 else
909 {
910 x = 0;
911 switch ( len )
912 {
913 case 1: x = *(uint8_t *)endianedp; break;
914 case 2: x = *(uint16_t *)endianedp; break;
915 case 4: x = *(uint32_t *)endianedp; break;
916 case 8: x = *(uint64_t *)endianedp; break;
917 }
918 for (i=0; i<len; i++,x >>= 8)
919 serialized[i] = (uint8_t)(x & 0xff);
920 }
921 return(len);
922}
923
924int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp)
925{
926 int32_t i;
927 if ( rwflag == 0 )
928 {
929 for (i=0; i<len; i++)
930 endianedp[i] = serialized[i];
931 }
932 else
933 {
934 for (i=0; i<len; i++)
935 serialized[i] = endianedp[i];
936 }
937 return(len);
938}
939
64bb0834 940int32_t komodo_scriptitemlen(int32_t *opretlenp,uint8_t *script)
941{
942 int32_t opretlen,len = 0;
943 if ( (opretlen= script[len++]) >= 0x4c )
944 {
945 if ( opretlen == 0x4c )
946 opretlen = script[len++];
947 else if ( opretlen == 0x4d )
948 {
949 opretlen = script[len++];
950 opretlen = (opretlen << 8) | script[len++];
951 }
952 }
953 *opretlenp = opretlen;
954 return(len);
955}
956
1e81ccb7 957int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_t opretlen)
958{
959 int32_t offset = 0;
960 script[offset++] = 0x6a;
961 opretlen++;
962 if ( opretlen >= 0x4c )
963 {
964 if ( opretlen > 0xff )
965 {
966 script[offset++] = 0x4d;
967 script[offset++] = opretlen & 0xff;
968 script[offset++] = (opretlen >> 8) & 0xff;
969 }
970 else
971 {
972 script[offset++] = 0x4c;
973 script[offset++] = opretlen;
974 }
975 } else script[offset++] = opretlen;
795751ec 976 script[offset++] = type; // covered by opretlen
977 memcpy(&script[offset],opret,opretlen-1);
978 return(offset + opretlen - 1);
1e81ccb7 979}
438d3f35 980
fd1799a6 981long _stripwhite(char *buf,int accept)
982{
983 int32_t i,j,c;
984 if ( buf == 0 || buf[0] == 0 )
985 return(0);
986 for (i=j=0; buf[i]!=0; i++)
987 {
988 buf[j] = c = buf[i];
989 if ( c == accept || (c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\b') )
990 j++;
991 }
992 buf[j] = 0;
993 return(j);
994}
995
2c36c760 996char *clonestr(char *str)
997{
998 char *clone;
999 if ( str == 0 || str[0] == 0 )
1000 {
1001 printf("warning cloning nullstr.%p\n",str);
1002#ifdef __APPLE__
1003 while ( 1 ) sleep(1);
1004#endif
1005 str = (char *)"<nullstr>";
1006 }
1007 clone = (char *)malloc(strlen(str)+16);
1008 strcpy(clone,str);
1009 return(clone);
1010}
1011
f3d5295c 1012int32_t safecopy(char *dest,char *src,long len)
1013{
1014 int32_t i = -1;
1015 if ( src != 0 && dest != 0 && src != dest )
1016 {
1017 if ( dest != 0 )
1018 memset(dest,0,len);
1019 for (i=0; i<len&&src[i]!=0; i++)
1020 dest[i] = src[i];
1021 if ( i == len )
1022 {
1023 printf("safecopy: %s too long %ld\n",src,len);
1024#ifdef __APPLE__
1025 //getchar();
1026#endif
1027 return(-1);
1028 }
1029 dest[i] = 0;
1030 }
1031 return(i);
1032}
1033
fd1799a6 1034char *parse_conf_line(char *line,char *field)
1035{
1036 line += strlen(field);
1037 for (; *line!='='&&*line!=0; line++)
1038 break;
1039 if ( *line == 0 )
1040 return(0);
1041 if ( *line == '=' )
1042 line++;
83c2d095 1043 while ( line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n' || line[strlen(line)-1] == ' ' )
5ae7efc1 1044 line[strlen(line)-1] = 0;
e78fb5d3 1045 //printf("LINE.(%s)\n",line);
fd1799a6 1046 _stripwhite(line,0);
1047 return(clonestr(line));
1048}
1049
438d3f35 1050void komodo_configfile(char *symbol,uint16_t port)
1051{
62b751ab 1052 FILE *fp; char fname[512],buf[128],line[4096],*str,*rpcuser,*rpcpassword;
1e2b1c77 1053 srand((uint32_t)time(NULL));
438d3f35 1054 sprintf(buf,"%s.conf",symbol);
fd1799a6 1055 BITCOIND_PORT = port;
438d3f35 1056#ifdef WIN32
1057 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),buf);
1058#else
1059 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),buf);
1060#endif
1061 if ( (fp= fopen(fname,"rb")) == 0 )
1062 {
1063 if ( (fp= fopen(fname,"wb")) != 0 )
1064 {
c0160cc3 1065 fprintf(fp,"rpcuser=user%u\nrpcpassword=pass%u\nrpcport=%u\nserver=1\ntxindex=1\n",rand(),rand(),port);
438d3f35 1066 fclose(fp);
1067 printf("Created (%s)\n",fname);
1068 }
1069 }
fd1799a6 1070 else
1071 {
fd1799a6 1072 fclose(fp);
46ff7513 1073 strcpy(fname,GetDataDir(false).string().c_str());
1074#ifdef WIN32
1075 while ( fname[strlen(fname)-1] != '\\' )
1076 fname[strlen(fname)-1] = 0;
1077 strcat(fname,".komodo/komodo.conf");
1078#else
1079 while ( fname[strlen(fname)-1] != '/' )
1080 fname[strlen(fname)-1] = 0;
1081 strcat(fname,".komodo/komodo.conf");
1082#endif
62b751ab 1083 printf("KOMODO.(%s)\n",fname);
46ff7513 1084 if ( (fp= fopen(fname,"rb")) != 0 )
1085 {
62b751ab 1086 rpcuser = rpcpassword = 0;
46ff7513 1087 while ( fgets(line,sizeof(line),fp) != 0 )
1088 {
1089 if ( line[0] == '#' )
1090 continue;
1091 //printf("line.(%s) %p %p\n",line,strstr(line,(char *)"rpcuser"),strstr(line,(char *)"rpcpassword"));
1092 if ( (str= strstr(line,(char *)"rpcuser")) != 0 )
1093 rpcuser = parse_conf_line(str,(char *)"rpcuser");
1094 else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 )
1095 rpcpassword = parse_conf_line(str,(char *)"rpcpassword");
46ff7513 1096 }
1097 if ( rpcuser != 0 && rpcpassword != 0 )
1098 {
d354612f 1099 sprintf(KMDUSERPASS,"%s:%s",rpcuser,rpcpassword);
46ff7513 1100 }
d354612f 1101 printf("rpcuser.(%s) rpcpassword.(%s) KMDUSERPASS.(%s) %u\n",rpcuser,rpcpassword,KMDUSERPASS,port);
46ff7513 1102 if ( rpcuser != 0 )
1103 free(rpcuser);
1104 if ( rpcpassword != 0 )
1105 free(rpcpassword);
1106 fclose(fp);
1107 } else printf("couldnt open.(%s)\n",fname);
fd1799a6 1108 }
438d3f35 1109}
62e09770 1110
1111double OS_milliseconds()
1112{
1113 struct timeval tv; double millis;
1114 gettimeofday(&tv,NULL);
1115 millis = ((double)tv.tv_sec * 1000. + (double)tv.tv_usec / 1000.);
1116 //printf("tv_sec.%ld usec.%d %f\n",tv.tv_sec,tv.tv_usec,millis);
1117 return(millis);
429dabb5 1118}
1119
1120void lock_queue(queue_t *queue)
1121{
1122 if ( queue->initflag == 0 )
1123 {
1124 portable_mutex_init(&queue->mutex);
1125 queue->initflag = 1;
1126 }
1127 portable_mutex_lock(&queue->mutex);
1128}
1129
5fa92ab2 1130void queue_enqueue(char *name,queue_t *queue,struct queueitem *item)
429dabb5 1131{
429dabb5 1132 if ( queue->name[0] == 0 && name != 0 && name[0] != 0 )
5fa92ab2 1133 strcpy(queue->name,name);
1134 if ( item == 0 )
429dabb5 1135 {
5fa92ab2 1136 printf("FATAL type error: queueing empty value\n");
429dabb5 1137 return;
1138 }
429dabb5 1139 lock_queue(queue);
429dabb5 1140 DL_APPEND(queue->list,item);
1141 portable_mutex_unlock(&queue->mutex);
429dabb5 1142}
1143
5fa92ab2 1144struct queueitem *queue_dequeue(queue_t *queue)
429dabb5 1145{
1146 struct queueitem *item = 0;
1147 lock_queue(queue);
1148 if ( queue->list != 0 )
1149 {
1150 item = queue->list;
429dabb5 1151 DL_DELETE(queue->list,item);
1152 }
1153 portable_mutex_unlock(&queue->mutex);
5fa92ab2 1154 return(item);
429dabb5 1155}
1156
405e2bdd 1157void *queue_delete(queue_t *queue,struct queueitem *copy,int32_t copysize)
429dabb5 1158{
429dabb5 1159 struct queueitem *item = 0;
1160 lock_queue(queue);
1161 if ( queue->list != 0 )
1162 {
1163 DL_FOREACH(queue->list,item)
1164 {
5fa92ab2 1165 if ( item == copy || (item->allocsize == copysize && memcmp((void *)((long)item + sizeof(struct queueitem)),(void *)((long)copy + sizeof(struct queueitem)),copysize) == 0) )
429dabb5 1166 {
1167 DL_DELETE(queue->list,item);
1168 portable_mutex_unlock(&queue->mutex);
5fa92ab2 1169 printf("name.(%s) deleted item.%p list.%p\n",queue->name,item,queue->list);
429dabb5 1170 return(item);
1171 }
1172 }
1173 }
1174 portable_mutex_unlock(&queue->mutex);
1175 return(0);
1176}
1177
1178void *queue_free(queue_t *queue)
1179{
1180 struct queueitem *item = 0;
1181 lock_queue(queue);
1182 if ( queue->list != 0 )
1183 {
1184 DL_FOREACH(queue->list,item)
1185 {
1186 DL_DELETE(queue->list,item);
5fa92ab2 1187 free(item);
429dabb5 1188 }
1189 //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1190 }
1191 portable_mutex_unlock(&queue->mutex);
1192 return(0);
1193}
1194
1195void *queue_clone(queue_t *clone,queue_t *queue,int32_t size)
1196{
1197 struct queueitem *ptr,*item = 0;
1198 lock_queue(queue);
1199 if ( queue->list != 0 )
1200 {
1201 DL_FOREACH(queue->list,item)
1202 {
5fa92ab2 1203 ptr = (struct queueitem *)calloc(1,sizeof(*ptr));
429dabb5 1204 memcpy(ptr,item,size);
a856c56a 1205 queue_enqueue(queue->name,clone,ptr);
429dabb5 1206 }
1207 //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1208 }
1209 portable_mutex_unlock(&queue->mutex);
1210 return(0);
1211}
1212
1213int32_t queue_size(queue_t *queue)
1214{
1215 int32_t count = 0;
1216 struct queueitem *tmp;
1217 lock_queue(queue);
1218 DL_COUNT(queue->list,tmp,count);
1219 portable_mutex_unlock(&queue->mutex);
1220 return count;
1221}
a18fbdc5 1222
1223void iguana_initQ(queue_t *Q,char *name)
1224{
5fa92ab2 1225 struct queueitem *item,I;
a18fbdc5 1226 memset(Q,0,sizeof(*Q));
5fa92ab2 1227 memset(&I,0,sizeof(I));
a18fbdc5 1228 strcpy(Q->name,name);
5fa92ab2 1229 queue_enqueue(name,Q,&I);
1230 if ( (item= queue_dequeue(Q)) != 0 )
1231 free(item);
a18fbdc5 1232}
This page took 0.180994 seconds and 4 git commands to generate.