· 6 years ago · Oct 05, 2019, 08:04 PM
1/*
2 * Vanitygen, vanity bitcoin address generator
3 * Copyright (C) 2011 <samr7@cs.washington.edu>
4 *
5 * Vanitygen is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * Vanitygen is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Vanitygen. If not, see <http://www.gnu.org/licenses/>.
17 */
18#ifndef _GNU_SOURCE
19#define _GNU_SOURCE 1
20#endif
21#include <stdio.h>
22#include <string.h>
23#include <math.h>
24#include <assert.h>
25#include <stdlib.h>
26#include <pthread.h>
27#include <unistd.h>
28#include <sqlite3.h>
29#include <curses.h>
30#include <openssl/sha.h>
31#include <openssl/ripemd.h>
32#include <openssl/bn.h>
33#include <openssl/ec.h>
34#include <openssl/obj_mac.h>
35#include <time.h>
36#include <sys/stat.h>
37
38#include <pcre.h>
39#define gotoxy(x,y) printf("\033[%d;%dH", (x), (y))
40#include "pattern.h"
41#include "util.h"
42#include "avl.h"
43// init for db vars
44int i;
45int t;
46int x;
47int accumulator;
48int total_accumulated;
49int n;
50int vi; //values inserted
51int tp; //total processed per file
52int remaining;
53char *wallet[10000000];
54char *privkey[10000000];
55char *db_name[5000];
56int db_inc=0;
57int db_max;
58int processed;
59const int db_maxsize = 1000000; //max number of rows
60int cfileexists(const char* filename){
61 struct stat buffer;
62 int exist = stat(filename,&buffer);
63 if(exist == 0)
64 return 1;
65 else // -1
66 return 0;
67}
68void* sqlCompare(void *arg) {
69printf("Thread started\n");
70 int rc;
71 int rc2;
72 char *zErrMsg = NULL;
73 char db_name;
74 sqlite3 *db;
75 sqlite3 *db2;
76 char *query = NULL;
77
78 sqlite3_stmt *stmt;
79
80 rc = sqlite3_open("wal.db", &db);
81
82 if (rc) {
83 gotoxy(7, 5);
84 fprintf(stderr, "Can't open main database: %s", sqlite3_errmsg(db));
85 sqlite3_close(db);
86 exit(EXIT_FAILURE);
87 } else {
88 gotoxy(8, 5);
89 //if (sqlite3_exec(db, "SELECT ;", NULL, NULL, NULL);)
90 fprintf(stdout, "Opened main database successfully");
91 }
92 get_db:
93 //asprintf(&query, "ATTACH '%ddatabase.db' as 'generated'",db_inc);
94 rc2 = sqlite3_exec(db2 , "0database.db", NULL, NULL, &zErrMsg);
95 if (rc2 != SQLITE_OK) {
96 gotoxy(9, 5);
97 fprintf(stderr, "Can't open database: %d", rc2);
98 fprintf(stderr, "Can't open database: %s", query);
99 db_inc++;
100 //goto get_db;
101 sqlite3_close(db);
102 exit(EXIT_FAILURE);
103 } else {
104 gotoxy(10, 5);
105 //if (sqlite3_exec(db, "SELECT ;", NULL, NULL, NULL);)
106 fprintf(stdout, "Opened database successfully");
107 }
108 sqlite3_close(db);
109 sqlite3_close(db2);
110
111 return NULL;
112
113}
114void* sqlInsert(void *arg)
115{
116 system("clear");
117 pthread_t tid[db_inc];
118 gotoxy(1, 3);
119 printf("Thread start processing");
120 sqlite3_stmt *stmt;
121 int rc;
122 char *zErrMsg = NULL;
123 char *db_name[db_inc];
124 sqlite3 *db;
125 char *query = NULL;
126 db_exists:
127 //dbfull: //database is full, get new name.
128
129 sprintf(&db_name, "%ddatabase.db",db_inc);
130 char* filename = &db_name;
131 int exist = cfileexists(filename);
132 if(exist){
133 gotoxy(2, 3);
134 printf("File %s exists, incrementing name...",filename);
135
136
137 db_inc++;
138 goto db_exists;
139 }
140 else
141 {
142 gotoxy(3, 3);
143 printf("File %s does not exist. Creating database...",filename);
144
145
146 }
147
148 /* Open database btc_dataBase.db*/
149
150
151 rc = sqlite3_open_v2(db_name, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_CONFIG_MULTITHREAD, NULL);
152 if (rc) {
153 gotoxy(4, 3);
154 fprintf(stderr, "Can't open database: %s", sqlite3_errmsg(db));
155 sqlite3_close(db);
156 exit(EXIT_FAILURE);
157 } else {
158
159 gotoxy(5, 3);
160 //if (sqlite3_exec(db, "SELECT ;", NULL, NULL, NULL);)
161 fprintf(stdout, "Opened database successfully");
162 }
163 sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS BITCOIN (ADDRESS TEXT, PRIVKEY TEXT)", NULL, NULL, NULL);
164 sqlite3_exec(db, "BEGIN EXCLUSIVE TRANSACTION;", NULL, NULL, NULL);
165 /* Insert generated address into database */
166 //Begin loop to mass insert our accumulated keys
167 for(n=0;n<=vi;n++) {
168 sqlite3_mutex_enter(sqlite3_db_mutex(db)); // Get our own connection
169 //sqlite3_busy_timeout(db, 150); // incase we get locked, slow it down.
170 //asprintf(&query, "INSERT INTO BITCOIN (ADDRESS, PRIVKEY) VALUES('%s','%s')",wallet[n], privkey[n]); //whatevs primal
171 asprintf(&query, "INSERT INTO BITCOIN (ADDRESS, PRIVKEY) VALUES('%s','%s')",wallet[n], privkey[n]); //whatevs primal
172 //free(wallet[n]);
173 //free(privkey[n]);
174 sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); // <---I really want to use this (I finally am!)
175
176 /* Execute Insert */
177 rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg); //do the execution
178 if (rc != SQLITE_OK) {
179 gotoxy(6, 40);
180 printf("Err %s ",sqlite3_errmsg(db));
181 goto out;
182 } else {
183 processed++; // increase processed because it was a success.
184 remaining = vi - processed;
185 gotoxy(6, 6);
186 printf("Inserting records");
187 gotoxy(7, 6);
188 fprintf(stdout, "Total: %d Processed: %d", vi, processed);
189 fflush( stdout );
190 }
191 sqlite3_finalize(stmt);
192 out: // in case of error
193 free(query); //free the memory
194 sqlite3_mutex_leave(sqlite3_db_mutex(db));
195
196 } //end for
197
198 sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, NULL, NULL);
199 sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);
200
201 if (rc != SQLITE_OK) {
202 fprintf(stderr, "INSERT SQL error: %s", zErrMsg);
203 sqlite3_free(zErrMsg);
204 }
205 i = 0; //reset i for next write
206 sqlite3_close(db);
207 gotoxy(8, 3);
208
209 printf("done writing database");
210 if (tp >= db_maxsize) { //Let's change the database name now.
211 gotoxy(9, 3);
212 printf("Database filled, changing filenames");
213 db_inc++;
214 tp = 0;
215 }
216 gotoxy(10, 3);
217 printf("Total processed: %d", total_accumulated);
218 processed = 0;
219 gotoxy(11, 3);
220 FILE *fout; // out file
221 fout = fopen("cmp.sh","w");
222 if(fout == NULL) { //error opening file
223 printf("Error opening file.");
224 };
225 // write to file
226
227 fprintf(fout,"attach 'wal.db' as 'db1';\n");
228 fprintf(fout,"attach '%s' as 'db2';\n",filename);
229 fprintf(fout,"select ADDRESS from db2.BITCOIN limit 0 offset 1;\n");
230 fprintf(fout,"select a.ADDRESS, b.PRIVKEY, a.balance from db1.BITCOIN a JOIN db2.BITCOIN b on a.ADDRESS = b.ADDRESS;\n");
231 // after you are done with the file
232 fflush(fout);
233 fclose(fout);
234 gotoxy(12, 6);
235 printf("Starting comparison of %s against the main database.",filename);
236 sprintf(query, "sqlite3 < cmp.sh | tee %s.txt", filename);
237 system(query);
238
239
240 gotoxy(13, 6);
241
242 printf("%s done processing , writing output.",filename);
243
244
245 pthread_exit(NULL);
246 return NULL;
247}
248
249/*
250 * Common code for execution helper
251 */
252
253EC_KEY *
254vg_exec_context_new_key(void)
255{
256 system("clear");
257 return EC_KEY_new_by_curve_name(NID_secp256k1);
258}
259
260/*
261 * Thread synchronization helpers
262 */
263
264static pthread_mutex_t vg_thread_lock = PTHREAD_MUTEX_INITIALIZER;
265static pthread_cond_t vg_thread_rdcond = PTHREAD_COND_INITIALIZER;
266static pthread_cond_t vg_thread_wrcond = PTHREAD_COND_INITIALIZER;
267static pthread_cond_t vg_thread_upcond = PTHREAD_COND_INITIALIZER;
268
269static void
270__vg_exec_context_yield(vg_exec_context_t *vxcp)
271{
272 vxcp->vxc_lockmode = 0;
273 while (vxcp->vxc_vc->vc_thread_excl) {
274 if (vxcp->vxc_stop) {
275 assert(vxcp->vxc_vc->vc_thread_excl);
276 vxcp->vxc_stop = 0;
277 pthread_cond_signal(&vg_thread_upcond);
278 }
279 pthread_cond_wait(&vg_thread_rdcond, &vg_thread_lock);
280 }
281 assert(!vxcp->vxc_stop);
282 assert(!vxcp->vxc_lockmode);
283 vxcp->vxc_lockmode = 1;
284}
285
286int
287vg_exec_context_upgrade_lock(vg_exec_context_t *vxcp)
288{
289 vg_exec_context_t *tp;
290 vg_context_t *vcp;
291
292 if (vxcp->vxc_lockmode == 2)
293 return 0;
294
295 pthread_mutex_lock(&vg_thread_lock);
296
297 assert(vxcp->vxc_lockmode == 1);
298 vxcp->vxc_lockmode = 0;
299 vcp = vxcp->vxc_vc;
300
301 if (vcp->vc_thread_excl++) {
302 assert(vxcp->vxc_stop);
303 vxcp->vxc_stop = 0;
304 pthread_cond_signal(&vg_thread_upcond);
305 pthread_cond_wait(&vg_thread_wrcond, &vg_thread_lock);
306
307 for (tp = vcp->vc_threads; tp != NULL; tp = tp->vxc_next) {
308 assert(!tp->vxc_lockmode);
309 assert(!tp->vxc_stop);
310 }
311
312 } else {
313 for (tp = vcp->vc_threads; tp != NULL; tp = tp->vxc_next) {
314 if (tp->vxc_lockmode) {
315 assert(tp->vxc_lockmode != 2);
316 tp->vxc_stop = 1;
317 }
318 }
319
320 do {
321 for (tp = vcp->vc_threads;
322 tp != NULL;
323 tp = tp->vxc_next) {
324 if (tp->vxc_lockmode) {
325 assert(tp->vxc_lockmode != 2);
326 pthread_cond_wait(&vg_thread_upcond,
327 &vg_thread_lock);
328 break;
329 }
330 }
331 } while (tp);
332 }
333
334 vxcp->vxc_lockmode = 2;
335 pthread_mutex_unlock(&vg_thread_lock);
336 return 1;
337}
338
339void
340vg_exec_context_downgrade_lock(vg_exec_context_t *vxcp)
341{
342 pthread_mutex_lock(&vg_thread_lock);
343 assert(vxcp->vxc_lockmode == 2);
344 assert(!vxcp->vxc_stop);
345 if (!--vxcp->vxc_vc->vc_thread_excl) {
346 vxcp->vxc_lockmode = 1;
347 pthread_cond_broadcast(&vg_thread_rdcond);
348 pthread_mutex_unlock(&vg_thread_lock);
349 return;
350 }
351 pthread_cond_signal(&vg_thread_wrcond);
352 __vg_exec_context_yield(vxcp);
353 pthread_mutex_unlock(&vg_thread_lock);
354}
355
356int
357vg_exec_context_init(vg_context_t *vcp, vg_exec_context_t *vxcp)
358{
359 pthread_mutex_lock(&vg_thread_lock);
360
361 memset(vxcp, 0, sizeof(*vxcp));
362
363 vxcp->vxc_vc = vcp;
364
365 BN_init(&vxcp->vxc_bntarg);
366 BN_init(&vxcp->vxc_bnbase);
367 BN_init(&vxcp->vxc_bntmp);
368 BN_init(&vxcp->vxc_bntmp2);
369
370 BN_set_word(&vxcp->vxc_bnbase, 58);
371
372 vxcp->vxc_bnctx = BN_CTX_new();
373 assert(vxcp->vxc_bnctx);
374 vxcp->vxc_key = vg_exec_context_new_key();
375 assert(vxcp->vxc_key);
376 EC_KEY_precompute_mult(vxcp->vxc_key, vxcp->vxc_bnctx);
377
378 vxcp->vxc_lockmode = 0;
379 vxcp->vxc_stop = 0;
380
381 vxcp->vxc_next = vcp->vc_threads;
382 vcp->vc_threads = vxcp;
383 __vg_exec_context_yield(vxcp);
384 pthread_mutex_unlock(&vg_thread_lock);
385 return 1;
386}
387
388void
389vg_exec_context_del(vg_exec_context_t *vxcp)
390{
391 vg_exec_context_t *tp, **pprev;
392
393 if (vxcp->vxc_lockmode == 2)
394 vg_exec_context_downgrade_lock(vxcp);
395
396 pthread_mutex_lock(&vg_thread_lock);
397 assert(vxcp->vxc_lockmode == 1);
398 vxcp->vxc_lockmode = 0;
399
400 for (pprev = &vxcp->vxc_vc->vc_threads, tp = *pprev;
401 (tp != vxcp) && (tp != NULL);
402 pprev = &tp->vxc_next, tp = *pprev);
403
404 assert(tp == vxcp);
405 *pprev = tp->vxc_next;
406
407 if (tp->vxc_stop)
408 pthread_cond_signal(&vg_thread_upcond);
409
410 BN_clear_free(&vxcp->vxc_bntarg);
411 BN_clear_free(&vxcp->vxc_bnbase);
412 BN_clear_free(&vxcp->vxc_bntmp);
413 BN_clear_free(&vxcp->vxc_bntmp2);
414 BN_CTX_free(vxcp->vxc_bnctx);
415 vxcp->vxc_bnctx = NULL;
416 pthread_mutex_unlock(&vg_thread_lock);
417}
418
419void
420vg_exec_context_yield(vg_exec_context_t *vxcp)
421{
422 if (vxcp->vxc_lockmode == 2)
423 vg_exec_context_downgrade_lock(vxcp);
424
425 else if (vxcp->vxc_stop) {
426 assert(vxcp->vxc_lockmode == 1);
427 pthread_mutex_lock(&vg_thread_lock);
428 __vg_exec_context_yield(vxcp);
429 pthread_mutex_unlock(&vg_thread_lock);
430 }
431
432 assert(vxcp->vxc_lockmode == 1);
433}
434
435void
436vg_exec_context_consolidate_key(vg_exec_context_t *vxcp)
437{
438 if (vxcp->vxc_delta) {
439 BN_clear(&vxcp->vxc_bntmp);
440 BN_set_word(&vxcp->vxc_bntmp, vxcp->vxc_delta);
441 BN_add(&vxcp->vxc_bntmp2,
442 EC_KEY_get0_private_key(vxcp->vxc_key),
443 &vxcp->vxc_bntmp);
444 vg_set_privkey(&vxcp->vxc_bntmp2, vxcp->vxc_key);
445 vxcp->vxc_delta = 0;
446 }
447}
448
449void
450vg_exec_context_calc_address(vg_exec_context_t *vxcp)
451{
452 EC_POINT *pubkey;
453 const EC_GROUP *pgroup;
454 unsigned char eckey_buf[96], hash1[32], hash2[20];
455 int len;
456
457 vg_exec_context_consolidate_key(vxcp);
458 pgroup = EC_KEY_get0_group(vxcp->vxc_key);
459 pubkey = EC_POINT_new(pgroup);
460 EC_POINT_copy(pubkey, EC_KEY_get0_public_key(vxcp->vxc_key));
461 if (vxcp->vxc_vc->vc_pubkey_base) {
462 EC_POINT_add(pgroup,
463 pubkey,
464 pubkey,
465 vxcp->vxc_vc->vc_pubkey_base,
466 vxcp->vxc_bnctx);
467 }
468 len = EC_POINT_point2oct(pgroup,
469 pubkey,
470 POINT_CONVERSION_UNCOMPRESSED,
471 eckey_buf,
472 sizeof(eckey_buf),
473 vxcp->vxc_bnctx);
474 SHA256(eckey_buf, len, hash1);
475 RIPEMD160(hash1, sizeof(hash1), hash2);
476 memcpy(&vxcp->vxc_binres[1],
477 hash2, 20);
478 EC_POINT_free(pubkey);
479}
480
481enum {
482 timing_hist_size = 5
483};
484
485typedef struct _timing_info_s {
486 struct _timing_info_s *ti_next;
487 pthread_t ti_thread;
488 unsigned long ti_last_rate;
489
490 unsigned long long ti_hist_time[timing_hist_size];
491 unsigned long ti_hist_work[timing_hist_size];
492 int ti_hist_last;
493} timing_info_t;
494
495static pthread_mutex_t timing_mutex = PTHREAD_MUTEX_INITIALIZER;
496
497int
498vg_output_timing(vg_context_t *vcp, int cycle, struct timeval *last)
499{
500 pthread_t me;
501 struct timeval tvnow, tv;
502 timing_info_t *tip, *mytip;
503 unsigned long long rate, myrate = 0, mytime, total, sincelast;
504 int p, i;
505
506 /* Compute the rate */
507 gettimeofday(&tvnow, NULL);
508 timersub(&tvnow, last, &tv);
509 memcpy(last, &tvnow, sizeof(*last));
510 mytime = tv.tv_usec + (1000000ULL * tv.tv_sec);
511 if (!mytime)
512 mytime = 1;
513 rate = 0;
514
515 pthread_mutex_lock(&timing_mutex);
516 me = pthread_self();
517 for (tip = vcp->vc_timing_head, mytip = NULL;
518 tip != NULL; tip = tip->ti_next) {
519 if (pthread_equal(tip->ti_thread, me)) {
520 mytip = tip;
521 p = ((tip->ti_hist_last + 1) % timing_hist_size);
522 tip->ti_hist_time[p] = mytime;
523 tip->ti_hist_work[p] = cycle;
524 tip->ti_hist_last = p;
525
526 mytime = 0;
527 myrate = 0;
528 for (i = 0; i < timing_hist_size; i++) {
529 mytime += tip->ti_hist_time[i];
530 myrate += tip->ti_hist_work[i];
531 }
532 myrate = (myrate * 1000000) / mytime;
533 tip->ti_last_rate = myrate;
534 rate += myrate;
535
536 } else
537 rate += tip->ti_last_rate;
538 }
539 if (!mytip) {
540 mytip = (timing_info_t *) malloc(sizeof(*tip));
541 mytip->ti_next = vcp->vc_timing_head;
542 mytip->ti_thread = me;
543 vcp->vc_timing_head = mytip;
544 mytip->ti_hist_last = 0;
545 mytip->ti_hist_time[0] = mytime;
546 mytip->ti_hist_work[0] = cycle;
547 for (i = 1; i < timing_hist_size; i++) {
548 mytip->ti_hist_time[i] = 1;
549 mytip->ti_hist_work[i] = 0;
550 }
551 myrate = ((unsigned long long)cycle * 1000000) / mytime;
552 mytip->ti_last_rate = myrate;
553 rate += myrate;
554 }
555
556 vcp->vc_timing_total += cycle;
557 if (vcp->vc_timing_prevfound != vcp->vc_found) {
558 vcp->vc_timing_prevfound = vcp->vc_found;
559 vcp->vc_timing_sincelast = 0;
560 }
561 vcp->vc_timing_sincelast += cycle;
562
563 if (mytip != vcp->vc_timing_head) {
564 pthread_mutex_unlock(&timing_mutex);
565 return myrate;
566 }
567 total = vcp->vc_timing_total;
568 sincelast = vcp->vc_timing_sincelast;
569 pthread_mutex_unlock(&timing_mutex);
570
571 vcp->vc_output_timing(vcp, sincelast, rate, total);
572 return myrate;
573}
574
575void
576vg_context_thread_exit(vg_context_t *vcp)
577{
578 timing_info_t *tip, **ptip;
579 pthread_t me;
580
581 pthread_mutex_lock(&timing_mutex);
582 me = pthread_self();
583 for (ptip = &vcp->vc_timing_head, tip = *ptip;
584 tip != NULL;
585 ptip = &tip->ti_next, tip = *ptip) {
586 if (!pthread_equal(tip->ti_thread, me))
587 continue;
588 *ptip = tip->ti_next;
589 free(tip);
590 break;
591 }
592 pthread_mutex_unlock(&timing_mutex);
593
594}
595
596static void
597vg_timing_info_free(vg_context_t *vcp)
598{
599 timing_info_t *tp;
600 while (vcp->vc_timing_head != NULL) {
601 tp = vcp->vc_timing_head;
602 vcp->vc_timing_head = tp->ti_next;
603 free(tp);
604 }
605}
606
607void
608vg_output_timing_console(vg_context_t *vcp, double count,
609 unsigned long long rate, unsigned long long total)
610{
611 double prob, time, targ;
612 char *unit;
613 char linebuf[80];
614 int rem, p, i;
615
616 const double targs[] = { 0.5, 0.75, 0.8, 0.9, 0.95, 1.0 };
617
618 targ = rate;
619 unit = "key/s";
620 if (targ > 1000) {
621 unit = "Kkey/s";
622 targ /= 1000.0;
623 if (targ > 1000) {
624 unit = "Mkey/s";
625 targ /= 1000.0;
626 }
627 }
628
629 rem = sizeof(linebuf);
630 p = snprintf(linebuf, rem, "[%.2f %s][total %lld]",
631 targ, unit, total);
632 assert(p > 0);
633 rem -= p;
634 if (rem < 0)
635 rem = 0;
636
637 if (vcp->vc_chance >= 1.0) {
638 prob = 1.0f - exp(-count/vcp->vc_chance);
639
640 if (prob <= 0.999) {
641 p = snprintf(&linebuf[p], rem, "[Prob %.1f%%]",
642 prob * 100);
643 assert(p > 0);
644 rem -= p;
645 if (rem < 0)
646 rem = 0;
647 p = sizeof(linebuf) - rem;
648 }
649
650 for (i = 0; i < sizeof(targs)/sizeof(targs[0]); i++) {
651 targ = targs[i];
652 if ((targ < 1.0) && (prob <= targ))
653 break;
654 }
655
656 if (targ < 1.0) {
657 time = ((-vcp->vc_chance * log(1.0 - targ)) - count) /
658 rate;
659 unit = "s";
660 if (time > 60) {
661 time /= 60;
662 unit = "min";
663 if (time > 60) {
664 time /= 60;
665 unit = "h";
666 if (time > 24) {
667 time /= 24;
668 unit = "d";
669 if (time > 365) {
670 time /= 365;
671 unit = "y";
672 }
673 }
674 }
675 }
676
677 if (time > 1000000) {
678 p = snprintf(&linebuf[p], rem,
679 "[%d%% in %e%s]",
680 (int) (100 * targ), time, unit);
681 } else {
682 p = snprintf(&linebuf[p], rem,
683 "[%d%% in %.1f%s]",
684 (int) (100 * targ), time, unit);
685 }
686 assert(p > 0);
687 rem -= p;
688 if (rem < 0)
689 rem = 0;
690 p = sizeof(linebuf) - rem;
691 }
692 }
693
694 if (vcp->vc_found) {
695
696 gotoxy(15, 40);
697 if (vcp->vc_remove_on_match)
698
699 p = snprintf(&linebuf[p], rem, "[Found %lld/%ld]",
700 vcp->vc_found, vcp->vc_npatterns_start);
701 else
702 p = snprintf(&linebuf[p], rem, "[Found %lld]",
703 vcp->vc_found);
704
705 assert(p > 0);
706 rem -= p;
707 if (rem < 0)
708 rem = 0;
709 }
710
711 if (rem) {
712 memset(&linebuf[sizeof(linebuf)-rem], 0x20, rem);
713 linebuf[sizeof(linebuf)-1] = '\0';
714 }
715 printf("\r%s", linebuf);
716 fflush(stdout);
717}
718
719void
720vg_output_match_console(vg_context_t *vcp, EC_KEY *pkey, const char *pattern)
721{
722 //unsigne1d char key_buf[512], *pend;
723 char addr_buf[64], addr2_buf[64];
724 char privkey_buf[VG_PROTKEY_MAX_B58];
725 const char *keytype = "Privkey";
726 int len;
727 int isscript = (vcp->vc_format == VCF_SCRIPT);
728
729 EC_POINT *ppnt;
730 int free_ppnt = 0;
731 if (vcp->vc_pubkey_base) {
732 ppnt = EC_POINT_new(EC_KEY_get0_group(pkey));
733 EC_POINT_copy(ppnt, EC_KEY_get0_public_key(pkey));
734 EC_POINT_add(EC_KEY_get0_group(pkey),
735 ppnt,
736 ppnt,
737 vcp->vc_pubkey_base,
738 NULL);
739 free_ppnt = 1;
740 keytype = "PrivkeyPart";
741 } else {
742 ppnt = (EC_POINT *) EC_KEY_get0_public_key(pkey);
743 }
744
745 assert(EC_KEY_check_key(pkey));
746 vg_encode_address(ppnt,
747 EC_KEY_get0_group(pkey),
748 vcp->vc_pubkeytype, addr_buf);
749 if (isscript)
750 vg_encode_script_address(ppnt,
751 EC_KEY_get0_group(pkey),
752 vcp->vc_addrtype, addr2_buf);
753
754 if (vcp->vc_key_protect_pass) {
755 len = vg_protect_encode_privkey(privkey_buf,
756 pkey, vcp->vc_privtype,
757 VG_PROTKEY_DEFAULT,
758 vcp->vc_key_protect_pass);
759 if (len) {
760 keytype = "Protkey";
761 } else {
762 fprintf(stderr,
763 "ERROR: could not password-protect key\n");
764 vcp->vc_key_protect_pass = NULL;
765 }
766 }
767 if (!vcp->vc_key_protect_pass) {
768 vg_encode_privkey(pkey, vcp->vc_privtype, privkey_buf);
769 }
770
771 if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) {
772
773 }
774
775 if (vcp->vc_verbose > 0) {
776 if (vcp->vc_verbose > 1) {
777
778 }
779
780 }
781
782 if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) {
783
784
785 wallet[i]=(char*)malloc(35); // alloc memory for wallet
786 strcpy(wallet[i], addr_buf); // copy the wallet to array
787 privkey[i]=(char*)malloc(100); // alloc memory for privkey
788 strcpy(privkey[i], privkey_buf); // copy the wallet to array
789
790
791 i++;
792
793
794 db_max = db_maxsize + (db_maxsize - accumulator);
795
796 // go into db func
797 if (accumulator == db_max) {
798 total_accumulated = total_accumulated + accumulator;
799 vi = accumulator;
800 pthread_t tid[db_inc];
801 pthread_create(&tid[db_inc], NULL, sqlInsert, NULL);
802 db_inc++;
803 tp = tp + i;
804 accumulator = 0;
805 i = 0;
806 }
807 }
808
809 if (vcp->vc_result_file) {
810 FILE *fp = fopen(vcp->vc_result_file, "a");
811 if (!fp) {
812 fprintf(stderr,
813 "ERROR: could not open result file: %s\n",
814 strerror(errno));
815 } else {
816
817 if (isscript)
818 fprintf(fp, "P2SHAddress: %s\n", addr2_buf);
819 fprintf(fp,
820 "Address: %s\n"
821 "%s: %s\n",
822 addr_buf, keytype, privkey_buf);
823 fclose(fp);
824 }
825 }
826 if (free_ppnt)
827 EC_POINT_free(ppnt);
828}
829
830
831void
832vg_context_free(vg_context_t *vcp)
833{
834 vg_timing_info_free(vcp);
835 vcp->vc_free(vcp);
836}
837
838int
839vg_context_add_patterns(vg_context_t *vcp,
840 const char ** const patterns, int npatterns)
841{
842 vcp->vc_pattern_generation++;
843 return vcp->vc_add_patterns(vcp, patterns, npatterns);
844}
845
846void
847vg_context_clear_all_patterns(vg_context_t *vcp)
848{
849 vcp->vc_clear_all_patterns(vcp);
850 vcp->vc_pattern_generation++;
851}
852
853int
854vg_context_hash160_sort(vg_context_t *vcp, void *buf)
855{
856 if (!vcp->vc_hash160_sort)
857 return 0;
858 return vcp->vc_hash160_sort(vcp, buf);
859}
860
861int
862vg_context_start_threads(vg_context_t *vcp)
863{
864 vg_exec_context_t *vxcp;
865 int res;
866
867 for (vxcp = vcp->vc_threads; vxcp != NULL; vxcp = vxcp->vxc_next) {
868 res = pthread_create((pthread_t *) &vxcp->vxc_pthread,
869 NULL,
870 (void *(*)(void *)) vxcp->vxc_threadfunc,
871 vxcp);
872 if (res) {
873 fprintf(stderr, "ERROR: could not create thread: %d\n",
874 res);
875 vg_context_stop_threads(vcp);
876 return -1;
877 }
878 vxcp->vxc_thread_active = 1;
879 }
880 return 0;
881}
882
883void
884vg_context_stop_threads(vg_context_t *vcp)
885{
886 vcp->vc_halt = 1;
887 vg_context_wait_for_completion(vcp);
888 vcp->vc_halt = 0;
889}
890
891void
892vg_context_wait_for_completion(vg_context_t *vcp)
893{
894 vg_exec_context_t *vxcp;
895
896 for (vxcp = vcp->vc_threads; vxcp != NULL; vxcp = vxcp->vxc_next) {
897 if (!vxcp->vxc_thread_active)
898 continue;
899 pthread_join((pthread_t) vxcp->vxc_pthread, NULL);
900 vxcp->vxc_thread_active = 0;
901 }
902}
903
904
905/*
906 * Find the bignum ranges that produce a given prefix.
907 */
908static int
909get_prefix_ranges(int addrtype, const char *pfx, BIGNUM **result,
910 BN_CTX *bnctx)
911{
912 int i, p, c;
913 int zero_prefix = 0;
914 int check_upper = 0;
915 int b58pow, b58ceil, b58top = 0;
916 int ret = -1;
917
918 BIGNUM bntarg, bnceil, bnfloor;
919 BIGNUM bnbase;
920 BIGNUM *bnap, *bnbp, *bntp;
921 BIGNUM *bnhigh = NULL, *bnlow = NULL, *bnhigh2 = NULL, *bnlow2 = NULL;
922 BIGNUM bntmp, bntmp2;
923
924 BN_init(&bntarg);
925 BN_init(&bnceil);
926 BN_init(&bnfloor);
927 BN_init(&bnbase);
928 BN_init(&bntmp);
929 BN_init(&bntmp2);
930
931 BN_set_word(&bnbase, 58);
932
933 p = strlen(pfx);
934
935 for (i = 0; i < p; i++) {
936 c = vg_b58_reverse_map[(int)pfx[i]];
937 if (c == -1) {
938 fprintf(stderr,
939 "Invalid character '%c' in prefix '%s'\n",
940 pfx[i], pfx);
941 goto out;
942 }
943 if (i == zero_prefix) {
944 if (c == 0) {
945 /* Add another zero prefix */
946 zero_prefix++;
947 if (zero_prefix > 19) {
948 fprintf(stderr,
949 "Prefix '%s' is too long\n",
950 pfx);
951 goto out;
952 }
953 continue;
954 }
955
956 /* First non-zero character */
957 b58top = c;
958 BN_set_word(&bntarg, c);
959
960 } else {
961 BN_set_word(&bntmp2, c);
962 BN_mul(&bntmp, &bntarg, &bnbase, bnctx);
963 BN_add(&bntarg, &bntmp, &bntmp2);
964 }
965 }
966
967 /* Power-of-two ceiling and floor values based on leading 1s */
968 BN_clear(&bntmp);
969 BN_set_bit(&bntmp, 200 - (zero_prefix * 8));
970 BN_sub(&bnceil, &bntmp, BN_value_one());
971 BN_set_bit(&bnfloor, 192 - (zero_prefix * 8));
972
973 bnlow = BN_new();
974 bnhigh = BN_new();
975
976 if (b58top) {
977 /*
978 * If a non-zero was given in the prefix, find the
979 * numeric boundaries of the prefix.
980 */
981
982 BN_copy(&bntmp, &bnceil);
983 bnap = &bntmp;
984 bnbp = &bntmp2;
985 b58pow = 0;
986 while (BN_cmp(bnap, &bnbase) > 0) {
987 b58pow++;
988 BN_div(bnbp, NULL, bnap, &bnbase, bnctx);
989 bntp = bnap;
990 bnap = bnbp;
991 bnbp = bntp;
992 }
993 b58ceil = BN_get_word(bnap);
994
995 if ((b58pow - (p - zero_prefix)) < 6) {
996 /*
997 * Do not allow the prefix to constrain the
998 * check value, this is ridiculous.
999 */
1000 fprintf(stderr, "Prefix '%s' is too long\n", pfx);
1001 goto out;
1002 }
1003
1004 BN_set_word(&bntmp2, b58pow - (p - zero_prefix));
1005 BN_exp(&bntmp, &bnbase, &bntmp2, bnctx);
1006 BN_mul(bnlow, &bntmp, &bntarg, bnctx);
1007 BN_sub(&bntmp2, &bntmp, BN_value_one());
1008 BN_add(bnhigh, bnlow, &bntmp2);
1009
1010 if (b58top <= b58ceil) {
1011 /* Fill out the upper range too */
1012 check_upper = 1;
1013 bnlow2 = BN_new();
1014 bnhigh2 = BN_new();
1015
1016 BN_mul(bnlow2, bnlow, &bnbase, bnctx);
1017 BN_mul(&bntmp2, bnhigh, &bnbase, bnctx);
1018 BN_set_word(&bntmp, 57);
1019 BN_add(bnhigh2, &bntmp2, &bntmp);
1020
1021 /*
1022 * Addresses above the ceiling will have one
1023 * fewer "1" prefix in front than we require.
1024 */
1025 if (BN_cmp(&bnceil, bnlow2) < 0) {
1026 /* High prefix is above the ceiling */
1027 check_upper = 0;
1028 BN_free(bnhigh2);
1029 bnhigh2 = NULL;
1030 BN_free(bnlow2);
1031 bnlow2 = NULL;
1032 }
1033 else if (BN_cmp(&bnceil, bnhigh2) < 0)
1034 /* High prefix is partly above the ceiling */
1035 BN_copy(bnhigh2, &bnceil);
1036
1037 /*
1038 * Addresses below the floor will have another
1039 * "1" prefix in front instead of our target.
1040 */
1041 if (BN_cmp(&bnfloor, bnhigh) >= 0) {
1042 /* Low prefix is completely below the floor */
1043 assert(check_upper);
1044 check_upper = 0;
1045 BN_free(bnhigh);
1046 bnhigh = bnhigh2;
1047 bnhigh2 = NULL;
1048 BN_free(bnlow);
1049 bnlow = bnlow2;
1050 bnlow2 = NULL;
1051 }
1052 else if (BN_cmp(&bnfloor, bnlow) > 0) {
1053 /* Low prefix is partly below the floor */
1054 BN_copy(bnlow, &bnfloor);
1055 }
1056 }
1057
1058 } else {
1059 BN_copy(bnhigh, &bnceil);
1060 BN_clear(bnlow);
1061 }
1062
1063 /* Limit the prefix to the address type */
1064 BN_clear(&bntmp);
1065 BN_set_word(&bntmp, addrtype);
1066 BN_lshift(&bntmp2, &bntmp, 192);
1067
1068 if (check_upper) {
1069 if (BN_cmp(&bntmp2, bnhigh2) > 0) {
1070 check_upper = 0;
1071 BN_free(bnhigh2);
1072 bnhigh2 = NULL;
1073 BN_free(bnlow2);
1074 bnlow2 = NULL;
1075 }
1076 else if (BN_cmp(&bntmp2, bnlow2) > 0)
1077 BN_copy(bnlow2, &bntmp2);
1078 }
1079
1080 if (BN_cmp(&bntmp2, bnhigh) > 0) {
1081 if (!check_upper)
1082 goto not_possible;
1083 check_upper = 0;
1084 BN_free(bnhigh);
1085 bnhigh = bnhigh2;
1086 bnhigh2 = NULL;
1087 BN_free(bnlow);
1088 bnlow = bnlow2;
1089 bnlow2 = NULL;
1090 }
1091 else if (BN_cmp(&bntmp2, bnlow) > 0) {
1092 BN_copy(bnlow, &bntmp2);
1093 }
1094
1095 BN_set_word(&bntmp, addrtype + 1);
1096 BN_lshift(&bntmp2, &bntmp, 192);
1097
1098 if (check_upper) {
1099 if (BN_cmp(&bntmp2, bnlow2) < 0) {
1100 check_upper = 0;
1101 BN_free(bnhigh2);
1102 bnhigh2 = NULL;
1103 BN_free(bnlow2);
1104 bnlow2 = NULL;
1105 }
1106 else if (BN_cmp(&bntmp2, bnhigh2) < 0)
1107 BN_copy(bnlow2, &bntmp2);
1108 }
1109
1110 if (BN_cmp(&bntmp2, bnlow) < 0) {
1111 if (!check_upper)
1112 goto not_possible;
1113 check_upper = 0;
1114 BN_free(bnhigh);
1115 bnhigh = bnhigh2;
1116 bnhigh2 = NULL;
1117 BN_free(bnlow);
1118 bnlow = bnlow2;
1119 bnlow2 = NULL;
1120 }
1121 else if (BN_cmp(&bntmp2, bnhigh) < 0) {
1122 BN_copy(bnhigh, &bntmp2);
1123 }
1124
1125 /* Address ranges are complete */
1126 assert(check_upper || ((bnlow2 == NULL) && (bnhigh2 == NULL)));
1127 result[0] = bnlow;
1128 result[1] = bnhigh;
1129 result[2] = bnlow2;
1130 result[3] = bnhigh2;
1131 bnlow = NULL;
1132 bnhigh = NULL;
1133 bnlow2 = NULL;
1134 bnhigh2 = NULL;
1135 ret = 0;
1136
1137 if (0) {
1138 not_possible:
1139 ret = -2;
1140 }
1141
1142out:
1143 BN_clear_free(&bntarg);
1144 BN_clear_free(&bnceil);
1145 BN_clear_free(&bnfloor);
1146 BN_clear_free(&bnbase);
1147 BN_clear_free(&bntmp);
1148 BN_clear_free(&bntmp2);
1149 if (bnhigh)
1150 BN_free(bnhigh);
1151 if (bnlow)
1152 BN_free(bnlow);
1153 if (bnhigh2)
1154 BN_free(bnhigh2);
1155 if (bnlow2)
1156 BN_free(bnlow2);
1157
1158 return ret;
1159}
1160
1161static void
1162free_ranges(BIGNUM **ranges)
1163{
1164 BN_free(ranges[0]);
1165 BN_free(ranges[1]);
1166 ranges[0] = NULL;
1167 ranges[1] = NULL;
1168 if (ranges[2]) {
1169 BN_free(ranges[2]);
1170 BN_free(ranges[3]);
1171 ranges[2] = NULL;
1172 ranges[3] = NULL;
1173 }
1174}
1175
1176
1177/*
1178 * Address prefix AVL tree node
1179 */
1180
1181const int vpk_nwords = (25 + sizeof(BN_ULONG) - 1) / sizeof(BN_ULONG);
1182
1183typedef struct _vg_prefix_s {
1184 avl_item_t vp_item;
1185 struct _vg_prefix_s *vp_sibling;
1186 const char *vp_pattern;
1187 BIGNUM *vp_low;
1188 BIGNUM *vp_high;
1189} vg_prefix_t;
1190
1191static void
1192vg_prefix_free(vg_prefix_t *vp)
1193{
1194 if (vp->vp_low)
1195 BN_free(vp->vp_low);
1196 if (vp->vp_high)
1197 BN_free(vp->vp_high);
1198 free(vp);
1199}
1200
1201static vg_prefix_t *
1202vg_prefix_avl_search(avl_root_t *rootp, BIGNUM *targ)
1203{
1204 vg_prefix_t *vp;
1205 avl_item_t *itemp = rootp->ar_root;
1206
1207 while (itemp) {
1208 vp = avl_item_entry(itemp, vg_prefix_t, vp_item);
1209 if (BN_cmp(vp->vp_low, targ) > 0) {
1210 itemp = itemp->ai_left;
1211 } else {
1212 if (BN_cmp(vp->vp_high, targ) < 0) {
1213 itemp = itemp->ai_right;
1214 } else
1215 return vp;
1216 }
1217 }
1218 return NULL;
1219}
1220
1221static vg_prefix_t *
1222vg_prefix_avl_insert(avl_root_t *rootp, vg_prefix_t *vpnew)
1223{
1224 vg_prefix_t *vp;
1225 avl_item_t *itemp = NULL;
1226 avl_item_t **ptrp = &rootp->ar_root;
1227 while (*ptrp) {
1228 itemp = *ptrp;
1229 vp = avl_item_entry(itemp, vg_prefix_t, vp_item);
1230 if (BN_cmp(vp->vp_low, vpnew->vp_high) > 0) {
1231 ptrp = &itemp->ai_left;
1232 } else {
1233 if (BN_cmp(vp->vp_high, vpnew->vp_low) < 0) {
1234 ptrp = &itemp->ai_right;
1235 } else
1236 return vp;
1237 }
1238 }
1239 vpnew->vp_item.ai_up = itemp;
1240 itemp = &vpnew->vp_item;
1241 *ptrp = itemp;
1242 avl_insert_fix(rootp, itemp);
1243 return NULL;
1244}
1245
1246static vg_prefix_t *
1247vg_prefix_first(avl_root_t *rootp)
1248{
1249 avl_item_t *itemp;
1250 itemp = avl_first(rootp);
1251 if (itemp)
1252 return avl_item_entry(itemp, vg_prefix_t, vp_item);
1253 return NULL;
1254}
1255
1256static vg_prefix_t *
1257vg_prefix_next(vg_prefix_t *vp)
1258{
1259 avl_item_t *itemp = &vp->vp_item;
1260 itemp = avl_next(itemp);
1261 if (itemp)
1262 return avl_item_entry(itemp, vg_prefix_t, vp_item);
1263 return NULL;
1264}
1265
1266static vg_prefix_t *
1267vg_prefix_add(avl_root_t *rootp, const char *pattern, BIGNUM *low, BIGNUM *high)
1268{
1269 vg_prefix_t *vp, *vp2;
1270 assert(BN_cmp(low, high) < 0);
1271 vp = (vg_prefix_t *) malloc(sizeof(*vp));
1272 if (vp) {
1273 avl_item_init(&vp->vp_item);
1274 vp->vp_sibling = NULL;
1275 vp->vp_pattern = pattern;
1276 vp->vp_low = low;
1277 vp->vp_high = high;
1278 vp2 = vg_prefix_avl_insert(rootp, vp);
1279 if (vp2 != NULL) {
1280 fprintf(stderr,
1281 "Prefix '%s' ignored, overlaps '%s'\n",
1282 pattern, vp2->vp_pattern);
1283 vg_prefix_free(vp);
1284 vp = NULL;
1285 }
1286 }
1287 return vp;
1288}
1289
1290static void
1291vg_prefix_delete(avl_root_t *rootp, vg_prefix_t *vp)
1292{
1293 vg_prefix_t *sibp, *delp;
1294
1295 avl_remove(rootp, &vp->vp_item);
1296 sibp = vp->vp_sibling;
1297 while (sibp && sibp != vp) {
1298 avl_remove(rootp, &sibp->vp_item);
1299 delp = sibp;
1300 sibp = sibp->vp_sibling;
1301 vg_prefix_free(delp);
1302 }
1303 vg_prefix_free(vp);
1304}
1305
1306static vg_prefix_t *
1307vg_prefix_add_ranges(avl_root_t *rootp, const char *pattern, BIGNUM **ranges,
1308 vg_prefix_t *master)
1309{
1310 vg_prefix_t *vp, *vp2 = NULL;
1311
1312 assert(ranges[0]);
1313 vp = vg_prefix_add(rootp, pattern, ranges[0], ranges[1]);
1314 if (!vp)
1315 return NULL;
1316
1317 if (ranges[2]) {
1318 vp2 = vg_prefix_add(rootp, pattern, ranges[2], ranges[3]);
1319 if (!vp2) {
1320 vg_prefix_delete(rootp, vp);
1321 return NULL;
1322 }
1323 }
1324
1325 if (!master) {
1326 vp->vp_sibling = vp2;
1327 if (vp2)
1328 vp2->vp_sibling = vp;
1329 } else if (vp2) {
1330 vp->vp_sibling = vp2;
1331 vp2->vp_sibling = (master->vp_sibling ?
1332 master->vp_sibling :
1333 master);
1334 master->vp_sibling = vp;
1335 } else {
1336 vp->vp_sibling = (master->vp_sibling ?
1337 master->vp_sibling :
1338 master);
1339 master->vp_sibling = vp;
1340 }
1341 return vp;
1342}
1343
1344static void
1345vg_prefix_range_sum(vg_prefix_t *vp, BIGNUM *result, BIGNUM *tmp1)
1346{
1347 vg_prefix_t *startp;
1348
1349 startp = vp;
1350 BN_clear(result);
1351 do {
1352 BN_sub(tmp1, vp->vp_high, vp->vp_low);
1353 BN_add(result, result, tmp1);
1354 vp = vp->vp_sibling;
1355 } while (vp && (vp != startp));
1356}
1357
1358
1359typedef struct _prefix_case_iter_s {
1360 char ci_prefix[32];
1361 char ci_case_map[32];
1362 char ci_nbits;
1363 int ci_value;
1364} prefix_case_iter_t;
1365
1366static const unsigned char b58_case_map[256] = {
1367 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1368 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1369 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1371 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0, 1, 1, 2,
1372 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
1373 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 2, 1, 1, 0,
1374 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
1375};
1376
1377static int
1378prefix_case_iter_init(prefix_case_iter_t *cip, const char *pfx)
1379{
1380 int i;
1381
1382 cip->ci_nbits = 0;
1383 cip->ci_value = 0;
1384 for (i = 0; pfx[i]; i++) {
1385 if (i > sizeof(cip->ci_prefix))
1386 return 0;
1387 if (!b58_case_map[(int)pfx[i]]) {
1388 /* Character isn't case-swappable, ignore it */
1389 cip->ci_prefix[i] = pfx[i];
1390 continue;
1391 }
1392 if (b58_case_map[(int)pfx[i]] == 2) {
1393 /* Character invalid, but valid in swapped case */
1394 cip->ci_prefix[i] = pfx[i] ^ 0x20;
1395 continue;
1396 }
1397 /* Character is case-swappable */
1398 cip->ci_prefix[i] = pfx[i] | 0x20;
1399 cip->ci_case_map[(int)cip->ci_nbits] = i;
1400 cip->ci_nbits++;
1401 }
1402 cip->ci_prefix[i] = '\0';
1403 return 1;
1404}
1405
1406static int
1407prefix_case_iter_next(prefix_case_iter_t *cip)
1408{
1409 unsigned long val, max, mask;
1410 int i, nbits;
1411
1412 nbits = cip->ci_nbits;
1413 max = (1UL << nbits) - 1;
1414 val = cip->ci_value + 1;
1415 if (val > max)
1416 return 0;
1417
1418 for (i = 0, mask = 1; i < nbits; i++, mask <<= 1) {
1419 if (val & mask)
1420 cip->ci_prefix[(int)cip->ci_case_map[i]] &= 0xdf;
1421 else
1422 cip->ci_prefix[(int)cip->ci_case_map[i]] |= 0x20;
1423 }
1424 cip->ci_value = val;
1425 return 1;
1426}
1427
1428
1429typedef struct _vg_prefix_context_s {
1430 vg_context_t base;
1431 avl_root_t vcp_avlroot;
1432 BIGNUM vcp_difficulty;
1433 int vcp_caseinsensitive;
1434} vg_prefix_context_t;
1435
1436void
1437vg_prefix_context_set_case_insensitive(vg_context_t *vcp, int caseinsensitive)
1438{
1439 ((vg_prefix_context_t *) vcp)->vcp_caseinsensitive = caseinsensitive;
1440}
1441
1442static void
1443vg_prefix_context_clear_all_patterns(vg_context_t *vcp)
1444{
1445 vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1446 vg_prefix_t *vp;
1447 unsigned long npfx_left = 0;
1448
1449 while (!avl_root_empty(&vcpp->vcp_avlroot)) {
1450 vp = avl_item_entry(vcpp->vcp_avlroot.ar_root,
1451 vg_prefix_t, vp_item);
1452 vg_prefix_delete(&vcpp->vcp_avlroot, vp);
1453 npfx_left++;
1454 }
1455
1456 assert(npfx_left == vcpp->base.vc_npatterns);
1457 vcpp->base.vc_npatterns = 0;
1458 vcpp->base.vc_npatterns_start = 0;
1459 vcpp->base.vc_found = 0;
1460 BN_clear(&vcpp->vcp_difficulty);
1461}
1462
1463static void
1464vg_prefix_context_free(vg_context_t *vcp)
1465{
1466 vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1467 vg_prefix_context_clear_all_patterns(vcp);
1468 BN_clear_free(&vcpp->vcp_difficulty);
1469 free(vcpp);
1470}
1471
1472static void
1473vg_prefix_context_next_difficulty(vg_prefix_context_t *vcpp,
1474 BIGNUM *bntmp, BIGNUM *bntmp2, BN_CTX *bnctx)
1475{
1476 char *dbuf;
1477
1478 BN_clear(bntmp);
1479 BN_set_bit(bntmp, 192);
1480 BN_div(bntmp2, NULL, bntmp, &vcpp->vcp_difficulty, bnctx);
1481
1482 dbuf = BN_bn2dec(bntmp2);
1483 if (vcpp->base.vc_verbose > 0) {
1484 if (vcpp->base.vc_npatterns > 1)
1485 fprintf(stderr,
1486 "Next match difficulty: %s (%ld prefixes)\n",
1487 dbuf, vcpp->base.vc_npatterns);
1488 else
1489 fprintf(stderr, "Difficulty: %s\n", dbuf);
1490 }
1491 vcpp->base.vc_chance = atof(dbuf);
1492 OPENSSL_free(dbuf);
1493}
1494
1495static int
1496vg_prefix_context_add_patterns(vg_context_t *vcp,
1497 const char ** const patterns, int npatterns)
1498{
1499 vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1500 prefix_case_iter_t caseiter;
1501 vg_prefix_t *vp, *vp2;
1502 BN_CTX *bnctx;
1503 BIGNUM bntmp, bntmp2, bntmp3;
1504 BIGNUM *ranges[4];
1505 int ret = 0;
1506 int i, impossible = 0;
1507 int case_impossible;
1508 unsigned long npfx;
1509 char *dbuf;
1510
1511 bnctx = BN_CTX_new();
1512 BN_init(&bntmp);
1513 BN_init(&bntmp2);
1514 BN_init(&bntmp3);
1515
1516 npfx = 0;
1517 for (i = 0; i < npatterns; i++) {
1518 if (!vcpp->vcp_caseinsensitive) {
1519 vp = NULL;
1520 ret = get_prefix_ranges(vcpp->base.vc_addrtype,
1521 patterns[i],
1522 ranges, bnctx);
1523 if (!ret) {
1524 vp = vg_prefix_add_ranges(&vcpp->vcp_avlroot,
1525 patterns[i],
1526 ranges, NULL);
1527 }
1528
1529 } else {
1530 /* Case-enumerate the prefix */
1531 if (!prefix_case_iter_init(&caseiter, patterns[i])) {
1532 fprintf(stderr,
1533 "Prefix '%s' is too long\n",
1534 patterns[i]);
1535 continue;
1536 }
1537
1538 if (caseiter.ci_nbits > 16) {
1539 fprintf(stderr,
1540 "WARNING: Prefix '%s' has "
1541 "2^%d case-varied derivatives\n",
1542 patterns[i], caseiter.ci_nbits);
1543 }
1544
1545 case_impossible = 0;
1546 vp = NULL;
1547 do {
1548 ret = get_prefix_ranges(vcpp->base.vc_addrtype,
1549 caseiter.ci_prefix,
1550 ranges, bnctx);
1551 if (ret == -2) {
1552 case_impossible++;
1553 ret = 0;
1554 continue;
1555 }
1556 if (ret)
1557 break;
1558 vp2 = vg_prefix_add_ranges(&vcpp->vcp_avlroot,
1559 patterns[i],
1560 ranges,
1561 vp);
1562 if (!vp2) {
1563 ret = -1;
1564 break;
1565 }
1566 if (!vp)
1567 vp = vp2;
1568
1569 } while (prefix_case_iter_next(&caseiter));
1570
1571 if (!vp && case_impossible)
1572 ret = -2;
1573
1574 if (ret && vp) {
1575 vg_prefix_delete(&vcpp->vcp_avlroot, vp);
1576 vp = NULL;
1577 }
1578 }
1579
1580 if (ret == -2) {
1581 fprintf(stderr,
1582 "Prefix '%s' not possible\n", patterns[i]);
1583 impossible++;
1584 }
1585
1586 if (!vp)
1587 continue;
1588
1589 npfx++;
1590
1591 /* Determine the probability of finding a match */
1592 vg_prefix_range_sum(vp, &bntmp, &bntmp2);
1593 BN_add(&bntmp2, &vcpp->vcp_difficulty, &bntmp);
1594 BN_copy(&vcpp->vcp_difficulty, &bntmp2);
1595
1596 if (vcp->vc_verbose > 1) {
1597 BN_clear(&bntmp2);
1598 BN_set_bit(&bntmp2, 192);
1599 BN_div(&bntmp3, NULL, &bntmp2, &bntmp, bnctx);
1600
1601 dbuf = BN_bn2dec(&bntmp3);
1602 fprintf(stderr,
1603 "Prefix difficulty: %20s %s\n",
1604 dbuf, patterns[i]);
1605 OPENSSL_free(dbuf);
1606 }
1607 }
1608
1609 vcpp->base.vc_npatterns += npfx;
1610 vcpp->base.vc_npatterns_start += npfx;
1611
1612 if (!npfx && impossible) {
1613 const char *ats = "bitcoin", *bw = "\"1\"";
1614 switch (vcpp->base.vc_addrtype) {
1615 case 5:
1616 ats = "bitcoin script";
1617 bw = "\"3\"";
1618 break;
1619 case 111:
1620 ats = "testnet";
1621 bw = "\"m\" or \"n\"";
1622 break;
1623 case 52:
1624 ats = "namecoin";
1625 bw = "\"M\" or \"N\"";
1626 break;
1627 default:
1628 break;
1629 }
1630 fprintf(stderr,
1631 "Hint: valid %s addresses begin with %s\n", ats, bw);
1632 }
1633
1634 if (npfx)
1635 vg_prefix_context_next_difficulty(vcpp, &bntmp, &bntmp2, bnctx);
1636
1637 ret = (npfx != 0);
1638
1639 BN_clear_free(&bntmp);
1640 BN_clear_free(&bntmp2);
1641 BN_clear_free(&bntmp3);
1642 BN_CTX_free(bnctx);
1643 return ret;
1644}
1645
1646double
1647vg_prefix_get_difficulty(int addrtype, const char *pattern)
1648{
1649 BN_CTX *bnctx;
1650 BIGNUM result, bntmp;
1651 BIGNUM *ranges[4];
1652 char *dbuf;
1653 int ret;
1654 double diffret = 0.0;
1655
1656 bnctx = BN_CTX_new();
1657 BN_init(&result);
1658 BN_init(&bntmp);
1659
1660 ret = get_prefix_ranges(addrtype,
1661 pattern, ranges, bnctx);
1662
1663 if (ret == 0) {
1664 BN_sub(&bntmp, ranges[1], ranges[0]);
1665 BN_add(&result, &result, &bntmp);
1666 if (ranges[2]) {
1667 BN_sub(&bntmp, ranges[3], ranges[2]);
1668 BN_add(&result, &result, &bntmp);
1669 }
1670 free_ranges(ranges);
1671
1672 BN_clear(&bntmp);
1673 BN_set_bit(&bntmp, 192);
1674 BN_div(&result, NULL, &bntmp, &result, bnctx);
1675
1676 dbuf = BN_bn2dec(&result);
1677 diffret = strtod(dbuf, NULL);
1678 OPENSSL_free(dbuf);
1679 }
1680
1681 BN_clear_free(&result);
1682 BN_clear_free(&bntmp);
1683 BN_CTX_free(bnctx);
1684 return diffret;
1685}
1686
1687
1688static int
1689vg_prefix_test(vg_exec_context_t *vxcp)
1690{
1691 vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vxcp->vxc_vc;
1692 vg_prefix_t *vp;
1693 int res = 0;
1694
1695 /*
1696 * We constrain the prefix so that we can check for
1697 * a match without generating the lower four byte
1698 * check code.
1699 */
1700
1701 BN_bin2bn(vxcp->vxc_binres, 25, &vxcp->vxc_bntarg);
1702
1703research:
1704 vp = vg_prefix_avl_search(&vcpp->vcp_avlroot, &vxcp->vxc_bntarg);
1705 if (vp) {
1706 if (vg_exec_context_upgrade_lock(vxcp))
1707 goto research;
1708
1709 vg_exec_context_consolidate_key(vxcp);
1710 vcpp->base.vc_output_match(&vcpp->base, vxcp->vxc_key,
1711 vp->vp_pattern);
1712
1713 vcpp->base.vc_found++;
1714 accumulator++;
1715
1716
1717 if (vcpp->base.vc_only_one) {
1718 return 2;
1719 }
1720
1721 if (vcpp->base.vc_remove_on_match) {
1722 /* Subtract the range from the difficulty */
1723 vg_prefix_range_sum(vp,
1724 &vxcp->vxc_bntarg,
1725 &vxcp->vxc_bntmp);
1726 BN_sub(&vxcp->vxc_bntmp,
1727 &vcpp->vcp_difficulty,
1728 &vxcp->vxc_bntarg);
1729 BN_copy(&vcpp->vcp_difficulty, &vxcp->vxc_bntmp);
1730
1731 vg_prefix_delete(&vcpp->vcp_avlroot,vp);
1732 vcpp->base.vc_npatterns--;
1733
1734 if (!avl_root_empty(&vcpp->vcp_avlroot))
1735 vg_prefix_context_next_difficulty(
1736 vcpp, &vxcp->vxc_bntmp,
1737 &vxcp->vxc_bntmp2,
1738 vxcp->vxc_bnctx);
1739 vcpp->base.vc_pattern_generation++;
1740 }
1741 res = 1;
1742 }
1743 if (avl_root_empty(&vcpp->vcp_avlroot)) {
1744 return 2;
1745 }
1746 return res;
1747}
1748
1749static int
1750vg_prefix_hash160_sort(vg_context_t *vcp, void *buf)
1751{
1752 vg_prefix_context_t *vcpp = (vg_prefix_context_t *) vcp;
1753 vg_prefix_t *vp;
1754 unsigned char *cbuf = (unsigned char *) buf;
1755 unsigned char bnbuf[25];
1756 int nbytes, ncopy, nskip, npfx = 0;
1757
1758 /*
1759 * Walk the prefix tree in order, copy the upper and lower bound
1760 * values into the hash160 buffer. Skip the lower four bytes
1761 * and anything above the 24th byte.
1762 */
1763 for (vp = vg_prefix_first(&vcpp->vcp_avlroot);
1764 vp != NULL;
1765 vp = vg_prefix_next(vp)) {
1766 npfx++;
1767 if (!buf)
1768 continue;
1769
1770 /* Low */
1771 nbytes = BN_bn2bin(vp->vp_low, bnbuf);
1772 ncopy = ((nbytes >= 24) ? 20 :
1773 ((nbytes > 4) ? (nbytes - 4) : 0));
1774 nskip = (nbytes >= 24) ? (nbytes - 24) : 0;
1775 if (ncopy < 20)
1776 memset(cbuf, 0, 20 - ncopy);
1777 memcpy(cbuf + (20 - ncopy),
1778 bnbuf + nskip,
1779 ncopy);
1780 cbuf += 20;
1781
1782 /* High */
1783 nbytes = BN_bn2bin(vp->vp_high, bnbuf);
1784 ncopy = ((nbytes >= 24) ? 20 :
1785 ((nbytes > 4) ? (nbytes - 4) : 0));
1786 nskip = (nbytes >= 24) ? (nbytes - 24) : 0;
1787 if (ncopy < 20)
1788 memset(cbuf, 0, 20 - ncopy);
1789 memcpy(cbuf + (20 - ncopy),
1790 bnbuf + nskip,
1791 ncopy);
1792 cbuf += 20;
1793 }
1794 return npfx;
1795}
1796
1797vg_context_t *
1798vg_prefix_context_new(int addrtype, int privtype, int caseinsensitive)
1799{
1800 vg_prefix_context_t *vcpp;
1801
1802 vcpp = (vg_prefix_context_t *) malloc(sizeof(*vcpp));
1803 if (vcpp) {
1804 memset(vcpp, 0, sizeof(*vcpp));
1805 vcpp->base.vc_addrtype = addrtype;
1806 vcpp->base.vc_privtype = privtype;
1807 vcpp->base.vc_npatterns = 0;
1808 vcpp->base.vc_npatterns_start = 0;
1809 vcpp->base.vc_found = 0;
1810 vcpp->base.vc_chance = 0.0;
1811 vcpp->base.vc_free = vg_prefix_context_free;
1812 vcpp->base.vc_add_patterns = vg_prefix_context_add_patterns;
1813 vcpp->base.vc_clear_all_patterns =
1814 vg_prefix_context_clear_all_patterns;
1815 vcpp->base.vc_test = vg_prefix_test;
1816 vcpp->base.vc_hash160_sort = vg_prefix_hash160_sort;
1817 avl_root_init(&vcpp->vcp_avlroot);
1818 BN_init(&vcpp->vcp_difficulty);
1819 vcpp->vcp_caseinsensitive = caseinsensitive;
1820 }
1821 return &vcpp->base;
1822}
1823
1824
1825
1826
1827typedef struct _vg_regex_context_s {
1828 vg_context_t base;
1829 pcre **vcr_regex;
1830 pcre_extra **vcr_regex_extra;
1831 const char **vcr_regex_pat;
1832 unsigned long vcr_nalloc;
1833} vg_regex_context_t;
1834
1835static int
1836vg_regex_context_add_patterns(vg_context_t *vcp,
1837 const char ** const patterns, int npatterns)
1838{
1839 vg_regex_context_t *vcrp = (vg_regex_context_t *) vcp;
1840 const char *pcre_errptr;
1841 int pcre_erroffset;
1842 unsigned long i, nres, count;
1843 void **mem;
1844
1845 if (!npatterns)
1846 return 1;
1847
1848 if (npatterns > (vcrp->vcr_nalloc - vcrp->base.vc_npatterns)) {
1849 count = npatterns + vcrp->base.vc_npatterns;
1850 if (count < (2 * vcrp->vcr_nalloc)) {
1851 count = (2 * vcrp->vcr_nalloc);
1852 }
1853 if (count < 16) {
1854 count = 16;
1855 }
1856 mem = (void **) malloc(3 * count * sizeof(void*));
1857 if (!mem)
1858 return 0;
1859
1860 for (i = 0; i < vcrp->base.vc_npatterns; i++) {
1861 mem[i] = vcrp->vcr_regex[i];
1862 mem[count + i] = vcrp->vcr_regex_extra[i];
1863 mem[(2 * count) + i] = (void *) vcrp->vcr_regex_pat[i];
1864 }
1865
1866 if (vcrp->vcr_nalloc)
1867 free(vcrp->vcr_regex);
1868 vcrp->vcr_regex = (pcre **) mem;
1869 vcrp->vcr_regex_extra = (pcre_extra **) &mem[count];
1870 vcrp->vcr_regex_pat = (const char **) &mem[2 * count];
1871 vcrp->vcr_nalloc = count;
1872 }
1873
1874 nres = vcrp->base.vc_npatterns;
1875 for (i = 0; i < npatterns; i++) {
1876 vcrp->vcr_regex[nres] =
1877 pcre_compile(patterns[i], 0,
1878 &pcre_errptr, &pcre_erroffset, NULL);
1879 if (!vcrp->vcr_regex[nres]) {
1880 const char *spaces = " ";
1881 fprintf(stderr, "%s\n", patterns[i]);
1882 while (pcre_erroffset > 16) {
1883 fprintf(stderr, "%s", spaces);
1884 pcre_erroffset -= 16;
1885 }
1886 if (pcre_erroffset > 0)
1887 fprintf(stderr,
1888 "%s", &spaces[16 - pcre_erroffset]);
1889 fprintf(stderr, "^\nRegex error: %s\n", pcre_errptr);
1890 continue;
1891 }
1892 vcrp->vcr_regex_extra[nres] =
1893 pcre_study(vcrp->vcr_regex[nres], 0, &pcre_errptr);
1894 if (pcre_errptr) {
1895 fprintf(stderr, "Regex error: %s\n", pcre_errptr);
1896 pcre_free(vcrp->vcr_regex[nres]);
1897 continue;
1898 }
1899 vcrp->vcr_regex_pat[nres] = patterns[i];
1900 nres += 1;
1901 }
1902
1903 if (nres == vcrp->base.vc_npatterns)
1904 return 0;
1905
1906 vcrp->base.vc_npatterns_start += (nres - vcrp->base.vc_npatterns);
1907 vcrp->base.vc_npatterns = nres;
1908 return 1;
1909}
1910
1911static void
1912vg_regex_context_clear_all_patterns(vg_context_t *vcp)
1913{
1914 vg_regex_context_t *vcrp = (vg_regex_context_t *) vcp;
1915 int i;
1916 for (i = 0; i < vcrp->base.vc_npatterns; i++) {
1917 if (vcrp->vcr_regex_extra[i])
1918 pcre_free(vcrp->vcr_regex_extra[i]);
1919 pcre_free(vcrp->vcr_regex[i]);
1920 }
1921 vcrp->base.vc_npatterns = 0;
1922 vcrp->base.vc_npatterns_start = 0;
1923 vcrp->base.vc_found = 0;
1924}
1925
1926static void
1927vg_regex_context_free(vg_context_t *vcp)
1928{
1929 vg_regex_context_t *vcrp = (vg_regex_context_t *) vcp;
1930 vg_regex_context_clear_all_patterns(vcp);
1931 if (vcrp->vcr_nalloc)
1932 free(vcrp->vcr_regex);
1933 free(vcrp);
1934}
1935
1936static int
1937vg_regex_test(vg_exec_context_t *vxcp)
1938{
1939 vg_regex_context_t *vcrp = (vg_regex_context_t *) vxcp->vxc_vc;
1940
1941 unsigned char hash1[32], hash2[32];
1942 int i, zpfx, p, d, nres, re_vec[9];
1943 char b58[40];
1944 BIGNUM bnrem;
1945 BIGNUM *bn, *bndiv, *bnptmp;
1946 int res = 0;
1947
1948 pcre *re;
1949
1950 BN_init(&bnrem);
1951
1952 /* Hash the hash and write the four byte check code */
1953 SHA256(vxcp->vxc_binres, 21, hash1);
1954 SHA256(hash1, sizeof(hash1), hash2);
1955 memcpy(&vxcp->vxc_binres[21], hash2, 4);
1956
1957 bn = &vxcp->vxc_bntmp;
1958 bndiv = &vxcp->vxc_bntmp2;
1959
1960 BN_bin2bn(vxcp->vxc_binres, 25, bn);
1961
1962 /* Compute the complete encoded address */
1963 for (zpfx = 0; zpfx < 25 && vxcp->vxc_binres[zpfx] == 0; zpfx++);
1964 p = sizeof(b58) - 1;
1965 b58[p] = '\0';
1966 while (!BN_is_zero(bn)) {
1967 BN_div(bndiv, &bnrem, bn, &vxcp->vxc_bnbase, vxcp->vxc_bnctx);
1968 bnptmp = bn;
1969 bn = bndiv;
1970 bndiv = bnptmp;
1971 d = BN_get_word(&bnrem);
1972 b58[--p] = vg_b58_alphabet[d];
1973 }
1974 while (zpfx--) {
1975 b58[--p] = vg_b58_alphabet[0];
1976 }
1977
1978 /*
1979 * Run the regular expressions on it
1980 * SLOW, runs in linear time with the number of REs
1981 */
1982restart_loop:
1983 nres = vcrp->base.vc_npatterns;
1984 if (!nres) {
1985 res = 2;
1986 goto out;
1987 }
1988 for (i = 0; i < nres; i++) {
1989 d = pcre_exec(vcrp->vcr_regex[i],
1990 vcrp->vcr_regex_extra[i],
1991 &b58[p], (sizeof(b58) - 1) - p, 0,
1992 0,
1993 re_vec, sizeof(re_vec)/sizeof(re_vec[0]));
1994
1995 if (d <= 0) {
1996 if (d != PCRE_ERROR_NOMATCH) {
1997 fprintf(stderr, "PCRE error: %d\n", d);
1998 res = 2;
1999 goto out;
2000 }
2001 continue;
2002 }
2003
2004 re = vcrp->vcr_regex[i];
2005
2006 if (vg_exec_context_upgrade_lock(vxcp) &&
2007 ((i >= vcrp->base.vc_npatterns) ||
2008 (vcrp->vcr_regex[i] != re)))
2009 goto restart_loop;
2010
2011 vg_exec_context_consolidate_key(vxcp);
2012 vcrp->base.vc_output_match(&vcrp->base, vxcp->vxc_key,
2013 vcrp->vcr_regex_pat[i]);
2014 vcrp->base.vc_found++;
2015
2016 if (vcrp->base.vc_only_one) {
2017 res = 2;
2018 goto out;
2019 }
2020
2021 if (vcrp->base.vc_remove_on_match) {
2022 pcre_free(vcrp->vcr_regex[i]);
2023 if (vcrp->vcr_regex_extra[i])
2024 pcre_free(vcrp->vcr_regex_extra[i]);
2025 nres -= 1;
2026 vcrp->base.vc_npatterns = nres;
2027 if (!nres) {
2028 res = 2;
2029 goto out;
2030 }
2031 vcrp->vcr_regex[i] = vcrp->vcr_regex[nres];
2032 vcrp->vcr_regex_extra[i] =
2033 vcrp->vcr_regex_extra[nres];
2034 vcrp->vcr_regex_pat[i] = vcrp->vcr_regex_pat[nres];
2035 vcrp->base.vc_npatterns = nres;
2036 vcrp->base.vc_pattern_generation++;
2037 }
2038 res = 1;
2039 }
2040out:
2041 BN_clear_free(&bnrem);
2042 return res;
2043}
2044
2045vg_context_t *
2046vg_regex_context_new(int addrtype, int privtype)
2047{
2048 vg_regex_context_t *vcrp;
2049
2050 vcrp = (vg_regex_context_t *) malloc(sizeof(*vcrp));
2051 if (vcrp) {
2052 memset(vcrp, 0, sizeof(*vcrp));
2053 vcrp->base.vc_addrtype = addrtype;
2054 vcrp->base.vc_privtype = privtype;
2055 vcrp->base.vc_npatterns = 0;
2056 vcrp->base.vc_npatterns_start = 0;
2057 vcrp->base.vc_found = 0;
2058 vcrp->base.vc_chance = 0.0;
2059 vcrp->base.vc_free = vg_regex_context_free;
2060 vcrp->base.vc_add_patterns = vg_regex_context_add_patterns;
2061 vcrp->base.vc_clear_all_patterns =
2062 vg_regex_context_clear_all_patterns;
2063 vcrp->base.vc_test = vg_regex_test;
2064 vcrp->base.vc_hash160_sort = NULL;
2065 vcrp->vcr_regex = NULL;
2066 vcrp->vcr_nalloc = 0;
2067 }
2068 return &vcrp->base;
2069}