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