gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  210. 210
  211. 211
  212. 212
  213. 213
  214. 214
  215. 215
  216. 216
  217. 217
  218. 218
  219. 219
  220. 220
  221. 221
  222. 222
  223. 223
  224. 224
  225. 225
  226. 226
  227. 227
  228. 228
  229. 229
  230. 230
  231. 231
  232. 232
  233. 233
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238
  239. 239
  240. 240
  241. 241
  242. 242
  243. 243
  244. 244
  245. 245
  246. 246
  247. 247
  248. 248
  249. 249
  250. 250
  251. 251
  252. 252
  253. 253
  254. 254
  255. 255
  256. 256
  257. 257
  258. 258
  259. 259
  260. 260
  261. 261
  262. 262
  263. 263
  264. 264
  265. 265
  266. 266
  267. 267
  268. 268
  269. 269
  270. 270
  271. 271
  272. 272
  273. 273
  274. 274
  275. 275
  276. 276
  277. 277
  278. 278
  279. 279
  280. 280
  281. 281
  282. 282
  283. 283
  284. 284
  285. 285
  286. 286
  287. 287
  288. 288
  289. 289
  290. 290
  291. 291
  292. 292
  293. 293
  294. 294
  295. 295
  296. 296
  297. 297
  298. 298
  299. 299
  300. 300
  301. 301
  302. 302
  303. 303
  304. 304
  305. 305
  306. 306
  307. 307
  308. 308
  309. 309
  310. 310
  311. 311
  312. 312
  313. 313
  314. 314
  315. 315
  316. 316
  317. 317
  318. 318
  319. 319
  320. 320
  321. 321
  322. 322
  323. 323
  324. 324
  325. 325
  326. 326
  327. 327
  328. 328
  329. 329
  330. 330
  331. 331
  332. 332
  333. 333
  334. 334
  335. 335
  336. 336
  337. 337
  338. 338
  339. 339
  340. 340
  341. 341
  342. 342
  343. 343
  344. 344
  345. 345
  346. 346
  347. 347
  348. 348
  349. 349
  350. 350
  351. 351
  352. 352
  353. 353
  354. 354
  355. 355
  356. 356
  357. 357
  358. 358
  359. 359
  360. 360
  361. 361
  362. 362
  363. 363
  364. 364
  365. 365
  366. 366
  367. 367
  368. 368
  369. 369
  370. 370
  371. 371
  372. 372
  373. 373
  374. 374
  375. 375
  376. 376
  377. 377
  378. 378
  379. 379
  380. 380
  381. 381
  382. 382
  383. 383
  384. 384
  385. 385
  386. 386
  387. 387
  388. 388
  389. 389
  390. 390
  391. 391
  392. 392
  393. 393
  394. 394
  395. 395
  396. 396
  397. 397
  398. 398
  399. 399
  400. 400
  401. 401
  402. 402
  403. 403
  404. 404
  405. 405
  406. 406
  407. 407
  408. 408
  409. 409
  410. 410
  411. 411
  412. 412
  413. 413
  414. 414
  415. 415
  416. 416
  417. 417
  418. 418
  419. 419
  420. 420
  421. 421
  422. 422
  423. 423
  424. 424
  425. 425
  426. 426
  427. 427
  428. 428
  429. 429
  430. 430
  431. 431
  432. 432
  433. 433
  434. 434
  435. 435
  436. 436
  437. 437
  438. 438
  439. 439
  440. 440
  441. 441
  442. 442
  443. 443
  444. 444
  445. 445
  446. 446
  447. 447
  448. 448
  449. 449
  450. 450
  451. 451
  452. 452
  453. 453
  454. 454
  455. 455
  456. 456
  457. 457
  458. 458
  459. 459
  460. 460
  461. 461
  462. 462
  463. 463
  464. 464
  465. 465
  466. 466
  467. 467
  468. 468
  469. 469
  470. 470
  471. 471
  472. 472
  473. 473
  474. 474
  475. 475
  476. 476
  477. 477
  478. 478
  479. 479
  480. 480
  481. 481
  482. 482
  483. 483
  484. 484
  485. 485
  486. 486
  487. 487
  488. 488
  489. 489
  490. 490
  491. 491
  492. 492
  493. 493
  494. 494
  495. 495
  496. 496
  497. 497
  498. 498
  499. 499
  500. 500
  501. 501
  502. 502
  503. 503
  504. 504
  505. 505
  506. 506
  507. 507
  508. 508
  509. 509
  510. 510
  511. 511
  512. 512
  513. 513
  514. 514
  515. 515
  516. 516
  517. 517
  518. 518
  519. 519
  520. 520
  521. 521
  522. 522
  523. 523
  524. 524
  525. 525
  526. 526
  527. 527
  528. 528
  529. 529
  530. 530
  531. 531
  532. 532
  533. 533
  534. 534
  535. 535
  536. 536
  537. 537
  538. 538
  539. 539
  540. 540
  541. 541
  542. 542
  543. 543
  544. 544
  545. 545
  546. 546
  547. 547
  548. 548
  549. 549
  550. 550
  551. 551
  552. 552
  553. 553
  554. 554
  555. 555
  556. 556
  557. 557
  558. 558
  559. 559
  560. 560
  561. 561
  562. 562
  563. 563
  564. 564
  565. 565
  566. 566
  567. 567
  568. 568
  569. 569
  570. 570
  571. 571
  572. 572
  573. 573
  574. 574
  575. 575
  576. 576
  577. 577
  578. 578
  579. 579
  580. 580
  581. 581
  582. 582
  583. 583
  584. 584
  585. 585
  586. 586
  587. 587
  588. 588
  589. 589
  590. 590
  591. 591
  592. 592
  593. 593
  594. 594
  595. 595
  596. 596
  597. 597
  598. 598
  599. 599
  600. 600
  601. 601
  602. 602
  603. 603
  604. 604
  605. 605
  606. 606
  607. 607
  608. 608
  609. 609
  610. 610
  611. 611
  612. 612
  613. 613
  614. 614
  615. 615
  616. 616
  617. 617
  618. 618
  619. 619
  620. 620
  621. 621
  622. 622
  623. 623
  624. 624
  625. 625
  626. 626
  627. 627
  628. 628
  629. 629
  630. 630
  631. 631
  632. 632
  633. 633
  634. 634
  635. 635
  636. 636
  637. 637
  638. 638
  639. 639
  640. 640
  641. 641
  642. 642
  643. 643
  644. 644
  645. 645
  646. 646
  647. 647
  648. 648
  649. 649
  650. 650
  651. 651
  652. 652
  653. 653
  654. 654
  655. 655
  656. 656
  657. 657
  658. 658
  659. 659
  660. 660
  661. 661
  662. 662
  663. 663
  664. 664
  665. 665
  666. 666
  667. 667
  668. 668
  669. 669
  670. 670
  671. 671
  672. 672
  673. 673
  674. 674
  675. 675
  676. 676
  677. 677
  678. 678
  679. 679
  680. 680
  681. 681
  682. 682
  683. 683
  684. 684
  685. 685
  686. 686
  687. 687
  688. 688
  689. 689
  690. 690
  691. 691
  692. 692
  693. 693
  694. 694
  695. 695
  696. 696
  697. 697
  698. 698
  699. 699
  700. 700
  701. 701
  702. 702
  703. 703
  704. 704
  705. 705
  706. 706
  707. 707
  708. 708
  709. 709
  710. 710
  711. 711
  712. 712
  713. 713
  714. 714
  715. 715
  716. 716
  717. 717
  718. 718
  719. 719
  720. 720
  721. 721
  722. 722
  723. 723
  724. 724
  725. 725
  726. 726
  727. 727
  728. 728
  729. 729
  730. 730
  731. 731
  732. 732
  733. 733
  734. 734
  735. 735
  736. 736
  737. 737
  738. 738
  739. 739
  740. 740
  741. 741
  742. 742
  743. 743
  744. 744
  745. 745
  746. 746
  747. 747
  748. 748
  749. 749
  750. 750
  751. 751
  752. 752
  753. 753
  754. 754
  755. 755
  756. 756
  757. 757
  758. 758
  759. 759
  760. 760
  761. 761
  762. 762
  763. 763
  764. 764
  765. 765
  766. 766
  767. 767
  768. 768
  769. 769
  770. 770
  771. 771
  772. 772
  773. 773
  774. 774
  775. 775
  776. 776
  777. 777
  778. 778
  779. 779
  780. 780
  781. 781
  782. 782
  783. 783
  784. 784
  785. 785
  786. 786
  787. 787
  788. 788
  789. 789
  790. 790
  791. 791
  792. 792
  793. 793
  794. 794
  795. 795
  796. 796
  797. 797
  798. 798
  799. 799
  800. 800
  801. 801
  802. 802
  803. 803
  804. 804
  805. 805
  806. 806
  807. 807
  808. 808
  809. 809
  810. 810
  811. 811
  812. 812
  813. 813
  814. 814
  815. 815
  816. 816
  817. 817
  818. 818
  819. 819
  820. 820
  821. 821
  822. 822
  823. 823
  824. 824
  825. 825
  826. 826
  827. 827
  828. 828
  829. 829
  830. 830
  831. 831
  832. 832
  833. 833
  834. 834
  835. 835
  836. 836
  837. 837
  838. 838
  839. 839
  840. 840
  841. 841
  842. 842
  843. 843
  844. 844
  845. 845
  846. 846
  847. 847
  848. 848
  849. 849
  850. 850
  851. 851
  852. 852
  853. 853
  854. 854
  855. 855
  856. 856
  857. 857
  858. 858
  859. 859
  860. 860
  861. 861
  862. 862
  863. 863
  864. 864
  865. 865
  866. 866
  867. 867
  868. 868
  869. 869
  870. 870
  871. 871
  872. 872
  873. 873
  874. 874
  875. 875
  876. 876
  877. 877
  878. 878
  879. 879
  880. 880
  881. 881
  882. 882
  883. 883
  884. 884
  885. 885
  886. 886
  887. 887
  888. 888
  889. 889
  890. 890
  891. 891
  892. 892
  893. 893
  894. 894
  895. 895
  896. 896
  897. 897
  898. 898
  899. 899
  900. 900
  901. 901
  902. 902
  903. 903
  904. 904
  905. 905
  906. 906
  907. 907
  908. 908
  909. 909
  910. 910
  911. 911
  912. 912
  913. 913
  914. 914
  915. 915
  916. 916
  917. 917
  918. 918
  919. 919
  920. 920
  921. 921
  922. 922
  923. 923
  924. 924
  925. 925
  926. 926
  927. 927
  928. 928
  929. 929
  930. 930
  931. 931
  932. 932
  933. 933
  934. 934
  935. 935
  936. 936
  937. 937
  938. 938
  939. 939
  940. 940
  941. 941
  942. 942
  943. 943
  944. 944
  945. 945
  946. 946
  947. 947
  948. 948
  949. 949
  950. 950
  951. 951
  952. 952
  953. 953
  954. 954
  955. 955
  956. 956
  957. 957
  958. 958
  959. 959
  960. 960
  961. 961
  962. 962
  963. 963
  964. 964
  965. 965
  966. 966
  967. 967
  968. 968
  969. 969
  970. 970
  971. 971
  972. 972
  973. 973
  974. 974
  975. 975
  976. 976
  977. 977
  978. 978
  979. 979
  980. 980
  981. 981
  982. 982
  983. 983
  984. 984
  985. 985
  986. 986
  987. 987
  988. 988
  989. 989
  990. 990
  991. 991
  992. 992
  993. 993
  994. 994
  995. 995
  996. 996
  997. 997
  998. 998
  999. 999
  1000. 1000
  1001. 1001
  1002. 1002
  1003. 1003
  1004. 1004
  1005. 1005
  1006. 1006
  1007. 1007
  1008. 1008
  1009. 1009
  1010. 1010
  1011. 1011
  1012. 1012
  1013. 1013
  1014. 1014
  1015. 1015
  1016. 1016
  1017. 1017
  1018. 1018
  1019. 1019
  1020. 1020
  1021. 1021
  1022. 1022
  1023. 1023
  1024. 1024
  1025. 1025
  1026. 1026
  1027. 1027
  1028. 1028
  1029. 1029
  1030. 1030
  1031. 1031
  1032. 1032
  1033. 1033
  1034. 1034
  1035. 1035
  1036. 1036
  1037. 1037
  1038. 1038
  1039. 1039
  1040. 1040
  1041. 1041
  1042. 1042
  1043. 1043
  1044. 1044
  1045. 1045
  1046. 1046
  1047. 1047
  1048. 1048
  1049. 1049
  1050. 1050
  1051. 1051
  1052. 1052
  1053. 1053
  1054. 1054
  1055. 1055
  1056. 1056
  1057. 1057
  1058. 1058
  1059. 1059
  1060. 1060
  1061. 1061
  1062. 1062
  1063. 1063
  1064. 1064
  1065. 1065
  1066. 1066
  1067. 1067
  1068. 1068
  1069. 1069
  1070. 1070
  1071. 1071
  1072. 1072
  1073. 1073
  1074. 1074
  1075. 1075
  1076. 1076
  1077. 1077
  1078. 1078
  1079. 1079
  1080. 1080
  1081. 1081
  1082. 1082
  1083. 1083
  1084. 1084
  1085. 1085
  1086. 1086
  1087. 1087
  1088. 1088
  1089. 1089
  1090. 1090
  1091. 1091
  1092. 1092
  1093. 1093
  1094. 1094
  1095. 1095
  1096. 1096
  1097. 1097
  1098. 1098
  1099. 1099
  1100. 1100
  1101. 1101
  1102. 1102
  1103. 1103
  1104. 1104
  1105. 1105
  1106. 1106
  1107. 1107
  1108. 1108
  1109. 1109
  1110. 1110
  1111. 1111
  1112. 1112
  1113. 1113
  1114. 1114
  1115. 1115
  1116. 1116
  1117. 1117
  1118. 1118
  1119. 1119
  1120. 1120
  1121. 1121
  1122. 1122
  1123. 1123
  1124. 1124
  1125. 1125
  1126. 1126
  1127. 1127
  1128. 1128
  1129. 1129
  1130. 1130
  1131. 1131
  1132. 1132
  1133. 1133
  1134. 1134
  1135. 1135
  1136. 1136
  1137. 1137
  1138. 1138
  1139. 1139
  1140. 1140
  1141. 1141
  1142. 1142
  1143. 1143
  1144. 1144
  1145. 1145
  1146. 1146
  1147. 1147
  1148. 1148
  1149. 1149
  1150. 1150
  1151. 1151
  1152. 1152
  1153. 1153
  1154. 1154
  1155. 1155
  1156. 1156
  1157. 1157
  1158. 1158
  1159. 1159
  1160. 1160
  1161. 1161
  1162. 1162
  1163. 1163
  1164. 1164
  1165. 1165
  1166. 1166
  1167. 1167
  1168. 1168
  1169. 1169
  1170. 1170
  1171. 1171
  1172. 1172
  1173. 1173
  1174. 1174
  1175. 1175
  1176. 1176
  1177. 1177
  1178. 1178
  1179. 1179
  1180. 1180
  1181. 1181
  1182. 1182
  1183. 1183
  1184. 1184
  1185. 1185
  1186. 1186
  1187. 1187
  1188. 1188
  1189. 1189
  1190. 1190
  1191. 1191
  1192. 1192
  1193. 1193
  1194. 1194
  1195. 1195
  1196. 1196
  1197. 1197
  1198. 1198
  1199. 1199
  1200. 1200
  1201. 1201
  1202. 1202
  1203. 1203
  1204. 1204
  1205. 1205
  1206. 1206
  1207. 1207
  1208. 1208
  1209. 1209
  1210. 1210
  1211. 1211
  1212. 1212
  1213. 1213
  1214. 1214
  1215. 1215
  1216. 1216
  1217. 1217
  1218. 1218
  1219. 1219
  1220. 1220
  1221. 1221
