2
0

a_utf8.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /* 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) return 0;
  77. p = str;
  78. /* Check syntax and work out the encoded value (if correct) */
  79. if((*p & 0x80) == 0) {
  80. value = *p++ & 0x7f;
  81. ret = 1;
  82. } else if((*p & 0xe0) == 0xc0) {
  83. if(len < 2) return -1;
  84. if((p[1] & 0xc0) != 0x80) return -3;
  85. value = (*p++ & 0x1f) << 6;
  86. value |= *p++ & 0x3f;
  87. if(value < 0x80) return -4;
  88. ret = 2;
  89. } else if((*p & 0xf0) == 0xe0) {
  90. if(len < 3) return -1;
  91. if( ((p[1] & 0xc0) != 0x80)
  92. || ((p[2] & 0xc0) != 0x80) ) return -3;
  93. value = (*p++ & 0xf) << 12;
  94. value |= (*p++ & 0x3f) << 6;
  95. value |= *p++ & 0x3f;
  96. if(value < 0x800) return -4;
  97. ret = 3;
  98. } else if((*p & 0xf8) == 0xf0) {
  99. if(len < 4) return -1;
  100. if( ((p[1] & 0xc0) != 0x80)
  101. || ((p[2] & 0xc0) != 0x80)
  102. || ((p[3] & 0xc0) != 0x80) ) return -3;
  103. value = ((unsigned long)(*p++ & 0x7)) << 18;
  104. value |= (*p++ & 0x3f) << 12;
  105. value |= (*p++ & 0x3f) << 6;
  106. value |= *p++ & 0x3f;
  107. if(value < 0x10000) return -4;
  108. ret = 4;
  109. } else if((*p & 0xfc) == 0xf8) {
  110. if(len < 5) return -1;
  111. if( ((p[1] & 0xc0) != 0x80)
  112. || ((p[2] & 0xc0) != 0x80)
  113. || ((p[3] & 0xc0) != 0x80)
  114. || ((p[4] & 0xc0) != 0x80) ) return -3;
  115. value = ((unsigned long)(*p++ & 0x3)) << 24;
  116. value |= ((unsigned long)(*p++ & 0x3f)) << 18;
  117. value |= ((unsigned long)(*p++ & 0x3f)) << 12;
  118. value |= (*p++ & 0x3f) << 6;
  119. value |= *p++ & 0x3f;
  120. if(value < 0x200000) return -4;
  121. ret = 5;
  122. } else if((*p & 0xfe) == 0xfc) {
  123. if(len < 6) return -1;
  124. if( ((p[1] & 0xc0) != 0x80)
  125. || ((p[2] & 0xc0) != 0x80)
  126. || ((p[3] & 0xc0) != 0x80)
  127. || ((p[4] & 0xc0) != 0x80)
  128. || ((p[5] & 0xc0) != 0x80) ) return -3;
  129. value = ((unsigned long)(*p++ & 0x1)) << 30;
  130. value |= ((unsigned long)(*p++ & 0x3f)) << 24;
  131. value |= ((unsigned long)(*p++ & 0x3f)) << 18;
  132. value |= ((unsigned long)(*p++ & 0x3f)) << 12;
  133. value |= (*p++ & 0x3f) << 6;
  134. value |= *p++ & 0x3f;
  135. if(value < 0x4000000) return -4;
  136. ret = 6;
  137. } else return -2;
  138. *val = value;
  139. return ret;
  140. }
  141. /* This takes a character 'value' and writes the UTF8 encoded value in
  142. * 'str' where 'str' is a buffer containing 'len' characters. Returns
  143. * the number of characters written or -1 if 'len' is too small. 'str' can
  144. * be set to NULL in which case it just returns the number of characters.
  145. * It will need at most 6 characters.
  146. */
  147. int UTF8_putc(unsigned char *str, int len, unsigned long value)
  148. {
  149. if(!str) len = 6; /* Maximum we will need */
  150. else if(len <= 0) return -1;
  151. if(value < 0x80) {
  152. if(str) *str = (unsigned char)value;
  153. return 1;
  154. }
  155. if(value < 0x800) {
  156. if(len < 2) return -1;
  157. if(str) {
  158. *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
  159. *str = (unsigned char)((value & 0x3f) | 0x80);
  160. }
  161. return 2;
  162. }
  163. if(value < 0x10000) {
  164. if(len < 3) return -1;
  165. if(str) {
  166. *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
  167. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  168. *str = (unsigned char)((value & 0x3f) | 0x80);
  169. }
  170. return 3;
  171. }
  172. if(value < 0x200000) {
  173. if(len < 4) return -1;
  174. if(str) {
  175. *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
  176. *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
  177. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  178. *str = (unsigned char)((value & 0x3f) | 0x80);
  179. }
  180. return 4;
  181. }
  182. if(value < 0x4000000) {
  183. if(len < 5) return -1;
  184. if(str) {
  185. *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
  186. *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
  187. *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
  188. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  189. *str = (unsigned char)((value & 0x3f) | 0x80);
  190. }
  191. return 5;
  192. }
  193. if(len < 6) return -1;
  194. if(str) {
  195. *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
  196. *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
  197. *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
  198. *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
  199. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  200. *str = (unsigned char)((value & 0x3f) | 0x80);
  201. }
  202. return 6;
  203. }