transform.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. #include "./transform.h"
  6. #if defined(__cplusplus) || defined(c_plusplus)
  7. extern "C" {
  8. #endif
  9. /* RFC 7932 transforms string data */
  10. static const char kPrefixSuffix[217] =
  11. "\1 \2, \10 of the \4 of \2s \1.\5 and \4 "
  12. /* 0x _0 _2 __5 _E _3 _6 _8 _E */
  13. "in \1\"\4 to \2\">\1\n\2. \1]\5 for \3 a \6 "
  14. /* 2x _3_ _5 _A_ _D_ _F _2 _4 _A _E */
  15. "that \1\'\6 with \6 from \4 by \1(\6. T"
  16. /* 4x _5_ _7 _E _5 _A _C */
  17. "he \4 on \4 as \4 is \4ing \2\n\t\1:\3ed "
  18. /* 6x _3 _8 _D _2 _7_ _ _A _C */
  19. "\2=\"\4 at \3ly \1,\2=\'\5.com/\7. This \5"
  20. /* 8x _0 _ _3 _8 _C _E _ _1 _7 _F */
  21. " not \3er \3al \4ful \4ive \5less \4es"
  22. /* Ax _5 _9 _D _2 _7 _D */
  23. "t \4ize \2\xc2\xa0\4ous \5 the \2e \0";
  24. /* Cx _2 _7___ ___ _A _F _5 _8 */
  25. static const uint16_t kPrefixSuffixMap[50] = {
  26. 0x00, 0x02, 0x05, 0x0E, 0x13, 0x16, 0x18, 0x1E, 0x23, 0x25,
  27. 0x2A, 0x2D, 0x2F, 0x32, 0x34, 0x3A, 0x3E, 0x45, 0x47, 0x4E,
  28. 0x55, 0x5A, 0x5C, 0x63, 0x68, 0x6D, 0x72, 0x77, 0x7A, 0x7C,
  29. 0x80, 0x83, 0x88, 0x8C, 0x8E, 0x91, 0x97, 0x9F, 0xA5, 0xA9,
  30. 0xAD, 0xB2, 0xB7, 0xBD, 0xC2, 0xC7, 0xCA, 0xCF, 0xD5, 0xD8
  31. };
  32. /* RFC 7932 transforms */
  33. static const uint8_t kTransformsData[] = {
  34. 49, BROTLI_TRANSFORM_IDENTITY, 49,
  35. 49, BROTLI_TRANSFORM_IDENTITY, 0,
  36. 0, BROTLI_TRANSFORM_IDENTITY, 0,
  37. 49, BROTLI_TRANSFORM_OMIT_FIRST_1, 49,
  38. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0,
  39. 49, BROTLI_TRANSFORM_IDENTITY, 47,
  40. 0, BROTLI_TRANSFORM_IDENTITY, 49,
  41. 4, BROTLI_TRANSFORM_IDENTITY, 0,
  42. 49, BROTLI_TRANSFORM_IDENTITY, 3,
  43. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49,
  44. 49, BROTLI_TRANSFORM_IDENTITY, 6,
  45. 49, BROTLI_TRANSFORM_OMIT_FIRST_2, 49,
  46. 49, BROTLI_TRANSFORM_OMIT_LAST_1, 49,
  47. 1, BROTLI_TRANSFORM_IDENTITY, 0,
  48. 49, BROTLI_TRANSFORM_IDENTITY, 1,
  49. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0,
  50. 49, BROTLI_TRANSFORM_IDENTITY, 7,
  51. 49, BROTLI_TRANSFORM_IDENTITY, 9,
  52. 48, BROTLI_TRANSFORM_IDENTITY, 0,
  53. 49, BROTLI_TRANSFORM_IDENTITY, 8,
  54. 49, BROTLI_TRANSFORM_IDENTITY, 5,
  55. 49, BROTLI_TRANSFORM_IDENTITY, 10,
  56. 49, BROTLI_TRANSFORM_IDENTITY, 11,
  57. 49, BROTLI_TRANSFORM_OMIT_LAST_3, 49,
  58. 49, BROTLI_TRANSFORM_IDENTITY, 13,
  59. 49, BROTLI_TRANSFORM_IDENTITY, 14,
  60. 49, BROTLI_TRANSFORM_OMIT_FIRST_3, 49,
  61. 49, BROTLI_TRANSFORM_OMIT_LAST_2, 49,
  62. 49, BROTLI_TRANSFORM_IDENTITY, 15,
  63. 49, BROTLI_TRANSFORM_IDENTITY, 16,
  64. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49,
  65. 49, BROTLI_TRANSFORM_IDENTITY, 12,
  66. 5, BROTLI_TRANSFORM_IDENTITY, 49,
  67. 0, BROTLI_TRANSFORM_IDENTITY, 1,
  68. 49, BROTLI_TRANSFORM_OMIT_FIRST_4, 49,
  69. 49, BROTLI_TRANSFORM_IDENTITY, 18,
  70. 49, BROTLI_TRANSFORM_IDENTITY, 17,
  71. 49, BROTLI_TRANSFORM_IDENTITY, 19,
  72. 49, BROTLI_TRANSFORM_IDENTITY, 20,
  73. 49, BROTLI_TRANSFORM_OMIT_FIRST_5, 49,
  74. 49, BROTLI_TRANSFORM_OMIT_FIRST_6, 49,
  75. 47, BROTLI_TRANSFORM_IDENTITY, 49,
  76. 49, BROTLI_TRANSFORM_OMIT_LAST_4, 49,
  77. 49, BROTLI_TRANSFORM_IDENTITY, 22,
  78. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 49,
  79. 49, BROTLI_TRANSFORM_IDENTITY, 23,
  80. 49, BROTLI_TRANSFORM_IDENTITY, 24,
  81. 49, BROTLI_TRANSFORM_IDENTITY, 25,
  82. 49, BROTLI_TRANSFORM_OMIT_LAST_7, 49,
  83. 49, BROTLI_TRANSFORM_OMIT_LAST_1, 26,
  84. 49, BROTLI_TRANSFORM_IDENTITY, 27,
  85. 49, BROTLI_TRANSFORM_IDENTITY, 28,
  86. 0, BROTLI_TRANSFORM_IDENTITY, 12,
  87. 49, BROTLI_TRANSFORM_IDENTITY, 29,
  88. 49, BROTLI_TRANSFORM_OMIT_FIRST_9, 49,
  89. 49, BROTLI_TRANSFORM_OMIT_FIRST_7, 49,
  90. 49, BROTLI_TRANSFORM_OMIT_LAST_6, 49,
  91. 49, BROTLI_TRANSFORM_IDENTITY, 21,
  92. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1,
  93. 49, BROTLI_TRANSFORM_OMIT_LAST_8, 49,
  94. 49, BROTLI_TRANSFORM_IDENTITY, 31,
  95. 49, BROTLI_TRANSFORM_IDENTITY, 32,
  96. 47, BROTLI_TRANSFORM_IDENTITY, 3,
  97. 49, BROTLI_TRANSFORM_OMIT_LAST_5, 49,
  98. 49, BROTLI_TRANSFORM_OMIT_LAST_9, 49,
  99. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1,
  100. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 8,
  101. 5, BROTLI_TRANSFORM_IDENTITY, 21,
  102. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 0,
  103. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 10,
  104. 49, BROTLI_TRANSFORM_IDENTITY, 30,
  105. 0, BROTLI_TRANSFORM_IDENTITY, 5,
  106. 35, BROTLI_TRANSFORM_IDENTITY, 49,
  107. 47, BROTLI_TRANSFORM_IDENTITY, 2,
  108. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 17,
  109. 49, BROTLI_TRANSFORM_IDENTITY, 36,
  110. 49, BROTLI_TRANSFORM_IDENTITY, 33,
  111. 5, BROTLI_TRANSFORM_IDENTITY, 0,
  112. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 21,
  113. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5,
  114. 49, BROTLI_TRANSFORM_IDENTITY, 37,
  115. 0, BROTLI_TRANSFORM_IDENTITY, 30,
  116. 49, BROTLI_TRANSFORM_IDENTITY, 38,
  117. 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 0,
  118. 49, BROTLI_TRANSFORM_IDENTITY, 39,
  119. 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 49,
  120. 49, BROTLI_TRANSFORM_IDENTITY, 34,
  121. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 8,
  122. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12,
  123. 0, BROTLI_TRANSFORM_IDENTITY, 21,
  124. 49, BROTLI_TRANSFORM_IDENTITY, 40,
  125. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12,
  126. 49, BROTLI_TRANSFORM_IDENTITY, 41,
  127. 49, BROTLI_TRANSFORM_IDENTITY, 42,
  128. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 17,
  129. 49, BROTLI_TRANSFORM_IDENTITY, 43,
  130. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5,
  131. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 10,
  132. 0, BROTLI_TRANSFORM_IDENTITY, 34,
  133. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33,
  134. 49, BROTLI_TRANSFORM_IDENTITY, 44,
  135. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 5,
  136. 45, BROTLI_TRANSFORM_IDENTITY, 49,
  137. 0, BROTLI_TRANSFORM_IDENTITY, 33,
  138. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30,
  139. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 30,
  140. 49, BROTLI_TRANSFORM_IDENTITY, 46,
  141. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 1,
  142. 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34,
  143. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33,
  144. 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 30,
  145. 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 1,
  146. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 33,
  147. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 21,
  148. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 12,
  149. 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 5,
  150. 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 34,
  151. 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 12,
  152. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30,
  153. 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 34,
  154. 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34,
  155. };
  156. static BrotliTransforms kBrotliTransforms = {
  157. sizeof(kPrefixSuffix),
  158. (const uint8_t*)kPrefixSuffix,
  159. kPrefixSuffixMap,
  160. sizeof(kTransformsData) / (3 * sizeof(kTransformsData[0])),
  161. kTransformsData,
  162. {0, 12, 27, 23, 42, 63, 56, 48, 59, 64}
  163. };
  164. const BrotliTransforms* BrotliGetTransforms(void) {
  165. return &kBrotliTransforms;
  166. }
  167. static int ToUpperCase(uint8_t* p) {
  168. if (p[0] < 0xC0) {
  169. if (p[0] >= 'a' && p[0] <= 'z') {
  170. p[0] ^= 32;
  171. }
  172. return 1;
  173. }
  174. /* An overly simplified uppercasing model for UTF-8. */
  175. if (p[0] < 0xE0) {
  176. p[1] ^= 32;
  177. return 2;
  178. }
  179. /* An arbitrary transform for three byte characters. */
  180. p[2] ^= 5;
  181. return 3;
  182. }
  183. int BrotliTransformDictionaryWord(uint8_t* dst, const uint8_t* word, int len,
  184. const BrotliTransforms* transforms, int transform_idx) {
  185. int idx = 0;
  186. const uint8_t* prefix = BROTLI_TRANSFORM_PREFIX(transforms, transform_idx);
  187. uint8_t type = BROTLI_TRANSFORM_TYPE(transforms, transform_idx);
  188. const uint8_t* suffix = BROTLI_TRANSFORM_SUFFIX(transforms, transform_idx);
  189. {
  190. int prefix_len = *prefix++;
  191. while (prefix_len--) { dst[idx++] = *prefix++; }
  192. }
  193. {
  194. const int t = type;
  195. int i = 0;
  196. if (t <= BROTLI_TRANSFORM_OMIT_LAST_9) {
  197. len -= t;
  198. } else if (t >= BROTLI_TRANSFORM_OMIT_FIRST_1
  199. && t <= BROTLI_TRANSFORM_OMIT_FIRST_9) {
  200. int skip = t - (BROTLI_TRANSFORM_OMIT_FIRST_1 - 1);
  201. word += skip;
  202. len -= skip;
  203. }
  204. while (i < len) { dst[idx++] = word[i++]; }
  205. if (t == BROTLI_TRANSFORM_UPPERCASE_FIRST) {
  206. ToUpperCase(&dst[idx - len]);
  207. } else if (t == BROTLI_TRANSFORM_UPPERCASE_ALL) {
  208. uint8_t* uppercase = &dst[idx - len];
  209. while (len > 0) {
  210. int step = ToUpperCase(uppercase);
  211. uppercase += step;
  212. len -= step;
  213. }
  214. }
  215. }
  216. {
  217. int suffix_len = *suffix++;
  218. while (suffix_len--) { dst[idx++] = *suffix++; }
  219. return idx;
  220. }
  221. }
  222. #if defined(__cplusplus) || defined(c_plusplus)
  223. } /* extern "C" */
  224. #endif