Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /branches/mty-makai/mty.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 231 - (hide annotations) (download) (as text)
Mon Sep 14 06:29:50 2009 UTC (14 years, 6 months ago) by notanpe
File MIME type: text/x-csrc
File size: 48691 byte(s)
特殊検索にエロい人型を追加
1 chapuni 1 /***********************************************************************
2     *
3 chapuni 26 * file: mty.c
4 chapuni 1 *
5 chapuni 2 * ‚Ü‚ A‘Ň‚Ä‰ŽB
6 chapuni 1 *
7     * $Id$
8     *
9     */
10    
11     #include <assert.h>
12     #include <ctype.h>
13 chapuni 24 #include <errno.h>
14 chapuni 9 #include <limits.h>
15 chapuni 24 #include <stdarg.h>
16 chapuni 20 #include <stddef.h>
17 chapuni 1 #include <stdio.h>
18     #include <stdlib.h>
19     #include <string.h>
20     #include <time.h>
21 notanpe 192 #include <sys/timeb.h>
22 notanpe 148 #include <sys/types.h>
23 chapuni 10
24 notanpe 197 #include <unistd.h>
25     #include <libgen.h>
26 notanpe 203 #include <limits.h>
27 notanpe 213 #ifdef REON
28     #ifdef ONI
29     #include <onigposix.h>
30     #else /* ONI */
31     /* #include <rxposix.h> */
32     #include <regex.h>
33     #endif /* ONI */
34     #endif /* REON */
35 notanpe 197
36 chapuni 10 #if defined(WIN32)
37    
38     #include <windows.h>
39 notanpe 148 #include <process.h>
40 chapuni 10
41     #elif defined(__GNUC__)
42    
43     #include <sys/time.h>
44    
45     #endif
46    
47 chapuni 26 #include "config.h"
48 chapuni 39 #include "cp932.h"
49 chapuni 1 #include "crypt64.h"
50 chapuni 41 #include "desconst.h"
51 chapuni 46 #include "expr_parse.h"
52     #include "scoreboard.h"
53     #include "synth.h"
54 chapuni 74 #include "tr64.h"
55 chapuni 26 #include "translate.h"
56 notanpe 148 #include "util.h"
57 chapuni 99 #include "wdict.h"
58 chapuni 1
59 notanpe 198 #define TRIP_LEN 10
60    
61 notanpe 227 #define MAKAI_TRUE 1
62     #define MAKAI_FALSE 0
63    
64 notanpe 197 #define MIN_THREAD 1
65     #define MAX_THREAD 32
66     int nThread = 0;
67 notanpe 199 uint64_t pmask = 0;
68 notanpe 197
69     #define MIN_UME 3
70     #define MAX_UME 6
71     unsigned char umeStr[10];
72     int umeLen;
73 notanpe 198
74 notanpe 204 FILE *nfp; /* ‘S” */
75     FILE *tfp; /* ŞˆČŠO‚Ě“ÁŽęŒŸő */
76 notanpe 218 #ifdef CHIN
77     FILE *cfp; /* ‚ż */
78     #endif /* CHIN */
79 notanpe 213 #ifdef REON
80     FILE *rfp; /* ł‹K•\Œť */
81     #endif /* REON */
82 notanpe 203
83 notanpe 214 /*
84     Cygwin 32bit
85     time_t = long = int
86     INT_MIN -2147483648
87     INT_MAX 2147483647
88     (2009 - 1970) * 365 * 24 * 60 * 60 = 1229904000
89     2147483647 - 1229904000 = 917579647
90     10y * 365 * 24 * 60 * 60 = 315360000
91 notanpe 217
92     perl -e 'use POSIX;printf(strftime("%Y/%m/%d %T\n",localtime (1234567890)));'
93 notanpe 214 */
94     #define MIN_SOFF -1229904000
95     #define MAX_SOFF 315360000
96 notanpe 203 unsigned int seed;
97 notanpe 214 int seedOffset;
98 notanpe 203 int verbose;
99 notanpe 225
100 notanpe 229 #define MAX_GEAR 10
101     #define MIN_GEAR 1
102     #define DEF_GEAR 10
103     int gear;
104    
105 notanpe 225 unsigned char saltChar[2];
106 notanpe 197
107 notanpe 227 #ifdef ALLKEY
108     unsigned char fixedSaltChar[2];
109     #endif /* ALLKEY */
110    
111 notanpe 192 /* CRYPT64 ‹LqŽq */
112     static
113     struct CRYPT64_DESC const *const crypt64_descs[] =
114     {
115     &crypt64_desc,
116     };
117    
118 chapuni 1 /* ŒŽƒNƒ‰ƒX */
119 chapuni 41 static
120 chapuni 1 struct
121     {
122 notanpe 148 unsigned short map[256];
123 notanpe 227 } kcls[8 + 8];
124 chapuni 1
125 chapuni 26 /* Šg’ŁŒŽƒNƒ‰ƒX */
126 chapuni 1 #define KCLS_DT0 64
127     #define KCLS_DT1 128
128     #define KCLS_K2 256
129    
130 notanpe 198 int special = 0;
131 notanpe 207 #define ST_ALLN 1 /* ‘S” */
132     #define ST_NIKO 1<<0x1 /* “ń\ */
133     #define ST_BUOO 1<<0x8 /* ‚Ô‚¨ */
134     #define ST_DOSU 1<<0x9 /* “{” */
135 notanpe 231 #define ST_EROI 1<<0xc /* ƒGƒ‚˘l Uni ‚ł͊g”ň‚Ěƒtƒ‰ƒO */
136 notanpe 207 #define ST_HREN 1<<0xd /* ”ň˜A */
137 notanpe 231 #define ST_CHIN 1<<0xb /* ^Chinko Uni ‚ł͊´’Q—p‚Ěƒtƒ‰ƒO */
138 notanpe 224 #define ST_YAKU 1<<0xe /* ”މ_ */
139 notanpe 198
140 notanpe 213 #ifdef REON
141     #define REGEXPLEN 1024
142     char regExpStr[REGEXPLEN];
143     regex_t regExp;
144     #endif /* REON */
145    
146 notanpe 198 void
147     comment( str )
148     char *str;
149     {
150     if ( strlen( str ) >= 4 ) {
151     if ( str[1] == '[' && str[3] == ']' ) {
152     switch ( str[2] ) {
153 notanpe 203 case '0': special |= ST_ALLN; break;
154     case '1': special |= ST_NIKO; break;
155     case '8': special |= ST_BUOO; break;
156 notanpe 204 case '9': special |= (ST_DOSU | ST_ALLN); break;
157 notanpe 207 case 'd': special |= ST_HREN; break;
158 notanpe 224 case 'e': special |= ST_YAKU; break;
159 notanpe 231 case 'Z': special |= ST_EROI; break;
160 notanpe 214 case 'S':
161     seedOffset = atoi( str + 4 );
162     if ( seedOffset < MIN_SOFF || seedOffset > MAX_SOFF ) {
163     fprintf( stderr, "—”‚ĚŽí‚ĚƒIƒtƒZƒbƒg‚́A%d ˆČă %d ˆČ‰ş‚ĹŽw’肾‚ĂˁB\n", MIN_SOFF, MAX_SOFF );
164     fprintf( stderr, "%d ‚͔͈͊O‚Ȃ̂ŖłŽ‹‚ľ‚Ü‚ˇB\n", seedOffset );
165     seedOffset = 0;
166     }
167     break;
168 notanpe 213 case 'R':
169     #ifdef REON
170     if ( strlen( str ) >= REGEXPLEN ) {
171     fprintf( stderr, "ł‹K•\Œť (%s) ‚Ş’ˇ‚ˇ‚Ź", str );
172     exit( 1 );
173     }
174     strcpy( regExpStr, str + 4 );
175     if ( regcomp( &regExp, regExpStr, REG_NOSUB|REG_EXTENDED ) != 0 ) {
176     fprintf( stderr, "ł‹K•\Œť (%s) ‚Ş‚¨‚Š‚ľ‚˘H\n", str );
177     exit( 1 );
178     }
179     #else /* REON */
180     fprintf( stderr, "‚ą‚ĚƒoƒCƒiƒŠ‚́Ał‹K•\Œť‚đƒTƒ|[ƒg‚ľ‚Ä‚˘‚Ü‚š‚ńB\n" );
181     fprintf( stderr, "%s ‚Í–łŽ‹‚ľ‚Ü‚ˇB\n", str );
182     #endif /* REON */
183     break;
184 notanpe 198 }
185     }
186     }
187     }
188    
189 notanpe 226 #ifndef ALLKEY
190 chapuni 1 /* Žw’肳‚ę‚˝ƒNƒ‰ƒX‚Ć“ü‚Á‚Ä‚˘‚éƒL[‚Š‚çAclassify ‚đs‚¤ */
191     void
192 notanpe 148 key_make_map(uint8_t *key, int n)
193 chapuni 1 {
194     int i, j;
195     unsigned c = kcls[n].map[key[n]];
196    
197     /* ĹŒă‚Ě•”•Ş‚Í class map ‚đśŹ‚ˇ‚é•K—vƒiƒV */
198 notanpe 224 /* MAKAI memo
199     6byte –„‚ß‚˝Žž‚Ě‹““Ž
200     ‰ş‚Ě•ű‚Ě if (n == 6) ‚Ć–ľ‚
201     */
202 notanpe 227 if ( n >= 6 ) return;
203 chapuni 1
204 notanpe 227 for ( i = 0; i < 256; i++ ) {
205     unsigned bm = 0;
206     if ( c & KCLS_K1 ) {
207     if ( cp932[256 * key[n] + i] & KCLS_K1 ) {
208     bm |= KCLS_K2 | (cp932[256 * key[n] + i] & KCLS_DT1);
209     }
210     if ( cp932[256 * (key[n] ^ 0x80) + i] & KCLS_K1 ) {
211     bm |= KCLS_K2 | (cp932[256 * (key[n] ^ 0x80) + i] & KCLS_DT1);
212     }
213     }
214     if ( c & (KCLS_AN | KCLS_KA | KCLS_K2) ) {
215     for ( j = 0; j < 256; j++ ) {
216     bm |= cp932[256 * i + j] & (KCLS_AN | KCLS_KA | KCLS_K1 | KCLS_DT0);
217     }
218     }
219     kcls[n + 1].map[i] = bm;
220     if ( i >= 128 && !(n == 0 || n == 1) ) {
221     kcls[n + 1].map[i - 128] |= kcls[n + 1].map[i];
222     }
223     }
224    
225     if ( n < 6 ) kcls[n + 1].map[0x00] = kcls[n + 1].map[0x80] = 0;
226 chapuni 1 #if 0
227 notanpe 227 if ( n == 6 ) kcls[7].map[0x00] |= KCLS_AN;
228     #endif /* 0 */
229 chapuni 1 }
230 notanpe 226 #endif /* not ALLKEY */
231 chapuni 1
232     /* ƒ}ƒbƒv‚Š‚ç•śŽš‚đE‚Á‚ăZƒbƒg */
233     unsigned
234     key_set(int n, unsigned ch)
235     {
236     int cnt = 0, i;
237    
238 notanpe 223 #ifdef NOMORE
239     ch %= 256;
240     #endif /* NOMORE */
241    
242 notanpe 227 for ( i = 0; i < 256; i++ ) {
243     if ( kcls[n].map[i] ) {
244     if ( ch-- == 0 ) return i;
245     cnt++;
246     }
247     if ( n != 1 && n != 2 && i >= 127 ) break;
248     }
249 chapuni 1 /* ŒŠ‚Â‚Š‚ç‚ȂЂÁ‚˝‚Ě‚Ĺ‚ŕ‚˘‚Á‚Ř‚ń */
250 notanpe 227 assert( cnt > 0 );
251 chapuni 1 ch %= cnt;
252 notanpe 227 for ( i = 0; i < 256; i++ ) {
253     if ( kcls[n].map[i] ) {
254     if ( ch-- == 0 ) return i;
255     }
256     }
257     assert( !"not matched" );
258 chapuni 1 return 0;
259     }
260    
261     /* bitwise key ‚đƒZƒbƒg */
262     static
263     void
264     key_set64(struct KEY *key64,
265     int n,
266     unsigned k,
267     unsigned vk,
268     unsigned sk)
269     {
270 chapuni 6 int i, j;
271 chapuni 1 if (!((vk | sk) & 0x7F))
272     return;
273    
274     for (i = 0; i < 7; i++)
275     {
276 chapuni 6 if (n == 7 && i < N_STRIDE) continue;
277 chapuni 1 if (sk & (1 << i))
278     {
279     /* ƒZƒbƒg */
280     int o = tr_pc1[n][6 - i] - 1;
281     if (o < 28)
282     {
283     assert(o >= 0);
284 chapuni 6 for (j = 0; j < N_ALU; j++)
285     key64->k[0][0][o].a[j]
286     = key64->k[0][1][o].a[j]
287     = -!!(k & (1 << i));
288 chapuni 1 }
289     else
290     {
291     assert(o >= 28);
292     assert(o < 56);
293 chapuni 6 for (j = 0; j < N_ALU; j++)
294     key64->k[1][0][o - 28].a[j]
295     = key64->k[1][1][o - 28].a[j]
296     = -!!(k & (1 << i));
297 chapuni 1 }
298     }
299     else if (vk & (1 << i))
300     {
301     /* ”˝“] */
302     int o = tr_pc1[n][6 - i] - 1;
303     if (o < 28)
304     {
305     assert(o >= 0);
306 chapuni 10 for (j = 0; j < N_ALU; j++)
307     key64->k[0][0][o].a[j]
308     = key64->k[0][1][o].a[j]
309     = ~key64->k[0][0][o].a[j];
310 chapuni 1 }
311     else
312     {
313     assert(o >= 28);
314     assert(o < 56);
315 chapuni 10 for (j = 0; j < N_ALU; j++)
316     key64->k[1][0][o - 28].a[j]
317     = key64->k[1][1][o - 28].a[j]
318     = ~key64->k[1][0][o - 28].a[j];
319 chapuni 1 }
320     }
321     }
322     }
323    
324     /* Žw’肳‚ę‚˝ƒNƒ‰ƒX‚ĚŠJŽn’l‚ÉƒŠƒZƒbƒg
325     ’ź‘O‚Ě•śŽš‚ĚƒNƒ‰ƒX‚É”›‚ç‚ę‚é */
326     int
327 notanpe 148 key_reset(uint8_t *key, int n)
328 chapuni 1 {
329 notanpe 197 static char firstCall = 1;
330    
331     if ( firstCall ) {
332     firstCall = 0;
333     } else {
334     if ( umeStr[0] != '\0' && n == 0 ) {
335     exit( 0 );
336     }
337     }
338    
339 chapuni 1 if (n >= 8)
340     return 1;
341     if (n == 7)
342     {
343     key[7] = 0;
344     return 1;
345     }
346    
347 notanpe 224 if ( n >= umeLen ) {
348     #ifdef ALLKEY
349     for ( ; n < 7; n++ ) {
350     key[n] = 1;
351     }
352     key[7] = 0;
353     return 1;
354     #else /* ALLKEY */
355     key[n] = key_set(n, 0);
356     #endif /* ALLKEY */
357     } else {
358 notanpe 197 if ( umeStr[0] == '\0' ) {
359 notanpe 223 #ifdef ALLKEY
360 notanpe 226 key[n] = rand() % 0x7f + 1;
361 notanpe 223 #else /* ALLKEY */
362 notanpe 197 key[n] = key_set(n, rand());
363 notanpe 223 #endif /* ALLKEY */
364 notanpe 197 } else {
365     key[n] = umeStr[n];
366     }
367     }
368 chapuni 1
369     #if DEBUG>=3
370     printf("key[%d]=%02X ncls=%04X\n", n, key[n], kcls[n].map[key[n]]);
371     #endif
372    
373 notanpe 223 #ifndef ALLKEY
374 chapuni 1 /* ƒZƒbƒg‚ł‚ę‚˝•śŽš‚đŒł‚ÉAŽŸƒLƒƒƒ‰‚Ě•śŽšƒNƒ‰ƒX‚đŒˆ‚ß‚é */
375 notanpe 148 key_make_map(key, n);
376 notanpe 223 #endif /* not ALLKEY */
377 chapuni 1
378 notanpe 148 return key_reset(key, n + 1);
379 chapuni 1 }
380    
381     /* Žw’肳‚ę‚˝ŒŽ‹óŠÔ‚Ě’†‚ŁAƒL[‚đ‚ЂƂi‚ß‚é
382     ˆŔ‘S‚ɃCƒ“ƒNƒŠƒƒ“ƒg‚Ĺ‚Ť‚˝ę‡ true ‚đ•Ô‚ˇ */
383     static
384     int
385 notanpe 148 key_inc(uint8_t *key, int n)
386 chapuni 1 {
387 notanpe 224 #if 0
388 notanpe 226 if ( n != 7 ) {
389 notanpe 224 printf( "%d : %02x %02x %02x %02x %02x %02x %02x %02x\n",
390     n, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7] );
391 notanpe 226 }
392 notanpe 224 #endif /* 0 */
393 notanpe 148
394 notanpe 224 if ( n >= 8 ) {
395     return 0;
396     } else if ( n == 7 ) {
397     /* MAKAI memo
398     2^N_STRIDE ‚Ş‚˘‚Á‚Ř‚ń‚ÉŒvŽZ‚ˇ‚éŒÂ”
399     MMX 6 : 64 ŒÂ
400     SSE2 7 : 128 ŒÂ
401     ‚ą‚Ě‚˝‚ßĹŒă‚ĚƒoƒCƒg‚̐i‚ß•ű‚Ş•Ď‚í‚é
402     */
403     uint8_t o_k = (key[7] + (1 << N_STRIDE)) & 0x7F;
404     if ( !o_k ) {
405     return 0; /* ƒCƒ“ƒNƒŠƒƒ“ƒg‚Ĺ‚Ť‚ȂЂÁ‚˝‚Ć‚Ť‚ÍŽŸ‚֐i‚ß‚¸‘҂ */
406     } else {
407     key[7] = o_k;
408     return 1;
409     }
410     } else if ( key_inc( key, n+1 ) ) {
411     return 1;
412     }
413 chapuni 1
414     /* Salt ‚̓Cƒ“ƒNƒŠƒƒ“ƒg‚ľ‚Č‚˘–ń‘Ђɂˇ‚é */
415 notanpe 224 if ( n == 1 || n == 2 ) {
416     return 1;
417     }
418 chapuni 1
419 notanpe 226 #ifdef ALLKEY
420     key[n] = ( key[n] & 0x7F ) + 1;
421     if ( key[n] >= 0x80 ) {
422     key[n] = 0xFF; /* ŽŸ‚ɓ˓ü‚ł‚š‚Č‚˘‚˝‚ß */
423     return 0;
424     }
425     key_reset( key, n + 1 );
426     return 1;
427     #endif /* ALLKEY */
428    
429 chapuni 1 /* ŽŔŰ‚É‘‚₾‚Ă݂é */
430 notanpe 224 assert( n >= 3 );
431     for ( ;; ) {
432 chapuni 1 if (n <= 3
433     && !(key[n] & 0x80)
434     && kcls[n].map[key[n] ^ 0x80] & (KCLS_DT0))
435     {
436     /* ”źŠpƒJƒ^ƒJƒi‚Ě1ƒoƒCƒg–Ú */
437     key[n] ^= 0x80;
438     }
439     else
440     {
441     key[n] = (key[n] & 0x7F) + 1;
442     if (key[n] >= 0x80)
443 notanpe 148 {
444     key[n] = 0xFF; /* ŽŸ‚ɓ˓ü‚ł‚š‚Č‚˘‚˝‚ß */
445     return 0;
446     }
447 chapuni 1 }
448    
449     if (kcls[n].map[key[n]])
450     {
451 notanpe 223 #ifndef ALLKEY
452 notanpe 148 key_make_map(key, n);
453 notanpe 223 #endif /* not ALLKEY */
454 notanpe 148 key_reset(key, n + 1);
455 chapuni 1 return 1;
456     }
457     }
458     }
459    
460     /* ŒŽ‚đŠŽ‘S‚ÉƒŠƒZƒbƒg‚ˇ‚é
461     Salt‚ŕƒZƒbƒg‚ľ’ꂎ */
462     static
463     void
464 notanpe 148 key_init(uint8_t *key)
465 chapuni 1 {
466     int i, j;
467    
468     key[8] = 0;
469    
470 notanpe 226 #ifndef ALLKEY
471 chapuni 1 /* ‰Šúƒ}ƒbƒv‚đ‘g‚Ţ */
472 notanpe 222 for ( i = 0; i < 256; i++ ) {
473     unsigned bm = 0;
474 chapuni 1
475 notanpe 222 for ( j = 0; j < 256; j++ ) {
476     bm |= cp932[256 * i + j];
477     }
478     kcls[0].map[i] = bm & (KCLS_AN | KCLS_KA | KCLS_K1);
479     if ( i >= 128 ) {
480     kcls[0].map[i - 128] |= kcls[0].map[i];
481     }
482     }
483 notanpe 226 #endif /* not ALLKEY */
484 notanpe 222
485 notanpe 148 key_reset(key, 0);
486 chapuni 1 }
487    
488     /***************************************************************
489     *
490 chapuni 122 * ŒĹ’čƒL[‚̐śŹ
491     *
492     * ˆęŒŠ Big Endian ‚É”ń‘Ήž‚̂悤‚ÉŒŠ‚Ś‚é‚ž‚낤‚Ş
493     * Š‚łɎU‚ç‚΂Á‚Ä‚˘‚é kludge ‚É‚ć‚č
494     * ALU_T ‚Ş 64 ƒrƒbƒg‚Ĺ‚ ‚éŒŔ‚čA‚ą‚ę‚Ĺ–â‘č‚Č‚­“Ž‚­B
495     *
496     */
497    
498     static
499     void
500     key_init_sk(struct KEY *key)
501     {
502     int i, j;
503     int o;
504     uint64_t m;
505    
506     for (i = 5, m = 0xFFFFFFFF00000000ULL;
507     i >= 0;
508     m ^= (m >> (1 << --i)))
509     {
510     o = tr_pc1[7][6 - i] - 1;
511 chapuni 123 #if DEBUG>=2
512 chapuni 122 printf("%d:%d->%2d: %08X%08X\n",
513     N_Q, i, o,
514     (unsigned)(m >> 32),
515     (unsigned)m);
516 chapuni 123 #endif
517 chapuni 122 for (j = 0; j < N_Q; j++)
518     if (o < 28)
519     key->k[0][0][o ].q[j] = key->k[0][1][o ].q[j] = m;
520     else
521     key->k[1][0][o - 28].q[j] = key->k[1][1][o - 28].q[j] = m;
522     }
523     #if N_STRIDE==7
524     /* bit 6 ‚Í Little Endian ‚Ć‚ľ‚Ĉľ‚¤ */
525     o = 0;
526     assert(tr_pc1[7][0] - 1 == o);
527     assert(N_Q == 2);
528     key->k[0][0][o].q[0] = key->k[0][1][o].q[0] = 0x0000000000000000ULL;
529     key->k[0][0][o].q[1] = key->k[0][1][o].q[1] = 0xFFFFFFFFFFFFFFFFULL;
530     #endif
531     }
532    
533     /***************************************************************
534     *
535 chapuni 1 * Salt ‚ĚƒZƒbƒg
536     * ƒIƒyƒ‰ƒ“ƒh‚ĚƒIƒtƒZƒbƒg‚đ‘‚ŤŠˇ‚ڂĉń‚Á‚Ä‚é‚̂ŒˆÓ
537     *
538     */
539    
540     void
541 notanpe 148 set_salt(CODE_T *code,
542 notanpe 192 struct CRYPT64_DESC const *desc,
543 notanpe 148 uint8_t const *k)
544 chapuni 1 {
545 notanpe 227 int i, j;
546 chapuni 1
547 notanpe 227 static unsigned char *s2c = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
548    
549     for ( i = 0; i < 2; i++ ) {
550     unsigned s;
551 notanpe 226 #ifdef ALLKEY
552 notanpe 227 if ( fixedSaltChar[0] != '\0' ) {
553     saltChar[i] = fixedSaltChar[i];
554     for ( s = 0; s2c[s] != '\0' && s2c[s] != saltChar[i]; s++ )
555     ;
556     assert( s2c[s] != '\0' );
557     } else {
558     s = rand() % 64;
559     saltChar[i] = s2c[s];
560     }
561 notanpe 226 #else /* ALLKEY */
562 notanpe 227 s = k[1 + i] & 255;
563     if (s > 'z')
564     s = 0;
565     else if (s >= 'a')
566     s = s - 'a' + 2 + 10 + 26;
567     else if (s >= 'A')
568     s = s - 'A' + 2 + 10;
569     else if (s >= '.')
570     s = s - '.';
571     else
572     s = 0;
573 notanpe 225 #ifdef OTAKU
574     {
575 notanpe 227 unsigned s2;
576     do {
577     s2 = rand() % 64;
578     } while ( s == s2 );
579     s = s2;
580 notanpe 225 }
581     #endif /* OTAKU */
582 notanpe 227 saltChar[i] = s2c[s];
583 notanpe 226 #endif /* ALLKEY */
584 notanpe 225
585 notanpe 227 for ( j = 0; j < 6; j++ ) {
586     if ( s & (1 << j) ) {
587     LSALT( desc, code, 0, i, j, 0 ) = sizeof(WS_T) * (((4*i+j+15) & 31) - 16);
588     LSALT( desc, code, 0, i, j, 24 ) = sizeof(WS_T) * (((4*i+j- 1) & 31) - 16);
589     } else {
590     LSALT( desc, code, 0, i, j, 0 ) = sizeof(WS_T) * (((4*i+j- 1) & 31) - 16);
591     LSALT( desc, code, 0, i, j, 24 ) = sizeof(WS_T) * (((4*i+j+15) & 31) - 16);
592     }
593     LSALT( desc, code, 0, i, j, 12 ) = sizeof(WS_T) * (((4*i+j+ 7) & 31) - 16);
594     LSALT( desc, code, 0, i, j, 36 ) = sizeof(WS_T) * (((4*i+j+23) & 31) - 16);
595     }
596     }
597 chapuni 1 }
598    
599 notanpe 192 #define USEC_SEC 1000 /* 1•b */
600    
601 chapuni 1 static
602 notanpe 192 uint64_t
603 notanpe 148 usec(void)
604 notanpe 119 {
605 notanpe 120 uint32_t sec, msec;
606    
607 notanpe 119 #if !defined(WIN32)
608     struct timeval tv;
609     gettimeofday(&tv, NULL);
610 notanpe 120 sec = tv.tv_sec;
611 notanpe 192 msec = tv.tv_usec / (1000000 / USEC_SEC);
612 notanpe 119 #else
613     struct timeb tm;
614     ftime(&tm);
615 notanpe 120 sec = tm.time;
616 notanpe 192 msec = tm.millitm / (1000 / USEC_SEC);
617 notanpe 119 #endif
618 notanpe 120
619 notanpe 192 return (uint64_t)USEC_SEC * sec + msec;
620 notanpe 119 }
621    
622     static
623 chapuni 24 int
624     log_printf(FILE *ofp, char const *fmt, ...)
625     {
626     int r;
627     va_list ap;
628     va_start(ap, fmt);
629 notanpe 204
630     if ( ofp != nfp ) {
631     vfprintf(stdout, fmt, ap);
632     }
633    
634 chapuni 24 r = vfprintf(ofp, fmt, ap);
635     va_end(ap);
636 notanpe 223 #ifdef HITEXIT
637     exit( 0 );
638     #endif /* HITEXIT */
639 chapuni 24 if (r > 0)
640     return r;
641     perror("log_printf");
642     exit(errno);
643     }
644    
645 notanpe 227 static
646     void
647     hit( fp, hash, key, kk, k, kind, dotrans )
648     FILE *fp;
649     char *hash;
650     uint8_t *key;
651     int kk;
652     int k;
653     unsigned char *kind;
654     int dotrans;
655     {
656     struct timeb tb;
657     struct tm *plt;
658     int namaKey;
659     uint8_t buf[32];
660    
661     ftime( &tb );
662     plt = localtime( &tb.time );
663    
664     memcpy( buf, key, 8 );
665     buf[8] = buf[9] = 0;
666     buf[7] = (buf[7] & -(1 << N_STRIDE) & 0x7F) + ALU_BITS * kk + k;
667    
668     namaKey = MAKAI_TRUE;
669     #ifndef NAMA
670     if ( dotrans ) {
671     if ( translate( buf, 0, 1 ) ) {
672     namaKey = MAKAI_FALSE;
673     }
674     }
675     #endif /* NAMA */
676    
677     if ( namaKey ) {
678     log_printf( fp, "Ÿ%s ##%02x%02x%02x%02x%02x%02x%02x%02x%c%c"
679     "\t%04d/%02d/%02d %02d:%02d:%02d.%03d"
680     " %s ś\n",
681     hash,
682     buf[0], buf[1], buf[2], buf[3],
683     buf[4], buf[5], buf[6], buf[7],
684     saltChar[0], saltChar[1],
685     plt->tm_year + 1900, plt->tm_mon + 1, plt->tm_mday,
686     plt->tm_hour, plt->tm_min, plt->tm_sec, tb.millitm,
687     kind );
688     } else {
689     log_printf( fp, "Ÿ%s #%s"
690     "\t%04d/%02d/%02d %02d:%02d:%02d.%03d"
691     "\t(%02X %02X %02X %02X %02X %02X %02X %02X/%02X)"
692     " %s\n",
693     hash,
694     buf,
695     plt->tm_year + 1900, plt->tm_mon + 1, plt->tm_mday,
696     plt->tm_hour, plt->tm_min, plt->tm_sec, tb.millitm,
697     buf[0], buf[1], buf[2], buf[3], buf[4],
698     buf[5], buf[6], buf[7], buf[8],
699     kind );
700     }
701     }
702    
703 chapuni 1 /***************************************************************
704     *
705 notanpe 192 * CPU capabilities ‚đŽć“ž
706     * [XXX] ‚ ‚Ü‚č‚É‚ŕŒĂ‚˘ƒvƒƒZƒbƒT‚Ě‚ą‚Ć‚Íl‚ڂȂ˘B
707 chapuni 1 *
708 notanpe 192 * a[4] = {EAX,EBX,ECX,EDX}
709     *
710 chapuni 1 */
711    
712 notanpe 192 #if defined(__GNUC__)
713    
714     #define cpuid(n,a,b,c,d) \
715     asm("cpuid" \
716     : "=a"(a), "=b"(b), "=c"(c), "=d"(d) \
717     : "a"(n))
718    
719     #elif defined(WIN32)
720    
721     #define cpuid(n,a,b,c,d) \
722     do {int r[4]; __cpuid(r,n); \
723     (a) = r[0]; (b) = r[1]; (c) = r[2]; (d) = r[3];} while (0)
724    
725     #endif
726    
727     static
728     unsigned
729     cpuid_getfflags(void)
730 notanpe 148 {
731 notanpe 192 unsigned a, b, c, d;
732     cpuid(1, a, b, c, d);
733     return d;
734     }
735 chapuni 1
736 notanpe 148 static
737 notanpe 192 int
738     cpuid_issupported(void)
739     {
740     unsigned m = REQUIRED_CAPS;
741     return !((cpuid_getfflags() ^ m) & m);
742     }
743    
744     /***************************************************************
745     *
746     * ƒoƒbƒ`ˆ——pƒpƒPƒbƒg
747     *
748     */
749    
750     static
751 notanpe 148 struct PACKET_CRYPT64 *
752     packet_create(int n, /* ƒpƒPƒbƒg” */
753     int tn, /* ––”ö—v‘f‚ɂĕK—v‚Čƒ[ƒN” */
754     uint8_t const *ini_key)
755     {
756     int i;
757     int siz;
758     void *p;
759 notanpe 192 intptr_t a = 128;
760 notanpe 148 struct PACKET_CRYPT64 *pkts;
761     assert(IS_POWER2(sizeof(struct PACKET_CRYPT64)));
762     assert(n >= 1);
763    
764 notanpe 192 siz = (a - 1
765 notanpe 148 + (n - 1) * sizeof(struct PACKET_CRYPT64)
766     + offsetof(struct PACKET_CRYPT64, param64.hit[tn]));
767     p = calloc(siz, 1);
768     /* ƒoƒ“ƒ_ƒŠ‚ ‚킚 */
769     pkts = (struct PACKET_CRYPT64 *)(((intptr_t)p
770 notanpe 192 + a - 1)
771     & -a);
772 notanpe 148 #if DEBUG>=1
773     fprintf(stderr,
774     "packet(n=%d,tn=%d) %d allocated; %p aligned to %p\n",
775     n, tn,
776     siz, p, pkts);
777     #endif
778    
779     /* “ŕ•”‚̏‰Šú‰ť
780     ƒRƒs[‚ľ‚ĉń‚é‚̂́AŒľ–§‚É‚Í
781     ĹI—v‘f‚ĚƒPƒc‚đ”j‚Á‚Ä‚ľ‚Ü‚¤‚ą‚ƂɂȂé‚Ě‚Ĺ
782     ‚Ç‚¤‚š‘Ź“x‚ŕ—v‹‚ł‚ę‚Č‚˘‚ľƒxƒ^ƒR[ƒh */
783     for (i = 0; i < n; i++)
784     {
785 notanpe 192 int j, k;
786 notanpe 148
787     /* t[16] ‚́A“ŕ•”‰‰ŽZ‚ĹŽg—p‚ˇ‚éAall 1 ‚Ş“ü‚Á‚Ä‚˘‚é */
788     memset(&pkts[i].param64.t[T_INV], -1, sizeof(SLICE));
789    
790     /* ŒĹ’čƒL[‚̐śŹ */
791     key_init_sk(&pkts[i].key64);
792    
793     /* ƒL[ƒXƒPƒWƒ…[ƒ‹‚đ‚ą‚ą‚ɉŸ‚ľž‚߂è‚­
794     ]—ˆ‚Í crypt64.S “ŕ‚ĹŠŽŒ‹‚ˇ‚é‚悤‚Ɉř‚˘‚Ä‚˘‚˝ */
795     for (j = 0; j < 28; j++)
796 notanpe 192 for (k = 0; k < N_ALU; k++)
797     pkts[i].key64.ks[j].a[k] = sizeof(WS_T) * ks_ls[j];
798 notanpe 148
799     /* ”O‚Ě‚˝‚߁AŒŽ‚đ‚ą‚ą‚Ĺ—Ž‚ż’…‚݂è‚­(•s—v?) */
800     for (j = 0; j < 8; j++)
801     key_set64(&pkts[i].key64, j, pkts[i].uk.key[j] = ini_key[j], 0, 0x7F);
802     }
803    
804     return pkts;
805     }
806    
807     /***************************************************************
808     *
809     * thread
810     *
811     */
812    
813     #define NQ_CRYPT 64
814     #define NQ_CMP 32
815    
816     #if defined(__GNUC__)
817    
818     typedef int32_t ATOMWORD_T;
819    
820     #define LOCK_INC(p) \
821     asm volatile ("lock incl %0" \
822     : "=m"(*(p)) \
823     : /*nil*/ \
824     : "memory")
825    
826     #define LOCK_DEC(p) \
827     asm volatile ("lock decl %0" \
828     : "=m"(*(p)) \
829     : /*nil*/ \
830     : "memory")
831    
832     #define LOCK_CAS(pd,s,r) \
833     ({ ATOMWORD_T a; \
834     asm volatile ("lock cmpxchg %2,%1" \
835     : "=a"(a) \
836     : "m"(*(pd)), "r"(s), "0"(r) \
837     : "memory");a;})
838    
839     #define LOCK_CASP(pd,s,r) \
840     ({ void *a; \
841     asm volatile ("lock cmpxchg %2,%1" \
842     : "=a"(a) \
843     : "m"(*(pd)), "r"(s), "0"(r) \
844     : "memory");a;})
845    
846     #elif defined(WIN32)
847    
848     typedef LONG ATOMWORD_T;
849    
850     #define LOCK_INC(p) InterlockedIncrement((LONG *)(p))
851     #define LOCK_DEC(p) InterlockedDecrement((LONG *)(p))
852     #define LOCK_CAS(pd,s,r) InterlockedCompareExchange((LONG *)(pd), s, r)
853     #define LOCK_CASP(pd,s,r) InterlockedCompareExchangePointer((PVOID *)(pd), (PVOID)(s), (PVOID)r)
854    
855     #else
856     #error "configuration not implemented"
857     #endif
858    
859     #if defined(WIN32)
860    
861     typedef DWORD THREAD_TIMEOUT_T;
862    
863     #define THREAD_INFINITE INFINITE
864    
865     typedef HANDLE THREAD_TH_T;
866     typedef HANDLE THREAD_EV_T;
867    
868     #define thread_sleep(n) Sleep(n)
869     #define thread_create(th, proc, arg) {(th) = (HANDLE)_beginthread(proc, 8192, arg);}
870     #define thread_create_event(ev, f) {(ev) = CreateEvent(NULL, TRUE, f, NULL);}
871     #define thread_signal_event(ev) SetEvent(ev)
872     #define thread_clear_event(ev) ResetEvent(ev)
873     #define thread_get_tid() GetCurrentThread()
874     #define thread_set_priority(tid,n) SetThreadPriority(tid, n)
875 notanpe 200 #if 0
876 notanpe 227 #undef thread_set_priority
877 notanpe 200 #endif
878 notanpe 148 #define thread_set_affinity(tid,m) SetThreadAffinityMask(tid, (DWORD_PTR)1 << (m))
879    
880     static
881 chapuni 1 int
882 notanpe 148 thread_wait_event(THREAD_EV_T ev, DWORD tmo)
883     {
884     DWORD r = WaitForSingleObject(ev, tmo);
885     return (r < 0
886     ? r
887     : (r == WAIT_TIMEOUT
888     ? -1
889     : r));
890     }
891    
892 notanpe 227 #elif defined(_POSIX_SOURCE) || defined(FREEBSD)
893 notanpe 148
894     #include <pthread.h>
895     #include <unistd.h>
896    
897     typedef int THREAD_TIMEOUT_T;
898    
899     #define THREAD_INFINITE INT_MAX
900    
901     #if defined(THREAD_PRIORITY_BELOW_NOROMAL) || defined(THREAD_PRIORITY_IDLE)
902     #error "unsupported implementation"
903     #endif
904    
905     #define THREAD_PRIORITY_NORMAL 14
906     #define THREAD_PRIORITY_BELOW_NORMAL 15
907     #define THREAD_PRIORITY_IDLE 16
908    
909     typedef pthread_t THREAD_TH_T;
910     typedef struct
911     {
912     pthread_mutex_t m;
913     pthread_cond_t c;
914     int volatile f;
915     } THREAD_EV_T;
916    
917     #define thread_sleep(n) (usleep(1000 * (n)) != EINVAL || sleep((n) / 1000))
918     #define thread_create(th, proc, arg) thread_create_p(&(th), proc, arg)
919     #define thread_create_event(ev, f) thread_create_event_p(&(ev), f)
920     #define thread_signal_event(ev) thread_set_event_p(&(ev), 1)
921     #define thread_clear_event(ev) thread_set_event_p(&(ev), 0)
922     #define thread_wait_event(ev,tmo) thread_wait_event_p(&(ev), tmo)
923    
924     static
925     void
926     thread_create_p(pthread_t *th, NORETURN (*proc)(void *), void *param)
927     {
928     pthread_create(th, NULL, (void *(*)(void *))proc, param);
929     }
930    
931     static
932     void
933     thread_create_event_p(THREAD_EV_T *ev, int f)
934     {
935     ev->f = f;
936     pthread_cond_init(&ev->c, NULL);
937     pthread_mutex_init(&ev->m, NULL);
938     }
939    
940     static
941     void
942     thread_set_event_p(THREAD_EV_T *ev, int f)
943     {
944     pthread_mutex_lock(&ev->m);
945     if (ev->f != f)
946     {
947     ev->f = f;
948     pthread_cond_broadcast(&ev->c);
949     }
950     pthread_mutex_unlock(&ev->m);
951     }
952    
953     static
954     int
955     thread_wait_event_p(THREAD_EV_T *ev, int a_tmo)
956     {
957     int timeout = a_tmo;
958     struct timeval now;
959     struct timespec tmo;
960     int r;
961    
962     pthread_mutex_lock(&ev->m);
963    
964     /* ŒťÝŽž‚Š‚çƒ^ƒCƒ€ƒAƒEƒgŽž‚đ‹‚ß‚é
965     ‚ß‚ń‚Ç‚­‚š[ */
966     gettimeofday(&now, NULL);
967     tmo.tv_sec = now.tv_sec + (timeout / 1000);
968     timeout %= 1000;
969     timeout *= 1000;
970     if (now.tv_usec >= 1000000 - timeout)
971     {
972     timeout -= 1000000;
973     tmo.tv_sec++;
974     }
975     tmo.tv_nsec = 1000 * (now.tv_usec + timeout);
976     r = 0;
977     while (!ev->f)
978     {
979     r = pthread_cond_timedwait(&ev->c, &ev->m, &tmo);
980     if (r == ETIMEDOUT
981     && a_tmo < THREAD_INFINITE)
982     break;
983     }
984    
985     pthread_mutex_unlock(&ev->m);
986    
987     return (r == ETIMEDOUT
988     ? (ETIMEDOUT < 0 ? ETIMEDOUT : -1)
989     : 0);
990     }
991    
992     #if defined(__linux__)
993    
994     /* ƒfƒtƒHƒ‹ƒgƒXƒPƒWƒ…[ƒŠƒ“ƒOƒ|ƒŠƒV[‚Ĺ‚Í
995     —Dć“xÝ’肾‚˝‚čƒAƒCƒhƒ‹ƒXƒŒƒbƒh‹N‚ą‚ľ‚Ä‚ŕ
996     ‚¨‚ŕ‚ľ‚ë‚­‚Č‚˘‚̂ŁA‚ť‚̂ւń‚͍ĄŒă‚ĚŒ¤‹†‰Ű‘čB */
997    
998     #include <linux/unistd.h>
999     _syscall0(pid_t,gettid)
1000    
1001     #define thread_get_tid() gettid()
1002    
1003     static
1004     int thread_set_affinity(pid_t tid, int i)
1005     {
1006     cpu_set_t m;
1007     CPU_ZERO(&m);
1008     CPU_SET(i, &m);
1009     return sched_setaffinity(tid, sizeof(m), &m);
1010     }
1011    
1012     #else
1013    
1014     /* POSIX ‚ł́AƒXƒŒƒbƒh’PˆĘ‚ĚƒXƒPƒWƒ…[ƒŠƒ“ƒO‚ɉî“ü‚Ĺ‚Ť‚Č‚˘B */
1015    
1016     #endif
1017    
1018     #else
1019     #error "configuration not supported"
1020     #endif
1021    
1022     struct THREAD_PARAM
1023     {
1024     /* ˆČ‰ş‚Í‹¤’ʏî•ń‚ĚƒRƒs[ */
1025     CODE_T *code;
1026 notanpe 192 THREAD_EV_T *p_ev_ks_activated;
1027 notanpe 148 ATOMWORD_T volatile *p_nidle; /* ‘Ň‚ż‚É“ü‚Á‚˝‚瑝‰Á */
1028    
1029     /* ˆČ‰ş‚̓XƒŒƒbƒhŒĹ—L */
1030     #ifdef thread_set_priority
1031     THREAD_TH_T th;
1032     int pri;
1033     #endif
1034     };
1035    
1036     static
1037     volatile ATOMWORD_T wp_crypt, rp_crypt;
1038     static
1039     struct PACKET_CRYPT64 *volatile q_crypt[NQ_CRYPT];
1040    
1041     static
1042     volatile ATOMWORD_T wp_cmp, rp_cmp;
1043     static
1044     struct PACKET_CRYPT64 *volatile q_cmp[NQ_CMP];
1045    
1046     static
1047     uint64_t
1048     thread_avail(void)
1049     {
1050     #if !USE_MT
1051    
1052     return 0x1U;
1053    
1054     #elif defined(WIN32) /* Win32 API */
1055     DWORD_PTR mask, mask_s;
1056     if (!GetProcessAffinityMask(GetCurrentProcess(),
1057     &mask,
1058     &mask_s)
1059     || !mask
1060     || !mask_s)
1061     return 0x1U;
1062     #if DEBUG>=1
1063     fprintf(stderr,
1064     "m=%08X s=%08X\n",
1065     (unsigned)mask,
1066     (unsigned)mask_s);
1067     #endif
1068     if (popcnt64(mask_s) == 1)
1069     /* ‰˝‚ŕŒž‚¤‚Ü‚˘ */;
1070     else if (mask == mask_s)
1071     fprintf(stderr,
1072     "’ʏí‚Ě%d”{‚Ƃ͂悭Œž‚Á‚˝‚ŕ‚̂łˇB\n",
1073     popcnt64(mask));
1074     else
1075     fprintf(stderr,
1076     "Ĺ‚‘Ź—Í‚Ě%g”{‚̗͂łĂŤ‚Ć‚¤‚ɂނń‚΂é‚ćB\n",
1077     (double)popcnt64(mask) / popcnt64(mask_s));
1078     return mask;
1079    
1080     #elif defined(__linux__) /* sched.h Šg’Ł */
1081    
1082     int i;
1083     uint64_t m = 0;
1084     cpu_set_t am;
1085     if (sched_getaffinity(getpid(), sizeof(am), &am) < 0)
1086     return 0x1U;
1087    
1088     for (i = 0; i < 64 && i < CPU_SETSIZE; i++)
1089     if (CPU_ISSET(i, &am))
1090     m |= 1ULL << i;
1091    
1092     return m;
1093     #else
1094    
1095     /* XXX ƒvƒƒZƒbƒT”‚đ’˛‚×ă‚°‚Ä‚­‚ž‚ł‚˘ */
1096     return 0x01U;
1097    
1098     #endif
1099     }
1100    
1101     static
1102     NORETURN
1103     thread_crypt64(void *a_param)
1104     {
1105     struct THREAD_PARAM *param = a_param;
1106     CODE_T *code = param->code;
1107     struct PACKET_CRYPT64 *pkt;
1108     #ifdef thread_set_priority
1109     THREAD_TH_T th = thread_get_tid();
1110     thread_set_priority(th, param->pri);
1111     #endif
1112    
1113     for(;;)
1114     {
1115     ATOMWORD_T rp;
1116     ATOMWORD_T wp;
1117    
1118     /* ƒLƒ…[‚Š‚ç—v‹‚đŽć‚čo‚ˇ */
1119     for (;;)
1120     {
1121     while ((rp = rp_crypt,
1122     WRAP(wp_crypt, NQ_CRYPT) == WRAP(rp, NQ_CRYPT)
1123     /*|| q_crypt[WRAP(rp, NQ_CRYPT)] == NULL*/))
1124     {
1125     THREAD_TIMEOUT_T tmo = (WRAP(wp_crypt, NQ_CRYPT) == WRAP(rp, NQ_CRYPT)
1126     ? THREAD_INFINITE
1127     : 1);
1128     int r;
1129    
1130 notanpe 192 /* Q‚˝ */
1131 notanpe 148 if (tmo == THREAD_INFINITE)
1132     {
1133     LOCK_INC(param->p_nidle);
1134     }
1135    
1136     /* —v‹‘Ň‚ż */
1137 notanpe 192 r = thread_wait_event(*param->p_ev_ks_activated, tmo);
1138 notanpe 148
1139     if (tmo == THREAD_INFINITE)
1140     {
1141     /* ‹N‚ą‚ł‚ę‚˝ */
1142     LOCK_DEC(param->p_nidle);
1143     }
1144     else if (r >= 0)
1145     {
1146     /* ‚ŕ‚¤‚ż‚ĺ‚Á‚ƐQ‚Ă݂é */
1147     thread_sleep(tmo);
1148     }
1149    
1150     /* ŽŠ‚ç‚Ě—Dć“x‚đ–ß‚ˇ
1151     (ŠO‚Š‚çƒu[ƒXƒg‚ł‚ę‚Ă邊‚ŕ) */
1152     #ifdef thread_set_priority
1153     if (r >= 0)
1154     thread_set_priority(th, param->pri);
1155     #endif
1156     }
1157    
1158     if (LOCK_CAS(&rp_crypt, rp + 1, rp) != rp)
1159     continue;
1160     rp = WRAP(rp, NQ_CRYPT);
1161     break;
1162     }
1163    
1164     pkt = q_crypt[rp];
1165     assert(pkt != NULL);
1166     pkt = LOCK_CASP(&q_crypt[rp], NULL, pkt);
1167     assert(pkt != NULL);
1168    
1169     /* ŽŔs‚ľ‚Ă݂é */
1170     CALL_CRYPT64(code, &pkt->key64, &pkt->param64);
1171    
1172     /* Œ‹‰Ę‚đƒLƒ…[‚É‚˝‚˝‚Ťž‚Ţ */
1173     for (;;)
1174     {
1175     while ((wp = wp_cmp,
1176     WRAP(rp_cmp - 1, NQ_CMP) == WRAP(wp, NQ_CMP))
1177     || q_cmp[WRAP(wp, NQ_CMP)] != NULL)
1178     {
1179     #if DEBUG>=1
1180     fprintf(stderr,
1181     "q_cmp stalled(%d,%d) %p\n",
1182     (unsigned)WRAP(wp, NQ_CMP),
1183     (unsigned)WRAP(rp_cmp - 1, NQ_CMP),
1184     q_cmp[WRAP(wp, NQ_CMP)]);
1185     #endif
1186     thread_sleep(1);
1187     }
1188    
1189     if (LOCK_CAS(&wp_cmp, wp + 1, wp) != wp)
1190     continue;
1191     wp = WRAP(wp, NQ_CMP);
1192     break;
1193     }
1194    
1195     pkt = LOCK_CASP(&q_cmp[wp], pkt, NULL);
1196     assert(pkt == NULL);
1197     }
1198     }
1199    
1200 notanpe 197 void
1201     usage( path )
1202     char *path;
1203     {
1204     char *myName, *chPtr;
1205    
1206     myName = basename( path );
1207     for ( chPtr = myName; *chPtr != '\0'; chPtr++ ) {
1208     if ( *chPtr == '.' ) {
1209     *chPtr = '\0';
1210     break;
1211     }
1212     }
1213 notanpe 229 #ifdef ALLKEY
1214 notanpe 230 printf( "%s [-t num] [-m mask] [-s num] [-g num] [-k salt] [-v] [\"str\"]\n", myName );
1215 notanpe 229 #else /* ALLKEY */
1216 notanpe 230 printf( "%s [-t num] [-m mask] [-s num] [-g num] [-v] [\"str\"]\n", myName );
1217 notanpe 229 #endif /* ALLKEY */
1218 notanpe 197 printf( " -t num : ŒŸőƒXƒŒƒbƒh” ( %d … num … %d )\n",
1219     MIN_THREAD, MAX_THREAD );
1220 notanpe 199 printf( " -m mask : ŽŔs‚ˇ‚é CPU ‚đŽw’股‚éƒ}ƒXƒN ( 1 ƒrƒbƒg … mask ‚Ěƒrƒbƒg” … %d ƒrƒbƒg )\n",
1221     MAX_THREAD );
1222 notanpe 203 printf( " -s num : —”‚ĚŽí ( 1 … num … %u )\n", UINT_MAX );
1223 notanpe 229 printf( " -g num : ‚Ş‚ń‚΂肎‚ ‚˘ ( %d (‚â‚é‹C‚Č‚ľ) … num … %d (ƒtƒ‹ƒpƒ[), ƒfƒtƒHƒ‹ƒg‚́A%d )\n", MIN_GEAR, MAX_GEAR, DEF_GEAR );
1224 notanpe 227 #ifdef ALLKEY
1225     printf( " -k salt : ‰–‚đŽw’č\n" );
1226     #endif /* ALLKEY */
1227 notanpe 203 printf( " -v : ç’ˇƒƒbƒZ[ƒW\n" );
1228 notanpe 197 printf( " str : ć“ނɖ„‚ߍž‚Ţ•śŽš—ń ( %d … str ‚ĚƒoƒCƒg” … %d )\n",
1229     MIN_UME, MAX_UME );
1230     }
1231 notanpe 198
1232     /* ƒqƒbƒgŽž‚ɂ͏o—̓tƒ@ƒCƒ‹‚Ö‚Ěƒ|ƒCƒ“ƒ^‚đ•Ô‚ˇ */
1233     FILE *
1234 notanpe 202 checkSpecial( trip, kind )
1235 notanpe 198 char *trip;
1236 notanpe 202 unsigned char *kind;
1237 notanpe 198 {
1238 notanpe 218 #ifdef CHIN
1239 notanpe 222 /* ‹­§“I‚ÉŒŸő */
1240 notanpe 218 if ( trip[0] == 'C' && trip[1] == 'h' && trip[2] == 'i' &&
1241     trip[3] == 'n' && trip[4] == 'k' && trip[5] == 'o' ) {
1242     strcpy( kind, "‚ż" );
1243     return( cfp );
1244     }
1245     #endif /* CHIN */
1246    
1247 notanpe 202 if ( special & ST_BUOO ) {
1248     /* ‚Ô‚¨ [A-Za-z]aoo[A-Za-z]uoo$ */
1249     if ( trip[3] == 'a' && trip[4] == 'o' && trip[5] == 'o' &&
1250     trip[7] == 'u' && trip[8] == 'o' && trip[9] == 'o' &&
1251     isalpha( trip[2] ) && isalpha( trip[6] ) ) {
1252     strcpy( kind, "‚Ô" );
1253 notanpe 198 return( tfp );
1254     }
1255     }
1256    
1257     if ( special & ST_NIKO ) {
1258     /* “ń\ */
1259     int i;
1260     char ch1, ch2;
1261     ch1 = trip[0];
1262     for ( i = 1; i < TRIP_LEN; i++ ) {
1263     if ( trip[i] != ch1 ) break;
1264     }
1265     ch2 = trip[i];
1266     for ( ; i < TRIP_LEN; i++ ) {
1267     if ( trip[i] != ch1 && trip[i] != ch2 ) goto NONIKO;
1268     }
1269 notanpe 202 strcpy( kind, "“ń" );
1270 notanpe 198 return( tfp );
1271     }
1272     NONIKO:
1273    
1274 notanpe 231 if ( special & ST_EROI ) {
1275     /* ƒGƒ‚˘l looooloooo */
1276     if ( trip[0] == trip[5] &&
1277     trip[1] == trip[2] && trip[1] == trip[3] && trip[1] == trip[4] &&
1278     trip[1] == trip[6] && trip[1] == trip[7] && trip[1] == trip[8] &&
1279     trip[1] == trip[9] ) {
1280     strcpy( kind, "ƒG" );
1281     return( tfp );
1282     }
1283     }
1284    
1285 notanpe 224 if ( special & ST_YAKU ) {
1286     /* ”މ_ */
1287     if ( trip[0] == trip[1] && trip[0] == trip[2] &&
1288     trip[3] == trip[4] && trip[3] == trip[5] &&
1289     trip[6] == trip[7] && trip[6] == trip[8] && trip[9] == '.' ) {
1290     strcpy( kind, "”Ş" );
1291     return( tfp );
1292     }
1293     }
1294    
1295 notanpe 212 /* ”ň˜AŠÖ˜A‚ĚƒR[ƒh‚́AžŰŘÝ ŸCeleron/rc ě */
1296     if ( special & ST_HREN ) {
1297     /* ”ň˜A */
1298     int w, x = 0, y = 0;
1299     for ( w = 0; w < TRIP_LEN; w++ ) {
1300     if ( trip[w] == trip[0] ) x += 1;
1301     if ( trip[w] == trip[1] ) y += 1;
1302     if ( x >= 8 || y >= 8 ) {
1303     strcpy( kind, "”ň" );
1304     return( tfp );
1305     }
1306     }
1307     }
1308    
1309 notanpe 202 if ( special & ST_ALLN ) {
1310 notanpe 204 /* ‘S” ‚Š “{” */
1311 notanpe 202 if ( isdigit( trip[0] ) && isdigit( trip[1] ) && isdigit( trip[2] ) &&
1312     isdigit( trip[3] ) && isdigit( trip[4] ) && isdigit( trip[5] ) &&
1313     isdigit( trip[6] ) && isdigit( trip[7] ) && isdigit( trip[8] ) &&
1314     isdigit( trip[9] ) ) {
1315 notanpe 204 if ( special & ST_DOSU ) {
1316     /* ‘S” & ‰ń•ś */
1317     if ( trip[0] == trip[9] && trip[1] == trip[8] && trip[2] == trip[7] &&
1318     trip[3] == trip[6] && trip[4] == trip[5] ) {
1319     strcpy( kind, "“{" );
1320     return( tfp );
1321     }
1322     /* ‘S” & ‘o˜A */
1323     if ( trip[0] == trip[1] && trip[2] == trip[3] && trip[4] == trip[5] &&
1324     trip[6] == trip[7] && trip[8] == trip[9] ) {
1325     strcpy( kind, "“{" );
1326     return( tfp );
1327     }
1328     /* ‘S” & ŽR•F */
1329     if ( trip[0] == trip[5] && trip[1] == trip[6] && trip[2] == trip[7] &&
1330     trip[3] == trip[8] && trip[4] == trip[9] ) {
1331     strcpy( kind, "“{" );
1332     return( tfp );
1333     }
1334 notanpe 227 /* Ĺ‘ĺ‚ĆĹŹ‚ÍAƒ 8 ˜A‚ŏo‚é‚Ě‚Ĺíœ */
1335 notanpe 204 } else {
1336     strcpy( kind, "”" );
1337     return( nfp );
1338     }
1339 notanpe 202 }
1340     }
1341    
1342 notanpe 198 return( NULL );
1343     }
1344 notanpe 197
1345 notanpe 148 /***************************************************************
1346     *
1347     * ƒƒCƒ“ƒ‹[ƒv‚Ć‚Š
1348     *
1349     */
1350    
1351     int
1352 chapuni 1 main(int argc, char *argv[])
1353     {
1354 chapuni 74 int i;
1355 chapuni 1 int mincnt;
1356 chapuni 77 int nblk_hit, nblk_total;
1357 chapuni 84 int nap_hit, nap_total;
1358 notanpe 148 CODE_T *code = NULL;
1359     off_t code_cmp;
1360 chapuni 1 FILE *ofp;
1361 chapuni 46 FILE *sfp; /* scoreboard */
1362     struct ITREE *root_expr;
1363 notanpe 148 uint64_t proc_mask;
1364     int ks_activated;
1365 notanpe 192 static THREAD_EV_T event_ks_activated;
1366 notanpe 148 static ATOMWORD_T volatile nidle;
1367     struct THREAD_PARAM *threads = NULL;
1368     int nthreads;
1369     int npkts;
1370     struct PACKET_CRYPT64 *pkts, *pkt_hit;
1371     uint64_t pkts_vacant;
1372     int tn;
1373 chapuni 1 int cr;
1374    
1375 notanpe 148 /* ŒŽ•śŽš—ń */
1376     uint8_t key[8 + 8];
1377    
1378 chapuni 74 int xhash_loaded;
1379    
1380 notanpe 229 #if defined(WIN32)
1381     #define YOUSUMI 6
1382     int maxSpeed; /* ˆę•bŠÔ‚ÉŒŸő‚ľ‚˝ŒÂ”‚ĚĹ‘ĺ’l (YOUSUMI ‰ńŒv‘Ş’†) */
1383     int sCnt; /* ‘Ź“x‚đ•\ŽŚ‚ľ‚˝‰ń” */
1384     int lCntMax; /* Sleep() ‚Ě‚˝‚ß‚Ěƒ‹[ƒvĹ‘ĺ’l */
1385     int lCnt; /* Sleep() ‚Ě‚˝‚ß‚Ěƒ‹[ƒvƒJƒEƒ“ƒ^ */
1386     #endif /* WIN32 */
1387    
1388 notanpe 119 #define UPDATE_INTERVAL 8 /* ‘Ź“x•\ŽŚ‚ĚŠÔŠu •b */
1389 notanpe 115 struct status {
1390 notanpe 192 uint64_t startTime; /* ŠJŽnŽž ƒ~ƒŠ•b */
1391     uint64_t lastTime; /* ĹŒă‚É•\ŽŚ‚ľ‚˝Žž ƒ~ƒŠ•b */
1392     uint64_t loop; /* ‘ŒŸőŒÂ” */
1393     uint64_t lastloop; /* ĹŒă‚É•\ŽŚ‚ľ‚˝Žž‚Ě loop */
1394 notanpe 115 } status;
1395 notanpe 192 uint64_t curTime;
1396     uint32_t upd_int = 0;
1397 chapuni 1
1398 notanpe 193 #if defined(WIN32)
1399     SetPriorityClass( GetCurrentProcess(), IDLE_PRIORITY_CLASS );
1400 notanpe 229 maxSpeed = 0;
1401     sCnt = 0;
1402     lCntMax = 0;
1403     lCnt = 0;
1404 notanpe 193 #endif
1405 notanpe 197
1406     {
1407     int optChar;
1408     extern char *optarg;
1409     extern int optind;
1410 notanpe 199 char *chPtr;
1411 notanpe 197
1412     nThread = 0;
1413 notanpe 199 pmask = 0;
1414 notanpe 203 seed = 0;
1415 notanpe 214 seedOffset = 0; /* ƒRƒ}ƒ“ƒhƒ‰ƒCƒ“ƒIƒvƒVƒ‡ƒ“‚ł͂Ȃ˘‚ށA‚ą‚ą‚ŏ‰Šú‰ť */
1416 notanpe 203 verbose = 0;
1417 notanpe 229 gear = DEF_GEAR;
1418 notanpe 227 #ifdef ALLKEY
1419     fixedSaltChar[0] = '\0';
1420 notanpe 229 while ( (optChar = getopt(argc, argv, "t:m:s:g:k:vh")) != EOF ) {
1421 notanpe 227 #else /* ALLKEY */
1422 notanpe 229 while ( (optChar = getopt(argc, argv, "t:m:s:g:vh")) != EOF ) {
1423 notanpe 227 #endif /* ALLKEY */
1424 notanpe 197 switch ( optChar ) {
1425     case 't':
1426     nThread = atoi( optarg );
1427     if ( nThread < MIN_THREAD || nThread > MAX_THREAD ) {
1428     usage( argv[0] );
1429     exit( 1 );
1430     }
1431     break;
1432 notanpe 199 case 'm':
1433     if ( strlen( optarg ) > MAX_THREAD ) {
1434     usage( argv[0] );
1435     exit( 1 );
1436     }
1437     for ( chPtr = optarg; *chPtr != '\0'; chPtr++ ) {
1438     pmask <<= 1;
1439     switch ( *chPtr ) {
1440     case '0':
1441     /* ‚Č‚É‚ŕ‚ľ‚Č‚˘ */
1442     break;
1443     case '1':
1444     pmask |= 1;
1445     break;
1446     default:
1447     usage( argv[0] );
1448     exit( 1 );
1449     break;
1450     }
1451     }
1452 notanpe 200 if ( pmask < MIN_THREAD ) {
1453     usage( argv[0] );
1454     exit( 1 );
1455     }
1456 notanpe 199 break;
1457 notanpe 203 case 's':
1458     if ( optarg[0] == '-' ) {
1459     usage( argv[0] );
1460     exit( 1 );
1461     }
1462     seed = (unsigned int)atoi( optarg );
1463     if ( seed < 1 || seed > UINT_MAX ) {
1464     usage( argv[0] );
1465     exit( 1 );
1466     }
1467     break;
1468 notanpe 229 case 'g':
1469     gear = atoi( optarg );
1470     if ( gear < MIN_GEAR || gear > MAX_GEAR ) {
1471     usage( argv[0] );
1472     exit( 1 );
1473     }
1474     break;
1475 notanpe 227 #ifdef ALLKEY
1476     case 'k':
1477     if ( strlen( optarg ) != 2 ) {
1478     usage( argv[0] );
1479     exit( 1 );
1480     }
1481     for ( i = 0; i < 2; i++ ) {
1482     if ( !isdigit( optarg[i] ) && !isalpha( optarg[i] ) &&
1483     optarg[i] != '.' && optarg[i] != '/' ) {
1484     usage( argv[0] );
1485     exit( 1 );
1486     }
1487     fixedSaltChar[i] = optarg[i];
1488     }
1489     break;
1490     #endif /* ALLKEY */
1491 notanpe 203 case 'v':
1492     verbose = 1;
1493     break;
1494 notanpe 197 case 'h':
1495     usage( argv[0] );
1496     exit( 0 );
1497     break;
1498 notanpe 227 default:
1499     usage( argv[0] );
1500     exit( 1 );
1501 notanpe 197 }
1502     }
1503    
1504     switch ( argc - optind ) {
1505     case 0:
1506     umeStr[0] = '\0';
1507     umeLen = KEY_SHUFFLE_POS;
1508     break;
1509     case 1:
1510     strcpy( umeStr, argv[optind] );
1511     umeLen = strlen( umeStr );
1512     if ( umeLen < MIN_UME || umeLen > MAX_UME ) {
1513     usage( argv[0] );
1514     exit( 1 );
1515     }
1516     break;
1517     default:
1518     usage( argv[0] );
1519     exit( 1 );
1520     }
1521     }
1522 notanpe 213 #ifdef REON
1523     regExpStr[0] = '\0';
1524     #endif /* REON */
1525 notanpe 222
1526 notanpe 226 #ifdef NAMA
1527 notanpe 225 fprintf(stderr, "śƒL[ƒo[ƒWƒ‡ƒ“\n");
1528     fprintf(stderr, "–œ‚ވęAƒgƒŠƒbƒv‚މť‚Ż‚˝ę‡A\n");
1529     fprintf(stderr, "http://trip.is-a-geek.net/test/read.cgi/ra8bbs/1217089930/\n");
1530     fprintf(stderr, "‚É•ń‚ˇ‚é‚ĆŽŸ‚Ěƒo[ƒWƒ‡ƒ“‚ʼnü‘P‚ł‚ę‚邊‚ŕB\n\n");
1531 notanpe 226 #endif /* NAMA */
1532 notanpe 225
1533 notanpe 222 #ifdef MAKEY
1534 notanpe 226 fprintf(stderr, "–‚ƒL[‹óŠÔƒo[ƒWƒ‡ƒ“‚ɂ‚ŤAƒVƒƒEƒg‚ɂ͂¨‚ˇ‚ˇ‚߂łŤ‚Č‚˘B\n\n");
1535 notanpe 222 #endif /* MAKEY */
1536    
1537     #ifdef ALLKEY
1538 notanpe 227 fprintf(stderr, "^E‘S‹óŠÔƒo[ƒWƒ‡ƒ“‚ɂ‚ŤAƒVƒƒEƒg‚ɂ͂¨‚ˇ‚ˇ‚߂łŤ‚Č‚˘B\n\n");
1539 notanpe 222 #endif /* MAKEY */
1540 notanpe 193
1541 notanpe 227 if ( !cpuid_issupported() ) {
1542     fprintf( stderr, "‚ą‚̊‹Ť‚Ĺ‘–‚炚‚邹‚Ć‚Ş‘z’肳‚ę‚Ä‚˘‚Ü‚š‚ńB\n" );
1543     exit( 1 );
1544     }
1545 chapuni 1
1546 notanpe 227 assert( (1 << N_STRIDE) == N_ALU * ALU_BITS );
1547 chapuni 10
1548 chapuni 46 /* ƒ^ƒQ“ǂݍž‚Ý */
1549 notanpe 227 root_expr = expr_parse( "target.txt" );
1550 chapuni 2
1551 notanpe 222 #ifdef CHIN
1552     special |= ST_CHIN;
1553     #endif /* CHIN */
1554 notanpe 203 if ( verbose ) {
1555     printf( "“ÁŽęŒŸőƒIƒvƒVƒ‡ƒ“ : " );
1556 notanpe 204 if ( special & ST_DOSU ) {
1557     printf( "“{” " );
1558     } else {
1559     if ( special & ST_ALLN ) {
1560     printf( "‘S” " );
1561     }
1562 notanpe 203 }
1563     if ( special & ST_NIKO ) {
1564     printf( "“ń\ " );
1565     }
1566     if ( special & ST_BUOO ) {
1567     printf( "‚Ô‚¨ " );
1568     }
1569 notanpe 207 if ( special & ST_HREN ) {
1570     printf( "”ň˜A " );
1571     }
1572 notanpe 224 if ( special & ST_YAKU ) {
1573     printf( "”މ_ " );
1574     }
1575 notanpe 203 if ( special ) {
1576     printf( "ƒIƒ“I\n" );
1577     } else {
1578     printf( "ƒI[ƒ‹ƒIƒtI\n" );
1579     }
1580 notanpe 214 if ( seedOffset != 0 ) {
1581     printf( "—”‚ĚŽí‚ĚƒIƒtƒZƒbƒg = %d\n", seedOffset );
1582     }
1583 notanpe 213 #ifdef REON
1584     if ( regExpStr[0] != '\0' ) {
1585     printf( "ł‹K•\Œť : %s\n", regExpStr );
1586     }
1587     #endif /* REON */
1588 notanpe 229 if ( gear != DEF_GEAR ) {
1589     printf( "‚Ş‚ń‚΂肎‚ ‚˘ : %d\n", gear );
1590     }
1591 notanpe 203 }
1592    
1593 notanpe 148 /* ƒR[ƒh‚đśŹE“WŠJ
1594     ‹N“Ž—\’čƒXƒŒƒbƒh”‚ɉž‚ś‚Ä
1595     śŹ‚ˇ‚éƒR[ƒh‚đ•Ď‚Ś‚é */
1596 chapuni 46 sfp = scoreboard_open();
1597 notanpe 227 /* prologue & ƒRƒAƒ‹[ƒv */
1598     fwrite( crypt64_descs[0]->pro, 1,
1599     crypt64_descs[0]->cmp_pro - crypt64_descs[0]->pro, sfp );
1600 notanpe 197
1601 notanpe 214 /* proc_mask ‚ÉŽg—p‚ˇ‚é CPU ‚Ěƒ}ƒXƒN‚đƒZƒbƒg */
1602 notanpe 227 if ( pmask == 0 ) {
1603     /* Žw’肪‚Ȃ݂ę‚΁AŽg‚Ś‚é CPU ‘S•” */
1604     proc_mask = thread_avail();
1605     } else {
1606     /* Žw’肳‚ę‚˝ CPU ‚Ě‘śÝƒ`ƒFƒbƒNB‚ž‚ށAĄ‚Í Win32 ‚ĚƒR[ƒh‚ľ‚ЂȂ˘B */
1607 notanpe 205 #ifdef WIN32
1608 notanpe 227 DWORD_PTR processMask, systemMask;
1609     if ( GetProcessAffinityMask( GetCurrentProcess(), &processMask, &systemMask )
1610     == 0 ) {
1611     printf( "CPU ‚ĚŠ„‚č“–‚ĂɎ¸”s ‚ť‚Ě 1\n" );
1612     exit( 1 );
1613     }
1614     if ( (processMask & pmask) != pmask ) {
1615     printf( "‚ť‚ń‚Č CPU ‚͂˂ĽI\n" );
1616     exit( 1 );
1617     }
1618 notanpe 205 #endif /* WIN32 */
1619 notanpe 214
1620 notanpe 227 proc_mask = pmask;
1621     printf( "CPU : " );
1622     for ( i = 0; i < MAX_THREAD; i++ ) {
1623     if ( pmask & 1 ) {
1624     printf( "%d ", i );
1625 notanpe 200 }
1626 notanpe 227 pmask >>= 1;
1627     }
1628     printf( "‚đŽg—p‚ľ‚Ü‚ˇB\n" );
1629 notanpe 214
1630     /* CPU ‚ĚŠ„‚č“–‚āB‚ž‚ށAĄ‚Í Wi(r */
1631 notanpe 200 #ifdef WIN32
1632 notanpe 227 if ( SetProcessAffinityMask( GetCurrentProcess(), proc_mask ) == 0 ) {
1633     printf( "CPU ‚ĚŠ„‚č“–‚ĂɎ¸”s ‚ť‚Ě 2\n" );
1634     exit( 1 );
1635     }
1636 notanpe 200 #endif /* WIN32 */
1637 notanpe 227 }
1638 notanpe 214
1639 notanpe 227 /* ƒXƒŒƒbƒh”‚ĚŽw’肪‚Č‚˘ę‡‚́Aproc_mask ‚Š‚çƒQƒbƒc */
1640     if ( nThread == 0 ) {
1641     nThread = popcnt64( proc_mask );
1642     }
1643 notanpe 214
1644 notanpe 227 fprintf( stderr, "%d ŒÂ‚ĚŒŸőƒXƒŒƒbƒh‚𐜐Ź\n", nThread );
1645 notanpe 197
1646 notanpe 227 if ( nThread == 1 ) {
1647     npkts = 1;
1648     pkts_vacant = 1;
1649     code_cmp = 0;
1650     } else {
1651     /* epilogue */
1652     fwrite( crypt64_descs[0]->ep, 1,
1653     crypt64_descs[0]->ep_end - crypt64_descs[0]->ep, sfp );
1654     /* ”äŠrŠí‚݂̂𐜐Ź(‘O”ź) */
1655     code_cmp = ftell( sfp );
1656     fseek( sfp, (-code_cmp) & 63, SEEK_CUR );
1657     code_cmp = ftell( sfp );
1658     /* prologue */
1659     fwrite( crypt64_descs[0]->pro, 1,
1660     crypt64_descs[0]->crypt - crypt64_descs[0]->pro, sfp );
1661     npkts = 64;
1662     pkts_vacant = (uint64_t)-1; /* (1 << 64) - 1 ‚đŒvŽZ‚ľ‚˝‚­‚Č‚˘ */
1663     }
1664 chapuni 2
1665 notanpe 148 /* ”äŠr•”‚đśŹ */
1666 notanpe 227 /* ”äŠrŠí€”ő */
1667     fwrite( crypt64_descs[0]->cmp_pro, 1,
1668     crypt64_descs[0]->cmp_ep - crypt64_descs[0]->cmp_pro, sfp );
1669     tn = synth_synthesize( sfp, root_expr );
1670     /* epilogue */
1671     fwrite( crypt64_descs[0]->cmp_ep, 1,
1672     crypt64_descs[0]->ep_end - crypt64_descs[0]->cmp_ep, sfp );
1673 chapuni 46 /* ƒR[ƒh‚đƒƒ‚ƒŠ‚É“\‚č•t‚Ż‚é */
1674 notanpe 227 code = scoreboard_map( sfp );
1675 chapuni 1
1676     /* ƒL[‚̏‰Šú‰ť */
1677 notanpe 203 if ( seed == 0 ) {
1678 notanpe 214 seed = (unsigned int)time( NULL ) + seedOffset;
1679 notanpe 203 }
1680 notanpe 204 if ( verbose ) {
1681     printf( "—”‚ĚŽí = %u\n", seed );
1682     }
1683     srand( seed );
1684 notanpe 222
1685     #ifdef KEYCHK
1686     {
1687     int i;
1688    
1689 notanpe 223 for ( i = 0; i < 100000; i++ ) {
1690 notanpe 222 printf( "%02x %02x %02x %02x %02x %02x %02x %02x\n",
1691     key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7] );
1692 notanpe 227 key_init( key );
1693 notanpe 222 }
1694     exit( 0 );
1695     }
1696     #endif /* KEYCHK */
1697    
1698 notanpe 227 key_init( key );
1699     set_salt( code, crypt64_descs[0], key );
1700 chapuni 1
1701 notanpe 148 /* ‰‰ŽZƒpƒPƒbƒg‚đěŹ */
1702 notanpe 227 pkts = packet_create( npkts, tn, key );
1703 notanpe 148 pkt_hit = &pkts[npkts - 1];
1704    
1705     /* “­‚­‚¨‚ś‚ł‚ń‚đ—ĘŽY */
1706 notanpe 227 thread_create_event( event_ks_activated, 1 );
1707 notanpe 148 ks_activated = 1;
1708     nthreads = 0;
1709 notanpe 227 if ( code_cmp ) {
1710     THREAD_TH_T h;
1711     int ots = -1;
1712     threads = calloc( 2 * nThread, sizeof(*threads) );
1713     for ( i = 0; i < nThread; i++ ) {
1714     if ( ots < 0 ) {
1715     /* ŽŠ•ŞŽŠg‚ĚƒXƒPƒWƒ…[ƒŠƒ“ƒO
1716     ‚ą[‚ä[Œn‚ĚƒAƒvƒŠ‚Í’á‚߂ɐݒ股‚é‚Ě‚Ş‹g(‚Š‚ŕ) */
1717 notanpe 148 #ifdef WIN32
1718 notanpe 227 h = GetCurrentProcess();
1719     SetPriorityClass( h, BELOW_NORMAL_PRIORITY_CLASS );
1720 chapuni 1 #endif
1721 notanpe 227 #if defined( thread_set_priority )
1722     /* S‚ĚŒ„ŠÔ‚¨–„‚ß‚ľ‚Ü‚ˇ */
1723     threads[nthreads].code = code;
1724     threads[nthreads].p_ev_ks_activated = &event_ks_activated;
1725     threads[nthreads].p_nidle = &nidle;
1726     threads[nthreads].pri = THREAD_PRIORITY_IDLE;
1727     thread_create( h, thread_crypt64, &threads[nthreads] );
1728     threads[nthreads].th = h;
1729     nthreads++;
1730 notanpe 148 #endif
1731 notanpe 227 if ( !code_cmp ) break;
1732     /* ŽŠ•ŞŽŠg‚ĚŽc‚č‚̐ݒč‚đA‚ ‚Ƃłâ‚é */
1733     ots = i;
1734     } else {
1735     /* ‘źƒXƒŒƒbƒh‚́A‚â‚â’á‚߂̗Dć“x‚ŁB */
1736     threads[nthreads].code = code;
1737     threads[nthreads].p_ev_ks_activated = &event_ks_activated;
1738     threads[nthreads].p_nidle = &nidle;
1739 notanpe 148 #ifdef thread_set_priority
1740 notanpe 227 threads[nthreads].pri = THREAD_PRIORITY_BELOW_NORMAL;
1741 notanpe 148 #endif
1742 notanpe 227 thread_create( h, thread_crypt64, &threads[nthreads] );
1743 notanpe 148 #ifdef thread_set_priority
1744 notanpe 227 threads[nthreads].th = h;
1745 notanpe 148 #endif
1746 notanpe 227 nthreads++;
1747     }
1748     }
1749     }
1750 notanpe 148
1751 notanpe 227 if ( (ofp = fopen( "log.txt", "at" )) == NULL ) {
1752     perror( "log.txt" );
1753     return errno;
1754     }
1755     setvbuf(ofp, NULL, _IONBF, BUFSIZ);
1756 chapuni 1
1757 notanpe 227 if ( (tfp = fopen( "logspe.txt", "at" )) == NULL ) {
1758     perror( "logspe.txt" );
1759 notanpe 198 return errno;
1760     }
1761     setvbuf( tfp, NULL, _IONBF, BUFSIZ );
1762 notanpe 227 if ( (nfp = fopen( "lognum.txt", "at" )) == NULL ) {
1763     perror( "lognum.txt" );
1764 notanpe 204 return errno;
1765     }
1766     setvbuf( nfp, NULL, _IONBF, BUFSIZ );
1767 notanpe 218 #ifdef CHIN
1768 notanpe 227 if ( (cfp = fopen( "logchi.txt", "at" )) == NULL ) {
1769     perror( "logchi.txt" );
1770 notanpe 218 return errno;
1771     }
1772     setvbuf( cfp, NULL, _IONBF, BUFSIZ );
1773     #endif /* CHIN */
1774 notanpe 213 #ifdef REON
1775 notanpe 227 if ( (rfp = fopen( "logreg.txt", "at" )) == NULL ) {
1776     perror( "logreg.txt" );
1777 notanpe 213 return errno;
1778     }
1779     setvbuf( rfp, NULL, _IONBF, BUFSIZ );
1780     #endif /* REON */
1781 notanpe 198
1782 chapuni 1 mincnt = 0x7FFFFFFF;
1783 chapuni 77 nblk_hit = nblk_total = 0;
1784 chapuni 84 nap_hit = nap_total = 0;
1785 chapuni 1 cr = 0;
1786 notanpe 120 memset( &status, 0, sizeof( struct status ) );
1787 notanpe 119 status.startTime = status.lastTime = usec();
1788 notanpe 213
1789     #ifdef MINAST
1790     #undef assert
1791     #define assert(x)
1792     #endif /* MINAST */
1793    
1794 chapuni 1 /* ’Tőƒ‹[ƒv‚ž‚ź‚Á‚Ć */
1795 notanpe 227 for ( ;; ) {
1796     struct PACKET_CRYPT64 *pkt_c;
1797     uint64_t cnt;
1798     #if DEBUG>=1
1799     int cnt1, cnt2;
1800     #endif
1801     int k, kk;
1802 chapuni 1
1803 notanpe 227 /* ”äŠrŠíŒó•â(may be NULL)
1804     ć‚ɃLƒ…[‚Š‚çŽć‚čo‚ˇ */
1805     pkt_c = q_cmp[WRAP( rp_cmp, NQ_CMP )];
1806     if ( pkt_c != NULL && WRAP( rp_cmp, NQ_CMP ) != WRAP( wp_cmp, NQ_CMP ) ) {
1807     pkt_c = LOCK_CASP( &q_cmp[WRAP( rp_cmp, NQ_CMP )], NULL, pkt_c );
1808     assert( pkt_c != NULL );
1809     LOCK_INC( &rp_cmp );
1810     /* ƒpƒPƒbƒg‚đ vacant ‚ɉń‚ľ‚Ä‚¨‚­ */
1811     pkts_vacant |= 1ULL << (pkt_c - pkts);
1812     }
1813 notanpe 148
1814 notanpe 227 /* Saltƒ`ƒFƒ“ƒW‘Ň‚ż */
1815     if ( !ks_activated ) {
1816     ATOMWORD_T rp;
1817     if ( pkt_c == NULL ) {
1818     if ( (rp = rp_crypt, WRAP( rp, NQ_CRYPT ) != WRAP( wp_crypt, NQ_CRYPT ))
1819     && LOCK_CAS( &rp_crypt, rp + 1, rp ) == rp ) {
1820     /* !ks_activate ó‘Ԃł́AŽŠ‚ç‚ŕ—v‹ƒLƒ…[‚đ‚â‚Á‚Â‚Ż‚É‚˘‚­ */
1821     rp = WRAP( rp, NQ_CRYPT );
1822     pkt_c = q_crypt[rp];
1823     assert( pkt_c != NULL );
1824     pkt_c = LOCK_CASP( &q_crypt[rp], NULL, pkt_c );
1825     assert( pkt_c != NULL );
1826     assert( pkt_c != pkt_hit );
1827     CALL_CRYPT64( code, &pkt_c->key64, &pkt_c->param64 );
1828     /* ƒpƒPƒbƒg‚đ vacant ‚ɉń‚ľ‚Ä‚¨‚­ */
1829     pkts_vacant |= 1ULL << (pkt_c - pkts);
1830     } else {
1831     /* ‚â‚͂股‚邹‚Ć‚Ş‚Č‚˘‚̂ł܂Á‚˝‚č‚Ɖ߂˛‚ˇ */
1832     if ( nidle != nthreads ) thread_sleep(1);
1833     }
1834     }
1835     if ( nidle == nthreads ) {
1836     assert( WRAP( rp_crypt, NQ_CRYPT ) == WRAP( wp_crypt, NQ_CRYPT ) );
1837     /* Salt ƒ`ƒFƒ“ƒW‚މ”\ */
1838     set_salt( code, crypt64_descs[0], key );
1839     if ( nthreads ) thread_signal_event( event_ks_activated );
1840     ks_activated = 1;
1841     }
1842     }
1843 chapuni 1
1844 notanpe 227 /* ŒŽ‚đƒLƒ…[‚É‚˝‚˝‚Ťž‚݂܂­‚é */
1845     if ( !ks_activated ) {
1846     /* ŒŽ‚đ“o˜^‚ľ‚Č‚˘ */
1847     ;
1848     } else {
1849     for ( i = npkts - 1; i >= 0; i-- ) {
1850     if ( pkts_vacant & (1ULL << i) ) {
1851     int j;
1852     if ( i == npkts - 1 ) {
1853     /* ‘O’i‚ŁA“­‚­‚¨‚ś‚ł‚ń‚Š‚çŒ‹‰Ę‚đ‚ŕ‚ç‚Á‚Ä‚˘‚˝‚çA‰˝‚ŕ‚ľ‚Č‚˘ */
1854     if ( pkt_c != NULL ) continue;
1855     } else {
1856     /* ‘O’i‚ĹŽć‚čo‚ľ‚˝‚Î‚Š‚č‚Ě“­‚­‚¨‚ś‚ł‚ń‚́A‘¸d‚ˇ‚é */
1857     if ( &pkts[i] == pkt_c ) continue;
1858     /* queue full ‚Ěę‡‚ÍŒŠ‘—‚é */
1859     /* XXX 16 ‚͂ĂŤ‚Ć‚¤ */
1860     if ( WRAP( wp_crypt, NQ_CRYPT ) == WRAP( rp_crypt - 16, NQ_CRYPT )
1861     || q_crypt[WRAP( wp_crypt, NQ_CRYPT )] != NULL ) break;
1862     }
1863     /* ŒŽ‚ĚƒZƒbƒg */
1864     for ( j = 0; j < 8; j++ ) {
1865     key_set64( &pkts[i].key64, j, key[j], key[j] ^ pkts[i].uk.key[j], 0 );
1866     pkts[i].uk.key[j] = key[j];
1867     }
1868     if ( i == npkts - 1 ) {
1869     /* ŽŸ’i‚Ĺ CRYPT64->CMP */
1870     assert(pkt_c == NULL);
1871     pkt_c = &pkts[i];
1872     assert( pkt_c == pkt_hit );
1873     } else {
1874     /* ƒLƒ…[‚É‚˝‚˝‚Ťž‚Ţ */
1875     while ( LOCK_CASP( &q_crypt[WRAP( wp_crypt, NQ_CRYPT )], &pkts[i], NULL )
1876     != NULL ) {
1877     /* ÝŒvă‚Í‚ą‚ą‚É—ˆ‚Č‚˘ */
1878     fprintf( stderr,
1879     "q_crypt ‚đ‰˜‚ľ‚Ä‚é‚̂͒N‚ž? (rp=%3d, wp=%3d, v=%08X%08X)\n",
1880     (unsigned)WRAP( rp_crypt, NQ_CRYPT ),
1881     (unsigned)WRAP( wp_crypt, NQ_CRYPT ),
1882     (unsigned)( pkts_vacant >> 32 ),
1883     (unsigned)pkts_vacant );
1884     thread_sleep( 1000 );
1885     }
1886     LOCK_INC( &wp_crypt );
1887     pkts_vacant ^= 1ULL << i;
1888     assert( !(pkts_vacant & (1ULL << i)) ); /* í‚ę */
1889     }
1890     /* ŒŽ‘‰Á‚Í‚ą‚ń‚ȂƂą‚ë‚ɈړŽ! */
1891     assert( ks_activated );
1892     if ( !key_inc( key, 6 ) && !key_inc( key, umeLen ) ) {
1893     /* ŒŽ‚ĚƒVƒƒƒbƒtƒ‹ q_crypt ‚ŞŽJ‚Ż‚é‚܂ŁAset_salt() ‚͂łŤ‚Č‚˘ */
1894 notanpe 148 #if DEBUG>=1
1895 notanpe 227 fprintf( stderr, "********************************SHUFFLE!\n" );
1896 notanpe 148 #endif
1897 notanpe 227 if ( nthreads ) thread_clear_event( event_ks_activated );
1898     key_reset( key, 0 );
1899     /* ƒLƒ…[‚ĚŒŽ‚ŞŽJ‚Ż‚é‚Ü‚ĹƒAƒCƒhƒ‹ó‘Ô‚É */
1900     ks_activated = 0;
1901     /* ƒXƒŒƒbƒh‚đƒu[ƒXƒg‚ľ‚ĉń‚é */
1902 notanpe 148 #ifdef thread_set_priority
1903 notanpe 227 for ( j = 0; j < nthreads; j++ ) {
1904     assert( threads != NULL );
1905     thread_set_priority( threads[j].th, THREAD_PRIORITY_NORMAL );
1906     }
1907 notanpe 148 #endif
1908 notanpe 227 /* ƒ‹[ƒv‘ąs‚Í‚ŕ‚Í‚â•s—v */
1909     break;
1910     }
1911     }
1912     }
1913     }
1914     /* ‚ˇ‚邹‚Ć‚Ş‚Č‚­‚Č‚Á‚Ä‚˘‚éę‡ */
1915     if ( pkt_c == NULL ) {
1916     assert( !ks_activated );
1917     continue;
1918     }
1919     /* ŒÄ‚Ô!
1920     LR ‰Šú‰ť‚́AƒTƒuƒ‚ƒWƒ…[ƒ‹“ŕ‚ōs‚¤‚ׂľ
1921     FASTCALL ‚ɏ€‚ś‚˝ŒÄ‚яo‚ľ‚Ě‚˝‚߁A
1922     ƒzƒ“ƒg‚Í‚˘‚낢‚냌ƒWƒXƒ^‚Ş”j‰ó‚ł‚ę‚éƒnƒYc‚Č‚ń‚ž‚ށB */
1923     if ( pkt_c != pkt_hit ) {
1924     assert( code_cmp != 0 );
1925     cnt = CALL_CMP64( code + code_cmp, pkt_hit->param64.hit,
1926     pkt_c->param64.lr );
1927     } else {
1928     /* ‚悤‚â‚­ŽŠƒXƒŒƒbƒh‚ʼnń‚š‚é */
1929     cnt = CALL_CRYPT64( code, &pkt_c->key64, &pkt_c->param64 );
1930     if ( code_cmp ) {
1931     cnt = CALL_CMP64( code + code_cmp, pkt_c->param64.hit,
1932     pkt_c->param64.lr );
1933     }
1934     }
1935     /* ƒqƒbƒg‚ľ‚˝‚Ć‚Ť‚̏ˆ—
1936     key ‚¨‚ć‚Ń lr ‚Í pkt_c ‚ɍ‡’v”ť’č‚Í pkt_hit ‚É“ü‚Á‚Ä‚˘‚éƒnƒY */
1937     xhash_loaded = 0;
1938     for ( kk = 0; kk < N_ALU; kk++ ) {
1939     ALU_T t;
1940     static uint64_t xhash[64];
1941     if ( special
1942 notanpe 213 #ifdef REON
1943 notanpe 227 || regExpStr[0] != '\0'
1944 notanpe 213 #endif /* REON */
1945 notanpe 227 ) {
1946     CALL_TR64( &pkt_c->param64.lr[0][0].q[kk / (N_ALU / N_Q)], xhash );
1947     xhash_loaded = 1;
1948     }
1949     if ( !(kk & (N_ALU / N_Q - 1)) ) nblk_total++, xhash_loaded = 0;
1950     if ( special
1951 notanpe 213 #ifdef REON
1952 notanpe 227 || regExpStr[0] != '\0'
1953 notanpe 213 #endif /* REON */
1954 notanpe 227 ) {
1955     char hash[16];
1956     FILE *lfp;
1957     unsigned char kind[3];
1958     for ( k = 0; k < ALU_BITS; k++ ) {
1959     for ( i = 1; i < 11; i++ ) {
1960     unsigned c = 0;
1961     c = (xhash[(ALU_BITS * kk + k) & 63] >> (6 * (i - 1))) & 0x3F;
1962     hash[i - 1] = C64[c];
1963     }
1964     hash[10] = 0;
1965 notanpe 213 #ifdef REON
1966 notanpe 227 if ( regExpStr[0] != '\0' ) {
1967     if ( regexec( &regExp, hash, (size_t)0, NULL, 0 ) == 0 ) {
1968     if ( cr ) fprintf( stderr, "\n" );
1969     cr = 0;
1970 notanpe 226 #ifdef NAMA
1971 notanpe 227 hit( rfp, hash, pkt_c->uk.key, kk, k, "ł", MAKAI_FALSE );
1972 notanpe 226 #else /* NAMA */
1973 notanpe 227 hit( rfp, hash, pkt_c->uk.key, kk, k, "ł", MAKAI_TRUE );
1974     #endif /* NAMA */
1975     }
1976 notanpe 213 }
1977     #endif /* REON */
1978 notanpe 227 if ( special ) {
1979     if ( (lfp = checkSpecial( hash, kind )) != NULL ) {
1980     if ( cr ) fprintf( stderr, "\n" );
1981     cr = 0;
1982 notanpe 226 #ifdef NAMA
1983 notanpe 227 hit( lfp, hash, pkt_c->uk.key, kk, k, kind, MAKAI_FALSE );
1984 notanpe 226 #else /* NAMA */
1985 notanpe 227 hit( lfp, hash, pkt_c->uk.key, kk, k, kind, MAKAI_TRUE );
1986     #endif /* NAMA */
1987     }
1988 notanpe 213 }
1989     }
1990     }
1991 notanpe 198
1992 notanpe 227 t = pkt_hit->param64.hit[HIT_ANY].a[kk];
1993     if ( !t ) continue;
1994     nap_total += ALU_BITS;
1995     for ( k = 0; k < ALU_BITS; k++ ) {
1996     char hash[16];
1997     int hitLen;
1998     if ( !(t & ((ALU_T)1 << k)) ) continue;
1999     nap_hit++;
2000     /* “]’u */
2001     if ( !xhash_loaded ) {
2002     nblk_hit++;
2003     CALL_TR64( &pkt_c->param64.lr[0][0].q[kk / (N_ALU / N_Q)], xhash );
2004     xhash_loaded = 1;
2005     }
2006     /* ŽŤ‘‚𒲂ׂé */
2007     hitLen = 0;
2008     if ( !((pkt_hit->param64.hit[HIT_BOOL].a[kk] & ((ALU_T)1 << k))
2009     || (hitLen = wdict_ishit( pkt_hit->param64.hit, kk, k,
2010     xhash[(ALU_BITS * kk + k) & 0x3F]))) )
2011     continue;
2012     for ( i = 1; i < 11; i++ ) {
2013     unsigned c;
2014     c = (xhash[(ALU_BITS * kk + k) & 63] >> (6 * (i - 1))) & 0x3F;
2015     hash[i - 1] = C64[c];
2016     }
2017     hash[10] = 0;
2018     if ( cr ) fprintf( stderr, "\n" );
2019     cr = 0;
2020     {
2021     unsigned char len[10];
2022     sprintf( len, "%02d", hitLen );
2023 notanpe 226 #ifdef NAMA
2024 notanpe 227 hit( ofp, hash, pkt_c->uk.key, kk, k, len, MAKAI_FALSE );
2025 notanpe 226 #else /* NAMA */
2026 notanpe 227 hit( ofp, hash, pkt_c->uk.key, kk, k, len, MAKAI_TRUE );
2027 notanpe 226 #endif /* NAMA */
2028 notanpe 227 }
2029     }
2030     }
2031 notanpe 193
2032 notanpe 227 /* ‘Ź“xŒv‘Ş */
2033     status.loop += N_ALU * ALU_BITS;
2034 notanpe 229
2035     #if defined(WIN32)
2036     if ( sCnt >= YOUSUMI ) {
2037     lCnt++;
2038     if ( lCnt > lCntMax ) {
2039     Sleep( (DWORD)10 );
2040     lCnt = 0;
2041     }
2042     }
2043     #endif /* WIN32 */
2044    
2045 notanpe 227 if ( status.loop >= status.lastloop + upd_int
2046     && (curTime = usec()) != status.lastTime ) {
2047     uint64_t diffTime;
2048     int a, b, c;
2049     /* ’ĘŽZ(’PˆĘ ktrips/sec) */
2050     diffTime = curTime - status.startTime;
2051     a = status.loop / ((1000 / USEC_SEC) * diffTime);
2052 notanpe 213 #ifdef BENCH
2053 notanpe 227 if ( (diffTime / USEC_SEC) > (BENCH * 60) ) {
2054     fprintf( stderr, "\n%6dktrips/s\n", a );
2055     exit( 0 );
2056     }
2057 notanpe 213 #endif /* BENCH */
2058 notanpe 227 /* ‹ćŠÔ(’PˆĘ trips/sec) */
2059     diffTime = curTime - status.lastTime;
2060     b = USEC_SEC * (status.loop - status.lastloop) / diffTime;
2061 notanpe 229 #if defined(WIN32)
2062     if ( gear != DEF_GEAR ) {
2063     if ( sCnt < YOUSUMI ) {
2064     if ( b > maxSpeed ) {
2065     maxSpeed = b;
2066     /* x = l/s * (gear / (10 - gear)) / 100 */
2067     lCntMax = maxSpeed / (N_ALU * ALU_BITS) * (gear * 1000 / (10 - gear)) / 100000;
2068     if ( lCntMax < 1 ) {
2069     lCntMax = 1;
2070     }
2071     }
2072     sCnt++;
2073     }
2074     }
2075     #endif /* WIN32 */
2076 notanpe 227 /* —\‘Ş */
2077     c = UPDATE_INTERVAL * b;
2078     /* —§‚żă‚Ş‚č‚ȂǁAŒëˇ‚Ş‚ ‚č upd_int ‚ŞŹ‚ł‚ˇ‚Ź‚˝‚Ć‚Ť‚Í
2079     ‚˘‚Ť‚Č‚č‘S•␳‚š‚¸ 1 •b(==b)‚ŽűĘ‚ł‚š‚éB */
2080     upd_int = ( upd_int + b < c ? upd_int + b : c );
2081     status.lastTime = curTime;
2082     status.lastloop = status.loop;
2083     fprintf( stderr, "%6dktrips/s [%6d.%03dktrips/s]\r",
2084     a, b / 1000, b % 1000 );
2085     cr++;
2086     }
2087     }
2088 notanpe 213
2089 chapuni 1 return 0;
2090     }
2091    
2092 chapuni 2 /*
2093     * Local Variables:
2094     * tab-width: 4
2095     * End:
2096     *
2097     * EOF */

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Rev URL

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26