package brotli

import (
	"io"
	"math"
)

/* Copyright 2016 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/** Minimal value for ::BROTLI_PARAM_LGWIN parameter. */
const minWindowBits = 10

/**
 * Maximal value for ::BROTLI_PARAM_LGWIN parameter.
 *
 * @note equal to @c BROTLI_MAX_DISTANCE_BITS constant.
 */
const maxWindowBits = 24

/**
 * Maximal value for ::BROTLI_PARAM_LGWIN parameter
 * in "Large Window Brotli" (32-bit).
 */
const largeMaxWindowBits = 30

/** Minimal value for ::BROTLI_PARAM_LGBLOCK parameter. */
const minInputBlockBits = 16

/** Maximal value for ::BROTLI_PARAM_LGBLOCK parameter. */
const maxInputBlockBits = 24

/** Minimal value for ::BROTLI_PARAM_QUALITY parameter. */
const minQuality = 0

/** Maximal value for ::BROTLI_PARAM_QUALITY parameter. */
const maxQuality = 11

/** Options for ::BROTLI_PARAM_MODE parameter. */
const (
	modeGeneric = 0
	modeText    = 1
	modeFont    = 2
)

/** Default value for ::BROTLI_PARAM_QUALITY parameter. */
const defaultQuality = 11

/** Default value for ::BROTLI_PARAM_LGWIN parameter. */
const defaultWindow = 22

/** Default value for ::BROTLI_PARAM_MODE parameter. */
const defaultMode = modeGeneric

/** Operations that can be performed by streaming encoder. */
const (
	operationProcess      = 0
	operationFlush        = 1
	operationFinish       = 2
	operationEmitMetadata = 3
)

const (
	streamProcessing     = 0
	streamFlushRequested = 1
	streamFinished       = 2
	streamMetadataHead   = 3
	streamMetadataBody   = 4
)

type Writer struct {
	dst     io.Writer
	options WriterOptions
	err     error

	params              encoderParams
	hasher_             hasherHandle
	input_pos_          uint64
	ringbuffer_         ringBuffer
	commands            []command
	num_literals_       uint
	last_insert_len_    uint
	last_flush_pos_     uint64
	last_processed_pos_ uint64
	dist_cache_         [numDistanceShortCodes]int
	saved_dist_cache_   [4]int
	last_bytes_         uint16
	last_bytes_bits_    byte
	prev_byte_          byte
	prev_byte2_         byte
	storage             []byte
	small_table_        [1 << 10]int
	large_table_        []int
	large_table_size_   uint
	cmd_depths_         [128]byte
	cmd_bits_           [128]uint16
	cmd_code_           [512]byte
	cmd_code_numbits_   uint
	command_buf_        []uint32
	literal_buf_        []byte
	tiny_buf_           struct {
		u64 [2]uint64
		u8  [16]byte
	}
	remaining_metadata_bytes_ uint32
	stream_state_             int
	is_last_block_emitted_    bool
	is_initialized_           bool
}

func inputBlockSize(s *Writer) uint {
	return uint(1) << uint(s.params.lgblock)
}

func unprocessedInputSize(s *Writer) uint64 {
	return s.input_pos_ - s.last_processed_pos_
}

func remainingInputBlockSize(s *Writer) uint {
	var delta uint64 = unprocessedInputSize(s)
	var block_size uint = inputBlockSize(s)
	if delta >= uint64(block_size) {
		return 0
	}
	return block_size - uint(delta)
}

/* Wraps 64-bit input position to 32-bit ring-buffer position preserving
   "not-a-first-lap" feature. */
func wrapPosition(position uint64) uint32 {
	var result uint32 = uint32(position)
	var gb uint64 = position >> 30
	if gb > 2 {
		/* Wrap every 2GiB; The first 3GB are continuous. */
		result = result&((1<<30)-1) | (uint32((gb-1)&1)+1)<<30
	}

	return result
}

func (s *Writer) getStorage(size int) []byte {
	if len(s.storage) < size {
		s.storage = make([]byte, size)
	}

	return s.storage
}

func hashTableSize(max_table_size uint, input_size uint) uint {
	var htsize uint = 256
	for htsize < max_table_size && htsize < input_size {
		htsize <<= 1
	}

	return htsize
}

func getHashTable(s *Writer, quality int, input_size uint, table_size *uint) []int {
	var max_table_size uint = maxHashTableSize(quality)
	var htsize uint = hashTableSize(max_table_size, input_size)
	/* Use smaller hash table when input.size() is smaller, since we
	   fill the table, incurring O(hash table size) overhead for
	   compression, and if the input is short, we won't need that
	   many hash table entries anyway. */

	var table []int
	assert(max_table_size >= 256)
	if quality == fastOnePassCompressionQuality {
		/* Only odd shifts are supported by fast-one-pass. */
		if htsize&0xAAAAA == 0 {
			htsize <<= 1
		}
	}

	if htsize <= uint(len(s.small_table_)) {
		table = s.small_table_[:]
	} else {
		if htsize > s.large_table_size_ {
			s.large_table_size_ = htsize
			s.large_table_ = nil
			s.large_table_ = make([]int, htsize)
		}

		table = s.large_table_
	}

	*table_size = htsize
	for i := 0; i < int(htsize); i++ {
		table[i] = 0
	}
	return table
}

func encodeWindowBits(lgwin int, large_window bool, last_bytes *uint16, last_bytes_bits *byte) {
	if large_window {
		*last_bytes = uint16((lgwin&0x3F)<<8 | 0x11)
		*last_bytes_bits = 14
	} else {
		if lgwin == 16 {
			*last_bytes = 0
			*last_bytes_bits = 1
		} else if lgwin == 17 {
			*last_bytes = 1
			*last_bytes_bits = 7
		} else if lgwin > 17 {
			*last_bytes = uint16((lgwin-17)<<1 | 0x01)
			*last_bytes_bits = 4
		} else {
			*last_bytes = uint16((lgwin-8)<<4 | 0x01)
			*last_bytes_bits = 7
		}
	}
}

/* Decide about the context map based on the ability of the prediction
   ability of the previous byte UTF8-prefix on the next byte. The
   prediction ability is calculated as Shannon entropy. Here we need
   Shannon entropy instead of 'BitsEntropy' since the prefix will be
   encoded with the remaining 6 bits of the following byte, and
   BitsEntropy will assume that symbol to be stored alone using Huffman
   coding. */

