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