a_utf8.c 8.7 KB

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