var kStaticContextMapContinuation = [64]uint32{
	1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
var kStaticContextMapSimpleUTF8 = [64]uint32{
	0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}

func chooseContextMap(quality int, bigram_histo []uint32, num_literal_contexts *uint, literal_context_map *[]uint32) {
	var monogram_histo = [3]uint32{0}
	var two_prefix_histo = [6]uint32{0}
	var total uint
	var i uint
	var dummy uint
	var entropy [4]float64
	for i = 0; i < 9; i++ {
		monogram_histo[i%3] += bigram_histo[i]
		two_prefix_histo[i%6] += bigram_histo[i]
	}

	entropy[1] = shannonEntropy(monogram_histo[:], 3, &dummy)
	entropy[2] = (shannonEntropy(two_prefix_histo[:], 3, &dummy) + shannonEntropy(two_prefix_histo[3:], 3, &dummy))
	entropy[3] = 0
	for i = 0; i < 3; i++ {
		entropy[3] += shannonEntropy(bigram_histo[3*i:], 3, &dummy)
	}

	total = uint(monogram_histo[0] + monogram_histo[1] + monogram_histo[2])
	assert(total != 0)
	entropy[0] = 1.0 / float64(total)
	entropy[1] *= entropy[0]
	entropy[2] *= entropy[0]
	entropy[3] *= entropy[0]

	if quality < minQualityForHqContextModeling {
		/* 3 context models is a bit slower, don't use it at lower qualities. */
		entropy[3] = entropy[1] * 10
	}

	/* If expected savings by symbol are less than 0.2 bits, skip the
	   context modeling -- in exchange for faster decoding speed. */
	if entropy[1]-entropy[2] < 0.2 && entropy[1]-entropy[3] < 0.2 {
		*num_literal_contexts = 1
	} else if entropy[2]-entropy[3] < 0.02 {
		*num_literal_contexts = 2
		*literal_context_map = kStaticContextMapSimpleUTF8[:]
	} else {
		*num_literal_contexts = 3
		*literal_context_map = kStaticContextMapContinuation[:]
	}
}

/* Decide if we want to use a more complex static context map containing 13
   context values, based on the entropy reduction of histograms over the
   first 5 bits of literals. */

var kStaticContextMapComplexUTF8 = [64]uint32{
	11, 11, 12, 12, /* 0 special */
	0, 0, 0, 0, /* 4 lf */
	1, 1, 9, 9, /* 8 space */
	2, 2, 2, 2, /* !, first after space/lf and after something else. */
	1, 1, 1, 1, /* " */
	8, 3, 3, 3, /* % */
	1, 1, 1, 1, /* ({[ */
	2, 2, 2, 2, /* }]) */
	8, 4, 4, 4, /* :; */
	8, 7, 4, 4, /* . */
	8, 0, 0, 0, /* > */
	3, 3, 3, 3, /* [0..9] */
	5, 5, 10, 5, /* [A-Z] */
	5, 5, 10, 5,
	6, 6, 6, 6, /* [a-z] */
	6, 6, 6, 6,
}

func shouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) bool {
	/* Try the more complex static context map only for long data. */
	if size_hint < 1<<20 {
		return false
	} else {
		var end_pos uint = start_pos + length
		var combined_histo = [32]uint32{0}
		var context_histo = [13][32]uint32{[32]uint32{0}}
		var total uint32 = 0
		var entropy [3]float64
		var dummy uint
		var i uint
		var utf8_lut contextLUT = getContextLUT(contextUTF8)
		/* To make entropy calculations faster and to fit on the stack, we collect
		   histograms over the 5 most significant bits of literals. One histogram
		   without context and 13 additional histograms for each context value. */
		for ; start_pos+64 <= end_pos; start_pos += 4096 {
			var stride_end_pos uint = start_pos + 64
			var prev2 byte = input[start_pos&mask]
			var prev1 byte = input[(start_pos+1)&mask]
			var pos uint

			/* To make the analysis of the data faster we only examine 64 byte long
			   strides at every 4kB intervals. */
			for pos = start_pos + 2; pos < stride_end_pos; pos++ {
				var literal byte = input[pos&mask]
				var context byte = byte(kStaticContextMapComplexUTF8[getContext(prev1, prev2, utf8_lut)])
				total++
				combined_histo[literal>>3]++
				context_histo[context][literal>>3]++
				prev2 = prev1
				prev1 = literal
			}
		}

		entropy[1] = shannonEntropy(combined_histo[:], 32, &dummy)
		entropy[2] = 0
		for i = 0; i < 13; i++ {
			entropy[2] += shannonEntropy(context_histo[i][0:], 32, &dummy)
		}

		entropy[0] = 1.0 / float64(total)
		entropy[1] *= entropy[0]
		entropy[2] *= entropy[0]

		/* The triggering heuristics below were tuned by compressing the individual
		   files of the silesia corpus. If we skip this kind of context modeling
		   for not very well compressible input (i.e. entropy using context modeling
		   is 60% of maximal entropy) or if expected savings by symbol are less
		   than 0.2 bits, then in every case when it triggers, the final compression
		   ratio is improved. Note however that this heuristics might be too strict
		   for some cases and could be tuned further. */
		if entropy[2] > 3.0 || entropy[1]-entropy[2] < 0.2 {
			return false
		} else {
			*num_literal_contexts = 13
			*literal_context_map = kStaticContextMapComplexUTF8[:]
			return true
		}
	}
}

func decideOverLiteralContextModeling(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) {
	if quality < minQualityForContextModeling || length < 64 {
		return
	} else if shouldUseComplexStaticContextMap(input, start_pos, length, mask, quality, size_hint, num_literal_contexts, literal_context_map) {
	} else /* Context map was already set, nothing else to do. */
	{
		var end_pos uint = start_pos + length
		/* Gather bi-gram data of the UTF8 byte prefixes. To make the analysis of
		   UTF8 data faster we only examine 64 byte long strides at every 4kB
		   intervals. */

		var bigram_prefix_histo = [9]uint32{0}
		for ; start_pos+64 <= end_pos; start_pos += 4096 {
			var lut = [4]int{0, 0, 1, 2}
			var stride_end_pos uint = start_pos + 64
			var prev int = lut[input[start_pos&mask]>>6] * 3
			var pos uint
			for pos = start_pos + 1; pos < stride_end_pos; pos++ {
				var literal byte = input[pos&mask]
				bigram_prefix_histo[prev+lut[literal>>6]]++
				prev = lut[literal>>6] * 3
			}
		}

		chooseContextMap(quality, bigram_prefix_histo[0:], num_literal_contexts, literal_context_map)
	}
}

func shouldCompress_encode(data []byte, mask uint, last_flush_pos uint64, bytes uint, num_literals uint, num_commands uint) bool {
	/* TODO: find more precise minimal block overhead. */
	if bytes <= 2 {
		return false
	}
	if num_commands < (bytes>>8)+2 {
		if float64(num_literals) > 0.99*float64(bytes) {
			var literal_histo = [256]uint32{0}
			const kSampleRate uint32 = 13
			const kMinEntropy float64 = 7.92
			var bit_cost_threshold float64 = float64(bytes) * kMinEntropy / float64(kSampleRate)
			var t uint = uint((uint32(bytes) + kSampleRate - 1) / kSampleRate)
			var pos uint32 = uint32(last_flush_pos)
			var i uint
			for i = 0; i < t; i++ {
				literal_histo[data[pos&uint32(mask)]]++
				pos += kSampleRate
			}

			if bitsEntropy(literal_histo[:], 256) > bit_cost_threshold {
				return false
			}
		}
	}

	return true
}

/* Chooses the literal context mode for a metablock */
func chooseContextMode(params *encoderParams, data []byte, pos uint, mask uint, length uint) int {
	/* We only do the computation for the option of something else than
	   CONTEXT_UTF8 for the highest qualities */
	if params.quality >= minQualityForHqBlockSplitting && !isMostlyUTF8(data, pos, mask, length, kMinUTF8Ratio) {
		return contextSigned
	}

	return contextUTF8
}

func writeMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes uint, is_last bool, literal_context_mode int, params *encoderParams, prev_byte byte, prev_byte2 byte, num_literals uint, commands []command, saved_dist_cache []int, dist_cache []int, storage_ix *uint, storage []byte) {
	var wrapped_last_flush_pos uint32 = wrapPosition(last_flush_pos)
	var last_bytes uint16
	var last_bytes_bits byte
	var literal_context_lut contextLUT = getContextLUT(literal_context_mode)
	var block_params encoderParams = *params

	if bytes == 0 {
		/* Write the ISLAST and ISEMPTY bits. */
		writeBits(2, 3, storage_ix, storage)

		*storage_ix = (*storage_ix + 7) &^ 7
		return
	}

	if !shouldCompress_encode(data, mask, last_flush_pos, bytes, num_literals, uint(len(commands))) {
		/* Restore the distance cache, as its last update by
		   CreateBackwardReferences is now unused. */
		copy(dist_cache, saved_dist_cache[:4])

		storeUncompressedMetaBlock(is_last, data, uint(wrapped_last_flush_pos), mask, bytes, storage_ix, storage)
		return
	}

	assert(*storage_ix <= 14)
	last_bytes = uint16(storage[1])<<8 | uint16(storage[0])
	last_bytes_bits = byte(*storage_ix)
	if params.quality <= maxQualityForStaticEntropyCodes {
		storeMetaBlockFast(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, storage_ix, storage)
	} else if params.quality < minQualityForBlockSplit {
		storeMetaBlockTrivial(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, storage_ix, storage)
	} else {
		mb := getMetaBlockSplit()
		if params.quality < minQualityForHqBlockSplitting {
			var num_literal_contexts uint = 1
			var literal_context_map []uint32 = nil
			if !params.disable_literal_context_modeling {
				decideOverLiteralContextModeling(data, uint(wrapped_last_flush_pos), bytes, mask, params.quality, params.size_hint, &num_literal_contexts, &literal_context_map)
			}

			buildMetaBlockGreedy(data, uint(wrapped_last_flush_pos), mask, prev_byte, prev_byte2, literal_context_lut, num_literal_contexts, literal_context_map, commands, mb)
		} else {
			buildMetaBlock(data, uint(wrapped_last_flush_pos), mask, &block_params, prev_byte, prev_byte2, commands, literal_context_mode, mb)
		}

		if params.quality >= minQualityForOptimizeHistograms {
			/* The number of distance symbols effectively used for distance
			   histograms. It might be less than distance alphabet size
			   for "Large Window Brotli" (32-bit). */
			var num_effective_dist_codes uint32 = block_params.dist.alphabet_size
			if num_effective_dist_codes > numHistogramDistanceSymbols {
				num_effective_dist_codes = numHistogramDistanceSymbols
			}

			optimizeHistograms(num_effective_dist_codes, mb)
		}

		storeMetaBlock(data, uint(wrapped_last_flush_pos), bytes, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, mb, storage_ix, storage)
		freeMetaBlockSplit(mb)
	}

	if bytes+4 < *storage_ix>>3 {
		/* Restore the distance cache and last byte. */
		copy(dist_cache, saved_dist_cache[:4])

		storage[0] = byte(last_bytes)
		storage[1] = byte(last_bytes >> 8)
		*storage_ix = uint(last_bytes_bits)
		storeUncompressedMetaBlock(is_last, data, uint(wrapped_last_flush_pos), mask, bytes, storage_ix, storage)
	}
}

func chooseDistanceParams(params *encoderParams) {
	var distance_postfix_bits uint32 = 0
	var num_direct_distance_codes uint32 = 0

	if params.quality >= minQualityForNonzeroDistanceParams {
		var ndirect_msb uint32
		if params.mode == modeFont {
			distance_postfix_bits = 1
			num_direct_distance_codes = 12
		} else {
			distance_postfix_bits = params.dist.distance_postfix_bits
			num_direct_distance_codes = params.dist.num_direct_distance_codes
		}

		ndirect_msb = (num_direct_distance_codes >> distance_postfix_bits) & 0x0F
		if distance_postfix_bits > maxNpostfix || num_direct_distance_codes > maxNdirect || ndirect_msb<<distance_postfix_bits != num_direct_distance_codes {
			distance_postfix_bits = 0
			num_direct_distance_codes = 0
		}
	}

	initDistanceParams(params, distance_postfix_bits, num_direct_distance_codes)
}

func ensureInitialized(s *Writer) bool {
	if s.is_initialized_ {
		return true
	}

	s.last_bytes_bits_ = 0
	s.last_bytes_ = 0
	s.remaining_metadata_bytes_ = math.MaxUint32

	sanitizeParams(&s.params)
	s.params.lgblock = computeLgBlock(&s.params)
	chooseDistanceParams(&s.params)

	ringBufferSetup(&s.params, &s.ringbuffer_)

	/* Initialize last byte with stream header. */
	{
		var lgwin int = int(s.params.lgwin)
		if s.params.quality == fastOnePassCompressionQuality || s.params.quality == fastTwoPassCompressionQuality {
			lgwin = brotli_max_int(lgwin, 18)
		}

		encodeWindowBits(lgwin, s.params.large_window, &s.last_bytes_, &s.last_bytes_bits_)
	}

	if s.params.quality == fastOnePassCompressionQuality {
		s.cmd_depths_ = [128]byte{
			0, 4, 4, 5, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
			0, 0, 0, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7,
			7, 7, 10, 10, 10, 10, 10, 10, 0, 4, 4, 5, 5, 5, 6, 6,
			7, 8, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
			5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,
			4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 10,
			12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
		}
		s.cmd_bits_ = [128]uint16{
			0, 0, 8, 9, 3, 35, 7, 71,
			39, 103, 23, 47, 175, 111, 239, 31,
			0, 0, 0, 4, 12, 2, 10, 6,
			13, 29, 11, 43, 27, 59, 87, 55,
			15, 79, 319, 831, 191, 703, 447, 959,
			0, 14, 1, 25, 5, 21, 19, 51,
			119, 159, 95, 223, 479, 991, 63, 575,
			127, 639, 383, 895, 255, 767, 511, 1023,
			14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			27, 59, 7, 39, 23, 55, 30, 1, 17, 9, 25, 5, 0, 8, 4, 12,
			2, 10, 6, 21, 13, 29, 3, 19, 11, 15, 47, 31, 95, 63, 127, 255,
			767, 2815, 1791, 3839, 511, 2559, 1535, 3583, 1023, 3071, 2047, 4095,
		}
		s.cmd_code_ = [512]byte{
			0xff, 0x77, 0xd5, 0xbf, 0xe7, 0xde, 0xea, 0x9e, 0x51, 0x5d, 0xde, 0xc6,
			0x70, 0x57, 0xbc, 0x58, 0x58, 0x58, 0xd8, 0xd8, 0x58, 0xd5, 0xcb, 0x8c,
			0xea, 0xe0, 0xc3, 0x87, 0x1f, 0x83, 0xc1, 0x60, 0x1c, 0x67, 0xb2, 0xaa,
			0x06, 0x83, 0xc1, 0x60, 0x30, 0x18, 0xcc, 0xa1, 0xce, 0x88, 0x54, 0x94,
			0x46, 0xe1, 0xb0, 0xd0, 0x4e, 0xb2, 0xf7, 0x04, 0x00,
		}
		s.cmd_code_numbits_ = 448
	}

	s.is_initialized_ = true
	return true
}

func encoderInitParams(params *encoderParams) {
	params.mode = defaultMode
	params.large_window = false
	params.quality = defaultQuality
	params.lgwin = defaultWindow
	params.lgblock = 0
	params.size_hint = 0
	params.disable_literal_context_modeling = false
	initEncoderDictionary(&params.dictionary)
	params.dist.distance_postfix_bits = 0
	params.dist.num_direct_distance_codes = 0
	params.dist.alphabet_size = uint32(distanceAlphabetSize(0, 0, maxDistanceBits))
	params.dist.max_distance = maxDistance
}

