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