tools.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * (c) Philippe G. 20201, philippe_44@outlook.com
  3. * see other copyrights below
  4. *
  5. * This software is released under the MIT License.
  6. * https://opensource.org/licenses/MIT
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include "tools.h"
  15. #include "esp_log.h"
  16. const static char TAG[] = "tools";
  17. /****************************************************************************************
  18. * UTF-8 tools
  19. */
  20. // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  21. // See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  22. // Copyright (c) 2017 ZephRay <zephray@outlook.com>
  23. //
  24. // utf8to1252 - almost equivalent to iconv -f utf-8 -t windows-1252, but better
  25. #define UTF8_ACCEPT 0
  26. #define UTF8_REJECT 1
  27. static const uint8_t utf8d[] = {
  28. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f
  29. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f
  30. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f
  31. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f
  32. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
  33. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
  34. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
  35. 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
  36. 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
  37. 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
  38. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
  39. 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
  40. 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
  41. 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
  42. };
  43. static uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
  44. uint32_t type = utf8d[byte];
  45. *codep = (*state != UTF8_ACCEPT) ?
  46. (byte & 0x3fu) | (*codep << 6) :
  47. (0xff >> type) & (byte);
  48. *state = utf8d[256 + *state*16 + type];
  49. return *state;
  50. }
  51. static uint8_t UNICODEtoCP1252(uint16_t chr) {
  52. if (chr <= 0xff)
  53. return (chr&0xff);
  54. else {
  55. ESP_LOGI(TAG, "some multi-byte %hx", chr);
  56. switch(chr) {
  57. case 0x20ac: return 0x80; break;
  58. case 0x201a: return 0x82; break;
  59. case 0x0192: return 0x83; break;
  60. case 0x201e: return 0x84; break;
  61. case 0x2026: return 0x85; break;
  62. case 0x2020: return 0x86; break;
  63. case 0x2021: return 0x87; break;
  64. case 0x02c6: return 0x88; break;
  65. case 0x2030: return 0x89; break;
  66. case 0x0160: return 0x8a; break;
  67. case 0x2039: return 0x8b; break;
  68. case 0x0152: return 0x8c; break;
  69. case 0x017d: return 0x8e; break;
  70. case 0x2018: return 0x91; break;
  71. case 0x2019: return 0x92; break;
  72. case 0x201c: return 0x93; break;
  73. case 0x201d: return 0x94; break;
  74. case 0x2022: return 0x95; break;
  75. case 0x2013: return 0x96; break;
  76. case 0x2014: return 0x97; break;
  77. case 0x02dc: return 0x98; break;
  78. case 0x2122: return 0x99; break;
  79. case 0x0161: return 0x9a; break;
  80. case 0x203a: return 0x9b; break;
  81. case 0x0153: return 0x9c; break;
  82. case 0x017e: return 0x9e; break;
  83. case 0x0178: return 0x9f; break;
  84. default: return 0x00; break;
  85. }
  86. }
  87. }
  88. void utf8_decode(char *src) {
  89. uint32_t codep = 0, state = UTF8_ACCEPT;
  90. char *dst = src;
  91. while (src && *src) {
  92. if (!decode(&state, &codep, *src++)) *dst++ = UNICODEtoCP1252(codep);
  93. }
  94. *dst = '\0';
  95. }
  96. /****************************************************************************************
  97. * URL tools
  98. */
  99. static inline char from_hex(char ch) {
  100. return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
  101. }
  102. void url_decode(char *url) {
  103. char *p, *src = strdup(url);
  104. for (p = src; *src; url++) {
  105. *url = *src++;
  106. if (*url == '%') {
  107. *url = from_hex(*src++) << 4;
  108. *url |= from_hex(*src++);
  109. } else if (*url == '+') {
  110. *url = ' ';
  111. }
  112. }
  113. *url = '\0';
  114. free(p);
  115. }