| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <string.h> |
| 33 |
#include <ctype.h> |
| 34 |
#include <stdlib.h> |
| 35 |
|
| 36 |
#include "ntnum.h" |
| 37 |
#include "table.h" |
| 38 |
#include "hash.h" |
| 39 |
#include "ophel.h" |
| 40 |
#include "original.h" |
| 41 |
#include "lmtable.h" |
| 42 |
|
| 43 |
static uint64_t ntnum_sizes[10] = { 4, 7086244, 259203456, 518406912, 259204606, 518409212, 259212464, 518424928, 259218600, 518437200}; |
| 44 |
|
| 45 |
int ntnum_setup(void *tbl_) { |
| 46 |
table_t *tbl = tbl_; |
| 47 |
|
| 48 |
tbl->ncols = 15000; |
| 49 |
tbl->offset = 100000; |
| 50 |
tbl->sizes = ntnum_sizes; |
| 51 |
|
| 52 |
tbl->find = ntnum_find; |
| 53 |
tbl->check = ntnum_check; |
| 54 |
tbl->isvalid = ntnum_isvalid; |
| 55 |
|
| 56 |
tbl->lookup_idx = lmtable_lookup_idx; |
| 57 |
tbl->lookup_end = lmtable_lookup_end; |
| 58 |
tbl->lookup_srt = ntnum_lookup_srt; |
| 59 |
|
| 60 |
return 1; |
| 61 |
} |
| 62 |
|
| 63 |
void ntnum_find(void *hsh_, void *tbl_, void *el_) { |
| 64 |
hash_t *hsh = hsh_; |
| 65 |
table_t *tbl = tbl_; |
| 66 |
ophel_t *el = el_; |
| 67 |
uchar_t *hash = hsh->hash; |
| 68 |
uchar_t *pwd = (uchar_t*)el->pwd; |
| 69 |
ophstat_t *stat = el->stat; |
| 70 |
|
| 71 |
int c; |
| 72 |
int col = el->col; |
| 73 |
int ncols = tbl->ncols; |
| 74 |
int n_redux = col + tbl->idx * tbl->offset; |
| 75 |
|
| 76 |
uchar_t tmp[16]; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ntnum_mkredux(tbl, hash, pwd, n_redux); |
| 81 |
|
| 82 |
for (c=1; c<ncols-col; ++c) { |
| 83 |
++n_redux; |
| 84 |
|
| 85 |
ntnum_mkhash(pwd, tmp); |
| 86 |
ntnum_mkredux(tbl, tmp, pwd, n_redux); |
| 87 |
++stat->hredux; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
uint32_t prefix = ntnum_bin11(pwd, 6); |
| 92 |
uint16_t postfix = (uint16_t) ntnum_bin11(pwd+6,6); |
| 93 |
|
| 94 |
el->prefix = prefix; |
| 95 |
el->postfix = postfix; |
| 96 |
} |
| 97 |
|
| 98 |
int ntnum_lookup_srt(void *hsh_, void *tbl_, void *el_) { |
| 99 |
table_t *tbl = tbl_; |
| 100 |
ophel_t *el = el_; |
| 101 |
|
| 102 |
uint64_t offset = el->offset; |
| 103 |
uint32_t start; |
| 104 |
|
| 105 |
ophstat_t *stat = el->stat; |
| 106 |
++stat->start; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if (tbl->srtmem) { |
| 111 |
uint32_t *mem = (uint32_t*)(tbl->srtmem + offset); |
| 112 |
start = mem[0]; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
else { |
| 118 |
FILE *srtfile = tbl->srtfile; |
| 119 |
|
| 120 |
fseeko(srtfile, offset, SEEK_SET); |
| 121 |
fread(&start, sizeof(start), 1, srtfile); |
| 122 |
++stat->fseek_srt; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
start = ftohl(start); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
uchar_t *pwd = (uchar_t*)el->pwd; |
| 132 |
|
| 133 |
uint32_t len = (start >= 1000000000) ? 10 : (start >= 100000000) ? 9 : |
| 134 |
(start >= 10000000) ? 8 : (start >= 1000000) ? 7 : (start >= 100000) ? 6 : |
| 135 |
(start >= 10000) ? 5 : (start >= 1000) ? 4 : (start >= 100) ? 3 : |
| 136 |
(start >= 10) ? 2 : 1; |
| 137 |
|
| 138 |
memset(pwd, 0, 13); |
| 139 |
memset(pwd, '0', 10-len); |
| 140 |
sprintf((char*)pwd+10-len, "%u", start); |
| 141 |
|
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
int ntnum_check(void *hsh_, void *tbl_, void *el_) { |
| 146 |
hash_t *hsh = hsh_; |
| 147 |
table_t *tbl = tbl_; |
| 148 |
ophel_t *el = el_; |
| 149 |
uchar_t *hash = hsh->hash; |
| 150 |
uchar_t *pwd = (uchar_t*)el->pwd; |
| 151 |
ophstat_t *stat = el->stat; |
| 152 |
|
| 153 |
int c; |
| 154 |
int col = el->col; |
| 155 |
int ncols = tbl->ncols; |
| 156 |
int n_redux = tbl->idx * tbl->offset; |
| 157 |
|
| 158 |
uchar_t tmp[16]; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
stat->falarm_hredux = 0; |
| 163 |
|
| 164 |
for (c=ncols-col; c<ncols; ++c) { |
| 165 |
ntnum_mkhash(pwd, tmp); |
| 166 |
ntnum_mkredux(tbl, tmp, pwd, n_redux); |
| 167 |
|
| 168 |
++n_redux; |
| 169 |
++stat->hredux; |
| 170 |
++stat->falarm_hredux; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
ntnum_mkhash(pwd, tmp); |
| 176 |
|
| 177 |
if (memcmp(tmp, hash, sizeof(tmp)) == 0) { |
| 178 |
++stat->match_table; |
| 179 |
stat->falarm_hredux = 0; |
| 180 |
return 1; |
| 181 |
} |
| 182 |
|
| 183 |
else { |
| 184 |
++stat->falarm; |
| 185 |
return 0; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
int ntnum_isvalid(void *hsh_, void *tbl_) { |
| 190 |
hash_t *hsh = hsh_; |
| 191 |
|
| 192 |
return hsh->kind == nt ? 1 : 0; |
| 193 |
} |
| 194 |
|
| 195 |
void ntnum_mkredux(table_t *tbl, uchar_t *hash, uchar_t *pwd, |
| 196 |
int n_redux) { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
uint32_t idx[4]; |
| 201 |
|
| 202 |
idx[0] = ftohl(*(uint32_t*)hash) ^ (uint32_t) n_redux; |
| 203 |
idx[1] = ftohl(*(uint32_t*)(hash+4)); |
| 204 |
idx[2] = ftohl(*(uint32_t*)(hash+8)); |
| 205 |
idx[3] = ftohl(*(uint32_t*)(hash+12)); |
| 206 |
|
| 207 |
|
| 208 |
uint64_t q = (uint64_t) idx[1] << 32 | (uint64_t) idx[0]; |
| 209 |
uint64_t l = (uint64_t) idx[3] << 32 | (uint64_t) idx[2]; |
| 210 |
l = l % 1000000000000LLU; |
| 211 |
|
| 212 |
uint32_t len = (l >= 100000000000LLU) ? 12 : (l >= 10000000000LLU) ? 11 : |
| 213 |
(l >= 1000000000) ? 10 : (l >= 100000000) ? 9 : (l >= 10000000) ? 8 : |
| 214 |
(l >= 1000000) ? 7 : (l >= 100000) ? 6 : (l >= 10000) ? 5 : |
| 215 |
(l >= 1000) ? 4 : (l >= 100) ? 3 : (l >= 10) ? 2 : 1; |
| 216 |
|
| 217 |
q = (len == 12) ? q % 1000000000000LLU : (len == 11) ? q % 100000000000LLU : |
| 218 |
(len == 10) ? q % 10000000000LLU : (len == 9) ? q % 1000000000 : |
| 219 |
(len == 8) ? q % 100000000 : (len == 7) ? q % 10000000 : |
| 220 |
(len == 6) ? q % 1000000 : (len == 5) ? q % 100000 : |
| 221 |
(len == 4) ? q % 10000 : (len == 3) ? q % 1000 : |
| 222 |
(len == 2) ? q % 100 : q % 10; |
| 223 |
|
| 224 |
uint32_t len_q = (q >= 100000000000LLU) ? 12 : (q >= 10000000000LLU) ? 11 : |
| 225 |
(q >= 1000000000) ? 10 : (q >= 100000000) ? 9 : (q >= 10000000) ? 8 : |
| 226 |
(q >= 1000000) ? 7 : (q >= 100000) ? 6 : (q >= 10000) ? 5 : |
| 227 |
(q >= 1000) ? 4 : (q >= 100) ? 3 : (q >= 10) ? 2 : 1; |
| 228 |
|
| 229 |
memset(pwd, 0, 13); |
| 230 |
memset(pwd, '0', len-len_q); |
| 231 |
snprintf((char*)pwd+len-len_q, len_q+1, "%llu", (long long unsigned int)q); |
| 232 |
|
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
void ntnum_mkhash(uchar_t *pwd, uchar_t *hash) { |
| 237 |
make_nthash((char*)pwd, (char*)hash); |
| 238 |
} |
| 239 |
|
| 240 |
uint32_t ntnum_bin11(uchar_t *input, int length) { |
| 241 |
|
| 242 |
uint32_t sum=0; |
| 243 |
int i = 0; |
| 244 |
|
| 245 |
for (i=0; i<length; i++) |
| 246 |
sum = sum*11 + (input[i] ? input[i]-0x2f : 0); |
| 247 |
return sum; |
| 248 |
} |