File _service:obs_scm:pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2.obscpio of Package pkg2zip

7145
 
1
07070100000000000081A4000001ED0000006B000000015EE6501B00000206000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/.appveyor.ymlversion: '#{build}'
2
skip_tags: true
3
4
build: off
5
deploy: off
6
7
environment:
8
  matrix:
9
10
    - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
11
      PATH: C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1\mingw64\bin;%PATH%
12
      CC: gcc
13
14
    - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
15
      PATH: C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin;%PATH%
16
      CC: gcc
17
18
    - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
19
20
test_script:
21
  - if "%CC%" == "" call build.cmd
22
  - if "%CC%" == "gcc" mingw32-make
23
07070100000001000081A4000001ED0000006B000000015EE6501B00000107000000000000000000000000000000000000003700000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/.travis.ymllanguage: c
24
25
matrix:
26
  include:
27
28
    - os: linux
29
      dist: trusty
30
      sudo: false
31
      compiler: gcc
32
33
    - os: linux
34
      dist: trusty
35
      sudo: false
36
      compiler: clang
37
38
    - os: osx
39
      osx_image: xcode9.1
40
      compiler: clang
41
42
script:
43
  - make
44
07070100000002000081A4000001ED0000006B000000015EE6501B000004BA000000000000000000000000000000000000003300000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/LICENSEThis is free and unencumbered software released into the public domain.
45
46
Anyone is free to copy, modify, publish, use, compile, sell, or
47
distribute this software, either in source code form or as a compiled
48
binary, for any purpose, commercial or non-commercial, and by any
49
means.
50
51
In jurisdictions that recognize copyright laws, the author or authors
52
of this software dedicate any and all copyright interest in the
53
software to the public domain. We make this dedication for the benefit
54
of the public at large and to the detriment of our heirs and
55
successors. We intend this dedication to be an overt act of
56
relinquishment in perpetuity of all present and future rights to this
57
software under copyright law.
58
59
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
60
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
61
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
62
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
63
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
64
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
65
OTHER DEALINGS IN THE SOFTWARE.
66
67
For more information, please refer to <http://unlicense.org>
68
07070100000003000081A4000001ED0000006B000000015EE6501B00001826000000000000000000000000000000000000003500000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/README.md# pkg2zip
69
70
[![Travis CI Build Status][img_travis]][travis] [![AppVeyor Build Status][img_appveyor]][appveyor] [![Downloads][img_downloads]][downloads] [![Release][img_latest]][latest] [![License][img_license]][license]
71
72
Utility that decrypts PlayStation Vita pkg file and creates zip package. Supported pkg files - main application, DLC, patch, theme and PSM files. Also supports PSX and PSP pkg files for use with [Adrenaline][].
73
74
Optionally writes [NoNpDrm][] or [NoPsmDrm][] fake license file from zRIF string. You must provide license key.
75
76
# Requirements
77
78
* [Henkaku][] / [Enso][]
79
* [Enso][] for Vita Theme installation via bgdl.
80
* [NoNpDrm][]
81
* [NoPsmDrm][] for PSM titles
82
* [VitaShell][] **v1.76** or newer required for DLC installation
83
* [Adrenaline][] for PSX or PSP titles
84
* [npdrm_free][] for PSP titles in eboot format.
85
86
# Features
87
88
* **portable**, written in cross-platform C code, runs on Windows, GNU/Linux, macOS (system dependent functionality is isolated in sys.c file).
89
* **small**, has no external library dependencies and uses very minimal dynamic memory allocations.
90
* **fast**, uses AESNI hardware accelerated AES decryption if supported by CPU (requires [AESNI][] and [SSSE3][] instructions).
91
* **simple**, creates zip package with same folder structure that Vita expects (just drag & drop all file from zip archive to ux0:). Zip file is created directly from pkg without any intermediate temporary files.
92
* **Vita DLC**, **Vita PATCH** and **PSM** pkg unpacking.
93
* **PSX**, **PSP**, **PSP Updates**, **PSP DLC**, and **PSP THEME** pkg unpacking.
94
95
Limitations:
96
97
* no actual title name is extracted for PSM pkg files.
98
99
# Usage
100
101
If you have zRIF fake license, then execute:
102
103
    pkg2zip package.pkg zRIF_STRING
104
105
This will create `title [id] [region].zip` file. Title, ID and region is automatically detected from pkg file. It will include work.bin file.
106
107
If you don't have zRIF fake license, but just want to unpack files, then omit last argument:
108
109
    pkg2zip package.pkg
110
111
Resulting zip file will not include work.bin. This is useful for patch pkg files.
112
113
To get output file name of the zip, use `-l` (must come before pkg file and cannot be used with `-x`):
114
115
    pkg2zip -l package.pkg
116
117
To avoid zipping process and create individual files, use `-x` argument (must come before pkg file):
118
119
    pkg2zip -x package.pkg [zRIF_STRING]
120
121
To disable bgdl output for VITA Theme extraction, use the '-b' argument.
122
123
    pkg2zip -b -x package.pkg zRIF_STRING
124
125
PSX or PSP pkg files do not require zRIF argument. It will be ignored.
126
127
For PSP files pkg2zip by default will create a .ISO file. To create a compressed .CSO file pass -cN argument where N is compression factor. For example, for fastest compression use:
128
129
    pkg2zip -c1 package.pkg
130
131
To create smaller cso file (more compression will require more time) use -c9, or anything inbetween:
132
133
    pkg2zip -c9 package.pkg
134
135
You can combine -cN argument together with -x:
136
137
    pkg2zip -x -c9 package.pkg
138
139
To extract PSP files in their original EBOOT.PBP format use the '-p' argument: 
140
141
    pkg2zip -p package.pkg
142
Note: On PSP hardware titles with DLC should be kept in ISO/CSO format due to limitations on the CFW NoDRM Engine.
143
144
# Generating zRIF string
145
146
If you have working NoNpDrm license file (work.bin or 6488b73b912a753a492e2714e9b38bc7.rif) you can create zRIF string with `rif2zrif.py` python script:
147
148
    $ python rif2zrif.py path/to/work.bin
149
150
It will print zRIF string to stdout.
151
152
To generate work.bin from zRIF string use `zrif2rif.py` script:
153
154
    $ python zrif2rif.py zRIF work.bin
155
156
Last argument is optional, it specifies where to save file and defaults to work.bin name.
157
158
# Download
159
160
Get latest Windows binaries [here][downloads].
161
162
ArchLinux users can build binary with [pkg2zip][AUR] package in AUR repository. For example, with pacaur:
163
164
    $ pacaur -S pkg2zip
165
166
openSUSE users can download the package from the [Packman](http://packman.links2linux.de/package/pkg2zip) repository.
167
If this repository is enabled, just install pkg2zip with zypper.
168
169
    # zypper install pkg2zip
170
171
# Building
172
173
Execute `make` if you are on GNU/Linux or macOS (gcc comiler required)
174
To install the resulting file use `make "install` as root
175
176
On Windows you can build either with MinGW (get [MinGW-w64][]) or [Visual Studio 2017 Community Edition][vs2017ce].
177
* for MinGW make sure you have make installed, and then execute `mingw32-make`
178
* for Visual Studio run `build.cmd`
179
180
# Alternatives
181
182
* https://github.com/RikuKH3/unpkg_vita
183
* https://github.com/St4rk/PkgDecrypt
184
* https://github.com/weaknespase/PkgDecrypt
185
186
# License
187
188
This is free and unencumbered software released into the public domain.
189
190
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
191
192
[travis]: https://travis-ci.org/mmozeiko/pkg2zip/
193
[appveyor]: https://ci.appveyor.com/project/mmozeiko/pkg2zip/
194
[downloads]: https://github.com/lusid1/pkg2zip/releases
195
[latest]: https://github.com/lusid1/pkg2zip/releases/latest
196
[license]: https://github.com/lusid1/pkg2zip/blob/master/LICENSE
197
[img_travis]: https://api.travis-ci.org/mmozeiko/pkg2zip.svg?branch=master
198
[img_appveyor]: https://ci.appveyor.com/api/projects/status/xmkl6509ahlp9b7k/branch/master?svg=true
199
[img_downloads]: https://img.shields.io/github/downloads/lusid1/pkg2zip/total.svg?maxAge=3600
200
[img_latest]: https://img.shields.io/github/release/lusid1/pkg2zip.svg?maxAge=3600
201
[img_license]: https://img.shields.io/github/license/mmozeiko/pkg2zip.svg?maxAge=2592000
202
[Adrenaline]: https://github.com/TheOfficialFloW/Adrenaline
203
[NoNpDrm]: https://github.com/TheOfficialFloW/NoNpDrm
204
[NoPsmDrm]: https://github.com/frangarcj/NoPsmDrm
205
[Henkaku]: https://henkaku.xyz/
206
[Enso]: https://enso.henkaku.xyz/
207
[VitaShell]: https://github.com/TheOfficialFloW/VitaShell
208
[AESNI]: https://en.wikipedia.org/wiki/AES_instruction_set
209
[SSSE3]: https://en.wikipedia.org/wiki/SSSE3
210
[AUR]: https://aur.archlinux.org/packages/pkg2zip/
211
[MinGW-w64]: http://www.msys2.org/
212
[vs2017ce]: https://www.visualstudio.com/vs/community/
213
[npdrm_free]: https://github.com/qwikrazor87/npdrm_free
214
07070100000004000081A4000001ED0000006B000000015EE6501B0000019E000000000000000000000000000000000000003500000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/build.cmd@echo off
215
216
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" -arch=amd64 -host_arch=amd64
217
cd /d "%~dp0"
218
219
set CL=/nologo /errorReport:none /Gm- /GF /GS- /MP /MT /W4 /WX /wd4324 /D_CRT_SECURE_NO_DEPRECATE
220
set LINK=/errorReport:none /INCREMENTAL:NO
221
222
set CL=%CL% /Ox
223
rem set CL=%CL% /Od /Zi
224
rem set LINK=%LINK% /DEBUG
225
226
cl.exe pkg2zip*.c miniz_tdef.c puff.c /Fepkg2zip.exe
227
07070100000005000081A4000001ED0000006B000000015EE6501B0000049F000000000000000000000000000000000000003400000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/makefileifeq ($(OS),Windows_NT)
228
  RM := del /q
229
  EXE := .exe
230
else
231
  EXE :=
232
endif
233
234
DESTDIR =
235
PREFIX = /usr/local
236
BINDIR = $(PREFIX)/bin
237
238
BIN = pkg2zip${EXE}
239
SRC = ${wildcard pkg2zip*.c} miniz_tdef.c puff.c
240
OBJ = ${SRC:.c=.o}
241
DEP = ${SRC:.c=.d}
242
243
CFLAGS = -std=c99 -pipe -fvisibility=hidden -Wall -Wextra -Werror -DNDEBUG -D_GNU_SOURCE -O2
244
${BIN}: LDFLAGS += -s
245
debug: CFLAGS += -g
246
debug: LDFLAGS += -g
247
248
.PHONY: all clean
249
250
all: ${BIN}
251
252
install:
253
    install -o root -g root -m 0755 ${BIN} $(DESTDIR)$(BINDIR)/${BIN}
254
    install -o root -g root -m 0755 rif2zrif.py $(DESTDIR)$(BINDIR)/rif2zrif
255
    install -o root -g root -m 0755 zrif2rif.py $(DESTDIR)$(BINDIR)/zrif2rif
256
257
uninstall:
258
    rm -f $(DESTDIR)$(BINDIR)/${BIN}
259
    rm -f $(DESTDIR)$(BINDIR)/rif2zrif
260
    rm -f $(DESTDIR)$(BINDIR)/zrif2rif
261
262
clean:
263
    @${RM} ${BIN} ${OBJ} ${DEP}
264
265
${BIN}: ${OBJ}
266
    @echo [L] $@
267
    @${CC} ${LDFLAGS} -o $@ $^
268
269
debug: ${OBJ}
270
    @echo [L] ${BIN}
271
    @${CC} ${LDFLAGS} -o ${BIN} $^
272
273
%aes_x86.o: %aes_x86.c
274
    @echo [C] $<
275
    @${CC} ${CFLAGS} -maes -mssse3 -MMD -c -o $@ $<
276
277
%crc32_x86.o: %crc32_x86.c
278
    @echo [C] $<
279
    @${CC} ${CFLAGS} -mpclmul -msse4 -MMD -c -o $@ $<
280
281
%.o: %.c
282
    @echo [C] $<
283
    @${CC} ${CFLAGS} -MMD -c -o $@ $<
284
285
-include ${DEP}
286
07070100000006000081A4000001ED0000006B000000015EE6501B0000F0A2000000000000000000000000000000000000003800000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/miniz_tdef.c#include "miniz_tdef.h"
287
288
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
289
/* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */
290
#define MINIZ_X86_OR_X64_CPU 1
291
#else
292
#define MINIZ_X86_OR_X64_CPU 0
293
#endif
294
295
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
296
/* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */
297
#define MINIZ_LITTLE_ENDIAN 1
298
#else
299
#define MINIZ_LITTLE_ENDIAN 0
300
#endif
301
302
#if MINIZ_X86_OR_X64_CPU
303
/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. */
304
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
305
#else
306
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
307
#endif
308
309
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
310
/* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions). */
311
#define MINIZ_HAS_64BIT_REGISTERS 1
312
#else
313
#define MINIZ_HAS_64BIT_REGISTERS 0
314
#endif
315
316
// ------------------- Low-level Compression (independent from all decompression API's)
317
318
// Purposely making these tables static for faster init and thread safety.
319
static const mz_uint16 s_tdefl_len_sym[256] =
320
    {
321
        257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272, 272, 272,
322
        273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274, 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276, 276, 276, 276, 276,
323
        277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278,
324
        279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280,
325
        281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281,
326
        282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282,
327
        283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283,
328
        284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 285
329
    };
330
331
static const mz_uint8 s_tdefl_len_extra[256] =
332
    {
333
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
334
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
335
        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
336
        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0
337
    };
338
339
static const mz_uint8 s_tdefl_small_dist_sym[512] =
340
    {
341
        0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11,
342
        11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13,
343
        13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
344
        14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
345
        14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
346
        15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
347
        16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
348
        16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
349
        16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
350
        17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
351
        17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
352
        17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17
353
    };
354
355
static const mz_uint8 s_tdefl_small_dist_extra[512] =
356
    {
357
        0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
358
        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
359
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
360
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
361
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
362
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
363
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
364
        7, 7, 7, 7, 7, 7, 7, 7
365
    };
366
367
static const mz_uint8 s_tdefl_large_dist_sym[128] =
368
    {
369
        0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
370
        26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
371
        28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
372
    };
373
374
static const mz_uint8 s_tdefl_large_dist_extra[128] =
375
    {
376
        0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
377
        12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
378
        13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13
379
    };
380
381
// Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values.
382
typedef struct
383
{
384
    mz_uint16 m_key, m_sym_index;
385
} tdefl_sym_freq;
386
static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1)
387
{
388
    mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
389
    tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;
390
    MZ_CLEAR_OBJ(hist);
391
    for (i = 0; i < num_syms; i++)
392
    {
393
        mz_uint freq = pSyms0[i].m_key;
394
        hist[freq & 0xFF]++;
395
        hist[256 + ((freq >> 8) & 0xFF)]++;
396
    }
397
    while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
398
        total_passes--;
399
    for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)
400
    {
401
        const mz_uint32 *pHist = &hist[pass << 8];
402
        mz_uint offsets[256], cur_ofs = 0;
403
        for (i = 0; i < 256; i++)
404
        {
405
            offsets[i] = cur_ofs;
406
            cur_ofs += pHist[i];
407
        }
408
        for (i = 0; i < num_syms; i++)
409
            pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
410
        {
411
            tdefl_sym_freq *t = pCur_syms;
412
            pCur_syms = pNew_syms;
413
            pNew_syms = t;
414
        }
415
    }
416
    return pCur_syms;
417
}
418
419
// tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996.
420
static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n)
421
{
422
    int root, leaf, next, avbl, used, dpth;
423
    if (n == 0)
424
        return;
425
    else if (n == 1)
426
    {
427
        A[0].m_key = 1;
428
        return;
429
    }
430
    A[0].m_key += A[1].m_key;
431
    root = 0;
432
    leaf = 2;
433
    for (next = 1; next < n - 1; next++)
434
    {
435
        if (leaf >= n || A[root].m_key < A[leaf].m_key)
436
        {
437
            A[next].m_key = A[root].m_key;
438
            A[root++].m_key = (mz_uint16)next;
439
        }
440
        else
441
            A[next].m_key = A[leaf++].m_key;
442
        if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key))
443
        {
444
            A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key);
445
            A[root++].m_key = (mz_uint16)next;
446
        }
447
        else
448
            A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);
449
    }
450
    A[n - 2].m_key = 0;
451
    for (next = n - 3; next >= 0; next--)
452
        A[next].m_key = A[A[next].m_key].m_key + 1;
453
    avbl = 1;
454
    used = dpth = 0;
455
    root = n - 2;
456
    next = n - 1;
457
    while (avbl > 0)
458
    {
459
        while (root >= 0 && (int)A[root].m_key == dpth)
460
        {
461
            used++;
462
            root--;
463
        }
464
        while (avbl > used)
465
        {
466
            A[next--].m_key = (mz_uint16)(dpth);
467
            avbl--;
468
        }
469
        avbl = 2 * used;
470
        dpth++;
471
        used = 0;
472
    }
473
}
474
475
// Limits canonical Huffman code table's max code size.
476
enum
477
{
478
    TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32
479
};
480
static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)
481
{
482
    int i;
483
    mz_uint32 total = 0;
484
    if (code_list_len <= 1)
485
        return;
486
    for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++)
487
        pNum_codes[max_code_size] += pNum_codes[i];
488
    for (i = max_code_size; i > 0; i--)
489
        total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));
490
    while (total != (1UL << max_code_size))
491
    {
492
        pNum_codes[max_code_size]--;
493
        for (i = max_code_size - 1; i > 0; i--)
494
            if (pNum_codes[i])
495
            {
496
                pNum_codes[i]--;
497
                pNum_codes[i + 1] += 2;
498
                break;
499
            }
500
        total--;
501
    }
502
}
503
504
static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)
505
{
506
    int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE];
507
    mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1];
508
    MZ_CLEAR_OBJ(num_codes);
509
    if (static_table)
510
    {
511
        for (i = 0; i < table_len; i++)
512
            num_codes[d->m_huff_code_sizes[table_num][i]]++;
513
    }
514
    else
515
    {
516
        tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms;
517
        int num_used_syms = 0;
518
        const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0];
519
        for (i = 0; i < table_len; i++)
520
            if (pSym_count[i])
521
            {
522
                syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i];
523
                syms0[num_used_syms++].m_sym_index = (mz_uint16)i;
524
            }
525
526
        pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);
527
        tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);
528
529
        for (i = 0; i < num_used_syms; i++)
530
            num_codes[pSyms[i].m_key]++;
531
532
        tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);
533
534
        MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]);
535
        MZ_CLEAR_OBJ(d->m_huff_codes[table_num]);
536
        for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
537
            for (l = num_codes[i]; l > 0; l--)
538
                d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);
539
    }
540
541
    next_code[1] = 0;
542
    for (j = 0, i = 2; i <= code_size_limit; i++)
543
        next_code[i] = j = ((j + num_codes[i - 1]) << 1);
544
545
    for (i = 0; i < table_len; i++)
546
    {
547
        mz_uint rev_code = 0, code, code_size;
548
        if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0)
549
            continue;
550
        code = next_code[code_size]++;
551
        for (l = code_size; l > 0; l--, code >>= 1)
552
            rev_code = (rev_code << 1) | (code & 1);
553
        d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;
554
    }
555
}
556
557
#define TDEFL_PUT_BITS(b, l)                                       \
558
    do                                                             \
559
    {                                                              \
560
        mz_uint bits = b;                                          \
561
        mz_uint len = l;                                           \
562
        MZ_ASSERT(bits <= ((1U << len) - 1U));                     \
563
        d->m_bit_buffer |= (bits << d->m_bits_in);                 \
564
        d->m_bits_in += len;                                       \
565
        while (d->m_bits_in >= 8)                                  \
566
        {                                                          \
567
            if (d->m_pOutput_buf < d->m_pOutput_buf_end)           \
568
                *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
569
            d->m_bit_buffer >>= 8;                                 \
570
            d->m_bits_in -= 8;                                     \
571
        }                                                          \
572
    }                                                              \
573
    MZ_MACRO_END
574
575
#define TDEFL_RLE_PREV_CODE_SIZE()                                                                                       \
576
    {                                                                                                                    \
577
        if (rle_repeat_count)                                                                                            \
578
        {                                                                                                                \
579
            if (rle_repeat_count < 3)                                                                                    \
580
            {                                                                                                            \
581
                d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
582
                while (rle_repeat_count--)                                                                               \
583
                    packed_code_sizes[num_packed_code_sizes++] = prev_code_size;                                         \
584
            }                                                                                                            \
585
            else                                                                                                         \
586
            {                                                                                                            \
587
                d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1);                                        \
588
                packed_code_sizes[num_packed_code_sizes++] = 16;                                                         \
589
                packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3);                           \
590
            }                                                                                                            \
591
            rle_repeat_count = 0;                                                                                        \
592
        }                                                                                                                \
593
    }
594
595
#define TDEFL_RLE_ZERO_CODE_SIZE()                                                         \
596
    {                                                                                      \
597
        if (rle_z_count)                                                                   \
598
        {                                                                                  \
599
            if (rle_z_count < 3)                                                           \
600
            {                                                                              \
601
                d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count);  \
602
                while (rle_z_count--)                                                      \
603
                    packed_code_sizes[num_packed_code_sizes++] = 0;                        \
604
            }                                                                              \
605
            else if (rle_z_count <= 10)                                                    \
606
            {                                                                              \
607
                d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1);          \
608
                packed_code_sizes[num_packed_code_sizes++] = 17;                           \
609
                packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3);  \
610
            }                                                                              \
611
            else                                                                           \
612
            {                                                                              \
613
                d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1);          \
614
                packed_code_sizes[num_packed_code_sizes++] = 18;                           \
615
                packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
616
            }                                                                              \
617
            rle_z_count = 0;                                                               \
618
        }                                                                                  \
619
    }
620
621
static mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
622
623
static void tdefl_start_dynamic_block(tdefl_compressor *d)
624
{
625
    int num_lit_codes, num_dist_codes, num_bit_lengths;
626
    mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
627
    mz_uint8 code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], prev_code_size = 0xFF;
628
629
    d->m_huff_count[0][256] = 1;
630
631
    tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE);
632
    tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE);
633
634
    for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
635
        if (d->m_huff_code_sizes[0][num_lit_codes - 1])
636
            break;
637
    for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
638
        if (d->m_huff_code_sizes[1][num_dist_codes - 1])
639
            break;
640
641
    memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
642
    memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);
643
    total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
644
    num_packed_code_sizes = 0;
645
    rle_z_count = 0;
646
    rle_repeat_count = 0;
647
648
    memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2);
649
    for (i = 0; i < total_code_sizes_to_pack; i++)
650
    {
651
        mz_uint8 code_size = code_sizes_to_pack[i];
652
        if (!code_size)
653
        {
654
            TDEFL_RLE_PREV_CODE_SIZE();
655
            if (++rle_z_count == 138)
656
            {
657
                TDEFL_RLE_ZERO_CODE_SIZE();
658
            }
659
        }
660
        else
661
        {
662
            TDEFL_RLE_ZERO_CODE_SIZE();
663
            if (code_size != prev_code_size)
664
            {
665
                TDEFL_RLE_PREV_CODE_SIZE();
666
                d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1);
667
                packed_code_sizes[num_packed_code_sizes++] = code_size;
668
            }
669
            else if (++rle_repeat_count == 6)
670
            {
671
                TDEFL_RLE_PREV_CODE_SIZE();
672
            }
673
        }
674
        prev_code_size = code_size;
675
    }
676
    if (rle_repeat_count)
677
    {
678
        TDEFL_RLE_PREV_CODE_SIZE();
679
    }
680
    else
681
    {
682
        TDEFL_RLE_ZERO_CODE_SIZE();
683
    }
684
685
    tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE);
686
687
    TDEFL_PUT_BITS(2, 2);
688
689
    TDEFL_PUT_BITS(num_lit_codes - 257, 5);
690
    TDEFL_PUT_BITS(num_dist_codes - 1, 5);
691
692
    for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
693
        if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
694
            break;
695
    num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));
696
    TDEFL_PUT_BITS(num_bit_lengths - 4, 4);
697
    for (i = 0; (int)i < num_bit_lengths; i++)
698
        TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3);
699
700
    for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;)
701
    {
702
        mz_uint code = packed_code_sizes[packed_code_sizes_index++];
703
        MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2);
704
        TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]);
705
        if (code >= 16)
706
            TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]);
707
    }
708
}
709
710
static void tdefl_start_static_block(tdefl_compressor *d)
711
{
712
    mz_uint i;
713
    mz_uint8 *p = &d->m_huff_code_sizes[0][0];
714
715
    for (i = 0; i <= 143; ++i)
716
        *p++ = 8;
717
    for (; i <= 255; ++i)
718
        *p++ = 9;
719
    for (; i <= 279; ++i)
720
        *p++ = 7;
721
    for (; i <= 287; ++i)
722
        *p++ = 8;
723
724
    memset(d->m_huff_code_sizes[1], 5, 32);
725
726
    tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE);
727
    tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE);
728
729
    TDEFL_PUT_BITS(1, 2);
730
}
731
732
static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
733
734
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
735
static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
736
{
737
    mz_uint flags;
738
    mz_uint8 *pLZ_codes;
739
    mz_uint8 *pOutput_buf = d->m_pOutput_buf;
740
    mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf;
741
    mz_uint64 bit_buffer = d->m_bit_buffer;
742
    mz_uint bits_in = d->m_bits_in;
743
744
#define TDEFL_PUT_BITS_FAST(b, l)                    \
745
    {                                                \
746
        bit_buffer |= (((mz_uint64)(b)) << bits_in); \
747
        bits_in += (l);                              \
748
    }
749
750
    flags = 1;
751
    for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1)
752
    {
753
        if (flags == 1)
754
            flags = *pLZ_codes++ | 0x100;
755
756
        if (flags & 1)
757
        {
758
            mz_uint s0, s1, n0, n1, sym, num_extra_bits;
759
            mz_uint match_len = pLZ_codes[0], match_dist = *(const mz_uint16 *)(pLZ_codes + 1);
760
            pLZ_codes += 3;
761
762
            MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
763
            TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
764
            TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
765
766
            // This sequence coaxes MSVC into using cmov's vs. jmp's.
767
            s0 = s_tdefl_small_dist_sym[match_dist & 511];
768
            n0 = s_tdefl_small_dist_extra[match_dist & 511];
769
            s1 = s_tdefl_large_dist_sym[match_dist >> 8];
770
            n1 = s_tdefl_large_dist_extra[match_dist >> 8];
771
            sym = (match_dist < 512) ? s0 : s1;
772
            num_extra_bits = (match_dist < 512) ? n0 : n1;
773
774
            MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
775
            TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
776
            TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
777
        }
778
        else
779
        {
780
            mz_uint lit = *pLZ_codes++;
781
            MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
782
            TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
783
784
            if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
785
            {
786
                flags >>= 1;
787
                lit = *pLZ_codes++;
788
                MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
789
                TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
790
791
                if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
792
                {
793
                    flags >>= 1;
794
                    lit = *pLZ_codes++;
795
                    MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
796
                    TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
797
                }
798
            }
799
        }
800
801
        if (pOutput_buf >= d->m_pOutput_buf_end)
802
            return MZ_FALSE;
803
804
        *(mz_uint64 *)pOutput_buf = bit_buffer;
805
        pOutput_buf += (bits_in >> 3);
806
        bit_buffer >>= (bits_in & ~7);
807
        bits_in &= 7;
808
    }
809
810
#undef TDEFL_PUT_BITS_FAST
811
812
    d->m_pOutput_buf = pOutput_buf;
813
    d->m_bits_in = 0;
814
    d->m_bit_buffer = 0;
815
816
    while (bits_in)
817
    {
818
        mz_uint32 n = MZ_MIN(bits_in, 16);
819
        TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n);
820
        bit_buffer >>= n;
821
        bits_in -= n;
822
    }
823
824
    TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
825
826
    return (d->m_pOutput_buf < d->m_pOutput_buf_end);
827
}
828
#else
829
static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
830
{
831
    mz_uint flags;
832
    mz_uint8 *pLZ_codes;
833
834
    flags = 1;
835
    for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1)
836
    {
837
        if (flags == 1)
838
            flags = *pLZ_codes++ | 0x100;
839
        if (flags & 1)
840
        {
841
            mz_uint sym, num_extra_bits;
842
            mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
843
            pLZ_codes += 3;
844
845
            MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
846
            TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
847
            TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
848
849
            if (match_dist < 512)
850
            {
851
                sym = s_tdefl_small_dist_sym[match_dist];
852
                num_extra_bits = s_tdefl_small_dist_extra[match_dist];
853
            }
854
            else
855
            {
856
                sym = s_tdefl_large_dist_sym[match_dist >> 8];
857
                num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
858
            }
859
            MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
860
            TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
861
            TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
862
        }
863
        else
864
        {
865
            mz_uint lit = *pLZ_codes++;
866
            MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
867
            TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
868
        }
869
    }
870
871
    TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
872
873
    return (d->m_pOutput_buf < d->m_pOutput_buf_end);
874
}
875
#endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
876
877
static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block)
878
{
879
    if (static_block)
880
        tdefl_start_static_block(d);
881
    else
882
        tdefl_start_dynamic_block(d);
883
    return tdefl_compress_lz_codes(d);
884
}
885
886
static int tdefl_flush_block(tdefl_compressor *d, int flush)
887
{
888
    mz_uint saved_bit_buf, saved_bits_in;
889
    mz_uint8 *pSaved_output_buf;
890
    mz_bool comp_block_succeeded = MZ_FALSE;
891
    int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size;
892
    mz_uint8 *pOutput_buf_start = ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf;
893
894
    d->m_pOutput_buf = pOutput_buf_start;
895
    d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;
896
897
    MZ_ASSERT(!d->m_output_flush_remaining);
898
    d->m_output_flush_ofs = 0;
899
    d->m_output_flush_remaining = 0;
900
901
    *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left);
902
    d->m_pLZ_code_buf -= (d->m_num_flags_left == 8);
903
904
    if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index))
905
    {
906
        TDEFL_PUT_BITS(0x78, 8);
907
        TDEFL_PUT_BITS(0x01, 8);
908
    }
909
910
    TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1);
911
912
    pSaved_output_buf = d->m_pOutput_buf;
913
    saved_bit_buf = d->m_bit_buffer;
914
    saved_bits_in = d->m_bits_in;
915
916
    if (!use_raw_block)
917
        comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48));
918
919
    // If the block gets expanded, forget the current contents of the output buffer and send a raw block instead.
920
    if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) &&
921
        ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size))
922
    {
923
        mz_uint i;
924
        d->m_pOutput_buf = pSaved_output_buf;
925
        d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
926
        TDEFL_PUT_BITS(0, 2);
927
        if (d->m_bits_in)
928
        {
929
            TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
930
        }
931
        for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF)
932
        {
933
            TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16);
934
        }
935
        for (i = 0; i < d->m_total_lz_bytes; ++i)
936
        {
937
            TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8);
938
        }
939
    }
940
    // Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes.
941
    else if (!comp_block_succeeded)
942
    {
943
        d->m_pOutput_buf = pSaved_output_buf;
944
        d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
945
        tdefl_compress_block(d, MZ_TRUE);
946
    }
947
948
    if (flush)
949
    {
950
        if (flush == TDEFL_FINISH)
951
        {
952
            if (d->m_bits_in)
953
            {
954
                TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
955
            }
956
            if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER)
957
            {
958
                mz_uint i, a = d->m_adler32;
959
                for (i = 0; i < 4; i++)
960
                {
961
                    TDEFL_PUT_BITS((a >> 24) & 0xFF, 8);
962
                    a <<= 8;
963
                }
964
            }
965
        }
966
        else
967
        {
968
            mz_uint i, z = 0;
969
            TDEFL_PUT_BITS(0, 3);
970
            if (d->m_bits_in)
971
            {
972
                TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
973
            }
974
            for (i = 2; i; --i, z ^= 0xFFFF)
975
            {
976
                TDEFL_PUT_BITS(z & 0xFFFF, 16);
977
            }
978
        }
979
    }
980
981
    MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);
982
983
    memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
984
    memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
985
986
    d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
987
    d->m_pLZ_flags = d->m_lz_code_buf;
988
    d->m_num_flags_left = 8;
989
    d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes;
990
    d->m_total_lz_bytes = 0;
991
    d->m_block_index++;
992
993
    if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0)
994
    {
995
        if (pOutput_buf_start == d->m_output_buf)
996
        {
997
            int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));
998
            memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);
999
            d->m_out_buf_ofs += bytes_to_copy;
1000
            if ((n -= bytes_to_copy) != 0)
1001
            {
1002
                d->m_output_flush_ofs = bytes_to_copy;
1003
                d->m_output_flush_remaining = n;
1004
            }
1005
        }
1006
        else
1007
        {
1008
            d->m_out_buf_ofs += n;
1009
        }
1010
    }
1011
1012
    return d->m_output_flush_remaining;
1013
}
1014
1015
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1016
static MZ_FORCEINLINE mz_uint16 TDEFL_READ_UNALIGNED_WORD(const void* p)
1017
{
1018
    return *(const mz_uint16 *)p;
1019
}
1020
1021
static MZ_FORCEINLINE mz_uint32 TDEFL_READ_UNALIGNED_DWORD(const void* p)
1022
{
1023
    return *(const mz_uint32 *)p;
1024
}
1025
1026
static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)
1027
{
1028
    mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
1029
    mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
1030
    const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q;
1031
    mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s);
1032
    MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
1033
    if (max_match_len <= match_len)
1034
        return;
1035
    for (;;)
1036
    {
1037
        for (;;)
1038
        {
1039
            if (--num_probes_left == 0)
1040
                return;
1041
#define TDEFL_PROBE                                                                             \
1042
    next_probe_pos = d->m_next[probe_pos];                                                      \
1043
    if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1044
        return;                                                                                 \
1045
    probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK;                                       \
1046
    if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01)                \
1047
        break;
1048
            TDEFL_PROBE;
1049
            TDEFL_PROBE;
1050
            TDEFL_PROBE;
1051
        }
1052
        if (!dist)
1053
            break;
1054
        q = (const mz_uint16 *)(d->m_dict + probe_pos);
1055
        if (TDEFL_READ_UNALIGNED_WORD(q) != s01)
1056
            continue;
1057
        p = s;
1058
        probe_len = 32;
1059
        do
1060
        {
1061
        } while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
1062
                 (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
1063
        if (!probe_len)
1064
        {
1065
            *pMatch_dist = dist;
1066
            *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);
1067
            break;
1068
        }
1069
        else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q)) > match_len)
1070
        {
1071
            *pMatch_dist = dist;
1072
            if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len)
1073
                break;
1074
            c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
1075
        }
1076
    }
1077
}
1078
#else
1079
static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)
1080
{
1081
    mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
1082
    mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
1083
    const mz_uint8 *s = d->m_dict + pos, *p, *q;
1084
    mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1];
1085
    MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
1086
    if (max_match_len <= match_len)
1087
        return;
1088
    for (;;)
1089
    {
1090
        for (;;)
1091
        {
1092
            if (--num_probes_left == 0)
1093
                return;
1094
#define TDEFL_PROBE                                                                               \
1095
    next_probe_pos = d->m_next[probe_pos];                                                        \
1096
    if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist))   \
1097
        return;                                                                                   \
1098
    probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK;                                         \
1099
    if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \
1100
        break;
1101
            TDEFL_PROBE;
1102
            TDEFL_PROBE;
1103
            TDEFL_PROBE;
1104
        }
1105
        if (!dist)
1106
            break;
1107
        p = s;
1108
        q = d->m_dict + probe_pos;
1109
        for (probe_len = 0; probe_len < max_match_len; probe_len++)
