cannam@128
|
1 A Fast Method for Identifying Plain Text Files
|
cannam@128
|
2 ==============================================
|
cannam@128
|
3
|
cannam@128
|
4
|
cannam@128
|
5 Introduction
|
cannam@128
|
6 ------------
|
cannam@128
|
7
|
cannam@128
|
8 Given a file coming from an unknown source, it is sometimes desirable
|
cannam@128
|
9 to find out whether the format of that file is plain text. Although
|
cannam@128
|
10 this may appear like a simple task, a fully accurate detection of the
|
cannam@128
|
11 file type requires heavy-duty semantic analysis on the file contents.
|
cannam@128
|
12 It is, however, possible to obtain satisfactory results by employing
|
cannam@128
|
13 various heuristics.
|
cannam@128
|
14
|
cannam@128
|
15 Previous versions of PKZip and other zip-compatible compression tools
|
cannam@128
|
16 were using a crude detection scheme: if more than 80% (4/5) of the bytes
|
cannam@128
|
17 found in a certain buffer are within the range [7..127], the file is
|
cannam@128
|
18 labeled as plain text, otherwise it is labeled as binary. A prominent
|
cannam@128
|
19 limitation of this scheme is the restriction to Latin-based alphabets.
|
cannam@128
|
20 Other alphabets, like Greek, Cyrillic or Asian, make extensive use of
|
cannam@128
|
21 the bytes within the range [128..255], and texts using these alphabets
|
cannam@128
|
22 are most often misidentified by this scheme; in other words, the rate
|
cannam@128
|
23 of false negatives is sometimes too high, which means that the recall
|
cannam@128
|
24 is low. Another weakness of this scheme is a reduced precision, due to
|
cannam@128
|
25 the false positives that may occur when binary files containing large
|
cannam@128
|
26 amounts of textual characters are misidentified as plain text.
|
cannam@128
|
27
|
cannam@128
|
28 In this article we propose a new, simple detection scheme that features
|
cannam@128
|
29 a much increased precision and a near-100% recall. This scheme is
|
cannam@128
|
30 designed to work on ASCII, Unicode and other ASCII-derived alphabets,
|
cannam@128
|
31 and it handles single-byte encodings (ISO-8859, MacRoman, KOI8, etc.)
|
cannam@128
|
32 and variable-sized encodings (ISO-2022, UTF-8, etc.). Wider encodings
|
cannam@128
|
33 (UCS-2/UTF-16 and UCS-4/UTF-32) are not handled, however.
|
cannam@128
|
34
|
cannam@128
|
35
|
cannam@128
|
36 The Algorithm
|
cannam@128
|
37 -------------
|
cannam@128
|
38
|
cannam@128
|
39 The algorithm works by dividing the set of bytecodes [0..255] into three
|
cannam@128
|
40 categories:
|
cannam@128
|
41 - The white list of textual bytecodes:
|
cannam@128
|
42 9 (TAB), 10 (LF), 13 (CR), 32 (SPACE) to 255.
|
cannam@128
|
43 - The gray list of tolerated bytecodes:
|
cannam@128
|
44 7 (BEL), 8 (BS), 11 (VT), 12 (FF), 26 (SUB), 27 (ESC).
|
cannam@128
|
45 - The black list of undesired, non-textual bytecodes:
|
cannam@128
|
46 0 (NUL) to 6, 14 to 31.
|
cannam@128
|
47
|
cannam@128
|
48 If a file contains at least one byte that belongs to the white list and
|
cannam@128
|
49 no byte that belongs to the black list, then the file is categorized as
|
cannam@128
|
50 plain text; otherwise, it is categorized as binary. (The boundary case,
|
cannam@128
|
51 when the file is empty, automatically falls into the latter category.)
|
cannam@128
|
52
|
cannam@128
|
53
|
cannam@128
|
54 Rationale
|
cannam@128
|
55 ---------
|
cannam@128
|
56
|
cannam@128
|
57 The idea behind this algorithm relies on two observations.
|
cannam@128
|
58
|
cannam@128
|
59 The first observation is that, although the full range of 7-bit codes
|
cannam@128
|
60 [0..127] is properly specified by the ASCII standard, most control
|
cannam@128
|
61 characters in the range [0..31] are not used in practice. The only
|
cannam@128
|
62 widely-used, almost universally-portable control codes are 9 (TAB),
|
cannam@128
|
63 10 (LF) and 13 (CR). There are a few more control codes that are
|
cannam@128
|
64 recognized on a reduced range of platforms and text viewers/editors:
|
cannam@128
|
65 7 (BEL), 8 (BS), 11 (VT), 12 (FF), 26 (SUB) and 27 (ESC); but these
|
cannam@128
|
66 codes are rarely (if ever) used alone, without being accompanied by
|
cannam@128
|
67 some printable text. Even the newer, portable text formats such as
|
cannam@128
|
68 XML avoid using control characters outside the list mentioned here.
|
cannam@128
|
69
|
cannam@128
|
70 The second observation is that most of the binary files tend to contain
|
cannam@128
|
71 control characters, especially 0 (NUL). Even though the older text
|
cannam@128
|
72 detection schemes observe the presence of non-ASCII codes from the range
|
cannam@128
|
73 [128..255], the precision rarely has to suffer if this upper range is
|
cannam@128
|
74 labeled as textual, because the files that are genuinely binary tend to
|
cannam@128
|
75 contain both control characters and codes from the upper range. On the
|
cannam@128
|
76 other hand, the upper range needs to be labeled as textual, because it
|
cannam@128
|
77 is used by virtually all ASCII extensions. In particular, this range is
|
cannam@128
|
78 used for encoding non-Latin scripts.
|
cannam@128
|
79
|
cannam@128
|
80 Since there is no counting involved, other than simply observing the
|
cannam@128
|
81 presence or the absence of some byte values, the algorithm produces
|
cannam@128
|
82 consistent results, regardless what alphabet encoding is being used.
|
cannam@128
|
83 (If counting were involved, it could be possible to obtain different
|
cannam@128
|
84 results on a text encoded, say, using ISO-8859-16 versus UTF-8.)
|
cannam@128
|
85
|
cannam@128
|
86 There is an extra category of plain text files that are "polluted" with
|
cannam@128
|
87 one or more black-listed codes, either by mistake or by peculiar design
|
cannam@128
|
88 considerations. In such cases, a scheme that tolerates a small fraction
|
cannam@128
|
89 of black-listed codes would provide an increased recall (i.e. more true
|
cannam@128
|
90 positives). This, however, incurs a reduced precision overall, since
|
cannam@128
|
91 false positives are more likely to appear in binary files that contain
|
cannam@128
|
92 large chunks of textual data. Furthermore, "polluted" plain text should
|
cannam@128
|
93 be regarded as binary by general-purpose text detection schemes, because
|
cannam@128
|
94 general-purpose text processing algorithms might not be applicable.
|
cannam@128
|
95 Under this premise, it is safe to say that our detection method provides
|
cannam@128
|
96 a near-100% recall.
|
cannam@128
|
97
|
cannam@128
|
98 Experiments have been run on many files coming from various platforms
|
cannam@128
|
99 and applications. We tried plain text files, system logs, source code,
|
cannam@128
|
100 formatted office documents, compiled object code, etc. The results
|
cannam@128
|
101 confirm the optimistic assumptions about the capabilities of this
|
cannam@128
|
102 algorithm.
|
cannam@128
|
103
|
cannam@128
|
104
|
cannam@128
|
105 --
|
cannam@128
|
106 Cosmin Truta
|
cannam@128
|
107 Last updated: 2006-May-28
|