string_escape.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. */
  22. /*escaping from https://github.com/akheron/jansson dump.c */
  23. #include "string_escape.h"
  24. size_t utf8_check_first(char byte)
  25. {
  26. unsigned char u = (unsigned char)byte;
  27. if(u < 0x80)
  28. return 1;
  29. if(0x80 <= u && u <= 0xBF) {
  30. /* second, third or fourth byte of a multi-byte
  31. sequence, i.e. a "continuation byte" */
  32. return 0;
  33. }
  34. else if(u == 0xC0 || u == 0xC1) {
  35. /* overlong encoding of an ASCII byte */
  36. return 0;
  37. }
  38. else if(0xC2 <= u && u <= 0xDF) {
  39. /* 2-byte sequence */
  40. return 2;
  41. }
  42. else if(0xE0 <= u && u <= 0xEF) {
  43. /* 3-byte sequence */
  44. return 3;
  45. }
  46. else if(0xF0 <= u && u <= 0xF4) {
  47. /* 4-byte sequence */
  48. return 4;
  49. }
  50. else { /* u >= 0xF5 */
  51. /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
  52. UTF-8 */
  53. return 0;
  54. }
  55. }
  56. size_t utf8_check_full(const char *buffer, size_t size, int32_t *codepoint)
  57. {
  58. size_t i;
  59. int32_t value = 0;
  60. unsigned char u = (unsigned char)buffer[0];
  61. if(size == 2)
  62. {
  63. value = u & 0x1F;
  64. }
  65. else if(size == 3)
  66. {
  67. value = u & 0xF;
  68. }
  69. else if(size == 4)
  70. {
  71. value = u & 0x7;
  72. }
  73. else
  74. return 0;
  75. for(i = 1; i < size; i++)
  76. {
  77. u = (unsigned char)buffer[i];
  78. if(u < 0x80 || u > 0xBF) {
  79. /* not a continuation byte */
  80. return 0;
  81. }
  82. value = (value << 6) + (u & 0x3F);
  83. }
  84. if(value > 0x10FFFF) {
  85. /* not in Unicode range */
  86. return 0;
  87. }
  88. else if(0xD800 <= value && value <= 0xDFFF) {
  89. /* invalid code point (UTF-16 surrogate halves) */
  90. return 0;
  91. }
  92. else if((size == 2 && value < 0x80) ||
  93. (size == 3 && value < 0x800) ||
  94. (size == 4 && value < 0x10000)) {
  95. /* overlong encoding */
  96. return 0;
  97. }
  98. if(codepoint)
  99. *codepoint = value;
  100. return 1;
  101. }
  102. const char *utf8_iterate(const char *buffer, size_t bufsize, int32_t *codepoint)
  103. {
  104. size_t count;
  105. int32_t value;
  106. if(!bufsize)
  107. return buffer;
  108. count = utf8_check_first(buffer[0]);
  109. if(count <= 0)
  110. return NULL;
  111. if(count == 1)
  112. value = (unsigned char)buffer[0];
  113. else
  114. {
  115. if(count > bufsize || !utf8_check_full(buffer, count, &value))
  116. return NULL;
  117. }
  118. if(codepoint)
  119. *codepoint = value;
  120. return buffer + count;
  121. }
  122. /* String unescape functions */
  123. int utf8_encode(int32_t codepoint, char *buffer, size_t *size)
  124. {
  125. if(codepoint < 0)
  126. return -1;
  127. else if(codepoint < 0x80)
  128. {
  129. buffer[0] = (char)codepoint;
  130. *size = 1;
  131. }
  132. else if(codepoint < 0x800)
  133. {
  134. buffer[0] = (char)(0xC0 + ((codepoint & 0x7C0) >> 6));
  135. buffer[1] = (char)(0x80 + ((codepoint & 0x03F)));
  136. *size = 2;
  137. }
  138. else if(codepoint < 0x10000)
  139. {
  140. buffer[0] = (char)(0xE0 + ((codepoint & 0xF000) >> 12));
  141. buffer[1] = (char)(0x80 + ((codepoint & 0x0FC0) >> 6));
  142. buffer[2] = (char)(0x80 + ((codepoint & 0x003F)));
  143. *size = 3;
  144. }
  145. else if(codepoint <= 0x10FFFF)
  146. {
  147. buffer[0] = (char)(0xF0 + ((codepoint & 0x1C0000) >> 18));
  148. buffer[1] = (char)(0x80 + ((codepoint & 0x03F000) >> 12));
  149. buffer[2] = (char)(0x80 + ((codepoint & 0x000FC0) >> 6));
  150. buffer[3] = (char)(0x80 + ((codepoint & 0x00003F)));
  151. *size = 4;
  152. }
  153. else
  154. return -1;
  155. return 0;
  156. }
  157. /* assumes that str points to 'u' plus at least 4 valid hex digits */
  158. int32_t decode_unicode_escape(const char *str) {
  159. int i;
  160. int32_t value = 0;
  161. for(i = 1; i <= 4; i++) {
  162. char c = str[i];
  163. value <<= 4;
  164. if(l_isdigit(c))
  165. value += c - '0';
  166. else if(l_islower(c))
  167. value += c - 'a' + 10;
  168. else if(l_isupper(c))
  169. value += c - 'A' + 10;
  170. else
  171. return -1;
  172. }
  173. return value;
  174. }