1110
            if (*p++ != *q++)
1111
                break;
1112
        if (probe_len > match_len)
1113
        {
1114
            *pMatch_dist = dist;
1115
            if ((*pMatch_len = match_len = probe_len) == max_match_len)
1116
                return;
1117
            c0 = d->m_dict[pos + match_len];
1118
            c1 = d->m_dict[pos + match_len - 1];
1119
        }
1120
    }
1121
}
1122
#endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1123
1124
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1125
static mz_bool tdefl_compress_fast(tdefl_compressor *d)
1126
{
1127
    // Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio.
1128
    mz_uint lookahead_pos = d->m_lookahead_pos, lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, total_lz_bytes = d->m_total_lz_bytes, num_flags_left = d->m_num_flags_left;
1129
    mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags;
1130
    mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
1131
1132
    while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size)))
1133
    {
1134
        const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
1135
        mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
1136
        mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size);
1137
        d->m_src_buf_left -= num_bytes_to_process;
1138
        lookahead_size += num_bytes_to_process;
1139
1140
        while (num_bytes_to_process)
1141
        {
1142
            mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);
1143
            memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
1144
            if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
1145
                memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
1146
            d->m_pSrc += n;
1147
            dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
1148
            num_bytes_to_process -= n;
1149
        }
1150
1151
        dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size);
1152
        if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
1153
            break;
1154
1155
        while (lookahead_size >= 4)
1156
        {
1157
            mz_uint cur_match_dist, cur_match_len = 1;
1158
            mz_uint8 *pCur_dict = d->m_dict + cur_pos;
1159
            mz_uint first_trigram = (*(const mz_uint32 *)pCur_dict) & 0xFFFFFF;
1160
            mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK;
1161
            mz_uint probe_pos = d->m_hash[hash];
1162
            d->m_hash[hash] = (mz_uint16)lookahead_pos;
1163
1164
            if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((TDEFL_READ_UNALIGNED_DWORD(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram))
1165
            {
1166
                const mz_uint16 *p = (const mz_uint16 *)pCur_dict;
1167
                const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos);
1168
                mz_uint32 probe_len = 32;
1169
                do
1170
                {
1171
                } while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
1172
                         (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
1173
                cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q);
1174
                if (!probe_len)
1175
                    cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
1176
1177
                if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)))
1178
                {
1179
                    cur_match_len = 1;
1180
                    *pLZ_code_buf++ = (mz_uint8)first_trigram;
1181
                    *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1182
                    d->m_huff_count[0][(mz_uint8)first_trigram]++;
1183
                }
1184
                else
1185
                {
1186
                    mz_uint32 s0, s1;
1187
                    cur_match_len = MZ_MIN(cur_match_len, lookahead_size);
1188
1189
                    MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE));
1190
1191
                    cur_match_dist--;
1192
1193
                    pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN);
1194
                    *(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;
1195
                    pLZ_code_buf += 3;
1196
                    *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);
1197
1198
                    s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
1199
                    s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
1200
                    d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
1201
1202
                    d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++;
1203
                }
1204
            }
1205
            else
1206
            {
1207
                *pLZ_code_buf++ = (mz_uint8)first_trigram;
1208
                *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1209
                d->m_huff_count[0][(mz_uint8)first_trigram]++;
1210
            }
1211
1212
            if (--num_flags_left == 0)
1213
            {
1214
                num_flags_left = 8;
1215
                pLZ_flags = pLZ_code_buf++;
1216
            }
1217
1218
            total_lz_bytes += cur_match_len;
1219
            lookahead_pos += cur_match_len;
1220
            dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE);
1221
            cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;
1222
            MZ_ASSERT(lookahead_size >= cur_match_len);
1223
            lookahead_size -= cur_match_len;
1224
1225
            if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
1226
            {
1227
                int n;
1228
                d->m_lookahead_pos = lookahead_pos;
1229
                d->m_lookahead_size = lookahead_size;
1230
                d->m_dict_size = dict_size;
1231
                d->m_total_lz_bytes = total_lz_bytes;
1232
                d->m_pLZ_code_buf = pLZ_code_buf;
1233
                d->m_pLZ_flags = pLZ_flags;
1234
                d->m_num_flags_left = num_flags_left;
1235
                if ((n = tdefl_flush_block(d, 0)) != 0)
1236
                    return (n < 0) ? MZ_FALSE : MZ_TRUE;
1237
                total_lz_bytes = d->m_total_lz_bytes;
1238
                pLZ_code_buf = d->m_pLZ_code_buf;
1239
                pLZ_flags = d->m_pLZ_flags;
1240
                num_flags_left = d->m_num_flags_left;
1241
            }
1242
        }
1243
1244
        while (lookahead_size)
1245
        {
1246
            mz_uint8 lit = d->m_dict[cur_pos];
1247
1248
            total_lz_bytes++;
1249
            *pLZ_code_buf++ = lit;
1250
            *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1251
            if (--num_flags_left == 0)
1252
            {
1253
                num_flags_left = 8;
1254
                pLZ_flags = pLZ_code_buf++;
1255
            }
1256
1257
            d->m_huff_count[0][lit]++;
1258
1259
            lookahead_pos++;
1260
            dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE);
1261
            cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
1262
            lookahead_size--;
1263
1264
            if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
1265
            {
1266
                int n;
1267
                d->m_lookahead_pos = lookahead_pos;
1268
                d->m_lookahead_size = lookahead_size;
1269
                d->m_dict_size = dict_size;
1270
                d->m_total_lz_bytes = total_lz_bytes;
1271
                d->m_pLZ_code_buf = pLZ_code_buf;
1272
                d->m_pLZ_flags = pLZ_flags;
1273
                d->m_num_flags_left = num_flags_left;
1274
                if ((n = tdefl_flush_block(d, 0)) != 0)
1275
                    return (n < 0) ? MZ_FALSE : MZ_TRUE;
1276
                total_lz_bytes = d->m_total_lz_bytes;
1277
                pLZ_code_buf = d->m_pLZ_code_buf;
1278
                pLZ_flags = d->m_pLZ_flags;
1279
                num_flags_left = d->m_num_flags_left;
1280
            }
1281
        }
1282
    }
1283
1284
    d->m_lookahead_pos = lookahead_pos;
1285
    d->m_lookahead_size = lookahead_size;
1286
    d->m_dict_size = dict_size;
1287
    d->m_total_lz_bytes = total_lz_bytes;
1288
    d->m_pLZ_code_buf = pLZ_code_buf;
1289
    d->m_pLZ_flags = pLZ_flags;
1290
    d->m_num_flags_left = num_flags_left;
1291
    return MZ_TRUE;
1292
}
1293
#endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1294
1295
static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit)
1296
{
1297
    d->m_total_lz_bytes++;
1298
    *d->m_pLZ_code_buf++ = lit;
1299
    *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1);
1300
    if (--d->m_num_flags_left == 0)
1301
    {
1302
        d->m_num_flags_left = 8;
1303
        d->m_pLZ_flags = d->m_pLZ_code_buf++;
1304
    }
1305
    d->m_huff_count[0][lit]++;
1306
}
1307
1308
static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist)
1309
{
1310
    mz_uint32 s0, s1;
1311
1312
    MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE));
1313
1314
    d->m_total_lz_bytes += match_len;
1315
1316
    d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);
1317
1318
    match_dist -= 1;
1319
    d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF);
1320
    d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8);
1321
    d->m_pLZ_code_buf += 3;
1322
1323
    *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80);
1324
    if (--d->m_num_flags_left == 0)
1325
    {
1326
        d->m_num_flags_left = 8;
1327
        d->m_pLZ_flags = d->m_pLZ_code_buf++;
1328
    }
1329
1330
    s0 = s_tdefl_small_dist_sym[match_dist & 511];
1331
    s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
1332
    d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++;
1333
1334
    if (match_len >= TDEFL_MIN_MATCH_LEN)
1335
        d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++;
1336
}
1337
1338
static mz_bool tdefl_compress_normal(tdefl_compressor *d)
1339
{
1340
    const mz_uint8 *pSrc = d->m_pSrc;
1341
    size_t src_buf_left = d->m_src_buf_left;
1342
    tdefl_flush flush = d->m_flush;
1343
1344
    while ((src_buf_left) || ((flush) && (d->m_lookahead_size)))
1345
    {
1346
        mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
1347
        // Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN.
1348
        if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1))
1349
        {
1350
            mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK, ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2;
1351
            mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK];
1352
            mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size);
1353
            const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process;
1354
            src_buf_left -= num_bytes_to_process;
1355
            d->m_lookahead_size += num_bytes_to_process;
1356
            while (pSrc != pSrc_end)
1357
            {
1358
                mz_uint8 c = *pSrc++;
1359
                d->m_dict[dst_pos] = c;
1360
                if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
1361
                    d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
1362
                hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
1363
                d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
1364
                d->m_hash[hash] = (mz_uint16)(ins_pos);
1365
                dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
1366
                ins_pos++;
1367
            }
1368
        }
1369
        else
1370
        {
1371
            while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
1372
            {
1373
                mz_uint8 c = *pSrc++;
1374
                mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
1375
                src_buf_left--;
1376
                d->m_dict[dst_pos] = c;
1377
                if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
1378
                    d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
1379
                if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN)
1380
                {
1381
                    mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2;
1382
                    mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << (TDEFL_LZ_HASH_SHIFT * 2)) ^ (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
1383
                    d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
1384
                    d->m_hash[hash] = (mz_uint16)(ins_pos);
1385
                }
1386
            }
1387
        }
1388
        d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size);
1389
        if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
1390
            break;
1391
1392
        // Simple lazy/greedy parsing state machine.
1393
        len_to_move = 1;
1394
        cur_match_dist = 0;
1395
        cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1);
1396
        cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
1397
        if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS))
1398
        {
1399
            if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))
1400
            {
1401
                mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];
1402
                cur_match_len = 0;
1403
                while (cur_match_len < d->m_lookahead_size)
1404
                {
1405
                    if (d->m_dict[cur_pos + cur_match_len] != c)
1406
                        break;
1407
                    cur_match_len++;
1408
                }
1409
                if (cur_match_len < TDEFL_MIN_MATCH_LEN)
1410
                    cur_match_len = 0;
1411
                else
1412
                    cur_match_dist = 1;
1413
            }
1414
        }
1415
        else
1416
        {
1417
            tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len);
1418
        }
1419
        if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5)))
1420
        {
1421
            cur_match_dist = cur_match_len = 0;
1422
        }
1423
        if (d->m_saved_match_len)
1424
        {
1425
            if (cur_match_len > d->m_saved_match_len)
1426
            {
1427
                tdefl_record_literal(d, (mz_uint8)d->m_saved_lit);
1428
                if (cur_match_len >= 128)
1429
                {
1430
                    tdefl_record_match(d, cur_match_len, cur_match_dist);
1431
                    d->m_saved_match_len = 0;
1432
                    len_to_move = cur_match_len;
1433
                }
1434
                else
1435
                {
1436
                    d->m_saved_lit = d->m_dict[cur_pos];
1437
                    d->m_saved_match_dist = cur_match_dist;
1438
                    d->m_saved_match_len = cur_match_len;
1439
                }
1440
            }
1441
            else
1442
            {
1443
                tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist);
1444
                len_to_move = d->m_saved_match_len - 1;
1445
                d->m_saved_match_len = 0;
1446
            }
1447
        }
1448
        else if (!cur_match_dist)
1449
            tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]);
1450
        else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128))
1451
        {
1452
            tdefl_record_match(d, cur_match_len, cur_match_dist);
1453
            len_to_move = cur_match_len;
1454
        }
1455
        else
1456
        {
1457
            d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)];
1458
            d->m_saved_match_dist = cur_match_dist;
1459
            d->m_saved_match_len = cur_match_len;
1460
        }
1461
        // Move the lookahead forward by len_to_move bytes.
1462
        d->m_lookahead_pos += len_to_move;
1463
        MZ_ASSERT(d->m_lookahead_size >= len_to_move);
1464
        d->m_lookahead_size -= len_to_move;
1465
        d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE);
1466
        // Check if it's time to flush the current LZ codes to the internal output buffer.
1467
        if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) ||
1468
            ((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))))
1469
        {
1470
            int n;
1471
            d->m_pSrc = pSrc;
1472
            d->m_src_buf_left = src_buf_left;
1473
            if ((n = tdefl_flush_block(d, 0)) != 0)
1474
                return (n < 0) ? MZ_FALSE : MZ_TRUE;
1475
        }
1476
    }
1477
1478
    d->m_pSrc = pSrc;
1479
    d->m_src_buf_left = src_buf_left;
1480
    return MZ_TRUE;
1481
}
1482
1483
static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)
1484
{
1485
    if (d->m_pIn_buf_size)
1486
    {
1487
        *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
1488
    }
1489
1490
    if (d->m_pOut_buf_size)
1491
    {
1492
        size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining);
1493
        memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);
1494
        d->m_output_flush_ofs += (mz_uint)n;
1495
        d->m_output_flush_remaining -= (mz_uint)n;
1496
        d->m_out_buf_ofs += n;
1497
1498
        *d->m_pOut_buf_size = d->m_out_buf_ofs;
1499
    }
1500
1501
    return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY;
1502
}
1503
1504
tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush)
1505
{
1506
    if (!d)
1507
    {
1508
        if (pIn_buf_size)
1509
            *pIn_buf_size = 0;
1510
        if (pOut_buf_size)
1511
            *pOut_buf_size = 0;
1512
        return TDEFL_STATUS_BAD_PARAM;
1513
    }
1514
1515
    d->m_pIn_buf = pIn_buf;
1516
    d->m_pIn_buf_size = pIn_buf_size;
1517
    d->m_pOut_buf = pOut_buf;
1518
    d->m_pOut_buf_size = pOut_buf_size;
1519
    d->m_pSrc = (const mz_uint8 *)(pIn_buf);
1520
    d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0;
1521
    d->m_out_buf_ofs = 0;
1522
    d->m_flush = flush;
1523
1524
    if (((0) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) ||
1525
        (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf))
1526
    {
1527
        if (pIn_buf_size)
1528
            *pIn_buf_size = 0;
1529
        if (pOut_buf_size)
1530
            *pOut_buf_size = 0;
1531
        return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);
1532
    }
1533
    d->m_wants_to_finish |= (flush == TDEFL_FINISH);
1534
1535
    if ((d->m_output_flush_remaining) || (d->m_finished))
1536
        return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
1537
1538
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1539
    if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) &&
1540
        ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) &&
1541
        ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0))
1542
    {
1543
        if (!tdefl_compress_fast(d))
1544
            return d->m_prev_return_status;
1545
    }
1546
    else
1547
#endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1548
    {
1549
        if (!tdefl_compress_normal(d))
1550
            return d->m_prev_return_status;
1551
    }
1552
1553
    if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf))
1554
        d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, d->m_pSrc - (const mz_uint8 *)pIn_buf);
1555
1556
    if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining))
1557
    {
1558
        if (tdefl_flush_block(d, flush) < 0)
1559
            return d->m_prev_return_status;
1560
        d->m_finished = (flush == TDEFL_FINISH);
1561
        if (flush == TDEFL_FULL_FLUSH)
1562
        {
1563
            MZ_CLEAR_OBJ(d->m_hash);
1564
            MZ_CLEAR_OBJ(d->m_next);
1565
            d->m_dict_size = 0;
1566
        }
1567
    }
1568
1569
    return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
1570
}
1571
1572
tdefl_status tdefl_init(tdefl_compressor *d, int flags)
1573
{
1574
    d->m_flags = (mz_uint)(flags);
1575
    d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3;
1576
    d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0;
1577
    d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
1578
    if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
1579
        MZ_CLEAR_OBJ(d->m_hash);
1580
    d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0;
1581
    d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0;
1582
    d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
1583
    d->m_pLZ_flags = d->m_lz_code_buf;
1584
    d->m_num_flags_left = 8;
1585
    d->m_pOutput_buf = d->m_output_buf;
1586
    d->m_pOutput_buf_end = d->m_output_buf;
1587
    d->m_prev_return_status = TDEFL_STATUS_OKAY;
1588
    d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0;
1589
    d->m_adler32 = 1;
1590
    d->m_pIn_buf = NULL;
1591
    d->m_pOut_buf = NULL;
1592
    d->m_pIn_buf_size = NULL;
1593
    d->m_pOut_buf_size = NULL;
1594
    d->m_flush = TDEFL_NO_FLUSH;
1595
    d->m_pSrc = NULL;
1596
    d->m_src_buf_left = 0;
1597
    d->m_out_buf_ofs = 0;
1598
    memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
1599
    memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
1600
    return TDEFL_STATUS_OKAY;
1601
}
1602
1603
static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
1604
1605
// level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files).
1606
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)
1607
{
1608
    mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
1609
    if (window_bits > 0)
1610
        comp_flags |= TDEFL_WRITE_ZLIB_HEADER;
1611
1612
    if (!level)
1613
        comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
1614
    else if (strategy == MZ_FILTERED)
1615
        comp_flags |= TDEFL_FILTER_MATCHES;
1616
    else if (strategy == MZ_HUFFMAN_ONLY)
1617
        comp_flags &= ~TDEFL_MAX_PROBES_MASK;
1618
    else if (strategy == MZ_FIXED)
1619
        comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;
1620
    else if (strategy == MZ_RLE)
1621
        comp_flags |= TDEFL_RLE_MATCHES;
1622
1623
    return comp_flags;
1624
}
1625
1626
mz_uint32 mz_adler32(mz_uint32 adler, const unsigned char *ptr, size_t buf_len)
1627
{
1628
    mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16);
1629
    size_t block_len = buf_len % 5552;
1630
    if (!ptr)
1631
        return MZ_ADLER32_INIT;
1632
    while (buf_len)
1633
    {
1634
        for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
1635
        {
1636
            s1 += ptr[0], s2 += s1;
1637
            s1 += ptr[1], s2 += s1;
1638
            s1 += ptr[2], s2 += s1;
1639
            s1 += ptr[3], s2 += s1;
1640
            s1 += ptr[4], s2 += s1;
1641
            s1 += ptr[5], s2 += s1;
1642
            s1 += ptr[6], s2 += s1;
1643
            s1 += ptr[7], s2 += s1;
1644
        }
1645
        for (; i < block_len; ++i)
1646
            s1 += *ptr++, s2 += s1;
1647
        s1 %= 65521U, s2 %= 65521U;
1648
        buf_len -= block_len;
1649
        block_len = 5552;
1650
    }
1651
    return (s2 << 16) + s1;
1652
}
1653
07070100000007000081A4000001ED0000006B000000015EE6501B00001B7F000000000000000000000000000000000000003800000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/miniz_tdef.h#pragma once
1654
1655
// public domain code from https://github.com/richgel999/miniz/tree/42c29103c304a6e9201d000c96c31cd3031efc4f
1656
1657
#include <string.h>
1658
#include <stdint.h>
1659
1660
// ------------------- Types and macros
1661
typedef uint8_t mz_uint8;
1662
typedef int16_t mz_int16;
1663
typedef uint16_t mz_uint16;
1664
typedef uint32_t mz_uint32;
1665
typedef uint32_t mz_uint;
1666
typedef int64_t mz_int64;
1667
typedef uint64_t mz_uint64;
1668
typedef int mz_bool;
1669
1670
#define MZ_FALSE (0)
1671
#define MZ_TRUE (1)
1672
1673
// Works around MSVC's spammy "warning C4127: conditional expression is constant" message.
1674
#ifdef _MSC_VER
1675
#define MZ_MACRO_END while (0, 0)
1676
#else
1677
#define MZ_MACRO_END while (0)
1678
#endif
1679
1680
#define MZ_ASSERT(x)
1681
1682
#define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b))
1683
#define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b))
1684
#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
1685
1686
#ifdef _MSC_VER
1687
#define MZ_FORCEINLINE __forceinline
1688
#elif defined(__GNUC__)
1689
#define MZ_FORCEINLINE inline __attribute__((__always_inline__))
1690
#else
1691
#define MZ_FORCEINLINE inline
1692
#endif
1693
1694
// ------------------- Low-level Compression API Definitions
1695
1696
// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search):
1697
// TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression).
1698
enum
1699
{
1700
    TDEFL_HUFFMAN_ONLY = 0,
1701
    TDEFL_DEFAULT_MAX_PROBES = 128,
1702
    TDEFL_MAX_PROBES_MASK = 0xFFF
1703
};
1704
1705
// TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of the source data at the end. Otherwise, you'll get raw deflate data.
1706
// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers).
1707
// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing.
1708
// TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's initialization time to the minimum, but the output may vary from run to run given the same input (depending on the contents of memory).
1709
// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
1710
// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled.
1711
// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables.
1712
// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks.
1713
// The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK).
1714
enum
1715
{
1716
    TDEFL_WRITE_ZLIB_HEADER = 0x01000,
1717
    TDEFL_COMPUTE_ADLER32 = 0x02000,
1718
    TDEFL_GREEDY_PARSING_FLAG = 0x04000,
1719
    TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
1720
    TDEFL_RLE_MATCHES = 0x10000,
1721
    TDEFL_FILTER_MATCHES = 0x20000,
1722
    TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
1723
    TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
1724
};
1725
1726
enum
1727
{
1728
    TDEFL_MAX_HUFF_TABLES = 3,
1729
    TDEFL_MAX_HUFF_SYMBOLS_0 = 288,
1730
    TDEFL_MAX_HUFF_SYMBOLS_1 = 32,
1731
    TDEFL_MAX_HUFF_SYMBOLS_2 = 19,
1732
    TDEFL_LZ_DICT_SIZE = 32768,
1733
    TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1,
1734
    TDEFL_MIN_MATCH_LEN = 3,
1735
    TDEFL_MAX_MATCH_LEN = 258
1736
};
1737
1738
enum
1739
{
1740
    TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024,
1741
    TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10,
1742
    TDEFL_MAX_HUFF_SYMBOLS = 288,
1743
    TDEFL_LZ_HASH_BITS = 15,
1744
    TDEFL_LEVEL1_HASH_SIZE_MASK = 4095,
1745
    TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3,
1746
    TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
1747
};
1748
1749
// The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The low-level functions don't make any heap allocations, unlike the above helper functions.
1750
typedef enum
1751
{
1752
    TDEFL_STATUS_BAD_PARAM = -2,
1753
    TDEFL_STATUS_PUT_BUF_FAILED = -1,
1754
    TDEFL_STATUS_OKAY = 0,
1755
    TDEFL_STATUS_DONE = 1,
1756
} tdefl_status;
1757
1758
// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums
1759
typedef enum
1760
{
1761
    TDEFL_NO_FLUSH = 0,
1762
    TDEFL_SYNC_FLUSH = 2,
1763
    TDEFL_FULL_FLUSH = 3,
1764
    TDEFL_FINISH = 4
1765
} tdefl_flush;
1766
1767
// tdefl's compression state structure.
1768
typedef struct
1769
{
1770
    mz_uint m_flags, m_max_probes[2];
1771
    int m_greedy_parsing;
1772
    mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
1773
    mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
1774
    mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
1775
    mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, m_wants_to_finish;
1776
    tdefl_status m_prev_return_status;
1777
    const void *m_pIn_buf;
1778
    void *m_pOut_buf;
1779
    size_t *m_pIn_buf_size, *m_pOut_buf_size;
1780
    tdefl_flush m_flush;
1781
    const mz_uint8 *m_pSrc;
1782
    size_t m_src_buf_left, m_out_buf_ofs;
1783
    mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
1784
    mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
1785
    mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
1786
    mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
1787
    mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
1788
    mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
1789
    mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
1790
    mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
1791
} tdefl_compressor;
1792
1793
// Initializes the compressor.
1794
// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory.
1795
// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.)
1796
tdefl_status tdefl_init(tdefl_compressor *d, int flags);
1797
1798
// Compresses a block of data, consuming as much of the specified input buffer as possible, and writing as much compressed data to the specified output buffer as possible.
1799
tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush);
1800
1801
#define MZ_ADLER32_INIT (1)
1802
// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL.
1803
mz_uint32 mz_adler32(mz_uint32 adler, const unsigned char *ptr, size_t buf_len);
1804
1805
// Compression strategies.
1806
enum
1807
{
1808
    MZ_DEFAULT_STRATEGY = 0,
1809
    MZ_FILTERED = 1,
1810
    MZ_HUFFMAN_ONLY = 2,
1811
    MZ_RLE = 3,
1812
    MZ_FIXED = 4
1813
};
1814
1815
// Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and may be very slow), MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL.
1816
enum
1817
{
1818
    MZ_NO_COMPRESSION = 0,
1819
    MZ_BEST_SPEED = 1,
1820
    MZ_BEST_COMPRESSION = 9,
1821
    MZ_UBER_COMPRESSION = 10,
1822
    MZ_DEFAULT_LEVEL = 6,
1823
    MZ_DEFAULT_COMPRESSION = -1
1824
};
1825
1826
#define MZ_DEFAULT_WINDOW_BITS 15
1827
1828
// Create tdefl_compress() flags given zlib-style compression parameters.
1829
// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files)
1830
// window_bits may be -15 (raw deflate) or 15 (zlib)
1831
// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED
1832
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
1833
07070100000008000081A4000001ED0000006B000000015EE6501B000099D8000000000000000000000000000000000000003500000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip.c#include "pkg2zip_aes.h"
1834
#include "pkg2zip_zip.h"
1835
#include "pkg2zip_out.h"
1836
#include "pkg2zip_psp.h"
1837
#include "pkg2zip_utils.h"
1838
#include "pkg2zip_zrif.h"
1839
1840
#include <assert.h>
1841
#include <stdint.h>
1842
#include <stdlib.h>
1843
#include <string.h>
1844
#include <stdio.h>
1845
1846
#ifdef __GNUC__
1847
#pragma GCC diagnostic ignored "-Wpragmas"
1848
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
1849
#pragma GCC diagnostic ignored "-Wformat-truncation"
1850
#endif
1851
1852
#define PKG_HEADER_SIZE 192
1853
#define PKG_HEADER_EXT_SIZE 64
1854
1855
// https://wiki.henkaku.xyz/vita/Packages#AES_Keys
1856
static const uint8_t pkg_ps3_key[] = { 0x2e, 0x7b, 0x71, 0xd7, 0xc9, 0xc9, 0xa1, 0x4e, 0xa3, 0x22, 0x1f, 0x18, 0x88, 0x28, 0xb8, 0xf8 };
1857
static const uint8_t pkg_psp_key[] = { 0x07, 0xf2, 0xc6, 0x82, 0x90, 0xb5, 0x0d, 0x2c, 0x33, 0x81, 0x8d, 0x70, 0x9b, 0x60, 0xe6, 0x2b };
1858
static const uint8_t pkg_vita_2[] = { 0xe3, 0x1a, 0x70, 0xc9, 0xce, 0x1d, 0xd7, 0x2b, 0xf3, 0xc0, 0x62, 0x29, 0x63, 0xf2, 0xec, 0xcb };
1859
static const uint8_t pkg_vita_3[] = { 0x42, 0x3a, 0xca, 0x3a, 0x2b, 0xd5, 0x64, 0x9f, 0x96, 0x86, 0xab, 0xad, 0x6f, 0xd8, 0x80, 0x1f };
1860
static const uint8_t pkg_vita_4[] = { 0xaf, 0x07, 0xfd, 0x59, 0x65, 0x25, 0x27, 0xba, 0xf1, 0x33, 0x89, 0x66, 0x8b, 0x17, 0xd9, 0xea };
1861
1862
// http://vitadevwiki.com/vita/System_File_Object_(SFO)_(PSF)#Internal_Structure
1863
// https://github.com/TheOfficialFloW/VitaShell/blob/1.74/sfo.h#L29
1864
static void parse_sfo_content(const uint8_t* sfo, uint32_t sfo_size, char* category, char* title, char* content, char* min_version, char* pkg_version)
1865
{
1866
    if (get32le(sfo) != 0x46535000)
1867
    {
1868
        sys_error("ERROR: incorrect sfo signature\n");
1869
    }
1870
1871
    uint32_t keys = get32le(sfo + 8);
1872
    uint32_t values = get32le(sfo + 12);
1873
    uint32_t count = get32le(sfo + 16);
1874
1875
    int title_index = -1;
1876
    int content_index = -1;
1877
    int category_index = -1;
1878
    int minver_index = -1;
1879
    int pkgver_index = -1;
1880
    for (uint32_t i = 0; i < count; i++)
1881
    {
1882
        if (i * 16 + 20 + 2 > sfo_size)
1883
        {
1884
            sys_error("ERROR: sfo information is too small\n");
1885
        }
1886
1887
        char* key = (char*)sfo + keys + get16le(sfo + i * 16 + 20);
1888
        if (strcmp(key, "TITLE") == 0)
1889
        {
1890
            if (title_index < 0)
1891
            {
1892
                title_index = (int)i;
1893
            }
1894
        }
1895
        else if (strcmp(key, "STITLE") == 0)
1896
        {
1897
            title_index = (int)i;
1898
        }
1899
        else if (strcmp(key, "CONTENT_ID") == 0)
1900
        {
1901
            content_index = (int)i;
1902
        }
1903
        else if (strcmp(key, "CATEGORY") == 0)
1904
        {
1905
            category_index = (int)i;
1906
        }
1907
        else if (strcmp(key, "PSP2_DISP_VER") == 0)
1908
        {
1909
            minver_index = (int)i;
1910
        }
1911
        else if (strcmp(key, "APP_VER") == 0)
1912
        {
1913
            pkgver_index = (int)i;
1914
        }
1915
    }
1916
1917
    if (title_index < 0)
1918
    {
1919
        sys_error("ERROR: cannot find title from sfo file, pkg is probably corrupted\n");
1920
    }
1921
1922
    char* value = (char*)sfo + values + get32le(sfo + title_index * 16 + 20 + 12);
1923
    size_t i;
1924
    size_t max = 255;
1925
    for (i = 0; i<max && *value; i++, value++)
1926
    {
1927
        if ((*value >= 32 && *value < 127 && strchr("<>\"/\\|?*", *value) == NULL) || (uint8_t)*value >= 128)
1928
        {
1929
            if (*value == ':')
1930
            {
1931
                *title++ = ' ';
1932
                *title++ = '-';
1933
                max--;
1934
            }
1935
            else
1936
            {
1937
                *title++ = *value;
1938
            }
1939
        }
1940
        else if (*value == 10)
1941
        {
1942
            *title++ = ' ';
1943
        }
1944
    }
1945
    *title = 0;
1946
1947
    if (content_index >= 0 && content)
1948
    {
1949
        value = (char*)sfo + values + get32le(sfo + content_index * 16 + 20 + 12);
1950
        while (*value)
1951
        {
1952
            *content++ = *value++;
1953
        }
1954
        *content = 0;
1955
    }
1956
1957
    if (category_index >= 0)
1958
    {
1959
        value = (char*)sfo + values + get32le(sfo + category_index * 16 + 20 + 12);
1960
        while (*value)
1961
        {
1962
            *category++ = *value++;
1963
        }
1964
    }
1965
    *category = 0;
1966
1967
    if (minver_index >= 0 && min_version)
1968
    {
1969
        value = (char*)sfo + values + get32le(sfo + minver_index * 16 + 20 + 12);
1970
        if (*value == '0')
1971
        {
1972
            value++;
1973
        }
1974
        while (*value)
1975
        {
1976
            *min_version++ = *value++;
1977
        }
1978
        if (min_version[-1] == '0')
1979
        {
1980
            min_version[-1] = 0;
1981
        }
1982
        else
1983
        {
1984
            *min_version = 0;
1985
        }
1986
    }
1987
1988
    if (pkgver_index >= 0 && pkg_version)
1989
    {
1990
        value = (char*)sfo + values + get32le(sfo + pkgver_index * 16 + 20 + 12);
1991
        if (*value == '0')
1992
        {
1993
            value++;
1994
        }
1995
        while (*value)
1996
        {
1997
            *pkg_version++ = *value++;
1998
        }
1999
        *pkg_version = 0;
2000
    }
2001
}
2002
2003
static void parse_sfo(sys_file f, uint64_t sfo_offset, uint32_t sfo_size, char* category, char* title, char* content, char* min_version, char* pkg_version)
2004
{
2005
    uint8_t sfo[16 * 1024];
2006
    if (sfo_size < 16)
2007
    {
2008
        sys_error("ERROR: sfo information is too small\n");
2009
    }
2010
    if (sfo_size > sizeof(sfo))
2011
    {
2012
        sys_error("ERROR: sfo information is too big, pkg file is probably corrupted\n");
2013
    }
2014
    sys_read(f, sfo_offset, sfo, sfo_size);
2015
2016
    parse_sfo_content(sfo, sfo_size, category, title, content, min_version, pkg_version);
2017
}
2018
2019
static void find_psp_sfo(const aes128_key* key, const aes128_key* ps3_key, const uint8_t* iv, sys_file pkg, uint64_t pkg_size, uint64_t enc_offset, uint64_t items_offset, uint32_t item_count, char* category, char* title)
2020
{
2021
    for (uint32_t item_index = 0; item_index < item_count; item_index++)
2022
    {
2023
        uint8_t item[32];
2024
        uint64_t item_offset = items_offset + item_index * 32;
2025
        sys_read(pkg, enc_offset + item_offset, item, sizeof(item));
2026
        aes128_ctr_xor(key, iv, item_offset / 16, item, sizeof(item));
2027
2028
        uint32_t name_offset = get32be(item + 0);
2029
        uint32_t name_size = get32be(item + 4);
2030
        uint64_t data_offset = get64be(item + 8);
2031
        uint64_t data_size = get64be(item + 16);
2032
        uint8_t psp_type = item[24];
2033
2034
        assert(name_offset % 16 == 0);
2035
        assert(data_offset % 16 == 0);
2036
2037
        if (pkg_size < enc_offset + name_offset + name_size ||
2038
            pkg_size < enc_offset + data_offset + data_size)
2039
        {
2040
            sys_error("ERROR: pkg file is too short, possibly corrupted\n");
2041
        }
2042
2043
        const aes128_key* item_key = psp_type == 0x90 ? key : ps3_key;
2044
2045
        char name[ZIP_MAX_FILENAME];
2046
        sys_read(pkg, enc_offset + name_offset, name, name_size);
2047
        aes128_ctr_xor(item_key, iv, name_offset / 16, (uint8_t*)name, name_size);
2048
        name[name_size] = 0;
2049
2050
        if (strcmp(name, "PARAM.SFO") == 0)
2051
        {
2052
            uint8_t sfo[16 * 1024];
2053
            if (data_size < 16)
2054
            {
2055
                sys_error("ERROR: sfo information is too small\n");
2056
            }
2057
            if (data_size > sizeof(sfo))
2058
            {
2059
                sys_error("ERROR: sfo information is too big, pkg file is probably corrupted\n");
2060
            }
2061
2062
            sys_read(pkg, enc_offset + data_offset, sfo, (uint32_t)data_size);
2063
            aes128_ctr_xor(item_key, iv, data_offset / 16, sfo, (uint32_t)data_size);
2064
2065
            parse_sfo_content(sfo, (uint32_t)data_size, category, title, NULL, NULL, NULL);
2066
            return;
2067
        }
2068
    }
2069
}
2070
2071
static const char* get_region(const char* id)
2072
{
2073
    if (memcmp(id, "PCSE", 4) == 0 || memcmp(id, "PCSA", 4) == 0 ||
2074
        memcmp(id, "NPNA", 4) == 0)
2075
    {
2076
        return "USA";
2077
    }
2078
    else if (memcmp(id, "PCSF", 4) == 0 || memcmp(id, "PCSB", 4) == 0 ||
2079
             memcmp(id, "NPOA", 4) == 0)
2080
    {
2081
        return "EUR";
2082
    }
2083
    else if (memcmp(id, "PCSC", 4) == 0 || memcmp(id, "VCJS", 4) == 0 || 
2084
             memcmp(id, "PCSG", 4) == 0 || memcmp(id, "VLJS", 4) == 0 ||
2085
             memcmp(id, "VLJM", 4) == 0 || memcmp(id, "NPPA", 4) == 0)
2086
    {
2087
        return "JPN";
2088
    }
2089
    else if (memcmp(id, "VCAS", 4) == 0 || memcmp(id, "PCSH", 4) == 0 ||
2090
             memcmp(id, "VLAS", 4) == 0 || memcmp(id, "PCSD", 4) == 0 ||
2091
             memcmp(id, "NPQA", 4) == 0)
2092
    {
2093
        return "ASA";
2094
    }
2095
    else
2096
    {
2097
        return "unknown region";
2098
    }
2099
}
2100
2101
void print_help(char* bin_name)
2102
{
2103
    sys_output("Parameters:\n");
2104
    sys_output("\n");
2105
    sys_output("-x|--extract       Extract only. No zip compression\n");
2106
    sys_output("-l|--list          Shows the package (sfo) name and exits\n");
2107
    sys_output("-b|--no-bgdl       Disable bgdl output for VITA Theme extraction\n");
2108
    sys_output("-q|--quiet         Do not output anything to stdout\n");
2109
    sys_output("-h|--help          Shows this help message\n");
2110
    sys_output("\n");
2111
    sys_output("PSP/PSX only options:\n");
2112
    sys_output("-c[NUM]            Create a *.CSO file instead of ISO. [NUM] is the compression ratio\n");
2113
    sys_output("-p|--psp           Extracts PSP files in their original EBOOT.PBP format\n");
2114
    sys_output("\n");
2115
    sys_output("Usage: %s [-x] [-c[N]] [-b] [-p] <file.pkg> [zRIF]\n", bin_name);
2116
}
2117
2118
typedef enum {
2119
    PKG_TYPE_VITA_APP,
2120
    PKG_TYPE_VITA_DLC,
2121
    PKG_TYPE_VITA_PATCH,
2122
    PKG_TYPE_VITA_PSM,
2123
    PKG_TYPE_VITA_THEME,
2124
    PKG_TYPE_PSP,
2125
    PKG_TYPE_PSP_THEME,
2126
    PKG_TYPE_PSX,
2127
} pkg_type;
2128
2129
int main(int argc, char* argv[])
2130
{
2131
    sys_output_init();
2132
2133
    int zipped = 1;
2134
    int listing = 0;
2135
    int verbose = 1;
2136
    int cso = 0;
2137
    int pbp = 0;
2138
    int bgdl = 1;
2139
    const char* pkg_arg = NULL;
2140
    const char* zrif_arg = NULL;
2141
    for (int i = 1; i < argc; i++)
2142
    {
2143
        if (strcmp(argv[i], "-x") == 0 || strcmp(argv[i], "--extract") == 0)
2144
        {
2145
            zipped = 0;
2146
        }
2147
        else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list") == 0)
2148
        {
2149
            listing = 1;
2150
            verbose = 0;
2151
        }
2152
        else if (strncmp(argv[i], "-c", 2) == 0)
2153
        {
2154
            if (argv[i][2] != 0)
2155
            {
2156
                cso = atoi(argv[i] + 2);
2157
                cso = cso > 9 ? 9 : cso < 0 ? 0 : cso;
2158
            }
2159
        }
2160
        else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--psp") == 0)
2161
        {
2162
            pbp = 1;
2163
        }
2164
        else if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--no-bgdl") == 0)
2165
        {
2166
            bgdl = 0;
2167
        }
2168
        else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0)
2169
        {
2170
            verbose = 0;
2171
        }
2172
        else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
2173
        {
2174
            sys_output("pkg2zip v2.2\n");
2175
            sys_output("\n");
2176
            print_help(argv[0]);
2177
            exit(0);
2178
        }
2179
        else
2180
        {
2181
            if (pkg_arg != NULL )
2182
            {
2183
                if(strlen(argv[i]) != 0)
2184
                {
2185
                    zrif_arg = argv[i];
2186
                }
2187
                break;
2188
            }
2189
            else
2190
            {
2191
                pkg_arg = argv[i];
2192
            }
2193
        }
2194
    }
2195
    if (pkg_arg == NULL)
2196
    {
2197
        fprintf(stderr, "ERROR: no pkg file specified\n");
2198
        print_help(argv[0]);
2199
        exit(1);
2200
    }
2201
2202
    if (verbose)
2203
    {
2204
        sys_output("pkg2zip v2.2\n");
2205
    }
2206
2207
    if (verbose)
2208
    {
2209
        sys_output("[*] loading...\n");
2210
    }
2211
2212
    uint64_t pkg_size;
2213
    sys_file pkg = sys_open(pkg_arg, &pkg_size);
2214
2215
    uint8_t pkg_header[PKG_HEADER_SIZE + PKG_HEADER_EXT_SIZE];
2216
    sys_read(pkg, 0, pkg_header, sizeof(pkg_header));
2217
2218
    if (get32be(pkg_header) != 0x7f504b47 || get32be(pkg_header + PKG_HEADER_SIZE) != 0x7F657874)
2219
    {
2220
        sys_error("ERROR: not a pkg file\n");
2221
    }
2222
2223
    // http://www.psdevwiki.com/ps3/PKG_files
2224
    uint64_t meta_offset = get32be(pkg_header + 8);
2225
    uint32_t meta_count = get32be(pkg_header + 12);
2226
    uint32_t item_count = get32be(pkg_header + 20);
2227
    uint64_t total_size = get64be(pkg_header + 24);
2228
    uint64_t enc_offset = get64be(pkg_header + 32);
2229
    uint64_t enc_size = get64be(pkg_header + 40);
2230
    const uint8_t* iv = pkg_header + 0x70;
2231
    int key_type = pkg_header[0xe7] & 7;
2232
2233
    if (pkg_size < total_size)
2234
    {
2235
        sys_error("ERROR: pkg file is too small\n");
2236
    }
2237
    if (pkg_size < enc_offset + item_count * 32)
2238
    {
2239
        sys_error("ERROR: pkg file is too small\n");
2240
    }
2241
    
2242
    uint32_t content_type = 0;
2243
    uint32_t sfo_offset = 0;
2244
    uint32_t sfo_size = 0;
2245
    uint32_t items_offset = 0;
2246
    uint32_t items_size = 0;
2247
    char install_directory[0x28] = {0};
2248
2249
    for (uint32_t i = 0; i < meta_count; i++)
2250
    {
2251
        uint8_t block[16];
2252
        sys_read(pkg, meta_offset, block, sizeof(block));
2253
2254
        uint32_t type = get32be(block + 0);
2255
        uint32_t size = get32be(block + 4);
2256
2257
        if (type == 2)
2258
        {
2259
            content_type = get32be(block + 8);
2260
        }
2261
        else if (type == 13)
2262
        {
2263
            items_offset = get32be(block + 8);
2264
            items_size = get32be(block + 12);
2265
        }
2266
        else if (type == 14)
2267
        {
2268
            sfo_offset = get32be(block + 8);
2269
            sfo_size = get32be(block + 12);
2270
        }
2271
        else if (type == 10)
2272
        {
2273
            sys_read(pkg, meta_offset + 8 + 8, install_directory,sizeof(install_directory));
2274
            //sys_output("[*] DLC Install Directory: %s\n", install_directory);
2275
        }
2276
2277
        meta_offset += 2 * sizeof(uint32_t) + size;
2278
    }
2279
2280
    pkg_type type;
2281
2282
    // http://www.psdevwiki.com/ps3/PKG_files
2283
    if (content_type == 6)
2284
    {
2285
        type = PKG_TYPE_PSX;
2286
    }
2287
    else if (content_type == 7 || content_type == 0xe || content_type == 0xf || content_type == 0x10)
2288
    {
2289
        // PSP & PSP-PCEngine & DLC / PSP-Go / PSP-Mini / PSP-NeoGeo 
2290
        type = PKG_TYPE_PSP;
2291
    }
2292
    else if (content_type == 0x9)
2293
    {
2294
        type = PKG_TYPE_PSP_THEME;
2295
    }
2296
    else if (content_type == 0x15)
2297
    {
2298
        type = PKG_TYPE_VITA_APP;
2299
    }
2300
    else if (content_type == 0x16)
2301
    {
2302
        type = PKG_TYPE_VITA_DLC;
2303
    }
2304
    else if (content_type == 0x18 || content_type == 0x1d)
2305
    {
2306
        type = PKG_TYPE_VITA_PSM;
2307
    }
2308
    else if (content_type ==  0x1f)
2309
    {
2310
        type = PKG_TYPE_VITA_THEME;
2311
    }
2312
    else
2313
    {
2314
        sys_error("ERROR: unsupported content type 0x%x", content_type);
2315
    }
2316
2317
    aes128_key ps3_key;
2318
    uint8_t main_key[16];
2319
    if (key_type == 1)
2320
    {
2321
        memcpy(main_key, pkg_psp_key, sizeof(main_key));
2322
        aes128_init(&ps3_key, pkg_ps3_key);
2323
    }
2324
    else if (key_type == 2)
2325
    {
2326
        aes128_key key;
2327
        aes128_init(&key, pkg_vita_2);
2328
        aes128_ecb_encrypt(&key, iv, main_key);
2329
    }
2330
    else if (key_type == 3)
2331
    {
2332
        aes128_key key;
2333
        aes128_init(&key, pkg_vita_3);
2334
        aes128_ecb_encrypt(&key, iv, main_key);
2335
    }
2336
    else if (key_type == 4)
2337
    {
2338
        aes128_key key;
2339
        aes128_init(&key, pkg_vita_4);
2340
        aes128_ecb_encrypt(&key, iv, main_key);
2341
    }
2342
2343
    aes128_key key;
2344
    aes128_init(&key, main_key);
2345
2346
    char content[256];
2347
    char title[256];
2348
    char category[256];
2349
    char min_version[256];
2350
    char pkg_version[256];
2351
    const char* id = content + 7;
2352
    const char* id2 = id + 13;
2353
2354
    // first 512 - for vita games - https://github.com/TheOfficialFloW/NoNpDrm/blob/v1.1/src/main.c#L42
2355
    // 1024 is used for PSM
2356
    uint8_t rif[1024];
2357
    uint32_t rif_size = 0;
2358
2359
    if (type == PKG_TYPE_PSP || type == PKG_TYPE_PSX)
2360
    {
2361
        find_psp_sfo(&key, &ps3_key, iv, pkg, pkg_size, enc_offset, items_offset, item_count, category, title);
2362
        id = (char*)pkg_header + 0x37;
2363
2364
        if (type == PKG_TYPE_PSX && zrif_arg != NULL)  //pocketstation pkg type is PSX
2365
        {
2366
            rif_size = 512;
2367
            zrif_decode(zrif_arg, rif, rif_size);
2368
        }
2369
    }
2370
    else if (type == PKG_TYPE_PSP_THEME)
2371
    {
2372
2373
        id = (char*)pkg_header + 0x37;
2374
        memcpy(title, pkg_header + 0x44, 0x10);
2375
2376
        uint8_t item[32];
2377
        sys_read(pkg, enc_offset + items_offset, item, sizeof(item));
2378
        aes128_ctr_xor(&key, iv, items_offset / 16, item, sizeof(item));
2379
2380
        uint64_t data_offset = get64be(item + 8);
2381
        uint64_t data_size = get64be(item + 16);
2382
        uint8_t psp_type = item[24];
2383
2384
        assert(data_offset % 16 == 0);
2385
2386
        if (pkg_size < enc_offset + data_offset + data_size)
2387
        {
2388
            sys_error("ERROR: pkg file is too short, possibly corrupted\n");
2389
        }
2390
2391
        const aes128_key* item_key;
2392
        item_key = psp_type == 0x90 ? &key : &ps3_key;
2393
        get_psp_theme_title(title,item_key, iv, pkg, enc_offset, data_offset);
2394
2395
        //Theme names are prone to having colons
2396
        for (uint32_t i = 0; i < sizeof(title); i++)
2397
        {
2398
            if (title[i] == 58)
2399
            {
2400
                title[i] = 32;
2401
            }
2402
        }
2403
    }
2404
    else // Vita
2405
    {
2406
        if (type == PKG_TYPE_VITA_PSM)
2407
        {
2408
            memcpy(content, pkg_header + 0x30, 0x30);
2409
            rif_size = 1024;
2410
        }
2411
        else if (type == PKG_TYPE_VITA_THEME)
2412
        {
2413
            parse_sfo(pkg, sfo_offset, sfo_size, category, title, content, min_version, pkg_version);
2414
            rif_size = 512;
2415
        }
2416
        else // Vita APP, DLC or PATCH
2417
        {
2418
            parse_sfo(pkg, sfo_offset, sfo_size, category, title, content, min_version, pkg_version);
2419
            rif_size = 512;
2420
            
2421
            if (type == PKG_TYPE_VITA_APP && strcmp(category, "gp") == 0)
2422
            {
2423
                type = PKG_TYPE_VITA_PATCH;
2424
            }
2425
        }
2426
2427
        if (type != PKG_TYPE_VITA_PATCH && zrif_arg != NULL)
2428
        {
2429
            zrif_decode(zrif_arg, rif, rif_size);
2430
            const char* rif_contentid = (char*)rif + (type == PKG_TYPE_VITA_PSM ? 0x50 : 0x10);
2431
            if (strncmp(rif_contentid, content, 0x30) != 0)
2432
            {
2433
                sys_error("ERROR: zRIF content id '%s' doesn't match pkg '%s'\n", rif_contentid, content);
2434
            }
2435
        }
2436
    }
2437
2438
    const char* ext = zipped ? ".zip" : "";
2439
2440
    char root[1024];
2441
    if (type == PKG_TYPE_PSP)
2442
    {
2443
        const char* type_str;
2444
        if (content_type == 7)
2445
        {
2446
            type_str = (strcmp(category, "HG") == 0) ? "PSP-PCEngine" : install_directory[0] != 0 ? "PSP-DLC" :"PSP";
2447
        }
2448
        else
2449
        {
2450
            type_str = content_type == 0xe ? "PSP-Go" : content_type == 0xf ? "PSP-Mini" : "PSP-NeoGeo";
2451
        }
2452
        snprintf(root, sizeof(root), "%s [%.9s] [%s]%s", title, id, type_str, ext);
2453
        if (verbose)
2454
        {
2455
            sys_output("[*] unpacking %s\n", type_str);
2456
        }
2457
    }
2458
    else if (type == PKG_TYPE_PSP_THEME)
2459
    {
2460
        snprintf(root, sizeof(root), "%s [%.9s] [PSP-Theme]%s", title, id, ext);
2461
        if (verbose)
2462
        {
2463
            sys_output("[*] unpacking PSP Theme\n");
2464
        }
2465
    }
2466
    else if (type == PKG_TYPE_PSX)
2467
    {
2468
        snprintf(root, sizeof(root), "%s [%.9s] [PSX]%s", title, id, ext);
2469
        if (verbose)
2470
        {
2471
            sys_output("[*] unpacking PSX\n");
2472
        }
2473
    }
2474
    else if (type == PKG_TYPE_VITA_DLC)
2475
    {
2476
        snprintf(root, sizeof(root), "%s [%.9s] [%s] [DLC-%s]%s", title, id, get_region(id), id2, ext);
2477
        if (verbose)
2478
        {
2479
            sys_output("[*] unpacking Vita DLC\n");
2480
        }
2481
    }
2482
    else if (type == PKG_TYPE_VITA_PATCH)
2483
    {
2484
        snprintf(root, sizeof(root), "%s [%.9s] [%s] [PATCH] [v%s]%s", title, id, get_region(id), pkg_version, ext);
2485
        if (verbose)
2486
        {
2487
            sys_output("[*] unpacking Vita PATCH\n");
2488
        }
2489
    }
2490
    else if (type == PKG_TYPE_VITA_PSM)
2491
    {
2492
        snprintf(root, sizeof(root), "%.9s [%s] [PSM]%s", id, get_region(id), ext);
2493
        if (verbose)
2494
        {
2495
            sys_output("[*] unpacking Vita PSM\n");
2496
        }
2497
    }
2498
    else if (type == PKG_TYPE_VITA_APP)
2499
    {
2500
        snprintf(root, sizeof(root), "%s [%.9s] [%s]%s", title, id, get_region(id), ext);
2501
        if (verbose)
2502
        {
2503
            sys_output("[*] unpacking Vita APP\n");
2504
        }
2505
    }
2506
    else if (type == PKG_TYPE_VITA_THEME)
2507
    {
2508
        snprintf(root, sizeof(root), "%s [%.9s] [%s]%s", title, id, get_region(id), ext);
2509
        if (verbose)
2510
        {
2511
            sys_output("[*] unpacking Vita theme\n");
2512
        }
2513
    }
2514
    else
2515
    {
2516
        assert(0);
2517
        sys_error("ERROR: unsupported type\n");
2518
    }
2519
2520
    if (listing && zipped)
2521
    {
2522
        sys_output("%s\n", root);
2523
        exit(0);
2524
    }
2525
    else if (listing && zipped == 0)
2526
    {
2527
        sys_error("ERROR: Listing option without creating zip is useless\n");
2528
    }
2529
2530
2531
    if (verbose)
2532
    {
2533
        sys_output("[*] creating '%s' archive\n", root);
2534
    }
2535
2536
    out_begin(root, zipped);
2537
    root[0] = 0;
2538
2539
    if (type == PKG_TYPE_PSP)
2540
    {
2541
        snprintf(root, sizeof(root), "pspemu/PSP/GAME/%.9s", id);
2542
    }
2543
    else if (type == PKG_TYPE_PSP_THEME)
2544
    {
2545
        snprintf(root, sizeof(root), "pspemu/PSP/THEME");
2546
    }
2547
    else if (type == PKG_TYPE_PSX)
2548
    {
2549
        snprintf(root, sizeof(root), "pspemu/PSP/GAME/%.9s", id);
2550
    }
2551
    else if (type == PKG_TYPE_VITA_DLC)
2552
    {
2553
        sys_vstrncat(root, sizeof(root), "addcont");
2554
        out_add_folder(root);
2555
2556
        sys_vstrncat(root, sizeof(root), "/%.9s", id);
2557
        out_add_folder(root);
2558
2559
        sys_vstrncat(root, sizeof(root), "/%s", id2);
2560
        out_add_folder(root);
2561
    }
2562
    else if (type == PKG_TYPE_VITA_PATCH)
2563
    {
2564
        sys_vstrncat(root, sizeof(root), "patch");
2565
        out_add_folder(root);
2566
2567
        sys_vstrncat(root, sizeof(root), "/%.9s", id);
2568
        out_add_folder(root);
2569
    }
2570
    else if (type == PKG_TYPE_VITA_PSM)
2571
    {
2572
        sys_vstrncat(root, sizeof(root), "psm");
2573
        out_add_folder(root);
2574
2575
        sys_vstrncat(root, sizeof(root), "/%.9s", id);
2576
        out_add_folder(root);
2577
    }
2578
    else if (type == PKG_TYPE_VITA_APP)
2579
    {
2580
        sys_vstrncat(root, sizeof(root), "app");
2581
        out_add_folder(root);
2582
2583
        sys_vstrncat(root, sizeof(root), "/%.9s", id);
2584
        out_add_folder(root);
2585
    }
2586
    else if (type == PKG_TYPE_VITA_THEME)
2587
    {
2588
2589
        if (bgdl == 1)
2590
        {
2591
            sys_vstrncat(root, sizeof(root), "bgdl/t");
2592
            out_add_folder(root);
2593
2594
            uint32_t bgdl_task = 0;
2595
            char dir[1024] = {0};
2596
            if(zipped == 0)
2597
            {
2598
                do 
2599
                {
2600
                    bgdl_task++;
2601
                    snprintf(dir, sizeof(dir), "%s/%08x",root, bgdl_task);
2602
                } 
2603
                while (sys_test_dir(dir));
2604
            }
2605
            else
2606
            {
2607
                bgdl_task = 1;
2608
            }
2609
2610
            sys_vstrncat(root,sizeof(root), "/%08x", bgdl_task);
2611
            out_add_folder(root);
2612
        }
2613
        else 
2614
        {
2615
            sys_vstrncat(root, sizeof(root), "app");
2616
            out_add_folder(root);
2617
        }
2618
2619
        sys_vstrncat(root, sizeof(root), "/%.9s", id);
2620
        out_add_folder(root);
2621
    }
2622
    else
2623
    {
2624
        assert(0);
2625
        sys_error("ERROR: unsupported type\n");
2626
    }
2627
2628
    char path[1024];
2629
2630
    sys_output_progress_init(pkg_size);
2631
2632
    for (uint32_t item_index = 0; item_index < item_count; item_index++)
2633
    {
2634
        uint8_t item[32];
2635
        uint64_t item_offset = items_offset + item_index * 32;
2636
        sys_read(pkg, enc_offset + item_offset, item, sizeof(item));
2637
        aes128_ctr_xor(&key, iv, item_offset / 16, item, sizeof(item));
2638
2639
        uint32_t name_offset = get32be(item + 0);
2640
        uint32_t name_size = get32be(item + 4);
2641
        uint64_t data_offset = get64be(item + 8);
2642
        uint64_t data_size = get64be(item + 16);
2643
        uint8_t psp_type = item[24];
2644
        uint8_t flags = item[27];
2645
2646
        assert(name_offset % 16 == 0);
2647
        assert(data_offset % 16 == 0);
2648
2649
        if (pkg_size < enc_offset + name_offset + name_size ||
2650
            pkg_size < enc_offset + data_offset + data_size)
2651
        {
2652
            sys_error("ERROR: pkg file is too short, possibly corrupted\n");
2653
        }
2654
2655
        if (name_size >= ZIP_MAX_FILENAME)
2656
        {
2657
            sys_error("ERROR: pkg file contains file with very long name\n");
2658
        }
2659
2660
        const aes128_key* item_key;
2661
        if (type == PKG_TYPE_PSP || type == PKG_TYPE_PSX || type == PKG_TYPE_PSP_THEME)
2662
        {
2663
            item_key = psp_type == 0x90 ? &key : &ps3_key;
2664
        }
2665
        else
2666
        {
2667
            item_key = &key;
2668
        }
2669
2670
        char name[ZIP_MAX_FILENAME];
2671
        sys_read(pkg, enc_offset + name_offset, name, name_size);
2672
        aes128_ctr_xor(item_key, iv, name_offset / 16, (uint8_t*)name, name_size);
2673
        name[name_size] = 0;
2674
2675
        // sys_output("[%u/%u] %s\n", item_index + 1, item_count, name);
2676
2677
        if (flags == 4 || flags == 18) // Directory
2678
        {
2679
            if (type == PKG_TYPE_VITA_PSM)
2680
            {
2681
                // skip "content/" prefix
2682
                char* slash = strchr(name, '/');
2683
                if (slash != NULL)
2684
                {
2685
                    if (strstr(name, "runtime"))
2686
                    {
2687
                        snprintf(path, sizeof(path), "%s/%s", root, name + 9);
2688
                    }
2689
                    else
2690
                    {
2691
                        snprintf(path, sizeof(path), "%s/RO/%s", root, name + 9);
2692
                    }
2693
                    out_add_folder(path);
2694
                }
2695
            }
2696
            else if (type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PATCH || type == PKG_TYPE_VITA_THEME)
2697
            {
2698
                snprintf(path, sizeof(path), "%s/%s", root, name);
2699
                out_add_folder(path);
2700
            }
2701
            // sys_output("dir : %s\n", path);
2702
        }
2703
        else // File
2704
        {
2705
            int decrypt = 1;
2706
            if ((type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PATCH || type == PKG_TYPE_VITA_THEME) && (strcmp("sce_sys/package/digs.bin", name) == 0 || strcmp("sce_sys/package/cert.bin", name) == 0 ))
2707
            {
2708
                snprintf(path, sizeof(path), "%s/sce_sys/package", root);
2709
                out_add_folder(path);
2710
                if (verbose)
2711
                {
2712
                    sys_output("[*] renaming %s to body.bin\n", name);
2713
                }
2714
                snprintf(name, sizeof(name), "%s", "sce_sys/package/body.bin");
2715
                decrypt = 0;
2716
            }
2717
2718
            if (type == PKG_TYPE_PSX)
2719
            {
2720
                if (strcmp("USRDIR/CONTENT/DOCUMENT.DAT", name) == 0)
2721
                {
2722
                    snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/DOCUMENT.DAT", id);
2723
                }
2724
                else if (strcmp("USRDIR/CONTENT/EBOOT.PBP", name) == 0)
2725
                {
2726
                    snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/KEYS.BIN", id);
2727
                    unpack_keys_bin(path, item_key, iv, pkg, enc_offset, data_offset, data_size);
2728
                    snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/EBOOT.PBP", id);
2729
                }
2730
                else if (strcmp("USRDIR/CONTENT/texture.enc", name) == 0)
2731
                {
2732
                    snprintf(path, sizeof(path), "ps1emu/%.9s/texture.enc", id);
2733
                }
2734
                else
2735
                {
2736
                    continue;
2737
                }
2738
            }
2739
            else if (type == PKG_TYPE_PSP)
2740
            {
2741
                if (strcmp("USRDIR/CONTENT/EBOOT.PBP", name) == 0)
2742
                {
2743
                    snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/EBOOT.PBP", id);
2744
                    if (!pbp)
2745
                    {
2746
                        snprintf(path, sizeof(path), "pspemu/ISO/%s [%.9s].%s", title, id, cso ? "cso" : "iso");
2747
                        out_add_parent(path);
2748
                        unpack_psp_eboot(path, item_key, iv, pkg, enc_offset, data_offset, data_size, cso);
2749
                        continue;
2750
                    }
2751
                }
2752
                else if (strcmp("USRDIR/CONTENT/DOCUMENT.DAT", name) == 0)
2753
                {
2754
                    snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/DOCUMENT.DAT", id);
2755
                    if (!pbp)
2756
                    {
2757
                        continue;
2758
                    }
2759
                }
2760
                else if (strcmp("USRDIR/CONTENT/DOCINFO.EDAT", name) == 0)
2761
                {
2762
                    snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/DOCINFO.EDAT", id);
2763
                    if (!pbp)
2764
                    {
2765
                        continue;
2766
                    }
2767
                }
2768
                else if (strcmp("USRDIR/CONTENT/PSP-KEY.EDAT", name) == 0)
2769
                {
2770
                    snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/PSP-KEY.EDAT", id);
2771
                    out_add_parent(path);
2772
                    unpack_psp_key(path, item_key, iv, pkg, enc_offset, data_offset, data_size);
2773
                    continue;
2774
                }
2775
                else if (strstr(name, "USRDIR/CONTENT"))
2776
                {
2777
                    // skip "USRDIR/CONTENT" prefix
2778
                    char* slash = strchr(name+14, '/');
2779
                    if (slash != NULL)
2780
                    {
2781
                        snprintf(path, sizeof(path), "pspemu/PSP/GAME/%.9s/%s", id, name+15);
2782
2783
                        char* edat = strrchr(name, '.');
2784
                        if (edat != NULL)
2785
                        {
2786
                            if (strcmp(edat, ".edat") == 0 || strcmp(edat, ".EDAT") == 0)
2787
                            {
2788
                                out_add_parent(path);
2789
                                unpack_psp_edat(path, item_key, iv, pkg, enc_offset, data_offset, data_size);
2790
                                continue;
2791
                            }
2792
                        }
2793
                    }
2794
                    else
2795
                    {
2796
                        continue;
2797
                    }
2798
                }
2799
                else
2800
                {
2801
                    continue;
2802
                }
2803
            }
2804
            else if (type == PKG_TYPE_PSP_THEME)
2805
            {
2806
                snprintf(path, sizeof(path), "pspemu/PSP/THEME/%s", name);
2807
                out_add_parent(path);
2808
                unpack_psp_edat(path, item_key, iv, pkg, enc_offset, data_offset, data_size);
2809
                continue;
2810
            }
2811
            else if (type == PKG_TYPE_VITA_PSM)
2812
            {
2813
                // skip "content/" prefix
2814
                if (strstr(name, "runtime"))
2815
                {
2816
                   snprintf(path, sizeof(path), "%s/%s", root, name + 9);
2817
                }
2818
                else
2819
                {
2820
                   snprintf(path, sizeof(path), "%s/RO/%s", root, name + 9);
2821
                }
2822
            }
2823
            else
2824
            {
2825
                snprintf(path, sizeof(path), "%s/%s", root, name);
2826
            }
2827
2828
            uint64_t offset = data_offset;
2829
            out_add_parent(path);
2830
            out_begin_file(path, 0);
2831
            while (data_size != 0)
2832
            {
2833
                uint8_t PKG_ALIGN(16) buffer[1 << 16];
2834
                uint32_t size = (uint32_t)min64(data_size, sizeof(buffer));
2835
                sys_output_progress(enc_offset + offset);
2836
                sys_read(pkg, enc_offset + offset, buffer, size);
2837
2838
                if (decrypt)
2839
                {
2840
                    aes128_ctr_xor(item_key, iv, offset / 16, buffer, size);
2841
                }
2842
2843
                out_write(buffer, size);
2844
                offset += size;
2845
                data_size -= size;
2846
            }
2847
            out_end_file();
2848
        }
2849
    }
2850
2851
    if (verbose)
2852
    {
2853
        sys_output("[*] unpacking completed\n");
2854
    }
2855
2856
    if (type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PATCH || type == PKG_TYPE_VITA_THEME)
2857
    {
2858
        if (verbose)
2859
        {
2860
            sys_output("[*] creating sce_sys/package/head.bin\n");
2861
        }
2862
        snprintf(path, sizeof(path), "%s/sce_sys/package/head.bin", root);
2863
        out_add_parent(path);
2864
2865
        out_begin_file(path, 0);
2866
        uint64_t head_size = enc_offset + items_size;
2867
        uint64_t head_offset = 0;
2868
        while (head_size != 0)
2869
        {
2870
            uint8_t PKG_ALIGN(16) buffer[1 << 16];
2871
            uint32_t size = (uint32_t)min64(head_size, sizeof(buffer));
2872
            sys_read(pkg, head_offset, buffer, size);
2873
            out_write(buffer, size);
2874
            head_size -= size;
2875
            head_offset += size;
2876
        }
2877
        out_end_file();
2878
2879
        if (verbose)
2880
        {
2881
            sys_output("[*] creating sce_sys/package/tail.bin\n");
2882
        }
2883
        snprintf(path, sizeof(path), "%s/sce_sys/package/tail.bin", root);
2884
2885
        out_begin_file(path, 0);
2886
        uint64_t tail_offset = enc_offset + enc_size;
2887
        while (tail_offset != pkg_size)
2888
        {
2889
            uint8_t PKG_ALIGN(16) buffer[1 << 16];
2890
            uint32_t size = (uint32_t)min64(pkg_size - tail_offset, sizeof(buffer));
2891
            sys_read(pkg, tail_offset, buffer, size);
2892
            out_write(buffer, size);
2893
            tail_offset += size;
2894
        }
2895
        out_end_file();
2896
2897
        if (verbose)
2898
        {
2899
            sys_output("[*] creating sce_sys/package/stat.bin\n");
2900
        }
2901
        snprintf(path, sizeof(path), "%s/sce_sys/package/stat.bin", root);
2902
2903
        uint8_t stat[768] = { 0 };
2904
        out_begin_file(path, 0);
2905
        out_write(stat, sizeof(stat));
2906
        out_end_file();
2907
    }
2908
2909
    if ((type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_DLC || type == PKG_TYPE_VITA_PSM || type == PKG_TYPE_VITA_THEME || type == PKG_TYPE_PSX) && zrif_arg != NULL)
2910
    {
2911
        if (type == PKG_TYPE_VITA_PSM)
2912
        {
2913
            if (verbose)
2914
            {
2915
                sys_output("[*] creating RO/License\n");
2916
            }
2917
            snprintf(path, sizeof(path), "%s/RO/License", root);
2918
            out_add_folder(path);
2919
2920
            if (verbose)
2921
            {
2922
                sys_output("[*] creating RO/License/FAKE.rif\n");
2923
            }
2924
            snprintf(path, sizeof(path), "%s/RO/License/FAKE.rif", root);
2925
        }
2926
        else if (type == PKG_TYPE_PSX)
2927
        {
2928
            //For Pocketstation
2929
            if (verbose)
2930
            {
2931
                sys_output("[*] creating rif");
2932
            }
2933
            const char* rif_contentid = (char*)rif + 0x10;
2934
            snprintf(path, sizeof(path), "pspemu/PSP/LICENSE");
2935
            out_add_folder(path);
2936
            snprintf(path, sizeof(path), "pspemu/PSP/LICENSE/%s.rif", rif_contentid);
2937
        }
2938
        else
2939
        {
2940
            if (verbose)
2941
            {
2942
                sys_output("[*] creating sce_sys/package/work.bin\n");
2943
            }
2944
            snprintf(path, sizeof(path), "%s/sce_sys/package/work.bin", root);
2945
        }
2946
2947
        out_begin_file(path, 0);
2948
        out_write(rif, rif_size);
2949
        out_end_file();
2950
    }
2951
2952
    if (type == PKG_TYPE_VITA_THEME && bgdl == 1)
2953
    {
2954
        //get the parent directory
2955
        char* lastslash = strrchr(root, '/');
2956
        if (lastslash != NULL)
2957
        {
2958
            root[strlen(root)-strlen(lastslash)] = 0;
2959
        }
2960
        uint8_t pdb[0x200] = { 0 };
2961
        //PDB entries :: https://www.psdevwiki.com/ps3/Project_Database_(PDB)
2962
        memcpy(pdb+0x04,"\x64\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",0x10);
2963
        memcpy(pdb+0x14,"\x65\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",0x10); // 02 in d0.pdb, 00 in d1.pdb 
2964
        memcpy(pdb+0x24,"\x66\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00",0x0D);             // "Task unregister auto"
2965
        memcpy(pdb+0x31,"\x68\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",0x10); // unknown but required
2966
        memcpy(pdb+0x41,"\x6B\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x0C\x00\x00\x00",0x10); // "Task Subtype"
2967
        memcpy(pdb+0x51,"\x6C\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00",0x10); // unknown but required 
2968
        memcpy(pdb+0x61,"\x6D\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00",0x10); // unknown but required 
2969
        memcpy(pdb+0x71,"\x6E\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00",0x0D);
2970
        memcpy(pdb+0x7E,"\x6F\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",0x10); // unknown but required
2971
        memcpy(pdb+0x8E,"\x70\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01",0x0D);
2972
        memcpy(pdb+0x9B,"\x71\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01",0x0D);
2973
        memcpy(pdb+0xA8,"\x72\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",0x10);
2974
        memcpy(pdb+0xB8,"\x73\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00",0x0D);
2975
        memcpy(pdb+0xC5,"\x74\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00",0x0D);
2976
        memcpy(pdb+0xD2,"\x69\x00\x00\x00\x44\x00\x00\x00\x44\x00\x00\x00",0x0C);                 //pkg title
2977
        memcpy(pdb+0xDE,title,0x40);
2978
        memcpy(pdb+0x122,"\xD9\x00\x00\x00\x25\x00\x00\x00\x25\x00\x00\x00",0x0C);                 //ContentID
2979
        memcpy(pdb+0x12E,id,0x25);
2980
        memcpy(pdb+0x153,"\xDA\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01",0x0D);            // Download Complete Flag
2981
        memcpy(pdb+0x160,"\xDC\x00\x00\x00\x0A\x00\x00\x00\x0A\x00\x00\x00",0x0C);                //Content ID 
2982
        memcpy(pdb+0x16C,id,0x09);
2983
2984
        pdb[0x20] = 0x02;
2985
        if (verbose)
2986
        {
2987
            sys_output("[*] creating d0.pdb\n");
2988
        }
2989
        snprintf(path, sizeof(path), "%s/d0.pdb", root);
2990
        out_begin_file(path,0);
2991
        out_write(pdb,sizeof(pdb));
2992
        out_end_file();
2993
2994
        pdb[0x20] = 0x00;
2995
        if (verbose)
2996
        {
2997
            sys_output("[*] creating d1.pdb\n");
2998
        }
2999
        snprintf(path, sizeof(path), "%s/d1.pdb", root);
3000
        out_begin_file(path,0);
3001
        out_write(pdb,sizeof(pdb));
3002
        out_end_file();
3003
3004
        if (verbose)
3005
        {
3006
            sys_output("[*] creating f0.pdb\n");
3007
        }
3008
        snprintf(path, sizeof(path), "%s/f0.pdb", root);
3009
        out_begin_file(path,0);
3010
        out_write(pdb,0);
3011
        out_end_file();
3012
    }
