├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples └── inspect.rs ├── src └── lib.rs └── testdata ├── create_text_files.py ├── file_sources.md ├── test.jpg ├── test.pdf ├── test.png ├── text_UTF-16BE-BOM.txt ├── text_UTF-16LE-BOM.txt ├── text_UTF-32BE-BOM.txt ├── text_UTF-32LE-BOM.txt ├── text_UTF-8-BOM.txt └── text_UTF-8.txt /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target 3 | **/*.rs.bk 4 | Cargo.lock 5 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "content_inspector" 3 | description = "Fast inspection of binary buffers to guess/determine the encoding" 4 | categories = ["encoding"] 5 | homepage = "https://github.com/sharkdp/content_inspector" 6 | repository = "https://github.com/sharkdp/content_inspector" 7 | keywords = [ 8 | "unicode", 9 | "encoding", 10 | "binary", 11 | "text", 12 | "library", 13 | ] 14 | license = "MIT/Apache-2.0" 15 | version = "0.2.4" 16 | readme = "README.md" 17 | authors = ["David Peter "] 18 | 19 | [dependencies] 20 | memchr = "2" 21 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining a copy 2 | of this software and associated documentation files (the "Software"), to deal 3 | in the Software without restriction, including without limitation the rights 4 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 5 | copies of the Software, and to permit persons to whom the Software is 6 | furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all 9 | copies or substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 17 | SOFTWARE. 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # content_inspector 2 | 3 | [![Crates.io](https://img.shields.io/crates/v/content_inspector.svg)](https://crates.io/crates/content_inspector) 4 | [![Documentation](https://docs.rs/content_inspector/badge.svg)](https://docs.rs/content_inspector) 5 | 6 | A simple library for *fast* inspection of binary buffers to guess the type of content. 7 | 8 | This is mainly intended to quickly determine whether a given buffer contains "binary" 9 | or "text" data. Programs like `grep` or `git diff` use similar mechanisms to decide whether 10 | to treat some files as "binary data" or not. 11 | 12 | The analysis is based on a very simple heuristic: Searching for NULL bytes 13 | (indicating "binary" content) and the detection of special [byte order 14 | marks](https://en.wikipedia.org/wiki/Byte_order_mark) (indicating a particular kind of textual 15 | encoding). Note that **this analysis can fail**. For example, even if unlikely, UTF-8-encoded 16 | text can legally contain NULL bytes. Conversely, some particular binary formats (like binary 17 | [PGM](https://en.wikipedia.org/wiki/Netpbm_format)) may not contain NULL bytes. Also, for 18 | performance reasons, only the first 1024 bytes are checked for the NULL-byte (if no BOM was 19 | detected). 20 | 21 | If this library reports a certain type of encoding (say `UTF_16LE`), there is **no guarantee** that 22 | the binary buffer can actually be decoded as UTF-16LE. 23 | 24 | ## Usage 25 | 26 | ```rust 27 | use content_inspector::{ContentType, inspect}; 28 | 29 | assert_eq!(ContentType::UTF_8, inspect(b"Hello")); 30 | assert_eq!(ContentType::BINARY, inspect(b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00")); 31 | 32 | assert!(inspect(b"Hello").is_text()); 33 | ``` 34 | 35 | ## CLI example 36 | 37 | This crate also comes with a small example command-line program (see [`examples/inspect.rs`](examples/inspect.rs)) that demonstrates the usage: 38 | ```bash 39 | > inspect 40 | USAGE: inspect FILE [FILE...] 41 | 42 | > inspect testdata/* 43 | testdata/create_text_files.py: UTF-8 44 | testdata/file_sources.md: UTF-8 45 | testdata/test.jpg: binary 46 | testdata/test.pdf: binary 47 | testdata/test.png: binary 48 | testdata/text_UTF-16BE-BOM.txt: UTF-16BE 49 | testdata/text_UTF-16LE-BOM.txt: UTF-16LE 50 | testdata/text_UTF-32BE-BOM.txt: UTF-32BE 51 | testdata/text_UTF-32LE-BOM.txt: UTF-32LE 52 | testdata/text_UTF-8-BOM.txt: UTF-8-BOM 53 | testdata/text_UTF-8.txt: UTF-8 54 | ``` 55 | 56 | If you only want to detect whether something is a binary or text file, this is about a factor of 250 faster than `file --mime ...`. 57 | 58 | ## License 59 | 60 | Licensed under either of 61 | 62 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 63 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 64 | 65 | at your option. 66 | -------------------------------------------------------------------------------- /examples/inspect.rs: -------------------------------------------------------------------------------- 1 | extern crate content_inspector; 2 | 3 | use std::env; 4 | use std::fs::File; 5 | use std::io::{Error, Read}; 6 | use std::path::Path; 7 | use std::process::exit; 8 | 9 | const MAX_PEEK_SIZE: usize = 1024; 10 | 11 | fn main() -> Result<(), Error> { 12 | let mut args = env::args(); 13 | 14 | if args.len() < 2 { 15 | eprintln!("USAGE: inspect FILE [FILE...]"); 16 | exit(1); 17 | } 18 | 19 | args.next(); 20 | 21 | for filename in args { 22 | if !Path::new(&filename).is_file() { 23 | continue; 24 | } 25 | 26 | let file = File::open(&filename)?; 27 | let mut buffer: Vec = vec![]; 28 | 29 | file.take(MAX_PEEK_SIZE as u64).read_to_end(&mut buffer)?; 30 | 31 | let content_type = content_inspector::inspect(&buffer); 32 | println!("{}: {}", filename, content_type); 33 | } 34 | 35 | Ok(()) 36 | } 37 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A simple library for *fast* inspection of binary buffers to guess the type of content. 2 | //! 3 | //! This is mainly intended to quickly determine whether a given buffer contains "binary" 4 | //! or "text" data. Programs like `grep` or `git diff` use similar mechanisms to decide whether 5 | //! to treat some files as "binary data" or not. 6 | //! 7 | //! The analysis is based on a very simple heuristic: Searching for NULL bytes 8 | //! (indicating "binary" content) and the detection of special [byte order 9 | //! marks](https://en.wikipedia.org/wiki/Byte_order_mark) (indicating a particular kind of textual 10 | //! encoding). Note that **this analysis can fail**. For example, even if unlikely, UTF-8-encoded 11 | //! text can legally contain NULL bytes. Conversely, some particular binary formats (like binary 12 | //! [PGM](https://en.wikipedia.org/wiki/Netpbm_format)) may not contain NULL bytes. Also, for 13 | //! performance reasons, only the first 1024 bytes are checked for the NULL-byte (if no BOM was 14 | //! detected). 15 | //! 16 | //! If this library reports a certain type of encoding (say `UTF_16LE`), there is **no guarantee** 17 | //! that the binary buffer can *actually* be decoded as UTF-16LE. 18 | //! 19 | //! # Example 20 | //! ``` 21 | //! use content_inspector::{ContentType, inspect}; 22 | //! 23 | //! assert_eq!(ContentType::UTF_8, inspect(b"Hello")); 24 | //! assert_eq!(ContentType::BINARY, inspect(b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00")); 25 | //! 26 | //! assert!(inspect(b"Hello").is_text()); 27 | //! ``` 28 | 29 | extern crate memchr; 30 | 31 | use memchr::memchr; 32 | use std::cmp::min; 33 | use std::fmt; 34 | 35 | const MAX_SCAN_SIZE: usize = 1024; 36 | 37 | /// The type of encoding that was detected (for "text" data) or `BINARY` for "binary" data. 38 | #[allow(non_camel_case_types)] 39 | #[derive(Copy, Clone, Debug, PartialEq)] 40 | pub enum ContentType { 41 | /// "binary" data 42 | BINARY, 43 | 44 | /// UTF-8 encoded "text" data 45 | UTF_8, 46 | 47 | /// UTF-8 encoded "text" data with a byte order mark. 48 | UTF_8_BOM, 49 | 50 | /// UTF-16 encoded "text" data (little endian) 51 | UTF_16LE, 52 | 53 | /// UTF-16 encoded "text" data (big endian) 54 | UTF_16BE, 55 | 56 | /// UTF-32 encoded "text" data (little endian) 57 | UTF_32LE, 58 | 59 | /// UTF-32 encoded "text" data (big endian) 60 | UTF_32BE, 61 | } 62 | 63 | impl ContentType { 64 | /// Returns `true`, if the `ContentType` is `BINARY`. 65 | pub fn is_binary(self) -> bool { 66 | self == ContentType::BINARY 67 | } 68 | 69 | /// Returns `true`, if the `ContentType` is __not__ `BINARY`. 70 | pub fn is_text(self) -> bool { 71 | !self.is_binary() 72 | } 73 | } 74 | 75 | impl fmt::Display for ContentType { 76 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 77 | use ContentType::*; 78 | 79 | let name: &str = match *self { 80 | BINARY => "binary", 81 | UTF_8 => "UTF-8", 82 | UTF_8_BOM => "UTF-8-BOM", 83 | UTF_16LE => "UTF-16LE", 84 | UTF_16BE => "UTF-16BE", 85 | UTF_32LE => "UTF-32LE", 86 | UTF_32BE => "UTF-32BE", 87 | }; 88 | write!(f, "{}", name) 89 | } 90 | } 91 | 92 | /// Common byte order marks 93 | /// (see https://en.wikipedia.org/wiki/Byte_order_mark) 94 | static BYTE_ORDER_MARKS: &[(&[u8], ContentType)] = &[ 95 | (&[0xEF, 0xBB, 0xBF], ContentType::UTF_8_BOM), 96 | // UTF-32 needs to be checked before UTF-16 (overlapping BOMs) 97 | (&[0x00, 0x00, 0xFE, 0xFF], ContentType::UTF_32BE), 98 | (&[0xFF, 0xFE, 0x00, 0x00], ContentType::UTF_32LE), 99 | (&[0xFE, 0xFF], ContentType::UTF_16BE), 100 | (&[0xFF, 0xFE], ContentType::UTF_16LE), 101 | ]; 102 | 103 | /// Magic numbers for some filetypes that could otherwise be characterized as text. 104 | static MAGIC_NUMBERS: [&[u8]; 2] = [b"%PDF", b"\x89PNG"]; 105 | 106 | /// Try to determine the type of content in the given buffer. See the crate documentation for a 107 | /// usage example and for more details on how this analysis is performed. 108 | /// 109 | /// If the buffer is empty, the content type will be reported as `UTF_8`. 110 | pub fn inspect(buffer: &[u8]) -> ContentType { 111 | use ContentType::*; 112 | 113 | for &(bom, content_type) in BYTE_ORDER_MARKS { 114 | if buffer.starts_with(bom) { 115 | return content_type; 116 | } 117 | } 118 | 119 | // Scan the first few bytes for zero-bytes 120 | let scan_size = min(buffer.len(), MAX_SCAN_SIZE); 121 | let has_zero_bytes = memchr(0x00, &buffer[..scan_size]).is_some(); 122 | 123 | if has_zero_bytes { 124 | return BINARY; 125 | } 126 | 127 | if MAGIC_NUMBERS.iter().any(|magic| buffer.starts_with(magic)) { 128 | return BINARY; 129 | } 130 | 131 | UTF_8 132 | } 133 | 134 | #[cfg(test)] 135 | mod tests { 136 | use {inspect, ContentType::*}; 137 | 138 | #[test] 139 | fn test_empty_buffer_utf_8() { 140 | assert_eq!(UTF_8, inspect(b"")); 141 | } 142 | 143 | #[test] 144 | fn test_text_simple() { 145 | assert_eq!(UTF_8, inspect("Simple UTF-8 string ☔".as_bytes())); 146 | } 147 | 148 | #[test] 149 | fn test_text_utf8() { 150 | assert_eq!(UTF_8, inspect(include_bytes!("../testdata/text_UTF-8.txt"))); 151 | } 152 | 153 | #[test] 154 | fn test_text_utf8_bom() { 155 | assert_eq!( 156 | UTF_8_BOM, 157 | inspect(include_bytes!("../testdata/text_UTF-8-BOM.txt")) 158 | ); 159 | } 160 | 161 | #[test] 162 | fn test_text_utf16le() { 163 | assert_eq!( 164 | UTF_16LE, 165 | inspect(include_bytes!("../testdata/text_UTF-16LE-BOM.txt")) 166 | ); 167 | } 168 | 169 | #[test] 170 | fn test_text_utf16be() { 171 | assert_eq!( 172 | UTF_16BE, 173 | inspect(include_bytes!("../testdata/text_UTF-16BE-BOM.txt")) 174 | ); 175 | } 176 | 177 | #[test] 178 | fn test_text_utf32le() { 179 | assert_eq!( 180 | UTF_32LE, 181 | inspect(include_bytes!("../testdata/text_UTF-32LE-BOM.txt")) 182 | ); 183 | } 184 | 185 | #[test] 186 | fn test_text_utf32be() { 187 | assert_eq!( 188 | UTF_32BE, 189 | inspect(include_bytes!("../testdata/text_UTF-32BE-BOM.txt")) 190 | ); 191 | } 192 | 193 | #[test] 194 | fn test_png() { 195 | assert_eq!(BINARY, inspect(include_bytes!("../testdata/test.png"))); 196 | } 197 | 198 | #[test] 199 | fn test_jpg() { 200 | assert_eq!(BINARY, inspect(include_bytes!("../testdata/test.jpg"))); 201 | } 202 | 203 | #[test] 204 | fn test_pdf() { 205 | assert_eq!(BINARY, inspect(include_bytes!("../testdata/test.pdf"))); 206 | } 207 | 208 | #[test] 209 | fn test_is_text() { 210 | assert!(UTF_8.is_text()); 211 | assert!(UTF_32LE.is_text()); 212 | } 213 | 214 | #[test] 215 | fn test_is_binary() { 216 | assert!(BINARY.is_binary()); 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /testdata/create_text_files.py: -------------------------------------------------------------------------------- 1 | import codecs 2 | 3 | encodings = { 4 | "UTF-8": ("utf-8", codecs.BOM_UTF8), 5 | "UTF-16BE": ("utf_16_be", codecs.BOM_UTF16_BE), 6 | "UTF-16LE": ("utf_16_le", codecs.BOM_UTF16_LE), 7 | "UTF-32BE": ("utf_32_be", codecs.BOM_UTF32_BE), 8 | "UTF-32LE": ("utf_32_le", codecs.BOM_UTF32_LE), 9 | } 10 | 11 | with open("text_UTF-8.txt", "rb") as source: 12 | data = source.read() 13 | text = data.decode("utf-8") 14 | 15 | for name, (encoding, bom) in encodings.items(): 16 | with open("text_{}-BOM.txt".format(name), "wb") as target: 17 | target.write(bom) 18 | target.write(text.encode(encoding)) 19 | -------------------------------------------------------------------------------- /testdata/file_sources.md: -------------------------------------------------------------------------------- 1 | test.png: https://commons.wikimedia.org/wiki/File:PNG-Gradient.png 2 | test.jpg: https://commons.wikimedia.org/wiki/File:JPEG_example_JPG_RIP_001.jpg 3 | test.pdf: http://brendanzagaeski.appspot.com/0004.html 4 | -------------------------------------------------------------------------------- /testdata/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharkdp/content_inspector/2b4d04be93df3b51f4c20b9773944750097246a7/testdata/test.jpg -------------------------------------------------------------------------------- /testdata/test.pdf: -------------------------------------------------------------------------------- 1 | %PDF-1.1 2 | %¥±ë 3 | 4 | 1 0 obj 5 | << /Type /Catalog 6 | /Pages 2 0 R 7 | >> 8 | endobj 9 | 10 | 2 0 obj 11 | << /Type /Pages 12 | /Kids [3 0 R] 13 | /Count 1 14 | /MediaBox [0 0 300 144] 15 | >> 16 | endobj 17 | 18 | 3 0 obj 19 | << /Type /Page 20 | /Parent 2 0 R 21 | /Resources 22 | << /Font 23 | << /F1 24 | << /Type /Font 25 | /Subtype /Type1 26 | /BaseFont /Times-Roman 27 | >> 28 | >> 29 | >> 30 | /Contents 4 0 R 31 | >> 32 | endobj 33 | 34 | 4 0 obj 35 | << /Length 55 >> 36 | stream 37 | BT 38 | /F1 18 Tf 39 | 0 0 Td 40 | (Hello World) Tj 41 | ET 42 | endstream 43 | endobj 44 | 45 | xref 46 | 0 5 47 | 0000000000 65535 f 48 | 0000000018 00000 n 49 | 0000000077 00000 n 50 | 0000000178 00000 n 51 | 0000000457 00000 n 52 | trailer 53 | << /Root 1 0 R 54 | /Size 5 55 | >> 56 | startxref 57 | 565 58 | %%EOF 59 | -------------------------------------------------------------------------------- /testdata/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharkdp/content_inspector/2b4d04be93df3b51f4c20b9773944750097246a7/testdata/test.png -------------------------------------------------------------------------------- /testdata/text_UTF-16BE-BOM.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharkdp/content_inspector/2b4d04be93df3b51f4c20b9773944750097246a7/testdata/text_UTF-16BE-BOM.txt -------------------------------------------------------------------------------- /testdata/text_UTF-16LE-BOM.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharkdp/content_inspector/2b4d04be93df3b51f4c20b9773944750097246a7/testdata/text_UTF-16LE-BOM.txt -------------------------------------------------------------------------------- /testdata/text_UTF-32BE-BOM.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharkdp/content_inspector/2b4d04be93df3b51f4c20b9773944750097246a7/testdata/text_UTF-32BE-BOM.txt -------------------------------------------------------------------------------- /testdata/text_UTF-32LE-BOM.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharkdp/content_inspector/2b4d04be93df3b51f4c20b9773944750097246a7/testdata/text_UTF-32LE-BOM.txt -------------------------------------------------------------------------------- /testdata/text_UTF-8-BOM.txt: -------------------------------------------------------------------------------- 1 | simple text and 2 | some characters like 🌂, 💖, ä, 𝄞, € and ∰ 3 | -------------------------------------------------------------------------------- /testdata/text_UTF-8.txt: -------------------------------------------------------------------------------- 1 | simple text and 2 | some characters like 🌂, 💖, ä, 𝄞, € and ∰ 3 | --------------------------------------------------------------------------------