func encoderInitState(s *Writer) {
	encoderInitParams(&s.params)
	s.input_pos_ = 0
	s.commands = s.commands[:0]
	s.num_literals_ = 0
	s.last_insert_len_ = 0
	s.last_flush_pos_ = 0
	s.last_processed_pos_ = 0
	s.prev_byte_ = 0
	s.prev_byte2_ = 0
	if s.hasher_ != nil {
		s.hasher_.Common().is_prepared_ = false
	}
	s.cmd_code_numbits_ = 0
	s.stream_state_ = streamProcessing
	s.is_last_block_emitted_ = false
	s.is_initialized_ = false

	ringBufferInit(&s.ringbuffer_)

	/* Initialize distance cache. */
	s.dist_cache_[0] = 4

	s.dist_cache_[1] = 11
	s.dist_cache_[2] = 15
	s.dist_cache_[3] = 16

	/* Save the state of the distance cache in case we need to restore it for
	   emitting an uncompressed block. */
	copy(s.saved_dist_cache_[:], s.dist_cache_[:])
}

/*
   Copies the given input data to the internal ring buffer of the compressor.
   No processing of the data occurs at this time and this function can be
   called multiple times before calling WriteBrotliData() to process the
   accumulated input. At most input_block_size() bytes of input data can be
   copied to the ring buffer, otherwise the next WriteBrotliData() will fail.
*/
func copyInputToRingBuffer(s *Writer, input_size uint, input_buffer []byte) {
	var ringbuffer_ *ringBuffer = &s.ringbuffer_
	ringBufferWrite(input_buffer, input_size, ringbuffer_)
	s.input_pos_ += uint64(input_size)

	/* TL;DR: If needed, initialize 7 more bytes in the ring buffer to make the
	   hashing not depend on uninitialized data. This makes compression
	   deterministic and it prevents uninitialized memory warnings in Valgrind.
	   Even without erasing, the output would be valid (but nondeterministic).

	   Background information: The compressor stores short (at most 8 bytes)
	   substrings of the input already read in a hash table, and detects
	   repetitions by looking up such substrings in the hash table. If it
	   can find a substring, it checks whether the substring is really there
	   in the ring buffer (or it's just a hash collision). Should the hash
	   table become corrupt, this check makes sure that the output is
	   still valid, albeit the compression ratio would be bad.

	   The compressor populates the hash table from the ring buffer as it's
	   reading new bytes from the input. However, at the last few indexes of
	   the ring buffer, there are not enough bytes to build full-length
	   substrings from. Since the hash table always contains full-length
	   substrings, we erase with dummy zeros here to make sure that those
	   substrings will contain zeros at the end instead of uninitialized
	   data.

	   Please note that erasing is not necessary (because the
	   memory region is already initialized since he ring buffer
	   has a `tail' that holds a copy of the beginning,) so we
	   skip erasing if we have already gone around at least once in
	   the ring buffer.

	   Only clear during the first round of ring-buffer writes. On
	   subsequent rounds data in the ring-buffer would be affected. */
	if ringbuffer_.pos_ <= ringbuffer_.mask_ {
		/* This is the first time when the ring buffer is being written.
		   We clear 7 bytes just after the bytes that have been copied from
		   the input buffer.

		   The ring-buffer has a "tail" that holds a copy of the beginning,
		   but only once the ring buffer has been fully written once, i.e.,
		   pos <= mask. For the first time, we need to write values
		   in this tail (where index may be larger than mask), so that
		   we have exactly defined behavior and don't read uninitialized
		   memory. Due to performance reasons, hashing reads data using a
		   LOAD64, which can go 7 bytes beyond the bytes written in the
		   ring-buffer. */
		for i := 0; i < int(7); i++ {
			ringbuffer_.buffer_[ringbuffer_.pos_:][i] = 0
		}
	}
}

/* Marks all input as processed.
   Returns true if position wrapping occurs. */
func updateLastProcessedPos(s *Writer) bool {
	var wrapped_last_processed_pos uint32 = wrapPosition(s.last_processed_pos_)
	var wrapped_input_pos uint32 = wrapPosition(s.input_pos_)
	s.last_processed_pos_ = s.input_pos_
	return wrapped_input_pos < wrapped_last_processed_pos
}

func extendLastCommand(s *Writer, bytes *uint32, wrapped_last_processed_pos *uint32) {
	var last_command *command = &s.commands[len(s.commands)-1]
	var data []byte = s.ringbuffer_.buffer_
	var mask uint32 = s.ringbuffer_.mask_
	var max_backward_distance uint64 = ((uint64(1)) << s.params.lgwin) - windowGap
	var last_copy_len uint64 = uint64(last_command.copy_len_) & 0x1FFFFFF
	var last_processed_pos uint64 = s.last_processed_pos_ - last_copy_len
	var max_distance uint64
	if last_processed_pos < max_backward_distance {
		max_distance = last_processed_pos
	} else {
		max_distance = max_backward_distance
	}
	var cmd_dist uint64 = uint64(s.dist_cache_[0])
	var distance_code uint32 = commandRestoreDistanceCode(last_command, &s.params.dist)
	if distance_code < numDistanceShortCodes || uint64(distance_code-(numDistanceShortCodes-1)) == cmd_dist {
		if cmd_dist <= max_distance {
			for *bytes != 0 && data[*wrapped_last_processed_pos&mask] == data[(uint64(*wrapped_last_processed_pos)-cmd_dist)&uint64(mask)] {
				last_command.copy_len_++
				(*bytes)--
				(*wrapped_last_processed_pos)++
			}
		}

		/* The copy length is at most the metablock size, and thus expressible. */
		getLengthCode(uint(last_command.insert_len_), uint(int(last_command.copy_len_&0x1FFFFFF)+int(last_command.copy_len_>>25)), (last_command.dist_prefix_&0x3FF == 0), &last_command.cmd_prefix_)
	}
}