3013
3014
    if (type == PKG_TYPE_VITA_PSM)
3015
    {
3016
        if (verbose)
3017
        {
3018
            sys_output("[*] creating RW\n");
3019
        }
3020
        snprintf(path, sizeof(path), "%s/RW", root);
3021
        out_add_folder(path);
3022
3023
        if (verbose)
3024
        {
3025
            sys_output("[*] creating RW/Documents\n");
3026
        }
3027
        snprintf(path, sizeof(path), "%s/RW/Documents", root);
3028
        out_add_folder(path);
3029
3030
        if (verbose)
3031
        {
3032
            sys_output("[*] creating RW/Temp\n");
3033
        }
3034
        snprintf(path, sizeof(path), "%s/RW/Temp", root);
3035
        out_add_folder(path);
3036
3037
        if (verbose)
3038
        {
3039
            sys_output("[*] creating RW/System\n");
3040
        }
3041
        snprintf(path, sizeof(path), "%s/RW/System", root);
3042
        out_add_folder(path);
3043
3044
        if (verbose)
3045
        {
3046
            sys_output("[*] creating RW/System/content_id\n");
3047
        }
3048
        snprintf(path, sizeof(path), "%s/RW/System/content_id", root);
3049
        out_begin_file(path, 0);
3050
        out_write(pkg_header + 0x30, 0x30);
3051
        out_end_file();
3052
3053
        if (verbose)
3054
        {
3055
            sys_output("[*] creating RW/System/pm.dat\n");
3056
        }
3057
        snprintf(path, sizeof(path), "%s/RW/System/pm.dat", root);
3058
3059
        uint8_t pm[1 << 16] = { 0 };
3060
        out_begin_file(path, 0);
3061
        out_write(pm, sizeof(pm));
3062
        out_end_file();
3063
    }
3064
3065
    out_end();
3066
3067
    if (verbose)
3068
    {
3069
        if (type == PKG_TYPE_VITA_APP || type == PKG_TYPE_VITA_PATCH)
3070
        {
3071
            sys_output("[*] minimum fw version required: %s\n", min_version);
3072
        }
3073
        sys_output("[*] done!\n");
3074
    }
3075
    sys_output_done();
3076
}
3077
07070100000009000081A4000001ED0000006B000000015EE6501B00005B74000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_aes.c#include "pkg2zip_aes.h"
3078
#include "pkg2zip_utils.h"
3079
3080
#include <assert.h>
3081
#include <string.h>
3082
3083
#if defined(_MSC_VER)
3084
#define PLATFORM_SUPPORTS_AESNI 1
3085
3086
#include <intrin.h>
3087
static void get_cpuid(uint32_t level, uint32_t* arr)
3088
{
3089
    __cpuidex((int*)arr, level, 0);
3090
}
3091
3092
#elif defined(__x86_64__) || defined(__i386__)
3093
#define PLATFORM_SUPPORTS_AESNI 1
3094
3095
#include <cpuid.h>
3096
static void get_cpuid(uint32_t level, uint32_t* arr)
3097
{
3098
    __cpuid_count(level, 0, arr[0], arr[1], arr[2], arr[3]);
3099
}
3100
3101
#else
3102
#define PLATFORM_SUPPORTS_AESNI 0
3103
#endif
3104
3105
#if PLATFORM_SUPPORTS_AESNI
3106
static int aes128_supported_x86()
3107
{
3108
    static int init = 0;
3109
    static int supported;
3110
    if (!init)
3111
    {
3112
        init = 1;
3113
3114
        uint32_t a[4];
3115
        get_cpuid(0, a);
3116
3117
        if (a[0] >= 1)
3118
        {
3119
            get_cpuid(1, a);
3120
            supported = ((a[2] & (1 << 9)) && (a[2] & (1 << 25)));
3121
        }
3122
    }
3123
    return supported;
3124
}
3125
3126
void aes128_init_x86(aes128_key* context, const uint8_t* key);
3127
void aes128_init_dec_x86(aes128_key* context, const uint8_t* key);
3128
void aes128_ecb_encrypt_x86(const aes128_key* context, const uint8_t* input, uint8_t* output);
3129
void aes128_ecb_decrypt_x86(const aes128_key* context, const uint8_t* input, uint8_t* output);
3130
void aes128_ctr_xor_x86(const aes128_key* context, const uint8_t* iv, uint8_t* buffer, size_t size);
3131
void aes128_cmac_process_x86(const aes128_key* ctx, uint8_t* block, const uint8_t *buffer, uint32_t size);
3132
void aes128_psp_decrypt_x86(const aes128_key* ctx, const uint8_t* prev, const uint8_t* block, uint8_t* buffer, uint32_t size);
3133
#endif
3134
3135
static const uint8_t rcon[] = {
3136
    0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36,
3137
};
3138
3139
static const uint8_t Te[] = {
3140
    0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
3141
    0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
3142
    0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
3143
    0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
3144
    0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
3145
    0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
3146
    0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
3147
    0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
3148
    0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
3149
    0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
3150
    0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
3151
    0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
3152
    0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
3153
    0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
3154
    0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
3155
    0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
3156
};
3157
3158
static uint8_t Td[] = {
3159
    0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
3160
    0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
3161
    0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
3162
    0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
3163
    0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
3164
    0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
3165
    0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
3166
    0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
3167
    0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
3168
    0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
3169
    0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
3170
    0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
3171
    0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
3172
    0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
3173
    0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
3174
    0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
3175
};
3176
3177
static const uint32_t TE[] = {
3178
    0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
3179
    0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a,
3180
    0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b,
3181
    0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b,
3182
    0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f,
3183
    0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f,
3184
    0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5,
3185
    0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f,
3186
    0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb,
3187
    0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497,
3188
    0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed,
3189
    0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a,
3190
    0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594,
3191
    0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3,
3192
    0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504,
3193
    0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d,
3194
    0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739,
3195
    0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395,
3196
    0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883,
3197
    0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76,
3198
    0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4,
3199
    0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b,
3200
    0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0,
3201
    0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818,
3202
    0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651,
3203
    0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85,
3204
    0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12,
3205
    0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9,
3206
    0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7,
3207
    0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a,
3208
    0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8,
3209
    0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a,
3210
};
3211
3212
static const uint32_t TD[] = {
3213
    0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
3214
    0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f,
3215
    0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6,
3216
    0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844,
3217
    0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4,
3218
    0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94,
3219
    0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a,
3220
    0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c,
3221
    0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a,
3222
    0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051,
3223
    0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff,
3224
    0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb,
3225
    0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e,
3226
    0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a,
3227
    0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16,
3228
    0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8,
3229
    0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34,
3230
    0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120,
3231
    0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0,
3232
    0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef,
3233
    0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4,
3234
    0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5,
3235
    0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b,
3236
    0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6,
3237
    0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0,
3238
    0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f,
3239
    0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f,
3240
    0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713,
3241
    0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c,
3242
    0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86,
3243
    0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541,
3244
    0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742,
3245
};
3246
3247
static uint8_t byte32(uint32_t x, int n)
3248
{
3249
    return (uint8_t)(x >> (8 * n));
3250
}
3251
3252
static uint32_t ror32(uint32_t x, int n)
3253
{
3254
    return (x >> n) | (x << (32 - n));
3255
}
3256
3257
static uint32_t setup_mix(uint32_t x)
3258
{
3259
    return (Te[byte32(x, 2)] << 24) ^ (Te[byte32(x, 1)] << 16) ^ (Te[byte32(x, 0)] << 8) ^ Te[byte32(x, 3)];
3260
}
3261
3262
static uint32_t setup_mix2(uint32_t x)
3263
{
3264
    return TD[Te[byte32(x, 3)]] ^ ror32(TD[Te[byte32(x, 2)]], 8) ^ ror32(TD[Te[byte32(x, 1)]], 16) ^ ror32(TD[Te[byte32(x, 0)]], 24);
3265
}
3266
3267
void aes128_init(aes128_key* ctx, const uint8_t* key)
3268
{
3269
#if PLATFORM_SUPPORTS_AESNI
3270
    if (aes128_supported_x86())
3271
    {
3272
        aes128_init_x86(ctx, key);
3273
        return;
3274
    }
3275
#endif
3276
3277
    uint32_t* ekey = ctx->key;
3278
3279
    ekey[0] = get32be(key +  0);
3280
    ekey[1] = get32be(key +  4);
3281
    ekey[2] = get32be(key +  8);
3282
    ekey[3] = get32be(key + 12);
3283
3284
    for (size_t i=0; i<10; i++)
3285
    {
3286
        uint32_t temp = ekey[3];
3287
        ekey[4] = ekey[0] ^ setup_mix(temp) ^ (rcon[i] << 24);
3288
        ekey[5] = ekey[1] ^ ekey[4];
3289
        ekey[6] = ekey[2] ^ ekey[5];
3290
        ekey[7] = ekey[3] ^ ekey[6];
3291
        ekey += 4;
3292
    }
3293
}
3294
3295
void aes128_init_dec(aes128_key* ctx, const uint8_t* key)
3296
{
3297
#if PLATFORM_SUPPORTS_AESNI
3298
    if (aes128_supported_x86())
3299
    {
3300
        aes128_init_dec_x86(ctx, key);
3301
        return;
3302
    }
3303
#endif
3304
3305
    aes128_key enc;
3306
    aes128_init(&enc, key);
3307
3308
    uint32_t* ekey = enc.key + 40;
3309
    uint32_t* dkey = ctx->key;
3310
3311
    *dkey++ = ekey[0];
3312
    *dkey++ = ekey[1];
3313
    *dkey++ = ekey[2];
3314
    *dkey++ = ekey[3];
3315
    ekey -= 4;
3316
3317
    for (size_t i = 0; i < 9; i++)
3318
    {
3319
        *dkey++ = setup_mix2(ekey[0]);
3320
        *dkey++ = setup_mix2(ekey[1]);
3321
        *dkey++ = setup_mix2(ekey[2]);
3322
        *dkey++ = setup_mix2(ekey[3]);
3323
        ekey -= 4;
3324
    }
3325
3326
    *dkey++ = ekey[0];
3327
    *dkey++ = ekey[1];
3328
    *dkey++ = ekey[2];
3329
    *dkey++ = ekey[3];
3330
}
3331
3332
static void aes128_encrypt(const aes128_key* ctx, const uint8_t* input, uint8_t* output)
3333
{
3334
    uint32_t t0, t1, t2, t3;
3335
    const uint32_t* key = ctx->key;
3336
3337
    uint32_t s0 = get32be(input + 0) ^ *key++;
3338
    uint32_t s1 = get32be(input + 4) ^ *key++;
3339
    uint32_t s2 = get32be(input + 8) ^ *key++;
3340
    uint32_t s3 = get32be(input + 12) ^ *key++;
3341
3342
    for (size_t i = 0; i<4; i++)
3343
    {
3344
        t0 = TE[byte32(s0, 3)] ^ ror32(TE[byte32(s1, 2)], 8) ^ ror32(TE[byte32(s2, 1)], 16) ^ ror32(TE[byte32(s3, 0)], 24) ^ *key++;
3345
        t1 = TE[byte32(s1, 3)] ^ ror32(TE[byte32(s2, 2)], 8) ^ ror32(TE[byte32(s3, 1)], 16) ^ ror32(TE[byte32(s0, 0)], 24) ^ *key++;
3346
        t2 = TE[byte32(s2, 3)] ^ ror32(TE[byte32(s3, 2)], 8) ^ ror32(TE[byte32(s0, 1)], 16) ^ ror32(TE[byte32(s1, 0)], 24) ^ *key++;
3347
        t3 = TE[byte32(s3, 3)] ^ ror32(TE[byte32(s0, 2)], 8) ^ ror32(TE[byte32(s1, 1)], 16) ^ ror32(TE[byte32(s2, 0)], 24) ^ *key++;
3348
3349
        s0 = TE[byte32(t0, 3)] ^ ror32(TE[byte32(t1, 2)], 8) ^ ror32(TE[byte32(t2, 1)], 16) ^ ror32(TE[byte32(t3, 0)], 24) ^ *key++;
3350
        s1 = TE[byte32(t1, 3)] ^ ror32(TE[byte32(t2, 2)], 8) ^ ror32(TE[byte32(t3, 1)], 16) ^ ror32(TE[byte32(t0, 0)], 24) ^ *key++;
3351
        s2 = TE[byte32(t2, 3)] ^ ror32(TE[byte32(t3, 2)], 8) ^ ror32(TE[byte32(t0, 1)], 16) ^ ror32(TE[byte32(t1, 0)], 24) ^ *key++;
3352
        s3 = TE[byte32(t3, 3)] ^ ror32(TE[byte32(t0, 2)], 8) ^ ror32(TE[byte32(t1, 1)], 16) ^ ror32(TE[byte32(t2, 0)], 24) ^ *key++;
3353
    }
3354
3355
    t0 = TE[byte32(s0, 3)] ^ ror32(TE[byte32(s1, 2)], 8) ^ ror32(TE[byte32(s2, 1)], 16) ^ ror32(TE[byte32(s3, 0)], 24) ^ *key++;
3356
    t1 = TE[byte32(s1, 3)] ^ ror32(TE[byte32(s2, 2)], 8) ^ ror32(TE[byte32(s3, 1)], 16) ^ ror32(TE[byte32(s0, 0)], 24) ^ *key++;
3357
    t2 = TE[byte32(s2, 3)] ^ ror32(TE[byte32(s3, 2)], 8) ^ ror32(TE[byte32(s0, 1)], 16) ^ ror32(TE[byte32(s1, 0)], 24) ^ *key++;
3358
    t3 = TE[byte32(s3, 3)] ^ ror32(TE[byte32(s0, 2)], 8) ^ ror32(TE[byte32(s1, 1)], 16) ^ ror32(TE[byte32(s2, 0)], 24) ^ *key++;
3359
3360
    s0 = (Te[byte32(t0, 3)] << 24) ^ (Te[byte32(t1, 2)] << 16) ^ (Te[byte32(t2, 1)] << 8) ^ Te[byte32(t3, 0)] ^ *key++;
3361
    s1 = (Te[byte32(t1, 3)] << 24) ^ (Te[byte32(t2, 2)] << 16) ^ (Te[byte32(t3, 1)] << 8) ^ Te[byte32(t0, 0)] ^ *key++;
3362
    s2 = (Te[byte32(t2, 3)] << 24) ^ (Te[byte32(t3, 2)] << 16) ^ (Te[byte32(t0, 1)] << 8) ^ Te[byte32(t1, 0)] ^ *key++;
3363
    s3 = (Te[byte32(t3, 3)] << 24) ^ (Te[byte32(t0, 2)] << 16) ^ (Te[byte32(t1, 1)] << 8) ^ Te[byte32(t2, 0)] ^ *key++;
3364
3365
    set32be(output + 0, s0);
3366
    set32be(output + 4, s1);
3367
    set32be(output + 8, s2);
3368
    set32be(output + 12, s3);
3369
}
3370
3371
static void aes128_decrypt(const aes128_key* ctx, const uint8_t* input, uint8_t* output)
3372
{
3373
    const uint32_t* key = ctx->key;
3374
3375
    uint32_t s0 = get32be(input + 0) ^ *key++;
3376
    uint32_t s1 = get32be(input + 4) ^ *key++;
3377
    uint32_t s2 = get32be(input + 8) ^ *key++;
3378
    uint32_t s3 = get32be(input + 12) ^ *key++;
3379
3380
    uint32_t t0 = TD[byte32(s0, 3)] ^ ror32(TD[byte32(s3, 2)], 8) ^ ror32(TD[byte32(s2, 1)], 16) ^ ror32(TD[byte32(s1, 0)], 24) ^ *key++;
3381
    uint32_t t1 = TD[byte32(s1, 3)] ^ ror32(TD[byte32(s0, 2)], 8) ^ ror32(TD[byte32(s3, 1)], 16) ^ ror32(TD[byte32(s2, 0)], 24) ^ *key++;
3382
    uint32_t t2 = TD[byte32(s2, 3)] ^ ror32(TD[byte32(s1, 2)], 8) ^ ror32(TD[byte32(s0, 1)], 16) ^ ror32(TD[byte32(s3, 0)], 24) ^ *key++;
3383
    uint32_t t3 = TD[byte32(s3, 3)] ^ ror32(TD[byte32(s2, 2)], 8) ^ ror32(TD[byte32(s1, 1)], 16) ^ ror32(TD[byte32(s0, 0)], 24) ^ *key++;
3384
3385
    for (size_t i = 0; i < 4; i++)
3386
    {
3387
        s0 = TD[byte32(t0, 3)] ^ ror32(TD[byte32(t3, 2)], 8) ^ ror32(TD[byte32(t2, 1)], 16) ^ ror32(TD[byte32(t1, 0)], 24) ^ *key++;
3388
        s1 = TD[byte32(t1, 3)] ^ ror32(TD[byte32(t0, 2)], 8) ^ ror32(TD[byte32(t3, 1)], 16) ^ ror32(TD[byte32(t2, 0)], 24) ^ *key++;
3389
        s2 = TD[byte32(t2, 3)] ^ ror32(TD[byte32(t1, 2)], 8) ^ ror32(TD[byte32(t0, 1)], 16) ^ ror32(TD[byte32(t3, 0)], 24) ^ *key++;
3390
        s3 = TD[byte32(t3, 3)] ^ ror32(TD[byte32(t2, 2)], 8) ^ ror32(TD[byte32(t1, 1)], 16) ^ ror32(TD[byte32(t0, 0)], 24) ^ *key++;
3391
3392
        t0 = TD[byte32(s0, 3)] ^ ror32(TD[byte32(s3, 2)], 8) ^ ror32(TD[byte32(s2, 1)], 16) ^ ror32(TD[byte32(s1, 0)], 24) ^ *key++;
3393
        t1 = TD[byte32(s1, 3)] ^ ror32(TD[byte32(s0, 2)], 8) ^ ror32(TD[byte32(s3, 1)], 16) ^ ror32(TD[byte32(s2, 0)], 24) ^ *key++;
3394
        t2 = TD[byte32(s2, 3)] ^ ror32(TD[byte32(s1, 2)], 8) ^ ror32(TD[byte32(s0, 1)], 16) ^ ror32(TD[byte32(s3, 0)], 24) ^ *key++;
3395
        t3 = TD[byte32(s3, 3)] ^ ror32(TD[byte32(s2, 2)], 8) ^ ror32(TD[byte32(s1, 1)], 16) ^ ror32(TD[byte32(s0, 0)], 24) ^ *key++;
3396
    }
3397
3398
    s0 = (Td[byte32(t0, 3)] << 24) ^ (Td[byte32(t3, 2)] << 16) ^ (Td[byte32(t2, 1)] << 8) ^ Td[byte32(t1, 0)] ^ *key++;
3399
    s1 = (Td[byte32(t1, 3)] << 24) ^ (Td[byte32(t0, 2)] << 16) ^ (Td[byte32(t3, 1)] << 8) ^ Td[byte32(t2, 0)] ^ *key++;
3400
    s2 = (Td[byte32(t2, 3)] << 24) ^ (Td[byte32(t1, 2)] << 16) ^ (Td[byte32(t0, 1)] << 8) ^ Td[byte32(t3, 0)] ^ *key++;
3401
    s3 = (Td[byte32(t3, 3)] << 24) ^ (Td[byte32(t2, 2)] << 16) ^ (Td[byte32(t1, 1)] << 8) ^ Td[byte32(t0, 0)] ^ *key++;
3402
3403
    set32be(output + 0, s0);
3404
    set32be(output + 4, s1);
3405
    set32be(output + 8, s2);
3406
    set32be(output + 12, s3);
3407
}
3408
3409
void aes128_ecb_encrypt(const aes128_key* ctx, const uint8_t* input, uint8_t* output)
3410
{
3411
#if PLATFORM_SUPPORTS_AESNI
3412
    if (aes128_supported_x86())
3413
    {
3414
        aes128_ecb_encrypt_x86(ctx, input, output);
3415
        return;
3416
    }
3417
#endif
3418
    aes128_encrypt(ctx, input, output);
3419
}
3420
3421
void aes128_ecb_decrypt(const aes128_key* ctx, const uint8_t* input, uint8_t* output)
3422
{
3423
#if PLATFORM_SUPPORTS_AESNI
3424
    if (aes128_supported_x86())
3425
    {
3426
        aes128_ecb_decrypt_x86(ctx, input, output);
3427
        return;
3428
    }
3429
#endif
3430
    aes128_decrypt(ctx, input, output);
3431
}
3432
3433
static void ctr_add(uint8_t* counter, uint64_t n)
3434
{
3435
    for (int i=15; i>=0; i--)
3436
    {
3437
        n = n + counter[i];
3438
        counter[i] = (uint8_t)n;
3439
        n >>= 8;
3440
    }
3441
}
3442
3443
void aes128_ctr_xor(const aes128_key* context, const uint8_t* iv, uint64_t block, uint8_t* buffer, size_t size)
3444
{
3445
    uint8_t tmp[16];
3446
    uint8_t counter[16];
3447
    for (uint32_t i=0; i<16; i++)
3448
    {
3449
        counter[i] = iv[i];
3450
    }
3451
    ctr_add(counter, block);
3452
3453
#if PLATFORM_SUPPORTS_AESNI
3454
    if (aes128_supported_x86())
3455
    {
3456
        aes128_ctr_xor_x86(context, counter, buffer, size);
3457
        return;
3458
    }
3459
#endif
3460
3461
    while (size >= 16)
3462
    {
3463
        aes128_encrypt(context, counter, tmp);
3464
        for (uint32_t i=0; i<16; i++)
3465
        {
3466
            *buffer++ ^= tmp[i];
3467
        }
3468
        ctr_add(counter, 1);
3469
        size -= 16;
3470
    }
3471
3472
    if (size != 0)
3473
    {
3474
        aes128_encrypt(context, counter, tmp);
3475
        for (size_t i=0; i<size; i++)
3476
        {
3477
            *buffer++ ^= tmp[i];
3478
        }
3479
    }
3480
}
3481
3482
// https://tools.ietf.org/rfc/rfc4493.txt
3483
3484
typedef struct {
3485
    aes128_key key;
3486
    uint8_t last[16];
3487
    uint8_t block[16];
3488
    uint32_t size;
3489
} aes128_cmac_ctx;
3490
3491
static void aes128_cmac_process(const aes128_key* ctx, uint8_t* block, const uint8_t *buffer, uint32_t size)
3492
{
3493
    assert(size % 16 == 0);
3494
3495
#if PLATFORM_SUPPORTS_AESNI
3496
    if (aes128_supported_x86())
3497
    {
3498
        aes128_cmac_process_x86(ctx, block, buffer, size);
3499
        return;
3500
    }
3501
#endif
3502
    for (uint32_t i = 0; i < size; i += 16)
3503
    {
3504
        for (size_t k = 0; k < 16; k++)
3505
        {
3506
            block[k] ^= *buffer++;
3507
        }
3508
        aes128_ecb_encrypt(ctx, block, block);
3509
    }
3510
}
3511
3512
static void aes128_cmac_init(aes128_cmac_ctx* ctx, const uint8_t* key)
3513
{
3514
    aes128_init(&ctx->key, key);
3515
    memset(ctx->last, 0, 16);
3516
    ctx->size = 0;
3517
}
3518
3519
static void aes128_cmac_update(aes128_cmac_ctx* ctx, const uint8_t* buffer, uint32_t size)
3520
{
3521
    if (ctx->size + size <= 16)
3522
    {
3523
        memcpy(ctx->block + ctx->size, buffer, size);
3524
        ctx->size += size;
3525
        return;
3526
    }
3527
3528
    if (ctx->size != 0)
3529
    {
3530
        uint32_t avail = 16 - ctx->size;
3531
        memcpy(ctx->block + ctx->size, buffer, avail < size ? avail : size);
3532
        buffer += avail;
3533
        size -= avail;
3534
3535
        aes128_cmac_process(&ctx->key, ctx->last, ctx->block, 16);
3536
    }
3537
3538
    if (size >= 16)
3539
    {
3540
        uint32_t full = (size - 1) & ~15;
3541
        aes128_cmac_process(&ctx->key, ctx->last, buffer, full);
3542
        buffer += full;
3543
        size -= full;
3544
    }
3545
3546
    memcpy(ctx->block, buffer, size);
3547
    ctx->size = size;
3548
}
3549
3550
static void cmac_gfmul(uint8_t* block)
3551
{
3552
    uint8_t carry = 0;
3553
    for (int i = 15; i >= 0; i--)
3554
    {
3555
        uint8_t x = block[i];
3556
        block[i] = (block[i] << 1) | (carry >> 7);
3557
        carry = x;
3558
    }
3559
3560
    block[15] ^= (carry & 0x80 ? 0x87 : 0);
3561
}
3562
3563
static void aes128_cmac_done(aes128_cmac_ctx* ctx, uint8_t* mac)
3564
{
3565
    uint8_t zero[16] = { 0 };
3566
    aes128_ecb_encrypt(&ctx->key, zero, mac);
3567
3568
    cmac_gfmul(mac);
3569
3570
    if (ctx->size != 16)
3571
    {
3572
        cmac_gfmul(mac);
3573
3574
        ctx->block[ctx->size] = 0x80;
3575
        memset(ctx->block + ctx->size + 1, 0, 16 - (ctx->size + 1));
3576
    }
3577
3578
    for (size_t i = 0; i < 16; i++)
3579
    {
3580
        mac[i] ^= ctx->block[i];
3581
    }
3582
3583
    aes128_cmac_process(&ctx->key, mac, ctx->last, 16);
3584
}
3585
3586
void aes128_cmac(const uint8_t* key, const uint8_t* buffer, uint32_t size, uint8_t* mac)
3587
{
3588
    aes128_cmac_ctx ctx;
3589
    aes128_cmac_init(&ctx, key);
3590
    aes128_cmac_update(&ctx, buffer, size);
3591
    aes128_cmac_done(&ctx, mac);
3592
}
3593
3594
void aes128_psp_decrypt(const aes128_key* ctx, const uint8_t* iv, uint32_t index, uint8_t* buffer, uint32_t size)
3595
{
3596
    assert(size % 16 == 0);
3597
3598
    uint8_t PKG_ALIGN(16) prev[16];
3599
    uint8_t PKG_ALIGN(16) block[16];
3600
3601
    if (index == 0)
3602
    {
3603
        memset(prev, 0, 16);
3604
    }
3605
    else
3606
    {
3607
        memcpy(prev, iv, 12);
3608
        set32le(prev + 12, index);
3609
    }
3610
3611
    memcpy(block, iv, 16);
3612
    set32le(block + 12, index);
3613
3614
#if PLATFORM_SUPPORTS_AESNI
3615
    if (aes128_supported_x86())
3616
    {
3617
        aes128_psp_decrypt_x86(ctx, prev, block, buffer, size);
3618
        return;
3619
    }
3620
#endif
3621
3622
    for (uint32_t i = 0; i < size; i += 16)
3623
    {
3624
        set32le(block + 12, get32le(block + 12) + 1);
3625
3626
        uint8_t out[16];
3627
        aes128_ecb_decrypt(ctx, block, out);
3628
3629
        for (size_t k = 0; k < 16; k++)
3630
        {
3631
            *buffer++ ^= prev[k] ^ out[k];
3632
        }
3633
        memcpy(prev, block, 16);
3634
    }
3635
}
3636
0707010000000A000081A4000001ED0000006B000000015EE6501B000002D7000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_aes.h#pragma once
3637
3638
#include "pkg2zip_utils.h"
3639
3640
typedef struct aes128_key {
3641
    uint32_t PKG_ALIGN(16) key[44];
3642
} aes128_key;
3643
3644
void aes128_init(aes128_key* ctx, const uint8_t* key);
3645
void aes128_init_dec(aes128_key* ctx, const uint8_t* key);
3646
3647
void aes128_ecb_encrypt(const aes128_key* ctx, const uint8_t* input, uint8_t* output);
3648
void aes128_ecb_decrypt(const aes128_key* ctx, const uint8_t* input, uint8_t* output);
3649
3650
void aes128_ctr_xor(const aes128_key* ctx, const uint8_t* iv, uint64_t block, uint8_t* buffer, size_t size);
3651
3652
void aes128_cmac(const uint8_t* key, const uint8_t* buffer, uint32_t size, uint8_t* mac);
3653
3654
void aes128_psp_decrypt(const aes128_key* ctx, const uint8_t* iv, uint32_t index, uint8_t* buffer, uint32_t size);
3655
0707010000000B000081A4000001ED0000006B000000015EE6501B00001776000000000000000000000000000000000000003D00000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_aes_x86.c#include "pkg2zip_aes.h"
3656
3657
#include <string.h>
3658
#include <wmmintrin.h> // AESNI
3659
#include <tmmintrin.h> // SSSE3
3660
3661
#define AES128_INIT(ctx, x, rcon)           \
3662
{                                           \
3663
    __m128i a, b;                           \
3664
    _mm_store_si128(ctx, x);                \
3665
    a = _mm_aeskeygenassist_si128(x, rcon); \
3666
    a = _mm_shuffle_epi32(a, 0xff);         \
3667
    b = _mm_slli_si128(x, 4);               \
3668
    x = _mm_xor_si128(x, b);                \
3669
    b = _mm_slli_si128(b, 4);               \
3670
    x = _mm_xor_si128(x, b);                \
3671
    b = _mm_slli_si128(b, 4);               \
3672
    x = _mm_xor_si128(x, b);                \
3673
    x = _mm_xor_si128(x, a);                \
3674
}
3675
3676
void aes128_init_x86(aes128_key* ctx, const uint8_t* key)
3677
{
3678
    __m128i* ekey = (__m128i*)ctx->key;
3679
3680
    __m128i x = _mm_loadu_si128((const __m128i*)key);
3681
    AES128_INIT(ekey + 0, x, 0x01);
3682
    AES128_INIT(ekey + 1, x, 0x02);
3683
    AES128_INIT(ekey + 2, x, 0x04);
3684
    AES128_INIT(ekey + 3, x, 0x08);
3685
    AES128_INIT(ekey + 4, x, 0x10);
3686
    AES128_INIT(ekey + 5, x, 0x20);
3687
    AES128_INIT(ekey + 6, x, 0x40);
3688
    AES128_INIT(ekey + 7, x, 0x80);
3689
    AES128_INIT(ekey + 8, x, 0x1b);
3690
    AES128_INIT(ekey + 9, x, 0x36);
3691
    _mm_store_si128(ekey + 10, x);
3692
}
3693
3694
void aes128_init_dec_x86(aes128_key* ctx, const uint8_t* key)
3695
{
3696
    aes128_key enc;
3697
    aes128_init_x86(&enc, key);
3698
3699
    const __m128i* ekey = (__m128i*)&enc.key;
3700
    __m128i* dkey = (__m128i*)&ctx->key;
3701
3702
    _mm_store_si128(dkey + 10, _mm_load_si128(ekey + 0));
3703
    for (size_t i = 1; i < 10; i++)
3704
    {
3705
        _mm_store_si128(dkey + 10 - i, _mm_aesimc_si128(_mm_load_si128(ekey + i)));
3706
    }
3707
    _mm_store_si128(dkey + 0, _mm_load_si128(ekey + 10));
3708
}
3709
3710
static __m128i aes128_encrypt_x86(__m128i input, const __m128i* key)
3711
{
3712
    __m128i tmp = _mm_xor_si128(input, _mm_load_si128(key + 0));
3713
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 1));
3714
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 2));
3715
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 3));
3716
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 4));
3717
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 5));
3718
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 6));
3719
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 7));
3720
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 8));
3721
    tmp = _mm_aesenc_si128(tmp, _mm_load_si128(key + 9));
3722
    return _mm_aesenclast_si128(tmp, _mm_load_si128(key + 10));
3723
}
3724
3725
static __m128i aes128_decrypt_x86(__m128i input, const __m128i* key)
3726
{
3727
    __m128i tmp = _mm_xor_si128(input, _mm_load_si128(key + 0));
3728
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 1));
3729
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 2));
3730
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 3));
3731
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 4));
3732
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 5));
3733
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 6));
3734
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 7));
3735
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 8));
3736
    tmp = _mm_aesdec_si128(tmp, _mm_load_si128(key + 9));
3737
    return _mm_aesdeclast_si128(tmp, _mm_load_si128(key + 10));
