static_dict.c 21 KB

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