mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-16 07:39:56 +00:00
Add raw parameter to redbean lua compress
This commit is contained in:
parent
f37524ef4f
commit
3d99ebb68c
4 changed files with 92 additions and 50 deletions
|
@ -1308,7 +1308,7 @@ FUNCTIONS
|
|||
the density of information. Cryptographic random should be in
|
||||
the ballpark of 7.9 whereas plaintext will be more like 4.5.
|
||||
|
||||
Compress(uncompdata:str[, level:int]) → compdata:str
|
||||
Compress(uncompdata:str[, level:int[, raw:bool]]) → compdata:str
|
||||
|
||||
Compresses data using DEFLATE algorithm. The compression
|
||||
format here is defined to be quick and handy for things like
|
||||
|
@ -1319,18 +1319,27 @@ FUNCTIONS
|
|||
>: Uncompress(Compress('hello'))
|
||||
"hello"
|
||||
|
||||
`level` is the compression level, which defaults to 7. The max
|
||||
is 10. Lower numbers go faster. Higher numbers go slower, but
|
||||
have better compression ratios.
|
||||
|
||||
[implementation details]
|
||||
The binary wire format is defined as follows:
|
||||
|
||||
1. uleb64 uncompressed byte size (1 to 10 bytes)
|
||||
2. uint32_t crc32 (4 bytes; zlib polynomial)
|
||||
3. data (created by zlib compress function)
|
||||
|
||||
Uncompress(compdata:str) → uncompdata:str
|
||||
`level` is the compression level, which defaults to 7. The max
|
||||
is 10. Lower numbers go faster. Higher numbers go slower, but
|
||||
have better compression ratios.
|
||||
|
||||
`raw` may be set to true if you only want `data` (3) to be
|
||||
returned. In this case, it's assumed the caller will take
|
||||
responsibility for storing the length (and optionall crc)
|
||||
separately. See the redbean Crc32() API.
|
||||
|
||||
>: a = 'hello'
|
||||
>: b = Compress(a, 9, true)
|
||||
>: Uncompress(b, #a)
|
||||
"hello"
|
||||
|
||||
Uncompress(compdata:str[, uncomplen:int]) → uncompdata:str
|
||||
|
||||
Uncompresses data using DEFLATE algorithm. This applies the
|
||||
inverse transform of the Compress() function. See its docs for
|
||||
|
@ -1341,6 +1350,15 @@ FUNCTIONS
|
|||
of validity iron-clad. It's implemented using Intel CLMUL so
|
||||
it has ludicrous speed performance as well.
|
||||
|
||||
If you used the `raw` parameter when calling Compress() i.e.
|
||||
`compdata` doesn't have the redbean header described above,
|
||||
then the `uncomplen` parameter may be supplied. IN that case
|
||||
your data is handed over directly to zlib `uncompress()`. In
|
||||
this case an exception will be raised if the value couldn't be
|
||||
decoded, or if the resulting length differed from the supplied
|
||||
length. It's recommended that Crc32() check still be performed
|
||||
manually after using this method.
|
||||
|
||||
Benchmark(func[, count[, maxattempts]])
|
||||
└─→ nanos:real, ticks:int, overhead-ticks:int, tries:int
|
||||
|
||||
|
|
|
@ -683,6 +683,7 @@ int LuaBenchmark(lua_State *L) {
|
|||
}
|
||||
|
||||
int LuaCompress(lua_State *L) {
|
||||
bool raw;
|
||||
size_t n, m;
|
||||
char *q, *e;
|
||||
uint32_t crc;
|
||||
|
@ -691,14 +692,23 @@ int LuaCompress(lua_State *L) {
|
|||
p = luaL_checklstring(L, 1, &n);
|
||||
level = luaL_optinteger(L, 2, Z_DEFAULT_COMPRESSION);
|
||||
m = compressBound(n);
|
||||
CHECK_NOTNULL((q = LuaAlloc(L, 10 + 4 + m)));
|
||||
crc = crc32_z(0, p, n);
|
||||
e = uleb64(q, n);
|
||||
e = WRITE32LE(e, crc);
|
||||
hdrlen = e - q;
|
||||
CHECK_EQ(Z_OK, compress2((unsigned char *)(q + hdrlen), &m,
|
||||
(unsigned char *)p, n, level));
|
||||
lua_pushlstring(L, q, hdrlen + m);
|
||||
if (lua_toboolean(L, 3)) {
|
||||
// raw mode
|
||||
CHECK_NOTNULL((q = LuaAlloc(L, m)));
|
||||
CHECK_EQ(Z_OK,
|
||||
compress2((unsigned char *)q, &m, (unsigned char *)p, n, level));
|
||||
lua_pushlstring(L, q, m);
|
||||
} else {
|
||||
// easy mode
|
||||
CHECK_NOTNULL((q = LuaAlloc(L, 10 + 4 + m)));
|
||||
crc = crc32_z(0, p, n);
|
||||
e = uleb64(q, n);
|
||||
e = WRITE32LE(e, crc);
|
||||
hdrlen = e - q;
|
||||
CHECK_EQ(Z_OK, compress2((unsigned char *)(q + hdrlen), &m,
|
||||
(unsigned char *)p, n, level));
|
||||
lua_pushlstring(L, q, hdrlen + m);
|
||||
}
|
||||
free(q);
|
||||
return 1;
|
||||
}
|
||||
|
@ -710,18 +720,28 @@ int LuaUncompress(lua_State *L) {
|
|||
const char *p;
|
||||
size_t n, m, len;
|
||||
p = luaL_checklstring(L, 1, &n);
|
||||
if ((rc = unuleb64(p, n, &m)) == -1 || n < rc + 4) {
|
||||
luaL_error(L, "compressed value too short to be valid");
|
||||
unreachable;
|
||||
}
|
||||
len = m;
|
||||
crc = READ32LE(p + rc);
|
||||
CHECK_NOTNULL((q = LuaAlloc(L, m)));
|
||||
if (uncompress((void *)q, &m, (unsigned char *)p + rc + 4, n) != Z_OK ||
|
||||
m != len || crc32_z(0, q, m) != crc) {
|
||||
free(q);
|
||||
luaL_error(L, "compressed value is corrupted");
|
||||
unreachable;
|
||||
if (lua_isnoneornil(L, 2)) {
|
||||
if ((rc = unuleb64(p, n, &m)) == -1 || n < rc + 4) {
|
||||
luaL_error(L, "compressed value too short to be valid");
|
||||
unreachable;
|
||||
}
|
||||
len = m;
|
||||
crc = READ32LE(p + rc);
|
||||
CHECK_NOTNULL((q = LuaAlloc(L, m)));
|
||||
if (uncompress((void *)q, &m, (unsigned char *)p + rc + 4, n) != Z_OK ||
|
||||
m != len || crc32_z(0, q, m) != crc) {
|
||||
free(q);
|
||||
luaL_error(L, "compressed value is corrupted");
|
||||
unreachable;
|
||||
}
|
||||
} else {
|
||||
len = m = luaL_checkinteger(L, 2);
|
||||
CHECK_NOTNULL((q = LuaAlloc(L, m)));
|
||||
if (uncompress((void *)q, &m, (void *)p, n) != Z_OK || m != len) {
|
||||
free(q);
|
||||
luaL_error(L, "compressed value is corrupted");
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
lua_pushlstring(L, q, m);
|
||||
free(q);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue