[16da5f8e] | 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 |
|
---|
[2f57690] | 29 | /** @addtogroup generic
|
---|
[16da5f8e] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
[2f57690] | 35 | * @brief Miscellaneous functions.
|
---|
[16da5f8e] | 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 |
|
---|
[74c8da2c] | 45 | char invalch = '?';
|
---|
| 46 |
|
---|
[32704cb] | 47 | /** Byte mask consisting of lowest @n bits (out of eight). */
|
---|
[0dd1d444] | 48 | #define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
|
---|
| 49 |
|
---|
[32704cb] | 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 |
|
---|
[0dd1d444] | 56 | /** Number of data bits in a UTF-8 continuation byte. */
|
---|
| 57 | #define CONT_BITS 6
|
---|
| 58 |
|
---|
[e1813cf] | 59 | /** Decode a single character from a substring.
|
---|
[21a639b7] | 60 | *
|
---|
[e1813cf] | 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).
|
---|
[21a639b7] | 65 | *
|
---|
[e1813cf] | 66 | * @param str String (not necessarily NULL-terminated).
|
---|
[21a639b7] | 67 | * @param index Index (counted in plain characters) where to start
|
---|
| 68 | * the decoding.
|
---|
[e1813cf] | 69 | * @param limit Size of the substring.
|
---|
[21a639b7] | 70 | *
|
---|
[e1813cf] | 71 | * @return Value of decoded character or '?' on decoding error.
|
---|
[21a639b7] | 72 | *
|
---|
| 73 | */
|
---|
[e1813cf] | 74 | wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
|
---|
[21a639b7] | 75 | {
|
---|
[0dd1d444] | 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 |
|
---|
[e1813cf] | 82 | if (*offset + 1 > sz)
|
---|
[74c8da2c] | 83 | return invalch;
|
---|
[0dd1d444] | 84 |
|
---|
[e1813cf] | 85 | b0 = (uint8_t) str[(*offset)++];
|
---|
[0dd1d444] | 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;
|
---|
[74c8da2c] | 108 | }
|
---|
[0dd1d444] | 109 |
|
---|
[e1813cf] | 110 | if (*offset + cbytes > sz) {
|
---|
[0dd1d444] | 111 | return invalch;
|
---|
[74c8da2c] | 112 | }
|
---|
[0dd1d444] | 113 |
|
---|
| 114 | ch = b0 & LO_MASK_8(b0_bits);
|
---|
| 115 |
|
---|
| 116 | /* Decode continuation bytes. */
|
---|
| 117 | while (cbytes > 0) {
|
---|
[e1813cf] | 118 | b = (uint8_t) str[(*offset)++];
|
---|
[0dd1d444] | 119 |
|
---|
| 120 | /* Must be 10xxxxxx. */
|
---|
| 121 | if ((b & 0xc0) != 0x80) {
|
---|
[74c8da2c] | 122 | return invalch;
|
---|
[0dd1d444] | 123 | }
|
---|
| 124 |
|
---|
| 125 | /* Shift data bits to ch. */
|
---|
| 126 | ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
|
---|
| 127 | --cbytes;
|
---|
[74c8da2c] | 128 | }
|
---|
[0dd1d444] | 129 |
|
---|
| 130 | return ch;
|
---|
[74c8da2c] | 131 | }
|
---|
| 132 |
|
---|
[e1813cf] | 133 | /** Encode a single character to string representation.
|
---|
[74c8da2c] | 134 | *
|
---|
[e1813cf] | 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.
|
---|
[74c8da2c] | 138 | *
|
---|
[e1813cf] | 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.
|
---|
[74c8da2c] | 143 | *
|
---|
[e1813cf] | 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.
|
---|
[74c8da2c] | 147 | */
|
---|
[e1813cf] | 148 | bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
|
---|
[74c8da2c] | 149 | {
|
---|
[32704cb] | 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 |
|
---|
[e1813cf] | 156 | if (*offset >= sz)
|
---|
[74c8da2c] | 157 | return false;
|
---|
[32704cb] | 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;
|
---|
[74c8da2c] | 181 | }
|
---|
[32704cb] | 182 |
|
---|
| 183 | /* Check for available space in buffer. */
|
---|
[e1813cf] | 184 | if (*offset + cbytes >= sz)
|
---|
[32704cb] | 185 | return false;
|
---|
| 186 |
|
---|
| 187 | /* Encode continuation bytes. */
|
---|
| 188 | for (i = cbytes; i > 0; --i) {
|
---|
[e1813cf] | 189 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
|
---|
[32704cb] | 190 | cc = cc >> CONT_BITS;
|
---|
[74c8da2c] | 191 | }
|
---|
[32704cb] | 192 |
|
---|
| 193 | /* Encode first byte. */
|
---|
[e1813cf] | 194 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
|
---|
[32704cb] | 195 |
|
---|
[e1813cf] | 196 | /* Advance offset. */
|
---|
| 197 | *offset += (1 + cbytes);
|
---|
[74c8da2c] | 198 |
|
---|
[32704cb] | 199 | return true;
|
---|
[74c8da2c] | 200 | }
|
---|
| 201 |
|
---|
| 202 | /** Get bytes used by UTF-8 characters.
|
---|
| 203 | *
|
---|
| 204 | * Get the number of bytes (count of plain characters) which
|
---|
| 205 | * are used by a given count of UTF-8 characters in a string.
|
---|
| 206 | * As UTF-8 encoding is multibyte, there is no constant
|
---|
| 207 | * correspondence between number of characters and used bytes.
|
---|
| 208 | *
|
---|
| 209 | * @param str UTF-8 string to consider.
|
---|
| 210 | * @param count Number of UTF-8 characters to count.
|
---|
| 211 | *
|
---|
| 212 | * @return Number of bytes used by the characters.
|
---|
| 213 | *
|
---|
| 214 | */
|
---|
| 215 | size_t utf8_count_bytes(const char *str, count_t count)
|
---|
| 216 | {
|
---|
| 217 | size_t size = 0;
|
---|
| 218 | index_t index = 0;
|
---|
[b54d2f1] | 219 | index_t iprev;
|
---|
| 220 | wchar_t ch;
|
---|
[74c8da2c] | 221 |
|
---|
[b54d2f1] | 222 | while (true) {
|
---|
| 223 | iprev = index;
|
---|
| 224 | if (size >= count)
|
---|
| 225 | break;
|
---|
[e1813cf] | 226 | ch = chr_decode(str, &index, UTF8_NO_LIMIT);
|
---|
[b54d2f1] | 227 | if (ch == '\0') break;
|
---|
| 228 |
|
---|
[74c8da2c] | 229 | size++;
|
---|
[21a639b7] | 230 | }
|
---|
| 231 |
|
---|
[b54d2f1] | 232 | return iprev;
|
---|
[74c8da2c] | 233 | }
|
---|
| 234 |
|
---|
| 235 | /** Check whether character is plain ASCII.
|
---|
| 236 | *
|
---|
| 237 | * @return True if character is plain ASCII.
|
---|
| 238 | *
|
---|
| 239 | */
|
---|
| 240 | bool ascii_check(const wchar_t ch)
|
---|
| 241 | {
|
---|
| 242 | if ((ch >= 0) && (ch <= 127))
|
---|
| 243 | return true;
|
---|
| 244 |
|
---|
| 245 | return false;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | /** Check whether character is Unicode.
|
---|
| 249 | *
|
---|
| 250 | * @return True if character is valid Unicode code point.
|
---|
| 251 | *
|
---|
| 252 | */
|
---|
| 253 | bool unicode_check(const wchar_t ch)
|
---|
| 254 | {
|
---|
| 255 | if ((ch >= 0) && (ch <= 1114111))
|
---|
| 256 | return true;
|
---|
| 257 |
|
---|
| 258 | return false;
|
---|
[21a639b7] | 259 | }
|
---|
| 260 |
|
---|
[74c8da2c] | 261 | /** Return number of plain characters in a string.
|
---|
[16da5f8e] | 262 | *
|
---|
[74c8da2c] | 263 | * @param str NULL-terminated string.
|
---|
[16da5f8e] | 264 | *
|
---|
| 265 | * @return Number of characters in str.
|
---|
[2f57690] | 266 | *
|
---|
[16da5f8e] | 267 | */
|
---|
| 268 | size_t strlen(const char *str)
|
---|
| 269 | {
|
---|
[74c8da2c] | 270 | size_t size;
|
---|
| 271 | for (size = 0; str[size]; size++);
|
---|
| 272 |
|
---|
| 273 | return size;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /** Return number of UTF-8 characters in a string.
|
---|
| 277 | *
|
---|
| 278 | * @param str NULL-terminated UTF-8 string.
|
---|
| 279 | *
|
---|
| 280 | * @return Number of UTF-8 characters in str.
|
---|
| 281 | *
|
---|
| 282 | */
|
---|
| 283 | size_t strlen_utf8(const char *str)
|
---|
| 284 | {
|
---|
| 285 | size_t size = 0;
|
---|
| 286 | index_t index = 0;
|
---|
[16da5f8e] | 287 |
|
---|
[e1813cf] | 288 | while (chr_decode(str, &index, UTF8_NO_LIMIT) != 0) {
|
---|
[74c8da2c] | 289 | size++;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | return size;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | /** Return number of UTF-32 characters in a string.
|
---|
| 296 | *
|
---|
| 297 | * @param str NULL-terminated UTF-32 string.
|
---|
| 298 | *
|
---|
| 299 | * @return Number of UTF-32 characters in str.
|
---|
| 300 | *
|
---|
| 301 | */
|
---|
| 302 | size_t strlen_utf32(const wchar_t *str)
|
---|
| 303 | {
|
---|
| 304 | size_t size;
|
---|
| 305 | for (size = 0; str[size]; size++);
|
---|
[16da5f8e] | 306 |
|
---|
[74c8da2c] | 307 | return size;
|
---|
[16da5f8e] | 308 | }
|
---|
| 309 |
|
---|
| 310 | /** Compare two NULL terminated strings
|
---|
| 311 | *
|
---|
| 312 | * Do a char-by-char comparison of two NULL terminated strings.
|
---|
| 313 | * The strings are considered equal iff they consist of the same
|
---|
| 314 | * characters on the minimum of their lengths.
|
---|
| 315 | *
|
---|
| 316 | * @param src First string to compare.
|
---|
| 317 | * @param dst Second string to compare.
|
---|
| 318 | *
|
---|
| 319 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
|
---|
| 320 | *
|
---|
| 321 | */
|
---|
| 322 | int strcmp(const char *src, const char *dst)
|
---|
| 323 | {
|
---|
| 324 | for (; *src && *dst; src++, dst++) {
|
---|
| 325 | if (*src < *dst)
|
---|
| 326 | return -1;
|
---|
| 327 | if (*src > *dst)
|
---|
| 328 | return 1;
|
---|
| 329 | }
|
---|
| 330 | if (*src == *dst)
|
---|
| 331 | return 0;
|
---|
[2f57690] | 332 |
|
---|
[16da5f8e] | 333 | if (!*src)
|
---|
| 334 | return -1;
|
---|
[2f57690] | 335 |
|
---|
[16da5f8e] | 336 | return 1;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 |
|
---|
| 340 | /** Compare two NULL terminated strings
|
---|
| 341 | *
|
---|
| 342 | * Do a char-by-char comparison of two NULL terminated strings.
|
---|
| 343 | * The strings are considered equal iff they consist of the same
|
---|
| 344 | * characters on the minimum of their lengths and specified maximal
|
---|
| 345 | * length.
|
---|
| 346 | *
|
---|
| 347 | * @param src First string to compare.
|
---|
| 348 | * @param dst Second string to compare.
|
---|
| 349 | * @param len Maximal length for comparison.
|
---|
| 350 | *
|
---|
| 351 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
|
---|
| 352 | *
|
---|
| 353 | */
|
---|
| 354 | int strncmp(const char *src, const char *dst, size_t len)
|
---|
| 355 | {
|
---|
| 356 | unsigned int i;
|
---|
| 357 |
|
---|
| 358 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
|
---|
| 359 | if (*src < *dst)
|
---|
| 360 | return -1;
|
---|
[2f57690] | 361 |
|
---|
[16da5f8e] | 362 | if (*src > *dst)
|
---|
| 363 | return 1;
|
---|
| 364 | }
|
---|
[2f57690] | 365 |
|
---|
[16da5f8e] | 366 | if (i == len || *src == *dst)
|
---|
| 367 | return 0;
|
---|
[2f57690] | 368 |
|
---|
[16da5f8e] | 369 | if (!*src)
|
---|
| 370 | return -1;
|
---|
[2f57690] | 371 |
|
---|
[16da5f8e] | 372 | return 1;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 |
|
---|
| 376 |
|
---|
| 377 | /** Copy NULL terminated string.
|
---|
| 378 | *
|
---|
| 379 | * Copy at most 'len' characters from string 'src' to 'dest'.
|
---|
| 380 | * If 'src' is shorter than 'len', '\0' is inserted behind the
|
---|
| 381 | * last copied character.
|
---|
| 382 | *
|
---|
[2f57690] | 383 | * @param src Source string.
|
---|
[16da5f8e] | 384 | * @param dest Destination buffer.
|
---|
[2f57690] | 385 | * @param len Size of destination buffer.
|
---|
| 386 | *
|
---|
[16da5f8e] | 387 | */
|
---|
| 388 | void strncpy(char *dest, const char *src, size_t len)
|
---|
| 389 | {
|
---|
| 390 | unsigned int i;
|
---|
[2f57690] | 391 |
|
---|
[16da5f8e] | 392 | for (i = 0; i < len; i++) {
|
---|
| 393 | if (!(dest[i] = src[i]))
|
---|
| 394 | return;
|
---|
| 395 | }
|
---|
[2f57690] | 396 |
|
---|
[16da5f8e] | 397 | dest[i - 1] = '\0';
|
---|
| 398 | }
|
---|
| 399 |
|
---|
[20f1597] | 400 | /** Find first occurence of character in string.
|
---|
| 401 | *
|
---|
[2f57690] | 402 | * @param s String to search.
|
---|
| 403 | * @param i Character to look for.
|
---|
[20f1597] | 404 | *
|
---|
[2f57690] | 405 | * @return Pointer to character in @a s or NULL if not found.
|
---|
[20f1597] | 406 | */
|
---|
| 407 | extern char *strchr(const char *s, int i)
|
---|
| 408 | {
|
---|
| 409 | while (*s != '\0') {
|
---|
[2f57690] | 410 | if (*s == i)
|
---|
| 411 | return (char *) s;
|
---|
[20f1597] | 412 | ++s;
|
---|
| 413 | }
|
---|
[2f57690] | 414 |
|
---|
[20f1597] | 415 | return NULL;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[16da5f8e] | 418 | /** @}
|
---|
| 419 | */
|
---|