| 1 | /* -*- Mode: C; c-file-style: "stroustrup" -*- */cxx . . $ `<file>/hashcash.h`/ |
| 2 | |
| 3 | #if !defined( _hashcash_h ) |
| 4 | #define _hashcash_hcxx . . $ `hashcash.h:4:9`! |
| 5 | |
| 6 | #if defined( __cplusplus ) |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
| 10 | #include <time.h> |
| 11 | |
| 12 | #if defined(WIN32) && !defined(MONOLITHIC) |
| 13 | #include <windows.h> |
| 14 | #endif |
| 15 | |
| 16 | #if !defined(HCEXPORT) |
| 17 | #if !defined(WIN32) || defined(MONOLITHIC) |
| 18 | #define HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 19 | #elif defined(BUILD_DLL) |
| 20 | #define HCEXPORT __declspec(dllexport) |
| 21 | #else /* USE_DLL */ |
| 22 | #define HCEXPORT extern __declspec(dllimport) |
| 23 | #endif |
| 24 | #endif |
| 25 | |
| 26 | extern int verbose_flagcxx . . $ verbose_flag.; |
| 27 | extern int no_purge_flagcxx . . $ no_purge_flag.; |
| 28 | |
| 29 | #define HASHCASH_VERSIONcxx . . $ `hashcash.h:29:9`! 1.22 |
| 30 | #define HASHCASH_FORMAT_VERSIONcxx . . $ `hashcash.h:30:9`! 1 |
| 31 | #define stringifycxx . . $ `hashcash.h:31:9`!( x ) stringify2( x ) |
| 32 | #define stringify2cxx . . $ `hashcash.h:32:9`!( x ) #x |
| 33 | #define HASHCASH_VERSION_STRINGcxx . . $ `hashcash.h:33:9`! stringify(HASHCASH_VERSION) |
| 34 | |
| 35 | #define TYPE_STRcxx . . $ `hashcash.h:35:9`! 0 |
| 36 | #define TYPE_WILDcxx . . $ `hashcash.h:36:9`! 1 |
| 37 | #define TYPE_REGEXPcxx . . $ `hashcash.h:37:9`! 2 |
| 38 | |
| 39 | #define MAX_UTCcxx . . $ `hashcash.h:39:9`! 13 |
| 40 | #define MAX_CTRcxx . . $ `hashcash.h:40:9`! 64 |
| 41 | #define MAX_REScxx . . $ `hashcash.h:41:9`! 256 |
| 42 | #define MAX_STRcxx . . $ `hashcash.h:42:9`! 256 |
| 43 | #define MAX_VERcxx . . $ `hashcash.h:43:9`! 2 |
| 44 | #define MAX_EXTcxx . . $ `hashcash.h:44:9`! 10240 |
| 45 | #define MAX_TOKcxx . . $ `hashcash.h:45:9`! ((MAX_VER)+1+(MAX_RES)+1+(MAX_EXT)+1+(MAX_UTC)+1+(MAX_STR)) |
| 46 | |
| 47 | #define TIME_MINUTEcxx . . $ `hashcash.h:47:9`! 60 |
| 48 | #define TIME_HOURcxx . . $ `hashcash.h:48:9`! (TIME_MINUTE*60) |
| 49 | #define TIME_DAYcxx . . $ `hashcash.h:49:9`! (TIME_HOUR*24) |
| 50 | #define TIME_YEARcxx . . $ `hashcash.h:50:9`! (TIME_DAY*365) |
| 51 | #define TIME_MONTHcxx . . $ `hashcash.h:51:9`! (TIME_YEAR/12) |
| 52 | #define TIME_AEONcxx . . $ `hashcash.h:52:9`! (TIME_YEAR*1000000000.0) |
| 53 | #define TIME_MILLI_SECONDcxx . . $ `hashcash.h:53:9`! (1.0/1000) |
| 54 | #define TIME_MICRO_SECONDcxx . . $ `hashcash.h:54:9`! (TIME_MILLI_SECOND/1000) |
| 55 | #define TIME_NANO_SECONDcxx . . $ `hashcash.h:55:9`! (TIME_MICRO_SECOND/1000) |
| 56 | |
| 57 | #define HASHCASH_OKcxx . . $ `hashcash.h:57:9`! 1 |
| 58 | #define HASHCASH_FAILcxx . . $ `hashcash.h:58:9`! 0 |
| 59 | #define HASHCASH_VALID_FOREVERcxx . . $ `hashcash.h:59:9`! 0 |
| 60 | #define HASHCASH_INVALID_TOK_LENcxx . . $ `hashcash.h:60:9`! -1 |
| 61 | #define HASHCASH_RNG_FAILEDcxx . . $ `hashcash.h:61:9`! -2 |
| 62 | #define HASHCASH_INVALID_TIMEcxx . . $ `hashcash.h:62:9`! -3 |
| 63 | #define HASHCASH_TOO_MANY_TRIEScxx . . $ `hashcash.h:63:9`! -4 |
| 64 | #define HASHCASH_EXPIRED_ON_CREATIONcxx . . $ `hashcash.h:64:9`! -5 |
| 65 | #define HASHCASH_INVALID_VALIDITY_PERIODcxx . . $ `hashcash.h:65:9`! -6 |
| 66 | #define HASHCASH_INTERNAL_ERRORcxx . . $ `hashcash.h:66:9`! -7 |
| 67 | #define HASHCASH_INVALID_TIME_WIDTHcxx . . $ `hashcash.h:67:9`! -8 |
| 68 | #define HASHCASH_VALID_IN_FUTUREcxx . . $ `hashcash.h:68:9`! -9 |
| 69 | #define HASHCASH_EXPIREDcxx . . $ `hashcash.h:69:9`! -10 |
| 70 | #define HASHCASH_INVALIDcxx . . $ `hashcash.h:70:9`! -11 |
| 71 | #define HASHCASH_WRONG_RESOURCEcxx . . $ `hashcash.h:71:9`! -12 |
| 72 | #define HASHCASH_INSUFFICIENT_BITScxx . . $ `hashcash.h:72:9`! -13 |
| 73 | #define HASHCASH_UNSUPPORTED_VERSIONcxx . . $ `hashcash.h:73:9`! -14 |
| 74 | #define HASHCASH_SPENTcxx . . $ `hashcash.h:74:9`! -15 |
| 75 | #define HASHCASH_NO_TOKENcxx . . $ `hashcash.h:75:9`! -16 |
| 76 | #define HASHCASH_REGEXP_ERRORcxx . . $ `hashcash.h:76:9`! -17 |
| 77 | #define HASHCASH_OUT_OF_MEMORYcxx . . $ `hashcash.h:77:9`! -18 |
| 78 | #define HASHCASH_USER_ABORTcxx . . $ `hashcash.h:78:9`! -19 |
| 79 | |
| 80 | /* return hashcash version number major.minor eg "1.13" */ |
| 81 | |
| 82 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 83 | const char* hashcash_versioncxx . . $ hashcash_version(33a1f659e94feb42).( void ); |
| 84 | |
| 85 | /* function hashcash_mint calling info: |
| 86 | * |
| 87 | * returns HASHCASH_OK on success or one of the error codes above on |
| 88 | * failure (values < 0) |
| 89 | * |
| 90 | * arguments are: |
| 91 | * |
| 92 | * now_time -- should be time in UTC, use time(2) |
| 93 | * |
| 94 | * time_width -- how many chars to abbreviate the time to |
| 95 | * default 0 is 6 chars YYMMDD |
| 96 | * |
| 97 | * resource -- resource name, unique descriptor for resource you're |
| 98 | * trying to protect |
| 99 | * |
| 100 | * bits -- bits of partial preimage the resource demands |
| 101 | * |
| 102 | * anon_period -- add (or subtract if negative) a random period |
| 103 | * in seconds between this value and 0 |
| 104 | * to get same as mixmaster does set this to -3days |
| 105 | * (default 0) |
| 106 | * |
| 107 | * stamp -- the stamp output |
| 108 | * |
| 109 | * anon_random -- default NULL, if set to pointer to long |
| 110 | * returns actual random offset added to time |
| 111 | * |
| 112 | * tries_taken -- default NULL, if set to pointer to double |
| 113 | * returns number of stamps tested before finding |
| 114 | * returned stamp. |
| 115 | * |
| 116 | * ext -- extension field |
| 117 | * |
| 118 | * compress -- compress the stamp 0 = fast (no compression), 1 = medium |
| 119 | * (some compression), 2 = very compressed (but slow) |
| 120 | * |
| 121 | * small -- optimize for stamp size (true) or stamp generation speed |
| 122 | * (false) |
| 123 | * |
| 124 | * cb -- user defined callback function which is called every |
| 125 | * 1/10th second; if it returns false hashcash_mint |
| 126 | * will return HASHCASH_USER_ABORT; if you do not need |
| 127 | * the callback function, pass NULL |
| 128 | * |
| 129 | * user_arg -- user argument passed to the callback function, if you |
| 130 | * do not need the callback function or if you do not need |
| 131 | * the arguement pass NULL. |
| 132 | * |
| 133 | */ |
| 134 | |
| 135 | /* note: it is the callers responsibility to call hashcash_free on |
| 136 | * stamp when finished */ |
| 137 | |
| 138 | typedef int (*hashcash_callbackcxx . . $ hashcash_callback#)(int percent, int largest, int target, |
| 139 | double count, double expected, |
| 140 | void* user); |
| 141 | |
| 142 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 143 | int hashcash_mintcxx . . $ hashcash_mint(79bf5527ce570278).( time_tcxx . . $ time_t# now_time, int time_width, const char* resource, |
| 144 | unsigned bits, long anon_period, char** stamp, |
| 145 | long* anon_random, double* tries_taken, char* ext, |
| 146 | int compress, hashcash_callbackcxx . . $ hashcash_callback# cb, void* user_arg ); |
| 147 | |
| 148 | /* simpler API for minting */ |
| 149 | |
| 150 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 151 | char* hashcash_simple_mintcxx . . $ hashcash_simple_mint(b1bd543d6fa08ca2).( const char* resource, unsigned bits, |
| 152 | long anon_period, char* ext, int compress ); |
| 153 | |
| 154 | /* convert a stamp into a X-Hashcash: header wrapping lines at line_len |
| 155 | * chars. If line_len = 0, does not wrap, just prepends header. |
| 156 | * |
| 157 | * arguments are: |
| 158 | * |
| 159 | * stamp -- the stamp to put in the header |
| 160 | * line_len -- the number of chars you want to wrap after (or no wrapping |
| 161 | * if 0 is given) |
| 162 | * lf -- what kind of line feed you want "\r\n" (DOS), "\n" (UNIX), |
| 163 | * "\r" (MAC). Actually I think RFC822 requires DOS lf. If |
| 164 | * you pass NULL it uses the standard RFC822 lf ("\r\n"). |
| 165 | * |
| 166 | */ |
| 167 | |
| 168 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 169 | char* hashcash_make_headercxx . . $ hashcash_make_header(cfa4c5328a7dc09e).( const char* stamp, int line_len, |
| 170 | const char* header, char cont, |
| 171 | const char* lf ); |
| 172 | |
| 173 | /* return time field width necessary to express time in sufficient |
| 174 | * resolution to avoid premature expiry of stamp minted |
| 175 | * for a resource which advertises the given validity period |
| 176 | * |
| 177 | */ |
| 178 | |
| 179 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 180 | int hashcash_validity_to_widthcxx . . $ hashcash_validity_to_width(a9e4e82268fd7bfa).( long validity_period ); |
| 181 | |
| 182 | /* count bits of preimage in stamp */ |
| 183 | |
| 184 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 185 | unsigned hashcash_countcxx . . $ hashcash_count(c61c63047ce273de).( const char* stamp ); |
| 186 | |
| 187 | /* parse stamp into time field and resouce name |
| 188 | * max lengths exclude space for trailing \0 |
| 189 | * |
| 190 | * arguments are: |
| 191 | * |
| 192 | * stamp -- stamp to parse |
| 193 | * vers -- stamp version (currently 0 or 1) |
| 194 | * bits -- (used in vers 1 only; with ver 0 returns -1) |
| 195 | * utct -- utc time field |
| 196 | * utct_max -- max width of utct field (definition is 13 max) |
| 197 | * stamp_resource -- resource name from stamp |
| 198 | * res_max -- max width of resource name |
| 199 | * ext -- extension field |
| 200 | * ext_max -- max width of extension field |
| 201 | */ |
| 202 | |
| 203 | /* note: if don't care about ext field pass NULL argument */ |
| 204 | |
| 205 | /* note: if you pass non-NULL in ext, but *ext = NULL, hashcash_parse |
| 206 | * will allocate the space, in this case it is the callers |
| 207 | * responsibility to call hashcash_free on ext when finished */ |
| 208 | |
| 209 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 210 | int hashcash_parsecxx . . $ hashcash_parse(9cd80e4a04369a5f).( const char* stamp, int* vers, int* bits, char* utct, |
| 211 | int utct_max, char* stamp_resource, int res_max, |
| 212 | char** ext, int ext_max ); |
| 213 | |
| 214 | /* return how many seconds the stamp remains valid for |
| 215 | * return value HASHCASH_VALID_FOREVER (value 0) means forever |
| 216 | * return value < 0 is error code |
| 217 | * codes are: HASHCASH_VALID_IN_FUTURE |
| 218 | * HASHCASH_EXPIRED |
| 219 | * |
| 220 | * arguments are: |
| 221 | * |
| 222 | * stamp_time -- time field from stamp |
| 223 | * validity_period -- how long stamps are valid for |
| 224 | * grace_period -- how much clock error to tolerate |
| 225 | * now_time -- current time (UTC) |
| 226 | */ |
| 227 | |
| 228 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 229 | long hashcash_valid_forcxx . . $ hashcash_valid_for(2baf7a67a5531251).( time_tcxx . . $ time_t# stamp_time, long validity_period, |
| 230 | long grace_period, time_tcxx . . $ time_t# now_time ); |
| 231 | |
| 232 | /* simple function calling hashcash_parse, hashcash_count for convenience |
| 233 | * |
| 234 | * on error returns -ve values: |
| 235 | * |
| 236 | * HASHCASH_INVALID |
| 237 | * HASHCASH_UNSUPPORTED_VERSION, |
| 238 | * HASHCASH_WRONG_RESOURCE |
| 239 | * HASHCASH_REGEXP_ERROR |
| 240 | * HASHCASH_INSUFFICIENT_BITS |
| 241 | * |
| 242 | * on success returns time: left |
| 243 | * |
| 244 | * HASHCASH_FOREVER (actually 0) |
| 245 | * +ve number being remaining validity time in seconds |
| 246 | * |
| 247 | * arguments are: |
| 248 | * |
| 249 | * stamp -- stamp to check |
| 250 | * case_flag -- if set true case-sensitive (false case insensitive) |
| 251 | * resource -- resource expecting (maybe wildcard, regexp...) |
| 252 | * compile -- cache for compiled regexp (caller free afterwards) |
| 253 | * re_err -- if get HASHCASH_REGEXP_ERROR this contains error |
| 254 | * type -- type of string (TYPE_STR, TYPE_WILD, TYPE_REGEXP) |
| 255 | * now_time -- current time (UTC) |
| 256 | * validity_period -- how long stamps are valid for |
| 257 | * grace_period -- how much clock error to tolerate |
| 258 | * required_bits -- how many bits the stamp must have |
| 259 | * stamp_time -- returns: parsed and decoded time field from stamp |
| 260 | */ |
| 261 | |
| 262 | /* note: compile argument can be NULL in which case regexps |
| 263 | * compilations are not cached (and compile arg does not have to be |
| 264 | * hashcash_free'd) |
| 265 | */ |
| 266 | |
| 267 | /* note: it is the callers responsibility to call hashcash_free on |
| 268 | * compile when finished if regexps are used */ |
| 269 | |
| 270 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 271 | int hashcash_checkcxx . . $ hashcash_check(cf6099f7d8aec866).( const char* stamp, int case_flag, const char* resource, |
| 272 | void **compile, char** re_err, int type, time_tcxx . . $ time_t# now_time, |
| 273 | long validity_period, long grace_period, |
| 274 | int required_bits, time_tcxx . . $ time_t#* stamp_time ); |
| 275 | |
| 276 | /* return how many tries per second the machine can do */ |
| 277 | |
| 278 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 279 | unsigned long hashcash_per_seccxx . . $ hashcash_per_sec(f708dce9a8856c70).( void ); |
| 280 | |
| 281 | /* estimate how many seconds it would take to mint a stamp of given size */ |
| 282 | |
| 283 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 284 | double hashcash_estimate_timecxx . . $ hashcash_estimate_time(4badf5dbac1308a7).( int b ); |
| 285 | |
| 286 | /* expected number of tries to mint stamp of size b */ |
| 287 | /* note: equivalent to pow( 2, b ) but without using libm */ |
| 288 | |
| 289 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 290 | double hashcash_expected_triescxx . . $ hashcash_expected_tries(4badf5dbac1308a7).( int b ); |
| 291 | |
| 292 | /* hashcash_resource_match |
| 293 | * |
| 294 | * For comparing received stamps to addresses the user is willing to |
| 295 | * receive email as. |
| 296 | * |
| 297 | * returns 0 and *err == NULL if no match |
| 298 | * returns 0 and *err != NULL if regexp error, regexp explanatory error msg |
| 299 | * held in *err |
| 300 | * returns 1 on match |
| 301 | * |
| 302 | * arguments are: |
| 303 | * |
| 304 | * type -- type of comparison: TYPE_STR (simple string), |
| 305 | * TYPE_WILD (wildcard '*'), TYPE_REGEXP |
| 306 | * (full regular expression) |
| 307 | * |
| 308 | * stamp_res -- resource from the stamp being compared |
| 309 | * |
| 310 | * res -- resource we're comparing against |
| 311 | * |
| 312 | * compile -- cache for regexp compilation result |
| 313 | * (hashcash_free it when you've finished) |
| 314 | * |
| 315 | * err -- NULL on success, regexp error string if regexp fails |
| 316 | */ |
| 317 | |
| 318 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 319 | int hashcash_resource_matchcxx . . $ hashcash_resource_match(4f0b9df207c9eefb).( int type, const char* stamp_res, const char* res, |
| 320 | void** compile, char** err ); |
| 321 | |
| 322 | /* free memory which may beallocated by: |
| 323 | * |
| 324 | * hashcash_check |
| 325 | * hashcash_mint |
| 326 | * hashcash_parse |
| 327 | * hashcash_simple_mint |
| 328 | * hashcash_resource_match |
| 329 | */ |
| 330 | |
| 331 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 332 | void hashcash_freecxx . . $ hashcash_free(5d22bdacc48458e8).( void* ptr); |
| 333 | |
| 334 | |
| 335 | /* bit of redundancy to avoid needing to distribute multiple header |
| 336 | * files with the DLL (this is an excerpt from utct.h) |
| 337 | */ |
| 338 | |
| 339 | #define MAX_UTCcxx . . $ `hashcash.h:339:9`! 13 |
| 340 | |
| 341 | /* convert utct string into a time_t |
| 342 | * |
| 343 | * returns (time_t)-1 on error |
| 344 | * |
| 345 | * arguments are: |
| 346 | * |
| 347 | * utct -- utct string to convert |
| 348 | * |
| 349 | * utc -- if true use UTC, else express in local time zone |
| 350 | */ |
| 351 | |
| 352 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 353 | time_tcxx . . $ time_t# hashcash_from_utctimestrcxx . . $ hashcash_from_utctimestr(71083d4ee4d9cb41).( const char utct[MAX_UTCcxx . . $ `hashcash.h:339:9`!+1], int utc ); |
| 354 | |
| 355 | /* convert time_t into utct string (note utct is always in utc time) |
| 356 | * |
| 357 | * returns 1 on success, 0 on failure |
| 358 | * |
| 359 | * arguments are: |
| 360 | * |
| 361 | * utct -- return utct converted string |
| 362 | * |
| 363 | * len -- how many digits of utc to use; valid utc widths are: |
| 364 | * 6,10,12 (though the code can generate 2,4,6,8,10,12 |
| 365 | */ |
| 366 | |
| 367 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 368 | int hashcash_to_utctimestrcxx . . $ hashcash_to_utctimestr(9276ee04ba38ce81).( char utct[MAX_UTCcxx . . $ `hashcash.h:339:9`!+1], int len, time_tcxx . . $ time_t# t ); |
| 369 | |
| 370 | /* returns max rate of hashcash / sec using a slower but more accurate |
| 371 | * benchmarking method |
| 372 | * |
| 373 | * arguments are: |
| 374 | * |
| 375 | * verbose -- verbosity level 0 = no output, 1 = some, 2 = more, 3 = most |
| 376 | * core -- core number CORE_ALL (= -1) = all cores, or specific |
| 377 | */ |
| 378 | |
| 379 | /* returns speed of hashcash */ |
| 380 | |
| 381 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 382 | unsigned long hashcash_benchtestcxx . . $ hashcash_benchtest(beccc266862daf71).( int verbose, int core ); |
| 383 | |
| 384 | /* returns current core */ |
| 385 | |
| 386 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 387 | int hashcash_corecxx . . $ hashcash_core(b126dc7c1de90089).(void); |
| 388 | |
| 389 | /* use specified core */ |
| 390 | |
| 391 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 392 | int hashcash_use_corecxx . . $ hashcash_use_core(7864480464b09eea).(int); |
| 393 | |
| 394 | /* give name of specified core */ |
| 395 | |
| 396 | HCEXPORTcxx . . $ `hashcash.h:18:17`! |
| 397 | const char* hashcash_core_namecxx . . $ hashcash_core_name(f7ae7e56280f18ae).(int); |
| 398 | |
| 399 | |
| 400 | #if defined( __cplusplus ) |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | #endif |