3738
}
3739
3740
void aes128_ecb_encrypt_x86(const aes128_key* ctx, const uint8_t* input, uint8_t* output)
3741
{
3742
    const __m128i* key = (__m128i*)ctx->key;
3743
    __m128i tmp = aes128_encrypt_x86(_mm_loadu_si128((const __m128i*)input), key);
3744
    _mm_storeu_si128((__m128i*)output, tmp);
3745
}
3746
3747
void aes128_ecb_decrypt_x86(const aes128_key* ctx, const uint8_t* input, uint8_t* output)
3748
{
3749
    const __m128i* key = (__m128i*)ctx->key;
3750
    __m128i tmp = aes128_decrypt_x86(_mm_loadu_si128((const __m128i*)input), key);
3751
    _mm_storeu_si128((__m128i*)output, tmp);
3752
}
3753
3754
static __m128i ctr_increment(__m128i counter)
3755
{
3756
    __m128i swap = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
3757
    __m128i tmp = _mm_shuffle_epi8(counter, swap);
3758
    tmp = _mm_add_epi64(tmp, _mm_set_epi32(0, 0, 0, 1));
3759
    return _mm_shuffle_epi8(tmp, swap);
3760
}
3761
3762
void aes128_ctr_xor_x86(const aes128_key* ctx, const uint8_t* iv, uint8_t* buffer, size_t size)
3763
{
3764
    const __m128i* key = (__m128i*)ctx->key;
3765
    __m128i counter = _mm_loadu_si128((const __m128i*)iv);
3766
3767
    while (size >= 16)
3768
    {
3769
        __m128i block = aes128_encrypt_x86(counter, key);
3770
        __m128i tmp = _mm_xor_si128(_mm_loadu_si128((const __m128i*)buffer), block);
3771
        _mm_storeu_si128((__m128i*)buffer, tmp);
3772
3773
        counter = ctr_increment(counter);
3774
3775
        buffer += 16;
3776
        size -= 16;
3777
    }
3778
3779
    if (size != 0)
3780
    {
3781
        uint8_t full[16];
3782
        memcpy(full, buffer, size);
3783
        memset(full + size, 0, 16 - size);
3784
3785
        __m128i block = aes128_encrypt_x86(counter, key);
3786
        __m128i tmp = _mm_xor_si128(_mm_loadu_si128((const __m128i*)full), block);
3787
        _mm_storeu_si128((__m128i*)full, tmp);
3788
3789
        memcpy(buffer, full, size);
3790
    }
3791
}
3792
3793
void aes128_cmac_process_x86(const aes128_key* ctx, uint8_t* block, const uint8_t* buffer, uint32_t size)
3794
{
3795
    const __m128i* key = (__m128i*)ctx->key;
3796
    __m128i* data = (__m128i*)buffer;
3797
3798
    __m128i tmp = _mm_loadu_si128((__m128i*)block);
3799
    for (uint32_t i = 0; i < size; i += 16)
3800
    {
3801
        __m128i input = _mm_loadu_si128(data++);
3802
        tmp = _mm_xor_si128(tmp, input);
3803
        tmp = aes128_encrypt_x86(tmp, key);
3804
    }
3805
    _mm_storeu_si128((__m128i*)block, tmp);
3806
}
3807
3808
void aes128_psp_decrypt_x86(const aes128_key* ctx, const uint8_t* prev, const uint8_t* block, uint8_t* buffer, uint32_t size)
3809
{
3810
    const __m128i* key = (__m128i*)ctx->key;
3811
    __m128i one = _mm_setr_epi32(0, 0, 0, 1);
3812
3813
    __m128i x = _mm_load_si128((__m128i*)prev);
3814
    __m128i y = _mm_load_si128((__m128i*)block);
3815
3816
    __m128i* data = (__m128i*)buffer;
3817
3818
    for (uint32_t i = 0; i < size; i += 16)
3819
    {
3820
        y = _mm_add_epi32(y, one);
3821
3822
        __m128i out = aes128_decrypt_x86(y, key);
3823
3824
        out = _mm_xor_si128(out, _mm_loadu_si128(data));
3825
        out = _mm_xor_si128(out, x);
3826
        _mm_storeu_si128(data++, out);
3827
        x = y;
3828
    }
3829
}
3830
0707010000000C000081A4000001ED0000006B000000015EE6501B00004282000000000000000000000000000000000000003B00000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_crc32.c#include "pkg2zip_crc32.h"
3831
#include "pkg2zip_utils.h"
3832
3833
#ifdef __linux__
3834
#include <endian.h>
3835
#endif
3836
3837
#if defined(_MSC_VER)
3838
#define PLATFORM_SUPPORTS_PCLMUL 1
3839
3840
#include <intrin.h>
3841
static void get_cpuid(uint32_t level, uint32_t* arr)
3842
{
3843
    __cpuidex((int*)arr, level, 0);
3844
}
3845
3846
#elif defined(__x86_64__) || defined(__i386__)
3847
#define PLATFORM_SUPPORTS_PCLMUL 1
3848
3849
#include <cpuid.h>
3850
static void get_cpuid(uint32_t level, uint32_t* arr)
3851
{
3852
    __cpuid_count(level, 0, arr[0], arr[1], arr[2], arr[3]);
3853
}
3854
3855
#else
3856
#define PLATFORM_SUPPORTS_PCLMUL 0
3857
#endif
3858
3859
#if PLATFORM_SUPPORTS_PCLMUL
3860
static int crc32_supported_x86()
3861
{
3862
    static int init = 0;
3863
    static int supported;
3864
    if (!init)
3865
    {
3866
        init = 1;
3867
3868
        uint32_t a[4];
3869
        get_cpuid(0, a);
3870
3871
        if (a[0] >= 1)
3872
        {
3873
            get_cpuid(1, a);
3874
            supported = ((a[2] & (1 << 9)) && (a[2] & (1 << 1)) && (a[2] & (1 << 19)));
3875
        }
3876
    }
3877
    return supported;
3878
}
3879
3880
void crc32_init_x86(crc32_ctx* ctx);
3881
void crc32_update_x86(crc32_ctx* ctx, const void* buffer, size_t size);
3882
uint32_t crc32_done_x86(crc32_ctx* ctx);
3883
3884
#endif
3885
3886
static const uint32_t crc32[4][256] =
3887
{
3888
    {
3889
        0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
3890
        0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
3891
        0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
3892
        0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
3893
        0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
3894
        0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
3895
        0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
3896
        0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
3897
        0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
3898
        0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
3899
        0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
3900
        0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
3901
        0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
3902
        0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
3903
        0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
3904
        0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
3905
        0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
3906
        0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
3907
        0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
3908
        0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
3909
        0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
3910
        0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
3911
        0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
3912
        0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
3913
        0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
3914
        0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
3915
        0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
3916
        0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
3917
        0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
3918
        0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
3919
        0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
3920
        0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
3921
    },
3922
    {
3923
        0x00000000, 0x191b3141, 0x32366282, 0x2b2d53c3, 0x646cc504, 0x7d77f445, 0x565aa786, 0x4f4196c7,
3924
        0xc8d98a08, 0xd1c2bb49, 0xfaefe88a, 0xe3f4d9cb, 0xacb54f0c, 0xb5ae7e4d, 0x9e832d8e, 0x87981ccf,
3925
        0x4ac21251, 0x53d92310, 0x78f470d3, 0x61ef4192, 0x2eaed755, 0x37b5e614, 0x1c98b5d7, 0x05838496,
3926
        0x821b9859, 0x9b00a918, 0xb02dfadb, 0xa936cb9a, 0xe6775d5d, 0xff6c6c1c, 0xd4413fdf, 0xcd5a0e9e,
3927
        0x958424a2, 0x8c9f15e3, 0xa7b24620, 0xbea97761, 0xf1e8e1a6, 0xe8f3d0e7, 0xc3de8324, 0xdac5b265,
3928
        0x5d5daeaa, 0x44469feb, 0x6f6bcc28, 0x7670fd69, 0x39316bae, 0x202a5aef, 0x0b07092c, 0x121c386d,
3929
        0xdf4636f3, 0xc65d07b2, 0xed705471, 0xf46b6530, 0xbb2af3f7, 0xa231c2b6, 0x891c9175, 0x9007a034,
3930
        0x179fbcfb, 0x0e848dba, 0x25a9de79, 0x3cb2ef38, 0x73f379ff, 0x6ae848be, 0x41c51b7d, 0x58de2a3c,
3931
        0xf0794f05, 0xe9627e44, 0xc24f2d87, 0xdb541cc6, 0x94158a01, 0x8d0ebb40, 0xa623e883, 0xbf38d9c2,
3932
        0x38a0c50d, 0x21bbf44c, 0x0a96a78f, 0x138d96ce, 0x5ccc0009, 0x45d73148, 0x6efa628b, 0x77e153ca,
3933
        0xbabb5d54, 0xa3a06c15, 0x888d3fd6, 0x91960e97, 0xded79850, 0xc7cca911, 0xece1fad2, 0xf5facb93,
3934
        0x7262d75c, 0x6b79e61d, 0x4054b5de, 0x594f849f, 0x160e1258, 0x0f152319, 0x243870da, 0x3d23419b,
3935
        0x65fd6ba7, 0x7ce65ae6, 0x57cb0925, 0x4ed03864, 0x0191aea3, 0x188a9fe2, 0x33a7cc21, 0x2abcfd60,
3936
        0xad24e1af, 0xb43fd0ee, 0x9f12832d, 0x8609b26c, 0xc94824ab, 0xd05315ea, 0xfb7e4629, 0xe2657768,
3937
        0x2f3f79f6, 0x362448b7, 0x1d091b74, 0x04122a35, 0x4b53bcf2, 0x52488db3, 0x7965de70, 0x607eef31,
3938
        0xe7e6f3fe, 0xfefdc2bf, 0xd5d0917c, 0xcccba03d, 0x838a36fa, 0x9a9107bb, 0xb1bc5478, 0xa8a76539,
3939
        0x3b83984b, 0x2298a90a, 0x09b5fac9, 0x10aecb88, 0x5fef5d4f, 0x46f46c0e, 0x6dd93fcd, 0x74c20e8c,
3940
        0xf35a1243, 0xea412302, 0xc16c70c1, 0xd8774180, 0x9736d747, 0x8e2de606, 0xa500b5c5, 0xbc1b8484,
3941
        0x71418a1a, 0x685abb5b, 0x4377e898, 0x5a6cd9d9, 0x152d4f1e, 0x0c367e5f, 0x271b2d9c, 0x3e001cdd,
3942
        0xb9980012, 0xa0833153, 0x8bae6290, 0x92b553d1, 0xddf4c516, 0xc4eff457, 0xefc2a794, 0xf6d996d5,
3943
        0xae07bce9, 0xb71c8da8, 0x9c31de6b, 0x852aef2a, 0xca6b79ed, 0xd37048ac, 0xf85d1b6f, 0xe1462a2e,
3944
        0x66de36e1, 0x7fc507a0, 0x54e85463, 0x4df36522, 0x02b2f3e5, 0x1ba9c2a4, 0x30849167, 0x299fa026,
3945
        0xe4c5aeb8, 0xfdde9ff9, 0xd6f3cc3a, 0xcfe8fd7b, 0x80a96bbc, 0x99b25afd, 0xb29f093e, 0xab84387f,
3946
        0x2c1c24b0, 0x350715f1, 0x1e2a4632, 0x07317773, 0x4870e1b4, 0x516bd0f5, 0x7a468336, 0x635db277,
3947
        0xcbfad74e, 0xd2e1e60f, 0xf9ccb5cc, 0xe0d7848d, 0xaf96124a, 0xb68d230b, 0x9da070c8, 0x84bb4189,
3948
        0x03235d46, 0x1a386c07, 0x31153fc4, 0x280e0e85, 0x674f9842, 0x7e54a903, 0x5579fac0, 0x4c62cb81,
3949
        0x8138c51f, 0x9823f45e, 0xb30ea79d, 0xaa1596dc, 0xe554001b, 0xfc4f315a, 0xd7626299, 0xce7953d8,
3950
        0x49e14f17, 0x50fa7e56, 0x7bd72d95, 0x62cc1cd4, 0x2d8d8a13, 0x3496bb52, 0x1fbbe891, 0x06a0d9d0,
3951
        0x5e7ef3ec, 0x4765c2ad, 0x6c48916e, 0x7553a02f, 0x3a1236e8, 0x230907a9, 0x0824546a, 0x113f652b,
3952
        0x96a779e4, 0x8fbc48a5, 0xa4911b66, 0xbd8a2a27, 0xf2cbbce0, 0xebd08da1, 0xc0fdde62, 0xd9e6ef23,
3953
        0x14bce1bd, 0x0da7d0fc, 0x268a833f, 0x3f91b27e, 0x70d024b9, 0x69cb15f8, 0x42e6463b, 0x5bfd777a,
3954
        0xdc656bb5, 0xc57e5af4, 0xee530937, 0xf7483876, 0xb809aeb1, 0xa1129ff0, 0x8a3fcc33, 0x9324fd72,
3955
    },
3956
    {
3957
        0x00000000, 0x01c26a37, 0x0384d46e, 0x0246be59, 0x0709a8dc, 0x06cbc2eb, 0x048d7cb2, 0x054f1685,
3958
        0x0e1351b8, 0x0fd13b8f, 0x0d9785d6, 0x0c55efe1, 0x091af964, 0x08d89353, 0x0a9e2d0a, 0x0b5c473d,
3959
        0x1c26a370, 0x1de4c947, 0x1fa2771e, 0x1e601d29, 0x1b2f0bac, 0x1aed619b, 0x18abdfc2, 0x1969b5f5,
3960
        0x1235f2c8, 0x13f798ff, 0x11b126a6, 0x10734c91, 0x153c5a14, 0x14fe3023, 0x16b88e7a, 0x177ae44d,
3961
        0x384d46e0, 0x398f2cd7, 0x3bc9928e, 0x3a0bf8b9, 0x3f44ee3c, 0x3e86840b, 0x3cc03a52, 0x3d025065,
3962
        0x365e1758, 0x379c7d6f, 0x35dac336, 0x3418a901, 0x3157bf84, 0x3095d5b3, 0x32d36bea, 0x331101dd,
3963
        0x246be590, 0x25a98fa7, 0x27ef31fe, 0x262d5bc9, 0x23624d4c, 0x22a0277b, 0x20e69922, 0x2124f315,
3964
        0x2a78b428, 0x2bbade1f, 0x29fc6046, 0x283e0a71, 0x2d711cf4, 0x2cb376c3, 0x2ef5c89a, 0x2f37a2ad,
3965
        0x709a8dc0, 0x7158e7f7, 0x731e59ae, 0x72dc3399, 0x7793251c, 0x76514f2b, 0x7417f172, 0x75d59b45,
3966
        0x7e89dc78, 0x7f4bb64f, 0x7d0d0816, 0x7ccf6221, 0x798074a4, 0x78421e93, 0x7a04a0ca, 0x7bc6cafd,
3967
        0x6cbc2eb0, 0x6d7e4487, 0x6f38fade, 0x6efa90e9, 0x6bb5866c, 0x6a77ec5b, 0x68315202, 0x69f33835,
3968
        0x62af7f08, 0x636d153f, 0x612bab66, 0x60e9c151, 0x65a6d7d4, 0x6464bde3, 0x662203ba, 0x67e0698d,
3969
        0x48d7cb20, 0x4915a117, 0x4b531f4e, 0x4a917579, 0x4fde63fc, 0x4e1c09cb, 0x4c5ab792, 0x4d98dda5,
3970
        0x46c49a98, 0x4706f0af, 0x45404ef6, 0x448224c1, 0x41cd3244, 0x400f5873, 0x4249e62a, 0x438b8c1d,
3971
        0x54f16850, 0x55330267, 0x5775bc3e, 0x56b7d609, 0x53f8c08c, 0x523aaabb, 0x507c14e2, 0x51be7ed5,
3972
        0x5ae239e8, 0x5b2053df, 0x5966ed86, 0x58a487b1, 0x5deb9134, 0x5c29fb03, 0x5e6f455a, 0x5fad2f6d,
3973
        0xe1351b80, 0xe0f771b7, 0xe2b1cfee, 0xe373a5d9, 0xe63cb35c, 0xe7fed96b, 0xe5b86732, 0xe47a0d05,
3974
        0xef264a38, 0xeee4200f, 0xeca29e56, 0xed60f461, 0xe82fe2e4, 0xe9ed88d3, 0xebab368a, 0xea695cbd,
3975
        0xfd13b8f0, 0xfcd1d2c7, 0xfe976c9e, 0xff5506a9, 0xfa1a102c, 0xfbd87a1b, 0xf99ec442, 0xf85cae75,
3976
        0xf300e948, 0xf2c2837f, 0xf0843d26, 0xf1465711, 0xf4094194, 0xf5cb2ba3, 0xf78d95fa, 0xf64fffcd,
3977
        0xd9785d60, 0xd8ba3757, 0xdafc890e, 0xdb3ee339, 0xde71f5bc, 0xdfb39f8b, 0xddf521d2, 0xdc374be5,
3978
        0xd76b0cd8, 0xd6a966ef, 0xd4efd8b6, 0xd52db281, 0xd062a404, 0xd1a0ce33, 0xd3e6706a, 0xd2241a5d,
3979
        0xc55efe10, 0xc49c9427, 0xc6da2a7e, 0xc7184049, 0xc25756cc, 0xc3953cfb, 0xc1d382a2, 0xc011e895,
3980
        0xcb4dafa8, 0xca8fc59f, 0xc8c97bc6, 0xc90b11f1, 0xcc440774, 0xcd866d43, 0xcfc0d31a, 0xce02b92d,
3981
        0x91af9640, 0x906dfc77, 0x922b422e, 0x93e92819, 0x96a63e9c, 0x976454ab, 0x9522eaf2, 0x94e080c5,
3982
        0x9fbcc7f8, 0x9e7eadcf, 0x9c381396, 0x9dfa79a1, 0x98b56f24, 0x99770513, 0x9b31bb4a, 0x9af3d17d,
3983
        0x8d893530, 0x8c4b5f07, 0x8e0de15e, 0x8fcf8b69, 0x8a809dec, 0x8b42f7db, 0x89044982, 0x88c623b5,
3984
        0x839a6488, 0x82580ebf, 0x801eb0e6, 0x81dcdad1, 0x8493cc54, 0x8551a663, 0x8717183a, 0x86d5720d,
3985
        0xa9e2d0a0, 0xa820ba97, 0xaa6604ce, 0xaba46ef9, 0xaeeb787c, 0xaf29124b, 0xad6fac12, 0xacadc625,
3986
        0xa7f18118, 0xa633eb2f, 0xa4755576, 0xa5b73f41, 0xa0f829c4, 0xa13a43f3, 0xa37cfdaa, 0xa2be979d,
3987
        0xb5c473d0, 0xb40619e7, 0xb640a7be, 0xb782cd89, 0xb2cddb0c, 0xb30fb13b, 0xb1490f62, 0xb08b6555,
3988
        0xbbd72268, 0xba15485f, 0xb853f606, 0xb9919c31, 0xbcde8ab4, 0xbd1ce083, 0xbf5a5eda, 0xbe9834ed,
3989
    },
3990
    {
3991
        0x00000000, 0xb8bc6765, 0xaa09c88b, 0x12b5afee, 0x8f629757, 0x37def032, 0x256b5fdc, 0x9dd738b9,
3992
        0xc5b428ef, 0x7d084f8a, 0x6fbde064, 0xd7018701, 0x4ad6bfb8, 0xf26ad8dd, 0xe0df7733, 0x58631056,
3993
        0x5019579f, 0xe8a530fa, 0xfa109f14, 0x42acf871, 0xdf7bc0c8, 0x67c7a7ad, 0x75720843, 0xcdce6f26,
3994
        0x95ad7f70, 0x2d111815, 0x3fa4b7fb, 0x8718d09e, 0x1acfe827, 0xa2738f42, 0xb0c620ac, 0x087a47c9,
3995
        0xa032af3e, 0x188ec85b, 0x0a3b67b5, 0xb28700d0, 0x2f503869, 0x97ec5f0c, 0x8559f0e2, 0x3de59787,
3996
        0x658687d1, 0xdd3ae0b4, 0xcf8f4f5a, 0x7733283f, 0xeae41086, 0x525877e3, 0x40edd80d, 0xf851bf68,
3997
        0xf02bf8a1, 0x48979fc4, 0x5a22302a, 0xe29e574f, 0x7f496ff6, 0xc7f50893, 0xd540a77d, 0x6dfcc018,
3998
        0x359fd04e, 0x8d23b72b, 0x9f9618c5, 0x272a7fa0, 0xbafd4719, 0x0241207c, 0x10f48f92, 0xa848e8f7,
3999
        0x9b14583d, 0x23a83f58, 0x311d90b6, 0x89a1f7d3, 0x1476cf6a, 0xaccaa80f, 0xbe7f07e1, 0x06c36084,
4000
        0x5ea070d2, 0xe61c17b7, 0xf4a9b859, 0x4c15df3c, 0xd1c2e785, 0x697e80e0, 0x7bcb2f0e, 0xc377486b,
4001
        0xcb0d0fa2, 0x73b168c7, 0x6104c729, 0xd9b8a04c, 0x446f98f5, 0xfcd3ff90, 0xee66507e, 0x56da371b,
4002
        0x0eb9274d, 0xb6054028, 0xa4b0efc6, 0x1c0c88a3, 0x81dbb01a, 0x3967d77f, 0x2bd27891, 0x936e1ff4,
4003
        0x3b26f703, 0x839a9066, 0x912f3f88, 0x299358ed, 0xb4446054, 0x0cf80731, 0x1e4da8df, 0xa6f1cfba,
4004
        0xfe92dfec, 0x462eb889, 0x549b1767, 0xec277002, 0x71f048bb, 0xc94c2fde, 0xdbf98030, 0x6345e755,
4005
        0x6b3fa09c, 0xd383c7f9, 0xc1366817, 0x798a0f72, 0xe45d37cb, 0x5ce150ae, 0x4e54ff40, 0xf6e89825,
4006
        0xae8b8873, 0x1637ef16, 0x048240f8, 0xbc3e279d, 0x21e91f24, 0x99557841, 0x8be0d7af, 0x335cb0ca,
4007
        0xed59b63b, 0x55e5d15e, 0x47507eb0, 0xffec19d5, 0x623b216c, 0xda874609, 0xc832e9e7, 0x708e8e82,
4008
        0x28ed9ed4, 0x9051f9b1, 0x82e4565f, 0x3a58313a, 0xa78f0983, 0x1f336ee6, 0x0d86c108, 0xb53aa66d,
4009
        0xbd40e1a4, 0x05fc86c1, 0x1749292f, 0xaff54e4a, 0x322276f3, 0x8a9e1196, 0x982bbe78, 0x2097d91d,
4010
        0x78f4c94b, 0xc048ae2e, 0xd2fd01c0, 0x6a4166a5, 0xf7965e1c, 0x4f2a3979, 0x5d9f9697, 0xe523f1f2,
4011
        0x4d6b1905, 0xf5d77e60, 0xe762d18e, 0x5fdeb6eb, 0xc2098e52, 0x7ab5e937, 0x680046d9, 0xd0bc21bc,
4012
        0x88df31ea, 0x3063568f, 0x22d6f961, 0x9a6a9e04, 0x07bda6bd, 0xbf01c1d8, 0xadb46e36, 0x15080953,
4013
        0x1d724e9a, 0xa5ce29ff, 0xb77b8611, 0x0fc7e174, 0x9210d9cd, 0x2aacbea8, 0x38191146, 0x80a57623,
4014
        0xd8c66675, 0x607a0110, 0x72cfaefe, 0xca73c99b, 0x57a4f122, 0xef189647, 0xfdad39a9, 0x45115ecc,
4015
        0x764dee06, 0xcef18963, 0xdc44268d, 0x64f841e8, 0xf92f7951, 0x41931e34, 0x5326b1da, 0xeb9ad6bf,
4016
        0xb3f9c6e9, 0x0b45a18c, 0x19f00e62, 0xa14c6907, 0x3c9b51be, 0x842736db, 0x96929935, 0x2e2efe50,
4017
        0x2654b999, 0x9ee8defc, 0x8c5d7112, 0x34e11677, 0xa9362ece, 0x118a49ab, 0x033fe645, 0xbb838120,
4018
        0xe3e09176, 0x5b5cf613, 0x49e959fd, 0xf1553e98, 0x6c820621, 0xd43e6144, 0xc68bceaa, 0x7e37a9cf,
4019
        0xd67f4138, 0x6ec3265d, 0x7c7689b3, 0xc4caeed6, 0x591dd66f, 0xe1a1b10a, 0xf3141ee4, 0x4ba87981,
4020
        0x13cb69d7, 0xab770eb2, 0xb9c2a15c, 0x017ec639, 0x9ca9fe80, 0x241599e5, 0x36a0360b, 0x8e1c516e,
4021
        0x866616a7, 0x3eda71c2, 0x2c6fde2c, 0x94d3b949, 0x090481f0, 0xb1b8e695, 0xa30d497b, 0x1bb12e1e,
4022
        0x43d23e48, 0xfb6e592d, 0xe9dbf6c3, 0x516791a6, 0xccb0a91f, 0x740cce7a, 0x66b96194, 0xde0506f1,
4023
    }
4024
};
4025
4026
void crc32_init(crc32_ctx* ctx)
4027
{
4028
#if PLATFORM_SUPPORTS_PCLMUL
4029
    if (crc32_supported_x86())
4030
    {
4031
        crc32_init_x86(ctx);
4032
        return;
4033
    }
4034
#endif
4035
    ctx->crc[0] = 0xffffffff;
4036
}
4037
4038
void crc32_update(crc32_ctx* ctx, const void* buffer, size_t size)
4039
{
4040
#if PLATFORM_SUPPORTS_PCLMUL
4041
    if (crc32_supported_x86())
4042
    {
4043
        crc32_update_x86(ctx, buffer, size);
4044
        return;
4045
    }
4046
#endif
4047
4048
    const uint8_t* buffer8 = buffer;
4049
    uint32_t value = ctx->crc[0];
4050
    while (size >= 4)
4051
    {
4052
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
4053
        uint32_t x = get32be(buffer8);
4054
        value = __builtin_bswap32(value) ^ x;
4055
        value = crc32[0][value & 0xff] ^
4056
                crc32[1][(value >> 8) & 0xff] ^
4057
                crc32[2][(value >> 16) & 0xff] ^
4058
                crc32[3][(value >> 24) & 0xff];
4059
#else
4060
        uint32_t x = get32le(buffer8);
4061
        value = value ^ x;
4062
        value = crc32[0][(value >> 24) & 0xff] ^
4063
                crc32[1][(value >> 16) & 0xff] ^
4064
                crc32[2][(value >> 8) & 0xff] ^
4065
                crc32[3][value & 0xff];
4066
#endif
4067
        buffer8 += sizeof(x);
4068
        size -= sizeof(x);
4069
    }
4070
4071
    while (size --> 0)
4072
    {
4073
        value = (value >> 8) ^ crc32[0][(uint8_t)value ^ *buffer8++];
4074
    }
4075
4076
    ctx->crc[0] = value;
4077
}
4078
4079
uint32_t crc32_done(crc32_ctx* ctx)
4080
{
4081
#if PLATFORM_SUPPORTS_PCLMUL
4082
    if (crc32_supported_x86())
4083
    {
4084
        return crc32_done_x86(ctx);
4085
    }
4086
#endif
4087
4088
    return ~ctx->crc[0];
4089
}
4090
4091
static uint32_t crc32_gfmulv(const uint32_t* m, uint32_t v)
4092
{
4093
    uint32_t r = 0;
4094
    while (v)
4095
    {
4096
        if (v & 1)
4097
        {
4098
            r ^= *m;
4099
        }
4100
        v >>= 1;
4101
        m++;
4102
    }
4103
    return r;
4104
}
4105
4106
static void crc32_gfsq(const uint32_t* m, uint32_t* r)
4107
{
4108
    for (size_t i = 0; i < 32; i++)
4109
    {
4110
        r[i] = crc32_gfmulv(m, m[i]);
4111
    }
4112
}
4113
4114
uint32_t crc32_combine(uint32_t a, uint32_t b, uint32_t blen)
4115
{
4116
    uint32_t m1[32];
4117
    uint32_t m2[32];
4118
4119
    m1[0] = 0xedb88320;
4120
    for (uint32_t i = 1; i < 32; i++)
4121
    {
4122
        m1[i] = 1 << (i - 1);
4123
    }
4124
4125
    crc32_gfsq(m1, m2);
4126
    crc32_gfsq(m2, m1);
4127
4128
    for (;;)
4129
    {
4130
        crc32_gfsq(m1, m2);
4131
        if (blen & 1)
4132
        {
4133
            a = crc32_gfmulv(m2, a);
4134
        }
4135
        blen >>= 1;
4136
4137
        if (blen == 0)
4138
        {
4139
            break;
4140
        }
4141
4142
        crc32_gfsq(m2, m1);
4143
        if (blen & 1)
4144
        {
4145
            a = crc32_gfmulv(m1, a);
4146
        }
4147
        blen >>= 1;
4148
4149
        if (blen == 0)
4150
        {
4151
            break;
4152
        }
4153
    }
4154
4155
    return a ^ b;
4156
}
4157
0707010000000D000081A4000001ED0000006B000000015EE6501B000001C0000000000000000000000000000000000000003B00000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_crc32.h#pragma once
4158
4159
#include "pkg2zip_utils.h"
4160
4161
typedef struct {
4162
    uint32_t PKG_ALIGN(16) crc[4 * 5];
4163
} crc32_ctx;
4164
4165
void crc32_init(crc32_ctx* ctx);
4166
void crc32_update(crc32_ctx* ctx, const void* buffer, size_t size);
4167
uint32_t crc32_done(crc32_ctx* ctx);
4168
4169
// returns crc32(x||y)
4170
// where a=crc32(x) and b=crc32(y)
4171
// crc32(x||y) = crc32(x||z) ^ crc32(y), where z=00...00 (same length as y)
4172
uint32_t crc32_combine(uint32_t a, uint32_t b, uint32_t blen);
4173
0707010000000E000081A4000001ED0000006B000000015EE6501B0000340D000000000000000000000000000000000000003F00000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_crc32_x86.c#include "pkg2zip_crc32.h"
4174
4175
#include <wmmintrin.h> // PCLMUL
4176
#include <tmmintrin.h> // SSSE3
4177
#include <smmintrin.h> // SSS4
4178
4179
// Whitepaper: https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/fast-crc-computation-generic-polynomials-pclmulqdq-paper.pdf
4180
// ZLIB licensed code from https://github.com/jtkukunas/zlib/blob/master/crc_folding.c
4181
4182
static const uint32_t PKG_ALIGN(16) shift_table[] = {
4183
    0x84838281, 0x88878685, 0x8c8b8a89, 0x008f8e8d,
4184
    0x85848382, 0x89888786, 0x8d8c8b8a, 0x01008f8e,
4185
    0x86858483, 0x8a898887, 0x8e8d8c8b, 0x0201008f,
4186
    0x87868584, 0x8b8a8988, 0x8f8e8d8c, 0x03020100,
4187
    0x88878685, 0x8c8b8a89, 0x008f8e8d, 0x04030201,
4188
    0x89888786, 0x8d8c8b8a, 0x01008f8e, 0x05040302,
4189
    0x8a898887, 0x8e8d8c8b, 0x0201008f, 0x06050403,
4190
    0x8b8a8988, 0x8f8e8d8c, 0x03020100, 0x07060504,
4191
    0x8c8b8a89, 0x008f8e8d, 0x04030201, 0x08070605,
4192
    0x8d8c8b8a, 0x01008f8e, 0x05040302, 0x09080706,
4193
    0x8e8d8c8b, 0x0201008f, 0x06050403, 0x0a090807,
4194
    0x8f8e8d8c, 0x03020100, 0x07060504, 0x0b0a0908,
4195
    0x008f8e8d, 0x04030201, 0x08070605, 0x0c0b0a09,
4196
    0x01008f8e, 0x05040302, 0x09080706, 0x0d0c0b0a,
4197
    0x0201008f, 0x06050403, 0x0a090807, 0x0e0d0c0b,
4198
};
4199
4200
#define FOLD1(xmm0, xmm1, xmm2, xmm3) do         \
4201
{                                                \
4202
    const __m128i fold4 = _mm_set_epi32(         \
4203
        0x00000001, 0x54442bd4,                  \
4204
        0x00000001, 0xc6e41596);                 \
4205
                                                 \
4206
    __m128i r0, r1, r2, r3, a, b;                \
4207
                                                 \
4208
    r0 = xmm1;                                   \
4209
    r1 = xmm2;                                   \
4210
    r2 = xmm3;                                   \
4211
                                                 \
4212
    a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
4213
    b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
4214
    r3 = _mm_xor_si128(a, b);                    \
4215
                                                 \
4216
    xmm0 = r0;                                   \
4217
    xmm1 = r1;                                   \
4218
    xmm2 = r2;                                   \
4219
    xmm3 = r3;                                   \
4220
} while (0)
4221
4222
#define FOLD2(xmm0, xmm1, xmm2, xmm3) do         \
4223
{                                                \
4224
    const __m128i fold4 = _mm_set_epi32(         \
4225
        0x00000001, 0x54442bd4,                  \
4226
        0x00000001, 0xc6e41596);                 \
4227
                                                 \
4228
    __m128i r0, r1, r2, r3, a, b;                \
4229
                                                 \
4230
    r0 = xmm2;                                   \
4231
    r1 = xmm3;                                   \
4232
                                                 \
4233
    a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
4234
    b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
4235
    r2 = _mm_xor_si128(a, b);                    \
4236
                                                 \
4237
    a = _mm_clmulepi64_si128(xmm1, fold4, 0x01); \
4238
    b = _mm_clmulepi64_si128(xmm1, fold4, 0x10); \
4239
    r3 = _mm_xor_si128(a, b);                    \
4240
                                                 \
4241
    xmm0 = r0;                                   \
4242
    xmm1 = r1;                                   \
4243
    xmm2 = r2;                                   \
4244
    xmm3 = r3;                                   \
4245
} while (0)
4246
4247
#define FOLD3(xmm0, xmm1, xmm2, xmm3) do         \
4248
{                                                \
4249
    const __m128i fold4 = _mm_set_epi32(         \
4250
        0x00000001, 0x54442bd4,                  \
4251
        0x00000001, 0xc6e41596);                 \
4252
                                                 \
4253
    __m128i r0, r1, r2, r3, a, b;                \
4254
                                                 \
4255
    r0 = xmm3;                                   \
4256
                                                 \
4257
    a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
4258
    b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
4259
    r1 = _mm_xor_si128(a, b);                    \
4260
                                                 \
4261
    a = _mm_clmulepi64_si128(xmm1, fold4, 0x01); \
4262
    b = _mm_clmulepi64_si128(xmm1, fold4, 0x10); \
4263
    r2 = _mm_xor_si128(a, b);                    \
4264
                                                 \
4265
    a = _mm_clmulepi64_si128(xmm2, fold4, 0x01); \
4266
    b = _mm_clmulepi64_si128(xmm2, fold4, 0x10); \
4267
    r3 = _mm_xor_si128(a, b);                    \
4268
                                                 \
4269
    xmm0 = r0;                                   \
4270
    xmm1 = r1;                                   \
4271
    xmm2 = r2;                                   \
4272
    xmm3 = r3;                                   \
4273
} while (0)
4274
4275
#define FOLD4(xmm0, xmm1, xmm2, xmm3) do         \
4276
{                                                \
4277
    const __m128i fold4 = _mm_set_epi32(         \
4278
        0x00000001, 0x54442bd4,                  \
4279
        0x00000001, 0xc6e41596);                 \
4280
                                                 \
4281
    __m128i a, b;                                \
4282
                                                 \
4283
    a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
4284
    b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
4285
    xmm0 = _mm_xor_si128(a, b);                  \
4286
                                                 \
4287
    a = _mm_clmulepi64_si128(xmm1, fold4, 0x01); \
4288
    b = _mm_clmulepi64_si128(xmm1, fold4, 0x10); \
4289
    xmm1 = _mm_xor_si128(a, b);                  \
4290
                                                 \
4291
    a = _mm_clmulepi64_si128(xmm2, fold4, 0x01); \
4292
    b = _mm_clmulepi64_si128(xmm2, fold4, 0x10); \
4293
    xmm2 = _mm_xor_si128(a, b);                  \
4294
                                                 \
4295
    a = _mm_clmulepi64_si128(xmm3, fold4, 0x01); \
4296
    b = _mm_clmulepi64_si128(xmm3, fold4, 0x10); \
4297
    xmm3 = _mm_xor_si128(a, b);                  \
4298
} while (0)
4299
4300
#define PARTIAL(len, xmm0, xmm1, xmm2, xmm3, xmm4) do                 \
4301
{                                                                     \
4302
    const __m128i fold4 = _mm_set_epi32(                              \
4303
        0x00000001, 0x54442bd4,                                       \
4304
        0x00000001, 0xc6e41596);                                      \
4305
    const __m128i mask = _mm_set1_epi32(0x80808080);                  \
4306
                                                                      \
4307
    __m128i shl = _mm_load_si128((__m128i *)shift_table + (len - 1)); \
4308
    __m128i shr = _mm_xor_si128(shl, mask);                           \
4309
                                                                      \
4310
    __m128i a, b, r;                                                  \
4311
    __m128i tmp = _mm_shuffle_epi8(xmm0, shl);                        \
4312
                                                                      \
4313
    a = _mm_shuffle_epi8(xmm0, shr);                                  \
4314
    b = _mm_shuffle_epi8(xmm1, shl);                                  \
4315
    xmm0 = _mm_or_si128(a, b);                                        \
4316
                                                                      \
4317
    a = _mm_shuffle_epi8(xmm1, shr);                                  \
4318
    b = _mm_shuffle_epi8(xmm2, shl);                                  \
4319
    xmm1 = _mm_or_si128(a, b);                                        \
4320
                                                                      \
4321
    a = _mm_shuffle_epi8(xmm2, shr);                                  \
4322
    b = _mm_shuffle_epi8(xmm3, shl);                                  \
4323
    xmm2 = _mm_or_si128(a, b);                                        \
4324
                                                                      \
4325
    a = _mm_shuffle_epi8(xmm3, shr);                                  \
4326
    b = _mm_shuffle_epi8(xmm4, shl);                                  \
4327
    xmm4 = b;                                                         \
4328
    r = _mm_or_si128(a, b);                                           \
4329
                                                                      \
4330
    a = _mm_clmulepi64_si128(tmp, fold4, 0x10);                       \
4331
    b = _mm_clmulepi64_si128(tmp, fold4, 0x01);                       \
4332
                                                                      \
4333
    r = _mm_xor_si128(r, a);                                          \
4334
    r = _mm_xor_si128(r, b);                                          \
4335
    xmm3 = r;                                                         \
4336
} while(0)
4337
4338
void crc32_init_x86(crc32_ctx* ctx)
4339
{
4340
    __m128i init = _mm_cvtsi32_si128(0x9db42487);
4341
    __m128i zero = _mm_setzero_si128();
4342
4343
    _mm_store_si128((__m128i*)ctx->crc + 0, init);
4344
    _mm_store_si128((__m128i*)ctx->crc + 1, zero);
4345
    _mm_store_si128((__m128i*)ctx->crc + 2, zero);
4346
    _mm_store_si128((__m128i*)ctx->crc + 3, zero);
4347
}
4348
4349
void crc32_update_x86(crc32_ctx* ctx, const void* buffer, size_t size)
4350
{
4351
    const uint8_t* buffer8 = buffer;
4352
4353
    __m128i xmm0 = _mm_load_si128((__m128i*)ctx->crc + 0);
4354
    __m128i xmm1 = _mm_load_si128((__m128i*)ctx->crc + 1);
4355
    __m128i xmm2 = _mm_load_si128((__m128i*)ctx->crc + 2);
4356
    __m128i xmm3 = _mm_load_si128((__m128i*)ctx->crc + 3);
4357
    __m128i xmm4 = _mm_load_si128((__m128i*)ctx->crc + 4);
4358
4359
    if (size < 16)
4360
    {
4361
        if (size == 0)
4362
        {
4363
            return;
4364
        }
4365
        xmm4 = _mm_loadu_si128((__m128i *)buffer8);
4366
        goto partial;
4367
    }
4368
4369
    uint32_t prefix = (0 - (uintptr_t)buffer8) & 0xF;
4370
    if (prefix != 0)
4371
    {
4372
        xmm4 = _mm_loadu_si128((__m128i *)buffer8);
4373
        buffer8 += prefix;
4374
        size -= prefix;
4375
4376
        PARTIAL(prefix, xmm0, xmm1, xmm2, xmm3, xmm4);
4377
    }
4378
4379
    while (size >= 64)
4380
    {
4381
        __m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
4382
        __m128i t1 = _mm_load_si128((__m128i *)buffer8 + 1);
4383
        __m128i t2 = _mm_load_si128((__m128i *)buffer8 + 2);
4384
        __m128i t3 = _mm_load_si128((__m128i *)buffer8 + 3);
4385
4386
        FOLD4(xmm0, xmm1, xmm2, xmm3);
4387
4388
        xmm0 = _mm_xor_si128(xmm0, t0);
4389
        xmm1 = _mm_xor_si128(xmm1, t1);
4390
        xmm2 = _mm_xor_si128(xmm2, t2);
4391
        xmm3 = _mm_xor_si128(xmm3, t3);
4392
4393
        buffer8 += 64;
4394
        size -= 64;
4395
    }
4396
4397
    if (size >= 48)
4398
    {
4399
        __m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
4400
        __m128i t1 = _mm_load_si128((__m128i *)buffer8 + 1);
4401
        __m128i t2 = _mm_load_si128((__m128i *)buffer8 + 2);
4402
4403
        FOLD3(xmm0, xmm1, xmm2, xmm3);
4404
4405
        xmm1 = _mm_xor_si128(xmm1, t0);
4406
        xmm2 = _mm_xor_si128(xmm2, t1);
4407
        xmm3 = _mm_xor_si128(xmm3, t2);
4408
4409
        buffer8 += 48;
4410
        size -= 48;
4411
    }
4412
    else if (size >= 32)
4413
    {
4414
        __m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
4415
        __m128i t1 = _mm_load_si128((__m128i *)buffer8 + 1);
4416
4417
        FOLD2(xmm0, xmm1, xmm2, xmm3);
4418
4419
        xmm2 = _mm_xor_si128(xmm2, t0);
4420
        xmm3 = _mm_xor_si128(xmm3, t1);
4421
4422
        buffer8 += 32;
4423
        size -= 32;
4424
    }
4425
    else if (size >= 16)
4426
    {
4427
        __m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
4428
4429
        FOLD1(xmm0, xmm1, xmm2, xmm3);
4430
4431
        xmm3 = _mm_xor_si128(xmm3, t0);
4432
4433
        buffer8 += 16;
4434
        size -= 16;
4435
    }
4436
4437
    if (size == 0)
4438
    {
4439
        goto done;
4440
    }
4441
4442
    xmm4 = _mm_load_si128((__m128i *)buffer8);
4443
4444
partial:
4445
    PARTIAL(size, xmm0, xmm1, xmm2, xmm3, xmm4);
4446
4447
done:
4448
    _mm_store_si128((__m128i*)ctx->crc + 0, xmm0);
4449
    _mm_store_si128((__m128i*)ctx->crc + 1, xmm1);
4450
    _mm_store_si128((__m128i*)ctx->crc + 2, xmm2);
4451
    _mm_store_si128((__m128i*)ctx->crc + 3, xmm3);
4452
    _mm_store_si128((__m128i*)ctx->crc + 4, xmm4);
4453
}
4454
4455
uint32_t crc32_done_x86(crc32_ctx* ctx)
4456
{
4457
    const __m128i mask1 = _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000);
