static_dict.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "./static_dict.h"
  6. #include "../common/dictionary.h"
  7. #include "../common/platform.h"
  8. #include "../common/transform.h"
  9. #include "./encoder_dict.h"
  10. #include "./find_match_length.h"
  11. #if defined(__cplusplus) || defined(c_plusplus)
  12. extern "C" {
  13. #endif
  14. static BROTLI_INLINE uint32_t Hash(const uint8_t* data) {
  15. uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kDictHashMul32;
  16. /* The higher bits contain more mixture from the multiplication,
  17. so we take our results from there. */
  18. return h >> (32 - kDictNumBits);
  19. }
  20. static BROTLI_INLINE void AddMatch(size_t distance, size_t len, size_t len_code,
  21. uint32_t* matches) {
  22. uint32_t match = (uint32_t)((distance << 5) + len_code);
  23. matches[len] = BROTLI_MIN(uint32_t, matches[len], match);
  24. }
  25. static BROTLI_INLINE size_t DictMatchLength(const BrotliDictionary* dictionary,
  26. const uint8_t* data,
  27. size_t id,
  28. size_t len,
  29. size_t maxlen) {
  30. const size_t offset = dictionary->offsets_by_length[len] + len * id;
  31. return FindMatchLengthWithLimit(&dictionary->data[offset], data,
  32. BROTLI_MIN(size_t, len, maxlen));
  33. }
  34. static BROTLI_INLINE BROTLI_BOOL IsMatch(const BrotliDictionary* dictionary,
  35. DictWord w, const uint8_t* data, size_t max_length) {
  36. if (w.len > max_length) {
  37. return BROTLI_FALSE;
  38. } else {
  39. const size_t offset = dictionary->offsets_by_length[w.len] +
  40. (size_t)w.len * (size_t)w.idx;
  41. const uint8_t* dict = &dictionary->data[offset];
  42. if (w.transform == 0) {
  43. /* Match against base dictionary word. */
  44. return
  45. TO_BROTLI_BOOL(FindMatchLengthWithLimit(dict, data, w.len) == w.len);
  46. } else if (w.transform == 10) {
  47. /* Match against uppercase first transform.
  48. Note that there are only ASCII uppercase words in the lookup table. */
  49. return TO_BROTLI_BOOL(dict[0] >= 'a' && dict[0] <= 'z' &&
  50. (dict[0] ^ 32) == data[0] &&
  51. FindMatchLengthWithLimit(&dict[1], &data[1], w.len - 1u) ==
  52. w.len - 1u);
  53. } else {
  54. /* Match against uppercase all transform.
  55. Note that there are only ASCII uppercase words in the lookup table. */
  56. size_t i;
  57. for (i = 0; i < w.len; ++i) {
  58. if (dict[i] >= 'a' && dict[i] <= 'z') {
  59. if ((dict[i] ^ 32) != data[i]) return BROTLI_FALSE;
  60. } else {
  61. if (dict[i] != data[i]) return BROTLI_FALSE;
  62. }
  63. }
  64. return BROTLI_TRUE;
  65. }
  66. }
  67. }
  68. BROTLI_BOOL BrotliFindAllStaticDictionaryMatches(
  69. const BrotliEncoderDictionary* dictionary, const uint8_t* data,
  70. size_t min_length, size_t max_length, uint32_t* matches) {
  71. BROTLI_BOOL has_found_match = BROTLI_FALSE;
  72. {
  73. size_t offset = dictionary->buckets[Hash(data)];
  74. BROTLI_BOOL end = !offset;
  75. while (!end) {
  76. DictWord w = dictionary->dict_words[offset++];
  77. const size_t l = w.len & 0x1F;
  78. const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l];
  79. const size_t id = w.idx;
  80. end = !!(w.len & 0x80);
  81. w.len = (uint8_t)l;
  82. if (w.transform == 0) {
  83. const size_t matchlen =
  84. DictMatchLength(dictionary->words, data, id, l, max_length);
  85. const uint8_t* s;
  86. size_t minlen;
  87. size_t maxlen;
  88. size_t len;
  89. /* Transform "" + BROTLI_TRANSFORM_IDENTITY + "" */
  90. if (matchlen == l) {
  91. AddMatch(id, l, l, matches);
  92. has_found_match = BROTLI_TRUE;
  93. }
  94. /* Transforms "" + BROTLI_TRANSFORM_OMIT_LAST_1 + "" and
  95. "" + BROTLI_TRANSFORM_OMIT_LAST_1 + "ing " */
  96. if (matchlen >= l - 1) {
  97. AddMatch(id + 12 * n, l - 1, l, matches);
  98. if (l + 2 < max_length &&
  99. data[l - 1] == 'i' && data[l] == 'n' && data[l + 1] == 'g' &&
  100. data[l + 2] == ' ') {
  101. AddMatch(id + 49 * n, l + 3, l, matches);
  102. }
  103. has_found_match = BROTLI_TRUE;
  104. }
  105. /* Transform "" + BROTLI_TRANSFORM_OMIT_LAST_# + "" (# = 2 .. 9) */
  106. minlen = min_length;
  107. if (l > 9) minlen = BROTLI_MAX(size_t, minlen, l - 9);
  108. maxlen = BROTLI_MIN(size_t, matchlen, l - 2);
  109. for (len = minlen; len <= maxlen; ++len) {
  110. size_t cut = l - len;
  111. size_t transform_id = (cut << 2) +
  112. (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F);
  113. AddMatch(id + transform_id * n, len, l, matches);
  114. has_found_match = BROTLI_TRUE;
  115. }
  116. if (matchlen < l || l + 6 >= max_length) {
  117. continue;
  118. }
  119. s = &data[l];
  120. /* Transforms "" + BROTLI_TRANSFORM_IDENTITY + <suffix> */
  121. if (s[0] == ' ') {
  122. AddMatch(id + n, l + 1, l, matches);
  123. if (s[1] == 'a') {
  124. if (s[2] == ' ') {
  125. AddMatch(id + 28 * n, l + 3, l, matches);
  126. } else if (s[2] == 's') {
  127. if (s[3] == ' ') AddMatch(id + 46 * n, l + 4, l, matches);
  128. } else if (s[2] == 't') {
  129. if (s[3] == ' ') AddMatch(id + 60 * n, l + 4, l, matches);
  130. } else if (s[2] == 'n') {
  131. if (s[3] == 'd' && s[4] == ' ') {
  132. AddMatch(id + 10 * n, l + 5, l, matches);
  133. }
  134. }
  135. } else if (s[1] == 'b') {
  136. if (s[2] == 'y' && s[3] == ' ') {
  137. AddMatch(id + 38 * n, l + 4, l, matches);
  138. }
  139. } else if (s[1] == 'i') {
  140. if (s[2] == 'n') {
  141. if (s[3] == ' ') AddMatch(id + 16 * n, l + 4, l, matches);
  142. } else if (s[2] == 's') {
  143. if (s[3] == ' ') AddMatch(id + 47 * n, l + 4, l, matches);
  144. }
  145. } else if (s[1] == 'f') {
  146. if (s[2] == 'o') {
  147. if (s[3] == 'r' && s[4] == ' ') {
  148. AddMatch(id + 25 * n, l + 5, l, matches);
  149. }
  150. } else if (s[2] == 'r') {
  151. if (s[3] == 'o' && s[4] == 'm' && s[5] == ' ') {
  152. AddMatch(id + 37 * n, l + 6, l, matches);
  153. }
  154. }
  155. } else if (s[1] == 'o') {
  156. if (s[2] == 'f') {
  157. if (s[3] == ' ') AddMatch(id + 8 * n, l + 4, l, matches);
  158. } else if (s[2] == 'n') {
  159. if (s[3] == ' ') AddMatch(id + 45 * n, l + 4, l, matches);
  160. }
  161. } else if (s[1] == 'n') {
  162. if (s[2] == 'o' && s[3] == 't' && s[4] == ' ') {
  163. AddMatch(id + 80 * n, l + 5, l, matches);
  164. }
  165. } else if (s[1] == 't') {
  166. if (s[2] == 'h') {
  167. if (s[3] == 'e') {
  168. if (s[4] == ' ') AddMatch(id + 5 * n, l + 5, l, matches);
  169. } else if (s[3] == 'a') {
  170. if (s[4] == 't' && s[5] == ' ') {
  171. AddMatch(id + 29 * n, l + 6, l, matches);
  172. }
  173. }
  174. } else if (s[2] == 'o') {
  175. if (s[3] == ' ') AddMatch(id + 17 * n, l + 4, l, matches);
  176. }
  177. } else if (s[1] == 'w') {
  178. if (s[2] == 'i' && s[3] == 't' && s[4] == 'h' && s[5] == ' ') {
  179. AddMatch(id + 35 * n, l + 6, l, matches);
  180. }
  181. }
  182. } else if (s[0] == '"') {
  183. AddMatch(id + 19 * n, l + 1, l, matches);
  184. if (s[1] == '>') {
  185. AddMatch(id + 21 * n, l + 2, l, matches);
  186. }
  187. } else if (s[0] == '.') {
  188. AddMatch(id + 20 * n, l + 1, l, matches);
  189. if (s[1] == ' ') {
  190. AddMatch(id + 31 * n, l + 2, l, matches);
  191. if (s[2] == 'T' && s[3] == 'h') {
  192. if (s[4] == 'e') {
  193. if (s[5] == ' ') AddMatch(id + 43 * n, l + 6, l, matches);
  194. } else if (s[4] == 'i') {
  195. if (s[5] == 's' && s[6] == ' ') {
  196. AddMatch(id + 75 * n, l + 7, l, matches);
  197. }
  198. }
  199. }
  200. }
  201. } else if (s[0] == ',') {
  202. AddMatch(id + 76 * n, l + 1, l, matches);
  203. if (s[1] == ' ') {
  204. AddMatch(id + 14 * n, l + 2, l, matches);
  205. }
  206. } else if (s[0] == '\n') {
  207. AddMatch(id + 22 * n, l + 1, l, matches);
  208. if (s[1] == '\t') {
  209. AddMatch(id + 50 * n, l + 2, l, matches);
  210. }
  211. } else if (s[0] == ']') {
  212. AddMatch(id + 24 * n, l + 1, l, matches);
  213. } else if (s[0] == '\'') {
  214. AddMatch(id + 36 * n, l + 1, l, matches);
  215. } else if (s[0] == ':') {
  216. AddMatch(id + 51 * n, l + 1, l, matches);
  217. } else if (s[0] == '(') {
  218. AddMatch(id + 57 * n, l + 1, l, matches);
  219. } else if (s[0] == '=') {
  220. if (s[1] == '"') {
  221. AddMatch(id + 70 * n, l + 2, l, matches);
  222. } else if (s[1] == '\'') {
  223. AddMatch(id + 86 * n, l + 2, l, matches);
  224. }
  225. } else if (s[0] == 'a') {
  226. if (s[1] == 'l' && s[2] == ' ') {
  227. AddMatch(id + 84 * n, l + 3, l, matches);
  228. }
  229. } else if (s[0] == 'e') {
  230. if (s[1] == 'd') {
  231. if (s[2] == ' ') AddMatch(id + 53 * n, l + 3, l, matches);
  232. } else if (s[1] == 'r') {
  233. if (s[2] == ' ') AddMatch(id + 82 * n, l + 3, l, matches);
  234. } else if (s[1] == 's') {
  235. if (s[2] == 't' && s[3] == ' ') {
  236. AddMatch(id + 95 * n, l + 4, l, matches);
  237. }
  238. }
  239. } else if (s[0] == 'f') {
  240. if (s[1] == 'u' && s[2] == 'l' && s[3] == ' ') {
  241. AddMatch(id + 90 * n, l + 4, l, matches);
  242. }
  243. } else if (s[0] == 'i') {
  244. if (s[1] == 'v') {
  245. if (s[2] == 'e' && s[3] == ' ') {
  246. AddMatch(id + 92 * n, l + 4, l, matches);
  247. }
  248. } else if (s[1] == 'z') {
  249. if (s[2] == 'e' && s[3] == ' ') {
  250. AddMatch(id + 100 * n, l + 4, l, matches);
  251. }
  252. }
  253. } else if (s[0] == 'l') {
  254. if (s[1] == 'e') {
  255. if (s[2] == 's' && s[3] == 's' && s[4] == ' ') {
  256. AddMatch(id + 93 * n, l + 5, l, matches);
  257. }
  258. } else if (s[1] == 'y') {
  259. if (s[2] == ' ') AddMatch(id + 61 * n, l + 3, l, matches);
  260. }
  261. } else if (s[0] == 'o') {
  262. if (s[1] == 'u' && s[2] == 's' && s[3] == ' ') {
  263. AddMatch(id + 106 * n, l + 4, l, matches);
  264. }
  265. }
  266. } else {
  267. /* Set is_all_caps=0 for BROTLI_TRANSFORM_UPPERCASE_FIRST and
  268. is_all_caps=1 otherwise (BROTLI_TRANSFORM_UPPERCASE_ALL)
  269. transform. */
  270. const BROTLI_BOOL is_all_caps =
  271. TO_BROTLI_BOOL(w.transform != BROTLI_TRANSFORM_UPPERCASE_FIRST);
  272. const uint8_t* s;
  273. if (!IsMatch(dictionary->words, w, data, max_length)) {
  274. continue;
  275. }
  276. /* Transform "" + kUppercase{First,All} + "" */
  277. AddMatch(id + (is_all_caps ? 44 : 9) * n, l, l, matches);
  278. has_found_match = BROTLI_TRUE;
  279. if (l + 1 >= max_length) {
  280. continue;
  281. }
  282. /* Transforms "" + kUppercase{First,All} + <suffix> */
  283. s = &data[l];
  284. if (s[0] == ' ') {
  285. AddMatch(id + (is_all_caps ? 68 : 4) * n, l + 1, l, matches);
  286. } else if (s[0] == '"') {
  287. AddMatch(id + (is_all_caps ? 87 : 66) * n, l + 1, l, matches);
  288. if (s[1] == '>') {
  289. AddMatch(id + (is_all_caps ? 97 : 69) * n, l + 2, l, matches);
  290. }
  291. } else if (s[0] == '.') {
  292. AddMatch(id + (is_all_caps ? 101 : 79) * n, l + 1, l, matches);
  293. if (s[1] == ' ') {
  294. AddMatch(id + (is_all_caps ? 114 : 88) * n, l + 2, l, matches);
  295. }
  296. } else if (s[0] == ',') {
  297. AddMatch(id + (is_all_caps ? 112 : 99) * n, l + 1, l, matches);
  298. if (s[1] == ' ') {
  299. AddMatch(id + (is_all_caps ? 107 : 58) * n, l + 2, l, matches);
  300. }
  301. } else if (s[0] == '\'') {
  302. AddMatch(id + (is_all_caps ? 94 : 74) * n, l + 1, l, matches);
  303. } else if (s[0] == '(') {
  304. AddMatch(id + (is_all_caps ? 113 : 78) * n, l + 1, l, matches);
  305. } else if (s[0] == '=') {
  306. if (s[1] == '"') {
  307. AddMatch(id + (is_all_caps ? 105 : 104) * n, l + 2, l, matches);
  308. } else if (s[1] == '\'') {
  309. AddMatch(id + (is_all_caps ? 116 : 108) * n, l + 2, l, matches);
  310. }
  311. }
  312. }
  313. }
  314. }
  315. /* Transforms with prefixes " " and "." */
  316. if (max_length >= 5 && (data[0] == ' ' || data[0] == '.')) {
  317. BROTLI_BOOL is_space = TO_BROTLI_BOOL(data[0] == ' ');
  318. size_t offset = dictionary->buckets[Hash(&data[1])];
  319. BROTLI_BOOL end = !offset;
  320. while (!end) {
  321. DictWord w = dictionary->dict_words[offset++];
  322. const size_t l = w.len & 0x1F;
  323. const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l];
  324. const size_t id = w.idx;
  325. end = !!(w.len & 0x80);
  326. w.len = (uint8_t)l;
  327. if (w.transform == 0) {
  328. const uint8_t* s;
  329. if (!IsMatch(dictionary->words, w, &data[1], max_length - 1)) {
  330. continue;
  331. }
  332. /* Transforms " " + BROTLI_TRANSFORM_IDENTITY + "" and
  333. "." + BROTLI_TRANSFORM_IDENTITY + "" */
  334. AddMatch(id + (is_space ? 6 : 32) * n, l + 1, l, matches);
  335. has_found_match = BROTLI_TRUE;
  336. if (l + 2 >= max_length) {
  337. continue;
  338. }
  339. /* Transforms " " + BROTLI_TRANSFORM_IDENTITY + <suffix> and
  340. "." + BROTLI_TRANSFORM_IDENTITY + <suffix>
  341. */
  342. s = &data[l + 1];
  343. if (s[0] == ' ') {
  344. AddMatch(id + (is_space ? 2 : 77) * n, l + 2, l, matches);
  345. } else if (s[0] == '(') {
  346. AddMatch(id + (is_space ? 89 : 67) * n, l + 2, l, matches);
  347. } else if (is_space) {
  348. if (s[0] == ',') {
  349. AddMatch(id + 103 * n, l + 2, l, matches);
  350. if (s[1] == ' ') {
  351. AddMatch(id + 33 * n, l + 3, l, matches);
  352. }
  353. } else if (s[0] == '.') {
  354. AddMatch(id + 71 * n, l + 2, l, matches);
  355. if (s[1] == ' ') {
  356. AddMatch(id + 52 * n, l + 3, l, matches);
  357. }
  358. } else if (s[0] == '=') {
  359. if (s[1] == '"') {
  360. AddMatch(id + 81 * n, l + 3, l, matches);
  361. } else if (s[1] == '\'') {
  362. AddMatch(id + 98 * n, l + 3, l, matches);
  363. }
  364. }
  365. }
  366. } else if (is_space) {
  367. /* Set is_all_caps=0 for BROTLI_TRANSFORM_UPPERCASE_FIRST and
  368. is_all_caps=1 otherwise (BROTLI_TRANSFORM_UPPERCASE_ALL)
  369. transform. */
  370. const BROTLI_BOOL is_all_caps =
  371. TO_BROTLI_BOOL(w.transform != BROTLI_TRANSFORM_UPPERCASE_FIRST);
  372. const uint8_t* s;
  373. if (!IsMatch(dictionary->words, w, &data[1], max_length - 1)) {
  374. continue;
  375. }
  376. /* Transforms " " + kUppercase{First,All} + "" */
  377. AddMatch(id + (is_all_caps ? 85 : 30) * n, l + 1, l, matches);
  378. has_found_match = BROTLI_TRUE;
  379. if (l + 2 >= max_length) {
  380. continue;
  381. }
  382. /* Transforms " " + kUppercase{First,All} + <suffix> */
  383. s = &data[l + 1];
  384. if (s[0] == ' ') {
  385. AddMatch(id + (is_all_caps ? 83 : 15) * n, l + 2, l, matches);
  386. } else if (s[0] == ',') {
  387. if (!is_all_caps) {
  388. AddMatch(id + 109 * n, l + 2, l, matches);
  389. }
  390. if (s[1] == ' ') {
  391. AddMatch(id + (is_all_caps ? 111 : 65) * n, l + 3, l, matches);
  392. }
  393. } else if (s[0] == '.') {
  394. AddMatch(id + (is_all_caps ? 115 : 96) * n, l + 2, l, matches);
  395. if (s[1] == ' ') {
  396. AddMatch(id + (is_all_caps ? 117 : 91) * n, l + 3, l, matches);
  397. }
  398. } else if (s[0] == '=') {
  399. if (s[1] == '"') {
  400. AddMatch(id + (is_all_caps ? 110 : 118) * n, l + 3, l, matches);
  401. } else if (s[1] == '\'') {
  402. AddMatch(id + (is_all_caps ? 119 : 120) * n, l + 3, l, matches);
  403. }
  404. }
  405. }
  406. }
  407. }
  408. if (max_length >= 6) {
  409. /* Transforms with prefixes "e ", "s ", ", " and "\xC2\xA0" */
  410. if ((data[1] == ' ' &&
  411. (data[0] == 'e' || data[0] == 's' || data[0] == ',')) ||
  412. (data[0] == 0xC2 && data[1] == 0xA0)) {
  413. size_t offset = dictionary->buckets[Hash(&data[2])];
  414. BROTLI_BOOL end = !offset;
  415. while (!end) {
  416. DictWord w = dictionary->dict_words[offset++];
  417. const size_t l = w.len & 0x1F;
  418. const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l];
  419. const size_t id = w.idx;
  420. end = !!(w.len & 0x80);
  421. w.len = (uint8_t)l;
  422. if (w.transform == 0 &&
  423. IsMatch(dictionary->words, w, &data[2], max_length - 2)) {
  424. if (data[0] == 0xC2) {
  425. AddMatch(id + 102 * n, l + 2, l, matches);
  426. has_found_match = BROTLI_TRUE;
  427. } else if (l + 2 < max_length && data[l + 2] == ' ') {
  428. size_t t = data[0] == 'e' ? 18 : (data[0] == 's' ? 7 : 13);
  429. AddMatch(id + t * n, l + 3, l, matches);
  430. has_found_match = BROTLI_TRUE;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. if (max_length >= 9) {
  437. /* Transforms with prefixes " the " and ".com/" */
  438. if ((data[0] == ' ' && data[1] == 't' && data[2] == 'h' &&
  439. data[3] == 'e' && data[4] == ' ') ||
  440. (data[0] == '.' && data[1] == 'c' && data[2] == 'o' &&
  441. data[3] == 'm' && data[4] == '/')) {
  442. size_t offset = dictionary->buckets[Hash(&data[5])];
  443. BROTLI_BOOL end = !offset;
  444. while (!end) {
  445. DictWord w = dictionary->dict_words[offset++];
  446. const size_t l = w.len & 0x1F;
  447. const size_t n = (size_t)1 << dictionary->words->size_bits_by_length[l];
  448. const size_t id = w.idx;
  449. end = !!(w.len & 0x80);
  450. w.len = (uint8_t)l;
  451. if (w.transform == 0 &&
  452. IsMatch(dictionary->words, w, &data[5], max_length - 5)) {
  453. AddMatch(id + (data[0] == ' ' ? 41 : 72) * n, l + 5, l, matches);
  454. has_found_match = BROTLI_TRUE;
  455. if (l + 5 < max_length) {
  456. const uint8_t* s = &data[l + 5];
  457. if (data[0] == ' ') {
  458. if (l + 8 < max_length &&
  459. s[0] == ' ' && s[1] == 'o' && s[2] == 'f' && s[3] == ' ') {
  460. AddMatch(id + 62 * n, l + 9, l, matches);
  461. if (l + 12 < max_length &&
  462. s[4] == 't' && s[5] == 'h' && s[6] == 'e' && s[7] == ' ') {
  463. AddMatch(id + 73 * n, l + 13, l, matches);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. return has_found_match;
  473. }
  474. #if defined(__cplusplus) || defined(c_plusplus)
  475. } /* extern "C" */
  476. #endif