2
0

a_utf8.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/unicode.h"
  12. #include <openssl/asn1.h>
  13. /* UTF8 utilities */
  14. /*-
  15. * This parses a UTF8 string one character at a time. It is passed a pointer
  16. * to the string and the length of the string. It sets 'value' to the value of
  17. * the current character. It returns the number of characters read or a
  18. * negative error code:
  19. * -1 = string too short
  20. * -2 = illegal character
  21. * -3 = subsequent characters not of the form 10xxxxxx
  22. * -4 = character encoded incorrectly (not minimal length).
  23. */
  24. int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
  25. {
  26. const unsigned char *p;
  27. unsigned long value;
  28. int ret;
  29. if (len <= 0)
  30. return 0;
  31. p = str;
  32. /* Check syntax and work out the encoded value (if correct) */
  33. if ((*p & 0x80) == 0) {
  34. value = *p++ & 0x7f;
  35. ret = 1;
  36. } else if ((*p & 0xe0) == 0xc0) {
  37. if (len < 2)
  38. return -1;
  39. if ((p[1] & 0xc0) != 0x80)
  40. return -3;
  41. value = (*p++ & 0x1f) << 6;
  42. value |= *p++ & 0x3f;
  43. if (value < 0x80)
  44. return -4;
  45. ret = 2;
  46. } else if ((*p & 0xf0) == 0xe0) {
  47. if (len < 3)
  48. return -1;
  49. if (((p[1] & 0xc0) != 0x80)
  50. || ((p[2] & 0xc0) != 0x80))
  51. return -3;
  52. value = (*p++ & 0xf) << 12;
  53. value |= (*p++ & 0x3f) << 6;
  54. value |= *p++ & 0x3f;
  55. if (value < 0x800)
  56. return -4;
  57. if (is_unicode_surrogate(value))
  58. return -2;
  59. ret = 3;
  60. } else if ((*p & 0xf8) == 0xf0) {
  61. if (len < 4)
  62. return -1;
  63. if (((p[1] & 0xc0) != 0x80)
  64. || ((p[2] & 0xc0) != 0x80)
  65. || ((p[3] & 0xc0) != 0x80))
  66. return -3;
  67. value = ((unsigned long)(*p++ & 0x7)) << 18;
  68. value |= (*p++ & 0x3f) << 12;
  69. value |= (*p++ & 0x3f) << 6;
  70. value |= *p++ & 0x3f;
  71. if (value < 0x10000)
  72. return -4;
  73. ret = 4;
  74. } else
  75. return -2;
  76. *val = value;
  77. return ret;
  78. }
  79. /*
  80. * This takes a character 'value' and writes the UTF8 encoded value in 'str'
  81. * where 'str' is a buffer containing 'len' characters. Returns the number of
  82. * characters written, -1 if 'len' is too small or -2 if 'value' is out of
  83. * range. 'str' can be set to NULL in which case it just returns the number of
  84. * characters. It will need at most 4 characters.
  85. */
  86. int UTF8_putc(unsigned char *str, int len, unsigned long value)
  87. {
  88. if (!str)
  89. len = 4; /* Maximum we will need */
  90. else if (len <= 0)
  91. return -1;
  92. if (value < 0x80) {
  93. if (str)
  94. *str = (unsigned char)value;
  95. return 1;
  96. }
  97. if (value < 0x800) {
  98. if (len < 2)
  99. return -1;
  100. if (str) {
  101. *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
  102. *str = (unsigned char)((value & 0x3f) | 0x80);
  103. }
  104. return 2;
  105. }
  106. if (value < 0x10000) {
  107. if (is_unicode_surrogate(value))
  108. return -2;
  109. if (len < 3)
  110. return -1;
  111. if (str) {
  112. *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
  113. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  114. *str = (unsigned char)((value & 0x3f) | 0x80);
  115. }
  116. return 3;
  117. }
  118. if (value < UNICODE_LIMIT) {
  119. if (len < 4)
  120. return -1;
  121. if (str) {
  122. *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
  123. *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
  124. *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
  125. *str = (unsigned char)((value & 0x3f) | 0x80);
  126. }
  127. return 4;
  128. }
  129. return -2;
  130. }