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