Debian Codegraph @

example.c

raw symbols SCIP
1 /* -*- Mode: C; c-file-style: "stroustrup" -*- */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "sstring.h"
7 #include "sdb.h"
8 #include "hashcash.h"
9 #include "getopt.h"
10
11 /* simplified hashcash.c example showing how to use the library
12 * functions. This code can only handle one operation per invocation
13 */
14
15 void usage( const char* msg );
16 void die( int err );
17 void die_msg( const char* str );
18 int parse_period( const char* aperiod, long* resp );
19 int progress_callback(int percent, int largest, int target,
20 double counter, double expected, void* user);
21
22 #define EOK 0 /* no error */
23 #define EINPUT -1 /* error invalid input */
24
25 #define MAX_PERIOD 11 /* max time_t = 10 plus 's' */
26 #define MAX_HDR 256
27 #define EXIT_ERROR 3
28
29 int main( int argc, char* argv[] ) {
30 int err = 0;
31 int opt, bits=20, str_type = TYPE_WILD, ignore_boundary_flag;
32 int case_flag = 0, check_flag = 0, db_flag = 0, core, res = HASHCASH_FAIL;
33 int mint_flag = 0, purge_flag = 0, core_flag = 0;
34 int time_width = 6, compress = 0, checked = 0;
35 long anon_random = 0, anon_period = 0, purge_period = 0, time_period = 0;
36 time_t real_time = time(0); /* now, in UTCTIME */
37 time_t now_time = real_time, token_time;
38 int speed_flag = 0, utc_flag = 0, version_flag = 0, hdr_flag = 0;
39 char* header = NULL;
40 char* db_filename = "hashcash.sdb";
41 char* purge_resource = NULL;
42 char* resource = NULL;
43 char* ext = NULL;
44 int purge_all = 0;
45 long validity_period = 28*TIME_DAY; /* default validity period: 28 days */
46 long purge_validity_period = 28*TIME_DAY; /* same default */
47 long grace_period = 2*TIME_DAY; /* default grace period: 2 days */
48 long valid_for = HASHCASH_INVALID;
49 char *new_token = NULL ;
50 char token[MAX_TOK+1], token_utime[MAX_UTC+1], period[MAX_PERIOD+1];
51 double tries_taken = 0;
52 void** compile = NULL;
53 char* re_err = NULL;
54 DB db;
55 hashcash_callback callback = NULL;
56
57 while ( (opt=getopt( argc, argv,
58 "-a:b:cde:f:g:hij:klmnop:qr:sSt:uvwx:yz:CEMO:PSVXZ:")) >0 ) {
59 switch( opt ) {
60 case 'a':
61 if ( !parse_period( optarg, &anon_period ) ) {
62 usage( "error: -a invalid period arg" );
63 }
64 break;
65 case 'b': bits = atoi( optarg+1 );
66 if ( bits < 0 ) { usage( "error: -b invalid bits arg" ); }
67 break;
68 case 'C': case_flag = 1; break;
69 case 'c': check_flag = 1; break;
70 case 'd': db_flag = 1; break;
71 case 'e':
72 if ( !parse_period( optarg, &validity_period ) ||
73 validity_period < 0 ) {
74 usage( "error: -e invalid validity period" );
75 }
76 break;
77 case 'E': str_type = TYPE_REGEXP; break;
78 case 'f': db_filename = strdup( optarg ); break;
79 case 'g':
80 if ( optarg[0] == '-' ) {
81 usage( "error: -g -ve grace period not valid" );
82 }
83 if ( !parse_period( optarg, &grace_period ) ) {
84 usage( "error: -g invalid grace period format" );
85 }
86 break;
87 case 'h': usage( "" ); break;
88 case 'i': ignore_boundary_flag = 1; break;
89 case 'j': purge_resource = optarg; break;
90 case 'k': purge_all = 1; break;
91 case 'm': mint_flag = 1; break;
92 case 'M': str_type = TYPE_WILD; break;
93 case 'O': core_flag = 1;
94 core = atoi( optarg );
95 res = hashcash_use_core( core );
96 if ( res < 1 ) {
97 usage( res == -1 ? "error: -O no such core\n" :
98 "error: -O core does not work on this platform" );
99 }
100 break;
101 case 'p':
102 purge_flag = 1;
103 if ( strcmp( optarg, "now" ) == 0 ) { purge_period = 0; }
104 else if ( !parse_period( optarg, &purge_period ) ||
105 purge_period < 0 ) {
106 usage( "error: -p invalid purge interval" );
107 }
108 break;
109 case 'P': callback = progress_callback; break;
110 case 1:
111 case 'r': resource = optarg; break;
112 case 's': speed_flag = 1; break;
113 case 'S': str_type = TYPE_STR; break;
114 case 't':
115 if ( optarg[0] == '-' || optarg[0] == '+' ) {
116 if ( !parse_period( optarg, &time_period ) ) {
117 usage( "error: -t invalid relative time format" );
118 }
119 now_time += time_period;
120 } else {
121 now_time = hashcash_from_utctimestr( optarg, utc_flag );
122 if ( now_time == (time_t)-1 ) {
123 usage( "error: -t invalid time format" );
124 }
125 }
126 break;
127 case 'u': utc_flag = 1; break;
128 case 'V': version_flag = 1; break;
129 case 'x': ext = strdup( optarg ); break;
130 case 'X':
131 hdr_flag = 1;
132 sstrncpy( header, "X-Hashcash: ", MAX_HDR );
133 break;
134 case 'z':
135 time_width = atoi( optarg );
136 if ( time_width != 6 && time_width != 10 && time_width != 12 ) {
137 usage( "error: -z invalid time width: must be 6, 10 or 12" );
138 }
139 break;
140 case 'Z': compress = atoi( optarg ); break;
141 case '?':
142 fprintf( stderr, "error: unrecognized option -%c", optopt );
143 usage( "" );
144 break;
145 case ':':
146 fprintf( stderr, "error: option -%c missing argument", optopt );
147 usage( "" );
148 break;
149 default:
150 usage( "error with argument processing" );
151 break;
152 }
153 }
154
155 if ( version_flag ) {
156 fprintf( stdout, "%s\n", HASHCASH_VERSION_STRING );
157 exit( EXIT_FAILURE );
158 }
159
160 if ( speed_flag ) {
161 if ( core_flag ) {
162 hashcash_benchtest( 3, core );
163 } else {
164 hashcash_benchtest( 3, -1 );
165 }
166 exit( EXIT_FAILURE );
167 }
168
169 if ( mint_flag ) {
170 err = hashcash_mint( now_time, time_width, resource, bits,
171 anon_period, &new_token, &anon_random,
172 &tries_taken, ext, compress,
173 callback, NULL );
174 } else if ( purge_flag ) {
175 if ( !hashcash_db_open( &db, db_filename, &err ) ) {
176 die(err);
177 }
178 if ( !hashcash_db_purge( &db, purge_resource, str_type, case_flag,
179 validity_period, grace_period, purge_all,
180 purge_period, now_time, &err ) ) {
181 die(err);
182 }
183 if ( !hashcash_db_close( &db, &err ) ) {
184 die(err);
185 }
186 } else if ( check_flag ) {
187 valid_for = hashcash_check( token, case_flag, resource, compile,
188 &re_err, str_type, now_time,
189 validity_period, grace_period, bits,
190 &token_time );
191 if ( valid_for < 0 ) {
192 switch ( valid_for ) {
193 case HASHCASH_INSUFFICIENT_BITS:
194 fprintf( stderr, "no match: token has insufficient bits\n" );
195 break;
196 case HASHCASH_VALID_IN_FUTURE:
197 fprintf( stderr, "no match: valid in future\n" );
198 break;
199 case HASHCASH_EXPIRED:
200 fprintf( stderr, "no match: token expired\n" );
201 break;
202 case HASHCASH_WRONG_RESOURCE:
203 fprintf( stderr, "no match: wrong resource\n" );
204 break;
205 case HASHCASH_REGEXP_ERROR:
206 fprintf( stderr, "regexp error: " );
207 die_msg( re_err );
208 break;
209
210 default:
211 die_msg( "internal error" );
212 break;
213 }
214 exit( EXIT_FAILURE );
215 }
216 checked = 1;
217 }
218
219 if ( db_flag && check_flag && checked ) {
220 if ( !hashcash_db_open( &db, db_filename, &err ) ) {
221 die(err);
222 }
223 if ( hashcash_db_in( &db, token, token_utime, &err ) ) {
224 if ( err ) { die(err); }
225 fprintf( stderr, "stamp: double spent\n" );
226 }
227 sprintf( period, "%ld", (long)validity_period );
228 if ( !hashcash_db_add( &db, token, period, &err ) ) {
229 die(err);
230 }
231 if ( !hashcash_db_close( &db, &err ) ) {
232 die(err);
233 }
234 }
235
236 exit( EXIT_SUCCESS );
237 }
238
239 int progress_callback(int percent, int largest, int target,
240 double counter, double expected, void* user)
241 {
242 static int previous_percent = -1;
243 static int previous_largest = -1;
244 static double previous_counter = -1;
245
246 if ( previous_counter != counter ||
247 previous_percent != percent || previous_largest != largest ) {
248 fprintf( stderr, "percent: %.0lf/%.0lf = %d%% [%d/%d bits]\r",
249 counter, expected, percent, largest, target );
250 previous_percent = percent;
251 previous_largest = largest;
252 previous_counter = counter;
253 }
254 return 1;
255 }
256
257 void usage( const char* msg )
258 {
259 if ( msg ) { fputs( msg, stderr ); fputs( "\n\n", stderr ); }
260 fprintf( stderr, "mint:\t\thashcash [-m] [opts] resource\n" );
261 fprintf( stderr, "measure speed:\thashcash -s [-b bits]\n" );
262 fprintf( stderr, "check:\t\thashcash -c [opts] -d -r resource [-e period] [token]\n" );
263 fprintf( stderr, "purge expired:\thashcash -p now [-k] [-j resource] [-t time] [-u]\n" );
264 fprintf( stderr, "\n" );
265 fprintf( stderr, "\t-b bits\t\tfind or check partial preimage of length bits\n" );
266 fprintf( stderr, "\t-d\t\tuse database (for double spending detection)\n" );
267 fprintf( stderr, "\t-r resource\tresource name for minting or checking token\n" );
268 fprintf( stderr, "\t-e period\ttime until token expires\n" );
269 fprintf( stderr, "\t-g\t\tgrace period for clock skew\n" );
270 fprintf( stderr, "\t-t time\t\tmodify current time token created at\n" );
271 fprintf( stderr, "\t-a time\t\tmodify time by random amount in range given\n" );
272 fprintf( stderr, "\t-u\t\tgive time in UTC instead of local time\n" );
273 fprintf( stderr, "\t-h\t\tprint this usage info\n" );
274 fprintf( stderr, "\t-f dbfile\tuse filename dbfile for database\n" );
275 fprintf( stderr, "\t-j resource\twith -p delete just tokens matching the given resource\n" );
276 fprintf( stderr, "\t-k\t\twith -p delete all not just expired\n" );
277 fprintf( stderr, "\t-x ext\t\tput in extension field\n" );
278 fprintf( stderr, "\t-X\t\toutput with header format 'X-Hashcash: '\n" );
279 fprintf( stderr, "\t-i\t\twith -X and -c, check msg body as well\n" );
280 fprintf( stderr, "\t-z width\twidth of time field 6,10 or 12 chars (default 6)\n" );
281 fprintf( stderr, "\t-C\t\tmatch resources as case sensitive (default insensitive)\n" );
282 fprintf( stderr, "\t-S\t\tmatch following resources as text strings\n" );
283 fprintf( stderr, "\t-W\t\tmatch following resources with wildcards (default)\n" );
284 fprintf( stderr, "\t-E\t\tmatch following resources as regular expression\n" );
285 fprintf( stderr, "\t-P\t\tshow progress while searching\n");
286 fprintf( stderr, "\t-O core\t\tuse specified minting core\n");
287 fprintf( stderr, "examples:\n" );
288 fprintf( stderr, "\thashcash -mb20 foo # mint 20 bit preimage\n" );
289 fprintf( stderr, "\thashcash -cdb20 -r foo 1:20:040806:foo::831d0c6f22eb81ff:15eae4 # check preimage\n" );
290 fprintf( stderr, "\n" );
291 fprintf( stderr, "see hashcash (1) man page or http://www.hashcash.org/ for more details.\n" );
292 exit( EXIT_ERROR );
293 }
294
295 int parse_period( const char* aperiod, long* resp )
296 {
297 int period_len;
298 char last_char;
299 long res = 1;
300 char period_array[MAX_PERIOD+1];
301 char* period = period_array;
302
303 if ( period == NULL ) { return 0; }
304 period_len = strlen( aperiod );
305 if ( period_len == 0 ) { return 0; }
306 last_char = aperiod[period_len-1];
307 if ( ! isdigit( last_char ) && ! strchr( "YyMdhmsw", last_char ) ) {
308 return 0;
309 }
310
311 sstrncpy( period, aperiod, MAX_PERIOD );
312
313 if ( ! isdigit( last_char ) ) { period[--period_len] = '\0'; }
314 if ( period[0] == '+' || period[0] == '-' ) {
315 if ( period[0] == '-' ) { res = -1; }
316 period++; period_len--;
317 }
318 if ( period_len > 0 ) { res *= atoi( period ); }
319 switch ( last_char )
320 {
321 case 's': break;
322 case 'm': res *= TIME_MINUTE; break;
323 case 'h': res *= TIME_HOUR; break;
324 case 'd': res *= TIME_DAY; break;
325 case 'w': res *= TIME_DAY*7; break;
326 case 'M': res *= TIME_MONTH; break;
327 case 'y': case 'Y': res *= TIME_YEAR; break;
328 case '0': case '1': case '2': case '3': case '4':
329 case '5': case '6': case '7': case '8': case '9': break;
330 default: return 0;
331 }
332 *resp = res;
333 return 1;
334 }
335
336 void die( int err )
337 {
338 const char* str = "";
339
340 switch ( err ) {
341 case EOK:
342 exit( EXIT_SUCCESS );
343 break;
344 case EINPUT:
345 str = "invalid inputs";
346 break;
347 default:
348 str = strerror( err );
349 break;
350 }
351 fprintf( stderr, "error: %s\n", str );
352 exit( EXIT_ERROR );
353 }
354
355 void die_msg( const char* str )
356 {
357 fprintf( stderr, str );
358 fprintf( stderr, "\n" );
359 exit( EXIT_ERROR );
360 }