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