| 1 |
/* -*- Mode: C; c-file-style: "stroustrup" -*- */ |
| 2 |
|
| 3 |
/* hashcash mint and verification |
| 4 |
* |
| 5 |
* hashcash is payment in burnt CPU cycles by calculating n-bit |
| 6 |
* partial hash preimages of the all 0 string |
| 7 |
* |
| 8 |
* see: http://www.hashcash.org/ |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
#if defined( THINK_C ) |
| 13 |
#include <console.h> |
| 14 |
#include <unix.h> |
| 15 |
#elif defined( unix ) || defined( VMS ) || defined( __APPLE__ ) |
| 16 |
#include <unistd.h> |
| 17 |
#elif defined ( WIN32 ) |
| 18 |
#include <io.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <ctype.h> |
| 25 |
#include <time.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <assert.h> |
| 28 |
#include <math.h> |
| 29 |
|
| 30 |
#include "sdb.h" |
| 31 |
#include "utct.h" |
| 32 |
#include "random.h" |
| 33 |
#include "hashcash.h" |
| 34 |
#include "libfastmint.h" |
| 35 |
#include "sstring.h" |
| •36 |
#include <getopt.h> |
| 37 |
|
| 38 |
#if defined( OPENSSL ) |
| 39 |
#include <openssl/sha.h> |
| 40 |
#define SHA1_ctx SHA_CTX |
| 41 |
#define SHA1_Final( x, digest ) SHA1_Final( digest, x ) |
| 42 |
#define SHA1_DIGEST_BYTES SHA_DIGEST_LENGTH |
| 43 |
#else |
| 44 |
#include "sha1.h" |
| 45 |
#endif |
| 46 |
|
| 47 |
#define EOK 0 /* no error */ |
| 48 |
#define EINPUT -1 /* error invalid input */ |
| 49 |
|
| 50 |
#define EXIT_UNCHECKED 2 /* don't consider the token valid because */ |
| 51 |
/* there are aspects of the token that */ |
| 52 |
/* were not checked */ |
| 53 |
#define EXIT_ERROR 3 |
| 54 |
|
| 55 |
#define MAX_PERIOD 11 /* max time_t = 10 plus 's' */ |
| 56 |
#define MAX_HDR 256 |
| 57 |
#define MAX_LINE 1024+MAX_EXT+1 |
| 58 |
|
| 59 |
/* WARNING: the *PRINTF macros assume a coding style which always uses |
| 60 |
* braces {} around conditional code -- unfortunately there is no |
| 61 |
* portable way to do vararg macros or functions which call other |
| 62 |
* vararg functions |
| 63 |
*/ |
| 64 |
|
| 65 |
#define QQE (!quiet_flag) |
| 66 |
#define PPE (!out_is_tty||quiet_flag) |
| 67 |
#define VVE (verbose_flag) |
| 68 |
|
| 69 |
#define QQ if (QQE) |
| 70 |
#define PP if (PPE) |
| 71 |
#define VV if (VVE) |
| 72 |
#define QPRINTF QQ fprintf |
| 73 |
#define PPRINTF PP fprintf |
| 74 |
#define VPRINTF VV fprintf |
| 75 |
|
| 76 |
#define QPUTS(f,str) QQ { fputs(str,f); } |
| 77 |
#define PPUTS(f,str) PP { fputs(str,f); } |
| 78 |
#define VPUTS(f,str) VV { fputs(str,f); } |
| 79 |
|
| 80 |
#define HDR_LINE_LEN 80 |
| 81 |
char *read_header( FILE* f, char** s, int* slen, int* alloc, |
| 82 |
char* a, int alen ); |
| 83 |
int read_eof( FILE* fp, char* a ); |
| 84 |
void chomplf( char* token ); |
| 85 |
void trimspace( char* token ); |
| 86 |
void die( int err ); |
| 87 |
void die_msg( const char* ); |
| 88 |
void usage( const char* ); |
| 89 |
int parse_period( const char* aperiod, long* resp ); |
| 90 |
double report_speed( int bits, double* time_est, int display ); |
| 91 |
|
| 92 |
#if defined( WIN32 ) |
| 93 |
#define LINEFEED "\r\n" |
| 94 |
#else |
| 95 |
#define LINEFEED "\n" |
| 96 |
#endif |
| 97 |
|
| 98 |
/* avoid needing to export stolower from hashcash.dll */ |
| 99 |
|
| 100 |
void mystolower( char* str ); |
| 101 |
#define stolower mystolower |
| 102 |
|
| 103 |
typedef struct { |
| 104 |
char* str; |
| 105 |
void* regexp; /* compiled regexp */ |
| 106 |
int type; |
| 107 |
int case_flag; |
| 108 |
long validity; |
| 109 |
long grace; |
| 110 |
long anon; |
| 111 |
int width; |
| 112 |
int bits; |
| 113 |
int over; |
| 114 |
} ELEMENT; |
| 115 |
|
| 116 |
typedef struct { |
| 117 |
int num; |
| 118 |
int max; |
| 119 |
ELEMENT* elt; |
| 120 |
} ARRAY; |
| 121 |
|
| 122 |
void db_open( DB* db, const char* db_filename ); |
| 123 |
void db_purge_arr( DB* db, ARRAY* purge_resource, int purge_all, |
| 124 |
long purge_period, time_t now_time, long validity_period, |
| 125 |
long grace_period ); |
| 126 |
int db_purge( DB* db, ARRAY* purge_resource, int purge_all, |
| 127 |
long purge_period, time_t now_time, long validity_period, |
| 128 |
long grace_period, int verbose, int* err ); |
| 129 |
int db_in( DB* db, char* token, char *period ); |
| 130 |
void db_add( DB* db, char* token, char *token_utime ); |
| 131 |
void db_close( DB* db ) ; |
| 132 |
|
| 133 |
void array_alloc( ARRAY* array, int num ); |
| 134 |
void array_push( ARRAY* array, const char *str, int type, int case_flag, |
| 135 |
long validity, long grace, long anon, int width, int bits, |
| 136 |
int over ); |
| 137 |
#define array_num( a ) ( (a)->num ) |
| 138 |
int bit_cmp( const void* ap, const void* bp ); |
| 139 |
void array_sort( ARRAY* array, int(*cmp)(const void*, const void*) ); |
| 140 |
|
| 141 |
|
| 142 |
#define hc_est_time(b) ( hashcash_expected_tries(b) / \ |
| 143 |
(double)hashcash_per_sec() ) |
| 144 |
int quiet_flag; |
| 145 |
int verbose_flag; |
| 146 |
int out_is_tty; |
| 147 |
int in_is_tty; |
| 148 |
|
| 149 |
#define PROGRESS_FMT "percent: %%.0lf/%%.0lf = %%.%dlf%%%% [%%d/%%d bits]%%c" |
| 150 |
char progress_format[80] = { 0 }; |
| 151 |
|
| 152 |
int progress_callback(int percent, int largest, int target, |
| 153 |
double counter, double expected, void* user); |
| 154 |
|
| 155 |
#define PURGED_KEY "last_purged" |
| 156 |
|
| 157 |
int main( int argc, char* argv[] ) |
| 158 |
{ |
| 159 |
long validity_period = 28*TIME_DAY; /* default validity period: 28 days */ |
| 160 |
long purge_validity_period = 28*TIME_DAY; /* same default */ |
| 161 |
long grace_period = 2*TIME_DAY; /* default grace period: 2 days */ |
| 162 |
|
| 163 |
char period[ MAX_PERIOD+1 ], utcttime[ MAX_UTC+1 ]; |
| 164 |
const char* db_filename = "hashcash.sdb"; |
| 165 |
int boundary, err, anon_flag = 0, over_flag = 0, grace_flag = 0; |
| 166 |
long anon_period = 0, anon_random = 0 ; |
| 167 |
int default_bits = 20; /* default 20 bits */ |
| 168 |
int count_bits, claimed_bits = 0, bits = 0; |
| 169 |
int check_flag = 0, case_flag = 0, hdr_flag = 0; |
| 170 |
int width_flag = 0, left_flag = 0, speed_flag = 0, utc_flag = 0; |
| 171 |
int bits_flag = 0, str_type = TYPE_WILD; /* default to wildcard match */ |
| 172 |
int validity_flag = 0, db_flag = 0, yes_flag = 0, purge_flag = 0; |
| 173 |
int mint_flag = 0, ignore_boundary_flag = 0, name_flag = 0, res_flag = 0; |
| 174 |
int auto_version = 0, version_flag = 0, checked = 0, comma = 0; |
| 175 |
char header[ MAX_HDR+1 ] = { 0 }; |
| 176 |
char header2[ MAX_HDR+1 ] = { 0 }; |
| 177 |
char* header_wrapped = NULL; |
| 178 |
int hdr_len, hdr2_len, token_found = 0, token2_found = 0, hdrs_found = 0; |
| 179 |
char token[ MAX_TOK+1 ] = { 0 }, token_resource[ MAX_RES+1 ] = { 0 }; |
| 180 |
char *new_token = NULL ; |
| 181 |
char line_arr[ MAX_LINE+1 ] = { 0 }, *line = line_arr; |
| 182 |
int line_max = MAX_LINE, line_alloc = 0; |
| 183 |
char ahead[ MAX_LINE+1 ] = { 0 } , *ext = NULL, *junk = NULL; |
| 184 |
ARRAY purge_resource, resource, tokens, args; |
| 185 |
|
| 186 |
clock_t start = 0, end = 0, tmp = 0; |
| 187 |
|
| 188 |
long purge_period = 0, valid_for = HASHCASH_INVALID, time_period = 0 ; |
| 189 |
int purge_all = 0; |
| 190 |
int multiple_validity = 0, multiple_bits = 0, multiple_resources = 0; |
| 191 |
int multiple_grace = 0, accept = 0, parsed = 0; |
| 192 |
|
| 193 |
char token_utime[ MAX_UTC+1 ] = { 0 } ; /* time token created */ |
| 194 |
time_t real_time = time(0); /* now, in UTCTIME */ |
| 195 |
time_t now_time = real_time; |
| 196 |
time_t token_time = 0, expiry_time = 0; |
| 197 |
int time_width_flag = 0; /* -z option, default 6 YYMMDD */ |
| 198 |
int compress = 0; /* fast by default */ |
| 199 |
int inferred_time_width = 0, time_width = 6; /* default YYMMDD */ |
| 200 |
int core = 0, res = 0, core_flag = 0; |
| 201 |
|
| 202 |
double tries_taken = 0, taken = 0, tries_expected = 0, time_est = 0; |
| 203 |
int opt = 0, vers = 0, db_opened = 0, i = 0, j = 0, t = 0, tty_info = 0; |
| 204 |
int in_headers = 0, skip = 0, over = 0, precision = 0; |
| 205 |
char *re_err = NULL; |
| 206 |
ELEMENT* ent = NULL; |
| 207 |
DB db; |
| 208 |
hashcash_callback callback = NULL; |
| 209 |
|
| 210 |
#if defined( THINK_C ) |
| 211 |
argc = ccommand(&argv); |
| 212 |
#endif |
| 213 |
|
| 214 |
out_is_tty = isatty( fileno( stdout ) ); |
| 215 |
in_is_tty = isatty( fileno( stdin ) ) && !feof( stdin ); |
| 216 |
quiet_flag = 0; |
| 217 |
verbose_flag = 0; |
| 218 |
|
| 219 |
token_resource[0] = '\0'; |
| 220 |
token[0] = '\0'; |
| 221 |
opterr = 0; |
| 222 |
|
| 223 |
array_alloc( &resource, 32 ); |
| 224 |
array_alloc( &purge_resource, 32 ); |
| 225 |
array_alloc( &tokens, 32 ); |
| 226 |
array_alloc( &args, 32 ); |
| 227 |
|
| 228 |
while ( (opt=getopt(argc, argv, |
| 229 |
"-a:b:cde:f:g:hij:klmnop:qr:st:uvwx:yz:CEMO:PSVXZ:")) >0 ) { |
| 230 |
switch ( opt ) { |
| 231 |
case 'a': anon_flag = 1; |
| 232 |
if ( !parse_period( optarg, &anon_period ) ) { |
| 233 |
usage( "error: -a invalid period arg" ); |
| 234 |
} |
| 235 |
break; |
| 236 |
case 'b': |
| 237 |
if ( bits_flag ) { multiple_bits = 1; } |
| 238 |
bits_flag = 1; |
| 239 |
|
| 240 |
if ( strcmp( optarg, "default" ) == 0 ) { |
| 241 |
bits = default_bits; |
| 242 |
} else if ( optarg[0] == '-' || optarg[0] == '+' ) { |
| 243 |
bits = atoi( optarg+1 ); |
| 244 |
if ( bits < 0 ) { usage( "error: -b invalid bits arg" ); } |
| 245 |
if ( optarg[0] == '-' ) { bits = -bits; } |
| 246 |
bits = default_bits + bits; |
| 247 |
if ( bits < 0 ) { bits = 0; } |
| 248 |
} else { /* absolute */ |
| 249 |
bits = atoi( optarg ); |
| 250 |
if ( bits < 0 ) { usage( "error: -b invalid bits arg" ); } |
| 251 |
} |
| 252 |
if ( bits > SHA1_DIGEST_BYTES * 8 ) { |
| 253 |
usage( "error: max preimage with sha1 is 160 bits" ); |
| 254 |
} |
| 255 |
break; |
| 256 |
case 'C': case_flag = 1; break; |
| 257 |
case 'c': check_flag = 1; break; |
| 258 |
case 'd': db_flag = 1; break; |
| 259 |
case 'e': |
| 260 |
if ( validity_flag ) { multiple_validity = 1; } |
| 261 |
validity_flag = 1; |
| 262 |
if ( !parse_period( optarg, &validity_period ) || |
| 263 |
validity_period < 0 ) { |
| 264 |
usage( "error: -e invalid validity period" ); |
| 265 |
} |
| 266 |
break; |
| 267 |
case 'E': str_type = TYPE_REGEXP; break; |
| 268 |
case 'f': db_filename = strdup( optarg ); break; |
| 269 |
case 'g': |
| 270 |
if ( grace_flag ) { multiple_grace = 1; } |
| 271 |
grace_flag = 1; |
| 272 |
if ( optarg[0] == '-' ) { |
| 273 |
usage( "error: -g -ve grace period not valid" ); |
| 274 |
} |
| 275 |
if ( !parse_period( optarg, &grace_period ) ) { |
| 276 |
usage( "error: -g invalid grace period format" ); |
| 277 |
} |
| 278 |
break; |
| 279 |
case 'h': usage( "" ); break; |
| 280 |
case 'i': ignore_boundary_flag = 1; break; |
| 281 |
case 'j': |
| 282 |
array_push( &purge_resource, optarg, str_type, case_flag, |
| 283 |
0, 0, 0, 0, 0, 0 ); |
| 284 |
over_flag = 0; |
| 285 |
break; |
| 286 |
case 'k': purge_all = 1; break; |
| 287 |
case 'l': left_flag = 1; break; |
| 288 |
case 'n': name_flag = 1; break; |
| 289 |
case 'o': over_flag = 1; break; |
| 290 |
case 'O': |
| 291 |
core_flag = 1; |
| 292 |
core = atoi( optarg ); |
| 293 |
res = hashcash_use_core( core ); |
| 294 |
if ( res < 1 ) { |
| 295 |
usage( res == -1 ? "error: -O no such core\n" : |
| 296 |
"error: -O core does not work on this platform" ); |
| 297 |
} |
| 298 |
break; |
| 299 |
case 'm': mint_flag = 1; |
| 300 |
if ( !bits_flag ) { |
| 301 |
bits_flag = 1; |
| 302 |
bits = default_bits; |
| 303 |
} |
| 304 |
break; |
| 305 |
case 'M': str_type = TYPE_WILD; break; |
| 306 |
case 'p': |
| 307 |
if ( purge_flag ) { usage("error: only one -p flag per call"); } |
| 308 |
purge_flag = 1; |
| 309 |
if ( strcmp( optarg, "now" ) == 0 ) { purge_period = 0; } |
| 310 |
else if ( !parse_period( optarg, &purge_period ) || |
| 311 |
purge_period < 0 ) { |
| 312 |
usage( "error: -p invalid purge interval" ); |
| 313 |
} |
| 314 |
purge_validity_period = validity_period; |
| 315 |
break; |
| 316 |
case 'P': callback = progress_callback; break; |
| 317 |
case 'q': quiet_flag = 1; break; |
| 318 |
case 1: |
| 319 |
case 'r': |
| 320 |
if ( opt == 'r' ) { |
| 321 |
if ( res_flag ) { multiple_resources = 1; } |
| 322 |
res_flag = 1; |
| 323 |
} |
| 324 |
|
| 325 |
/* infer appropriate time_width from validity period */ |
| 326 |
if ( validity_flag && !time_width_flag && !inferred_time_width ) { |
| 327 |
time_width = hashcash_validity_to_width( validity_period ); |
| 328 |
if ( !time_width ) { |
| 329 |
die_msg( "error: -ve validity period" ); |
| 330 |
} |
| 331 |
inferred_time_width = time_width; |
| 332 |
} |
| 333 |
|
| 334 |
array_push( &args, optarg, str_type, case_flag, validity_period, |
| 335 |
grace_period, anon_period, time_width, bits, 0 ); |
| 336 |
|
| 337 |
if ( opt == 'r' ) { |
| 338 |
array_push( &resource, optarg, str_type, case_flag, |
| 339 |
validity_period, grace_period, anon_period, |
| 340 |
time_width, bits, over_flag ); |
| 341 |
} else if ( opt == 1 ) { |
| 342 |
array_push( &tokens, optarg, str_type, case_flag, |
| 343 |
validity_period, grace_period, anon_period, |
| 344 |
time_width, bits, 0 ); |
| 345 |
} |
| 346 |
over_flag = 0; |
| 347 |
|
| 348 |
break; |
| 349 |
case 's': speed_flag = 1; break; |
| 350 |
case 'S': str_type = TYPE_STR; break; |
| 351 |
case 't': |
| 352 |
if ( optarg[0] == '-' || optarg[0] == '+' ) { |
| 353 |
if ( !parse_period( optarg, &time_period ) ) { |
| 354 |
usage( "error: -t invalid relative time format" ); |
| 355 |
} |
| 356 |
now_time += time_period; |
| 357 |
} else { |
| 358 |
now_time = hashcash_from_utctimestr( optarg, utc_flag ); |
| 359 |
if ( now_time == (time_t)-1 ) { |
| 360 |
usage( "error: -t invalid time format" ); |
| 361 |
} |
| 362 |
} |
| 363 |
break; |
| 364 |
case 'u': utc_flag = 1; break; |
| 365 |
case 'v': verbose_flag = 1; break; |
| 366 |
case 'V': version_flag = 1; break; |
| 367 |
case 'w': width_flag = 1; break; |
| 368 |
case 'x': |
| 369 |
ext = strdup( optarg ); |
| 370 |
/* deprecate old -x flag */ |
| 371 |
/* hdr_flag = 1; */ |
| 372 |
/* sstrncpy( header, optarg, MAX_HDR ); */ |
| 373 |
break; |
| 374 |
case 'X': |
| 375 |
hdr_flag = 1; |
| 376 |
sstrncpy( header, "X-Hashcash: ", MAX_HDR ); |
| 377 |
sstrncpy( header2, "Hashcash: ", MAX_HDR ); |
| 378 |
break; |
| 379 |
case 'y': yes_flag = 1; break; |
| 380 |
case 'z': |
| 381 |
time_width_flag = 1; |
| 382 |
time_width = atoi( optarg ); |
| 383 |
if ( time_width != 6 && time_width != 10 && time_width != 12 ) { |
| 384 |
usage( "error: -z invalid time width: must be 6, 10 or 12" ); |
| 385 |
} |
| 386 |
break; |
| 387 |
case 'Z': compress = atoi( optarg ); |
| 388 |
/* temporary work around for bug discovered in 1.15, disable -Z2 */ |
| 389 |
/* if ( compress > 1 ) { compress = 1; } */ |
| 390 |
break; |
| 391 |
case '?': |
| 392 |
fprintf( stderr, "error: unrecognized option -%c", optopt ); |
| 393 |
usage( "" ); |
| 394 |
break; |
| 395 |
case ':': |
| 396 |
fprintf( stderr, "error: option -%c missing argument", optopt ); |
| 397 |
usage( "" ); |
| 398 |
break; |
| 399 |
default: |
| 400 |
usage( "error with argument processing" ); |
| 401 |
break; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if ( validity_flag && !time_width_flag && !inferred_time_width ) { |
| 406 |
time_width = hashcash_validity_to_width( resource.elt[0].validity ); |
| 407 |
if ( !time_width ) { |
| 408 |
die_msg( "error: -ve validity period" ); |
| 409 |
} |
| 410 |
inferred_time_width = time_width; |
| 411 |
} |
| 412 |
|
| 413 |
if ( array_num( &args ) == 0 && verbose_flag && |
| 414 |
name_flag + left_flag + width_flag + check_flag + |
| 415 |
mint_flag + purge_flag + speed_flag < 1 ) { |
| 416 |
auto_version = 1; |
| 417 |
version_flag = 1; verbose_flag = 0; |
| 418 |
} |
| 419 |
|
| 420 |
if ( version_flag ) { |
| 421 |
QPRINTF( stderr, "Version: hashcash-%s\n", |
| 422 |
HASHCASH_VERSION_STRING ); |
| 423 |
PPRINTF( stdout, "%s\n", HASHCASH_VERSION_STRING ); |
| 424 |
if ( auto_version ) { exit( EXIT_FAILURE ); } |
| 425 |
} |
| 426 |
|
| 427 |
if ( mint_flag + check_flag > 1 ) { |
| 428 |
usage( "can only specify one of -m, -c" ); |
| 429 |
} |
| 430 |
|
| 431 |
if ( mint_flag && ( name_flag || width_flag || left_flag ) ) { |
| 432 |
usage( "can not use -n, -w or -l with -m" ); |
| 433 |
} |
| 434 |
|
| 435 |
if ( mint_flag + check_flag + name_flag + left_flag + width_flag + db_flag+ |
| 436 |
bits_flag + res_flag + purge_flag + speed_flag + version_flag == 0 ) { |
| 437 |
usage( "must specify at least one of -m, -c, -d, -n, -l-, -w, -b, -r, -p or -s"); |
| 438 |
} |
| 439 |
|
| 440 |
if ( quiet_flag ) { verbose_flag = 0; } /* quiet overrides verbose */ |
| 441 |
if ( speed_flag && check_flag ) { speed_flag = 0; } /* ignore speed */ |
| 442 |
|
| 443 |
if ( purge_flag ) { |
| 444 |
db_open( &db, db_filename ); |
| 445 |
db_opened = 1; |
| 446 |
|
| 447 |
db_purge_arr( &db, &purge_resource, purge_all, purge_period, |
| 448 |
now_time, validity_flag ? purge_validity_period : 0, |
| 449 |
grace_period ); |
| 450 |
|
| 451 |
if ( mint_flag + check_flag + name_flag + left_flag + |
| 452 |
width_flag + bits_flag + res_flag + speed_flag == 0 ) { |
| 453 |
db_close( &db ); /* just -p we're done */ |
| 454 |
exit( EXIT_SUCCESS ); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
if ( mint_flag || speed_flag ) { /* mint stamp */ |
| 459 |
|
| 460 |
if ( mint_flag ) { |
| 461 |
|
| 462 |
/* no resources given, read them from stdin */ |
| 463 |
if ( array_num( &args ) == 0 ) { |
| 464 |
if ( in_is_tty ) { |
| 465 |
fprintf( stderr, "enter resources to mint stamps for one per line:\n" ); |
| 466 |
} |
| 467 |
while ( !feof(stdin) ) { |
| 468 |
line[0] = '\0'; |
| 469 |
junk = fgets( line, MAX_LINE, stdin ); |
| 470 |
chomplf( line ); |
| 471 |
trimspace( line ); |
| 472 |
if ( line[0] != '\0' ) { |
| 473 |
array_push( &args, line, 0, 0, validity_period, |
| 474 |
grace_period, anon_period, time_width, |
| 475 |
bits, 0 ); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/* integrate the bench test */ |
| 482 |
|
| 483 |
if ( verbose_flag && speed_flag && !mint_flag ) { |
| 484 |
if ( core_flag ) { |
| 485 |
hashcash_benchtest( 3, core ); |
| 486 |
} else { |
| 487 |
hashcash_benchtest( 3, -1 ); |
| 488 |
} |
| 489 |
exit( EXIT_SUCCESS ); /* don't actually calculate it */ |
| 490 |
} |
| 491 |
|
| 492 |
if ( !verbose_flag && speed_flag && mint_flag ) { |
| 493 |
QPRINTF( stderr, "core: %s\n", |
| 494 |
hashcash_core_name( hashcash_core() ) ); |
| 495 |
QPRINTF( stderr, "speed: %ld preimage tests per second\n", |
| 496 |
hashcash_per_sec() ); |
| 497 |
PPRINTF( stdout, "%ld\n", hashcash_per_sec() ); |
| 498 |
QPRINTF( stderr, "compression: %d\n", compress ); |
| 499 |
} |
| 500 |
|
| 501 |
if ( verbose_flag || ( speed_flag && !bits_flag ) ) { |
| 502 |
QPRINTF( stderr, "core: %s\n", |
| 503 |
hashcash_core_name( hashcash_core() ) ); |
| 504 |
QPRINTF( stderr, "speed: %ld preimage tests per second\n", |
| 505 |
hashcash_per_sec() ); |
| 506 |
if ( speed_flag && !bits_flag && !mint_flag ) { |
| 507 |
PPRINTF( stdout, "%ld\n", hashcash_per_sec() ); |
| 508 |
} |
| 509 |
QPRINTF( stderr, "compression: %d\n", compress ); |
| 510 |
if ( speed_flag && !bits_flag && !mint_flag ) { |
| 511 |
exit( EXIT_SUCCESS ); /* don't actually calculate it */ |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
if ( speed_flag && bits_flag && !mint_flag ) { |
| 516 |
tries_expected = report_speed( bits, (PPE) ? &time_est : 0, |
| 517 |
!quiet_flag ); |
| 518 |
PPRINTF( stdout, "%.0f\n", time_est ); |
| 519 |
exit( EXIT_SUCCESS ); /* don't actually calculate it */ |
| 520 |
} |
| 521 |
|
| 522 |
for ( i = 0; i < array_num( &args ); i++ ) { |
| 523 |
|
| 524 |
start = clock(); |
| 525 |
|
| 526 |
ent = &(args.elt[i]); |
| 527 |
|
| 528 |
tries_expected = report_speed( ent->bits, NULL, |
| 529 |
verbose_flag||speed_flag ); |
| 530 |
|
| 531 |
if ( !ent->case_flag ) { stolower( ent->str ); } |
| 532 |
|
| 533 |
/* calculate precision to see responsive % progress */ |
| 534 |
/* aim to see progress min every 0.25 seconds */ |
| 535 |
time_est = hc_est_time( ent->bits ) / 100; |
| 536 |
precision = 0; |
| 537 |
for ( j = 0; j <= 12; j++ ) { |
| 538 |
if ( time_est > 0.25 ) { time_est /= 10; precision++; } |
| 539 |
} |
| 540 |
sprintf( progress_format, PROGRESS_FMT, precision ); |
| 541 |
|
| 542 |
err = hashcash_mint( now_time, ent->width, ent->str, ent->bits, |
| 543 |
ent->anon, &new_token, &anon_random, |
| 544 |
&tries_taken, ext, compress, |
| 545 |
callback, NULL ); |
| 546 |
end = clock(); |
| 547 |
|
| 548 |
switch ( err ) { |
| 549 |
case HASHCASH_INVALID_TOK_LEN: |
| 550 |
die_msg( "error: maximum preimage with sha1 is 160 bits" ); |
| 551 |
case HASHCASH_RNG_FAILED: |
| 552 |
die_msg( "error: random number generator failed" ); |
| 553 |
case HASHCASH_INVALID_TIME: |
| 554 |
die_msg( "error: outside unix time Epoch" ); |
| 555 |
case HASHCASH_TOO_MANY_TRIES: |
| 556 |
die_msg( "error: failed to find preimage in 2^96 tries!" ); |
| 557 |
case HASHCASH_EXPIRED_ON_CREATION: |
| 558 |
die_msg( "error: -a stamp may expire on creation" ); |
| 559 |
case HASHCASH_INVALID_VALIDITY_PERIOD: |
| 560 |
die_msg( "error: negative validity period" ); |
| 561 |
case HASHCASH_INVALID_TIME_WIDTH: |
| 562 |
die_msg( "error: invalid time width" ); |
| 563 |
case HASHCASH_INTERNAL_ERROR: |
| 564 |
die_msg( "error: internal error" ); |
| 565 |
case HASHCASH_OK: |
| 566 |
break; |
| 567 |
default: |
| 568 |
die_msg( "error: unknown failure" ); |
| 569 |
} |
| 570 |
|
| 571 |
if ( anon_flag ) { |
| 572 |
VPRINTF( stderr, "anon period: %ld seconds\n", |
| 573 |
(long)anon_period ); |
| 574 |
VPRINTF( stderr, "adding: %ld seconds\n", anon_random ); |
| 575 |
} |
| 576 |
|
| 577 |
VPRINTF( stderr, "tries: %.0f", tries_taken ); |
| 578 |
VPRINTF( stderr, " %.0f%% of expected \n", |
| 579 |
tries_taken / tries_expected * 100 ); |
| 580 |
|
| 581 |
if ( end < start ) { tmp = end; end = start; start = tmp; } |
| 582 |
taken = (end-start)/(double)CLOCKS_PER_SEC; |
| 583 |
VPRINTF( stderr, "time: %.0f seconds\n", taken ); |
| 584 |
|
| 585 |
if ( hdr_flag ) { |
| 586 |
header_wrapped = hashcash_make_header( new_token, HDR_LINE_LEN, |
| 587 |
header, '\t', |
| 588 |
LINEFEED ); |
| 589 |
if ( header_wrapped == NULL ) { |
| 590 |
/* don't expect this but for security fail closed */ |
| 591 |
fprintf(stderr, "out of memory\n"); |
| 592 |
exit( EXIT_FAILURE ); |
| 593 |
} |
| 594 |
fprintf( stdout, "%s", header_wrapped ); |
| 595 |
free( header_wrapped ); |
| 596 |
} else { |
| 597 |
fprintf( stdout, "%s%s\n", |
| 598 |
((quiet_flag||!out_is_tty)? "" : "hashcash stamp: "), |
| 599 |
new_token ); |
| 600 |
} |
| 601 |
free( new_token ); |
| 602 |
} |
| 603 |
exit( EXIT_SUCCESS ); |
| 604 |
|
| 605 |
} else if ( check_flag || name_flag || width_flag || |
| 606 |
left_flag || bits_flag || res_flag || db_flag ) { |
| 607 |
|
| 608 |
/* check token is valid */ |
| 609 |
trimspace( header ); /* be forgiving about missing space */ |
| 610 |
hdr_len = strlen( header ); |
| 611 |
|
| 612 |
if ( header2[0] ) { |
| 613 |
trimspace( header2 ); |
| 614 |
hdr2_len = strlen( header2 ); |
| 615 |
} |
| 616 |
|
| 617 |
/* if no resources given create a kind of NULL entry to carry */ |
| 618 |
/* the other options along; hashcash_check knows to not check */ |
| 619 |
/* resource name if resource given is NULL */ |
| 620 |
|
| 621 |
if ( array_num( &resource ) == 0 ) { |
| 622 |
array_push( &resource, NULL, str_type, case_flag, validity_period, |
| 623 |
grace_period, anon_period, time_width, bits, 0 ); |
| 624 |
} |
| 625 |
|
| 626 |
hdrs_found = 0; |
| 627 |
tty_info = 0; |
| 628 |
in_headers = 0; |
| 629 |
|
| 630 |
ahead[0] = '\0'; |
| 631 |
for ( t = 0, token_found = 0, boundary = 0; !boundary; ) { |
| 632 |
/* read tokens from cmd line args 1st */ |
| 633 |
if ( t < array_num( &tokens ) ) { |
| 634 |
sstrncpy( token, tokens.elt[t].str, MAX_TOK ); |
| 635 |
t++; |
| 636 |
token_found = 1; |
| 637 |
} |
| 638 |
/* if no hdr flag, and were cmd line tokens, we're done */ |
| 639 |
else if ( !hdr_flag && array_num( &tokens ) > 0 ) { break; } |
| 640 |
else { |
| 641 |
if ( !in_headers ) { in_headers = 1; } |
| 642 |
if ( in_is_tty && !tty_info ) { |
| 643 |
if ( hdr_flag ) { |
| 644 |
if ( ignore_boundary_flag ) { |
| 645 |
QPRINTF( stderr, "enter email headers followed by mail body:\n" ); |
| 646 |
} else { |
| 647 |
QPRINTF( stderr, "enter email headers:\n" ); |
| 648 |
} |
| 649 |
} else { |
| 650 |
QPRINTF( stderr, |
| 651 |
"enter stamps to check one per line:\n" ); |
| 652 |
} |
| 653 |
tty_info = 1; |
| 654 |
} |
| 655 |
if ( read_eof( stdin, ahead ) ) { break; } |
| 656 |
read_header( stdin, &line, &line_max, &line_alloc, |
| 657 |
ahead, MAX_LINE ); |
| 658 |
if ( line[0] == '\0' ) { continue; } |
| 659 |
if ( hdr_flag ) { |
| 660 |
token_found = (strncasecmp( line, header, |
| 661 |
hdr_len ) == 0); |
| 662 |
token2_found = header2[0] && |
| 663 |
(strncasecmp( line, header2, hdr2_len ) == 0); |
| 664 |
if ( token_found ) { |
| 665 |
sstrncpy( token, line+hdr_len, MAX_TOK ); |
| 666 |
hdrs_found = 1; |
| 667 |
} else if ( token2_found ) { |
| 668 |
sstrncpy( token, line+hdr2_len, MAX_TOK ); |
| 669 |
hdrs_found = 1; |
| 670 |
token_found = 1; |
| 671 |
} |
| 672 |
} else { |
| 673 |
token_found = 1; |
| 674 |
sstrncpy( token, line, MAX_TOK ); |
| 675 |
} |
| 676 |
trimspace( token ); |
| 677 |
} |
| 678 |
|
| 679 |
/* end of stamp finder which results in clean stamp in token */ |
| 680 |
|
| 681 |
if ( token_found ) { |
| 682 |
VPRINTF( stderr, "examining stamp: %s\n", token ); |
| 683 |
|
| 684 |
skip = 0; |
| 685 |
over = 0; |
| 686 |
accept = 0; |
| 687 |
for ( i = 0; i < array_num( &resource ) && !skip; i++ ) { |
| 688 |
ent = &resource.elt[i]; |
| 689 |
|
| 690 |
/* keep going until find non overridden */ |
| 691 |
|
| 692 |
if ( over && ent->over ) { |
| 693 |
if ( multiple_resources ) { |
| 694 |
VPRINTF( stderr, "checking against resource: %s\n", |
| 695 |
ent->str ); |
| 696 |
} |
| 697 |
QPRINTF( stderr, "no match: overridden resource\n" ); |
| 698 |
continue; |
| 699 |
} |
| 700 |
|
| 701 |
over = 0; |
| 702 |
|
| 703 |
if ( !ent->case_flag ) { stolower( ent->str ); } |
| 704 |
valid_for = hashcash_check( token, ent->case_flag, |
| 705 |
ent->str, |
| 706 |
&(ent->regexp), &re_err, |
| 707 |
ent->type, now_time, |
| 708 |
ent->validity, ent->grace, |
| 709 |
bits_flag ? ent->bits : 0, |
| 710 |
&token_time ); |
| 711 |
|
| 712 |
if ( valid_for < 0 ) { |
| 713 |
switch ( valid_for ) { |
| 714 |
/* reasons token is broken, give up */ |
| 715 |
|
| 716 |
case HASHCASH_INVALID: |
| 717 |
QPRINTF( stderr, "skipped: stamp invalid\n" ); |
| 718 |
skip = 1; continue; |
| 719 |
|
| 720 |
case HASHCASH_UNSUPPORTED_VERSION: |
| 721 |
QPRINTF( stderr, "skipped: unsupported version\n" ); |
| 722 |
skip = 1; continue; |
| 723 |
|
| 724 |
/* avoid plural reporting if one of something */ |
| 725 |
/* not possible for match with */ |
| 726 |
/* other resources in these situations */ |
| 727 |
/* as will just fail for same reason again */ |
| 728 |
|
| 729 |
case HASHCASH_WRONG_RESOURCE: |
| 730 |
if ( !multiple_resources ) { |
| 731 |
QPRINTF( stderr, "skipped: stamp with wrong resource\n" ); |
| 732 |
skip = 1; continue; |
| 733 |
} |
| 734 |
break; |
| 735 |
|
| 736 |
case HASHCASH_INSUFFICIENT_BITS: |
| 737 |
if ( !multiple_bits && bits_flag ) { |
| 738 |
QPRINTF( stderr, |
| 739 |
"skipped: stamp has insufficient bits\n" ); |
| 740 |
skip = 1; continue; |
| 741 |
} |
| 742 |
break; |
| 743 |
|
| 744 |
case HASHCASH_EXPIRED: |
| 745 |
if ( !multiple_validity && check_flag ) { |
| 746 |
QPRINTF( stderr, "skipped: stamp expired\n" ); |
| 747 |
skip = 1; continue; |
| 748 |
} |
| 749 |
break; |
| 750 |
|
| 751 |
case HASHCASH_VALID_IN_FUTURE: |
| 752 |
if ( !multiple_grace && check_flag ) { |
| 753 |
QPRINTF( stderr, "skipped: stamp valid in future\n" ); |
| 754 |
skip = 1; continue; |
| 755 |
} |
| 756 |
break; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
if ( multiple_resources ) { |
| 761 |
VPRINTF( stderr, "checking against resource: %s\n", |
| 762 |
ent->str ); |
| 763 |
} |
| 764 |
|
| 765 |
if ( valid_for >= 0 ) { break; } |
| 766 |
|
| 767 |
if ( width_flag || left_flag || name_flag ) { |
| 768 |
accept = 1; |
| 769 |
} |
| 770 |
|
| 771 |
/* reason token failed for this resource */ |
| 772 |
/* but may succeed with diff resource */ |
| 773 |
|
| 774 |
switch ( valid_for ) { |
| 775 |
|
| 776 |
/* need non override to continue after this */ |
| 777 |
/* kind of failure */ |
| 778 |
|
| 779 |
case HASHCASH_INSUFFICIENT_BITS: |
| 780 |
if ( bits_flag ) { |
| 781 |
QPUTS( stderr, "no match: stamp has insufficient bits\n" ); |
| 782 |
over = 1; accept = 0; continue; |
| 783 |
} |
| 784 |
break; |
| 785 |
case HASHCASH_VALID_IN_FUTURE: |
| 786 |
if ( check_flag ) { |
| 787 |
QPRINTF( stderr, "no match: stamp valid in future\n" ); |
| 788 |
over = 1; accept = 0; continue; |
| 789 |
} |
| 790 |
break; |
| 791 |
case HASHCASH_EXPIRED: |
| 792 |
if ( check_flag ) { |
| 793 |
QPUTS( stderr, "no match: stamp expired\n" ); |
| 794 |
over = 1; accept = 0; continue; |
| 795 |
} |
| 796 |
break; |
| 797 |
case HASHCASH_WRONG_RESOURCE: |
| 798 |
QPRINTF( stderr, "no match: wrong resource\n" ); |
| 799 |
accept = 0; |
| 800 |
continue; |
| 801 |
|
| 802 |
/* fatal error stop */ |
| 803 |
|
| 804 |
case HASHCASH_REGEXP_ERROR: |
| 805 |
fprintf( stderr, "regexp error: " ); |
| 806 |
die_msg( re_err ); |
| 807 |
break; |
| 808 |
|
| 809 |
default: |
| 810 |
die_msg( "internal error" ); |
| 811 |
break; |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
if ( valid_for >= 0 || accept ) { |
| 816 |
if ( db_flag ) { |
| 817 |
if ( !db_opened ) { |
| 818 |
db_open( &db, db_filename ); |
| 819 |
db_opened = 1; |
| 820 |
} |
| 821 |
if ( db_in( &db, token, token_utime ) ) { |
| 822 |
QPRINTF( stderr, "skipped: spent stamp\n" ); |
| 823 |
valid_for = HASHCASH_SPENT; |
| 824 |
continue; /* to next token */ |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
QPRINTF( stderr, "matched stamp: %s\n", token ); |
| 829 |
|
| 830 |
if ( name_flag || verbose_flag ) { |
| 831 |
if ( ext ) { free( ext ); ext = NULL; } |
| 832 |
hashcash_parse( token, &vers, &claimed_bits, |
| 833 |
utcttime, MAX_UTC, |
| 834 |
token_resource, MAX_RES, &ext, 0 ); |
| 835 |
parsed = 1; |
| 836 |
QPRINTF( stderr, "stamp resource name: %s\n", |
| 837 |
token_resource ); |
| 838 |
PPRINTF( stdout, "%s", token_resource ); |
| 839 |
} |
| 840 |
|
| 841 |
if ( width_flag || verbose_flag ) { |
| 842 |
count_bits = hashcash_count( token ); |
| 843 |
if ( !parsed ) { |
| 844 |
hashcash_parse( token, &vers, &claimed_bits, |
| 845 |
utcttime, MAX_UTC, |
| 846 |
token_resource, MAX_RES, &ext, 0 ); |
| 847 |
parsed = 1; |
| 848 |
} |
| 849 |
if ( vers == 1 ) { |
| 850 |
count_bits = ( count_bits < claimed_bits ) ? |
| 851 |
0 : claimed_bits; |
| 852 |
} |
| 853 |
QPRINTF( stderr, "stamp value: %d\n", count_bits ); |
| 854 |
if ( name_flag || verbose_flag ) { |
| 855 |
PPUTS( stdout, " " ); |
| 856 |
} |
| 857 |
PPRINTF( stdout, "%d", count_bits ); |
| 858 |
} |
| 859 |
|
| 860 |
if ( left_flag || verbose_flag ) { |
| 861 |
QPRINTF( stderr, "valid: " ); |
| 862 |
if ( valid_for > 0 ) { |
| 863 |
QPRINTF( stderr, "for %ld seconds\n", |
| 864 |
(long)valid_for ); |
| 865 |
if ( name_flag || width_flag ) { |
| 866 |
PPUTS( stdout, " " ); |
| 867 |
} |
| 868 |
PPRINTF( stdout, "%ld", (long)valid_for ); |
| 869 |
} else { |
| 870 |
expiry_time = token_time + validity_period; |
| 871 |
switch ( valid_for ) { |
| 872 |
case HASHCASH_VALID_FOREVER: |
| 873 |
QPUTS( stderr, "forever\n" ); |
| 874 |
if ( name_flag || width_flag ) { |
| 875 |
PPUTS( stdout, " " ); |
| 876 |
} |
| 877 |
PPUTS( stdout, "0" ); |
| 878 |
break; |
| 879 |
case HASHCASH_VALID_IN_FUTURE: |
| 880 |
QPRINTF( stderr, "in %ld seconds\n", |
| 881 |
token_time-(now_time+grace_period) ); |
| 882 |
if ( name_flag || width_flag ) { |
| 883 |
PPUTS( stdout, " " ); |
| 884 |
} |
| 885 |
PPRINTF( stdout, "+%ld", |
| 886 |
token_time-(now_time+grace_period) ); |
| 887 |
break; |
| 888 |
case HASHCASH_EXPIRED: |
| 889 |
QPRINTF( stderr, "expired %ld seconds ago\n", |
| 890 |
now_time-(expiry_time+grace_period) ); |
| 891 |
if ( name_flag || width_flag ) { |
| 892 |
PPUTS( stdout, " " ); |
| 893 |
} |
| 894 |
PPRINTF( stdout, "-%ld", |
| 895 |
now_time-(expiry_time+grace_period) ); |
| 896 |
break; |
| 897 |
default: |
| 898 |
QPRINTF( stderr, "not valid\n" ); |
| 899 |
} |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
if ( name_flag || width_flag || left_flag ) { |
| 904 |
PPUTS( stdout, "\n" ); |
| 905 |
continue; /* to next token */ |
| 906 |
} |
| 907 |
|
| 908 |
if ( db_flag ) { |
| 909 |
VPUTS( stderr, |
| 910 |
"database: not double spent\n" ); |
| 911 |
checked = yes_flag || ( res_flag && bits_flag); |
| 912 |
if ( checked ) { |
| 913 |
sprintf( period, "%ld", validity_period ); |
| 914 |
db_add( &db, token, period ); |
| 915 |
} |
| 916 |
} else { |
| 917 |
checked = yes_flag; |
| 918 |
} |
| 919 |
goto leave; /* matched so leave */ |
| 920 |
} |
| 921 |
} else if ( hdr_flag && in_headers && !ignore_boundary_flag ) { |
| 922 |
if ( line[0] == '\0' ) { boundary = 1; } |
| 923 |
} |
| 924 |
} |
| 925 |
if ( !width_flag && !left_flag && !name_flag ) { |
| 926 |
if ( hdr_flag && !hdrs_found ) { |
| 927 |
QPRINTF( stderr, "warning: no line matching %s found in input\n", |
| 928 |
header ); |
| 929 |
} |
| 930 |
QPUTS( stderr, "rejected: no valid stamps found\n" ); |
| 931 |
valid_for = HASHCASH_NO_TOKEN; |
| 932 |
} |
| 933 |
|
| 934 |
leave: |
| 935 |
if ( width_flag || left_flag || name_flag ) { |
| 936 |
exit( yes_flag ? EXIT_SUCCESS : EXIT_UNCHECKED ); |
| 937 |
} |
| 938 |
QPRINTF( stderr, "check: %s", |
| 939 |
( valid_for >= 0 ) ? |
| 940 |
( checked ? "ok" : "ok but not fully checked as" ) : |
| 941 |
"failed" ); |
| 942 |
if ( valid_for >= 0 ) { |
| 943 |
if ( !checked ) { |
| 944 |
if ( !bits_flag ) { |
| 945 |
comma = 1; QPUTS( stderr, " bits" ); |
| 946 |
} |
| 947 |
if ( !check_flag ) { |
| 948 |
if ( comma ) { QPUTS( stderr, "," ); } |
| 949 |
comma = 1; QPUTS( stderr, " expiry" ); |
| 950 |
} |
| 951 |
if ( !res_flag ) { |
| 952 |
if ( comma ) { QPUTS( stderr, "," ); } |
| 953 |
comma = 1; QPUTS( stderr, " resource" ); |
| 954 |
} |
| 955 |
if ( !db_flag ) { |
| 956 |
if ( comma ) { QPUTS( stderr, "," ); } |
| 957 |
comma = 1; QPUTS( stderr, " database" ); |
| 958 |
} |
| 959 |
if ( comma ) { QPUTS( stderr, " not specified" ); } |
| 960 |
} |
| 961 |
} |
| 962 |
QPUTS( stderr, "\n" ); |
| 963 |
if ( valid_for < 0 ) { exit( EXIT_FAILURE ); } |
| 964 |
exit( checked ? EXIT_SUCCESS : |
| 965 |
( yes_flag ? EXIT_SUCCESS : EXIT_UNCHECKED ) ); |
| 966 |
} |
| 967 |
exit( EXIT_ERROR ); /* get here if no functional flags */ |
| 968 |
return 0; |
| 969 |
} |
| 970 |
|
| 971 |
int progress_callback(int ignore, int largest, int target, |
| 972 |
double counter, double expected, void* user) |
| 973 |
{ |
| 974 |
static int previous_percent = -1; |
| 975 |
static int previous_largest = -1; |
| 976 |
static double previous_counter = -1; |
| 977 |
double percent; |
| 978 |
|
| 979 |
if ( previous_counter != counter || previous_largest != largest ) { |
| 980 |
percent = counter*100/expected; |
| 981 |
fprintf( stderr, progress_format, |
| 982 |
counter, expected, percent, largest, target, |
| 983 |
(previous_largest == largest) ? '\r' : '\n' ); |
| 984 |
previous_percent = percent; |
| 985 |
previous_largest = largest; |
| 986 |
previous_counter = counter; |
| 987 |
} |
| 988 |
return 1; |
| 989 |
} |
| 990 |
|
| 991 |
int parse_period( const char* aperiod, long* resp ) |
| 992 |
{ |
| 993 |
int period_len; |
| 994 |
char last_char; |
| 995 |
long res = 1; |
| 996 |
char period_array[MAX_PERIOD+1]; |
| 997 |
char* period = period_array; |
| 998 |
|
| 999 |
if ( period == NULL ) { return 0; } |
| 1000 |
period_len = strlen( aperiod ); |
| 1001 |
if ( period_len == 0 ) { return 0; } |
| 1002 |
last_char = aperiod[period_len-1]; |
| 1003 |
if ( ! isdigit( last_char ) && ! strchr( "YyMdhmsw", last_char ) ) { |
| 1004 |
return 0; |
| 1005 |
} |
| 1006 |
|
| 1007 |
sstrncpy( period, aperiod, MAX_PERIOD ); |
| 1008 |
|
| 1009 |
if ( ! isdigit( last_char ) ) { period[--period_len] = '\0'; } |
| 1010 |
if ( period[0] == '+' || period[0] == '-' ) { |
| 1011 |
if ( period[0] == '-' ) { res = -1; } |
| 1012 |
period++; period_len--; |
| 1013 |
} |
| 1014 |
if ( period_len > 0 ) { res *= atoi( period ); } |
| 1015 |
switch ( last_char ) |
| 1016 |
{ |
| 1017 |
case 's': break; |
| 1018 |
case 'm': res *= TIME_MINUTE; break; |
| 1019 |
case 'h': res *= TIME_HOUR; break; |
| 1020 |
case 'd': res *= TIME_DAY; break; |
| 1021 |
case 'w': res *= TIME_DAY*7; break; |
| 1022 |
case 'M': res *= TIME_MONTH; break; |
| 1023 |
case 'y': case 'Y': res *= TIME_YEAR; break; |
| 1024 |
case '0': case '1': case '2': case '3': case '4': |
| 1025 |
case '5': case '6': case '7': case '8': case '9': break; |
| 1026 |
default: return 0; |
| 1027 |
} |
| 1028 |
*resp = res; |
| 1029 |
return 1; |
| 1030 |
} |
| 1031 |
|
| 1032 |
void usage( const char* msg ) |
| 1033 |
{ |
| 1034 |
if ( msg ) { fputs( msg, stderr ); fputs( "\n\n", stderr ); } |
| 1035 |
fprintf( stderr, "mint:\t\thashcash [-m] [opts] resource\n" ); |
| 1036 |
fprintf( stderr, "measure speed:\thashcash -s [-b bits]\n" ); |
| 1037 |
fprintf( stderr, "check:\t\thashcash -c [opts] -d -r resource [-e period] [stamp]\n" ); |
| 1038 |
fprintf( stderr, "purge expired:\thashcash -p now [-k] [-j resource] [-t time] [-u]\n" ); |
| 1039 |
fprintf( stderr, "count bits:\thashcash -w [opts] [stamp]\n" ); |
| 1040 |
fprintf( stderr, "get resource:\thashcash -n [opts] [stamp]\n" ); |
| 1041 |
fprintf( stderr, "time remaining:\thashcash -l [opts] -e period [stamp]\n" ); |
| 1042 |
fprintf( stderr, "\n" ); |
| 1043 |
fprintf( stderr, "\t-b bits\t\tfind or check partial preimage of length bits\n" ); |
| 1044 |
fprintf( stderr, "\t-d\t\tuse database (for double spending detection)\n" ); |
| 1045 |
fprintf( stderr, "\t-r resource\tresource name for minting or checking stamp\n" ); |
| 1046 |
fprintf( stderr, "\t-o\t\tprevious resource overrides this resource\n" ); |
| 1047 |
fprintf( stderr, "\t-e period\ttime until stamp expires\n" ); |
| 1048 |
fprintf( stderr, "\t-g\t\tgrace period for clock skew\n" ); |
| 1049 |
fprintf( stderr, "\t-t time\t\tmodify current time stamp created at\n" ); |
| 1050 |
fprintf( stderr, "\t-a time\t\tmodify time by random amount in range given\n" ); |
| 1051 |
fprintf( stderr, "\t-u\t\tgive time in UTC instead of local time\n" ); |
| 1052 |
fprintf( stderr, "\t-q\t\tquiet -- suppress all informational output\n" ); |
| 1053 |
fprintf( stderr, "\t-v\t\tprint verbose informational output\n" ); |
| 1054 |
fprintf( stderr, "\t-h\t\tprint this usage info\n" ); |
| 1055 |
fprintf( stderr, "\t-f dbfile\tuse filename dbfile for database\n" ); |
| 1056 |
fprintf( stderr, "\t-j resource\twith -p delete just stamps matching the given resource\n" ); |
| 1057 |
fprintf( stderr, "\t-k\t\twith -p delete all not just expired\n" ); |
| 1058 |
fprintf( stderr, "\t-x ext\t\tput in extension field\n" ); |
| 1059 |
fprintf( stderr, "\t-X\t\toutput with header format 'X-Hashcash: '\n" ); |
| 1060 |
fprintf( stderr, "\t-i\t\twith -X and -c, check msg body as well\n" ); |
| 1061 |
fprintf( stderr, "\t-y\t\treturn success if stamps is valid but not fully checked\n" ); |
| 1062 |
fprintf( stderr, "\t-z width\twidth of time field 6,10 or 12 chars (default 6)\n" ); |
| 1063 |
fprintf( stderr, "\t-C\t\tmatch resources as case sensitive (default insensitive)\n" ); |
| 1064 |
fprintf( stderr, "\t-S\t\tmatch following resources as text strings\n" ); |
| 1065 |
fprintf( stderr, "\t-W\t\tmatch following resources with wildcards (default)\n" ); |
| 1066 |
fprintf( stderr, "\t-E\t\tmatch following resources as regular expression\n" ); |
| 1067 |
fprintf( stderr, "\t-P\t\tshow progress while searching\n"); |
| 1068 |
fprintf( stderr, "\t-O core\t\tuse specified minting core\n"); |
| 1069 |
fprintf( stderr, "\t-Z n\t\t0 = fast (default), 1 = medium, 2 = small/slow\n"); |
| 1070 |
fprintf( stderr, "examples:\n" ); |
| 1071 |
fprintf( stderr, "\thashcash -mb20 foo # mint 20 bit preimage\n" ); |
| 1072 |
fprintf( stderr, "\thashcash -cdb20 -r foo 1:20:040806:foo::831d0c6f22eb81ff:15eae4 # check preimage\n" ); |
| 1073 |
fprintf( stderr, "\n" ); |
| 1074 |
fprintf( stderr, "see hashcash (1) man page or http://www.hashcash.org/ for more details.\n" ); |
| 1075 |
fflush( stderr ); |
| 1076 |
exit( EXIT_ERROR ); |
| 1077 |
} |
| 1078 |
|
| 1079 |
typedef struct { |
| 1080 |
time_t expires_before; |
| 1081 |
char now_utime[ MAX_UTC+1 ]; |
| 1082 |
int all; |
| 1083 |
long validity; |
| 1084 |
long grace; |
| 1085 |
ARRAY* resource; |
| 1086 |
} db_arg; |
| 1087 |
|
| 1088 |
/* compile time assert */ |
| 1089 |
|
| 1090 |
#if MAX_UTC > MAX_VAL |
| 1091 |
#error "MAX_UTC must be less than MAX_VAL" |
| 1092 |
#endif |
| 1093 |
|
| 1094 |
static int sdb_cb_token_matcher( const char* key, char* val, |
| 1095 |
void* argp, int* err ) { |
| 1096 |
db_arg* arg = (db_arg*)argp; |
| 1097 |
char token_utime[ MAX_UTC+1 ] = {0}; |
| 1098 |
char token_res[ MAX_RES+1 ] = {0}, lower_token_res[ MAX_RES+1 ] = {0}; |
| 1099 |
char *res = NULL; |
| 1100 |
time_t expires = 0; |
| 1101 |
time_t expiry_period = 0; |
| 1102 |
time_t created = 0; |
| 1103 |
int vers = 0, i = 0, bits = 0, matched = 0, type = 0, case_flag = 0; |
| 1104 |
int lowered = 0 ; |
| 1105 |
void** compile = NULL; |
| 1106 |
char* re_err = NULL; |
| 1107 |
|
| 1108 |
*err = 0; |
| 1109 |
if ( strcmp( key, PURGED_KEY ) == 0 ) { |
| 1110 |
sstrncpy( val, arg->now_utime, MAX_UTC ); |
| 1111 |
return 1; /* update purge time */ |
| 1112 |
} |
| 1113 |
|
| 1114 |
if ( !hashcash_parse( key, &vers, &bits, token_utime, MAX_UTC, |
| 1115 |
token_res, MAX_RES, NULL, 0 ) ) { |
| 1116 |
*err = EINPUT; /* corrupted token in DB */ |
| 1117 |
return 0; |
| 1118 |
} |
| 1119 |
if ( vers != 0 && vers != 1 ) { |
| 1120 |
*err = EINPUT; /* unsupported version number in DB */ |
| 1121 |
return 0; |
| 1122 |
} |
| 1123 |
/* if purging only for given resource */ |
| 1124 |
if ( array_num( arg->resource ) > 0 ) { |
| 1125 |
matched = 0; |
| 1126 |
for ( i = 0; !matched && i < array_num( arg->resource ); i++ ) { |
| 1127 |
res = arg->resource->elt[i].str; |
| 1128 |
type = arg->resource->elt[i].type; |
| 1129 |
case_flag = arg->resource->elt[i].case_flag; |
| 1130 |
compile = &(arg->resource->elt[i].regexp); |
| 1131 |
if ( !case_flag && !lowered ) { |
| 1132 |
strncpy( lower_token_res, token_res, MAX_RES ); |
| 1133 |
stolower( lower_token_res ); |
| 1134 |
lowered = 1; |
| 1135 |
} |
| 1136 |
matched = hashcash_resource_match( type, case_flag ? |
| 1137 |
token_res : lower_token_res, |
| 1138 |
res, compile, &re_err ); |
| 1139 |
if ( re_err != NULL ) { |
| 1140 |
fprintf( stderr, "regexp error: " ); |
| 1141 |
die_msg( re_err ); |
| 1142 |
} |
| 1143 |
} |
| 1144 |
if ( !matched ) { return 1; } /* if it doesn't match, keep it */ |
| 1145 |
} |
| 1146 |
if ( arg->all ) { return 0; } /* delete all regardless of expiry */ |
| 1147 |
if ( val[0] == '0' && val[1] == '\0' ) { return 1; } /* keep forever */ |
| 1148 |
if ( strlen( val ) != strspn( val, "0123456789" ) ) { |
| 1149 |
*err = EINPUT; return 0; |
| 1150 |
} |
| 1151 |
expiry_period = atoi( val ); |
| 1152 |
if ( expiry_period < 0 ) { *err = EINPUT; return 0; } /* corrupted */ |
| 1153 |
created = hashcash_from_utctimestr( token_utime, 1 ); |
| 1154 |
if ( created < 0 ) { *err = EINPUT; return 0; } /* corrupted */ |
| 1155 |
expires = created |
| 1156 |
+ (arg->validity ? arg->validity : expiry_period) + arg->grace; |
| 1157 |
if ( expires <= arg->expires_before ) { return 0; } /* delete if expired */ |
| 1158 |
return 1; /* otherwise keep */ |
| 1159 |
} |
| 1160 |
|
| 1161 |
int hashcash_db_purge( DB* db, const char* purge_resource, int type, |
| 1162 |
int case_flag, long validity_period, long grace_period, |
| 1163 |
int purge_all, long purge_period, time_t now_time, |
| 1164 |
int* err ) { |
| 1165 |
ARRAY* purge_resource_arr = NULL; |
| 1166 |
array_alloc( purge_resource_arr, 1 ); |
| 1167 |
array_push( purge_resource_arr, purge_resource, type, case_flag, |
| 1168 |
validity_period, grace_period, 0, 0, 0, 0 ); |
| 1169 |
return db_purge( db, purge_resource_arr, purge_all, purge_period, |
| 1170 |
now_time, validity_period, grace_period, 0, err ); |
| 1171 |
} |
| 1172 |
|
| 1173 |
void db_purge_arr( DB* db, ARRAY* purge_resource, int purge_all, |
| 1174 |
long purge_period, time_t now_time, long validity_period, |
| 1175 |
long grace_period ) { |
| 1176 |
int res, err = HASHCASH_FAIL; |
| 1177 |
res = db_purge( db, purge_resource, purge_all, purge_period, now_time, |
| 1178 |
validity_period, grace_period, verbose_flag, &err ); |
| 1179 |
switch ( res ) { |
| 1180 |
case HASHCASH_INVALID_TIME: |
| 1181 |
die_msg( "error: invalid time argument" ); break; |
| 1182 |
case HASHCASH_FAIL: die( err ); break; |
| 1183 |
case HASHCASH_OK: break; |
| 1184 |
default: die( err ); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
int db_purge( DB* db, ARRAY* purge_resource, int purge_all, |
| 1189 |
long purge_period, time_t now_time, long validity_period, |
| 1190 |
long grace_period, int verbose, int* err ) { |
| 1191 |
time_t last_time = 0 ; |
| 1192 |
char purge_utime[ MAX_UTC+1 ] = {0}; /* time token created */ |
| 1193 |
int ret = 0; |
| 1194 |
db_arg arg; |
| 1195 |
|
| 1196 |
if ( now_time < 0 ) { return HASHCASH_INVALID_TIME; } |
| 1197 |
|
| 1198 |
if ( !sdb_lookup( db, PURGED_KEY, purge_utime, MAX_UTC, err ) ) { |
| 1199 |
return 0; |
| 1200 |
} |
| 1201 |
|
| 1202 |
last_time = hashcash_from_utctimestr( purge_utime, 1 ); |
| 1203 |
if ( last_time < 0 ) { /* not first time, but corrupted */ |
| 1204 |
purge_period = 0; /* purge now */ |
| 1205 |
} |
| 1206 |
|
| 1207 |
if ( !hashcash_to_utctimestr( arg.now_utime, MAX_UTC, now_time ) ) { |
| 1208 |
return HASHCASH_INVALID_TIME; |
| 1209 |
} |
| 1210 |
|
| 1211 |
arg.expires_before = now_time; |
| 1212 |
arg.resource = purge_resource; |
| 1213 |
arg.all = purge_all; |
| 1214 |
arg.validity = validity_period; |
| 1215 |
arg.grace = grace_period; |
| 1216 |
|
| 1217 |
if ( purge_period == 0 || now_time >= last_time + purge_period ) { |
| 1218 |
VPRINTF( stderr, "purging database: ..." ); |
| 1219 |
ret = sdb_updateiterate( db, sdb_cb_token_matcher, (void*)&arg, err ); |
| 1220 |
VPRINTF( stderr, ret ? "done\n" : "failed\n" ); |
| 1221 |
} else { |
| 1222 |
ret = 1; |
| 1223 |
} |
| 1224 |
return ret; |
| 1225 |
} |
| 1226 |
|
| 1227 |
void db_open( DB* db, const char* db_filename ) { |
| 1228 |
int err; |
| 1229 |
if (!hashcash_db_open( db, db_filename, &err )) { die(err); } |
| 1230 |
} |
| 1231 |
|
| 1232 |
int db_in( DB* db, char* token, char *period ) { |
| 1233 |
int err = 0; |
| 1234 |
int res; |
| 1235 |
|
| 1236 |
res = hashcash_db_in( db, token, period, &err ); |
| 1237 |
if ( err ) { die( err ); } |
| 1238 |
return res; |
| 1239 |
} |
| 1240 |
|
| 1241 |
void db_add( DB* db, char* token, char *period ) { |
| 1242 |
int err = 0; |
| 1243 |
if ( !hashcash_db_add( db, token, period, &err ) ) { |
| 1244 |
die( err ); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
void db_close( DB* db ) { |
| 1249 |
int err = 0; |
| 1250 |
if ( !hashcash_db_close( db, &err ) ) { |
| 1251 |
die( err ); |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
/* num = start size, auto-grows */ |
| 1256 |
|
| 1257 |
void array_alloc( ARRAY* array, int num ) { |
| 1258 |
array->num = 0; |
| 1259 |
array->max = num; |
| 1260 |
array->elt = malloc( sizeof( ELEMENT ) * num ); |
| 1261 |
if ( array->elt == NULL ) { die_msg( "out of memory" ); } |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* auto-grow array */ |
| 1265 |
|
| 1266 |
void array_push( ARRAY* array, const char *str, int type, int case_flag, |
| 1267 |
long validity, long grace, long anon, int width, int bits, |
| 1268 |
int over ) |
| 1269 |
{ |
| 1270 |
if ( array->num >= array->max ) { |
| 1271 |
array->elt = realloc( array->elt, sizeof( ELEMENT) * array->max * 2 ); |
| 1272 |
if ( array->elt == NULL ) { die_msg( "out of memory" ); } |
| 1273 |
array->max *= 2; |
| 1274 |
} |
| 1275 |
array->elt[array->num].regexp = NULL; /* compiled regexp */ |
| 1276 |
array->elt[array->num].type = type; |
| 1277 |
array->elt[array->num].case_flag = case_flag; |
| 1278 |
array->elt[array->num].validity = validity; |
| 1279 |
array->elt[array->num].grace = grace; |
| 1280 |
array->elt[array->num].anon = anon; |
| 1281 |
array->elt[array->num].width = width; |
| 1282 |
array->elt[array->num].bits = bits; |
| 1283 |
array->elt[array->num].over = over; |
| 1284 |
if ( str ) { |
| 1285 |
array->elt[array->num].str = strdup( str ); |
| 1286 |
if ( array->elt[array->num].str == NULL ) { |
| 1287 |
die_msg( "out of memory" ); |
| 1288 |
} |
| 1289 |
} else { |
| 1290 |
array->elt[array->num].str = NULL; |
| 1291 |
} |
| 1292 |
array->num++; |
| 1293 |
} |
| 1294 |
|
| 1295 |
int bit_cmp( const void* ap, const void* bp ) |
| 1296 |
{ |
| 1297 |
ELEMENT* a = (ELEMENT*) ap; |
| 1298 |
ELEMENT* b = (ELEMENT*) bp; |
| 1299 |
|
| 1300 |
return a->bits == b->bits ? 0 : ( a->bits < b->bits ? 1 : -1 ); |
| 1301 |
} |
| 1302 |
|
| 1303 |
void array_sort( ARRAY* array, int(*cmp)(const void*, const void*) ) |
| 1304 |
{ |
| 1305 |
qsort( array->elt, array->num, sizeof( ELEMENT ), cmp ); |
| 1306 |
} |
| 1307 |
|
| 1308 |
void die( int err ) |
| 1309 |
{ |
| 1310 |
const char* str = ""; |
| 1311 |
|
| 1312 |
switch ( err ) { |
| 1313 |
case EOK: |
| 1314 |
exit( EXIT_SUCCESS ); |
| 1315 |
break; |
| 1316 |
case EINPUT: |
| 1317 |
str = "invalid inputs"; |
| 1318 |
break; |
| 1319 |
default: |
| 1320 |
str = strerror( err ); |
| 1321 |
break; |
| 1322 |
} |
| 1323 |
QPRINTF( stderr, "error: %s\n", str ); |
| 1324 |
exit( EXIT_ERROR ); |
| 1325 |
} |
| 1326 |
|
| 1327 |
void die_msg( const char* str ) |
| 1328 |
{ |
| 1329 |
QPUTS( stderr, str ); |
| 1330 |
QPUTS( stderr, "\n" ); |
| 1331 |
exit( EXIT_ERROR ); |
| 1332 |
} |
| 1333 |
|
| 1334 |
/* remove unix, DOS and MAC linefeeds from line ending */ |
| 1335 |
|
| 1336 |
void chomplf( char* token ) { |
| 1337 |
int tok_len = strlen(token); |
| 1338 |
if ( token[tok_len-1] == '\n' ) { token[--tok_len] = '\0'; } |
| 1339 |
if ( token[tok_len-1] == '\r' ) { token[--tok_len] = '\0'; } |
| 1340 |
if ( token[tok_len-1] == '\n' ) { token[--tok_len] = '\0'; } |
| 1341 |
} |
| 1342 |
|
| 1343 |
void trimspace( char* token ) { |
| 1344 |
int tok_len = strlen(token); |
| 1345 |
int tok_begin = 0; |
| 1346 |
while ( tok_begin < tok_len && isspace(token[tok_begin]) ) { |
| 1347 |
tok_begin++; |
| 1348 |
} |
| 1349 |
if ( tok_begin > 0 ) { |
| 1350 |
tok_len -= tok_begin; |
| 1351 |
memmove( token, token+tok_begin, strlen(token+tok_begin)+1 ); |
| 1352 |
} |
| 1353 |
while ( tok_len > 0 && isspace(token[tok_len-1]) ) { |
| 1354 |
token[--tok_len] = '\0'; |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
double report_speed( int bits, double* time_est, int display ) |
| 1359 |
{ |
| 1360 |
double te = 0; |
| 1361 |
double tries_expected = hashcash_expected_tries( bits ); |
| 1362 |
|
| 1363 |
VPRINTF( stderr, "mint: %d bit partial hash preimage\n", bits ); |
| 1364 |
VPRINTF( stderr, "expected: %.0f tries (= 2^%d tries)\n", |
| 1365 |
tries_expected, bits ); |
| 1366 |
|
| 1367 |
if ( time_est ) { *time_est = hc_est_time( bits ); } |
| 1368 |
|
| 1369 |
if ( display ) { |
| 1370 |
te = hc_est_time( bits ); |
| 1371 |
QPRINTF( stderr, "time estimate: %.0f seconds", te ); |
| 1372 |
if ( te > TIME_AEON ) { |
| 1373 |
QPRINTF( stderr, " (%.0f aeons)", te / TIME_AEON ); |
| 1374 |
} else if ( te > TIME_YEAR * 10 ) { |
| 1375 |
QPRINTF( stderr, " (%.0f years)", te / TIME_YEAR ); |
| 1376 |
} else if ( te > TIME_YEAR ) { |
| 1377 |
QPRINTF( stderr, " (%.1f years)", te / TIME_YEAR ); |
| 1378 |
} else if ( te > TIME_MONTH ) { |
| 1379 |
QPRINTF( stderr, " (%.1f months)", te / TIME_MONTH ); |
| 1380 |
} else if ( te > TIME_DAY * 10 ) { |
| 1381 |
QPRINTF( stderr, " (%.0f days)", te / TIME_DAY ); |
| 1382 |
} else if ( te > TIME_DAY ) { |
| 1383 |
QPRINTF( stderr, " (%.1f days)", te / TIME_DAY ); |
| 1384 |
} else if ( te > TIME_HOUR ) { |
| 1385 |
QPRINTF( stderr, " (%.0f hours)", te / TIME_HOUR ); |
| 1386 |
} else if ( te > TIME_MINUTE ) { |
| 1387 |
QPRINTF( stderr, " (%.0f minutes)", te / TIME_MINUTE ); |
| 1388 |
} else if ( te > 1 ) { |
| 1389 |
/* already printed seconds */ |
| 1390 |
} else if ( te > TIME_MILLI_SECOND ) { |
| 1391 |
QPRINTF( stderr, " (%.0f milli-seconds)", |
| 1392 |
te / TIME_MILLI_SECOND ); |
| 1393 |
} else if ( te > TIME_MICRO_SECOND ) { |
| 1394 |
QPRINTF( stderr, " (%.0f micro-seconds)", |
| 1395 |
te / TIME_MICRO_SECOND ); |
| 1396 |
} else if ( te > TIME_NANO_SECOND ) { |
| 1397 |
QPRINTF( stderr, " (%.0f nano-seconds)", |
| 1398 |
te / TIME_NANO_SECOND ); |
| 1399 |
} |
| 1400 |
QPUTS( stderr, "\n" ); |
| 1401 |
} |
| 1402 |
return tries_expected; |
| 1403 |
} |
| 1404 |
|
| 1405 |
int read_append( char** s, int* smax, int* alloc, char* append ) |
| 1406 |
{ |
| 1407 |
int slen = strlen( *s ); |
| 1408 |
int alen = strlen( append ); |
| 1409 |
|
| 1410 |
if ( slen + alen > *smax ) { |
| 1411 |
if ( *alloc ) { |
| 1412 |
*s = realloc( *s, slen+alen+1 ); |
| 1413 |
} else { |
| 1414 |
*s = malloc( slen+alen+1 ); |
| 1415 |
*alloc = 1; |
| 1416 |
} |
| 1417 |
if ( *s == NULL ) { return 0; } /* out of memory */ |
| 1418 |
} |
| 1419 |
sstrncpy( (*s)+slen, append, alen ); |
| 1420 |
return 1; |
| 1421 |
} |
| 1422 |
|
| 1423 |
char *read_header( FILE* f, char** s, int* smax, int* alloc, |
| 1424 |
char* a, int amax ) |
| 1425 |
{ |
| 1426 |
char* junk; |
| 1427 |
|
| 1428 |
(*s)[0] = '\0'; |
| 1429 |
if ( a[0] ) { |
| 1430 |
sstrncpy( *s, a, *smax ); |
| 1431 |
} else { |
| 1432 |
junk = fgets( *s, *smax, f ); |
| 1433 |
chomplf( *s ); |
| 1434 |
} |
| 1435 |
|
| 1436 |
do { |
| 1437 |
a[0] = '\0'; |
| 1438 |
junk = fgets( a, amax, f ); |
| 1439 |
chomplf( a ); |
| 1440 |
if ( a[0] == '\t' || a[0] == ' ') { |
| 1441 |
read_append( s, smax, alloc, a+1 ); |
| 1442 |
} |
| 1443 |
} while ( a[0] == '\t' || a[0] == ' ' ); |
| 1444 |
|
| 1445 |
return *s; |
| 1446 |
} |
| 1447 |
|
| 1448 |
int read_eof( FILE* fp, char* a ) |
| 1449 |
{ |
| 1450 |
return feof(fp) && a[0] == '\0'; |
| 1451 |
} |
| 1452 |
|
| 1453 |
void mystolower( char* str ) { |
| 1454 |
if ( !str ) { return; } |
| 1455 |
for ( ; *str; str++ ) { |
| 1456 |
*str = tolower( *str ); |
| 1457 |
} |
| 1458 |
} |