11cb0ef41Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci   Distributed under MIT license.
41cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
51cb0ef41Sopenharmony_ci*/
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "./transform.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
101cb0ef41Sopenharmony_ciextern "C" {
111cb0ef41Sopenharmony_ci#endif
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci/* RFC 7932 transforms string data */
141cb0ef41Sopenharmony_cistatic const char kPrefixSuffix[217] =
151cb0ef41Sopenharmony_ci      "\1 \2, \10 of the \4 of \2s \1.\5 and \4 "
161cb0ef41Sopenharmony_ci/* 0x  _0 _2  __5        _E    _3  _6 _8     _E */
171cb0ef41Sopenharmony_ci      "in \1\"\4 to \2\">\1\n\2. \1]\5 for \3 a \6 "
181cb0ef41Sopenharmony_ci/* 2x     _3_ _5    _A_  _D_ _F  _2 _4     _A   _E */
191cb0ef41Sopenharmony_ci      "that \1\'\6 with \6 from \4 by \1(\6. T"
201cb0ef41Sopenharmony_ci/* 4x       _5_ _7      _E      _5    _A _C */
211cb0ef41Sopenharmony_ci      "he \4 on \4 as \4 is \4ing \2\n\t\1:\3ed "
221cb0ef41Sopenharmony_ci/* 6x     _3    _8    _D    _2    _7_ _ _A _C */
231cb0ef41Sopenharmony_ci      "\2=\"\4 at \3ly \1,\2=\'\5.com/\7. This \5"
241cb0ef41Sopenharmony_ci/* 8x  _0 _ _3    _8   _C _E _ _1     _7       _F */
251cb0ef41Sopenharmony_ci      " not \3er \3al \4ful \4ive \5less \4es"
261cb0ef41Sopenharmony_ci/* Ax       _5   _9   _D    _2    _7     _D */
271cb0ef41Sopenharmony_ci      "t \4ize \2\xc2\xa0\4ous \5 the \2e "; /* \0 - implicit trailing zero. */
281cb0ef41Sopenharmony_ci/* Cx    _2    _7___ ___ _A    _F     _5        _8 */
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cistatic const uint16_t kPrefixSuffixMap[50] = {
311cb0ef41Sopenharmony_ci  0x00, 0x02, 0x05, 0x0E, 0x13, 0x16, 0x18, 0x1E, 0x23, 0x25,
321cb0ef41Sopenharmony_ci  0x2A, 0x2D, 0x2F, 0x32, 0x34, 0x3A, 0x3E, 0x45, 0x47, 0x4E,
331cb0ef41Sopenharmony_ci  0x55, 0x5A, 0x5C, 0x63, 0x68, 0x6D, 0x72, 0x77, 0x7A, 0x7C,
341cb0ef41Sopenharmony_ci  0x80, 0x83, 0x88, 0x8C, 0x8E, 0x91, 0x97, 0x9F, 0xA5, 0xA9,
351cb0ef41Sopenharmony_ci  0xAD, 0xB2, 0xB7, 0xBD, 0xC2, 0xC7, 0xCA, 0xCF, 0xD5, 0xD8
361cb0ef41Sopenharmony_ci};
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci/* RFC 7932 transforms */
391cb0ef41Sopenharmony_cistatic const uint8_t kTransformsData[] = {
401cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 49,
411cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 0,
421cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 0,
431cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_1, 49,
441cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0,
451cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 47,
461cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 49,
471cb0ef41Sopenharmony_ci   4, BROTLI_TRANSFORM_IDENTITY, 0,
481cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 3,
491cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49,
501cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 6,
511cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_2, 49,
521cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_1, 49,
531cb0ef41Sopenharmony_ci   1, BROTLI_TRANSFORM_IDENTITY, 0,
541cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 1,
551cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0,
561cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 7,
571cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 9,
581cb0ef41Sopenharmony_ci  48, BROTLI_TRANSFORM_IDENTITY, 0,
591cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 8,
601cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 5,
611cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 10,
621cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 11,
631cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_3, 49,
641cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 13,
651cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 14,
661cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_3, 49,
671cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_2, 49,
681cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 15,
691cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 16,
701cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49,
711cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 12,
721cb0ef41Sopenharmony_ci   5, BROTLI_TRANSFORM_IDENTITY, 49,
731cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 1,
741cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_4, 49,
751cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 18,
761cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 17,
771cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 19,
781cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 20,
791cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_5, 49,
801cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_6, 49,
811cb0ef41Sopenharmony_ci  47, BROTLI_TRANSFORM_IDENTITY, 49,
821cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_4, 49,
831cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 22,
841cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 49,
851cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 23,
861cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 24,
871cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 25,
881cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_7, 49,
891cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_1, 26,
901cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 27,
911cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 28,
921cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 12,
931cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 29,
941cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_9, 49,
951cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_FIRST_7, 49,
961cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_6, 49,
971cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 21,
981cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1,
991cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_8, 49,
1001cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 31,
1011cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 32,
1021cb0ef41Sopenharmony_ci  47, BROTLI_TRANSFORM_IDENTITY, 3,
1031cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_5, 49,
1041cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_OMIT_LAST_9, 49,
1051cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1,
1061cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 8,
1071cb0ef41Sopenharmony_ci   5, BROTLI_TRANSFORM_IDENTITY, 21,
1081cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 0,
1091cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 10,
1101cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 30,
1111cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 5,
1121cb0ef41Sopenharmony_ci  35, BROTLI_TRANSFORM_IDENTITY, 49,
1131cb0ef41Sopenharmony_ci  47, BROTLI_TRANSFORM_IDENTITY, 2,
1141cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 17,
1151cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 36,
1161cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 33,
1171cb0ef41Sopenharmony_ci   5, BROTLI_TRANSFORM_IDENTITY, 0,
1181cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 21,
1191cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5,
1201cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 37,
1211cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 30,
1221cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 38,
1231cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_ALL, 0,
1241cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 39,
1251cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_ALL, 49,
1261cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 34,
1271cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 8,
1281cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12,
1291cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 21,
1301cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 40,
1311cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12,
1321cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 41,
1331cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 42,
1341cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 17,
1351cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 43,
1361cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5,
1371cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 10,
1381cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 34,
1391cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33,
1401cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 44,
1411cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 5,
1421cb0ef41Sopenharmony_ci  45, BROTLI_TRANSFORM_IDENTITY, 49,
1431cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_IDENTITY, 33,
1441cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30,
1451cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 30,
1461cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_IDENTITY, 46,
1471cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 1,
1481cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34,
1491cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33,
1501cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_ALL, 30,
1511cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_ALL, 1,
1521cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 33,
1531cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 21,
1541cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 12,
1551cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_ALL, 5,
1561cb0ef41Sopenharmony_ci  49, BROTLI_TRANSFORM_UPPERCASE_ALL, 34,
1571cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_ALL, 12,
1581cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30,
1591cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_ALL, 34,
1601cb0ef41Sopenharmony_ci   0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34,
1611cb0ef41Sopenharmony_ci};
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_cistatic const BrotliTransforms kBrotliTransforms = {
1641cb0ef41Sopenharmony_ci  sizeof(kPrefixSuffix),
1651cb0ef41Sopenharmony_ci  (const uint8_t*)kPrefixSuffix,
1661cb0ef41Sopenharmony_ci  kPrefixSuffixMap,
1671cb0ef41Sopenharmony_ci  sizeof(kTransformsData) / (3 * sizeof(kTransformsData[0])),
1681cb0ef41Sopenharmony_ci  kTransformsData,
1691cb0ef41Sopenharmony_ci  NULL,  /* no extra parameters */
1701cb0ef41Sopenharmony_ci  {0, 12, 27, 23, 42, 63, 56, 48, 59, 64}
1711cb0ef41Sopenharmony_ci};
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ciconst BrotliTransforms* BrotliGetTransforms(void) {
1741cb0ef41Sopenharmony_ci  return &kBrotliTransforms;
1751cb0ef41Sopenharmony_ci}
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_cistatic int ToUpperCase(uint8_t* p) {
1781cb0ef41Sopenharmony_ci  if (p[0] < 0xC0) {
1791cb0ef41Sopenharmony_ci    if (p[0] >= 'a' && p[0] <= 'z') {
1801cb0ef41Sopenharmony_ci      p[0] ^= 32;
1811cb0ef41Sopenharmony_ci    }
1821cb0ef41Sopenharmony_ci    return 1;
1831cb0ef41Sopenharmony_ci  }
1841cb0ef41Sopenharmony_ci  /* An overly simplified uppercasing model for UTF-8. */
1851cb0ef41Sopenharmony_ci  if (p[0] < 0xE0) {
1861cb0ef41Sopenharmony_ci    p[1] ^= 32;
1871cb0ef41Sopenharmony_ci    return 2;
1881cb0ef41Sopenharmony_ci  }
1891cb0ef41Sopenharmony_ci  /* An arbitrary transform for three byte characters. */
1901cb0ef41Sopenharmony_ci  p[2] ^= 5;
1911cb0ef41Sopenharmony_ci  return 3;
1921cb0ef41Sopenharmony_ci}
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_cistatic int Shift(uint8_t* word, int word_len, uint16_t parameter) {
1951cb0ef41Sopenharmony_ci  /* Limited sign extension: scalar < (1 << 24). */
1961cb0ef41Sopenharmony_ci  uint32_t scalar =
1971cb0ef41Sopenharmony_ci      (parameter & 0x7FFFu) + (0x1000000u - (parameter & 0x8000u));
1981cb0ef41Sopenharmony_ci  if (word[0] < 0x80) {
1991cb0ef41Sopenharmony_ci    /* 1-byte rune / 0sssssss / 7 bit scalar (ASCII). */
2001cb0ef41Sopenharmony_ci    scalar += (uint32_t)word[0];
2011cb0ef41Sopenharmony_ci    word[0] = (uint8_t)(scalar & 0x7Fu);
2021cb0ef41Sopenharmony_ci    return 1;
2031cb0ef41Sopenharmony_ci  } else if (word[0] < 0xC0) {
2041cb0ef41Sopenharmony_ci    /* Continuation / 10AAAAAA. */
2051cb0ef41Sopenharmony_ci    return 1;
2061cb0ef41Sopenharmony_ci  } else if (word[0] < 0xE0) {
2071cb0ef41Sopenharmony_ci    /* 2-byte rune / 110sssss AAssssss / 11 bit scalar. */
2081cb0ef41Sopenharmony_ci    if (word_len < 2) return 1;
2091cb0ef41Sopenharmony_ci    scalar += (uint32_t)((word[1] & 0x3Fu) | ((word[0] & 0x1Fu) << 6u));
2101cb0ef41Sopenharmony_ci    word[0] = (uint8_t)(0xC0 | ((scalar >> 6u) & 0x1F));
2111cb0ef41Sopenharmony_ci    word[1] = (uint8_t)((word[1] & 0xC0) | (scalar & 0x3F));
2121cb0ef41Sopenharmony_ci    return 2;
2131cb0ef41Sopenharmony_ci  } else if (word[0] < 0xF0) {
2141cb0ef41Sopenharmony_ci    /* 3-byte rune / 1110ssss AAssssss BBssssss / 16 bit scalar. */
2151cb0ef41Sopenharmony_ci    if (word_len < 3) return word_len;
2161cb0ef41Sopenharmony_ci    scalar += (uint32_t)((word[2] & 0x3Fu) | ((word[1] & 0x3Fu) << 6u) |
2171cb0ef41Sopenharmony_ci        ((word[0] & 0x0Fu) << 12u));
2181cb0ef41Sopenharmony_ci    word[0] = (uint8_t)(0xE0 | ((scalar >> 12u) & 0x0F));
2191cb0ef41Sopenharmony_ci    word[1] = (uint8_t)((word[1] & 0xC0) | ((scalar >> 6u) & 0x3F));
2201cb0ef41Sopenharmony_ci    word[2] = (uint8_t)((word[2] & 0xC0) | (scalar & 0x3F));
2211cb0ef41Sopenharmony_ci    return 3;
2221cb0ef41Sopenharmony_ci  } else if (word[0] < 0xF8) {
2231cb0ef41Sopenharmony_ci    /* 4-byte rune / 11110sss AAssssss BBssssss CCssssss / 21 bit scalar. */
2241cb0ef41Sopenharmony_ci    if (word_len < 4) return word_len;
2251cb0ef41Sopenharmony_ci    scalar += (uint32_t)((word[3] & 0x3Fu) | ((word[2] & 0x3Fu) << 6u) |
2261cb0ef41Sopenharmony_ci        ((word[1] & 0x3Fu) << 12u) | ((word[0] & 0x07u) << 18u));
2271cb0ef41Sopenharmony_ci    word[0] = (uint8_t)(0xF0 | ((scalar >> 18u) & 0x07));
2281cb0ef41Sopenharmony_ci    word[1] = (uint8_t)((word[1] & 0xC0) | ((scalar >> 12u) & 0x3F));
2291cb0ef41Sopenharmony_ci    word[2] = (uint8_t)((word[2] & 0xC0) | ((scalar >> 6u) & 0x3F));
2301cb0ef41Sopenharmony_ci    word[3] = (uint8_t)((word[3] & 0xC0) | (scalar & 0x3F));
2311cb0ef41Sopenharmony_ci    return 4;
2321cb0ef41Sopenharmony_ci  }
2331cb0ef41Sopenharmony_ci  return 1;
2341cb0ef41Sopenharmony_ci}
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ciint BrotliTransformDictionaryWord(uint8_t* dst, const uint8_t* word, int len,
2371cb0ef41Sopenharmony_ci    const BrotliTransforms* transforms, int transform_idx) {
2381cb0ef41Sopenharmony_ci  int idx = 0;
2391cb0ef41Sopenharmony_ci  const uint8_t* prefix = BROTLI_TRANSFORM_PREFIX(transforms, transform_idx);
2401cb0ef41Sopenharmony_ci  uint8_t type = BROTLI_TRANSFORM_TYPE(transforms, transform_idx);
2411cb0ef41Sopenharmony_ci  const uint8_t* suffix = BROTLI_TRANSFORM_SUFFIX(transforms, transform_idx);
2421cb0ef41Sopenharmony_ci  {
2431cb0ef41Sopenharmony_ci    int prefix_len = *prefix++;
2441cb0ef41Sopenharmony_ci    while (prefix_len--) { dst[idx++] = *prefix++; }
2451cb0ef41Sopenharmony_ci  }
2461cb0ef41Sopenharmony_ci  {
2471cb0ef41Sopenharmony_ci    const int t = type;
2481cb0ef41Sopenharmony_ci    int i = 0;
2491cb0ef41Sopenharmony_ci    if (t <= BROTLI_TRANSFORM_OMIT_LAST_9) {
2501cb0ef41Sopenharmony_ci      len -= t;
2511cb0ef41Sopenharmony_ci    } else if (t >= BROTLI_TRANSFORM_OMIT_FIRST_1
2521cb0ef41Sopenharmony_ci        && t <= BROTLI_TRANSFORM_OMIT_FIRST_9) {
2531cb0ef41Sopenharmony_ci      int skip = t - (BROTLI_TRANSFORM_OMIT_FIRST_1 - 1);
2541cb0ef41Sopenharmony_ci      word += skip;
2551cb0ef41Sopenharmony_ci      len -= skip;
2561cb0ef41Sopenharmony_ci    }
2571cb0ef41Sopenharmony_ci    while (i < len) { dst[idx++] = word[i++]; }
2581cb0ef41Sopenharmony_ci    if (t == BROTLI_TRANSFORM_UPPERCASE_FIRST) {
2591cb0ef41Sopenharmony_ci      ToUpperCase(&dst[idx - len]);
2601cb0ef41Sopenharmony_ci    } else if (t == BROTLI_TRANSFORM_UPPERCASE_ALL) {
2611cb0ef41Sopenharmony_ci      uint8_t* uppercase = &dst[idx - len];
2621cb0ef41Sopenharmony_ci      while (len > 0) {
2631cb0ef41Sopenharmony_ci        int step = ToUpperCase(uppercase);
2641cb0ef41Sopenharmony_ci        uppercase += step;
2651cb0ef41Sopenharmony_ci        len -= step;
2661cb0ef41Sopenharmony_ci      }
2671cb0ef41Sopenharmony_ci    } else if (t == BROTLI_TRANSFORM_SHIFT_FIRST) {
2681cb0ef41Sopenharmony_ci      uint16_t param = (uint16_t)(transforms->params[transform_idx * 2]
2691cb0ef41Sopenharmony_ci          + (transforms->params[transform_idx * 2 + 1] << 8u));
2701cb0ef41Sopenharmony_ci      Shift(&dst[idx - len], len, param);
2711cb0ef41Sopenharmony_ci    } else if (t == BROTLI_TRANSFORM_SHIFT_ALL) {
2721cb0ef41Sopenharmony_ci      uint16_t param = (uint16_t)(transforms->params[transform_idx * 2]
2731cb0ef41Sopenharmony_ci          + (transforms->params[transform_idx * 2 + 1] << 8u));
2741cb0ef41Sopenharmony_ci      uint8_t* shift = &dst[idx - len];
2751cb0ef41Sopenharmony_ci      while (len > 0) {
2761cb0ef41Sopenharmony_ci        int step = Shift(shift, len, param);
2771cb0ef41Sopenharmony_ci        shift += step;
2781cb0ef41Sopenharmony_ci        len -= step;
2791cb0ef41Sopenharmony_ci      }
2801cb0ef41Sopenharmony_ci    }
2811cb0ef41Sopenharmony_ci  }
2821cb0ef41Sopenharmony_ci  {
2831cb0ef41Sopenharmony_ci    int suffix_len = *suffix++;
2841cb0ef41Sopenharmony_ci    while (suffix_len--) { dst[idx++] = *suffix++; }
2851cb0ef41Sopenharmony_ci    return idx;
2861cb0ef41Sopenharmony_ci  }
2871cb0ef41Sopenharmony_ci}
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
2901cb0ef41Sopenharmony_ci}  /* extern "C" */
2911cb0ef41Sopenharmony_ci#endif
292