annotate src/zlib-1.2.8/doc/txtvsbin.txt @ 65:a69c1527268d

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