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