a_utf8.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include "internal/cryptlib.h"
  59. #include <openssl/asn1.h>
  60. /* UTF8 utilities */
  61. /*-
  62. * This parses a UTF8 string one character at a time. It is passed a pointer
  63. * to the string and the length of the string. It sets 'value' to the value of
  64. * the current character. It returns the number of characters read or a
  65. * negative error code:
  66. * -1 = string too short
  67. * -2 = illegal character
  68. * -3 = subsequent characters not of the form 10xxxxxx
  69. * -4 = character encoded incorrectly (not minimal length).
  70. */
  71. int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
  72. {
  73. const unsigned char *p;
  74. unsigned long value;
  75. int ret;
  76. if (len <= 0)
  77. return 0;
  78. p = str;
  79. /* Check syntax and work out the encoded value (if correct) */
  80. if ((*p & 0x80) == 0) {
  81. value = *p++ & 0x7f;
  82. ret = 1;
  83. } else if ((*p & 0xe0) == 0xc0) {
  84. if (len < 2)
  85. return -1;
  86. if ((p[1] & 0xc0) != 0x80)
  87. return -3;
  88. value = (*p++ & 0x1f) << 6;
  89. value |= *p++ & 0x3f;
  90. if (value < 0x80)
  91. return -4;
  92. ret = 2;
  93. } else if ((*p & 0xf0) == 0xe0) {
  94. if (len < 3)
  95. return -1;
  96. if (((p[1] & 0xc0) != 0x80)
  97. || ((p[2] & 0xc0) != 0x80))
  98. return -3;
  99. value = (*p++ & 0xf) << 12;
  100. value |= (*p++ & 0x3f) << 6;
  101. value |= *p++ & 0x3f;
  102. if (value < 0x800)
  103. return -4;
  104. ret = 3;
  105. } else if ((*p & 0xf8) == 0xf0) {
  106. if (len < 4)
  107. return -1;
  108. if (((p[1] & 0xc0) != 0x80)
  109. || ((p[2] & 0xc0) != 0x80)
  110. || ((p[3] & 0xc0) != 0x80))
  111. return -3;
  112. value = ((unsigned long)(*p++ & 0x7)) << 18;
  113. value |= (*p++ & 0x3f) << 12;
  114. value |= (*p++ & 0x3f) << 6;
  115. value |= *p++ & 0x3f;
  116. if (value < 0x10000)
  117. return -4;
  118. ret = 4;
  119. } else if ((*p & 0xfc) == 0xf8) {
  120. if (len < 5)
  121. return -1;
  122. if (((p[1] & 0xc0) != 0x80)
  123. || ((p[2] & 0xc0) != 0x80)
  124. || ((p[3] & 0xc0) != 0x80)
  125. || ((p[4] & 0xc0) != 0x80))
  126. return -3;
  127. value = ((unsigned long)(*p++ & 0x3)) << 24;
  128. value |= ((unsigned long)(*p++ & 0x3f)) << 18;
  129. value |= ((unsigned long)(*p++ & 0x3f)) << 12;
  130. value |= (*p++ & 0x3f) << 6;
  131. value |= *p++ & 0x3f;
  132. if (value < 0x200000)
  133. return -4;
  134. ret = 5;
  135. } else if ((*p & 0xfe) == 0xfc) {
  136. if (len < 6)
  137. return -1;
  138. if (((p[1] & 0xc0) != 0x80)
  139. || ((p[2] & 0xc0) != 0x80)
  140. || ((p[3] & 0xc0) != 0x80)
  141. || ((p[4] & 0xc0) != 0x80)
  142. || ((p[5] & 0xc0) != 0x80))
  143. return -3;
  144. value = ((unsigned long)(*p++ & 0x1)) << 30;
  145. value |= ((unsigned long)(*p++ & 0x3f)) << 24;
  146. value |= ((unsigned long)(*p++ & 0x3f)) << 18;
  147. value |= ((unsigned long)(*p++ & 0x3f)) << 12;
  148. value |= (*p++ & 0x3f) << 6;
  149. value |= *p++ & 0x3f;
  150. if (value < 0x4000000)
  151. return -4;
  152. ret = 6;
  153. } else
  154. return -2;
  155. *val = value;
  156. return ret;
  157. }
  158. /*
  159. * This takes a character 'value' and writes the UTF8 encoded value in 'str'
  160. * where 'str' is a buffer containing 'len' characters. Returns the number of
  161. * characters written or -1 if 'len' is too small. 'str' can be set to NULL
  162. * in which case it just returns the number of characters. It will need at
  163. * most 6 characters.
  164. */
  165. int UTF8_putc(unsigned char *str, int len, unsigned long value)
  166. {
  167. if (!str)
  168. len = 6; /* Maximum we will need */
  169. else if (len <= 0)
  170. return -1;
  171. if (value < 0x80) {
  172. if (str)
  173. *str = (unsigned char)value;
  174. return 1;
  175. }
  176. if (value < 0x800) {
  177. if (len < 2)
  178. return -1;
  179. if (str) {
  180. *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
  181. *str = (unsigned char)((value & 0x3f) | 0x80);
  182. }
  183. return 2;
  184. }
  185. if (value < 0x10000) {
  186. if (len < 3)
  187. return -1;
  188. if (str) {
  189. *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
  190. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  191. *str = (unsigned char)((value & 0x3f) | 0x80);
  192. }
  193. return 3;
  194. }
  195. if (value < 0x200000) {
  196. if (len < 4)
  197. return -1;
  198. if (str) {
  199. *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
  200. *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
  201. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  202. *str = (unsigned char)((value & 0x3f) | 0x80);
  203. }
  204. return 4;
  205. }
  206. if (value < 0x4000000) {
  207. if (len < 5)
  208. return -1;
  209. if (str) {
  210. *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
  211. *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
  212. *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
  213. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  214. *str = (unsigned char)((value & 0x3f) | 0x80);
  215. }
  216. return 5;
  217. }
  218. if (len < 6)
  219. return -1;
  220. if (str) {
  221. *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
  222. *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
  223. *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
  224. *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
  225. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  226. *str = (unsigned char)((value & 0x3f) | 0x80);
  227. }
  228. return 6;
  229. }