/*
   Processes the accumulated input data and writes
   the new output meta-block to s.dest, if one has been
   created (otherwise the processed input data is buffered internally).
   If |is_last| or |force_flush| is true, an output meta-block is
   always created. However, until |is_last| is true encoder may retain up
   to 7 bits of the last byte of output. To force encoder to dump the remaining
   bits use WriteMetadata() to append an empty meta-data block.
   Returns false if the size of the input data is larger than
   input_block_size().
*/
func encodeData(s *Writer, is_last bool, force_flush bool) bool {
	var delta uint64 = unprocessedInputSize(s)
	var bytes uint32 = uint32(delta)
	var wrapped_last_processed_pos uint32 = wrapPosition(s.last_processed_pos_)
	var data []byte
	var mask uint32
	var literal_context_mode int

	data = s.ringbuffer_.buffer_
	mask = s.ringbuffer_.mask_

	/* Adding more blocks after "last" block is forbidden. */
	if s.is_last_block_emitted_ {
		return false
	}
	if is_last {
		s.is_last_block_emitted_ = true
	}

	if delta > uint64(inputBlockSize(s)) {
		return false
	}

	if s.params.quality == fastTwoPassCompressionQuality {
		if s.command_buf_ == nil || cap(s.command_buf_) < int(kCompressFragmentTwoPassBlockSize) {
			s.command_buf_ = make([]uint32, kCompressFragmentTwoPassBlockSize)
			s.literal_buf_ = make([]byte, kCompressFragmentTwoPassBlockSize)
		} else {
			s.command_buf_ = s.command_buf_[:kCompressFragmentTwoPassBlockSize]
			s.literal_buf_ = s.literal_buf_[:kCompressFragmentTwoPassBlockSize]
		}
	}

	if s.params.quality == fastOnePassCompressionQuality || s.params.quality == fastTwoPassCompressionQuality {
		var storage []byte
		var storage_ix uint = uint(s.last_bytes_bits_)
		var table_size uint
		var table []int

		if delta == 0 && !is_last {
			/* We have no new input data and we don't have to finish the stream, so
			   nothing to do. */
			return true
		}

		storage = s.getStorage(int(2*bytes + 503))
		storage[0] = byte(s.last_bytes_)
		storage[1] = byte(s.last_bytes_ >> 8)
		table = getHashTable(s, s.params.quality, uint(bytes), &table_size)
		if s.params.quality == fastOnePassCompressionQuality {
			compressFragmentFast(data[wrapped_last_processed_pos&mask:], uint(bytes), is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage)
		} else {
			compressFragmentTwoPass(data[wrapped_last_processed_pos&mask:], uint(bytes), is_last, s.command_buf_, s.literal_buf_, table, table_size, &storage_ix, storage)
		}

		s.last_bytes_ = uint16(storage[storage_ix>>3])
		s.last_bytes_bits_ = byte(storage_ix & 7)
		updateLastProcessedPos(s)
		s.writeOutput(storage[:storage_ix>>3])
		return true
	}
	{
		/* Theoretical max number of commands is 1 per 2 bytes. */
		newsize := len(s.commands) + int(bytes)/2 + 1
		if newsize > cap(s.commands) {
			/* Reserve a bit more memory to allow merging with a next block
			   without reallocation: that would impact speed. */
			newsize += int(bytes/4) + 16

			new_commands := make([]command, len(s.commands), newsize)
			if s.commands != nil {
				copy(new_commands, s.commands)
			}

			s.commands = new_commands
		}
	}

	initOrStitchToPreviousBlock(&s.hasher_, data, uint(mask), &s.params, uint(wrapped_last_processed_pos), uint(bytes), is_last)

	literal_context_mode = chooseContextMode(&s.params, data, uint(wrapPosition(s.last_flush_pos_)), uint(mask), uint(s.input_pos_-s.last_flush_pos_))

	if len(s.commands) != 0 && s.last_insert_len_ == 0 {
		extendLastCommand(s, &bytes, &wrapped_last_processed_pos)
	}

	if s.params.quality == zopflificationQuality {
		assert(s.params.hasher.type_ == 10)
		createZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_.(*h10), s.dist_cache_[:], &s.last_insert_len_, &s.commands, &s.num_literals_)
	} else if s.params.quality == hqZopflificationQuality {
		assert(s.params.hasher.type_ == 10)
		createHqZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_, s.dist_cache_[:], &s.last_insert_len_, &s.commands, &s.num_literals_)
	} else {
		createBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_, s.dist_cache_[:], &s.last_insert_len_, &s.commands, &s.num_literals_)
	}
	{
		var max_length uint = maxMetablockSize(&s.params)
		var max_literals uint = max_length / 8
		max_commands := int(max_length / 8)
		var processed_bytes uint = uint(s.input_pos_ - s.last_flush_pos_)
		var next_input_fits_metablock bool = (processed_bytes+inputBlockSize(s) <= max_length)
		var should_flush bool = (s.params.quality < minQualityForBlockSplit && s.num_literals_+uint(len(s.commands)) >= maxNumDelayedSymbols)
		/* If maximal possible additional block doesn't fit metablock, flush now. */
		/* TODO: Postpone decision until next block arrives? */

		/* If block splitting is not used, then flush as soon as there is some
		   amount of commands / literals produced. */
		if !is_last && !force_flush && !should_flush && next_input_fits_metablock && s.num_literals_ < max_literals && len(s.commands) < max_commands {
			/* Merge with next input block. Everything will happen later. */
			if updateLastProcessedPos(s) {
				hasherReset(s.hasher_)
			}

			return true
		}
	}

	/* Create the last insert-only command. */
	if s.last_insert_len_ > 0 {
		s.commands = append(s.commands, makeInsertCommand(s.last_insert_len_))
		s.num_literals_ += s.last_insert_len_
		s.last_insert_len_ = 0
	}

	if !is_last && s.input_pos_ == s.last_flush_pos_ {
		/* We have no new input data and we don't have to finish the stream, so
		   nothing to do. */
		return true
	}

	assert(s.input_pos_ >= s.last_flush_pos_)
	assert(s.input_pos_ > s.last_flush_pos_ || is_last)
	assert(s.input_pos_-s.last_flush_pos_ <= 1<<24)
	{
		var metablock_size uint32 = uint32(s.input_pos_ - s.last_flush_pos_)
		var storage []byte = s.getStorage(int(2*metablock_size + 503))
		var storage_ix uint = uint(s.last_bytes_bits_)
		storage[0] = byte(s.last_bytes_)
		storage[1] = byte(s.last_bytes_ >> 8)
		writeMetaBlockInternal(data, uint(mask), s.last_flush_pos_, uint(metablock_size), is_last, literal_context_mode, &s.params, s.prev_byte_, s.prev_byte2_, s.num_literals_, s.commands, s.saved_dist_cache_[:], s.dist_cache_[:], &storage_ix, storage)
		s.last_bytes_ = uint16(storage[storage_ix>>3])
		s.last_bytes_bits_ = byte(storage_ix & 7)
		s.last_flush_pos_ = s.input_pos_
		if updateLastProcessedPos(s) {
			hasherReset(s.hasher_)
		}

		if s.last_flush_pos_ > 0 {
			s.prev_byte_ = data[(uint32(s.last_flush_pos_)-1)&mask]
		}

		if s.last_flush_pos_ > 1 {
			s.prev_byte2_ = data[uint32(s.last_flush_pos_-2)&mask]
		}

		s.commands = s.commands[:0]
		s.num_literals_ = 0

		/* Save the state of the distance cache in case we need to restore it for
		   emitting an uncompressed block. */
		copy(s.saved_dist_cache_[:], s.dist_cache_[:])

		s.writeOutput(storage[:storage_ix>>3])
		return true
	}
}

/* Dumps remaining output bits and metadata header to |header|.
   Returns number of produced bytes.
   REQUIRED: |header| should be 8-byte aligned and at least 16 bytes long.
   REQUIRED: |block_size| <= (1 << 24). */
func writeMetadataHeader(s *Writer, block_size uint, header []byte) uint {
	var storage_ix uint
	storage_ix = uint(s.last_bytes_bits_)
	header[0] = byte(s.last_bytes_)
	header[1] = byte(s.last_bytes_ >> 8)
	s.last_bytes_ = 0
	s.last_bytes_bits_ = 0

	writeBits(1, 0, &storage_ix, header)
	writeBits(2, 3, &storage_ix, header)
	writeBits(1, 0, &storage_ix, header)
	if block_size == 0 {
		writeBits(2, 0, &storage_ix, header)
	} else {
		var nbits uint32
		if block_size == 1 {
			nbits = 0
		} else {
			nbits = log2FloorNonZero(uint(uint32(block_size)-1)) + 1
		}
		var nbytes uint32 = (nbits + 7) / 8
		writeBits(2, uint64(nbytes), &storage_ix, header)
		writeBits(uint(8*nbytes), uint64(block_size)-1, &storage_ix, header)
	}

	return (storage_ix + 7) >> 3
}

func injectBytePaddingBlock(s *Writer) {
	var seal uint32 = uint32(s.last_bytes_)
	var seal_bits uint = uint(s.last_bytes_bits_)
	s.last_bytes_ = 0
	s.last_bytes_bits_ = 0

	/* is_last = 0, data_nibbles = 11, reserved = 0, meta_nibbles = 00 */
	seal |= 0x6 << seal_bits

	seal_bits += 6

	destination := s.tiny_buf_.u8[:]

	destination[0] = byte(seal)
	if seal_bits > 8 {
		destination[1] = byte(seal >> 8)
	}
	if seal_bits > 16 {
		destination[2] = byte(seal >> 16)
	}
	s.writeOutput(destination[:(seal_bits+7)>>3])
}

func checkFlushComplete(s *Writer) {
	if s.stream_state_ == streamFlushRequested && s.err == nil {
		s.stream_state_ = streamProcessing
	}
}

