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