Debian Codegraph @

third_party/rust/chardetng_c/src/lib.rs

raw
1 // Copyright Mozilla Foundation. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![doc(html_root_url = "https://docs.rs/chardetng_c/0.1.0")]
11
12 //! C API for [`chardetng`](https://docs.rs/chardetng/)
13 //!
14 //! # Panics
15 //!
16 //! This crate is designed to be used only in a `panic=abort` scenario.
17 //! Panic propagation across FFI is not handled!
18 //!
19 //! # Licensing
20 //!
21 //! See the file named [COPYRIGHT](https://github.com/hsivonen/chardetng_c/blob/master/COPYRIGHT).
22
23 use encoding_rs::Encoding;
24 use chardetng::EncodingDetector;
25
26 /// Instantiates a Web browser-oriented detector for guessing what
27 /// character encoding a stream of bytes is encoded in.
28 ///
29 /// The bytes are fed to the detector incrementally using the
30 /// `chardetng_encoding_detector_free` function. The current guess of the
31 /// detector can be queried using the `chardetng_encoding_detector_guess`
32 /// function. The guessing parameters are arguments to the
33 /// `chardetng_encoding_detector_guess` function rather than arguments to the
34 /// constructor in order to enable the application to check if the arguments
35 /// affect the guessing outcome. (The specific use case is to disable UI for
36 /// re-running the detector with UTF-8 allowed and the top-level domain name
37 /// ignored if those arguments don't change the guess.)
38 ///
39 /// The instantiated detector must be freed after use using
40 /// `chardetng_detectordetector_free`.
41 #[no_mangle]
42 pub unsafe extern "C" fn chardetng_encoding_detector_new() -> *mut EncodingDetector {
43 Box::into_raw(Box::new(EncodingDetector::new()))
44 }
45
46 /// Deallocates a detector obtained from `chardetng_encodingdetector_new`.
47 #[no_mangle]
48 pub unsafe extern "C" fn chardetng_encoding_detector_free(detector: *mut EncodingDetector) {
49 let _ = Box::from_raw(detector);
50 }
51
52 /// Queries whether the TLD is considered non-generic and could affect the guess.
53 ///
54 /// # Undefined Behavior
55 ///
56 /// UB ensues if
57 ///
58 /// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len`
59 /// don't designate a range of memory valid for reading.
60 #[no_mangle]
61 pub unsafe extern "C" fn chardetng_encoding_detector_tld_may_affect_guess(
62 tld: *const u8,
63 tld_len: usize,
64 ) -> bool {
65 let tld_opt = if tld.is_null() {
66 assert_eq!(tld_len, 0);
67 None
68 } else {
69 Some(::std::slice::from_raw_parts(tld, tld_len))
70 };
71 EncodingDetector::tld_may_affect_guess(tld_opt)
72 }
73
74 /// Inform the detector of a chunk of input.
75 ///
76 /// The byte stream is represented as a sequence of calls to this
77 /// function such that the concatenation of the arguments to this
78 /// function form the byte stream. It does not matter how the application
79 /// chooses to chunk the stream. It is OK to call this function with
80 /// a zero-length byte slice.
81 ///
82 /// The end of the stream is indicated by calling this function with
83 /// `last` set to `true`. In that case, the end of the stream is
84 /// considered to occur after the last byte of the `buffer` (which
85 /// may be zero-length) passed in the same call. Once this function
86 /// has been called with `last` set to `true` this function must not
87 /// be called again.
88 ///
89 /// If you want to perform detection on just the prefix of a longer
90 /// stream, do not pass `last=true` after the prefix if the stream
91 /// actually still continues.
92 ///
93 /// Returns `true` if after processing `buffer` the stream has
94 /// contained at least one non-ASCII byte and `false` if only
95 /// ASCII has been seen so far.
96 ///
97 /// # Panics
98 ///
99 /// If this function has previously been called with `last` set to `true`.
100 ///
101 /// # Undefined Behavior
102 ///
103 /// UB ensues if
104 ///
105 /// * `detector` does not point to a detector obtained from
106 /// `chardetng_detector_new` but not yet freed with
107 /// `chardetng_detector_free`.
108 /// * `buffer` is `NULL`. (It can be a bogus pointer when `buffer_len` is 0.)
109 /// * ,buffer_len` is non-zero and `buffer` and `buffer_len` don't designate
110 /// a range of memory valid for reading.
111 #[no_mangle]
112 pub unsafe extern "C" fn chardetng_encoding_detector_feed(
113 detector: *mut EncodingDetector,
114 buffer: *const u8,
115 buffer_len: usize,
116 last: bool,
117 ) -> bool {
118 (*detector).feed(::std::slice::from_raw_parts(buffer, buffer_len), last)
119 }
120
121 /// Guess the encoding given the bytes pushed to the detector so far
122 /// (via `chardetng_encoding_detector_feed()`), the top-level domain name
123 /// from which the bytes were loaded, and an indication of whether to
124 /// consider UTF-8 as a permissible guess.
125 ///
126 /// The `tld` argument takes the rightmost DNS label of the hostname of the
127 /// host the stream was loaded from in lower-case ASCII form. That is, if
128 /// the label is an internationalized top-level domain name, it must be
129 /// provided in its Punycode form. If the TLD that the stream was loaded
130 /// from is unavalable, `NULL` may be passed instead (and 0 as `tld_len`),
131 /// which is equivalent to passing pointer to "com" as `tld` and 3 as
132 /// `tld_len`.
133 ///
134 /// If the `allow_utf8` argument is set to `false`, the return value of
135 /// this function won't be `UTF_8_ENCODING`. When performing detection
136 /// on `text/html` on non-`file:` URLs, Web browsers must pass `false`,
137 /// unless the user has taken a specific contextual action to request an
138 /// override. This way, Web developers cannot start depending on UTF-8
139 /// detection. Such reliance would make the Web Platform more brittle.
140 ///
141 /// Returns the guessed encoding (never `NULL`).
142 ///
143 /// # Panics
144 ///
145 /// If `tld` is `NULL` but `tld_len` is not zero.
146 ///
147 /// If `tld` contains non-ASCII, period, or upper-case letters. (The panic
148 /// condition is intentionally limited to signs of failing to extract the
149 /// label correctly, failing to provide it in its Punycode form, and failure
150 /// to lower-case it. Full DNS label validation is intentionally not performed
151 /// to avoid panics when the reality doesn't match the specs.)
152 ///
153 /// # Undefined Behavior
154 ///
155 /// UB ensues if
156 ///
157 /// * `detector` does not point to a detector obtained from
158 /// `chardetng_detector_new` but not yet freed with
159 /// `chardetng_detector_free`.
160 /// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len`
161 /// don't designate a range of memory valid for reading.
162 #[no_mangle]
163 pub unsafe extern "C" fn chardetng_encoding_detector_guess(
164 detector: *const EncodingDetector,
165 tld: *const u8,
166 tld_len: usize,
167 allow_utf8: bool,
168 ) -> *const Encoding {
169 let tld_opt = if tld.is_null() {
170 assert_eq!(tld_len, 0);
171 None
172 } else {
173 Some(::std::slice::from_raw_parts(tld, tld_len))
174 };
175 (*detector).guess(tld_opt, allow_utf8)
176 }