4458
    const __m128i mask2 = _mm_setr_epi32(0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
4459
4460
    __m128i xmm0 = _mm_load_si128((__m128i*)ctx->crc + 0);
4461
    __m128i xmm1 = _mm_load_si128((__m128i*)ctx->crc + 1);
4462
    __m128i xmm2 = _mm_load_si128((__m128i*)ctx->crc + 2);
4463
    __m128i xmm3 = _mm_load_si128((__m128i*)ctx->crc + 3);
4464
4465
    __m128i fold;
4466
    __m128i a, b, t;
4467
4468
    fold = _mm_setr_epi32(0xccaa009e, 0x00000000, 0x751997d0, 0x00000001);
4469
4470
    a = _mm_clmulepi64_si128(xmm0, fold, 0x10);
4471
    b = _mm_clmulepi64_si128(xmm0, fold, 0x01);
4472
    t = _mm_xor_si128(xmm1, a);
4473
    t = _mm_xor_si128(t, b);
4474
4475
    a = _mm_clmulepi64_si128(t, fold, 0x10);
4476
    b = _mm_clmulepi64_si128(t, fold, 0x01);
4477
    t = _mm_xor_si128(xmm2, a);
4478
    t = _mm_xor_si128(t, b);
4479
4480
    a = _mm_clmulepi64_si128(t, fold, 0x10);
4481
    b = _mm_clmulepi64_si128(t, fold, 0x01);
4482
    t = _mm_xor_si128(xmm3, a);
4483
    t = _mm_xor_si128(t, b);
4484
4485
    fold = _mm_setr_epi32(0xccaa009e, 0x00000000, 0x63cd6124, 0x00000001);
4486
4487
    a = _mm_clmulepi64_si128(t, fold, 0);
4488
    b = _mm_srli_si128(t, 8);
4489
    a = _mm_xor_si128(a, b);
4490
4491
    b = _mm_slli_si128(a, 4);
4492
    b = _mm_clmulepi64_si128(b, fold, 0x10);
4493
    t = _mm_xor_si128(a, b);
4494
    t = _mm_and_si128(t, mask2);
4495
4496
    fold = _mm_setr_epi32(0xf7011640, 0x00000001, 0xdb710640, 0x00000001);
4497
4498
    a = _mm_clmulepi64_si128(t, fold, 0);
4499
    a = _mm_xor_si128(a, t);
4500
    a = _mm_and_si128(a, mask1);
4501
4502
    b = _mm_clmulepi64_si128(a, fold, 0x10);
4503
    b = _mm_xor_si128(b, a);
4504
    b = _mm_xor_si128(b, t);
4505
4506
    uint32_t crc = _mm_extract_epi32(b, 2);
4507
    return ~crc;
4508
}
4509
0707010000000F000081A4000001ED0000006B000000015EE6501B000008C8000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_out.c#include "pkg2zip_out.h"
4510
#include "pkg2zip_sys.h"
4511
#include "pkg2zip_zip.h"
4512
#include <stdio.h>
4513
4514
static zip out_zip;
4515
static int out_zipped;
4516
static sys_file out_file;
4517
static uint64_t out_file_offset;
4518
4519
void out_begin(const char* name, int zipped)
4520
{
4521
    if (zipped)
4522
    {
4523
        zip_create(&out_zip, name);
4524
    }
4525
    out_zipped = zipped;
4526
}
4527
4528
void out_end(void)
4529
{
4530
    if (out_zipped)
4531
    {
4532
        zip_close(&out_zip);
4533
    }
4534
}
4535
4536
void out_add_folder(const char* path)
4537
{
4538
    if (out_zipped)
4539
    {
4540
        zip_add_folder(&out_zip, path);
4541
    }
4542
    else
4543
    {
4544
        sys_mkdir(path);
4545
    }
4546
}
4547
4548
void out_add_parent(const char* path)
4549
{
4550
    char parent[1024];
4551
    char* lastslash = strrchr(path, '/');
4552
    if (lastslash != NULL)
4553
    {
4554
        snprintf(parent, strlen(path)-strlen(lastslash)+1, "%s", path);
4555
        if (out_zipped)
4556
        {
4557
            zip_add_folder(&out_zip, parent);
4558
        }
4559
        else
4560
        {
4561
            sys_mkdir(parent);
4562
        }
4563
    }
4564
}
4565
4566
uint64_t out_begin_file(const char* name, int compress)
4567
{
4568
    if (out_zipped)
4569
    {
4570
        return zip_begin_file(&out_zip, name, compress);
4571
    }
4572
    else
4573
    {
4574
        out_file = sys_create(name);
4575
        out_file_offset = 0;
4576
        return 0;
4577
    }
4578
}
4579
4580
void out_end_file(void)
4581
{
4582
    if (out_zipped)
4583
    {
4584
        zip_end_file(&out_zip);
4585
    }
4586
    else
4587
    {
4588
        sys_close(out_file);
4589
    }
4590
}
4591
4592
void out_write(const void* buffer, uint32_t size)
4593
{
4594
    if (out_zipped)
4595
    {
4596
        zip_write_file(&out_zip, buffer, size);
4597
    }
4598
    else
4599
    {
4600
        sys_write(out_file, out_file_offset, buffer, size);
4601
        out_file_offset += size;
4602
    }
4603
}
4604
4605
void out_write_at(uint64_t offset, const void* buffer, uint32_t size)
4606
{
4607
    if (out_zipped)
4608
    {
4609
        zip_write_file_at(&out_zip, offset, buffer, size);
4610
    }
4611
    else
4612
    {
4613
        sys_write(out_file, offset, buffer, size);
4614
    }
4615
}
4616
4617
void out_set_offset(uint64_t offset)
4618
{
4619
    if (out_zipped)
4620
    {
4621
        zip_set_offset(&out_zip, offset);
4622
    }
4623
    else
4624
    {
4625
        out_file_offset = offset;
4626
4627
    }
4628
}
4629
4630
uint32_t out_zip_get_crc32(void)
4631
{
4632
    if (out_zipped)
4633
    {
4634
        return zip_get_crc32(&out_zip);
4635
    }
4636
    return 0;
4637
}
4638
4639
void out_zip_set_crc32(uint32_t crc)
4640
{
4641
    if (out_zipped)
4642
    {
4643
        zip_set_crc32(&out_zip, crc);
4644
    }
4645
}
4646
07070100000010000081A4000001ED0000006B000000015EE6501B00000239000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_out.h#pragma once
4647
4648
#include <stdint.h>
4649
4650
void out_begin(const char* name, int zipped);
4651
void out_end(void);
4652
void out_add_folder(const char* path);
4653
void out_add_parent(const char* path);
4654
uint64_t out_begin_file(const char* name, int compress);
4655
void out_end_file(void);
4656
void out_write(const void* buffer, uint32_t size);
4657
4658
// hacky solution to be able to write cso header after the data is written
4659
void out_write_at(uint64_t offset, const void* buffer, uint32_t size);
4660
void out_set_offset(uint64_t offset);
4661
uint32_t out_zip_get_crc32(void);
4662
void out_zip_set_crc32(uint32_t crc);
4663
07070100000011000081A4000001ED0000006B000000015EE6501B000061DE000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_psp.c#include "pkg2zip_psp.h"
4664
#include "pkg2zip_out.h"
4665
#include "pkg2zip_crc32.h"
4666
#include "pkg2zip_utils.h"
4667
#include "miniz_tdef.h"
4668
4669
#include <assert.h>
4670
#include <string.h>
4671
4672
#define ISO_SECTOR_SIZE 2048
4673
4674
#define CSO_HEADER_SIZE 24
4675
4676
// https://vitadevwiki.com/vita/Keys_NonVita#PSPAESKirk4.2F7
4677
static const uint8_t kirk7_key38[] = { 0x12, 0x46, 0x8d, 0x7e, 0x1c, 0x42, 0x20, 0x9b, 0xba, 0x54, 0x26, 0x83, 0x5e, 0xb0, 0x33, 0x03 };
4678
static const uint8_t kirk7_key39[] = { 0xc4, 0x3b, 0xb6, 0xd6, 0x53, 0xee, 0x67, 0x49, 0x3e, 0xa9, 0x5f, 0xbc, 0x0c, 0xed, 0x6f, 0x8a };
4679
static const uint8_t kirk7_key63[] = { 0x9c, 0x9b, 0x13, 0x72, 0xf8, 0xc6, 0x40, 0xcf, 0x1c, 0x62, 0xf5, 0xd5, 0x92, 0xdd, 0xb5, 0x82 };
4680
4681
// https://vitadevwiki.com/vita/Keys_NonVita#PSPAMHashKey
4682
static const uint8_t amctl_hashkey_3[] = { 0xe3, 0x50, 0xed, 0x1d, 0x91, 0x0a, 0x1f, 0xd0, 0x29, 0xbb, 0x1c, 0x3e, 0xf3, 0x40, 0x77, 0xfb };
4683
static const uint8_t amctl_hashkey_4[] = { 0x13, 0x5f, 0xa4, 0x7c, 0xab, 0x39, 0x5b, 0xa4, 0x76, 0xb8, 0xcc, 0xa9, 0x8f, 0x3a, 0x04, 0x45 };
4684
static const uint8_t amctl_hashkey_5[] = { 0x67, 0x8d, 0x7f, 0xa3, 0x2a, 0x9c, 0xa0, 0xd1, 0x50, 0x8a, 0xd8, 0x38, 0x5e, 0x4b, 0x01, 0x7e };
4685
4686
// lzrc decompression code from libkirk by tpu
4687
typedef struct {
4688
    // input stream
4689
    const uint8_t* input;
4690
    uint32_t in_ptr;
4691
    uint32_t in_len;
4692
4693
    // output stream
4694
    uint8_t* output;
4695
    uint32_t out_ptr;
4696
    uint32_t out_len;
4697
4698
    // range decode
4699
    uint32_t range;
4700
    uint32_t code;
4701
    uint32_t out_code;
4702
    uint8_t lc;
4703
4704
    uint8_t bm_literal[8][256];
4705
    uint8_t bm_dist_bits[8][39];
4706
    uint8_t bm_dist[18][8];
4707
    uint8_t bm_match[8][8];
4708
    uint8_t bm_len[8][31];
4709
} lzrc_decode;
4710
4711
static void rc_init(lzrc_decode* rc, void* out, int out_len, const void* in, int in_len)
4712
{
4713
    if (in_len < 5)
4714
    {
4715
        sys_error("ERROR: internal error - lzrc input underflow! pkg may be corrupted?\n");
4716
    }
4717
4718
    rc->input = in;
4719
    rc->in_len = in_len;
4720
    rc->in_ptr = 5;
4721
4722
    rc->output = out;
4723
    rc->out_len = out_len;
4724
    rc->out_ptr = 0;
4725
4726
    rc->range = 0xffffffff;
4727
    rc->lc = rc->input[0];
4728
    rc->code = get32be(rc->input + 1);
4729
    rc->out_code = 0xffffffff;
4730
4731
    memset(rc->bm_literal, 0x80, sizeof(rc->bm_literal));
4732
    memset(rc->bm_dist_bits, 0x80, sizeof(rc->bm_dist_bits));
4733
    memset(rc->bm_dist, 0x80, sizeof(rc->bm_dist));
4734
    memset(rc->bm_match, 0x80, sizeof(rc->bm_match));
4735
    memset(rc->bm_len, 0x80, sizeof(rc->bm_len));
4736
}
4737
4738
static void normalize(lzrc_decode* rc)
4739
{
4740
    if (rc->range < 0x01000000)
4741
    {
4742
        rc->range <<= 8;
4743
        rc->code = (rc->code << 8) + rc->input[rc->in_ptr];
4744
        rc->in_ptr++;
4745
    }
4746
}
4747
4748
static int rc_bit(lzrc_decode* rc, uint8_t *prob)
4749
{
4750
    uint32_t bound;
4751
4752
    normalize(rc);
4753
4754
    bound = (rc->range >> 8) * (*prob);
4755
    *prob -= *prob >> 3;
4756
4757
    if (rc->code < bound)
4758
    {
4759
        rc->range = bound;
4760
        *prob += 31;
4761
        return 1;
4762
    }
4763
    else
4764
    {
4765
        rc->code -= bound;
4766
        rc->range -= bound;
4767
        return 0;
4768
    }
4769
}
4770
4771
static int rc_bittree(lzrc_decode* rc, uint8_t *probs, int limit)
4772
{
4773
    int number = 1;
4774
4775
    do
4776
    {
4777
        number = (number << 1) + rc_bit(rc, probs + number);
4778
    }
4779
    while (number < limit);
4780
4781
    return number;
4782
}
4783
4784
static int rc_number(lzrc_decode* rc, uint8_t *prob, uint32_t n)
4785
{
4786
    int number = 1;
4787
4788
    if (n > 3)
4789
    {
4790
        number = (number << 1) + rc_bit(rc, prob + 3);
4791
        if (n > 4)
4792
        {
4793
            number = (number << 1) + rc_bit(rc, prob + 3);
4794
            if (n > 5)
4795
            {
4796
                // direct bits
4797
                normalize(rc);
4798
4799
                for (uint32_t i = 0; i < n - 5; i++)
4800
                {
4801
                    rc->range >>= 1;
4802
                    number <<= 1;
4803
                    if (rc->code < rc->range)
4804
                    {
4805
                        number += 1;
4806
                    }
4807
                    else
4808
                    {
4809
                        rc->code -= rc->range;
4810
                    }
4811
                }
4812
            }
4813
        }
4814
    }
4815
4816
    if (n > 0)
4817
    {
4818
        number = (number << 1) + rc_bit(rc, prob);
4819
        if (n > 1)
4820
        {
4821
            number = (number << 1) + rc_bit(rc, prob + 1);
4822
            if (n > 2)
4823
            {
4824
                number = (number << 1) + rc_bit(rc, prob + 2);
4825
            }
4826
        }
4827
    }
4828
4829
    return number;
4830
}
4831
4832
static int lzrc_decompress(void* out, int out_len, const void* in, int in_len)
4833
{
4834
    lzrc_decode rc;
4835
    rc_init(&rc, out, out_len, in, in_len);
4836
4837
    if (rc.lc & 0x80)
4838
    {
4839
        // plain text
4840
        memcpy(rc.output, rc.input + 5, rc.code);
4841
        return rc.code;
4842
    }
4843
4844
    int rc_state = 0;
4845
    uint8_t last_byte = 0;
4846
4847
    for (;;)
4848
    {
4849
        uint32_t match_step = 0;
4850
4851
        int bit = rc_bit(&rc, &rc.bm_match[rc_state][match_step]);
4852
        if (bit == 0) // literal
4853
        {
4854
            if (rc_state > 0)
4855
            {
4856
                rc_state -= 1;
4857
            }
4858
4859
            int byte = rc_bittree(&rc, &rc.bm_literal[((last_byte >> rc.lc) & 0x07)][0], 0x100);
4860
            byte -= 0x100;
4861
4862
            if (rc.out_ptr == rc.out_len)
4863
            {
4864
                sys_error("ERROR: internal error - lzrc output overflow! pkg may be corrupted?\n");
4865
            }
4866
            rc.output[rc.out_ptr++] = (uint8_t)byte;
4867
            last_byte = (uint8_t)byte;
4868
        }
4869
        else // match
4870
        {
4871
            // find bits of match length
4872
            uint32_t len_bits = 0;
4873
            for (int i = 0; i < 7; i++)
4874
            {
4875
                match_step += 1;
4876
                bit = rc_bit(&rc, &rc.bm_match[rc_state][match_step]);
4877
                if (bit == 0)
4878
                {
4879
                    break;
4880
                }
4881
                len_bits += 1;
4882
            }
4883
4884
            // find match length
4885
            uint32_t match_len;
4886
            if (len_bits == 0)
4887
            {
4888
                match_len = 1;
4889
            }
4890
            else
4891
            {
4892
                uint32_t len_state = ((len_bits - 1) << 2) + ((rc.out_ptr << (len_bits - 1)) & 0x03);
4893
                match_len = rc_number(&rc, &rc.bm_len[rc_state][len_state], len_bits);
4894
                if (match_len == 0xFF)
4895
                {
4896
                    // end of stream
4897
                    return rc.out_ptr;
4898
                }
4899
            }
4900
4901
            // find number of bits of match distance
4902
            uint32_t dist_state = 0;
4903
            uint32_t limit = 8;
4904
            if (match_len > 2)
4905
            {
4906
                dist_state += 7;
4907
                limit = 44;
4908
            }
4909
            int dist_bits = rc_bittree(&rc, &rc.bm_dist_bits[len_bits][dist_state], limit);
4910
            dist_bits -= limit;
4911
4912
            // find match distance
4913
            uint32_t match_dist;
4914
            if (dist_bits > 0)
4915
            {
4916
                match_dist = rc_number(&rc, &rc.bm_dist[dist_bits][0], dist_bits);
4917
            }
4918
            else
4919
            {
4920
                match_dist = 1;
4921
            }
4922
4923
            // copy match bytes
4924
            if (match_dist > rc.out_ptr)
4925
            {
4926
                sys_error("ERROR: internal error - lzrc match_dist out of range! pkg may be corrupted?\n");
4927
            }
4928
4929
            if (rc.out_ptr + match_len + 1 > rc.out_len)
4930
            {
4931
                sys_error("ERROR: internal error - lzrc output overflow! pkg may be corrupted?\n");
4932
            }
4933
4934
            const uint8_t* match_src = rc.output + rc.out_ptr - match_dist;
4935
            for (uint32_t i = 0; i <= match_len; i++)
4936
            {
4937
                rc.output[rc.out_ptr++] = *match_src++;
4938
            }
4939
            last_byte = match_src[-1];
4940
4941
            rc_state = 6 + ((rc.out_ptr + 1) & 1);
4942
        }
4943
    }
4944
}
4945
4946
static void init_psp_decrypt(aes128_key* key, uint8_t* iv, int eboot, const uint8_t* mac, const uint8_t* header, uint32_t offset1, uint32_t offset2)
4947
{
4948
    uint8_t tmp[16];
4949
    aes128_init_dec(key, kirk7_key63);
4950
    if (eboot)
4951
    {
4952
        aes128_ecb_decrypt(key, header + offset1, tmp);
4953
    }
4954
    else
4955
    {
4956
        memcpy(tmp, header + offset1, 16);
4957
    }
4958
4959
    aes128_key aes;
4960
    aes128_init_dec(&aes, kirk7_key38);
4961
    aes128_ecb_decrypt(&aes, tmp, tmp);
4962
4963
    for (size_t i = 0; i < 16; i++)
4964
    {
4965
        iv[i] = mac[i] ^ tmp[i] ^ header[offset2 + i] ^ amctl_hashkey_3[i] ^ amctl_hashkey_5[i];
4966
    }
4967
    aes128_init_dec(&aes, kirk7_key39);
4968
    aes128_ecb_decrypt(&aes, iv, iv);
4969
4970
    for (size_t i = 0; i < 16; i++)
4971
    {
4972
        iv[i] ^= amctl_hashkey_4[i];
4973
    }
4974
}
4975
4976
static void init_psx_keys(uint8_t* iv, const uint8_t* mac, const uint8_t* header, uint32_t offset1)
4977
{
4978
    uint8_t tmp[16];
4979
    memcpy(tmp, header + offset1, 16);
4980
4981
    aes128_key aes;
4982
    aes128_init_dec(&aes, kirk7_key38);
4983
    aes128_ecb_decrypt(&aes, tmp, tmp);
4984
4985
    for (size_t i = 0; i < 16; i++)
4986
    {
4987
        iv[i] = mac[i] ^ tmp[i] ^ amctl_hashkey_3[i];
4988
    }
4989
4990
}
4991
void unpack_psp_eboot(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size, int cso)
4992
{
4993
    if (item_size < 0x28)
4994
    {
4995
        sys_error("ERROR: eboot.pbp file is to short!\n");
4996
    }
4997
4998
    uint8_t eboot_header[0x28];
4999
    sys_read(pkg, enc_offset + item_offset, eboot_header, sizeof(eboot_header));
5000
    aes128_ctr_xor(pkg_key, pkg_iv, item_offset / 16, eboot_header, sizeof(eboot_header));
5001
5002
    if (memcmp(eboot_header, "\x00PBP", 4) != 0)
5003
    {
5004
        sys_error("ERROR: wrong eboot.pbp header signature!\n");
5005
    }
5006
5007
    uint32_t psar_offset = get32le(eboot_header + 0x24);
5008
    if (psar_offset + 256 > item_size)
5009
    {
5010
        sys_error("ERROR: eboot.pbp file is to short!\n");
5011
    }
5012
    assert(psar_offset % 16 == 0);
5013
5014
    uint8_t psar_header[256];
5015
    sys_read(pkg, enc_offset + item_offset + psar_offset, psar_header, sizeof(psar_header));
5016
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + psar_offset) / 16, psar_header, sizeof(psar_header));
5017
5018
    if (memcmp(psar_header, "NPUMDIMG", 8) != 0)
5019
    {
5020
        sys_error("ERROR: wrong data.psar header signature!\n");
5021
    }
5022
5023
    uint32_t iso_block = get32le(psar_header + 0x0c);
5024
    if (iso_block > 16)
5025
    {
5026
        sys_error("ERROR: unsupported data.psar block size %u, max %u supported!\b", iso_block, 16);
5027
    }
5028
5029
    uint8_t mac[16];
5030
    aes128_cmac(kirk7_key38, psar_header, 0xc0, mac);
5031
5032
    aes128_key psp_key;
5033
    uint8_t psp_iv[16];
5034
    init_psp_decrypt(&psp_key, psp_iv, 1, mac, psar_header, 0xc0, 0xa0);
5035
    aes128_psp_decrypt(&psp_key, psp_iv, 0, psar_header + 0x40, 0x60);
5036
5037
    uint32_t iso_start = get32le(psar_header + 0x54);
5038
    uint32_t iso_end = get32le(psar_header + 0x64);
5039
    uint32_t iso_total = iso_end - iso_start - 1;
5040
    uint32_t block_count = (iso_total + iso_block - 1) / iso_block;
5041
5042
    uint32_t iso_table = get32le(psar_header + 0x6c);
5043
5044
    if (iso_table + block_count * 32 > item_size)
5045
    {
5046
        sys_error("ERROR: offset table in data.psar file is too large!\n");
5047
    }
5048
5049
    mz_uint cso_compress_flags = 0;
5050
    uint32_t cso_index = 0;
5051
    uint32_t cso_offset = 0;
5052
    uint64_t cso_size = 0;
5053
    uint32_t* cso_block = NULL;
5054
    uint32_t initial_size = 0;
5055
5056
    uint64_t file_offset = out_begin_file(path, !cso);
5057
    if (cso)
5058
    {
5059
        cso_size = block_count * iso_block * ISO_SECTOR_SIZE;
5060
        cso_compress_flags = tdefl_create_comp_flags_from_zip_params(cso, -MZ_DEFAULT_WINDOW_BITS, MZ_FIXED);
5061
5062
        uint32_t cso_block_count = (uint32_t)(1 + (cso_size + ISO_SECTOR_SIZE - 1) / ISO_SECTOR_SIZE);
5063
        cso_block = sys_realloc(NULL, cso_block_count * sizeof(uint32_t));
5064
5065
        initial_size = CSO_HEADER_SIZE + cso_block_count * sizeof(uint32_t);
5066
        out_set_offset(file_offset + initial_size);
5067
5068
        cso_offset = initial_size;
5069
    }
5070
5071
    for (uint32_t i = 0; i < block_count; i++)
5072
    {
5073
        uint64_t table_offset = item_offset + psar_offset + iso_table + 32 * i;
5074
5075
        uint8_t table[32];
5076
        sys_read(pkg, enc_offset + table_offset, table, sizeof(table));
5077
        aes128_ctr_xor(pkg_key, pkg_iv, table_offset / 16, table, sizeof(table));
5078
5079
        uint32_t t[8];
5080
        for (size_t k = 0; k < 8; k++)
5081
        {
5082
            t[k] = get32le(table + k * 4);
5083
        }
5084
5085
        uint32_t block_offset = t[4] ^ t[2] ^ t[3];
5086
        uint32_t block_size = t[5] ^ t[1] ^ t[2];
5087
        uint32_t block_flags = t[6] ^ t[0] ^ t[3];
5088
5089
        if (psar_offset + block_size > item_size)
5090
        {
5091
            sys_error("ERROR: iso block size/offset is to large!\n");
5092
        }
5093
5094
        uint8_t PKG_ALIGN(16) data[16 * ISO_SECTOR_SIZE];
5095
5096
        uint64_t abs_offset = item_offset + psar_offset + block_offset;
5097
        sys_output_progress(enc_offset + abs_offset);
5098
        sys_read(pkg, enc_offset + abs_offset, data, block_size);
5099
        aes128_ctr_xor(pkg_key, pkg_iv, abs_offset / 16, data, block_size);
5100
5101
        if ((block_flags & 4) == 0)
5102
        {
5103
            aes128_psp_decrypt(&psp_key, psp_iv, block_offset / 16, data, block_size);
5104
        }
5105
5106
        uint32_t out_size;
5107
        if (block_size == iso_block * ISO_SECTOR_SIZE)
5108
        {
5109
            if (cso)
5110
            {
5111
                for (size_t n = 0; n < iso_block * ISO_SECTOR_SIZE; n += ISO_SECTOR_SIZE)
5112
                {
5113
                    cso_block[cso_index] = cso_offset;
5114
5115
                    uint8_t PKG_ALIGN(16) output[ISO_SECTOR_SIZE];
5116
                    size_t insize = ISO_SECTOR_SIZE;
5117
                    size_t outsize = sizeof(output);
5118
5119
                    tdefl_compressor c;
5120
                    tdefl_init(&c, cso_compress_flags);
5121
                    tdefl_status st = tdefl_compress(&c, data + n, &insize, output, &outsize, TDEFL_FINISH);
5122
                    if (st == TDEFL_STATUS_DONE)
5123
                    {
5124
                        out_write(output, (uint32_t)outsize);
5125
                        cso_offset += (uint32_t)outsize;
5126
                    }
5127
                    else
5128
                    {
5129
                        cso_block[cso_index] |= 0x80000000;
5130
                        out_write(data + n, ISO_SECTOR_SIZE);
5131
                        cso_offset += ISO_SECTOR_SIZE;
5132
                    }
5133
                    cso_index++;
5134
                }
5135
            }
5136
            else
5137
            {
5138
                out_write(data, (uint32_t)block_size);
5139
            }
5140
        }
5141
        else
5142
        {
5143
            uint8_t PKG_ALIGN(16) uncompressed[16 * ISO_SECTOR_SIZE];
5144
            out_size = lzrc_decompress(uncompressed, sizeof(uncompressed), data, block_size);
5145
            if (out_size != iso_block * ISO_SECTOR_SIZE)
5146
            {
5147
                sys_error("ERROR: internal error - lzrc decompression failed! pkg may be corrupted?\n");
5148
            }
5149
            if (cso)
5150
            {
5151
                for (size_t n = 0; n < iso_block * ISO_SECTOR_SIZE; n += ISO_SECTOR_SIZE)
5152
                {
5153
                    cso_block[cso_index] = cso_offset;
5154
5155
                    uint8_t output[ISO_SECTOR_SIZE];
5156
                    size_t insize = ISO_SECTOR_SIZE;
5157
                    size_t outsize = sizeof(output);
5158
5159
                    tdefl_compressor c;
5160
                    tdefl_init(&c, cso_compress_flags);
5161
                    tdefl_status st = tdefl_compress(&c, uncompressed + n, &insize, output, &outsize, TDEFL_FINISH);
5162
                    if (st == TDEFL_STATUS_DONE)
5163
                    {
5164
                        out_write(output, (uint32_t)outsize);
5165
                        cso_offset += (uint32_t)outsize;
5166
                    }
5167
                    else
5168
                    {
5169
                        cso_block[cso_index] |= 0x80000000;
5170
                        out_write(uncompressed + n, ISO_SECTOR_SIZE);
5171
                        cso_offset += ISO_SECTOR_SIZE;
5172
                    }
5173
                    cso_index++;
5174
                }
5175
            }
5176
            else
5177
            {
5178
                out_write(uncompressed, (uint32_t)out_size);
5179
            }
5180
        }
5181
    }
5182
5183
    if (cso)
5184
    {
5185
        cso_block[cso_index++] = cso_offset;
5186
5187
        uint8_t cso_header[CSO_HEADER_SIZE] = { 0x43, 0x49, 0x53, 0x4f };
5188
        // header size
5189
        set32le(cso_header + 4, sizeof(cso_header));
5190
        // original size
5191
        set64le(cso_header + 8, cso_size);
5192
        // block size
5193
        set32le(cso_header + 16, ISO_SECTOR_SIZE);
5194
        // version
5195
        cso_header[20] = 1;
5196
5197
        out_write_at(file_offset, cso_header, sizeof(cso_header));
5198
        out_write_at(file_offset + sizeof(cso_header), cso_block, cso_index * sizeof(uint32_t));
5199
5200
        crc32_ctx cheader;
5201
        crc32_init(&cheader);
5202
        crc32_update(&cheader, cso_header, sizeof(cso_header));
5203
        crc32_update(&cheader, cso_block, cso_index * sizeof(uint32_t));
5204
5205
        uint32_t header_crc32 = crc32_done(&cheader);
5206
        uint32_t data_crc32 = out_zip_get_crc32();
5207
        uint32_t data_len = (uint32_t)(cso_offset - initial_size);
5208
5209
        uint32_t crc32 = crc32_combine(header_crc32, data_crc32, data_len);
5210
        out_zip_set_crc32(crc32);
5211
5212
        sys_realloc(cso_block, 0);
5213
    }
5214
5215
    out_end_file();
5216
}
5217
5218
void unpack_psp_key(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size)
5219
{
5220
    if (item_size < 0x90 + 0xa0)
5221
    {
5222
        sys_error("ERROR: PSP-KEY.EDAT file is to short!\n");
5223
    }
5224
5225
    uint8_t key_header[0xa0];
5226
    sys_read(pkg, enc_offset + item_offset + 0x90, key_header, sizeof(key_header));
5227
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + 0x90) / 16, key_header, sizeof(key_header));
5228
5229
    if (memcmp(key_header, "\x00PGD", 4) != 0)
5230
    {
5231
        sys_error("ERROR: wrong PSP-KEY.EDAT header signature!\n");
5232
    }
5233
5234
    uint32_t key_index = get32le(key_header + 4);
5235
    uint32_t drm_type = get32le(key_header + 8);
5236
    if (key_index != 1 || drm_type != 1)
5237
    {
5238
        sys_error("ERROR: unsupported PSP-KEY.EDAT file, key/drm type is wrong!\n");
5239
    }
5240
5241
    uint8_t mac[16];
5242
    aes128_cmac(kirk7_key38, key_header, 0x70, mac);
5243
5244
    aes128_key psp_key;
5245
    uint8_t psp_iv[16];
5246
    init_psp_decrypt(&psp_key, psp_iv, 0, mac, key_header, 0x70, 0x10);
5247
    aes128_psp_decrypt(&psp_key, psp_iv, 0, key_header + 0x30, 0x30);
5248
5249
    uint32_t data_size = get32le(key_header + 0x44);
5250
    uint32_t data_offset = get32le(key_header + 0x4c);
5251
5252
    if (data_size != 0x10 || data_offset != 0x90)
5253
    {
5254
        sys_error("ERROR: unsupported PSP-KEY.EDAT file, data/offset is wrong!\n");
5255
    }
5256
5257
    init_psp_decrypt(&psp_key, psp_iv, 0, mac, key_header, 0x70, 0x30);
5258
    aes128_psp_decrypt(&psp_key, psp_iv, 0, key_header + 0x90, 0x10);
5259
5260
    out_begin_file(path, 0);
5261
    out_write(key_header + 0x90, 0x10);
5262
    out_end_file();
5263
}
5264
5265
void unpack_psp_edat(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size)
5266
{
5267
    if (item_size < 0x90 + 0xa0)
5268
    {
5269
        sys_error("ERROR: EDAT file is to short!\n");
5270
    }
5271
5272
    uint8_t item_header[90];
5273
    sys_read(pkg, enc_offset + item_offset, item_header, sizeof(item_header));
5274
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset) / 16, item_header, sizeof(item_header));
5275
    uint8_t key_header_offset = item_header[0xC];
