Subversion Repositories HelenOS

Rev

Rev 4201 | Rev 4296 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  29. /** @addtogroup generic
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  * @brief Miscellaneous functions.
  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.  
  45. char invalch = '?';
  46.  
  47. /** Byte mask consisting of lowest @n bits (out of eight). */
  48. #define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
  49.  
  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.  
  56. /** Number of data bits in a UTF-8 continuation byte. */
  57. #define CONT_BITS 6
  58.  
  59. /** Decode a single character from a substring.
  60.  *
  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).
  65.  *
  66.  * @param str   String (not necessarily NULL-terminated).
  67.  * @param index Index (counted in plain characters) where to start
  68.  *              the decoding.
  69.  * @param limit Size of the substring.
  70.  *
  71.  * @return  Value of decoded character or '?' on decoding error.
  72.  *
  73.  */
  74. wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
  75. {
  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.  
  82.     if (*offset + 1 > sz)
  83.         return invalch;
  84.  
  85.     b0 = (uint8_t) str[(*offset)++];
  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;
  108.     }
  109.  
  110.     if (*offset + cbytes > sz) {
  111.         return invalch;
  112.     }
  113.  
  114.     ch = b0 & LO_MASK_8(b0_bits);
  115.  
  116.     /* Decode continuation bytes. */
  117.     while (cbytes > 0) {
  118.         b = (uint8_t) str[(*offset)++];
  119.  
  120.         /* Must be 10xxxxxx. */
  121.         if ((b & 0xc0) != 0x80) {
  122.             return invalch;
  123.         }
  124.  
  125.         /* Shift data bits to ch. */
  126.         ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
  127.         --cbytes;
  128.     }
  129.  
  130.     return ch;
  131. }
  132.  
  133. /** Encode a single character to string representation.
  134.  *
  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.
  138.  *
  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.
  143.  *
  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.
  147.  */
  148. bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
  149. {
  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.  
  156.     if (*offset >= sz)
  157.         return false;
  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;
  181.     }
  182.  
  183.     /* Check for available space in buffer. */
  184.     if (*offset + cbytes >= sz)
  185.         return false;
  186.  
  187.     /* Encode continuation bytes. */
  188.     for (i = cbytes; i > 0; --i) {
  189.         str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
  190.         cc = cc >> CONT_BITS;
  191.     }
  192.  
  193.     /* Encode first byte. */
  194.     str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
  195.  
  196.     /* Advance offset. */
  197.     *offset += (1 + cbytes);
  198.    
  199.     return true;
  200. }
  201.  
  202. /** Get size of string, with length limit.
  203.  *
  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.
  207.  *
  208.  * @param str   String to consider.
  209.  * @param count Maximum number of characters to measure.
  210.  *
  211.  * @return Number of bytes used by the characters.
  212.  */
  213. size_t str_lsize(const char *str, count_t max_len)
  214. {
  215.     count_t len = 0;
  216.     size_t cur = 0;
  217.     size_t prev;
  218.     wchar_t ch;
  219.  
  220.     while (true) {
  221.         prev = cur;
  222.         if (len >= max_len)
  223.             break;
  224.         ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
  225.         if (ch == '\0') break;
  226.  
  227.         len++;
  228.     }
  229.  
  230.     return prev;
  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.  *
  263.  * @return Number of characters in @a str.
  264.  *
  265.  */
  266. size_t strlen(const char *str)
  267. {
  268.     size_t size;
  269.     for (size = 0; str[size]; size++);
  270.    
  271.     return size;
  272. }
  273.  
  274. /** Return number of characters in a string.
  275.  *
  276.  * @param str NULL-terminated string.
  277.  * @return Number of characters in string.
  278.  */
  279. count_t str_length(const char *str)
  280. {
  281.     count_t len = 0;
  282.     size_t offset = 0;
  283.  
  284.     while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
  285.         len++;
  286.     }
  287.  
  288.     return len;
  289. }
  290.  
  291. /** Return number of characters in a wide string.
  292.  *
  293.  * @param str NULL-terminated wide string.
  294.  * @return Number of characters in @a str.
  295.  */
  296. count_t wstr_length(const wchar_t *wstr)
  297. {
  298.     count_t len;
  299.  
  300.     len = 0;
  301.     while (*wstr++ != '\0')
  302.         ++len;
  303.  
  304.     return len;
  305. }
  306.  
  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;
  329.    
  330.     if (!*src)
  331.         return -1;
  332.    
  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;
  358.        
  359.         if (*src > *dst)
  360.             return 1;
  361.     }
  362.    
  363.     if (i == len || *src == *dst)
  364.         return 0;
  365.    
  366.     if (!*src)
  367.         return -1;
  368.    
  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.  *
  380.  * @param src  Source string.
  381.  * @param dest Destination buffer.
  382.  * @param len  Size of destination buffer.
  383.  *
  384.  */
  385. void strncpy(char *dest, const char *src, size_t len)
  386. {
  387.     unsigned int i;
  388.    
  389.     for (i = 0; i < len; i++) {
  390.         if (!(dest[i] = src[i]))
  391.             return;
  392.     }
  393.    
  394.     dest[i - 1] = '\0';
  395. }
  396.  
  397. /** Find first occurence of character in string.
  398.  *
  399.  * @param s String to search.
  400.  * @param i Character to look for.
  401.  *
  402.  * @return Pointer to character in @a s or NULL if not found.
  403.  */
  404. extern char *strchr(const char *s, int i)
  405. {
  406.     while (*s != '\0') {
  407.         if (*s == i)
  408.             return (char *) s;
  409.         ++s;
  410.     }
  411.    
  412.     return NULL;
  413. }
  414.  
  415. /** @}
  416.  */
  417.