| 1 |
|
| /** |
| 2 |
|
| ** chad's modifications for perl xs - Digest::Nilsimsa |
| 3 |
|
| ** |
| 4 |
|
| ** main() - removed (too many warnings) |
| 5 |
|
| ** accbuf - added, practically identical to accfile() |
| 6 |
|
| ** dprint() - added (prints debug msgs to debug.txt) |
| 7 |
|
| ** |
| 8 |
|
| ** $Id: _nilsimsa.c,v 1.1 2002/05/20 22:29:07 chad Exp $ |
| 9 |
|
| **/ |
| 10 |
|
| |
| 11 |
|
| /*************************************************************************** |
| 12 |
|
| main.c - nilsimsa |
| 13 |
|
| ------------------- |
| 14 |
|
| begin : Fri Mar 16 01:41:08 EST 2001 |
| 15 |
|
| copyright : (C) 2001-2002 by cmeclax |
| 16 |
|
| email : cmeclax@ixazon.dynip.com |
| 17 |
|
| ***************************************************************************/ |
| 18 |
|
| |
| 19 |
|
| /*************************************************************************** |
| 20 |
|
| * * |
| 21 |
|
| * This program is free software; you can redistribute it and/or modify * |
| 22 |
|
| * it under the terms of the GNU General Public License as published by * |
| 23 |
|
| * the Free Software Foundation; either version 2 of the License, or * |
| 24 |
|
| * (at your option) any later version. * |
| 25 |
|
| * * |
| 26 |
|
| ***************************************************************************/ |
| 27 |
|
| |
| 28 |
|
| /* Computes a nilsimsa code for a file fed to stdin. Files with |
| 29 |
|
| similar content often have the same nilsimsa code. |
| 30 |
|
| Options eventually to be implemented: |
| 31 |
|
| --long-code Instead of a 256-bit vector, write the entire histogram as a code. |
| 32 |
|
| This will allow more accurate detection of certain kinds of viruses and worms. |
| 33 |
|
| --code-list Read a list of codes from a file. |
| 34 |
|
| Anywhere a code is called for, a file may be given; if a code is |
| 35 |
|
| intended and such a file exists, prepend a few zeros to the code. |
| 36 |
|
| */ |
| 37 |
|
| |
| 38 |
|
| #ifdef HAVE_CONFIG_H |
| 39 |
|
| #include <config.h> |
| 40 |
|
| #endif |
| 41 |
|
| |
| 42 |
|
| #include <sys/stat.h> |
| 43 |
|
| #include <unistd.h> |
| 44 |
|
| #include "nilsimsa.h" |
| 45 |
|
| /* |
| 46 |
|
| #include "cluster.h" |
| 47 |
|
| #include "rules.h" |
| 48 |
|
| */ |
| 49 |
|
| |
| 50 |
|
| unsigned int acc[256],threshold; |
| 51 |
|
| unsigned char tran[256],popcount[256]; |
| 52 |
|
| struct nsrecord *selkarbi,terkarbi,*rules,gunma; |
| 53 |
|
| int nilselkarbi,nrules; |
| 54 |
|
| char *comparestr,*rulefile,*checkrulefile; |
| 55 |
|
| int comparethreshold,minclustersize; |
| 56 |
|
| int helpflag,versionflag,aggregateflag,noheaderflag,verboseflag,catflag,mboxflag; |
| 57 |
|
| int exhaustiveflag,debugflag,recursiveflag; |
| 58 |
|
| int exitcode; |
| 59 |
|
| |
| 60 |
5 |
| void clear(struct nsrecord *a) |
| 61 |
5 |
| {a->total=a->threshold=0; |
| 62 |
5 |
| memset(a->acc,0,sizeof(a->acc)); |
| 63 |
5 |
| memset(a->code,0,sizeof(a->code)); |
| 64 |
5 |
| } |
| 65 |
|
| |
| 66 |
5 |
| void filltran() |
| 67 |
|
| {int i,j,k; |
| 68 |
1285 |
▶ | for (i=j=0;i<256;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=j=0;i<256;i++) |
1280 |
5 |
1285 |
|
| 69 |
1280 |
| {j=(j*53+1)&255; |
| 70 |
1280 |
| j+=j; |
| 71 |
1280 |
▶ | if (j>255) |
Branch: 1 branch
| Branch | True | False | Total |
| if (j>255) |
640 |
640 |
1280 |
|
| 72 |
640 |
| j-=255; |
| 73 |
209205 |
▶ | for (k=0;k<i;k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k=0;k<i;k++) |
207925 |
1280 |
209205 |
|
| 74 |
207925 |
▶ | if (j==tran[k]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (j==tran[k]) |
380 |
207545 |
207925 |
|
| 75 |
380 |
| {j=(j+1)&255; |
| 76 |
380 |
| k=0; |
| 77 |
|
| } |
| 78 |
1280 |
| tran[i]=j; |
| 79 |
|
| } |
| 80 |
5 |
| } |
| 81 |
|
| |
| 82 |
0 |
| void dumptran() |
| 83 |
|
| {int i; |
| 84 |
0 |
▶ | for (i=0;i<256;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<256;i++) |
0 |
0 |
0 |
|
| 85 |
0 |
| {printf("%02x ",tran[i]); |
| 86 |
0 |
▶ | if ((i&15)==15) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((i&15)==15) |
0 |
0 |
0 |
|
| 87 |
0 |
| putchar('\n'); |
| 88 |
|
| } |
| 89 |
0 |
| } |
| 90 |
|
| |
| 91 |
0 |
| void fillpopcount() |
| 92 |
|
| {int i,j; |
| 93 |
0 |
| memset(popcount,0,256); |
| 94 |
0 |
▶ | for (i=0;i<256;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<256;i++) |
0 |
0 |
0 |
|
| 95 |
0 |
▶ | for (j=0;j<8;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0;j<8;j++) |
0 |
0 |
0 |
|
| 96 |
0 |
| popcount[i]+=1&(i>>j); |
| 97 |
0 |
| } |
| 98 |
|
| |
| 99 |
0 |
| int defromulate(FILE *file) |
| 100 |
|
| #define CANY 258 |
| 101 |
|
| /* apply this line to the character in any, instead of reading one */ |
| 102 |
|
| #define ANY 257 |
| 103 |
|
| /* read a character into any, or emit any */ |
| 104 |
|
| #define NONE 256 |
| 105 |
|
| /* don't read a character, or don't emit one */ |
| 106 |
|
| /* Reads a character from a mailbox file according to these rules: |
| 107 |
|
| Ignore all lines up to and including the first one that begins |
| 108 |
|
| with "From ". |
| 109 |
|
| Return -2 on encountering "\nFrom ". Ignore that and the rest |
| 110 |
|
| of the line. |
| 111 |
|
| Return -3 if the file does not contain a "From " line (usually |
| 112 |
|
| this means that the mailbox is empty). |
| 113 |
|
| On seeing "\n>From ", return "\nFrom ". Generally, if "\n>" |
| 114 |
|
| is followed by any number of greater-than signs and "From ", |
| 115 |
|
| drop the last greater-than sign. |
| 116 |
|
| |
| 117 |
|
| State table: |
| 118 |
|
| |
| 119 |
|
| 00 \n 0 01 F 0 03 r 0 06 o 0 10 m 0 15 bl 0 21 \n -2 01 |
| 120 |
|
| * * * * * * * |
| 121 |
|
| * \n \n \n \n \n 0 |
| 122 |
|
| 00 02 04 07 11 16 21 |
| 123 |
|
| 0 0 0 0 |
| 124 |
|
| F F F F |
| 125 |
|
| 05 08 12 17 |
| 126 |
|
| 0 0 0 |
| 127 |
|
| r r r |
| 128 |
|
| 09 13 18 |
| 129 |
|
| 0 0 |
| 130 |
|
| o o |
| 131 |
|
| 02,05,09,14,20 0 * 00 14 19 |
| 132 |
|
| 0 |
| 133 |
|
| m |
| 134 |
|
| 20 |
| 135 |
|
| |
| 136 |
|
| 01 > \n 22 F 0 23 r 0 24 o 0 25 m 0 26 bl F 27 0 r 28 0 o 29 0 m 30 0 bl 0 |
| 137 |
|
| * * * * * |
| 138 |
|
| > > > > > |
| 139 |
|
| 02 04 07 11 16 |
| 140 |
|
| |
| 141 |
|
| 22 > > 22 |
| 142 |
|
| |
| 143 |
|
| * EOF EOF 31 F 0 32 r 0 33 o 0 34 m 0 35 bl 0 36 \n 0 01 |
| 144 |
|
| * * * * * * |
| 145 |
|
| * * * * * * |
| 146 |
|
| 31 31 31 31 31 36 |
| 147 |
|
| */ |
| 148 |
|
| {static const short statetable[][5][3]= |
| 149 |
|
| {{{'\n',NONE, 1},{EOF ,EOF ,31},{ANY ,ANY , 0},{0 ,0 , 0},{0 ,0 , 0}}, /* 0*/ |
| 150 |
|
| {{'F' ,NONE, 3},{EOF ,EOF ,31},{'>' ,'\n',22},{'\n','\n',01},{ANY ,'\n',02}}, |
| 151 |
|
| {{NONE,ANY ,38},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0}}, |
| 152 |
|
| {{'r' ,NONE, 6},{EOF ,EOF ,31},{ANY ,'\n', 4},{0 ,0 , 0},{0 ,0 , 0}}, |
| 153 |
|
| {{NONE,'F' , 5},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 154 |
|
| {{NONE,ANY ,38},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0}}, /* 5*/ |
| 155 |
|
| {{'o' ,NONE,10},{EOF ,EOF ,31},{ANY ,'\n', 7},{0 ,0 , 0},{0 ,0 , 0}}, |
| 156 |
|
| {{NONE,'F' , 8},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 157 |
|
| {{NONE,'r' , 9},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 158 |
|
| {{NONE,ANY ,38},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0}}, |
| 159 |
|
| {{'m' ,NONE,15},{EOF ,EOF ,31},{ANY ,'\n',11},{0 ,0 , 0},{0 ,0 , 0}}, /*10*/ |
| 160 |
|
| {{NONE,'F' ,12},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 161 |
|
| {{NONE,'r' ,13},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 162 |
|
| {{NONE,'o' ,14},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 163 |
|
| {{NONE,ANY ,38},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0}}, |
| 164 |
|
| {{' ' ,NONE,21},{EOF ,EOF ,31},{ANY ,'\n',16},{0 ,0 , 0},{0 ,0 , 0}}, /*15*/ |
| 165 |
|
| {{NONE,'F' ,17},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 166 |
|
| {{NONE,'r' ,18},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 167 |
|
| {{NONE,'o' ,19},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 168 |
|
| {{NONE,'m' ,20},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 169 |
|
| {{NONE,ANY ,38},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0},{NONE,ANY , 0}}, /*20*/ |
| 170 |
|
| {{'\n',-2 ,37},{EOF ,EOF ,31},{ANY ,NONE,21},{0 ,0 , 0},{0 ,0 , 0}}, |
| 171 |
|
| {{'>' ,'>' ,22},{'F' ,NONE,23},{ANY,'>' , 2},{0 ,0 , 0},{0 ,0 , 0}}, |
| 172 |
|
| {{'r' ,NONE,24},{ANY,'>' , 4},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 173 |
|
| {{'o' ,NONE,25},{ANY,'>' , 7},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 174 |
|
| {{'m' ,NONE,26},{ANY,'>' ,11},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, /*25*/ |
| 175 |
|
| {{' ' ,'F' ,27},{ANY,'>' ,16},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 176 |
|
| {{NONE,'r' ,28},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 177 |
|
| {{NONE,'o' ,29},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 178 |
|
| {{NONE,'m' ,30},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 179 |
|
| {{NONE,' ' , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0},{0 ,0 , 0}}, /*30*/ |
| 180 |
|
| {{'F' ,NONE,32},{EOF ,-3 ,31},{ANY ,NONE,31},{0 ,0 , 0},{0 ,0 , 0}}, |
| 181 |
|
| {{'r' ,NONE,33},{EOF ,-3 ,31},{ANY ,NONE,31},{0 ,0 , 0},{0 ,0 , 0}}, |
| 182 |
|
| {{'o' ,NONE,34},{EOF ,-3 ,31},{ANY ,NONE,31},{0 ,0 , 0},{0 ,0 , 0}}, |
| 183 |
|
| {{'m' ,NONE,35},{EOF ,-3 ,31},{ANY ,NONE,31},{0 ,0 , 0},{0 ,0 , 0}}, |
| 184 |
|
| {{' ' ,NONE,36},{EOF ,-3 ,31},{ANY ,NONE,31},{0 ,0 , 0},{0 ,0 , 0}}, /*35*/ |
| 185 |
|
| {{'\n',NONE,37},{EOF ,-3 ,31},{ANY ,NONE,36},{0 ,0 , 0},{0 ,0 , 0}}, |
| 186 |
|
| {{'F' ,NONE,40},{EOF ,EOF ,31},{'>' ,NONE,22},{ANY ,NONE, 2},{ANY ,NONE, 2}}, |
| 187 |
|
| {{CANY,NONE, 0},{'\n',NONE,39},{ANY ,NONE, 0},{0 ,0 , 0},{0 ,0 , 0}}, |
| 188 |
|
| {{'F' ,NONE, 3},{EOF ,EOF ,31},{'>' ,NONE,22},{'\n',NONE,01},{ANY ,NONE,02}}, |
| 189 |
|
| {{'r' ,NONE,41},{EOF ,EOF ,31},{ANY ,NONE, 4},{0 ,0 , 0},{0 ,0 , 0}}, /*40*/ |
| 190 |
|
| {{'o' ,NONE,42},{EOF ,EOF ,31},{ANY ,NONE, 7},{0 ,0 , 0},{0 ,0 , 0}}, |
| 191 |
|
| {{'m' ,NONE,43},{EOF ,EOF ,31},{ANY ,NONE,11},{0 ,0 , 0},{0 ,0 , 0}}, |
| 192 |
|
| {{' ' ,NONE,21},{EOF ,EOF ,31},{ANY ,NONE,16},{0 ,0 , 0},{0 ,0 , 0}}}; |
| 193 |
|
| static int any,state=31,ch,i; |
| 194 |
0 |
| do {for (i=0,ch=NONE;;i++) |
| 195 |
0 |
▶ | {if (statetable[state][i][0]==NONE) |
Branch: 1 branch
| Branch | True | False | Total |
| {if (statetable[state][i][0]==NONE) |
0 |
0 |
0 |
|
| 196 |
0 |
| break; |
| 197 |
0 |
▶ | if (statetable[state][i][0]==CANY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (statetable[state][i][0]==CANY) |
0 |
0 |
0 |
|
| 198 |
0 |
| {ch=any; |
| 199 |
0 |
| continue; |
| 200 |
|
| } |
| 201 |
0 |
▶ | if (i==0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i==0) |
0 |
0 |
0 |
|
| 202 |
0 |
| ch=getc(file); |
| 203 |
0 |
▶ | if (statetable[state][i][0]==ANY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (statetable[state][i][0]==ANY) |
0 |
0 |
0 |
|
| 204 |
0 |
| any=ch; |
| 205 |
0 |
▶ | if (statetable[state][i][0]==ANY || statetable[state][i][0]==ch) |
Branch: 2 branches
| Branch | True | False | Total |
| if (statetable[state][i][0]==ANY || statetable[state][i][0]==ch) |
0 |
0 |
0 |
| if (statetable[state][i][0]==ANY || statetable[state][i][0]==ch) |
0 |
0 |
0 |
|
| 206 |
|
| break; |
| 207 |
|
| } |
| 208 |
|
| /*if (ch==NONE) |
| 209 |
|
| printf(" "); |
| 210 |
|
| else |
| 211 |
|
| printf("%3x",ch&0xfff);*/ |
| 212 |
0 |
| ch=statetable[state][i][1]; |
| 213 |
0 |
▶ | if (ch==ANY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch==ANY) |
0 |
0 |
0 |
|
| 214 |
0 |
| ch=any; |
| 215 |
0 |
| state=statetable[state][i][2]; |
| 216 |
|
| /*printf("%3d%3x %c\n",state,ch&0xfff,isprint(ch)?ch:' ');*/ |
| 217 |
|
| } |
| 218 |
0 |
▶ | while (ch==NONE); |
Branch: 1 branch
| Branch | True | False | Total |
| while (ch==NONE); |
0 |
0 |
0 |
|
| 219 |
0 |
| return ch; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| |
| 223 |
0 |
| void dprint(char *msg) |
| 224 |
|
| { |
| 225 |
|
| FILE *file; |
| 226 |
|
| |
| 227 |
0 |
| return; /* -- remove to output to /tmp/debug.txt */ |
| 228 |
|
| |
| 229 |
|
| file=fopen("/tmp/nilsimsa_debug.txt","a"); |
| 230 |
|
| |
| 231 |
|
| if (file) { |
| 232 |
|
| fprintf(file," %s",msg); |
| 233 |
|
| fclose(file); |
| 234 |
|
| } |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
5 |
| int isbadbuf(unsigned char *buf,int buflen) |
| 238 |
|
| { |
| 239 |
|
| int ch, chcount, bad; |
| 240 |
5 |
| chcount = bad = 0; |
| 241 |
128562 |
| do { ch=*(buf+chcount++); |
| 242 |
|
| /* sprintf(msg,"chcount=%d, ch=%02x:%03d\n",chcount,ch,ch); dprint(msg); */ |
| 243 |
128562 |
▶ | if (ch < 0) bad=1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch < 0) bad=1; |
0 |
128562 |
128562 |
|
| 244 |
128562 |
▶ | if (ch > 255) bad=2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch > 255) bad=2; |
0 |
128562 |
128562 |
|
| 245 |
128562 |
▶ | if (bad) return bad; |
Branch: 1 branch
| Branch | True | False | Total |
| if (bad) return bad; |
0 |
128562 |
128562 |
|
| 246 |
128562 |
▶ | } while (chcount < buflen); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (chcount < buflen); |
128557 |
5 |
128562 |
|
| 247 |
5 |
| return bad; |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
5 |
| int accbuf(unsigned char *buf,int buflen,struct nsrecord *a) |
| 251 |
|
| /* slight modification of accfile, where we pass in buffer instead of FILE |
| 252 |
|
| * returns |
| 253 |
|
| * 0 success |
| 254 |
|
| * -1 error, buflen is 0 or less |
| 255 |
|
| * -2 error, buf contains illegal chars (<0) |
| 256 |
|
| * -3 error, buf contains illegal chars (>255) |
| 257 |
|
| */ |
| 258 |
|
| {unsigned int chcount; |
| 259 |
|
| char msg[512]; |
| 260 |
|
| int ch,lastch[4],hflag; |
| 261 |
5 |
| int illegalchars = 0; |
| 262 |
5 |
| catflag = noheaderflag = 0; |
| 263 |
5 |
| lastch[0]=lastch[1]=lastch[2]=lastch[3]=-1; |
| 264 |
5 |
| hflag=noheaderflag; |
| 265 |
5 |
▶ | if (buflen<1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (buflen<1) |
0 |
5 |
5 |
|
| 266 |
0 |
| return -1; |
| 267 |
5 |
▶ | if (isbadbuf(buf,buflen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (isbadbuf(buf,buflen)) |
0 |
5 |
5 |
|
| 268 |
0 |
| return -2; |
| 269 |
|
| /* sprintf(msg,"bufsize=%d\n",buflen); dprint(msg); */ |
| 270 |
5 |
| chcount=0; |
| 271 |
128562 |
| do {ch=*(buf+chcount); |
| 272 |
|
| /* sprintf(msg,"\nbuflen=%d, chcount=%d, ch=%02x:%03d:%c",buflen,chcount,ch,ch,ch); dprint(msg); */ |
| 273 |
128562 |
▶ | if (!hflag && ch>=0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!hflag && ch>=0) |
128562 |
0 |
128562 |
| if (!hflag && ch>=0) |
128562 |
0 |
128562 |
|
| 274 |
128562 |
| {chcount++; |
| 275 |
128562 |
▶ | if (lastch[1]>=0){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (lastch[1]>=0){ |
128552 |
10 |
128562 |
|
| 276 |
128552 |
| a->acc[tran3(ch,lastch[0],lastch[1],0)]++; |
| 277 |
|
| } |
| 278 |
128562 |
▶ | if (lastch[2]>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lastch[2]>=0) |
128547 |
15 |
128562 |
|
| 279 |
128547 |
| {a->acc[tran3(ch,lastch[0],lastch[2],1)]++; |
| 280 |
128547 |
| a->acc[tran3(ch,lastch[1],lastch[2],2)]++; |
| 281 |
|
| } |
| 282 |
128562 |
▶ | if (lastch[3]>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lastch[3]>=0) |
128542 |
20 |
128562 |
|
| 283 |
128542 |
| {a->acc[tran3(ch,lastch[0],lastch[3],3)]++; |
| 284 |
128542 |
| a->acc[tran3(ch,lastch[1],lastch[3],4)]++; |
| 285 |
128542 |
| a->acc[tran3(ch,lastch[2],lastch[3],5)]++; |
| 286 |
128542 |
| a->acc[tran3(lastch[3],lastch[0],ch,6)]++; |
| 287 |
128542 |
| a->acc[tran3(lastch[3],lastch[2],ch,7)]++; |
| 288 |
|
| } |
| 289 |
|
| } else { |
| 290 |
0 |
▶ | if (ch<0) illegalchars++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch<0) illegalchars++; |
0 |
0 |
0 |
|
| 291 |
0 |
▶ | if (illegalchars > 22) return -2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (illegalchars > 22) return -2; |
0 |
0 |
0 |
|
| 292 |
|
| } |
| 293 |
128562 |
| lastch[3]=lastch[2]; |
| 294 |
128562 |
| lastch[2]=lastch[1]; |
| 295 |
128562 |
| lastch[1]=lastch[0]; |
| 296 |
128562 |
| lastch[0]=ch; |
| 297 |
|
| } |
| 298 |
128562 |
▶ | while ( chcount < buflen ); |
Branch: 1 branch
| Branch | True | False | Total |
| while ( chcount < buflen ); |
128557 |
5 |
128562 |
|
| 299 |
|
| |
| 300 |
|
| /* sprintf(msg,"a-chcount=%d,lastch=%c,lastch2=%c,a->total=%d\n",chcount,ch,lastch[1],a->total); dprint(msg); */ |
| 301 |
5 |
| switch (chcount) |
| 302 |
0 |
| {case 0: ; |
| 303 |
|
| case 1: ; |
| 304 |
0 |
| case 2: break; |
| 305 |
0 |
| case 3: a->total++; |
| 306 |
0 |
| break; |
| 307 |
0 |
| case 4: a->total+=4; |
| 308 |
0 |
| break; |
| 309 |
5 |
| default:a->total+=(8*chcount)-28; |
| 310 |
|
| } |
| 311 |
5 |
| a->threshold=(a->total)/256; /* round down because criterion is >threshold */ |
| 312 |
|
| /* sprintf(msg,"chcount=%d; lastch=%03d,%03d; a->total=%d\n",chcount,lastch[1],ch,a->total); dprint(msg); */ |
| 313 |
5 |
| return chcount; |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
|
| |
| 317 |
0 |
| int accfile(FILE *file,struct nsrecord *a,int mboxflag) |
| 318 |
|
| /* Returns -1 on reaching end of file, -2 on reaching end of message, |
| 319 |
|
| -3 if the file contains no messages. */ |
| 320 |
|
| {unsigned int chcount; |
| 321 |
|
| int ch,lastch[4],hflag; |
| 322 |
0 |
| lastch[0]=lastch[1]=lastch[2]=lastch[3]=-1; |
| 323 |
0 |
| chcount=0; |
| 324 |
0 |
| hflag=noheaderflag; |
| 325 |
0 |
▶ | do {ch=mboxflag?defromulate(file):getc(file); |
Branch: 1 branch
| Branch | True | False | Total |
| do {ch=mboxflag?defromulate(file):getc(file); |
0 |
0 |
0 |
|
| 326 |
0 |
▶ | if (ch>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch>=0) |
0 |
0 |
0 |
|
| 327 |
0 |
▶ | if (hflag) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hflag) |
0 |
0 |
0 |
|
| 328 |
0 |
▶ | if ((lastch[0]=='\n' && lastch[1]=='\n') |
Branch: 2 branches
| Branch | True | False | Total |
| if ((lastch[0]=='\n' && lastch[1]=='\n') |
0 |
0 |
0 |
| if ((lastch[0]=='\n' && lastch[1]=='\n') |
0 |
0 |
0 |
|
| 329 |
0 |
▶ | ||(lastch[0]=='\r' && lastch[1]=='\r') |
Branch: 2 branches
| Branch | True | False | Total |
| ||(lastch[0]=='\r' && lastch[1]=='\r') |
0 |
0 |
0 |
| ||(lastch[0]=='\r' && lastch[1]=='\r') |
0 |
0 |
0 |
|
| 330 |
0 |
▶ | ||(lastch[0]=='\n' && lastch[1]=='\r' && lastch[2]=='\n' && lastch[3]=='\r')) |
Branch: 4 branches
| Branch | True | False | Total |
| ||(lastch[0]=='\n' && lastch[1]=='\r' && lastch[2]=='\n' && lastch[3]=='\r')) |
0 |
0 |
0 |
| ||(lastch[0]=='\n' && lastch[1]=='\r' && lastch[2]=='\n' && lastch[3]=='\r')) |
0 |
0 |
0 |
| ||(lastch[0]=='\n' && lastch[1]=='\r' && lastch[2]=='\n' && lastch[3]=='\r')) |
0 |
0 |
0 |
| ||(lastch[0]=='\n' && lastch[1]=='\r' && lastch[2]=='\n' && lastch[3]=='\r')) |
0 |
0 |
0 |
|
| 331 |
0 |
| {hflag=0; |
| 332 |
0 |
| lastch[0]=lastch[1]=lastch[2]=lastch[3]=-1; |
| 333 |
|
| } |
| 334 |
|
| else |
| 335 |
|
| ; |
| 336 |
|
| else |
| 337 |
|
| ; |
| 338 |
0 |
▶ | if (!hflag && ch>=0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!hflag && ch>=0) |
0 |
0 |
0 |
| if (!hflag && ch>=0) |
0 |
0 |
0 |
|
| 339 |
0 |
| {chcount++; |
| 340 |
0 |
▶ | if (catflag) |
Branch: 1 branch
| Branch | True | False | Total |
| if (catflag) |
0 |
0 |
0 |
|
| 341 |
0 |
| putchar(ch); |
| 342 |
0 |
▶ | if (lastch[1]>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lastch[1]>=0) |
0 |
0 |
0 |
|
| 343 |
0 |
| a->acc[tran3(ch,lastch[0],lastch[1],0)]++; |
| 344 |
0 |
▶ | if (lastch[2]>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lastch[2]>=0) |
0 |
0 |
0 |
|
| 345 |
0 |
| {a->acc[tran3(ch,lastch[0],lastch[2],1)]++; |
| 346 |
0 |
| a->acc[tran3(ch,lastch[1],lastch[2],2)]++; |
| 347 |
|
| } |
| 348 |
0 |
▶ | if (lastch[3]>=0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lastch[3]>=0) |
0 |
0 |
0 |
|
| 349 |
0 |
| {a->acc[tran3(ch,lastch[0],lastch[3],3)]++; |
| 350 |
0 |
| a->acc[tran3(ch,lastch[1],lastch[3],4)]++; |
| 351 |
0 |
| a->acc[tran3(ch,lastch[2],lastch[3],5)]++; |
| 352 |
0 |
| a->acc[tran3(lastch[3],lastch[0],ch,6)]++; |
| 353 |
0 |
| a->acc[tran3(lastch[3],lastch[2],ch,7)]++; |
| 354 |
|
| } |
| 355 |
|
| } |
| 356 |
0 |
| lastch[3]=lastch[2]; |
| 357 |
0 |
| lastch[2]=lastch[1]; |
| 358 |
0 |
| lastch[1]=lastch[0]; |
| 359 |
0 |
| lastch[0]=ch; |
| 360 |
|
| } |
| 361 |
0 |
▶ | while (ch>=0); |
Branch: 1 branch
| Branch | True | False | Total |
| while (ch>=0); |
0 |
0 |
0 |
|
| 362 |
0 |
| switch (chcount) |
| 363 |
0 |
| {case 0: ; |
| 364 |
|
| case 1: ; |
| 365 |
0 |
| case 2: break; |
| 366 |
0 |
| case 3: a->total++; |
| 367 |
0 |
| break; |
| 368 |
0 |
| case 4: a->total+=4; |
| 369 |
0 |
| break; |
| 370 |
0 |
| default:a->total+=(8*chcount)-28; |
| 371 |
|
| } |
| 372 |
0 |
| a->threshold=(a->total)/256; /* round down because criterion is >threshold */ |
| 373 |
0 |
| return ch; |
| 374 |
|
| } |
| 375 |
|
| |
| 376 |
5 |
| void makecode(struct nsrecord *a) |
| 377 |
|
| {int i; |
| 378 |
5 |
| memset(a->code,0,32); |
| 379 |
1285 |
▶ | for (i=0;i<256;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<256;i++) |
1280 |
5 |
1285 |
|
| 380 |
1280 |
| {a->code[i>>3]+=((a->acc[i]>a->threshold)<<(i&7)); |
| 381 |
|
| } |
| 382 |
5 |
| } |
| 383 |
|
| |
| 384 |
0 |
| int nilsimsa(struct nsrecord *a,struct nsrecord *b) |
| 385 |
|
| {int i,bits; |
| 386 |
0 |
▶ | for (i=bits=0;i<32;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=bits=0;i<32;i++) |
0 |
0 |
0 |
|
| 387 |
0 |
| bits+=popcount[255&(a->code[i]^b->code[i])]; |
| 388 |
0 |
| return 128-bits; |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
5 |
| void codetostr(struct nsrecord *a,char *str) |
| 392 |
|
| {int i; |
| 393 |
165 |
▶ | for (i=0;i<32;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<32;i++) |
160 |
5 |
165 |
|
| 394 |
160 |
| sprintf(str+i+i,"%02x",0xff&(a->code[31-i])); |
| 395 |
5 |
| } |
| 396 |
|
| |
| 397 |
0 |
| int strtocode(char *str,struct nsrecord *a) |
| 398 |
|
| /* Returns 0 if error, or 1 if valid code. */ |
| 399 |
|
| {unsigned int i,len,valid,byte; |
| 400 |
0 |
| len=strlen(str); |
| 401 |
0 |
▶ | valid=(len>63) && (isxdigit(*str)); |
Branch: 2 branches
| Branch | True | False | Total |
| valid=(len>63) && (isxdigit(*str)); |
0 |
0 |
0 |
| valid=(len>63) && (isxdigit(*str)); |
0 |
0 |
0 |
|
| 402 |
0 |
| a->total=0; |
| 403 |
0 |
▶ | if (len&1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len&1) |
0 |
0 |
0 |
|
| 404 |
0 |
| str++; |
| 405 |
0 |
▶ | for (;*str;str+=2) |
Branch: 1 branch
| Branch | True | False | Total |
| for (;*str;str+=2) |
0 |
0 |
0 |
|
| 406 |
0 |
| {memmove(a->code+1,a->code,31); |
| 407 |
0 |
▶ | if (!isxdigit(str[0]) || !isxdigit(str[1])) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!isxdigit(str[0]) || !isxdigit(str[1])) |
0 |
0 |
0 |
| if (!isxdigit(str[0]) || !isxdigit(str[1])) |
0 |
0 |
0 |
|
| 408 |
0 |
| valid=0; |
| 409 |
0 |
| sscanf(str,"%2x",&byte); |
| 410 |
0 |
| a->code[0]=byte; |
| 411 |
0 |
| memmove(a->acc+8,a->acc,248*sizeof(int)); |
| 412 |
0 |
▶ | for (i=0;i<8;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<8;i++) |
0 |
0 |
0 |
|
| 413 |
0 |
| a->acc[i]=1&(byte>>i); |
| 414 |
|
| } |
| 415 |
0 |
▶ | if (!valid) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!valid) |
0 |
0 |
0 |
|
| 416 |
0 |
| clear(a); |
| 417 |
0 |
▶ | for (i=0;i<256;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<256;i++) |
0 |
0 |
0 |
|
| 418 |
0 |
| a->total+=a->acc[i]; |
| 419 |
0 |
| a->threshold=0; |
| 420 |
0 |
| return valid; |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
0 |
| int codeorfile(struct nsrecord *a,char *str,int mboxflag) |
| 424 |
|
| /* Attempts to read the file named str. If that fails, |
| 425 |
|
| attempts to convert str to a code. Returns 2 if directory, 1 if success, |
| 426 |
|
| 0 if failure, -1 if mboxflag is set and there are more messages, |
| 427 |
|
| -2 if mbox file is empty (failure again, but different error message). */ |
| 428 |
|
| {static FILE *file; |
| 429 |
|
| struct stat statbuf; |
| 430 |
|
| static int msgnum=0; |
| 431 |
|
| int ret; |
| 432 |
0 |
▶ | if (!strcmp(str,"-")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!strcmp(str,"-")) |
0 |
0 |
0 |
|
| 433 |
0 |
| {ret=accfile(stdin,a,mboxflag); |
| 434 |
0 |
| file=stdin; |
| 435 |
0 |
| a->name=""; |
| 436 |
0 |
▶ | if (mboxflag) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mboxflag) |
0 |
0 |
0 |
|
| 437 |
0 |
| {a->name=malloc(24); |
| 438 |
0 |
| sprintf(a->name,"#%u",msgnum); |
| 439 |
0 |
| a->name=realloc(a->name,strlen(a->name)+1); |
| 440 |
|
| } |
| 441 |
0 |
| a->flag=FILECODE; |
| 442 |
0 |
| msgnum++; |
| 443 |
0 |
▶ | if (ret!=-2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret!=-2) |
0 |
0 |
0 |
|
| 444 |
0 |
| msgnum=0; |
| 445 |
|
| } |
| 446 |
|
| else |
| 447 |
0 |
| {ret=stat(str,&statbuf); |
| 448 |
0 |
▶ | if (ret==0 && (statbuf.st_mode&S_IFMT)==S_IFDIR) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret==0 && (statbuf.st_mode&S_IFMT)==S_IFDIR) |
0 |
0 |
0 |
| if (ret==0 && (statbuf.st_mode&S_IFMT)==S_IFDIR) |
0 |
0 |
0 |
|
| 449 |
0 |
| return 2; |
| 450 |
0 |
▶ | if (msgnum==0 || !mboxflag) |
Branch: 2 branches
| Branch | True | False | Total |
| if (msgnum==0 || !mboxflag) |
0 |
0 |
0 |
| if (msgnum==0 || !mboxflag) |
0 |
0 |
0 |
|
| 451 |
0 |
| file=fopen(str,"rb"); |
| 452 |
0 |
| a->name=str; |
| 453 |
0 |
▶ | if (file) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file) |
0 |
0 |
0 |
|
| 454 |
0 |
| {ret=accfile(file,a,mboxflag); |
| 455 |
0 |
| a->flag=FILECODE; |
| 456 |
0 |
▶ | if (mboxflag) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mboxflag) |
0 |
0 |
0 |
|
| 457 |
0 |
| {a->name=malloc(strlen(str)+24); |
| 458 |
0 |
| sprintf(a->name,"%s#%u",str,msgnum); |
| 459 |
0 |
| a->name=realloc(a->name,strlen(a->name)+1); |
| 460 |
|
| } |
| 461 |
|
| else |
| 462 |
0 |
| a->name=strdup(str); |
| 463 |
0 |
| msgnum++; |
| 464 |
0 |
▶ | if (ret!=-2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret!=-2) |
0 |
0 |
0 |
|
| 465 |
0 |
| {fclose(file); |
| 466 |
0 |
| msgnum=0; |
| 467 |
|
| } |
| 468 |
|
| } |
| 469 |
|
| else |
| 470 |
0 |
| {ret=strtocode(str,a); |
| 471 |
0 |
▶ | if (ret) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret) |
0 |
0 |
0 |
|
| 472 |
0 |
| a->flag=LITERALCODE; |
| 473 |
0 |
| return ret; |
| 474 |
|
| } |
| 475 |
|
| } |
| 476 |
0 |
| makecode(a); |
| 477 |
0 |
▶ | if (ret==-3) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret==-3) |
0 |
0 |
0 |
|
| 478 |
0 |
| a->flag=INVALID; |
| 479 |
0 |
| ret+=!++ret; |
| 480 |
0 |
| return ret; |
| 481 |
|
| } |
| 482 |
|
| |
| 483 |
0 |
| void aggregate(int n) |
| 484 |
|
| /* Aggregate the first n codes in selkarbi into gunma. */ |
| 485 |
|
| {int i,j; |
| 486 |
0 |
| clear(&gunma); |
| 487 |
0 |
▶ | for (i=0;i<n;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<n;i++) |
0 |
0 |
0 |
|
| 488 |
0 |
| {gunma.total+=selkarbi[i].total; |
| 489 |
0 |
▶ | for (j=0;j<256;j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0;j<256;j++) |
0 |
0 |
0 |
|
| 490 |
0 |
| gunma.acc[j]+=selkarbi[i].acc[j]; |
| 491 |
|
| } |
| 492 |
0 |
| gunma.threshold=gunma.total/256; |
| 493 |
0 |
| makecode(&gunma); |
| 494 |
0 |
| } |
| 495 |
|
| |
| 496 |
0 |
| void dump1code(struct nsrecord *code) |
| 497 |
|
| {char str[65]; |
| 498 |
0 |
| codetostr(code,str); |
| 499 |
0 |
| printf("%s %4d %c %d \n",str,code->nilsmi,"ILFAD"[code->flag],code->nilsmi); |
| 500 |
0 |
| } |
| 501 |
|
| |
| 502 |
0 |
| void dumpcodes(struct nsrecord *code,int n) |
| 503 |
|
| {int i; |
| 504 |
0 |
▶ | for (i=0;i<n;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0;i<n;i++) |
0 |
0 |
0 |
|
| 505 |
0 |
| dump1code(code+i); |
| 506 |
0 |
| } |
| 507 |
|
| |
| 508 |
0 |
| void version() |
| 509 |
0 |
| {printf("nilsimsa %s \n© 2001-2002 cmeclax\nDistributed under GNU GPL\n",VERSION); |
| 510 |
0 |
| } |
| 511 |
|
| |
| 512 |
0 |
| void help() |
| 513 |
0 |
| {puts("\ |
| 514 |
|
| nilsimsa [options] [files]\n\ |
| 515 |
|
| --help\n\ |
| 516 |
|
| Prints this message.\n\ |
| 517 |
|
| --version\n\ |
| 518 |
|
| Prints the version number.\n\ |
| 519 |
|
| file1 file2 ...\n\ |
| 520 |
|
| Compute the nilsimsa codes of the files.\n\ |
| 521 |
|
| -r --recursive\n\ |
| 522 |
|
| Process all files inside listed directories.\n\ |
| 523 |
|
| -c code file1 file2 --compare-to\n\ |
| 524 |
|
| Compare files to code.\n\ |
| 525 |
|
| -c code -t 48 file --threshold\n\ |
| 526 |
|
| Return true if file is more than 48 bits similar to code.\n\ |
| 527 |
|
| -s --skip-headers\n\ |
| 528 |
|
| file is a mail message; ignore the headers.\n\ |
| 529 |
|
| --mbox\n\ |
| 530 |
|
| files are mailbox folders; take the code of each message.\n\ |
| 531 |
|
| -a file1 file2 ... --aggregate\n\ |
| 532 |
|
| Compute the aggregate nilsimsa code of the files.\n\ |
| 533 |
|
| -C clusterlist file1 file2 ... --find-clusters\n\ |
| 534 |
|
| Find a cluster of similar files.\n\ |
| 535 |
|
| -H clusterlist file1 file2 ... --check-clusters\n\ |
| 536 |
|
| Compare files to all priority 0 clusters in clusterlist,\n\ |
| 537 |
|
| then all priority 1 clusters. If the first match is an\n\ |
| 538 |
|
| accept cluster, return 0. If it is a deny cluster, return\n\ |
| 539 |
|
| 1. If there is no match, return 0.\n\ |
| 540 |
|
| -m n --min-cluster-size\n\ |
| 541 |
|
| Set the minimum cluster size (default 3).\n\ |
| 542 |
|
| -v --verbose\n\ |
| 543 |
|
| In -C, lists all the codes, the ones which are in the\n\ |
| 544 |
|
| cluster first; in -H, lists which cluster matches.\n\ |
| 545 |
|
| -x --exhaustive\n\ |
| 546 |
|
| In -C, compare all pairs of codes, not just n*log(n) pairs.\n\ |
| 547 |
|
| \n\ |
| 548 |
|
| Any time a file is called for, a code may be given, and vice versa,\n\ |
| 549 |
|
| except after -C and -H."); |
| 550 |
0 |
| } |
| 551 |
|
| |
| 552 |
|
| |