testRfc8259Util.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "RyanJson.h"
  2. #include "testRfc8259Util.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. static int32_t hexval(int32_t c)
  9. {
  10. if (c >= '0' && c <= '9') { return c - '0'; }
  11. c = (c >= 'a' && c <= 'f') ? (c - 'a' + 'A') : c;
  12. if (c >= 'A' && c <= 'F') { return c - 'A' + 10; }
  13. return -1;
  14. }
  15. static int32_t decode_u4(const char *s, uint16_t *out)
  16. {
  17. int32_t h0 = hexval(s[0]), h1 = hexval(s[1]), h2 = hexval(s[2]), h3 = hexval(s[3]);
  18. if (h0 < 0 || h1 < 0 || h2 < 0 || h3 < 0) { return 0; }
  19. *out = (uint16_t)((h0 << 12) | (h1 << 8) | (h2 << 4) | h3);
  20. return 1;
  21. }
  22. int32_t RyanJsonNormalizeString(const char *in, uint32_t inLen, unsigned char **out, uint32_t *outLen)
  23. {
  24. uint32_t cap = inLen * 4 + 8;
  25. unsigned char *buf = (unsigned char *)malloc(cap);
  26. if (NULL == buf) { return 0; }
  27. uint32_t pos = 0;
  28. for (uint32_t i = 0; i < inLen; i++)
  29. {
  30. unsigned char ch = (unsigned char)in[i];
  31. if (ch == '\\')
  32. {
  33. if (i + 1 >= inLen)
  34. {
  35. free(buf);
  36. return 0;
  37. }
  38. unsigned char esc = (unsigned char)in[++i];
  39. switch (esc)
  40. {
  41. case '\"': buf[pos++] = '\"'; break;
  42. case '\\': buf[pos++] = '\\'; break;
  43. case '/': buf[pos++] = '/'; break;
  44. case 'b': buf[pos++] = '\b'; break;
  45. case 'f': buf[pos++] = '\f'; break;
  46. case 'n': buf[pos++] = '\n'; break;
  47. case 'r': buf[pos++] = '\r'; break;
  48. case 't': buf[pos++] = '\t'; break;
  49. case 'u': {
  50. if (i + 4 >= inLen)
  51. {
  52. free(buf);
  53. return 0;
  54. }
  55. uint16_t u1;
  56. if (!decode_u4(&in[i + 1], &u1))
  57. {
  58. free(buf);
  59. return 0;
  60. }
  61. i += 4;
  62. if (u1 >= 0xD800 && u1 <= 0xDBFF)
  63. {
  64. if (i + 2 >= inLen || in[i + 1] != '\\' || in[i + 2] != 'u' || i + 6 >= inLen)
  65. {
  66. free(buf);
  67. return 0;
  68. }
  69. i += 2;
  70. uint16_t u2;
  71. if (!decode_u4(&in[i + 1], &u2))
  72. {
  73. free(buf);
  74. return 0;
  75. }
  76. i += 4;
  77. if (!(u2 >= 0xDC00 && u2 <= 0xDFFF))
  78. {
  79. free(buf);
  80. return 0;
  81. }
  82. uint32_t cp = 0x10000 + (((uint32_t)(u1 - 0xD800) << 10) | (uint32_t)(u2 - 0xDC00));
  83. buf[pos++] = (unsigned char)(0xF0 | ((cp >> 18) & 0x07));
  84. buf[pos++] = (unsigned char)(0x80 | ((cp >> 12) & 0x3F));
  85. buf[pos++] = (unsigned char)(0x80 | ((cp >> 6) & 0x3F));
  86. buf[pos++] = (unsigned char)(0x80 | (cp & 0x3F));
  87. }
  88. else if (u1 >= 0xDC00 && u1 <= 0xDFFF)
  89. {
  90. free(buf);
  91. return 0;
  92. }
  93. else
  94. {
  95. if (u1 <= 0x007F) { buf[pos++] = (unsigned char)u1; }
  96. else if (u1 <= 0x07FF)
  97. {
  98. buf[pos++] = (unsigned char)(0xC0 | ((u1 >> 6) & 0x1F));
  99. buf[pos++] = (unsigned char)(0x80 | (u1 & 0x3F));
  100. }
  101. else
  102. {
  103. buf[pos++] = (unsigned char)(0xE0 | ((u1 >> 12) & 0x0F));
  104. buf[pos++] = (unsigned char)(0x80 | ((u1 >> 6) & 0x3F));
  105. buf[pos++] = (unsigned char)(0x80 | (u1 & 0x3F));
  106. }
  107. }
  108. break;
  109. }
  110. default: free(buf); return 0;
  111. }
  112. }
  113. else
  114. {
  115. buf[pos++] = ch;
  116. }
  117. if (pos + 8 > cap)
  118. {
  119. cap *= 2;
  120. unsigned char *nb = (unsigned char *)realloc(buf, cap);
  121. if (NULL == nb)
  122. {
  123. free(buf);
  124. return 0;
  125. }
  126. buf = nb;
  127. }
  128. }
  129. *out = buf;
  130. *outLen = pos;
  131. return 1;
  132. }
  133. static void trim(const char **s, uint32_t *len)
  134. {
  135. const char *p = *s;
  136. uint32_t n = *len;
  137. while (n && isspace((unsigned char)*p))
  138. {
  139. p++;
  140. n--;
  141. }
  142. while (n && isspace((unsigned char)p[n - 1]))
  143. {
  144. n--;
  145. }
  146. *s = p;
  147. *len = n;
  148. }
  149. static int32_t is_quoted_string(const char *s, uint32_t len)
  150. {
  151. return len >= 2 && s[0] == '\"' && s[len - 1] == '\"';
  152. }
  153. int32_t RyanJsonScalarSemanticEqual(const char *a, uint32_t aLen, const char *b, uint32_t bLen)
  154. {
  155. trim(&a, &aLen);
  156. trim(&b, &bLen);
  157. if (is_quoted_string(a, aLen) && is_quoted_string(b, bLen))
  158. {
  159. unsigned char *na = NULL, *nb = NULL;
  160. uint32_t nla = 0, nlb = 0;
  161. if (!RyanJsonNormalizeString(a + 1, aLen - 2, &na, &nla)) { return 0; }
  162. if (!RyanJsonNormalizeString(b + 1, bLen - 2, &nb, &nlb))
  163. {
  164. free(na);
  165. return 0;
  166. }
  167. int32_t eq = (nla == nlb) && (0 == memcmp(na, nb, nla));
  168. free(na);
  169. free(nb);
  170. return eq;
  171. }
  172. if (aLen == 4 && 0 == strncmp(a, "true", 4) && bLen == 4 && 0 == strncmp(b, "true", 4)) { return 1; }
  173. if (aLen == 5 && 0 == strncmp(a, "false", 5) && bLen == 5 && 0 == strncmp(b, "false", 5)) { return 1; }
  174. if (aLen == 4 && 0 == strncmp(a, "null", 4) && bLen == 4 && 0 == strncmp(b, "null", 4)) { return 1; }
  175. char *endA = NULL, *endB = NULL;
  176. char *bufA = (char *)malloc(aLen + 1);
  177. char *bufB = (char *)malloc(bLen + 1);
  178. if (NULL != bufA && NULL != bufB)
  179. {
  180. memcpy(bufA, a, aLen);
  181. bufA[aLen] = '\0';
  182. memcpy(bufB, b, bLen);
  183. bufB[bLen] = '\0';
  184. double va = strtod(bufA, &endA);
  185. double vb = strtod(bufB, &endB);
  186. int32_t okA = (endA && *endA == '\0');
  187. int32_t okB = (endB && *endB == '\0');
  188. if (okA && okB)
  189. {
  190. free(bufA);
  191. free(bufB);
  192. // 使用相对误差比较,处理极大极小值
  193. if (RyanJsonCompareDouble(va, vb)) { return 1; }
  194. return 0;
  195. // if (va == vb) { return 1; }
  196. // double absA = fabs(va), absB = fabs(vb);
  197. // double maxAbs = (absA > absB) ? absA : absB;
  198. // if (maxAbs < 1e-300) { return 1; } // 两者都接近零
  199. // return (fabs(va - vb) / maxAbs) < 1e-14;
  200. }
  201. }
  202. free(bufA);
  203. free(bufB);
  204. return (aLen == bLen) && (0 == memcmp(a, b, aLen));
  205. }
  206. int32_t RyanJsonExtractSingleArrayElement(const char *s, uint32_t len, const char **elem, uint32_t *elemLen)
  207. {
  208. trim(&s, &len);
  209. if (len < 2 || s[0] != '[' || s[len - 1] != ']') { return 0; }
  210. const char *p = s + 1;
  211. uint32_t n = len - 2;
  212. trim(&p, &n);
  213. if (0 == n) { return 0; }
  214. int32_t in_str = 0, escape = 0;
  215. for (uint32_t i = 0; i < n; i++)
  216. {
  217. char c = p[i];
  218. if (in_str)
  219. {
  220. if (escape) { escape = 0; }
  221. else if (c == '\\') { escape = 1; }
  222. else if (c == '\"') { in_str = 0; }
  223. }
  224. else
  225. {
  226. if (c == '\"') { in_str = 1; }
  227. else if (c == ',') { return 0; }
  228. }
  229. }
  230. *elem = p;
  231. *elemLen = n;
  232. return 1;
  233. }
  234. int32_t RyanJsonValueSemanticEqual(const char *a, uint32_t aLen, const char *b, uint32_t bLen)
  235. {
  236. // 尝试将两个字符串解析为 Json 对象进行语义比较
  237. // 这可以处理对象和数组的递归比较
  238. RyanJson_t jsonA = RyanJsonParseOptions(a, aLen, RyanJsonFalse, NULL);
  239. RyanJson_t jsonB = RyanJsonParseOptions(b, bLen, RyanJsonFalse, NULL);
  240. if (NULL != jsonA && NULL != jsonB)
  241. {
  242. // 使用 RyanJsonCompare 进行完整的语义比较
  243. int32_t result = RyanJsonCompare(jsonA, jsonB);
  244. (void)RyanJsonDelete(jsonA);
  245. (void)RyanJsonDelete(jsonB);
  246. return result;
  247. }
  248. // 解析失败时清理并回退到原有逻辑
  249. if (NULL != jsonA) { (void)RyanJsonDelete(jsonA); }
  250. if (NULL != jsonB) { (void)RyanJsonDelete(jsonB); }
  251. // 回退:单元素数组提取比较
  252. const char *ae = NULL, *be = NULL;
  253. uint32_t ale = 0, ble = 0;
  254. if (RyanJsonExtractSingleArrayElement(a, aLen, &ae, &ale) && RyanJsonExtractSingleArrayElement(b, bLen, &be, &ble))
  255. {
  256. return RyanJsonScalarSemanticEqual(ae, ale, be, ble);
  257. }
  258. return RyanJsonScalarSemanticEqual(a, aLen, b, bLen);
  259. }