5276
5277
    uint8_t key_header[0xa0];
5278
    sys_read(pkg, enc_offset + item_offset + key_header_offset, key_header, sizeof(key_header));
5279
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + key_header_offset) / 16, key_header, sizeof(key_header));
5280
5281
    if (memcmp(key_header, "\x00PGD", 4) != 0)
5282
    {
5283
        sys_error("ERROR: wrong EDAT header signature!\n");
5284
    }
5285
5286
    uint32_t key_index = get32le(key_header + 4);
5287
    uint32_t drm_type = get32le(key_header + 8);
5288
5289
    if (key_index != 1 || drm_type != 1)
5290
    {
5291
        sys_error("ERROR: unsupported EDAT file, key/drm type is wrong!\n");
5292
    }
5293
5294
    uint8_t mac[16];
5295
    aes128_cmac(kirk7_key38, key_header, 0x70, mac);
5296
5297
    aes128_key psp_key;
5298
    uint8_t psp_iv[16];
5299
    init_psp_decrypt(&psp_key, psp_iv, 0, mac, key_header, 0x70, 0x10);
5300
    aes128_psp_decrypt(&psp_key, psp_iv, 0, key_header + 0x30, 0x30);
5301
5302
    uint32_t data_size = get32le(key_header + 0x44);
5303
    uint32_t data_offset = get32le(key_header + 0x4c);
5304
5305
    if (data_offset != 0x90)
5306
    {
5307
        sys_error("ERROR: unsupported EDAT file, data offset is wrong!\n");
5308
    }
5309
5310
    init_psp_decrypt(&psp_key, psp_iv, 0, mac, key_header, 0x70, 0x30);
5311
5312
    uint32_t block_size = 0x10;
5313
    uint32_t block_count = ((data_size + (block_size - 1)) / block_size );
5314
5315
    out_begin_file(path, 0);
5316
    for (uint32_t i = 0; i < block_count; i++)
5317
    {
5318
        uint8_t block[0x10];
5319
        uint32_t block_offset = (data_offset + (i * block_size)); 
5320
5321
        sys_read(pkg, enc_offset + item_offset + key_header_offset + block_offset, block, block_size);
5322
        aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + key_header_offset + block_offset)  / 16, block, block_size);
5323
        aes128_psp_decrypt(&psp_key, psp_iv, i * block_size / 16, block, block_size);
5324
5325
        uint32_t out_size = 0x10;
5326
        if ( ((i + 1) * block_size) > data_size )
5327
        {
5328
            out_size = data_size - (i * block_size);
5329
        }
5330
        out_write(block, out_size);
5331
    }
5332
    out_end_file();
5333
}
5334
5335
void unpack_keys_bin(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size)
5336
{
5337
5338
    if (item_size < 0x28 + 0x10 + 0x1f0)
5339
    {
5340
        sys_error("ERROR: EBOOT file is to short!\n");
5341
    }
5342
5343
    uint8_t eboot_header[0x28];
5344
    sys_read(pkg, enc_offset + item_offset, eboot_header, sizeof(eboot_header));
5345
    aes128_ctr_xor(pkg_key, pkg_iv, item_offset / 16, eboot_header, sizeof(eboot_header));
5346
5347
    if (memcmp(eboot_header, "\x00PBP", 4) != 0)
5348
    {
5349
        sys_error("ERROR: wrong eboot header signature!\n");
5350
    }
5351
5352
    uint32_t psar_offset = get32le(eboot_header + 0x24);
5353
5354
    if (psar_offset + 16 > item_size)
5355
    {
5356
        sys_error("ERROR: eboot file is to short!\n");
5357
    }
5358
    assert(psar_offset % 16 == 0);
5359
5360
    uint8_t psar_header[16];
5361
    sys_read(pkg, enc_offset + item_offset + psar_offset, psar_header, sizeof(psar_header));
5362
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + psar_offset) / 16, psar_header, sizeof(psar_header));
5363
5364
    uint32_t key_header_offset = 0;
5365
    if (memcmp(psar_header, "PSTITLE", 7) == 0)
5366
    {
5367
        key_header_offset = 0x1f0;
5368
    }
5369
    else if (memcmp(psar_header, "PSISO", 5) == 0)
5370
    {
5371
        key_header_offset = 0x3f0;
5372
    }
5373
    else 
5374
    {
5375
        sys_error("ERROR: wrong psar header signature!\n");
5376
    }
5377
5378
    uint8_t key_header[0x90];
5379
    sys_read(pkg, enc_offset + item_offset + psar_offset+ 16 + key_header_offset, key_header, sizeof(key_header));
5380
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + psar_offset + 16 + key_header_offset) / 16, key_header, sizeof(key_header));
5381
5382
    if (memcmp(key_header, "\x00PGD", 4) != 0)
5383
    {
5384
        sys_error("ERROR: wrong key header signature!\n");
5385
    }
5386
5387
    uint32_t key_index = get32le(key_header + 4);
5388
    uint32_t drm_type = get32le(key_header + 8);
5389
5390
    if (key_index != 1 || drm_type != 1)
5391
    {
5392
        sys_error("ERROR: unsupported EBOOT file, key/drm type is unsupported!\n");
5393
    }
5394
5395
    uint8_t mac[16];
5396
    aes128_cmac(kirk7_key38, key_header, 0x70, mac);
5397
5398
    uint8_t keys_bin[16];
5399
    init_psx_keys(keys_bin, mac, key_header, 0x70);
5400
5401
    out_begin_file(path, 0);
5402
    out_write(keys_bin, sizeof(keys_bin));
5403
    out_end_file();
5404
}
5405
5406
void get_psp_theme_title(char* title, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset)
5407
{
5408
    uint8_t item_header[90];
5409
    sys_read(pkg, enc_offset + item_offset, item_header, sizeof(item_header));
5410
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset) / 16, item_header, sizeof(item_header));
5411
    uint8_t key_header_offset = item_header[0xC];
5412
5413
    uint8_t key_header[0xa0];
5414
    sys_read(pkg, enc_offset + item_offset + key_header_offset, key_header, sizeof(key_header));
5415
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + key_header_offset) / 16, key_header, sizeof(key_header));
5416
5417
    if (memcmp(key_header, "\x00PGD", 4) != 0)
5418
    {
5419
        sys_error("ERROR: wrong EDAT header signature!\n");
5420
    }
5421
5422
    uint32_t key_index = get32le(key_header + 4);
5423
    uint32_t drm_type = get32le(key_header + 8);
5424
5425
    if (key_index != 1 || drm_type != 1)
5426
    {
5427
        sys_error("ERROR: unsupported EDAT file, key/drm type is wrong!\n");
5428
    }
5429
5430
    uint8_t mac[16];
5431
    aes128_cmac(kirk7_key38, key_header, 0x70, mac);
5432
5433
    aes128_key psp_key;
5434
    uint8_t psp_iv[16];
5435
    init_psp_decrypt(&psp_key, psp_iv, 0, mac, key_header, 0x70, 0x10);
5436
    aes128_psp_decrypt(&psp_key, psp_iv, 0, key_header + 0x30, 0x30);
5437
5438
    uint32_t data_offset = get32le(key_header + 0x4c);
5439
5440
    uint8_t theme_header[256];
5441
    sys_read(pkg, enc_offset + item_offset + key_header_offset + data_offset, theme_header, sizeof(theme_header));
5442
    aes128_ctr_xor(pkg_key, pkg_iv, (item_offset + key_header_offset + data_offset)/ 16, theme_header, sizeof(theme_header));
5443
5444
    init_psp_decrypt(&psp_key, psp_iv, 0, mac, key_header, 0x70, 0x30);
5445
    aes128_psp_decrypt(&psp_key, psp_iv, 0, theme_header, sizeof(theme_header));
5446
5447
    if (memcmp(theme_header, "\x00PTF", 4) != 0)
5448
    {
5449
        sys_error("ERROR: wrong PTF header signature!\n");
5450
    }
5451
5452
    memcpy(title,theme_header+8, 128);
5453
    title[128]=0;
5454
}
5455
07070100000012000081A4000001ED0000006B000000015EE6501B0000036F000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_psp.h#include "pkg2zip_aes.h"
5456
#include "pkg2zip_sys.h"
5457
5458
void unpack_psp_eboot(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size, int cso);
5459
void unpack_psp_key(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size);
5460
void unpack_psp_edat(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size);
5461
void unpack_keys_bin(const char* path, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset, uint64_t item_size);
5462
void get_psp_theme_title(char* title, const aes128_key* pkg_key, const uint8_t* pkg_iv, sys_file* pkg, uint64_t enc_offset, uint64_t item_offset);
5463
07070100000013000081A4000001ED0000006B000000015EE6501B00001DEC000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_sys.c#include "pkg2zip_sys.h"
5464
#include "pkg2zip_utils.h"
5465
5466
#include <stdlib.h>
5467
#include <stdio.h>
5468
#include <string.h>
5469
#include <stdarg.h>
5470
#include <sys/stat.h>
5471
#include <errno.h>
5472
5473
#if defined(_WIN32)
5474
5475
#define WIN32_LEAN_AND_MEAN
5476
#include <windows.h>
5477
5478
static HANDLE gStdout;
5479
static int gStdoutRedirected;
5480
static UINT gOldCP;
5481
5482
void sys_output_init(void)
5483
{
5484
    gOldCP = GetConsoleOutputCP();
5485
    SetConsoleOutputCP(CP_UTF8);
5486
    gStdout = GetStdHandle(STD_OUTPUT_HANDLE);
5487
5488
    DWORD mode;
5489
    gStdoutRedirected = !GetConsoleMode(gStdout, &mode);
5490
}
5491
5492
void sys_output_done(void)
5493
{
5494
    SetConsoleOutputCP(gOldCP);
5495
}
5496
5497
void sys_output(const char* msg, ...)
5498
{
5499
    char buffer[1024];
5500
5501
    va_list arg;
5502
    va_start(arg, msg);
5503
    vsnprintf(buffer, sizeof(buffer), msg, arg);
5504
    va_end(arg);
5505
5506
    if (!gStdoutRedirected)
5507
    {
5508
        WCHAR wbuffer[sizeof(buffer)];
5509
        int wcount = MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, sizeof(buffer));
5510
5511
        DWORD written;
5512
        WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wbuffer, wcount - 1, &written, NULL);
5513
        return;
5514
    }
5515
    fputs(buffer, stdout);
5516
}
5517
5518
void sys_error(const char* msg, ...)
5519
{
5520
    char buffer[1024];
5521
5522
    va_list arg;
5523
    va_start(arg, msg);
5524
    vsnprintf(buffer, sizeof(buffer), msg, arg);
5525
    va_end(arg);
5526
5527
    DWORD mode;
5528
    if (GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE), &mode))
5529
    {
5530
        WCHAR wbuffer[sizeof(buffer)];
5531
        int wcount = MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, sizeof(buffer));
5532
5533
        DWORD written;
5534
        WriteConsoleW(GetStdHandle(STD_ERROR_HANDLE), wbuffer, wcount - 1, &written, NULL);
5535
    }
5536
    else
5537
    {
5538
        fputs(buffer, stderr);
5539
    }
5540
5541
    SetConsoleOutputCP(gOldCP);
5542
    exit(EXIT_FAILURE);
5543
}
5544
5545
static void sys_mkdir_real(const char* path)
5546
{
5547
    WCHAR wpath[MAX_PATH];
5548
    MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH);
5549
5550
    if (CreateDirectoryW(wpath, NULL) == 0)
5551
    {
5552
        if (GetLastError() != ERROR_ALREADY_EXISTS)
5553
        {
5554
            sys_error("ERROR: cannot create '%s' folder\n", path);
5555
        }
5556
    }
5557
}
5558
5559
sys_file sys_open(const char* fname, uint64_t* size)
5560
{
5561
    WCHAR path[MAX_PATH];
5562
    MultiByteToWideChar(CP_UTF8, 0, fname, -1, path, MAX_PATH);
5563
5564
    HANDLE handle = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
5565
    if (handle == INVALID_HANDLE_VALUE)
5566
    {
5567
        sys_error("ERROR: cannot open '%s' file\n", fname);
5568
    }
5569
5570
    LARGE_INTEGER sz;
5571
    if (!GetFileSizeEx(handle, &sz))
5572
    {
5573
        sys_error("ERROR: cannot get size of '%s' file\n", fname);
5574
    }
5575
    *size = sz.QuadPart;
5576
5577
    return handle;
5578
}
5579
5580
sys_file sys_create(const char* fname)
5581
{
5582
    WCHAR path[MAX_PATH];
5583
    MultiByteToWideChar(CP_UTF8, 0, fname, -1, path, MAX_PATH);
5584
5585
    HANDLE handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
5586
    if (handle == INVALID_HANDLE_VALUE)
5587
    {
5588
        sys_error("ERROR:1: cannot create '%s' file\n", fname);
5589
    }
5590
5591
    return handle;
5592
}
5593
5594
void sys_close(sys_file file)
5595
{
5596
    if (!CloseHandle(file))
5597
    {
5598
        sys_error("ERROR: failed to close file\n");
5599
    }
5600
}
5601
5602
void sys_read(sys_file file, uint64_t offset, void* buffer, uint32_t size)
5603
{
5604
    DWORD read;
5605
    OVERLAPPED ov;
5606
    ov.hEvent = NULL;
5607
    ov.Offset = (uint32_t)offset;
5608
    ov.OffsetHigh = (uint32_t)(offset >> 32);
5609
    if (!ReadFile(file, buffer, size, &read, &ov) || read != size)
5610
    {
5611
        sys_error("ERROR: failed to read %u bytes from file\n", size);
5612
    }
5613
}
5614
5615
void sys_write(sys_file file, uint64_t offset, const void* buffer, uint32_t size)
5616
{
5617
    DWORD written;
5618
    OVERLAPPED ov;
5619
    ov.hEvent = NULL;
5620
    ov.Offset = (uint32_t)offset;
5621
    ov.OffsetHigh = (uint32_t)(offset >> 32);
5622
    if (!WriteFile(file, buffer, size, &written, &ov) || written != size)
5623
    {
5624
        sys_error("ERROR: failed to write %u bytes to file\n", size);
5625
    }
5626
}
5627
5628
#else
5629
5630
#define _FILE_OFFSET_BITS 64
5631
#include <stdio.h>
5632
#include <fcntl.h>
5633
#include <errno.h>
5634
#include <unistd.h>
5635
#include <sys/stat.h>
5636
5637
static int gStdoutRedirected;
5638
5639
void sys_output_init(void)
5640
{
5641
    gStdoutRedirected = !isatty(STDOUT_FILENO);
5642
}
5643
5644
void sys_output_done(void)
5645
{
5646
}
5647
5648
void sys_output(const char* msg, ...)
5649
{
5650
    va_list arg;
5651
    va_start(arg, msg);
5652
    vfprintf(stdout, msg, arg);
5653
    va_end(arg);
5654
}
5655
5656
void sys_error(const char* msg, ...)
5657
{
5658
    va_list arg;
5659
    va_start(arg, msg);
5660
    vfprintf(stderr, msg, arg);
5661
    va_end(arg);
5662
5663
    exit(EXIT_FAILURE);
5664
}
5665
5666
static void sys_mkdir_real(const char* path)
5667
{
5668
    if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0)
5669
    {
5670
        if (errno != EEXIST)
5671
        {
5672
            sys_error("ERROR: cannot create '%s' folder\n", path);
5673
        }
5674
    }
5675
}
5676
5677
sys_file sys_open(const char* fname, uint64_t* size)
5678
{
5679
    int fd = open(fname, O_RDONLY);
5680
    if (fd < 0)
5681
    {
5682
        sys_error("ERROR: cannot open '%s' file\n", fname);
5683
    }
5684
5685
    struct stat st;
5686
    if (fstat(fd, &st) != 0)
5687
    {
5688
        sys_error("ERROR: cannot get size of '%s' file\n", fname);
5689
    }
5690
    *size = st.st_size;
5691
5692
    return (void*)(intptr_t)fd;
5693
}
5694
5695
sys_file sys_create(const char* fname)
5696
{
5697
    int fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
5698
    if (fd < 0)
5699
    {
5700
        sys_error("ERROR:2: cannot create '%s' file\n", fname);
5701
    }
5702
5703
    return (void*)(intptr_t)fd;
5704
}
5705
5706
void sys_close(sys_file file)
5707
{
5708
    if (close((int)(intptr_t)file) != 0)
5709
    {
5710
        sys_error("ERROR: failed to close file\n");
5711
    }
5712
}
5713
5714
void sys_read(sys_file file, uint64_t offset, void* buffer, uint32_t size)
5715
{
5716
    ssize_t read = pread((int)(intptr_t)file, buffer, size, offset);
5717
    if (read < 0 || read != (ssize_t)size)
5718
    {
5719
        sys_error("ERROR: failed to read %u bytes from file\n", size);
5720
    }
5721
}
5722
5723
void sys_write(sys_file file, uint64_t offset, const void* buffer, uint32_t size)
5724
{
5725
    ssize_t wrote = pwrite((int)(intptr_t)file, buffer, size, offset);
5726
    if (wrote < 0 || wrote != (ssize_t)size)
5727
    {
5728
        sys_error("ERROR: failed to read %u bytes from file\n", size);
5729
    }
5730
}
5731
5732
#endif
5733
5734
void sys_mkdir(const char* path)
5735
{
5736
    char* last = strrchr(path, '/');
5737
    if (last)
5738
    {
5739
        *last = 0;
5740
        sys_mkdir(path);
5741
        *last = '/';
5742
    }
5743
    sys_mkdir_real(path);
5744
}
5745
5746
void* sys_realloc(void* ptr, size_t size)
5747
{
5748
    void* result = NULL;
5749
    if (!ptr && size)
5750
    {
5751
        result = malloc(size);
5752
    }
5753
    else if (ptr && !size)
5754
    {
5755
        free(ptr);
5756
        return NULL;
5757
    }
5758
    else if (ptr && size)
5759
    {
5760
        result = realloc(ptr, size);
5761
    }
5762
    else
5763
    {
5764
        sys_error("ERROR: internal error, wrong sys_realloc usage\n");
5765
    }
5766
5767
    if (!result)
5768
    {
5769
        sys_error("ERROR: out of memory\n");
5770
    }
5771
5772
    return result;
5773
}
5774
5775
void sys_vstrncat(char* dst, size_t n, const char* format, ...)
5776
{
5777
    char temp[1024];
5778
5779
    va_list args;
5780
    va_start(args, format);
5781
    vsnprintf(temp, sizeof(temp), format, args);
5782
    va_end(args);
5783
5784
    strncat(dst, temp, n - strlen(dst) - 1);
5785
}
5786
5787
static uint64_t out_size;
5788
static uint32_t out_next;
5789
5790
void sys_output_progress_init(uint64_t size)
5791
{
5792
    out_size = size;
5793
    out_next = 0;
5794
}
5795
5796
void sys_output_progress(uint64_t progress)
5797
{
5798
    if (gStdoutRedirected)
5799
    {
5800
        return;
5801
    }
5802
5803
    uint32_t now = (uint32_t)(progress * 100 / out_size);
5804
    if (now >= out_next)
5805
    {
5806
        sys_output("[*] unpacking... %u%%\r", now);
5807
        out_next = now + 1;
5808
    }
5809
}
5810
5811
int sys_test_dir(const char* const path)
5812
{
5813
    struct stat info;
5814
5815
    int statRC = stat( path, &info );
5816
    if( statRC != 0 )
5817
    {
5818
        if (errno == ENOENT)  { return 0; } 
5819
        if (errno == ENOTDIR) { return 0; } 
5820
        return -1;
5821
    }
5822
5823
    return ( info.st_mode & S_IFDIR ) ? 1 : 0;
5824
}
5825
07070100000014000081A4000001ED0000006B000000015EE6501B0000037B000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_sys.h#pragma once
5826
5827
#include "pkg2zip_utils.h"
5828
5829
// correctly outputs utf8 string
5830
void sys_output_init(void);
5831
void sys_output_done(void);
5832
void sys_output(const char* msg, ...);
5833
void NORETURN sys_error(const char* msg, ...);
5834
5835
void sys_output_progress_init(uint64_t size);
5836
void sys_output_progress(uint64_t progress);
5837
5838
typedef void* sys_file;
5839
5840
void sys_mkdir(const char* path);
5841
5842
sys_file sys_open(const char* fname, uint64_t* size);
5843
sys_file sys_create(const char* fname);
5844
void sys_close(sys_file file);
5845
void sys_read(sys_file file, uint64_t offset, void* buffer, uint32_t size);
5846
void sys_write(sys_file file, uint64_t offset, const void* buffer, uint32_t size);
5847
5848
// if !ptr && size => malloc
5849
// if ptr && !size => free
5850
// if ptr && size => realloc
5851
void* sys_realloc(void* ptr, size_t size);
5852
5853
void sys_vstrncat(char* dst, size_t n, const char* format, ...);
5854
5855
int sys_test_dir(const char* const path);07070100000015000081A4000001ED0000006B000000015EE6501B00000B4A000000000000000000000000000000000000003B00000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_utils.h#pragma once
5856
5857
#include <stdint.h>
5858
#include <stddef.h>
5859
5860
#if defined(_MSC_VER)
5861
#  define NORETURN __declspec(noreturn)
5862
#  define PKG_ALIGN(x) __declspec(align(x))
5863
#else
5864
#  define NORETURN __attribute__((noreturn))
5865
#  define PKG_ALIGN(x) __attribute__((aligned(x)))
5866
#endif
5867
5868
static inline uint32_t min32(uint32_t a, uint32_t b)
5869
{
5870
    return a < b ? a : b;
5871
}
5872
5873
static inline uint64_t min64(uint64_t a, uint64_t b)
5874
{
5875
    return a < b ? a : b;
5876
}
5877
5878
static inline uint16_t get16le(const uint8_t* bytes)
5879
{
5880
    return (bytes[0]) | (bytes[1] << 8);
5881
}
5882
5883
static inline uint32_t get32le(const uint8_t* bytes)
5884
{
5885
    return (bytes[0]) | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
5886
}
5887
5888
static inline uint64_t get64le(const uint8_t* bytes)
5889
{
5890
    return (uint64_t)bytes[0]
5891
        | ((uint64_t)bytes[1] << 8)
5892
        | ((uint64_t)bytes[2] << 16)
5893
        | ((uint64_t)bytes[3] << 24)
5894
        | ((uint64_t)bytes[4] << 32)
5895
        | ((uint64_t)bytes[5] << 40)
5896
        | ((uint64_t)bytes[6] << 48)
5897
        | ((uint64_t)bytes[7] << 56);
5898
}
5899
5900
static inline uint16_t get16be(const uint8_t* bytes)
5901
{
5902
    return (bytes[1]) | (bytes[0] << 8);
5903
}
5904
5905
static inline uint32_t get32be(const uint8_t* bytes)
5906
{
5907
    return (bytes[3]) | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24);
