-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathisBinFile.ahk
82 lines (73 loc) · 2.61 KB
/
isBinFile.ahk
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
isBinFile(Filename,NumBytes=32,Minimum=4,complexunicode=1) {
file:=FileOpen(Filename,"r")
file.Position:=0 ;force position to 0 (zero)
nbytes:=file.RawRead(rawbytes,NumBytes) ;read bytes
file.Close() ;close file
if (nbytes < Minimum) ;recommended 4 minimum for unicode detection
return 0 ;asume text file, if too short
t:=0, i:=0, bytes:=[] ;Initialize vars
loop % nbytes ;create c-style bytes array
bytes[(A_Index-1)]:=Numget(&rawbytes,(A_Index-1),"UChar")
;determine BOM if possible/existant
if (bytes[0]=0xFE && bytes[1]=0xFF)
|| (bytes[0]=0xFF && bytes[1]=0xFE)
return 0 ;text Utf-16 BE/LE file
if (bytes[0]=0xEF && bytes[1]=0xBB && bytes[2]=0xBF)
return 0 ;text Utf-8 file
if (bytes[0]=0x00 && bytes[1]=0x00
&& bytes[2]=0xFE && bytes[3]=0xFF)
|| (bytes[0]=0xFF && bytes[1]=0xFE
&& bytes[2]=0x00 && bytes[3]=0x00)
return 0 ;text Utf-32 BE/LE file
while(i<nbytes) {
;// ASCII
if( bytes[i] == 0x09 || bytes[i] == 0x0A || bytes[i] == 0x0D
|| (0x20 <= bytes[i] && bytes[i] <= 0x7E) ) {
i += 1
continue
}
;// non-overlong 2-byte
if( (0xC2 <= bytes[i] && bytes[i] <= 0xDF)
&& (0x80 <= bytes[i+1] && bytes[i+1] <= 0xBF) ) {
i += 2
continue
}
;// excluding overlongs, straight 3-byte, excluding surrogates
if( ( bytes[i] == 0xE0 && (0xA0 <= bytes[i+1] && bytes[i+1] <= 0xBF)
&& (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF) )
|| ( ((0xE1 <= bytes[i] && bytes[i] <= 0xEC)
|| bytes[i] == 0xEE || bytes[i] == 0xEF)
&& (0x80 <= bytes[i+1] && bytes[i+1] <= 0xBF)
&& (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF) )
|| ( bytes[i] == 0xED && (0x80 <= bytes[i+1] && bytes[i+1] <= 0x9F)
&& (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF) ) ) {
i += 3
continue
}
;// planes 1-3, planes 4-15, plane 16
if( ( bytes[i] == 0xF0 && (0x90 <= bytes[i+1] && bytes[i+1] <= 0xBF)
&& (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF)
&& (0x80 <= bytes[i+3] && bytes[i+3] <= 0xBF) )
|| ( (0xF1 <= bytes[i] && bytes[i] <= 0xF3)
&& (0x80 <= bytes[i+1] && bytes[i+1] <= 0xBF)
&& (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF)
&& (0x80 <= bytes[i+3] && bytes[i+3] <= 0xBF) )
|| ( bytes[i] == 0xF4 && (0x80 <= bytes[i+1] && bytes[i+1] <= 0x8F)
&& (0x80 <= bytes[i+2] && bytes[i+2] <= 0xBF)
&& (0x80 <= bytes[i+3] && bytes[i+3] <= 0xBF) ) ) {
i += 4
continue
}
t:=1
break
}
if (t=0) ;the while-loop has no fails, then confirmed utf-8
return 0
;else do nothing and check again with the classic method below
loop, %nbytes% {
if (bytes[(A_Index-1)]<9) or (bytes[(A_Index-1)]>126)
or ((bytes[(A_Index-1)]<32) and (bytes[(A_Index-1)]>13))
return 1
}
return 0
}