|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Fluent Bit |
| 4 | + * ========== |
| 5 | + * Copyright (C) 2025 The Fluent Bit Authors |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef FLB_WCHAR_H |
| 21 | +#define FLB_WCHAR_H |
| 22 | + |
| 23 | +#include <stddef.h> |
| 24 | +#include <stdbool.h> |
| 25 | + |
| 26 | +#include <fluent-bit/flb_log.h> |
| 27 | + |
| 28 | +/* msb for char */ |
| 29 | +#define HIGHBIT (0x80) |
| 30 | +#define IS_HIGHBIT_SET(ch) ((unsigned char)(ch) & HIGHBIT) |
| 31 | + |
| 32 | +/* |
| 33 | + * The FLB_wchar type |
| 34 | + */ |
| 35 | +typedef unsigned int flb_wchar; |
| 36 | + |
| 37 | +/* |
| 38 | + * Maximum byte length of multibyte characters in any backend encoding |
| 39 | + */ |
| 40 | +#define MAX_MULTIBYTE_CHAR_LEN 4 |
| 41 | + |
| 42 | +/* |
| 43 | + * SJIS validation macros |
| 44 | + */ |
| 45 | +#define ISSJISHEAD(c) (((c) >= 0x81 && (c) <= 0x9f) || ((c) >= 0xe0 && (c) <= 0xfc)) |
| 46 | +#define ISSJISTAIL(c) (((c) >= 0x40 && (c) <= 0x7e) || ((c) >= 0x80 && (c) <= 0xfc)) |
| 47 | + |
| 48 | +#include <fluent-bit/flb_macros.h> |
| 49 | + |
| 50 | +/* |
| 51 | + * Encoding identifiers |
| 52 | + */ |
| 53 | +typedef enum flb_enc |
| 54 | +{ |
| 55 | + FLB_STR_ASCII = 0, /* STR/ASCII */ |
| 56 | + FLB_UTF8, /* Unicode UTF8 */ |
| 57 | + FLB_WIN1256, /* windows-1256 */ |
| 58 | + FLB_WIN866, /* (MS-DOS CP866) */ |
| 59 | + FLB_WIN874, /* windows-874 */ |
| 60 | + FLB_WIN1251, /* windows-1251 */ |
| 61 | + FLB_WIN1252, /* windows-1252 */ |
| 62 | + FLB_WIN1250, /* windows-1250 */ |
| 63 | + FLB_WIN1253, /* windows-1253 */ |
| 64 | + FLB_WIN1254, /* windows-1254 */ |
| 65 | + FLB_WIN1255, /* windows-1255 */ |
| 66 | + FLB_SJIS, /* Shift JIS (Windows-932) */ |
| 67 | + FLB_BIG5, /* Big5 (Windows-950) */ |
| 68 | + FLB_GBK, /* GBK (Windows-936) */ |
| 69 | + FLB_UHC, /* UHC (Windows-949) */ |
| 70 | + FLB_GB18030, /* GB18030 */ |
| 71 | + _FLB_LAST_ENCODING_ /* mark only */ |
| 72 | + |
| 73 | +} flb_enc; |
| 74 | + |
| 75 | +#define FLB_VALID_ENCODING(_enc) \ |
| 76 | + ((_enc) >= 0 && (_enc) < _FLB_LAST_ENCODING_) |
| 77 | + |
| 78 | +/* On FE are possible all encodings */ |
| 79 | +#define FLB_VALID_FE_ENCODING(_enc) FLB_VALID_ENCODING(_enc) |
| 80 | + |
| 81 | +/* |
| 82 | + * flb_wchar stuff |
| 83 | + */ |
| 84 | +typedef int (*mb2wchar_with_len_converter) (const unsigned char *from, |
| 85 | + flb_wchar *to, |
| 86 | + int len); |
| 87 | + |
| 88 | +typedef int (*wchar2mb_with_len_converter) (const flb_wchar *from, |
| 89 | + unsigned char *to, |
| 90 | + int len); |
| 91 | + |
| 92 | +typedef int (*mblen_converter) (const unsigned char *mbstr); |
| 93 | +typedef int (*mbdisplaylen_converter) (const unsigned char *mbstr); |
| 94 | +typedef bool (*mbcharacter_incrementer) (unsigned char *mbstr, int len); |
| 95 | +typedef int (*mbchar_verifier) (const unsigned char *mbstr, int len); |
| 96 | +typedef int (*mbstr_verifier) (const unsigned char *mbstr, int len); |
| 97 | + |
| 98 | +typedef struct |
| 99 | +{ |
| 100 | + mb2wchar_with_len_converter mb2wchar_with_len; /* convert a multibyte |
| 101 | + * string to a wchar */ |
| 102 | + wchar2mb_with_len_converter wchar2mb_with_len; /* convert a wchar string |
| 103 | + * to a multibyte */ |
| 104 | + mblen_converter mblen; /* get byte length of a char */ |
| 105 | + mbdisplaylen_converter dsplen; /* get display width of a char */ |
| 106 | + mbchar_verifier mbverifychar; /* verify multibyte character */ |
| 107 | + mbstr_verifier mbverifystr; /* verify multibyte string */ |
| 108 | + int maxmblen; /* max bytes for a char in this encoding */ |
| 109 | +} flb_wchar_tbl; |
| 110 | + |
| 111 | +extern const flb_wchar_tbl flb_wchar_table[]; |
| 112 | + |
| 113 | +/* |
| 114 | + * Radix tree for character conversion. |
| 115 | + * |
| 116 | + */ |
| 117 | +typedef struct { |
| 118 | + const uint16_t *chars16; |
| 119 | + const uint32_t *chars32; |
| 120 | + |
| 121 | + /* Radix tree for 1-byte inputs */ |
| 122 | + uint32_t b1root; |
| 123 | + uint8_t b1_lower; |
| 124 | + uint8_t b1_upper; |
| 125 | + |
| 126 | + /* Radix tree for 2-byte inputs */ |
| 127 | + uint32_t b2root; |
| 128 | + uint8_t b2_1_lower; |
| 129 | + uint8_t b2_1_upper; |
| 130 | + uint8_t b2_2_lower; |
| 131 | + uint8_t b2_2_upper; |
| 132 | + |
| 133 | + /* Radix tree for 3-byte inputs */ |
| 134 | + uint32_t b3root; |
| 135 | + uint8_t b3_1_lower; |
| 136 | + uint8_t b3_1_upper; |
| 137 | + uint8_t b3_2_lower; |
| 138 | + uint8_t b3_2_upper; |
| 139 | + uint8_t b3_3_lower; |
| 140 | + uint8_t b3_3_upper; |
| 141 | + |
| 142 | + /* Radix tree for 4-byte inputs */ |
| 143 | + uint32_t b4root; |
| 144 | + uint8_t b4_1_lower; |
| 145 | + uint8_t b4_1_upper; |
| 146 | + uint8_t b4_2_lower; |
| 147 | + uint8_t b4_2_upper; |
| 148 | + uint8_t b4_3_lower; |
| 149 | + uint8_t b4_3_upper; |
| 150 | + uint8_t b4_4_lower; |
| 151 | + uint8_t b4_4_upper; |
| 152 | + |
| 153 | +} flb_mb_radix_tree; |
| 154 | + |
| 155 | +/* |
| 156 | + * UTF-8 to local code conversion map (for combined characters) |
| 157 | + */ |
| 158 | +typedef struct { |
| 159 | + uint32_t utf1; |
| 160 | + uint32_t utf2; |
| 161 | + uint32_t code; |
| 162 | +} flb_utf_to_local_combined; |
| 163 | + |
| 164 | +/* |
| 165 | + * local code to UTF-8 conversion map (for combined characters) |
| 166 | + */ |
| 167 | +typedef struct { |
| 168 | + uint32_t code; |
| 169 | + uint32_t utf1; |
| 170 | + uint32_t utf2; |
| 171 | +} flb_local_to_utf_combined; |
| 172 | + |
| 173 | +/* |
| 174 | + * @brief callback function for algorithmic encoding conversions (in either direction) |
| 175 | + * |
| 176 | + * if function returns zero, it does not know how to convert the code |
| 177 | + */ |
| 178 | +typedef uint32_t (*utf_local_conversion_func) (uint32_t code); |
| 179 | + |
| 180 | +extern void flb_encoding_set_invalid(int encoding, char *dst); |
| 181 | +extern int flb_encoding_mblen(int encoding, const char *mbstr); |
| 182 | +extern int flb_encoding_mblen_or_incomplete(int encoding, const char *mbstr, |
| 183 | + size_t remaining); |
| 184 | +extern int flb_encoding_mblen_bounded(int encoding, const char *mbstr); |
| 185 | +extern int flb_encoding_dsplen(int encoding, const char *mbstr); |
| 186 | +extern int flb_encoding_verifymbchar(int encoding, const char *mbstr, int len); |
| 187 | +extern int flb_encoding_verifymbstr(int encoding, const char *mbstr, int len); |
| 188 | +extern int flb_encoding_max_length(int encoding); |
| 189 | + |
| 190 | +extern bool flb_utf8_islegal(const unsigned char *source, int length); |
| 191 | +extern int flb_utf_mblen(const unsigned char *s); |
| 192 | + |
| 193 | +/* Those of converting functions is not public APIs in flb_conv.h. */ |
| 194 | +extern int flb_convert_to_local_internal(const unsigned char *utf, int len, |
| 195 | + unsigned char *iso, |
| 196 | + const flb_mb_radix_tree *map, |
| 197 | + const flb_utf_to_local_combined *cmap, int cmapsize, |
| 198 | + utf_local_conversion_func conv_func, |
| 199 | + int encoding, bool noError); |
| 200 | +extern int flb_convert_to_utf_internal(const unsigned char *iso, int len, |
| 201 | + unsigned char *utf, |
| 202 | + const flb_mb_radix_tree *map, |
| 203 | + const flb_local_to_utf_combined *cmap, int cmapsize, |
| 204 | + utf_local_conversion_func conv_func, |
| 205 | + int encoding, bool noError); |
| 206 | + |
| 207 | +extern bool flb_verifymbstr(const char *mbstr, int len, bool noError); |
| 208 | +extern bool flb_verify_mbstr(int encoding, const char *mbstr, int len, |
| 209 | + bool noError); |
| 210 | +extern int flb_verify_mbstr_len(int encoding, const char *mbstr, int len, |
| 211 | + bool noError); |
| 212 | + |
| 213 | +extern void flb_report_invalid_encoding(int encoding, const char *mbstr, int len); |
| 214 | +extern void flb_report_untranslatable_char(int src_encoding, int dest_encoding, |
| 215 | + const char *mbstr, int len); |
| 216 | + |
| 217 | +#endif /* FLB_WCHAR_H */ |
0 commit comments