5908
}
5909
5910
static inline uint64_t get64be(const uint8_t* bytes)
5911
{
5912
    return (uint64_t)bytes[7]
5913
        | ((uint64_t)bytes[6] << 8)
5914
        | ((uint64_t)bytes[5] << 16)
5915
        | ((uint64_t)bytes[4] << 24)
5916
        | ((uint64_t)bytes[3] << 32)
5917
        | ((uint64_t)bytes[2] << 40)
5918
        | ((uint64_t)bytes[1] << 48)
5919
        | ((uint64_t)bytes[0] << 56);
5920
}
5921
5922
static inline void set16le(uint8_t* bytes, uint16_t x)
5923
{
5924
    bytes[0] = (uint8_t)x;
5925
    bytes[1] = (uint8_t)(x >> 8);
5926
}
5927
5928
static inline void set32le(uint8_t* bytes, uint32_t x)
5929
{
5930
    bytes[0] = (uint8_t)x;
5931
    bytes[1] = (uint8_t)(x >> 8);
5932
    bytes[2] = (uint8_t)(x >> 16);
5933
    bytes[3] = (uint8_t)(x >> 24);
5934
}
5935
5936
static inline void set64le(uint8_t* bytes, uint64_t x)
5937
{
5938
    bytes[0] = (uint8_t)x;
5939
    bytes[1] = (uint8_t)(x >> 8);
5940
    bytes[2] = (uint8_t)(x >> 16);
5941
    bytes[3] = (uint8_t)(x >> 24);
5942
    bytes[4] = (uint8_t)(x >> 32);
5943
    bytes[5] = (uint8_t)(x >> 40);
5944
    bytes[6] = (uint8_t)(x >> 48);
5945
    bytes[7] = (uint8_t)(x >> 56);
5946
}
5947
5948
static inline void set16be(uint8_t* bytes, uint16_t x)
5949
{
5950
    bytes[0] = (uint8_t)(x >> 8);
5951
    bytes[1] = (uint8_t)x;
5952
}
5953
5954
static inline void set32be(uint8_t* bytes, uint32_t x)
5955
{
5956
    bytes[0] = (uint8_t)(x >> 24);
5957
    bytes[1] = (uint8_t)(x >> 16);
5958
    bytes[2] = (uint8_t)(x >> 8);
5959
    bytes[3] = (uint8_t)x;
5960
}
5961
5962
static inline void set64be(uint8_t* bytes, uint64_t x)
5963
{
5964
    bytes[0] = (uint8_t)(x >> 56);
5965
    bytes[1] = (uint8_t)(x >> 48);
5966
    bytes[2] = (uint8_t)(x >> 40);
5967
    bytes[3] = (uint8_t)(x >> 32);
5968
    bytes[4] = (uint8_t)(x >> 24);
5969
    bytes[5] = (uint8_t)(x >> 16);
5970
    bytes[6] = (uint8_t)(x >> 8);
5971
    bytes[7] = (uint8_t)x;
5972
}
5973
07070100000016000081A4000001ED0000006B000000015EE6501B00003291000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_zip.c#if defined(__MINGW32__) && !defined(__x86_64__)
5974
#  define _USE_32BIT_TIME_T
5975
#  define __CRT__NO_INLINE
5976
#endif
5977
5978
#include "pkg2zip_zip.h"
5979
#include "pkg2zip_out.h"
5980
#include "pkg2zip_crc32.h"
5981
#include "pkg2zip_utils.h"
5982
5983
#include <string.h>
5984
#include <time.h>
5985
5986
#define ZIP_MEMORY_BLOCK (1024 * 1024)
5987
5988
// https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
5989
5990
#define ZIP_VERSION 45
5991
#define ZIP_METHOD_STORE 0
5992
#define ZIP_METHOD_DEFLATE 8
5993
#define ZIP_UTF8_FLAG (1 << 11)
5994
5995
#define ZIP_DOS_ATTRIBUTE_DIRECTORY 0x10
5996
#define ZIP_DOS_ATTRIBUTE_ARCHIVE   0x20
5997
5998
#define ZIP_LOCAL_HEADER_SIZE 30
5999
#define ZIP_GLOBAL_HEADER_SIZE 46
6000
#define ZIP64_EOC_DIR_SIZE 56
6001
#define ZIP64_EOC_DIR_LOCATOR_SIZE 20
6002
#define ZIP_EOC_DIR_SIZE 22
6003
6004
#define ZIP_LOCAL_HEADER_CRC32_OFFSET 14
6005
#define ZIP_LOCAL_HEADER_FILENAME_LENGTH_OFFSET 26
6006
6007
struct zip_file
6008
{
6009
    uint64_t offset;
6010
    uint64_t size;
6011
    uint64_t compressed;
6012
    uint32_t crc32;
6013
    int compress;
6014
};
6015
6016
static zip_file* zip_new_file(zip* z)
6017
{
6018
    if (z->count == z->max)
6019
    {
6020
        z->allocated += ZIP_MEMORY_BLOCK;
6021
        z->files = sys_realloc(z->files, z->allocated);
6022
        z->max = z->allocated / sizeof(zip_file);
6023
    }
6024
6025
    return z->files + z->count++;
6026
}
6027
6028
void zip_create(zip* z, const char* name)
6029
{
6030
    z->file = sys_create(name);
6031
    z->total = 0;
6032
    z->count = 0;
6033
    z->max = 0;
6034
    z->allocated = 0;
6035
    z->files = NULL;
6036
    z->current = NULL;
6037
6038
    time_t t = time(NULL);
6039
    struct tm* tm = localtime(&t);
6040
    z->date = (uint16_t)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
6041
    z->time = (uint16_t)((tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2));
6042
}
6043
6044
void zip_add_folder(zip* z, const char* name)
6045
{
6046
    size_t name_length = strlen(name) + 1;
6047
    if (name_length > ZIP_MAX_FILENAME)
6048
    {
6049
        sys_error("ERROR: dirname too long\n");
6050
    }
6051
6052
    zip_file* f = zip_new_file(z);
6053
    f->offset = z->total;
6054
    f->size = 0;
6055
    f->compressed = 0;
6056
    f->crc32 = 0;
6057
    f->compress = 0;
6058
6059
    uint8_t header[ZIP_LOCAL_HEADER_SIZE] = { 0x50, 0x4b, 0x03, 0x04 };
6060
    // version needed to extract
6061
    set16le(header + 4, ZIP_VERSION);
6062
    // general purpose bit flag
6063
    set16le(header + 6, ZIP_UTF8_FLAG);
6064
    // compression method
6065
    set16le(header + 8, ZIP_METHOD_STORE);
6066
    // last mod file time
6067
    set16le(header + 10, z->time);
6068
    // last mod file date
6069
    set16le(header + 12, z->date);
6070
    // file name length
6071
    set16le(header + 26, (uint16_t)name_length);
6072
6073
    sys_write(z->file, z->total, header, sizeof(header));
6074
    z->total += sizeof(header);
6075
6076
    sys_write(z->file, z->total, name, (uint16_t)name_length);
6077
    z->total += name_length - 1;
6078
6079
    char slash = '/';
6080
    sys_write(z->file, z->total, &slash, 1);
6081
    z->total += 1;
6082
}
6083
6084
uint64_t zip_begin_file(zip* z, const char* name, int compress)
6085
{
6086
    size_t name_length = strlen(name);
6087
    if (name_length > ZIP_MAX_FILENAME)
6088
    {
6089
        sys_error("ERROR: filename too long\n");
6090
    }
6091
6092
    zip_file* f = zip_new_file(z);
6093
    f->offset = z->total;
6094
    f->size = 0;
6095
    f->compressed = 0;
6096
    f->compress = compress;
6097
    z->current = f;
6098
6099
    crc32_init(&z->crc32);
6100
    z->crc32_set = 0;
6101
6102
    uint8_t header[ZIP_LOCAL_HEADER_SIZE] = { 0x50, 0x4b, 0x03, 0x04 };
6103
    // version needed to extract
6104
    set16le(header + 4, ZIP_VERSION);
6105
    // general purpose bit flag
6106
    set16le(header + 6, ZIP_UTF8_FLAG);
6107
    // compression method
6108
    set16le(header + 8, compress ? ZIP_METHOD_DEFLATE : ZIP_METHOD_STORE);
6109
    // last mod file time
6110
    set16le(header + 10, z->time);
6111
    // last mod file date
6112
    set16le(header + 12, z->date);
6113
    // file name length
6114
    set16le(header + 26, (uint16_t)name_length);
6115
6116
    sys_write(z->file, z->total, header, sizeof(header));
6117
    z->total += sizeof(header);
6118
6119
    sys_write(z->file, z->total, name, (uint16_t)name_length);
6120
    z->total += name_length;
6121
6122
    if (compress)
6123
    {
6124
        int flags = tdefl_create_comp_flags_from_zip_params(MZ_BEST_SPEED, -MZ_DEFAULT_WINDOW_BITS, MZ_DEFAULT_STRATEGY);
6125
        tdefl_init(&z->tdefl, flags);
6126
    }
6127
6128
    return z->total - f->offset;
6129
}
6130
6131
void zip_write_file(zip* z, const void* data, uint32_t size)
6132
{
6133
    z->current->size += size;
6134
    crc32_update(&z->crc32, data, size);
6135
6136
    if (z->current->compress)
6137
    {
6138
        const uint8_t* data8 = data;
6139
        while (size != 0)
6140
        {
6141
            uint8_t buffer[4096];
6142
6143
            size_t isize = size;
6144
            size_t osize = sizeof(buffer);
6145
            tdefl_compress(&z->tdefl, data8, &isize, buffer, &osize, TDEFL_NO_FLUSH);
6146
6147
            if (osize != 0)
6148
            {
6149
                sys_write(z->file, z->total, buffer, (uint32_t)osize);
6150
                z->current->compressed += osize;
6151
                z->total += osize;
6152
            }
6153
            data8 += isize;
6154
            size -= (uint32_t)isize;
6155
        }
6156
    }
6157
    else
6158
    {
6159
        sys_write(z->file, z->total, data, size);
6160
        z->current->compressed += size;
6161
        z->total += size;
6162
    }
6163
}
6164
6165
void zip_end_file(zip* z)
6166
{
6167
    if (z->current->compress)
6168
    {
6169
        for (;;)
6170
        {
6171
            uint8_t buffer[4096];
6172
6173
            size_t isize = 0;
6174
            size_t osize = sizeof(buffer);
6175
            tdefl_status st = tdefl_compress(&z->tdefl, NULL, &isize, buffer, &osize, TDEFL_FINISH);
6176
6177
            if (osize != 0)
6178
            {
6179
                sys_write(z->file, z->total, buffer, (uint32_t)osize);
6180
                z->current->compressed += osize;
6181
                z->total += osize;
6182
            }
6183
            if (st == TDEFL_STATUS_DONE)
6184
            {
6185
                break;
6186
            }
6187
        }
6188
    }
6189
6190
    if (!z->crc32_set)
6191
    {
6192
        z->current->crc32 = crc32_done(&z->crc32);
6193
    }
6194
6195
    if (z->current->size != 0)
6196
    {
6197
        uint8_t update[3 * sizeof(uint32_t)];
6198
        // crc-32
6199
        set32le(update + 0, z->current->crc32);
6200
        // compressed size
6201
        set32le(update + 4, (uint32_t)min64(z->current->compressed, 0xffffffff));
6202
        // uncompressed size
6203
        set32le(update + 8, (uint32_t)min64(z->current->size, 0xffffffff));
6204
6205
        sys_write(z->file, z->current->offset + ZIP_LOCAL_HEADER_CRC32_OFFSET, update, sizeof(update));
6206
    }
6207
6208
    z->current = NULL;
6209
}
6210
6211
void zip_close(zip* z)
6212
{
6213
    uint64_t central_dir_offset = z->total;
6214
6215
    // central directory headers
6216
    for (uint32_t i = 0; i < z->count; i++)
6217
    {
6218
        const zip_file* f = z->files + i;
6219
6220
        uint8_t local[ZIP_LOCAL_HEADER_SIZE];
6221
        sys_read(z->file, f->offset, local, sizeof(local));
6222
6223
        uint32_t filename_length = get16le(local + ZIP_LOCAL_HEADER_FILENAME_LENGTH_OFFSET);
6224
6225
        uint8_t global[ZIP_GLOBAL_HEADER_SIZE + ZIP_MAX_FILENAME] = { 0x50, 0x4b, 0x01, 0x02 };
6226
        sys_read(z->file, f->offset + sizeof(local), global + ZIP_GLOBAL_HEADER_SIZE, filename_length);
6227
        int is_folder = global[ZIP_GLOBAL_HEADER_SIZE + filename_length - 1] == '/';
6228
6229
        uint8_t extra[28];
6230
        uint16_t extra_size = 0;
6231
        uint64_t size = f->size;
6232
        uint64_t compressed = f->compressed;
6233
        uint64_t offset = f->offset;
6234
        uint32_t attributes = ZIP_DOS_ATTRIBUTE_ARCHIVE;
6235
        if (is_folder)
6236
        {
6237
            attributes |= ZIP_DOS_ATTRIBUTE_DIRECTORY;
6238
            if (offset > 0xffffffff)
6239
            {
6240
                extra_size += sizeof(uint64_t);
6241
            }
6242
        }
6243
        else
6244
        {
6245
            if (size > 0xffffffff)
6246
            {
6247
                extra_size += sizeof(uint64_t);
6248
            }
6249
            if (compressed > 0xffffffff)
6250
            {
6251
                extra_size += sizeof(uint64_t);
6252
            }
6253
            if (offset > 0xffffffff)
6254
            {
6255
                extra_size += sizeof(uint64_t);
6256
            }
6257
        }
6258
6259
        if (extra_size)
6260
        {
6261
            extra_size += 2 * sizeof(uint16_t);
6262
        }
6263
6264
        // version made by
6265
        set16le(global + 4, ZIP_VERSION);
6266
        // version needed to extract
6267
        set16le(global + 6, ZIP_VERSION);
6268
        // general purpose bit flag
6269
        set16le(global + 8, ZIP_UTF8_FLAG);
6270
        // compression method
6271
        set16le(global + 10, f->compress ? ZIP_METHOD_DEFLATE : ZIP_METHOD_STORE);
6272
        // last mod file time
6273
        set16le(global + 12, z->time);
6274
        // last mod file date
6275
        set16le(global + 14, z->date);
6276
        // crc-32
6277
        set32le(global + 16, f->crc32);
6278
        // compressed size
6279
        set32le(global + 20, (uint32_t)min64(compressed, 0xffffffff));
6280
        // uncompressed size
6281
        set32le(global + 24, (uint32_t)min64(size, 0xffffffff));
6282
        // file name length
6283
        set16le(global + 28, (uint16_t)filename_length);
6284
        // extra field length
6285
        set16le(global + 30, extra_size);
6286
        // external file attributes
6287
        set32le(global + 38, attributes);
6288
        // relative offset of local header 4 bytes
6289
        set32le(global + 42, (uint32_t)min64(offset, 0xffffffff));
6290
6291
        sys_write(z->file, z->total, global, ZIP_GLOBAL_HEADER_SIZE + filename_length);
6292
        z->total += ZIP_GLOBAL_HEADER_SIZE + filename_length;
6293
6294
        // zip64 Extended Information Extra Field
6295
        set16le(extra + 0, 1);
6296
        // size of this "extra" block
6297
        uint32_t extra_offset = 2 * sizeof(uint16_t);
6298
        set16le(extra + 2, (uint16_t)(extra_size - extra_offset));
6299
        if (compressed > 0xffffffff)
6300
        {
6301
            // size of compressed data
6302
            set64le(extra + extra_offset, compressed);
6303
            extra_offset += sizeof(uint64_t);
6304
        }
6305
        if (size > 0xffffffff)
6306
        {
6307
            // original uncompressed file size
6308
            set64le(extra + extra_offset, size);
6309
            extra_offset += sizeof(uint64_t);
6310
        }
6311
        if (offset > 0xffffffff)
6312
        {
6313
            // offset of local header record
6314
            set64le(extra + extra_offset, offset);
6315
            extra_offset += sizeof(uint64_t);
6316
        }
6317
6318
        if (extra_size > 2 * sizeof(uint16_t))
6319
        {
6320
            sys_write(z->file, z->total, extra, extra_size);
6321
            z->total += extra_size;
6322
        }
6323
    }
6324
6325
    uint64_t end_of_central_dir_offset = z->total;
6326
    uint64_t central_dir_size = end_of_central_dir_offset - central_dir_offset;
6327
6328
    // zip64 end of central directory record
6329
    {
6330
        uint8_t header[ZIP64_EOC_DIR_SIZE] = { 0x50, 0x4b, 0x06, 0x06 };
6331
        // size of zip64 end of central directory record
6332
        set64le(header + 4, sizeof(header) - sizeof(uint32_t) - sizeof(uint64_t));
6333
        // version made by
6334
        set16le(header + 12, ZIP_VERSION);
6335
        // version needed to extract
6336
        set16le(header + 14, ZIP_VERSION);
6337
        // total number of entries in the central directory on this disk
6338
        set64le(header + 24, z->count);
6339
        // total number of entries in the central directory
6340
        set64le(header + 32, z->count);
6341
        // size of the central directory
6342
        set64le(header + 40, central_dir_size);
6343
        // offset of start of central directory with respect to the starting disk number
6344
        set64le(header + 48, central_dir_offset);
6345
6346
        sys_write(z->file, z->total, header, sizeof(header));
6347
        z->total += sizeof(header);
6348
    }
6349
6350
    // zip64 end of central directory locator
6351
    {
6352
        uint8_t header[ZIP64_EOC_DIR_LOCATOR_SIZE] = { 0x50, 0x4b, 0x06, 0x07 };
6353
        // relative offset of the zip64 end of central directory record 8 bytes
6354
        set64le(header + 8, end_of_central_dir_offset);
6355
        // total number of disks
6356
        set32le(header + 16, 1);
6357
6358
        sys_write(z->file, z->total, header, sizeof(header));
6359
        z->total += sizeof(header);
6360
    }
6361
6362
    // end of central directory record
6363
    {
6364
        uint8_t header[ZIP_EOC_DIR_SIZE] = { 0x50, 0x4b, 0x05, 0x06 };
6365
        // total number of entries in the central directory on this disk
6366
        set16le(header + 8, (uint16_t)min32(z->count, 0xffff));
6367
        // total number of entries in the central directory
6368
        set16le(header + 10, (uint16_t)min32(z->count, 0xffff));
6369
        // size of the central directory
6370
        set32le(header + 12, (uint32_t)min64(central_dir_size, 0xffffffff));
6371
        // offset of start of central directory with respect to the starting disk number
6372
        set32le(header + 16, (uint32_t)min64(central_dir_offset, 0xffffffff));
6373
6374
        sys_write(z->file, z->total, header, sizeof(header));
6375
        z->total += sizeof(header);
6376
    }
6377
6378
    sys_close(z->file);
6379
6380
    sys_realloc(z->files, 0);
6381
}
6382
6383
void zip_write_file_at(zip* z, uint64_t offset, const void* data, uint32_t size)
6384
{
6385
    if (z->current->compress)
6386
    {
6387
        sys_error("ERROR: cannot write at specific offset for compressed files\n");
6388
    }
6389
6390
    sys_write(z->file, z->current->offset + offset, data, size);
6391
    z->current->size += size;
6392
    z->current->compressed += size;
6393
}
6394
6395
void zip_set_offset(zip* z, uint64_t offset)
6396
{
6397
    z->total = z->current->offset + offset;
6398
}
6399
6400
void zip_set_crc32(zip* z, uint32_t crc)
6401
{
6402
    z->current->crc32 = crc;
6403
    z->crc32_set = 1;
6404
}
6405
6406
uint32_t zip_get_crc32(zip* z)
6407
{
6408
    return crc32_done(&z->crc32);
6409
}
6410
07070100000017000081A4000001ED0000006B000000015EE6501B00000405000000000000000000000000000000000000003900000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_zip.h#pragma once
6411
6412
#include "pkg2zip_sys.h"
6413
#include "pkg2zip_crc32.h"
6414
#include "miniz_tdef.h"
6415
6416
#include <stddef.h>
6417
#include <stdint.h>
6418
6419
#define ZIP_MAX_FILENAME 1024
6420
6421
typedef struct zip_file zip_file;
6422
6423
typedef struct {
6424
    sys_file file;
6425
    uint64_t total;
6426
    uint32_t count;
6427
    uint32_t max;
6428
    uint16_t time;
6429
    uint16_t date;
6430
    tdefl_compressor tdefl;
6431
    crc32_ctx crc32;
6432
    int crc32_set;
6433
    uint32_t allocated; // bytes
6434
    zip_file* files;
6435
    zip_file* current;
6436
} zip;
6437
6438
void zip_create(zip* z, const char* name);
6439
void zip_add_folder(zip* z, const char* name);
6440
uint64_t zip_begin_file(zip* z, const char* name, int compress);
6441
void zip_write_file(zip* z, const void* data, uint32_t size);
6442
void zip_end_file(zip* z);
6443
void zip_close(zip* z);
6444
6445
// hacky solution to be able to write cso header after the data is written
6446
void zip_write_file_at(zip* z, uint64_t offset, const void* data, uint32_t size);
6447
void zip_set_offset(zip* z, uint64_t offset);
6448
void zip_set_crc32(zip* z, uint32_t crc);
6449
uint32_t zip_get_crc32(zip* z);
6450
07070100000018000081A4000001ED0000006B000000015EE6501B00001CAD000000000000000000000000000000000000003A00000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_zrif.c#include "pkg2zip_zrif.h"
6451
#include "pkg2zip_utils.h"
6452
#include "pkg2zip_sys.h"
6453
#include "miniz_tdef.h"
6454
#include "puff.h"
6455
6456
#include <assert.h>
6457
#include <string.h>
6458
6459
#define ADLER32_MOD 65521
6460
6461
#define ZLIB_DEFLATE_METHOD 8
6462
#define ZLIB_DICTIONARY_ID_ZRIF 0x627d1d5d
6463
6464
static const uint8_t zrif_dict[] =
6465
{
6466
    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,
6467
    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,
6468
    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,
6469
    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,
6470
    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,
6471
    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,
6472
    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,
6473
    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,
6474
    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,
6475
    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,
6476
    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,
6477
    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,
6478
    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,
6479
    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,
6480
    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,
6481
    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,
6482
    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,
6483
    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,
6484
    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,
6485
    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,
6486
    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,
6487
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 48, 48, 48, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 48,
6488
    48, 48, 54, 48, 48, 48, 48, 55, 48, 48, 48, 48, 56, 0, 48, 48, 48, 48, 51, 48, 48, 48, 48, 52, 48, 48, 48, 48,
6489
    53, 48, 95, 48, 48, 45, 65, 68, 68, 67, 79, 78, 84, 48, 48, 48, 48, 50, 45, 80, 67, 83, 71, 48, 48, 48, 48,
6490
    48, 48, 48, 48, 48, 48, 49, 45, 80, 67, 83, 69, 48, 48, 48, 45, 80, 67, 83, 70, 48, 48, 48, 45, 80, 67, 83,
6491
    67, 48, 48, 48, 45, 80, 67, 83, 68, 48, 48, 48, 45, 80, 67, 83, 65, 48, 48, 48, 45, 80, 67, 83, 66, 48, 48,
6492
    48, 0, 1, 0, 1, 0, 1, 0, 2, 239, 205, 171, 137, 103, 69, 35, 1,
6493
};
6494
6495
static const uint8_t b64d[] =
6496
{
6497
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6498
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6499
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
6500
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
6501
    64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
6502
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
6503
    64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
6504
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
6505
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6506
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6507
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6508
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6509
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6510
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6511
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6512
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
6513
};
6514
6515
static uint32_t base64_decode(const char* in, uint8_t* out)
6516
{
6517
    const uint8_t* out0 = out;
6518
    const uint8_t* in8 = (uint8_t*)in;
6519
6520
    size_t len = strlen(in);
6521
    if (in[len - 1] == '=')
6522
    {
6523
        len--;
6524
    }
6525
    if (in[len - 1] == '=')
6526
    {
6527
        len--;
6528
    }
6529
6530
    for (size_t i = 0; i < len / 4; i++)
6531
    {
6532
        *out++ = (b64d[in8[0]] << 2) + ((b64d[in8[1]] & 0x30) >> 4);
6533
        *out++ = (b64d[in8[1]] << 4) + (b64d[in8[2]] >> 2);
6534
        *out++ = (b64d[in8[2]] << 6) + b64d[in8[3]];
6535
        in8 += 4;
6536
    }
6537
6538
    size_t left = len % 4;
6539
    if (left == 2)
6540
    {
6541
        *out++ = (b64d[in8[0]] << 2) + ((b64d[in8[1]] & 0x30) >> 4);
6542
        *out++ = (b64d[in8[1]] << 4);
6543
    }
6544
    else if (left == 3)
6545
    {
6546
        *out++ = (b64d[in8[0]] << 2) + ((b64d[in8[1]] & 0x30) >> 4);
6547
        *out++ = (b64d[in8[1]] << 4) + (b64d[in8[2]] >> 2);
6548
        *out++ = b64d[in8[2]] << 6;
6549
    }
6550
6551
    return (uint32_t)(out - out0);
6552
}
6553
6554
// https://www.ietf.org/rfc/rfc1950.txt
6555
static uint32_t zlib_inflate(const uint8_t* in, uint32_t inlen, uint8_t* out, uint32_t outlen)
6556
{
6557
    if (inlen < 2 + 4)
6558
    {
6559
        sys_error("ERROR: zRIF length too short\n");
6560
    }
6561
6562
    if (((in[0] << 8) + in[1]) % 31 != 0)
6563
    {
6564
        sys_error("ERROR: zRIF header is corrupted\n");
6565
    }
6566
6567
    if ((in[0] & 0xf) != ZLIB_DEFLATE_METHOD)
6568
    {
6569
        sys_error("ERROR: only deflate method supported in zRIF\n");
6570
    }
6571
6572
    unsigned long slen = inlen - 4;
6573
    unsigned long dlen = outlen;
6574
    unsigned long dictlen = 0;
6575
6576
    if (in[1] & (1 << 5))
6577
    {
6578
        assert(outlen > sizeof(zrif_dict));
6579
6580
        memcpy(out, zrif_dict, sizeof(zrif_dict));
6581
        dictlen = sizeof(zrif_dict);
6582
6583
        if (get32be(in + 2) != ZLIB_DICTIONARY_ID_ZRIF)
6584
        {
6585
            sys_error("ERROR: zRIF uses unknown dictionary\n");
6586
        }
6587
6588
        in += 6;
6589
        slen -= 6;
6590
    }
6591
    else
6592
    {
6593
        in += 2;
6594
        slen -= 2;
6595
    }
6596
6597
    if (puff(dictlen, out, &dlen, in, &slen) != 0)
6598
    {
6599
        sys_error("ERROR: failed to uncompress zRIF\n");
6600
    }
6601
    memmove(out, out + dictlen, dlen);
6602
6603
    if (mz_adler32(MZ_ADLER32_INIT, out, dlen) != get32be(in + slen))
6604
    {
6605
        sys_error("ERROR: zRIF is corrupted, wrong checksum\n");
6606
    }
6607
6608
    return dlen;
6609
}
6610
6611
void zrif_decode(const char* str, uint8_t* rif, uint32_t rif_size)
6612
{
6613
    uint8_t raw[1024];
6614
    uint32_t len = base64_decode(str, raw);
6615
6616
    uint8_t out[sizeof(zrif_dict) + 1024];
6617
    len = zlib_inflate(raw, len, out, sizeof(out));
6618
    if (len != rif_size)
6619
    {
6620
        sys_error("ERROR: wrong size of zRIF, is it corrupted?\n");
6621
    }
6622
6623
    memcpy(rif, out, rif_size);
6624
}
6625
07070100000019000081A4000001ED0000006B000000015EE6501B00000067000000000000000000000000000000000000003A00000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/pkg2zip_zrif.h#pragma once
6626
6627
#include <stdint.h>
6628
6629
void zrif_decode(const char* str, uint8_t* rif, uint32_t rif_size);
6630
0707010000001A000081A4000001ED0000006B000000015EE6501B00003F9D000000000000000000000000000000000000003200000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/puff.c/*
6631
 * puff.c
6632
 * Copyright (C) 2002-2013 Mark Adler
6633
 * For conditions of distribution and use, see copyright notice in puff.h
6634
 * version 2.3, 21 Jan 2013
6635
 */
6636
6637
#include <setjmp.h>             /* for setjmp(), longjmp(), and jmp_buf */
6638
#include "puff.h"               /* prototype for puff() */
6639
6640
#define local static            /* for local function definitions */
6641
6642
#define MAXBITS 15              /* maximum bits in a code */
6643
#define MAXLCODES 286           /* maximum number of literal/length codes */
6644
#define MAXDCODES 30            /* maximum number of distance codes */
6645
#define MAXCODES (MAXLCODES+MAXDCODES)  /* maximum codes lengths to read */
6646
#define FIXLCODES 288           /* number of fixed literal/length codes */
6647
6648
/* input and output state */
6649
struct state {
6650
    /* output state */
6651
    unsigned char *out;         /* output buffer */
6652
    unsigned long outlen;       /* available space at out */
6653
    unsigned long outcnt;       /* bytes written to out so far */
6654
6655
    /* input state */
6656
    const unsigned char *in;    /* input buffer */
6657
    unsigned long inlen;        /* available input at in */
6658
    unsigned long incnt;        /* bytes read so far */
6659
    int bitbuf;                 /* bit buffer */
6660
    int bitcnt;                 /* number of bits in bit buffer */
6661
6662
    /* input limit error return state for bits() and decode() */
6663
    jmp_buf env;
6664
};
6665
6666
local int bits(struct state *s, int need)
6667
{
6668
    long val;           /* bit accumulator (can use up to 20 bits) */
6669
6670
    /* load at least need bits into val */
6671
    val = s->bitbuf;
6672
    while (s->bitcnt < need) {
6673
        if (s->incnt == s->inlen)
6674
            longjmp(s->env, 1);         /* out of input */
6675
        val |= (long)(s->in[s->incnt++]) << s->bitcnt;  /* load eight bits */
6676
        s->bitcnt += 8;
6677
    }
6678
6679
    /* drop need bits and update buffer, always zero to seven bits left */
6680
    s->bitbuf = (int)(val >> need);
6681
    s->bitcnt -= need;
6682
6683
    /* return need bits, zeroing the bits above that */
6684
    return (int)(val & ((1L << need) - 1));
6685
}
6686
6687
local int stored(struct state *s)
6688
{
6689
    unsigned len;       /* length of stored block */
6690
6691
    /* discard leftover bits from current byte (assumes s->bitcnt < 8) */
6692
    s->bitbuf = 0;
6693
    s->bitcnt = 0;
6694
6695
    /* get length and check against its one's complement */
6696
    if (s->incnt + 4 > s->inlen)
6697
        return 2;                               /* not enough input */
6698
    len = s->in[s->incnt++];
6699
    len |= s->in[s->incnt++] << 8;
6700
    if (s->in[s->incnt++] != (~len & 0xff) ||
6701
        s->in[s->incnt++] != ((~len >> 8) & 0xff))
6702
        return -2;                              /* didn't match complement! */
6703
6704
    /* copy len bytes from in to out */
6705
    if (s->incnt + len > s->inlen)
6706
        return 2;                               /* not enough input */
6707
    if (s->out != NIL) {
6708
        if (s->outcnt + len > s->outlen)
6709
            return 1;                           /* not enough output space */
6710
        while (len--)
6711
            s->out[s->outcnt++] = s->in[s->incnt++];
6712
    }
6713
    else {                                      /* just scanning */
6714
        s->outcnt += len;
6715
        s->incnt += len;
6716
    }
6717
6718
    /* done with a valid stored block */
6719
    return 0;
6720
}
6721
6722
struct huffman {
6723
    short *count;       /* number of symbols of each length */
6724
    short *symbol;      /* canonically ordered symbols */
6725
};
6726
6727
local int decode(struct state *s, const struct huffman *h)
6728
{
6729
    int len;            /* current number of bits in code */
6730
    int code;           /* len bits being decoded */
6731
    int first;          /* first code of length len */
6732
    int count;          /* number of codes of length len */
6733
    int index;          /* index of first code of length len in symbol table */
6734
    int bitbuf;         /* bits from stream */
6735
    int left;           /* bits left in next or left to process */
6736
    short *next;        /* next number of codes */
6737
6738
    bitbuf = s->bitbuf;
6739
    left = s->bitcnt;
6740
    code = first = index = 0;
6741
    len = 1;
6742
    next = h->count + 1;
6743
    while (1) {
6744
        while (left--) {
6745
            code |= bitbuf & 1;
6746
            bitbuf >>= 1;
6747
            count = *next++;
6748
            if (code - count < first) { /* if length len, return symbol */
6749
                s->bitbuf = bitbuf;
6750
                s->bitcnt = (s->bitcnt - len) & 7;
6751
                return h->symbol[index + (code - first)];
6752
            }
6753
            index += count;             /* else update for next length */
6754
            first += count;
6755
            first <<= 1;
6756
            code <<= 1;
6757
            len++;
6758
        }
6759
        left = (MAXBITS+1) - len;
6760
        if (left == 0)
6761
            break;
6762
        if (s->incnt == s->inlen)
6763
            longjmp(s->env, 1);         /* out of input */
6764
        bitbuf = s->in[s->incnt++];
6765
        if (left > 8)
6766
            left = 8;
6767
    }
6768
    return -10;                         /* ran out of codes */
6769
}
6770
6771
local int construct(struct huffman *h, const short *length, int n)
6772
{
6773
    int symbol;         /* current symbol when stepping through length[] */
6774
    int len;            /* current length when stepping through h->count[] */
6775
    int left;           /* number of possible codes left of current length */
6776
    short offs[MAXBITS+1];      /* offsets in symbol table for each length */
6777
6778
    /* count number of codes of each length */
6779
    for (len = 0; len <= MAXBITS; len++)
6780
        h->count[len] = 0;
6781
    for (symbol = 0; symbol < n; symbol++)
6782
        (h->count[length[symbol]])++;   /* assumes lengths are within bounds */
6783
    if (h->count[0] == n)               /* no codes! */
6784
        return 0;                       /* complete, but decode() will fail */
6785
6786
    /* check for an over-subscribed or incomplete set of lengths */
6787
    left = 1;                           /* one possible code of zero length */
6788
    for (len = 1; len <= MAXBITS; len++) {
6789
        left <<= 1;                     /* one more bit, double codes left */
6790
        left -= h->count[len];          /* deduct count from possible codes */
6791
        if (left < 0)
6792
            return left;                /* over-subscribed--return negative */
6793
    }                                   /* left > 0 means incomplete */
6794
6795
    /* generate offsets into symbol table for each length for sorting */
6796
    offs[1] = 0;
6797
    for (len = 1; len < MAXBITS; len++)
6798
        offs[len + 1] = offs[len] + h->count[len];
6799
6800
    /*
6801
     * put symbols in table sorted by length, by symbol order within each
6802
     * length
6803
     */
6804
    for (symbol = 0; symbol < n; symbol++)
6805
        if (length[symbol] != 0)
6806
            h->symbol[offs[length[symbol]]++] = (short)symbol;
6807
6808
    /* return zero for complete set, positive for incomplete set */
6809
    return left;
6810
}
6811
6812
local int codes(struct state *s,
6813
                const struct huffman *lencode,
6814
                const struct huffman *distcode)
6815
{
6816
    int symbol;         /* decoded symbol */
6817
    int len;            /* length for copy */
6818
    unsigned dist;      /* distance for copy */
6819
    static const short lens[29] = { /* Size base for length codes 257..285 */
6820
        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
6821
        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
6822
    static const short lext[29] = { /* Extra bits for length codes 257..285 */
6823
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
6824
        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
6825
    static const short dists[30] = { /* Offset base for distance codes 0..29 */
6826
        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
6827
        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
6828
        8193, 12289, 16385, 24577};
6829
    static const short dext[30] = { /* Extra bits for distance codes 0..29 */
6830
        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
6831
        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
6832
        12, 12, 13, 13};
6833
6834
    /* decode literals and length/distance pairs */
6835
    do {
6836
        symbol = decode(s, lencode);
6837
        if (symbol < 0)
6838
            return symbol;              /* invalid symbol */
6839
        if (symbol < 256) {             /* literal: symbol is the byte */
6840
            /* write out the literal */
6841
            if (s->out != NIL) {
6842
                if (s->outcnt == s->outlen)
6843
                    return 1;
6844
                s->out[s->outcnt] = (unsigned char)symbol;
6845
            }
6846
            s->outcnt++;
6847
        }
6848
        else if (symbol > 256) {        /* length */
6849
            /* get and compute length */
6850
            symbol -= 257;
6851
            if (symbol >= 29)
6852
                return -10;             /* invalid fixed code */
6853
            len = lens[symbol] + bits(s, lext[symbol]);
6854
6855
            /* get and check distance */
6856
            symbol = decode(s, distcode);
6857
            if (symbol < 0)
6858
                return symbol;          /* invalid symbol */
6859
            dist = dists[symbol] + bits(s, dext[symbol]);
6860
#ifndef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
6861
            if (dist > s->outcnt)
6862
                return -11;     /* distance too far back */
6863
#endif
6864
6865
            /* copy length bytes from distance bytes back */
6866
            if (s->out != NIL) {
6867
                if (s->outcnt + len > s->outlen)
6868
                    return 1;
6869
                while (len--) {
6870
                    s->out[s->outcnt] =
6871
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
6872
                        dist > s->outcnt ?
6873
                            0 :
6874
#endif
6875
                            s->out[s->outcnt - dist];
6876
                    s->outcnt++;
6877
                }
6878
            }
6879
            else
6880
                s->outcnt += len;
6881
        }
6882
    } while (symbol != 256);            /* end of block symbol */
6883
6884
    /* done with a valid fixed or dynamic block */
6885
    return 0;
6886
}
6887
6888
local int fixed(struct state *s)
6889
{
6890
    static int virgin = 1;
6891
    static short lencnt[MAXBITS+1], lensym[FIXLCODES];
6892
    static short distcnt[MAXBITS+1], distsym[MAXDCODES];
6893
    static struct huffman lencode, distcode;
6894
6895
    /* build fixed huffman tables if first call (may not be thread safe) */
6896
    if (virgin) {
6897
        int symbol;
6898
        short lengths[FIXLCODES];
6899
6900
        /* construct lencode and distcode */
6901
        lencode.count = lencnt;
6902
        lencode.symbol = lensym;
6903
        distcode.count = distcnt;
6904
        distcode.symbol = distsym;
6905
6906
        /* literal/length table */
6907
        for (symbol = 0; symbol < 144; symbol++)
6908
            lengths[symbol] = 8;
6909
        for (; symbol < 256; symbol++)
6910
            lengths[symbol] = 9;
6911
        for (; symbol < 280; symbol++)
6912
            lengths[symbol] = 7;
6913
        for (; symbol < FIXLCODES; symbol++)
6914
            lengths[symbol] = 8;
6915
        construct(&lencode, lengths, FIXLCODES);
6916
6917
        /* distance table */
6918
        for (symbol = 0; symbol < MAXDCODES; symbol++)
6919
            lengths[symbol] = 5;
6920
        construct(&distcode, lengths, MAXDCODES);
6921
6922
        /* do this just once */
6923
        virgin = 0;
6924
    }
6925
6926
    /* decode data until end-of-block code */
6927
    return codes(s, &lencode, &distcode);
6928
}
6929
6930
local int dynamic(struct state *s)
6931
{
6932
    int nlen, ndist, ncode;             /* number of lengths in descriptor */
6933
    int index;                          /* index of lengths[] */
6934
    int err;                            /* construct() return value */
6935
    short lengths[MAXCODES];            /* descriptor code lengths */
6936
    short lencnt[MAXBITS+1], lensym[MAXLCODES];         /* lencode memory */
6937
    short distcnt[MAXBITS+1], distsym[MAXDCODES];       /* distcode memory */
6938
    struct huffman lencode, distcode;   /* length and distance codes */
6939
    static const short order[19] =      /* permutation of code length codes */
6940
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
6941
6942
    /* construct lencode and distcode */
6943
    lencode.count = lencnt;
6944
    lencode.symbol = lensym;
6945
    distcode.count = distcnt;
6946
    distcode.symbol = distsym;
6947
6948
    /* get number of lengths in each table, check lengths */
6949
    nlen = bits(s, 5) + 257;
6950
    ndist = bits(s, 5) + 1;
6951
    ncode = bits(s, 4) + 4;
6952
    if (nlen > MAXLCODES || ndist > MAXDCODES)
6953
        return -3;                      /* bad counts */
6954
6955
    /* read code length code lengths (really), missing lengths are zero */
6956
    for (index = 0; index < ncode; index++)
6957
        lengths[order[index]] = (short)bits(s, 3);
6958
    for (; index < 19; index++)
6959
        lengths[order[index]] = 0;
6960
6961
    /* build huffman table for code lengths codes (use lencode temporarily) */
6962
    err = construct(&lencode, lengths, 19);
6963
    if (err != 0)               /* require complete code set here */
6964
        return -4;
6965
6966
    /* read length/literal and distance code length tables */
6967
    index = 0;
6968
    while (index < nlen + ndist) {
6969
        int symbol;             /* decoded value */
6970
        int len;                /* last length to repeat */
6971
6972
        symbol = decode(s, &lencode);
6973
        if (symbol < 0)
6974
            return symbol;          /* invalid symbol */
6975
        if (symbol < 16)                /* length in 0..15 */
6976
            lengths[index++] = (short)symbol;
6977
        else {                          /* repeat instruction */
6978
            len = 0;                    /* assume repeating zeros */
6979
            if (symbol == 16) {         /* repeat last length 3..6 times */
6980
                if (index == 0)
6981
                    return -5;          /* no last length! */
6982
                len = lengths[index - 1];       /* last length */
6983
                symbol = 3 + bits(s, 2);
6984
            }
6985
            else if (symbol == 17)      /* repeat zero 3..10 times */
6986
                symbol = 3 + bits(s, 3);
6987
            else                        /* == 18, repeat zero 11..138 times */
6988
                symbol = 11 + bits(s, 7);
6989
            if (index + symbol > nlen + ndist)
6990
                return -6;              /* too many lengths! */
6991
            while (symbol--)            /* repeat last or zero symbol times */
6992
                lengths[index++] = (short)len;
6993
        }
6994
    }
6995
6996
    /* check for end-of-block code -- there better be one! */
6997
    if (lengths[256] == 0)
6998
        return -9;
6999
7000
    /* build huffman table for literal/length codes */
7001
    err = construct(&lencode, lengths, nlen);
7002
    if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1]))
7003
        return -7;      /* incomplete code ok only for single length 1 code */
7004
7005
    /* build huffman table for distance codes */
7006
    err = construct(&distcode, lengths + nlen, ndist);
7007
    if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1]))
7008
        return -8;      /* incomplete code ok only for single length 1 code */
7009
7010
    /* decode data until end-of-block code */
7011
    return codes(s, &lencode, &distcode);
7012
}
7013
7014
int puff(unsigned long dictlen,         // length of custom dictionary
7015
         unsigned char *dest,           /* pointer to destination pointer */
7016
         unsigned long *destlen,        /* amount of output space */
7017
         const unsigned char *source,   /* pointer to source data pointer */
7018
         unsigned long *sourcelen)      /* amount of input available */
7019
{
7020
    struct state s;             /* input/output state */
7021
    int last, type;             /* block information */
7022
    int err;                    /* return value */
7023
7024
    /* initialize output state */
7025
    s.out = dest;
7026
    s.outlen = *destlen;                /* ignored if dest is NIL */
7027
    s.outcnt = dictlen;
7028
7029
    /* initialize input state */
7030
    s.in = source;
7031
    s.inlen = *sourcelen;
7032
    s.incnt = 0;
7033
    s.bitbuf = 0;
7034
    s.bitcnt = 0;
7035
7036
    /* return if bits() or decode() tries to read past available input */
7037
    if (setjmp(s.env) != 0)             /* if came back here via longjmp() */
7038
        err = 2;                        /* then skip do-loop, return error */
7039
    else {
7040
        /* process blocks until last block or error */
7041
        do {
7042
            last = bits(&s, 1);         /* one if last block */
7043
            type = bits(&s, 2);         /* block type 0..3 */
7044
            err = type == 0 ?
7045
                    stored(&s) :
7046
                    (type == 1 ?
7047
                        fixed(&s) :
7048
                        (type == 2 ?
7049
                            dynamic(&s) :
7050
                            -1));       /* type == 3, invalid */
7051
            if (err != 0)
7052
                break;                  /* return with error */
7053
        } while (!last);
7054
    }
7055
7056
    /* update the lengths and return */
7057
    if (err <= 0) {
7058
        *destlen = s.outcnt - dictlen;
7059
        *sourcelen = s.incnt;
7060
    }
7061
    return err;
7062
}
7063
0707010000001B000081A4000001ED0000006B000000015EE6501B00000634000000000000000000000000000000000000003200000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/puff.h/* puff.h
7064
  Copyright (C) 2002-2013 Mark Adler, all rights reserved
7065
  version 2.3, 21 Jan 2013
7066
7067
  This software is provided 'as-is', without any express or implied
7068
  warranty.  In no event will the author be held liable for any damages
7069
  arising from the use of this software.
7070
7071
  Permission is granted to anyone to use this software for any purpose,
7072
  including commercial applications, and to alter it and redistribute it
7073
  freely, subject to the following restrictions:
7074
7075
  1. The origin of this software must not be misrepresented; you must not
7076
     claim that you wrote the original software. If you use this software
7077
     in a product, an acknowledgment in the product documentation would be
7078
     appreciated but is not required.
7079
  2. Altered source versions must be plainly marked as such, and must not be
7080
     misrepresented as being the original software.
7081
  3. This notice may not be removed or altered from any source distribution.
7082
7083
  Mark Adler    madler@alumni.caltech.edu
7084
 */
7085
7086
// Extra modifications to support custom dictionary for pkg2zip
7087
7088
/*
7089
 * See puff.c for purpose and usage.
7090
 */
7091
#ifndef NIL
7092
#  define NIL ((unsigned char *)0)      /* for no output option */
7093
#endif
7094
7095
int puff(unsigned long dictlen,         // length of custom dictionary (must be placed in beginning of dest)
7096
         unsigned char *dest,           /* pointer to destination pointer */
7097
         unsigned long *destlen,        /* amount of output space */
7098
         const unsigned char *source,   /* pointer to source data pointer */
7099
         unsigned long *sourcelen);     /* amount of input available */
7100
0707010000001C000081ED000001ED0000006B000000015EE6501B00000285000000000000000000000000000000000000003700000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/rif2zrif.py#!/usr/bin/env python3
7101
7102
import sys
7103
import zlib
7104
import base64
7105
7106
zrif_dict = list(zlib.decompress(base64.b64decode(
7107
  b"eNpjYBgFo2AU0AsYAIElGt8MRJiDCAsw3xhEmIAIU4N4AwNdRxcXZ3+/EJCAkW6Ac7C7ARwYgviuQAaIdoPSzlDaBUo7QmknIM3ACIZM78+u7kx3VWYEAGJ9HV0=")))
7108
7109
if len(sys.argv) != 2:
7110
  exit("Usage: %s path/to/file.rif" % sys.argv[0])
7111
7112
rif = open(sys.argv[1], "rb").read()
7113
7114
c = zlib.compressobj(level=9, wbits=10, memLevel=8, zdict=bytes(zrif_dict))
7115
bin = c.compress(rif)
7116
bin += c.flush()
7117
7118
if len(bin) % 3 != 0:
7119
  bin += b"\0" * (3 - len(bin) % 3)
7120
7121
content = rif[0x10:0x40].rstrip(b"\0").decode("ascii")
7122
7123
print(content, base64.b64encode(bin).decode("ascii"))
7124
0707010000001D000081ED000001ED0000006B000000015EE6501B00000251000000000000000000000000000000000000003700000000pkg2zip-2.2+git.c75a9b2.1592152091.c75a9b2/zrif2rif.py#!/usr/bin/env python3
7125
7126
import sys
7127
import zlib
7128
import base64
7129
7130
zrif_dict = list(zlib.decompress(base64.b64decode(
7131
  b"eNpjYBgFo2AU0AsYAIElGt8MRJiDCAsw3xhEmIAIU4N4AwNdRxcXZ3+/EJCAkW6Ac7C7ARwYgviuQAaIdoPSzlDaBUo7QmknIM3ACIZM78+u7kx3VWYEAGJ9HV0=")))
7132
7133
if len(sys.argv) != 2 and len(sys.argv) != 3:
7134
  exit("Usage: %s zRIF [path/to/work.bin]" % sys.argv[0])
7135
7136
bin = base64.b64decode(sys.argv[1].encode("ascii"))
7137
7138
d = zlib.decompressobj(wbits=10, zdict=bytes(zrif_dict))
7139
rif = d.decompress(bin)
7140
rif += d.flush()
7141
7142
output = sys.argv[2] if len(sys.argv) == 3 else "work.bin"
7143
open(output, "wb").write(rif)
7144
07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!517 blocks
7145