| 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 |
#ifndef chardetng_h |
| 11 |
#define chardetng_h |
| 12 |
|
| 13 |
#ifdef __cplusplus |
| 14 |
extern "C" { |
| 15 |
#endif |
| 16 |
|
| 17 |
#include <stdint.h> |
| 18 |
#include <stdbool.h> |
| 19 |
#include "encoding_rs.h" |
| 20 |
|
| 21 |
#ifndef CHARDETNG_ENCODING_DETECTOR |
| 22 |
#define CHARDETNG_ENCODING_DETECTOR EncodingDetector |
| 23 |
#ifndef __cplusplus |
| 24 |
typedef struct Detector_ EncodingDetector; |
| 25 |
#endif |
| 26 |
#endif |
| 27 |
|
| 28 |
/// Instantiates a Web browser-oriented detector for guessing what |
| 29 |
/// character encoding a stream of bytes is encoded in. |
| 30 |
/// |
| 31 |
/// The bytes are fed to the detector incrementally using the |
| 32 |
/// `chardetng_encoding_detector_free` function. The current guess of the |
| 33 |
/// detector can be queried using the `chardetng_encoding_detector_guess` |
| 34 |
/// function. The guessing parameters are arguments to the |
| 35 |
/// `chardetng_encoding_detector_guess` function rather than arguments to the |
| 36 |
/// constructor in order to enable the application to check if the arguments |
| 37 |
/// affect the guessing outcome. (The specific use case is to disable UI for |
| 38 |
/// re-running the detector with UTF-8 allowed and the top-level domain name |
| 39 |
/// ignored if those arguments don't change the guess.) |
| 40 |
/// |
| 41 |
/// The instantiated detector must be freed after use using |
| 42 |
/// `chardetng_detectordetector_free`. |
| 43 |
CHARDETNG_ENCODING_DETECTOR* chardetng_encoding_detector_new(); |
| 44 |
|
| 45 |
/// Deallocates a detector obtained from `chardetng_encodingdetector_new`. |
| 46 |
void chardetng_encoding_detector_free(CHARDETNG_ENCODING_DETECTOR* detector); |
| 47 |
|
| 48 |
/// Queries whether the TLD is considered non-generic and could affect the guess. |
| 49 |
/// |
| 50 |
/// # Undefined Behavior |
| 51 |
/// |
| 52 |
/// UB ensues if |
| 53 |
/// |
| 54 |
/// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len` |
| 55 |
/// don't designate a range of memory valid for reading. |
| 56 |
bool chardetng_encoding_detector_tld_may_affect_guess(char const* tld, size_t tld_len); |
| 57 |
|
| 58 |
/// Inform the detector of a chunk of input. |
| 59 |
/// |
| 60 |
/// The byte stream is represented as a sequence of calls to this |
| 61 |
/// function such that the concatenation of the arguments to this |
| 62 |
/// function form the byte stream. It does not matter how the application |
| 63 |
/// chooses to chunk the stream. It is OK to call this function with |
| 64 |
/// a zero-length byte slice. |
| 65 |
/// |
| 66 |
/// The end of the stream is indicated by calling this function with |
| 67 |
/// `last` set to `true`. In that case, the end of the stream is |
| 68 |
/// considered to occur after the last byte of the `buffer` (which |
| 69 |
/// may be zero-length) passed in the same call. Once this function |
| 70 |
/// has been called with `last` set to `true` this function must not |
| 71 |
/// be called again. |
| 72 |
/// |
| 73 |
/// If you want to perform detection on just the prefix of a longer |
| 74 |
/// stream, do not pass `last=true` after the prefix if the stream |
| 75 |
/// actually still continues. |
| 76 |
/// |
| 77 |
/// Returns `true` if after processing `buffer` the stream has |
| 78 |
/// contained at least one non-ASCII byte and `false` if only |
| 79 |
/// ASCII has been seen so far. |
| 80 |
/// |
| 81 |
/// # Panics |
| 82 |
/// |
| 83 |
/// If this function has previously been called with `last` set to `true`. |
| 84 |
/// |
| 85 |
/// # Undefined Behavior |
| 86 |
/// |
| 87 |
/// UB ensues if |
| 88 |
/// |
| 89 |
/// * `detector` does not point to a detector obtained from |
| 90 |
/// `chardetng_detector_new` but not yet freed with |
| 91 |
/// `chardetng_detector_free`. |
| 92 |
/// * `buffer` is `NULL`. (It can be a bogus pointer when `buffer_len` is 0.) |
| 93 |
/// * ,buffer_len` is non-zero and `buffer` and `buffer_len` don't designate |
| 94 |
/// a range of memory valid for reading. |
| 95 |
bool chardetng_encoding_detector_feed( |
| 96 |
CHARDETNG_ENCODING_DETECTOR* detector, |
| 97 |
uint8_t const* buffer, |
| 98 |
size_t buffer_len, |
| 99 |
bool last |
| 100 |
); |
| 101 |
|
| 102 |
/// Guess the encoding given the bytes pushed to the detector so far |
| 103 |
/// (via `chardetng_encoding_detector_feed()`), the top-level domain name |
| 104 |
/// from which the bytes were loaded, and an indication of whether to |
| 105 |
/// consider UTF-8 as a permissible guess. |
| 106 |
/// |
| 107 |
/// The `tld` argument takes the rightmost DNS label of the hostname of the |
| 108 |
/// host the stream was loaded from in lower-case ASCII form. That is, if |
| 109 |
/// the label is an internationalized top-level domain name, it must be |
| 110 |
/// provided in its Punycode form. If the TLD that the stream was loaded |
| 111 |
/// from is unavalable, `NULL` may be passed instead (and 0 as `tld_len`), |
| 112 |
/// which is equivalent to passing pointer to "com" as `tld` and 3 as |
| 113 |
/// `tld_len`. |
| 114 |
/// |
| 115 |
/// If the `allow_utf8` argument is set to `false`, the return value of |
| 116 |
/// this function won't be `UTF_8_ENCODING`. When performing detection |
| 117 |
/// on `text/html` on non-`file:` URLs, Web browsers must pass `false`, |
| 118 |
/// unless the user has taken a specific contextual action to request an |
| 119 |
/// override. This way, Web developers cannot start depending on UTF-8 |
| 120 |
/// detection. Such reliance would make the Web Platform more brittle. |
| 121 |
/// |
| 122 |
/// Returns the guessed encoding (never `NULL`). |
| 123 |
/// |
| 124 |
/// # Panics |
| 125 |
/// |
| 126 |
/// If `tld` is `NULL` but `tld_len` is not zero. |
| 127 |
/// |
| 128 |
/// If `tld` contains non-ASCII, period, or upper-case letters. (The panic |
| 129 |
/// condition is intentionally limited to signs of failing to extract the |
| 130 |
/// label correctly, failing to provide it in its Punycode form, and failure |
| 131 |
/// to lower-case it. Full DNS label validation is intentionally not performed |
| 132 |
/// to avoid panics when the reality doesn't match the specs.) |
| 133 |
/// |
| 134 |
/// # Undefined Behavior |
| 135 |
/// |
| 136 |
/// UB ensues if |
| 137 |
/// |
| 138 |
/// * `detector` does not point to a detector obtained from |
| 139 |
/// `chardetng_detector_new` but not yet freed with |
| 140 |
/// `chardetng_detector_free`. |
| 141 |
/// * `tld` is non-NULL and `tld_len` is non-zero but `tld` and `tld_len` |
| 142 |
/// don't designate a range of memory valid for reading. |
| 143 |
ENCODING_RS_ENCODING const* chardetng_encoding_detector_guess( |
| 144 |
CHARDETNG_ENCODING_DETECTOR const* detector, |
| 145 |
char const* tld, |
| 146 |
size_t tld_len, |
| 147 |
bool allow_utf8 |
| 148 |
); |
| 149 |
|
| 150 |
#ifdef __cplusplus |
| 151 |
} |
| 152 |
#endif |
| 153 |
|
| 154 |
#endif // chardetng_h |