Rev 4200 | Rev 4208 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4011 | svoboda | 1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
4014 | decky | 29 | /** @addtogroup generic |
4011 | svoboda | 30 | * @{ |
31 | */ |
||
32 | |||
33 | /** |
||
34 | * @file |
||
4014 | decky | 35 | * @brief Miscellaneous functions. |
4011 | svoboda | 36 | */ |
37 | |||
38 | #include <string.h> |
||
39 | #include <print.h> |
||
40 | #include <cpu.h> |
||
41 | #include <arch/asm.h> |
||
42 | #include <arch.h> |
||
43 | #include <console/kconsole.h> |
||
44 | |||
4179 | decky | 45 | char invalch = '?'; |
46 | |||
4198 | svoboda | 47 | /** Byte mask consisting of lowest @n bits (out of eight). */ |
4196 | svoboda | 48 | #define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1)) |
49 | |||
4198 | svoboda | 50 | /** Byte mask consisting of lowest @n bits (out of 32). */ |
51 | #define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1)) |
||
52 | |||
53 | /** Byte mask consisting of highest @n bits (out of eight). */ |
||
54 | #define HI_MASK_8(n) (~LO_MASK_8(8 - (n))) |
||
55 | |||
4196 | svoboda | 56 | /** Number of data bits in a UTF-8 continuation byte. */ |
57 | #define CONT_BITS 6 |
||
58 | |||
4200 | svoboda | 59 | /** Decode a single character from a substring. |
4175 | decky | 60 | * |
4200 | svoboda | 61 | * Decode a single character from a substring of size @a sz. Decoding starts |
62 | * at @a offset and this offset is moved to the beginning of the next |
||
63 | * character. In case of decoding error, offset generally advances at least |
||
64 | * by one. However, offset is never moved beyond (str + sz). |
||
4175 | decky | 65 | * |
4200 | svoboda | 66 | * @param str String (not necessarily NULL-terminated). |
4175 | decky | 67 | * @param index Index (counted in plain characters) where to start |
68 | * the decoding. |
||
4200 | svoboda | 69 | * @param limit Size of the substring. |
4175 | decky | 70 | * |
4200 | svoboda | 71 | * @return Value of decoded character or '?' on decoding error. |
4175 | decky | 72 | * |
73 | */ |
||
4200 | svoboda | 74 | wchar_t chr_decode(const char *str, size_t *offset, size_t sz) |
4175 | decky | 75 | { |
4196 | svoboda | 76 | uint8_t b0, b; /* Bytes read from str. */ |
77 | wchar_t ch; |
||
78 | |||
79 | int b0_bits; /* Data bits in first byte. */ |
||
80 | int cbytes; /* Number of continuation bytes. */ |
||
81 | |||
4200 | svoboda | 82 | if (*offset + 1 > sz) |
4179 | decky | 83 | return invalch; |
4196 | svoboda | 84 | |
4200 | svoboda | 85 | b0 = (uint8_t) str[(*offset)++]; |
4196 | svoboda | 86 | |
87 | /* Determine code length. */ |
||
88 | |||
89 | if ((b0 & 0x80) == 0) { |
||
90 | /* 0xxxxxxx (Plain ASCII) */ |
||
91 | b0_bits = 7; |
||
92 | cbytes = 0; |
||
93 | } else if ((b0 & 0xe0) == 0xc0) { |
||
94 | /* 110xxxxx 10xxxxxx */ |
||
95 | b0_bits = 5; |
||
96 | cbytes = 1; |
||
97 | } else if ((b0 & 0xf0) == 0xe0) { |
||
98 | /* 1110xxxx 10xxxxxx 10xxxxxx */ |
||
99 | b0_bits = 4; |
||
100 | cbytes = 2; |
||
101 | } else if ((b0 & 0xf8) == 0xf0) { |
||
102 | /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ |
||
103 | b0_bits = 3; |
||
104 | cbytes = 3; |
||
105 | } else { |
||
106 | /* 10xxxxxx -- unexpected continuation byte. */ |
||
107 | return invalch; |
||
4179 | decky | 108 | } |
4196 | svoboda | 109 | |
4200 | svoboda | 110 | if (*offset + cbytes > sz) { |
4196 | svoboda | 111 | return invalch; |
4179 | decky | 112 | } |
4196 | svoboda | 113 | |
114 | ch = b0 & LO_MASK_8(b0_bits); |
||
115 | |||
116 | /* Decode continuation bytes. */ |
||
117 | while (cbytes > 0) { |
||
4200 | svoboda | 118 | b = (uint8_t) str[(*offset)++]; |
4196 | svoboda | 119 | |
120 | /* Must be 10xxxxxx. */ |
||
121 | if ((b & 0xc0) != 0x80) { |
||
4179 | decky | 122 | return invalch; |
4196 | svoboda | 123 | } |
124 | |||
125 | /* Shift data bits to ch. */ |
||
126 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); |
||
127 | --cbytes; |
||
4179 | decky | 128 | } |
4196 | svoboda | 129 | |
130 | return ch; |
||
4175 | decky | 131 | } |
132 | |||
4200 | svoboda | 133 | /** Encode a single character to string representation. |
4011 | svoboda | 134 | * |
4200 | svoboda | 135 | * Encode a single character to string representation (i.e. UTF-8) and store |
136 | * it into a buffer at @a offset. Encoding starts at @a offset and this offset |
||
137 | * is moved to the position where the next character can be written to. |
||
4011 | svoboda | 138 | * |
4200 | svoboda | 139 | * @param ch Input character. |
140 | * @param str Output buffer. |
||
141 | * @param offset Offset (in bytes) where to start writing. |
||
142 | * @param sz Size of the output buffer. |
||
4179 | decky | 143 | * |
4200 | svoboda | 144 | * @return True if the character was encoded successfully or false if there |
145 | * was not enough space in the output buffer or the character code |
||
146 | * was invalid. |
||
4179 | decky | 147 | */ |
4200 | svoboda | 148 | bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz) |
4179 | decky | 149 | { |
4198 | svoboda | 150 | uint32_t cc; /* Unsigned version of ch. */ |
151 | |||
152 | int cbytes; /* Number of continuation bytes. */ |
||
153 | int b0_bits; /* Number of data bits in first byte. */ |
||
154 | int i; |
||
155 | |||
4200 | svoboda | 156 | if (*offset >= sz) |
4179 | decky | 157 | return false; |
4198 | svoboda | 158 | |
159 | if (ch < 0) |
||
160 | return false; |
||
161 | |||
162 | /* Bit operations should only be done on unsigned numbers. */ |
||
163 | cc = (uint32_t) ch; |
||
164 | |||
165 | /* Determine how many continuation bytes are needed. */ |
||
166 | if ((cc & ~LO_MASK_32(7)) == 0) { |
||
167 | b0_bits = 7; |
||
168 | cbytes = 0; |
||
169 | } else if ((cc & ~LO_MASK_32(11)) == 0) { |
||
170 | b0_bits = 5; |
||
171 | cbytes = 1; |
||
172 | } else if ((cc & ~LO_MASK_32(16)) == 0) { |
||
173 | b0_bits = 4; |
||
174 | cbytes = 2; |
||
175 | } else if ((cc & ~LO_MASK_32(21)) == 0) { |
||
176 | b0_bits = 3; |
||
177 | cbytes = 3; |
||
178 | } else { |
||
179 | /* Codes longer than 21 bits are not supported. */ |
||
180 | return false; |
||
4179 | decky | 181 | } |
4198 | svoboda | 182 | |
183 | /* Check for available space in buffer. */ |
||
4200 | svoboda | 184 | if (*offset + cbytes >= sz) |
4198 | svoboda | 185 | return false; |
186 | |||
187 | /* Encode continuation bytes. */ |
||
188 | for (i = cbytes; i > 0; --i) { |
||
4200 | svoboda | 189 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
4198 | svoboda | 190 | cc = cc >> CONT_BITS; |
4179 | decky | 191 | } |
4198 | svoboda | 192 | |
193 | /* Encode first byte. */ |
||
4200 | svoboda | 194 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
4198 | svoboda | 195 | |
4200 | svoboda | 196 | /* Advance offset. */ |
197 | *offset += (1 + cbytes); |
||
4179 | decky | 198 | |
4198 | svoboda | 199 | return true; |
4179 | decky | 200 | } |
201 | |||
4205 | svoboda | 202 | /** Get size of string, with length limit. |
4179 | decky | 203 | * |
4205 | svoboda | 204 | * Get the number of bytes which are used by up to @a max_len first |
205 | * characters in the string @a str. If @a max_len is greater than |
||
206 | * the length of @a str, the entire string is measured. |
||
4179 | decky | 207 | * |
4205 | svoboda | 208 | * @param str String to consider. |
209 | * @param count Maximum number of characters to measure. |
||
4179 | decky | 210 | * |
211 | * @return Number of bytes used by the characters. |
||
212 | */ |
||
4205 | svoboda | 213 | size_t str_lsize(const char *str, count_t max_len) |
4179 | decky | 214 | { |
4205 | svoboda | 215 | count_t len = 0; |
216 | size_t cur = 0; |
||
217 | size_t prev; |
||
4199 | svoboda | 218 | wchar_t ch; |
4205 | svoboda | 219 | |
4199 | svoboda | 220 | while (true) { |
4205 | svoboda | 221 | prev = cur; |
222 | if (len >= max_len) |
||
4199 | svoboda | 223 | break; |
4205 | svoboda | 224 | ch = chr_decode(str, &cur, UTF8_NO_LIMIT); |
4199 | svoboda | 225 | if (ch == '\0') break; |
226 | |||
4205 | svoboda | 227 | len++; |
4179 | decky | 228 | } |
4205 | svoboda | 229 | |
230 | return prev; |
||
4179 | decky | 231 | } |
232 | |||
233 | /** Check whether character is plain ASCII. |
||
234 | * |
||
235 | * @return True if character is plain ASCII. |
||
236 | * |
||
237 | */ |
||
238 | bool ascii_check(const wchar_t ch) |
||
239 | { |
||
240 | if ((ch >= 0) && (ch <= 127)) |
||
241 | return true; |
||
242 | |||
243 | return false; |
||
244 | } |
||
245 | |||
246 | /** Check whether character is Unicode. |
||
247 | * |
||
248 | * @return True if character is valid Unicode code point. |
||
249 | * |
||
250 | */ |
||
251 | bool unicode_check(const wchar_t ch) |
||
252 | { |
||
253 | if ((ch >= 0) && (ch <= 1114111)) |
||
254 | return true; |
||
255 | |||
256 | return false; |
||
257 | } |
||
258 | |||
259 | /** Return number of plain characters in a string. |
||
260 | * |
||
261 | * @param str NULL-terminated string. |
||
262 | * |
||
4205 | svoboda | 263 | * @return Number of characters in @a str. |
4014 | decky | 264 | * |
4011 | svoboda | 265 | */ |
266 | size_t strlen(const char *str) |
||
267 | { |
||
4179 | decky | 268 | size_t size; |
269 | for (size = 0; str[size]; size++); |
||
4011 | svoboda | 270 | |
4179 | decky | 271 | return size; |
272 | } |
||
273 | |||
4205 | svoboda | 274 | /** Return number of characters in a string. |
4179 | decky | 275 | * |
4205 | svoboda | 276 | * @param str NULL-terminated string. |
277 | * @return Number of characters in string. |
||
4179 | decky | 278 | */ |
4205 | svoboda | 279 | count_t str_length(const char *str) |
4179 | decky | 280 | { |
4205 | svoboda | 281 | count_t len = 0; |
282 | size_t offset = 0; |
||
283 | |||
284 | while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) { |
||
285 | len++; |
||
4179 | decky | 286 | } |
4205 | svoboda | 287 | |
288 | return len; |
||
4011 | svoboda | 289 | } |
290 | |||
4205 | svoboda | 291 | /** Return number of characters in a wide string. |
4179 | decky | 292 | * |
4205 | svoboda | 293 | * @param str NULL-terminated wide string. |
294 | * @return Number of characters in @a str. |
||
4179 | decky | 295 | */ |
4205 | svoboda | 296 | count_t wstr_length(const wchar_t *wstr) |
4179 | decky | 297 | { |
4205 | svoboda | 298 | count_t len; |
299 | |||
300 | len = 0; |
||
301 | while (*wstr++ != '\0') |
||
302 | ++len; |
||
303 | |||
304 | return len; |
||
4179 | decky | 305 | } |
306 | |||
4011 | svoboda | 307 | /** Compare two NULL terminated strings |
308 | * |
||
309 | * Do a char-by-char comparison of two NULL terminated strings. |
||
310 | * The strings are considered equal iff they consist of the same |
||
311 | * characters on the minimum of their lengths. |
||
312 | * |
||
313 | * @param src First string to compare. |
||
314 | * @param dst Second string to compare. |
||
315 | * |
||
316 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
||
317 | * |
||
318 | */ |
||
319 | int strcmp(const char *src, const char *dst) |
||
320 | { |
||
321 | for (; *src && *dst; src++, dst++) { |
||
322 | if (*src < *dst) |
||
323 | return -1; |
||
324 | if (*src > *dst) |
||
325 | return 1; |
||
326 | } |
||
327 | if (*src == *dst) |
||
328 | return 0; |
||
4014 | decky | 329 | |
4011 | svoboda | 330 | if (!*src) |
331 | return -1; |
||
4014 | decky | 332 | |
4011 | svoboda | 333 | return 1; |
334 | } |
||
335 | |||
336 | |||
337 | /** Compare two NULL terminated strings |
||
338 | * |
||
339 | * Do a char-by-char comparison of two NULL terminated strings. |
||
340 | * The strings are considered equal iff they consist of the same |
||
341 | * characters on the minimum of their lengths and specified maximal |
||
342 | * length. |
||
343 | * |
||
344 | * @param src First string to compare. |
||
345 | * @param dst Second string to compare. |
||
346 | * @param len Maximal length for comparison. |
||
347 | * |
||
348 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller. |
||
349 | * |
||
350 | */ |
||
351 | int strncmp(const char *src, const char *dst, size_t len) |
||
352 | { |
||
353 | unsigned int i; |
||
354 | |||
355 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) { |
||
356 | if (*src < *dst) |
||
357 | return -1; |
||
4014 | decky | 358 | |
4011 | svoboda | 359 | if (*src > *dst) |
360 | return 1; |
||
361 | } |
||
4014 | decky | 362 | |
4011 | svoboda | 363 | if (i == len || *src == *dst) |
364 | return 0; |
||
4014 | decky | 365 | |
4011 | svoboda | 366 | if (!*src) |
367 | return -1; |
||
4014 | decky | 368 | |
4011 | svoboda | 369 | return 1; |
370 | } |
||
371 | |||
372 | |||
373 | |||
374 | /** Copy NULL terminated string. |
||
375 | * |
||
376 | * Copy at most 'len' characters from string 'src' to 'dest'. |
||
377 | * If 'src' is shorter than 'len', '\0' is inserted behind the |
||
378 | * last copied character. |
||
379 | * |
||
4014 | decky | 380 | * @param src Source string. |
4011 | svoboda | 381 | * @param dest Destination buffer. |
4014 | decky | 382 | * @param len Size of destination buffer. |
383 | * |
||
4011 | svoboda | 384 | */ |
385 | void strncpy(char *dest, const char *src, size_t len) |
||
386 | { |
||
387 | unsigned int i; |
||
4014 | decky | 388 | |
4011 | svoboda | 389 | for (i = 0; i < len; i++) { |
390 | if (!(dest[i] = src[i])) |
||
391 | return; |
||
392 | } |
||
4014 | decky | 393 | |
4011 | svoboda | 394 | dest[i - 1] = '\0'; |
395 | } |
||
396 | |||
4012 | svoboda | 397 | /** Find first occurence of character in string. |
398 | * |
||
4014 | decky | 399 | * @param s String to search. |
400 | * @param i Character to look for. |
||
4012 | svoboda | 401 | * |
4014 | decky | 402 | * @return Pointer to character in @a s or NULL if not found. |
4012 | svoboda | 403 | */ |
404 | extern char *strchr(const char *s, int i) |
||
405 | { |
||
406 | while (*s != '\0') { |
||
4014 | decky | 407 | if (*s == i) |
408 | return (char *) s; |
||
4012 | svoboda | 409 | ++s; |
410 | } |
||
4014 | decky | 411 | |
4012 | svoboda | 412 | return NULL; |
413 | } |
||
414 | |||
4011 | svoboda | 415 | /** @} |
416 | */ |