transform.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. package brotli
  2. const (
  3. transformIdentity = 0
  4. transformOmitLast1 = 1
  5. transformOmitLast2 = 2
  6. transformOmitLast3 = 3
  7. transformOmitLast4 = 4
  8. transformOmitLast5 = 5
  9. transformOmitLast6 = 6
  10. transformOmitLast7 = 7
  11. transformOmitLast8 = 8
  12. transformOmitLast9 = 9
  13. transformUppercaseFirst = 10
  14. transformUppercaseAll = 11
  15. transformOmitFirst1 = 12
  16. transformOmitFirst2 = 13
  17. transformOmitFirst3 = 14
  18. transformOmitFirst4 = 15
  19. transformOmitFirst5 = 16
  20. transformOmitFirst6 = 17
  21. transformOmitFirst7 = 18
  22. transformOmitFirst8 = 19
  23. transformOmitFirst9 = 20
  24. transformShiftFirst = 21
  25. transformShiftAll = 22 + iota - 22
  26. numTransformTypes
  27. )
  28. const transformsMaxCutOff = transformOmitLast9
  29. type transforms struct {
  30. prefix_suffix_size uint16
  31. prefix_suffix []byte
  32. prefix_suffix_map []uint16
  33. num_transforms uint32
  34. transforms []byte
  35. params []byte
  36. cutOffTransforms [transformsMaxCutOff + 1]int16
  37. }
  38. func transformPrefixId(t *transforms, I int) byte {
  39. return t.transforms[(I*3)+0]
  40. }
  41. func transformType(t *transforms, I int) byte {
  42. return t.transforms[(I*3)+1]
  43. }
  44. func transformSuffixId(t *transforms, I int) byte {
  45. return t.transforms[(I*3)+2]
  46. }
  47. func transformPrefix(t *transforms, I int) []byte {
  48. return t.prefix_suffix[t.prefix_suffix_map[transformPrefixId(t, I)]:]
  49. }
  50. func transformSuffix(t *transforms, I int) []byte {
  51. return t.prefix_suffix[t.prefix_suffix_map[transformSuffixId(t, I)]:]
  52. }
  53. /* RFC 7932 transforms string data */
  54. const kPrefixSuffix string = "\001 \002, \010 of the \004 of \002s \001.\005 and \004 " + "in \001\"\004 to \002\">\001\n\002. \001]\005 for \003 a \006 " + "that \001'\006 with \006 from \004 by \001(\006. T" + "he \004 on \004 as \004 is \004ing \002\n\t\001:\003ed " + "\002=\"\004 at \003ly \001,\002='\005.com/\007. This \005" + " not \003er \003al \004ful \004ive \005less \004es" + "t \004ize \002\xc2\xa0\004ous \005 the \002e \000"
  55. var kPrefixSuffixMap = [50]uint16{
  56. 0x00,
  57. 0x02,
  58. 0x05,
  59. 0x0E,
  60. 0x13,
  61. 0x16,
  62. 0x18,
  63. 0x1E,
  64. 0x23,
  65. 0x25,
  66. 0x2A,
  67. 0x2D,
  68. 0x2F,
  69. 0x32,
  70. 0x34,
  71. 0x3A,
  72. 0x3E,
  73. 0x45,
  74. 0x47,
  75. 0x4E,
  76. 0x55,
  77. 0x5A,
  78. 0x5C,
  79. 0x63,
  80. 0x68,
  81. 0x6D,
  82. 0x72,
  83. 0x77,
  84. 0x7A,
  85. 0x7C,
  86. 0x80,
  87. 0x83,
  88. 0x88,
  89. 0x8C,
  90. 0x8E,
  91. 0x91,
  92. 0x97,
  93. 0x9F,
  94. 0xA5,
  95. 0xA9,
  96. 0xAD,
  97. 0xB2,
  98. 0xB7,
  99. 0xBD,
  100. 0xC2,
  101. 0xC7,
  102. 0xCA,
  103. 0xCF,
  104. 0xD5,
  105. 0xD8,
  106. }
  107. /* RFC 7932 transforms */
  108. var kTransformsData = []byte{
  109. 49,
  110. transformIdentity,
  111. 49,
  112. 49,
  113. transformIdentity,
  114. 0,
  115. 0,
  116. transformIdentity,
  117. 0,
  118. 49,
  119. transformOmitFirst1,
  120. 49,
  121. 49,
  122. transformUppercaseFirst,
  123. 0,
  124. 49,
  125. transformIdentity,
  126. 47,
  127. 0,
  128. transformIdentity,
  129. 49,
  130. 4,
  131. transformIdentity,
  132. 0,
  133. 49,
  134. transformIdentity,
  135. 3,
  136. 49,
  137. transformUppercaseFirst,
  138. 49,
  139. 49,
  140. transformIdentity,
  141. 6,
  142. 49,
  143. transformOmitFirst2,
  144. 49,
  145. 49,
  146. transformOmitLast1,
  147. 49,
  148. 1,
  149. transformIdentity,
  150. 0,
  151. 49,
  152. transformIdentity,
  153. 1,
  154. 0,
  155. transformUppercaseFirst,
  156. 0,
  157. 49,
  158. transformIdentity,
  159. 7,
  160. 49,
  161. transformIdentity,
  162. 9,
  163. 48,
  164. transformIdentity,
  165. 0,
  166. 49,
  167. transformIdentity,
  168. 8,
  169. 49,
  170. transformIdentity,
  171. 5,
  172. 49,
  173. transformIdentity,
  174. 10,
  175. 49,
  176. transformIdentity,
  177. 11,
  178. 49,
  179. transformOmitLast3,
  180. 49,
  181. 49,
  182. transformIdentity,
  183. 13,
  184. 49,
  185. transformIdentity,
  186. 14,
  187. 49,
  188. transformOmitFirst3,
  189. 49,
  190. 49,
  191. transformOmitLast2,
  192. 49,
  193. 49,
  194. transformIdentity,
  195. 15,
  196. 49,
  197. transformIdentity,
  198. 16,
  199. 0,
  200. transformUppercaseFirst,
  201. 49,
  202. 49,
  203. transformIdentity,
  204. 12,
  205. 5,
  206. transformIdentity,
  207. 49,
  208. 0,
  209. transformIdentity,
  210. 1,
  211. 49,
  212. transformOmitFirst4,
  213. 49,
  214. 49,
  215. transformIdentity,
  216. 18,
  217. 49,
  218. transformIdentity,
  219. 17,
  220. 49,
  221. transformIdentity,
  222. 19,
  223. 49,
  224. transformIdentity,
  225. 20,
  226. 49,
  227. transformOmitFirst5,
  228. 49,
  229. 49,
  230. transformOmitFirst6,
  231. 49,
  232. 47,
  233. transformIdentity,
  234. 49,
  235. 49,
  236. transformOmitLast4,
  237. 49,
  238. 49,
  239. transformIdentity,
  240. 22,
  241. 49,
  242. transformUppercaseAll,
  243. 49,
  244. 49,
  245. transformIdentity,
  246. 23,
  247. 49,
  248. transformIdentity,
  249. 24,
  250. 49,
  251. transformIdentity,
  252. 25,
  253. 49,
  254. transformOmitLast7,
  255. 49,
  256. 49,
  257. transformOmitLast1,
  258. 26,
  259. 49,
  260. transformIdentity,
  261. 27,
  262. 49,
  263. transformIdentity,
  264. 28,
  265. 0,
  266. transformIdentity,
  267. 12,
  268. 49,
  269. transformIdentity,
  270. 29,
  271. 49,
  272. transformOmitFirst9,
  273. 49,
  274. 49,
  275. transformOmitFirst7,
  276. 49,
  277. 49,
  278. transformOmitLast6,
  279. 49,
  280. 49,
  281. transformIdentity,
  282. 21,
  283. 49,
  284. transformUppercaseFirst,
  285. 1,
  286. 49,
  287. transformOmitLast8,
  288. 49,
  289. 49,
  290. transformIdentity,
  291. 31,
  292. 49,
  293. transformIdentity,
  294. 32,
  295. 47,
  296. transformIdentity,
  297. 3,
  298. 49,
  299. transformOmitLast5,
  300. 49,
  301. 49,
  302. transformOmitLast9,
  303. 49,
  304. 0,
  305. transformUppercaseFirst,
  306. 1,
  307. 49,
  308. transformUppercaseFirst,
  309. 8,
  310. 5,
  311. transformIdentity,
  312. 21,
  313. 49,
  314. transformUppercaseAll,
  315. 0,
  316. 49,
  317. transformUppercaseFirst,
  318. 10,
  319. 49,
  320. transformIdentity,
  321. 30,
  322. 0,
  323. transformIdentity,
  324. 5,
  325. 35,
  326. transformIdentity,
  327. 49,
  328. 47,
  329. transformIdentity,
  330. 2,
  331. 49,
  332. transformUppercaseFirst,
  333. 17,
  334. 49,
  335. transformIdentity,
  336. 36,
  337. 49,
  338. transformIdentity,
  339. 33,
  340. 5,
  341. transformIdentity,
  342. 0,
  343. 49,
  344. transformUppercaseFirst,
  345. 21,
  346. 49,
  347. transformUppercaseFirst,
  348. 5,
  349. 49,
  350. transformIdentity,
  351. 37,
  352. 0,
  353. transformIdentity,
  354. 30,
  355. 49,
  356. transformIdentity,
  357. 38,
  358. 0,
  359. transformUppercaseAll,
  360. 0,
  361. 49,
  362. transformIdentity,
  363. 39,
  364. 0,
  365. transformUppercaseAll,
  366. 49,
  367. 49,
  368. transformIdentity,
  369. 34,
  370. 49,
  371. transformUppercaseAll,
  372. 8,
  373. 49,
  374. transformUppercaseFirst,
  375. 12,
  376. 0,
  377. transformIdentity,
  378. 21,
  379. 49,
  380. transformIdentity,
  381. 40,
  382. 0,
  383. transformUppercaseFirst,
  384. 12,
  385. 49,
  386. transformIdentity,
  387. 41,
  388. 49,
  389. transformIdentity,
  390. 42,
  391. 49,
  392. transformUppercaseAll,
  393. 17,
  394. 49,
  395. transformIdentity,
  396. 43,
  397. 0,
  398. transformUppercaseFirst,
  399. 5,
  400. 49,
  401. transformUppercaseAll,
  402. 10,
  403. 0,
  404. transformIdentity,
  405. 34,
  406. 49,
  407. transformUppercaseFirst,
  408. 33,
  409. 49,
  410. transformIdentity,
  411. 44,
  412. 49,
  413. transformUppercaseAll,
  414. 5,
  415. 45,
  416. transformIdentity,
  417. 49,
  418. 0,
  419. transformIdentity,
  420. 33,
  421. 49,
  422. transformUppercaseFirst,
  423. 30,
  424. 49,
  425. transformUppercaseAll,
  426. 30,
  427. 49,
  428. transformIdentity,
  429. 46,
  430. 49,
  431. transformUppercaseAll,
  432. 1,
  433. 49,
  434. transformUppercaseFirst,
  435. 34,
  436. 0,
  437. transformUppercaseFirst,
  438. 33,
  439. 0,
  440. transformUppercaseAll,
  441. 30,
  442. 0,
  443. transformUppercaseAll,
  444. 1,
  445. 49,
  446. transformUppercaseAll,
  447. 33,
  448. 49,
  449. transformUppercaseAll,
  450. 21,
  451. 49,
  452. transformUppercaseAll,
  453. 12,
  454. 0,
  455. transformUppercaseAll,
  456. 5,
  457. 49,
  458. transformUppercaseAll,
  459. 34,
  460. 0,
  461. transformUppercaseAll,
  462. 12,
  463. 0,
  464. transformUppercaseFirst,
  465. 30,
  466. 0,
  467. transformUppercaseAll,
  468. 34,
  469. 0,
  470. transformUppercaseFirst,
  471. 34,
  472. }
  473. var kBrotliTransforms = transforms{
  474. 217,
  475. []byte(kPrefixSuffix),
  476. kPrefixSuffixMap[:],
  477. 121,
  478. kTransformsData,
  479. nil, /* no extra parameters */
  480. [transformsMaxCutOff + 1]int16{0, 12, 27, 23, 42, 63, 56, 48, 59, 64},
  481. }
  482. func getTransforms() *transforms {
  483. return &kBrotliTransforms
  484. }
  485. func toUpperCase(p []byte) int {
  486. if p[0] < 0xC0 {
  487. if p[0] >= 'a' && p[0] <= 'z' {
  488. p[0] ^= 32
  489. }
  490. return 1
  491. }
  492. /* An overly simplified uppercasing model for UTF-8. */
  493. if p[0] < 0xE0 {
  494. p[1] ^= 32
  495. return 2
  496. }
  497. /* An arbitrary transform for three byte characters. */
  498. p[2] ^= 5
  499. return 3
  500. }
  501. func shiftTransform(word []byte, word_len int, parameter uint16) int {
  502. /* Limited sign extension: scalar < (1 << 24). */
  503. var scalar uint32 = (uint32(parameter) & 0x7FFF) + (0x1000000 - (uint32(parameter) & 0x8000))
  504. if word[0] < 0x80 {
  505. /* 1-byte rune / 0sssssss / 7 bit scalar (ASCII). */
  506. scalar += uint32(word[0])
  507. word[0] = byte(scalar & 0x7F)
  508. return 1
  509. } else if word[0] < 0xC0 {
  510. /* Continuation / 10AAAAAA. */
  511. return 1
  512. } else if word[0] < 0xE0 {
  513. /* 2-byte rune / 110sssss AAssssss / 11 bit scalar. */
  514. if word_len < 2 {
  515. return 1
  516. }
  517. scalar += uint32(word[1]&0x3F | (word[0]&0x1F)<<6)
  518. word[0] = byte(0xC0 | (scalar>>6)&0x1F)
  519. word[1] = byte(uint32(word[1]&0xC0) | scalar&0x3F)
  520. return 2
  521. } else if word[0] < 0xF0 {
  522. /* 3-byte rune / 1110ssss AAssssss BBssssss / 16 bit scalar. */
  523. if word_len < 3 {
  524. return word_len
  525. }
  526. scalar += uint32(word[2])&0x3F | uint32(word[1]&0x3F)<<6 | uint32(word[0]&0x0F)<<12
  527. word[0] = byte(0xE0 | (scalar>>12)&0x0F)
  528. word[1] = byte(uint32(word[1]&0xC0) | (scalar>>6)&0x3F)
  529. word[2] = byte(uint32(word[2]&0xC0) | scalar&0x3F)
  530. return 3
  531. } else if word[0] < 0xF8 {
  532. /* 4-byte rune / 11110sss AAssssss BBssssss CCssssss / 21 bit scalar. */
  533. if word_len < 4 {
  534. return word_len
  535. }
  536. scalar += uint32(word[3])&0x3F | uint32(word[2]&0x3F)<<6 | uint32(word[1]&0x3F)<<12 | uint32(word[0]&0x07)<<18
  537. word[0] = byte(0xF0 | (scalar>>18)&0x07)
  538. word[1] = byte(uint32(word[1]&0xC0) | (scalar>>12)&0x3F)
  539. word[2] = byte(uint32(word[2]&0xC0) | (scalar>>6)&0x3F)
  540. word[3] = byte(uint32(word[3]&0xC0) | scalar&0x3F)
  541. return 4
  542. }
  543. return 1
  544. }
  545. func transformDictionaryWord(dst []byte, word []byte, len int, trans *transforms, transform_idx int) int {
  546. var idx int = 0
  547. var prefix []byte = transformPrefix(trans, transform_idx)
  548. var type_ byte = transformType(trans, transform_idx)
  549. var suffix []byte = transformSuffix(trans, transform_idx)
  550. {
  551. var prefix_len int = int(prefix[0])
  552. prefix = prefix[1:]
  553. for {
  554. tmp1 := prefix_len
  555. prefix_len--
  556. if tmp1 == 0 {
  557. break
  558. }
  559. dst[idx] = prefix[0]
  560. idx++
  561. prefix = prefix[1:]
  562. }
  563. }
  564. {
  565. var t int = int(type_)
  566. var i int = 0
  567. if t <= transformOmitLast9 {
  568. len -= t
  569. } else if t >= transformOmitFirst1 && t <= transformOmitFirst9 {
  570. var skip int = t - (transformOmitFirst1 - 1)
  571. word = word[skip:]
  572. len -= skip
  573. }
  574. for i < len {
  575. dst[idx] = word[i]
  576. idx++
  577. i++
  578. }
  579. if t == transformUppercaseFirst {
  580. toUpperCase(dst[idx-len:])
  581. } else if t == transformUppercaseAll {
  582. var uppercase []byte = dst
  583. uppercase = uppercase[idx-len:]
  584. for len > 0 {
  585. var step int = toUpperCase(uppercase)
  586. uppercase = uppercase[step:]
  587. len -= step
  588. }
  589. } else if t == transformShiftFirst {
  590. var param uint16 = uint16(trans.params[transform_idx*2]) + uint16(trans.params[transform_idx*2+1])<<8
  591. shiftTransform(dst[idx-len:], int(len), param)
  592. } else if t == transformShiftAll {
  593. var param uint16 = uint16(trans.params[transform_idx*2]) + uint16(trans.params[transform_idx*2+1])<<8
  594. var shift []byte = dst
  595. shift = shift[idx-len:]
  596. for len > 0 {
  597. var step int = shiftTransform(shift, int(len), param)
  598. shift = shift[step:]
  599. len -= step
  600. }
  601. }
  602. }
  603. {
  604. var suffix_len int = int(suffix[0])
  605. suffix = suffix[1:]
  606. for {
  607. tmp2 := suffix_len
  608. suffix_len--
  609. if tmp2 == 0 {
  610. break
  611. }
  612. dst[idx] = suffix[0]
  613. idx++
  614. suffix = suffix[1:]
  615. }
  616. return idx
  617. }
  618. }