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