diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/chardetng_c | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/chardetng_c')
-rw-r--r-- | third_party/rust/chardetng_c/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/CONTRIBUTING.md | 34 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/COPYRIGHT | 9 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/Cargo.toml | 38 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/LICENSE-APACHE | 202 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/LICENSE-MIT | 25 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/README.md | 31 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/include/chardetng.h | 154 | ||||
-rw-r--r-- | third_party/rust/chardetng_c/src/lib.rs | 176 |
9 files changed, 670 insertions, 0 deletions
diff --git a/third_party/rust/chardetng_c/.cargo-checksum.json b/third_party/rust/chardetng_c/.cargo-checksum.json new file mode 100644 index 0000000000..404017a483 --- /dev/null +++ b/third_party/rust/chardetng_c/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CONTRIBUTING.md":"5f28b63428b92d27d796d6d926447d15a19232236200e161ec870f4fdda1b489","COPYRIGHT":"5fa6d8c0701e5ce051b72b9ed08e3a75d5aee8e4132d876556c3dc04084238c7","Cargo.toml":"4bdc7754f31d433d876bb1055ad3bb1b8061fe7044daa316b7cc43c323cf22bc","LICENSE-APACHE":"cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30","LICENSE-MIT":"3fa4ca83dcc9237839b1bdeb2e6d16bdfb5ec0c5ce42b24694d8bbf0dcbef72c","README.md":"f458dc617c487ace6c60096e1bf3ab1b39c151543b40916717596c28e81deebc","include/chardetng.h":"8a781fcbf6441d063abc6c004d485cb2d5a0b304f3bfe5d5978e70437b7b778e","src/lib.rs":"2eeaf976144bab2c9c819934ca0fce36fe2d9b21236d19b506426733d9a57e2b"},"package":null}
\ No newline at end of file diff --git a/third_party/rust/chardetng_c/CONTRIBUTING.md b/third_party/rust/chardetng_c/CONTRIBUTING.md new file mode 100644 index 0000000000..ca03c5d355 --- /dev/null +++ b/third_party/rust/chardetng_c/CONTRIBUTING.md @@ -0,0 +1,34 @@ +If you send a pull request / patch, please observe the following. + +## Licensing + +Since this crate is dual-licensed, +[section 5 of the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0#contributions) +is considered to apply in the sense of Contributions being automatically +under the Apache License 2.0 or MIT dual license (see the `COPYRIGHT` file). +That is, by the act of offering a Contribution, you place your Contribution +under the Apache License 2.0 or MIT dual license stated in the `COPYRIGHT` +file. Please do not contribute if you aren't willing or allowed to license your +contributions in this manner. + +You are encouraged to dedicate test code that you contribute to the Public +Domain using the CC0 dedication. If you contribute test code that is not +dedicated to the Public Domain, please be sure not to put it in a part of +source code that the comments designate as being dedicated to the Public +Domain. + +## Copyright Notices + +If you require the addition of your copyright notice, it's up to you to edit in +your notice as part of your Contribution. Not adding a copyright notice is +taken as a waiver of copyright notice. + +## Compatibility with Stable Rust + +Please ensure that your Contribution compiles with the latest stable-channel +rustc. + +## rustfmt + +The `rustfmt` version used for this code is `rustfmt-nightly`. Please either +use that version or avoid using `rustfmt` (so as not to reformat all the code). diff --git a/third_party/rust/chardetng_c/COPYRIGHT b/third_party/rust/chardetng_c/COPYRIGHT new file mode 100644 index 0000000000..e149878112 --- /dev/null +++ b/third_party/rust/chardetng_c/COPYRIGHT @@ -0,0 +1,9 @@ +chardetng_c is copyright Mozilla Foundation. + +Licensed under the Apache License, Version 2.0 +<LICENSE-APACHE or +https://www.apache.org/licenses/LICENSE-2.0> or the MIT +license <LICENSE-MIT or https://opensource.org/licenses/MIT>, +at your option. All files in the project carrying such +notice may not be copied, modified, or distributed except +according to those terms. diff --git a/third_party/rust/chardetng_c/Cargo.toml b/third_party/rust/chardetng_c/Cargo.toml new file mode 100644 index 0000000000..a4dbace3b2 --- /dev/null +++ b/third_party/rust/chardetng_c/Cargo.toml @@ -0,0 +1,38 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "chardetng_c" +version = "0.1.2" +authors = ["Henri Sivonen <hsivonen@hsivonen.fi>"] +description = "C bindings for chardetng" +homepage = "https://docs.rs/chardetng_c/" +documentation = "https://docs.rs/chardetng_c/" +readme = "README.md" +keywords = [ + "encoding", + "web", + "unicode", + "charset", +] +categories = [ + "text-processing", + "encoding", + "web-programming", + "internationalization", +] +license = "Apache-2.0 OR MIT" +repository = "https://github.com/hsivonen/chardetng-c" + +[dependencies] +chardetng = "0.1.1" +encoding_rs = "0.8.17" diff --git a/third_party/rust/chardetng_c/LICENSE-APACHE b/third_party/rust/chardetng_c/LICENSE-APACHE new file mode 100644 index 0000000000..d645695673 --- /dev/null +++ b/third_party/rust/chardetng_c/LICENSE-APACHE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/third_party/rust/chardetng_c/LICENSE-MIT b/third_party/rust/chardetng_c/LICENSE-MIT new file mode 100644 index 0000000000..3317c82e2f --- /dev/null +++ b/third_party/rust/chardetng_c/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright Mozilla Foundation + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/third_party/rust/chardetng_c/README.md b/third_party/rust/chardetng_c/README.md new file mode 100644 index 0000000000..bba148ceb3 --- /dev/null +++ b/third_party/rust/chardetng_c/README.md @@ -0,0 +1,31 @@ +# chardetng_c + +[![crates.io](https://meritbadge.herokuapp.com/chardetng_c)](https://crates.io/crates/chardetng_c) +[![docs.rs](https://docs.rs/chardetng_c/badge.svg)](https://docs.rs/chardetng_c/) +[![Apache 2 / MIT dual-licensed](https://img.shields.io/badge/license-Apache%202%20%2F%20MIT-blue.svg)](https://github.com/hsivonen/chardetng_c/blob/master/COPYRIGHT) + +A C wrapper for [`chardetng`](https://crates.io/crates/chardetng). + +## Licensing + +Please see the file named +[COPYRIGHT](https://github.com/hsivonen/chardetng_c/blob/master/COPYRIGHT). + +## Documentation + +Generated [API documentation](https://docs.rs/chardetng_c/) is available +online. + +## Release Notes + +### 0.1.2 + +* Remove year from copyright notices. + +### 0.1.1 + +* Add newline to the end of the C header. + +### 0.1.0 + +* Initial release. diff --git a/third_party/rust/chardetng_c/include/chardetng.h b/third_party/rust/chardetng_c/include/chardetng.h new file mode 100644 index 0000000000..b59d05702e --- /dev/null +++ b/third_party/rust/chardetng_c/include/chardetng.h @@ -0,0 +1,154 @@ +// Copyright Mozilla Foundation. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#ifndef chardetng_h +#define chardetng_h + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdint.h> +#include <stdbool.h> +#include "encoding_rs.h" + +#ifndef CHARDETNG_ENCODING_DETECTOR +#define CHARDETNG_ENCODING_DETECTOR EncodingDetector +#ifndef __cplusplus +typedef struct Detector_ EncodingDetector; +#endif +#endif + +/// Instantiates a Web browser-oriented detector for guessing what +/// character encoding a stream of bytes is encoded in. +/// +/// The bytes are fed to the detector incrementally using the +/// `chardetng_encoding_detector_free` function. The current guess of the +/// detector can be queried using the `chardetng_encoding_detector_guess` +/// function. The guessing parameters are arguments to the +/// `chardetng_encoding_detector_guess` function rather than arguments to the +/// constructor in order to enable the application to check if the arguments +/// affect the guessing outcome. (The specific use case is to disable UI for +/// re-running the detector with UTF-8 allowed and the top-level domain name +/// ignored if those arguments don't change the guess.) +/// +/// The instantiated detector must be freed after use using +/// `chardetng_detectordetector_free`. +CHARDETNG_ENCODING_DETECTOR* chardetng_encoding_detector_new(); + +/// Deallocates a detector obtained from `chardetng_encodingdetector_new`. +void chardetng_encoding_detector_free(CHARDETNG_ENCODING_DETECTOR* detector); + +/// Queries whether the TLD is considered non-generic and could affect the guess. +/// +/// # Undefined Behavior +/// +/// UB ensues if +/// +/// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len` +/// don't designate a range of memory valid for reading. +bool chardetng_encoding_detector_tld_may_affect_guess(char const* tld, size_t tld_len); + +/// Inform the detector of a chunk of input. +/// +/// The byte stream is represented as a sequence of calls to this +/// function such that the concatenation of the arguments to this +/// function form the byte stream. It does not matter how the application +/// chooses to chunk the stream. It is OK to call this function with +/// a zero-length byte slice. +/// +/// The end of the stream is indicated by calling this function with +/// `last` set to `true`. In that case, the end of the stream is +/// considered to occur after the last byte of the `buffer` (which +/// may be zero-length) passed in the same call. Once this function +/// has been called with `last` set to `true` this function must not +/// be called again. +/// +/// If you want to perform detection on just the prefix of a longer +/// stream, do not pass `last=true` after the prefix if the stream +/// actually still continues. +/// +/// Returns `true` if after processing `buffer` the stream has +/// contained at least one non-ASCII byte and `false` if only +/// ASCII has been seen so far. +/// +/// # Panics +/// +/// If this function has previously been called with `last` set to `true`. +/// +/// # Undefined Behavior +/// +/// UB ensues if +/// +/// * `detector` does not point to a detector obtained from +/// `chardetng_detector_new` but not yet freed with +/// `chardetng_detector_free`. +/// * `buffer` is `NULL`. (It can be a bogus pointer when `buffer_len` is 0.) +/// * ,buffer_len` is non-zero and `buffer` and `buffer_len` don't designate +/// a range of memory valid for reading. +bool chardetng_encoding_detector_feed( + CHARDETNG_ENCODING_DETECTOR* detector, + uint8_t const* buffer, + size_t buffer_len, + bool last +); + +/// Guess the encoding given the bytes pushed to the detector so far +/// (via `chardetng_encoding_detector_feed()`), the top-level domain name +/// from which the bytes were loaded, and an indication of whether to +/// consider UTF-8 as a permissible guess. +/// +/// The `tld` argument takes the rightmost DNS label of the hostname of the +/// host the stream was loaded from in lower-case ASCII form. That is, if +/// the label is an internationalized top-level domain name, it must be +/// provided in its Punycode form. If the TLD that the stream was loaded +/// from is unavalable, `NULL` may be passed instead (and 0 as `tld_len`), +/// which is equivalent to passing pointer to "com" as `tld` and 3 as +/// `tld_len`. +/// +/// If the `allow_utf8` argument is set to `false`, the return value of +/// this function won't be `UTF_8_ENCODING`. When performing detection +/// on `text/html` on non-`file:` URLs, Web browsers must pass `false`, +/// unless the user has taken a specific contextual action to request an +/// override. This way, Web developers cannot start depending on UTF-8 +/// detection. Such reliance would make the Web Platform more brittle. +/// +/// Returns the guessed encoding (never `NULL`). +/// +/// # Panics +/// +/// If `tld` is `NULL` but `tld_len` is not zero. +/// +/// If `tld` contains non-ASCII, period, or upper-case letters. (The panic +/// condition is intentionally limited to signs of failing to extract the +/// label correctly, failing to provide it in its Punycode form, and failure +/// to lower-case it. Full DNS label validation is intentionally not performed +/// to avoid panics when the reality doesn't match the specs.) +/// +/// # Undefined Behavior +/// +/// UB ensues if +/// +/// * `detector` does not point to a detector obtained from +/// `chardetng_detector_new` but not yet freed with +/// `chardetng_detector_free`. +/// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len` +/// don't designate a range of memory valid for reading. +ENCODING_RS_ENCODING const* chardetng_encoding_detector_guess( + CHARDETNG_ENCODING_DETECTOR const* detector, + char const* tld, + size_t tld_len, + bool allow_utf8 +); + +#ifdef __cplusplus +} +#endif + +#endif // chardetng_h diff --git a/third_party/rust/chardetng_c/src/lib.rs b/third_party/rust/chardetng_c/src/lib.rs new file mode 100644 index 0000000000..0ca3383b9e --- /dev/null +++ b/third_party/rust/chardetng_c/src/lib.rs @@ -0,0 +1,176 @@ +// Copyright Mozilla Foundation. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![doc(html_root_url = "https://docs.rs/chardetng_c/0.1.0")] + +//! C API for [`chardetng`](https://docs.rs/chardetng/) +//! +//! # Panics +//! +//! This crate is designed to be used only in a `panic=abort` scenario. +//! Panic propagation across FFI is not handled! +//! +//! # Licensing +//! +//! See the file named [COPYRIGHT](https://github.com/hsivonen/chardetng_c/blob/master/COPYRIGHT). + +use encoding_rs::Encoding; +use chardetng::EncodingDetector; + +/// Instantiates a Web browser-oriented detector for guessing what +/// character encoding a stream of bytes is encoded in. +/// +/// The bytes are fed to the detector incrementally using the +/// `chardetng_encoding_detector_free` function. The current guess of the +/// detector can be queried using the `chardetng_encoding_detector_guess` +/// function. The guessing parameters are arguments to the +/// `chardetng_encoding_detector_guess` function rather than arguments to the +/// constructor in order to enable the application to check if the arguments +/// affect the guessing outcome. (The specific use case is to disable UI for +/// re-running the detector with UTF-8 allowed and the top-level domain name +/// ignored if those arguments don't change the guess.) +/// +/// The instantiated detector must be freed after use using +/// `chardetng_detectordetector_free`. +#[no_mangle] +pub unsafe extern "C" fn chardetng_encoding_detector_new() -> *mut EncodingDetector { + Box::into_raw(Box::new(EncodingDetector::new())) +} + +/// Deallocates a detector obtained from `chardetng_encodingdetector_new`. +#[no_mangle] +pub unsafe extern "C" fn chardetng_encoding_detector_free(detector: *mut EncodingDetector) { + let _ = Box::from_raw(detector); +} + +/// Queries whether the TLD is considered non-generic and could affect the guess. +/// +/// # Undefined Behavior +/// +/// UB ensues if +/// +/// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len` +/// don't designate a range of memory valid for reading. +#[no_mangle] +pub unsafe extern "C" fn chardetng_encoding_detector_tld_may_affect_guess( + tld: *const u8, + tld_len: usize, +) -> bool { + let tld_opt = if tld.is_null() { + assert_eq!(tld_len, 0); + None + } else { + Some(::std::slice::from_raw_parts(tld, tld_len)) + }; + EncodingDetector::tld_may_affect_guess(tld_opt) +} + +/// Inform the detector of a chunk of input. +/// +/// The byte stream is represented as a sequence of calls to this +/// function such that the concatenation of the arguments to this +/// function form the byte stream. It does not matter how the application +/// chooses to chunk the stream. It is OK to call this function with +/// a zero-length byte slice. +/// +/// The end of the stream is indicated by calling this function with +/// `last` set to `true`. In that case, the end of the stream is +/// considered to occur after the last byte of the `buffer` (which +/// may be zero-length) passed in the same call. Once this function +/// has been called with `last` set to `true` this function must not +/// be called again. +/// +/// If you want to perform detection on just the prefix of a longer +/// stream, do not pass `last=true` after the prefix if the stream +/// actually still continues. +/// +/// Returns `true` if after processing `buffer` the stream has +/// contained at least one non-ASCII byte and `false` if only +/// ASCII has been seen so far. +/// +/// # Panics +/// +/// If this function has previously been called with `last` set to `true`. +/// +/// # Undefined Behavior +/// +/// UB ensues if +/// +/// * `detector` does not point to a detector obtained from +/// `chardetng_detector_new` but not yet freed with +/// `chardetng_detector_free`. +/// * `buffer` is `NULL`. (It can be a bogus pointer when `buffer_len` is 0.) +/// * ,buffer_len` is non-zero and `buffer` and `buffer_len` don't designate +/// a range of memory valid for reading. +#[no_mangle] +pub unsafe extern "C" fn chardetng_encoding_detector_feed( + detector: *mut EncodingDetector, + buffer: *const u8, + buffer_len: usize, + last: bool, +) -> bool { + (*detector).feed(::std::slice::from_raw_parts(buffer, buffer_len), last) +} + +/// Guess the encoding given the bytes pushed to the detector so far +/// (via `chardetng_encoding_detector_feed()`), the top-level domain name +/// from which the bytes were loaded, and an indication of whether to +/// consider UTF-8 as a permissible guess. +/// +/// The `tld` argument takes the rightmost DNS label of the hostname of the +/// host the stream was loaded from in lower-case ASCII form. That is, if +/// the label is an internationalized top-level domain name, it must be +/// provided in its Punycode form. If the TLD that the stream was loaded +/// from is unavalable, `NULL` may be passed instead (and 0 as `tld_len`), +/// which is equivalent to passing pointer to "com" as `tld` and 3 as +/// `tld_len`. +/// +/// If the `allow_utf8` argument is set to `false`, the return value of +/// this function won't be `UTF_8_ENCODING`. When performing detection +/// on `text/html` on non-`file:` URLs, Web browsers must pass `false`, +/// unless the user has taken a specific contextual action to request an +/// override. This way, Web developers cannot start depending on UTF-8 +/// detection. Such reliance would make the Web Platform more brittle. +/// +/// Returns the guessed encoding (never `NULL`). +/// +/// # Panics +/// +/// If `tld` is `NULL` but `tld_len` is not zero. +/// +/// If `tld` contains non-ASCII, period, or upper-case letters. (The panic +/// condition is intentionally limited to signs of failing to extract the +/// label correctly, failing to provide it in its Punycode form, and failure +/// to lower-case it. Full DNS label validation is intentionally not performed +/// to avoid panics when the reality doesn't match the specs.) +/// +/// # Undefined Behavior +/// +/// UB ensues if +/// +/// * `detector` does not point to a detector obtained from +/// `chardetng_detector_new` but not yet freed with +/// `chardetng_detector_free`. +/// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len` +/// don't designate a range of memory valid for reading. +#[no_mangle] +pub unsafe extern "C" fn chardetng_encoding_detector_guess( + detector: *const EncodingDetector, + tld: *const u8, + tld_len: usize, + allow_utf8: bool, +) -> *const Encoding { + let tld_opt = if tld.is_null() { + assert_eq!(tld_len, 0); + None + } else { + Some(::std::slice::from_raw_parts(tld, tld_len)) + }; + (*detector).guess(tld_opt, allow_utf8) +} |