func encoderCompressStreamFast(s *Writer, op int, available_in *uint, next_in *[]byte) bool {
	var block_size_limit uint = uint(1) << s.params.lgwin
	var buf_size uint = brotli_min_size_t(kCompressFragmentTwoPassBlockSize, brotli_min_size_t(*available_in, block_size_limit))
	var command_buf []uint32 = nil
	var literal_buf []byte = nil
	if s.params.quality != fastOnePassCompressionQuality && s.params.quality != fastTwoPassCompressionQuality {
		return false
	}

	if s.params.quality == fastTwoPassCompressionQuality {
		if s.command_buf_ == nil || cap(s.command_buf_) < int(buf_size) {
			s.command_buf_ = make([]uint32, buf_size)
			s.literal_buf_ = make([]byte, buf_size)
		} else {
			s.command_buf_ = s.command_buf_[:buf_size]
			s.literal_buf_ = s.literal_buf_[:buf_size]
		}

		command_buf = s.command_buf_
		literal_buf = s.literal_buf_
	}

	for {
		if s.stream_state_ == streamFlushRequested && s.last_bytes_bits_ != 0 {
			injectBytePaddingBlock(s)
			continue
		}

		/* Compress block only when stream is not
		   finished, there is no pending flush request, and there is either
		   additional input or pending operation. */
		if s.stream_state_ == streamProcessing && (*available_in != 0 || op != int(operationProcess)) {
			var block_size uint = brotli_min_size_t(block_size_limit, *available_in)
			var is_last bool = (*available_in == block_size) && (op == int(operationFinish))
			var force_flush bool = (*available_in == block_size) && (op == int(operationFlush))
			var max_out_size uint = 2*block_size + 503
			var storage []byte = nil
			var storage_ix uint = uint(s.last_bytes_bits_)
			var table_size uint
			var table []int

			if force_flush && block_size == 0 {
				s.stream_state_ = streamFlushRequested
				continue
			}

			storage = s.getStorage(int(max_out_size))

			storage[0] = byte(s.last_bytes_)
			storage[1] = byte(s.last_bytes_ >> 8)
			table = getHashTable(s, s.params.quality, block_size, &table_size)

			if s.params.quality == fastOnePassCompressionQuality {
				compressFragmentFast(*next_in, block_size, is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage)
			} else {
				compressFragmentTwoPass(*next_in, block_size, is_last, command_buf, literal_buf, table, table_size, &storage_ix, storage)
			}

			*next_in = (*next_in)[block_size:]
			*available_in -= block_size
			var out_bytes uint = storage_ix >> 3
			s.writeOutput(storage[:out_bytes])

			s.last_bytes_ = uint16(storage[storage_ix>>3])
			s.last_bytes_bits_ = byte(storage_ix & 7)

			if force_flush {
				s.stream_state_ = streamFlushRequested
			}
			if is_last {
				s.stream_state_ = streamFinished
			}
			continue
		}

		break
	}

	checkFlushComplete(s)
	return true
}

func processMetadata(s *Writer, available_in *uint, next_in *[]byte) bool {
	if *available_in > 1<<24 {
		return false
	}

	/* Switch to metadata block workflow, if required. */
	if s.stream_state_ == streamProcessing {
		s.remaining_metadata_bytes_ = uint32(*available_in)
		s.stream_state_ = streamMetadataHead
	}

	if s.stream_state_ != streamMetadataHead && s.stream_state_ != streamMetadataBody {
		return false
	}

	for {
		if s.stream_state_ == streamFlushRequested && s.last_bytes_bits_ != 0 {
			injectBytePaddingBlock(s)
			continue
		}

		if s.input_pos_ != s.last_flush_pos_ {
			var result bool = encodeData(s, false, true)
			if !result {
				return false
			}
			continue
		}

		if s.stream_state_ == streamMetadataHead {
			n := writeMetadataHeader(s, uint(s.remaining_metadata_bytes_), s.tiny_buf_.u8[:])
			s.writeOutput(s.tiny_buf_.u8[:n])
			s.stream_state_ = streamMetadataBody
			continue
		} else {
			/* Exit workflow only when there is no more input and no more output.
			   Otherwise client may continue producing empty metadata blocks. */
			if s.remaining_metadata_bytes_ == 0 {
				s.remaining_metadata_bytes_ = math.MaxUint32
				s.stream_state_ = streamProcessing
				break
			}

			/* This guarantees progress in "TakeOutput" workflow. */
			var c uint32 = brotli_min_uint32_t(s.remaining_metadata_bytes_, 16)
			copy(s.tiny_buf_.u8[:], (*next_in)[:c])
			*next_in = (*next_in)[c:]
			*available_in -= uint(c)
			s.remaining_metadata_bytes_ -= c
			s.writeOutput(s.tiny_buf_.u8[:c])

			continue
		}
	}

	return true
}

func updateSizeHint(s *Writer, available_in uint) {
	if s.params.size_hint == 0 {
		var delta uint64 = unprocessedInputSize(s)
		var tail uint64 = uint64(available_in)
		var limit uint32 = 1 << 30
		var total uint32
		if (delta >= uint64(limit)) || (tail >= uint64(limit)) || ((delta + tail) >= uint64(limit)) {
			total = limit
		} else {
			total = uint32(delta + tail)
		}

		s.params.size_hint = uint(total)
	}
}

func encoderCompressStream(s *Writer, op int, available_in *uint, next_in *[]byte) bool {
	if !ensureInitialized(s) {
		return false
	}

	/* Unfinished metadata block; check requirements. */
	if s.remaining_metadata_bytes_ != math.MaxUint32 {
		if uint32(*available_in) != s.remaining_metadata_bytes_ {
			return false
		}
		if op != int(operationEmitMetadata) {
			return false
		}
	}

	if op == int(operationEmitMetadata) {
		updateSizeHint(s, 0) /* First data metablock might be emitted here. */
		return processMetadata(s, available_in, next_in)
	}

	if s.stream_state_ == streamMetadataHead || s.stream_state_ == streamMetadataBody {
		return false
	}

	if s.stream_state_ != streamProcessing && *available_in != 0 {
		return false
	}

	if s.params.quality == fastOnePassCompressionQuality || s.params.quality == fastTwoPassCompressionQuality {
		return encoderCompressStreamFast(s, op, available_in, next_in)
	}

	for {
		var remaining_block_size uint = remainingInputBlockSize(s)

		if remaining_block_size != 0 && *available_in != 0 {
			var copy_input_size uint = brotli_min_size_t(remaining_block_size, *available_in)
			copyInputToRingBuffer(s, copy_input_size, *next_in)
			*next_in = (*next_in)[copy_input_size:]
			*available_in -= copy_input_size
			continue
		}

		if s.stream_state_ == streamFlushRequested && s.last_bytes_bits_ != 0 {
			injectBytePaddingBlock(s)
			continue
		}

		/* Compress data only when stream is not
		   finished and there is no pending flush request. */
		if s.stream_state_ == streamProcessing {
			if remaining_block_size == 0 || op != int(operationProcess) {
				var is_last bool = ((*available_in == 0) && op == int(operationFinish))
				var force_flush bool = ((*available_in == 0) && op == int(operationFlush))
				var result bool
				updateSizeHint(s, *available_in)
				result = encodeData(s, is_last, force_flush)
				if !result {
					return false
				}
				if force_flush {
					s.stream_state_ = streamFlushRequested
				}
				if is_last {
					s.stream_state_ = streamFinished
				}
				continue
			}
		}

		break
	}

	checkFlushComplete(s)
	return true
}

func (w *Writer) writeOutput(data []byte) {
	if w.err != nil {
		return
	}

	_, w.err = w.dst.Write(data)
	if w.err == nil {
		checkFlushComplete(w)
	}
}