houdini_html_u.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "buffer.h"
  5. #include "houdini.h"
  6. #include "utf8.h"
  7. #include "entities.inc"
  8. /* Binary tree lookup code for entities added by JGM */
  9. static const unsigned char *S_lookup(int i, int low, int hi,
  10. const unsigned char *s, int len) {
  11. int j;
  12. int cmp =
  13. strncmp((const char *)s, (const char *)cmark_entities[i].entity, len);
  14. if (cmp == 0 && cmark_entities[i].entity[len] == 0) {
  15. return (const unsigned char *)cmark_entities[i].bytes;
  16. } else if (cmp <= 0 && i > low) {
  17. j = i - ((i - low) / 2);
  18. if (j == i)
  19. j -= 1;
  20. return S_lookup(j, low, i - 1, s, len);
  21. } else if (cmp > 0 && i < hi) {
  22. j = i + ((hi - i) / 2);
  23. if (j == i)
  24. j += 1;
  25. return S_lookup(j, i + 1, hi, s, len);
  26. } else {
  27. return NULL;
  28. }
  29. }
  30. static const unsigned char *S_lookup_entity(const unsigned char *s, int len) {
  31. return S_lookup(CMARK_NUM_ENTITIES / 2, 0, CMARK_NUM_ENTITIES - 1, s, len);
  32. }
  33. bufsize_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src,
  34. bufsize_t size) {
  35. bufsize_t i = 0;
  36. if (size >= 3 && src[0] == '#') {
  37. int codepoint = 0;
  38. int num_digits = 0;
  39. if (_isdigit(src[1])) {
  40. for (i = 1; i < size && _isdigit(src[i]); ++i) {
  41. codepoint = (codepoint * 10) + (src[i] - '0');
  42. if (codepoint >= 0x110000) {
  43. // Keep counting digits but
  44. // avoid integer overflow.
  45. codepoint = 0x110000;
  46. }
  47. }
  48. num_digits = i - 1;
  49. }
  50. else if (src[1] == 'x' || src[1] == 'X') {
  51. for (i = 2; i < size && _isxdigit(src[i]); ++i) {
  52. codepoint = (codepoint * 16) + ((src[i] | 32) % 39 - 9);
  53. if (codepoint >= 0x110000) {
  54. // Keep counting digits but
  55. // avoid integer overflow.
  56. codepoint = 0x110000;
  57. }
  58. }
  59. num_digits = i - 2;
  60. }
  61. if (num_digits >= 1 && num_digits <= 8 && i < size && src[i] == ';') {
  62. if (codepoint == 0 || (codepoint >= 0xD800 && codepoint < 0xE000) ||
  63. codepoint >= 0x110000) {
  64. codepoint = 0xFFFD;
  65. }
  66. cmark_utf8proc_encode_char(codepoint, ob);
  67. return i + 1;
  68. }
  69. }
  70. else {
  71. if (size > CMARK_ENTITY_MAX_LENGTH)
  72. size = CMARK_ENTITY_MAX_LENGTH;
  73. for (i = CMARK_ENTITY_MIN_LENGTH; i < size; ++i) {
  74. if (src[i] == ' ')
  75. break;
  76. if (src[i] == ';') {
  77. const unsigned char *entity = S_lookup_entity(src, i);
  78. if (entity != NULL) {
  79. cmark_strbuf_puts(ob, (const char *)entity);
  80. return i + 1;
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. return 0;
  87. }
  88. int houdini_unescape_html(cmark_strbuf *ob, const uint8_t *src,
  89. bufsize_t size) {
  90. bufsize_t i = 0, org, ent;
  91. while (i < size) {
  92. org = i;
  93. while (i < size && src[i] != '&')
  94. i++;
  95. if (likely(i > org)) {
  96. if (unlikely(org == 0)) {
  97. if (i >= size)
  98. return 0;
  99. cmark_strbuf_grow(ob, HOUDINI_UNESCAPED_SIZE(size));
  100. }
  101. cmark_strbuf_put(ob, src + org, i - org);
  102. }
  103. /* escaping */
  104. if (i >= size)
  105. break;
  106. i++;
  107. ent = houdini_unescape_ent(ob, src + i, size - i);
  108. i += ent;
  109. /* not really an entity */
  110. if (ent == 0)
  111. cmark_strbuf_putc(ob, '&');
  112. }
  113. return 1;
  114. }
  115. void houdini_unescape_html_f(cmark_strbuf *ob, const uint8_t *src,
  116. bufsize_t size) {
  117. if (!houdini_unescape_html(ob, src, size))
  118. cmark_strbuf_put(ob, src, size);
  119. }