Skip to content

Commit

Permalink
Update C sources
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2brain committed Jun 21, 2022
1 parent 239ec40 commit 640db1b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 29 deletions.
5 changes: 3 additions & 2 deletions unarrc/external/unarr/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* The Unarchiver project (https://bitbucket.org/kosovan/theunarchiver/)
* Simon Bünzli (zeniko at gmail.com, http://www.zeniko.ch/#SumatraPDF)
* Felix Kauselmann (licorn at gmail.com)
* Bastien Nocera (hadess@hadess.net, http://www.hadess.net/)
* Bastien Nocera (hadess at hadess.net, http://www.hadess.net/)
* Wang Xinyu (comicfans44 at gmail.com)
* Liu Xiang (liuxiang@loongson.cn, https://www.loongson.cn/)
* Liu Xiang (liuxiang at loongson.cn, https://www.loongson.cn/)
* Mastercoms (mastercoms at tuta.io)

Most code is licensed under LGPLv3 (see COPYING). Exceptions are in code
included from other projects:
Expand Down
63 changes: 49 additions & 14 deletions unarrc/external/unarr/common/crc32.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,69 @@
/* Copyright 2015 the unarr project authors (see AUTHORS file).
/* Copyright 2022 the unarr project authors (see AUTHORS file).
License: LGPLv3 */

#include "unarr-imp.h"

#ifndef HAVE_ZLIB
#if !defined HAVE_ZLIB || !defined USE_ZLIB_CRC

/*
crc32 calculation based on Intel slice-by-8 algorithm with lookup-table generation code
adapted from https://gnunet.org/svn/gnunet/src/util/crypto_crc.c (public domain) */

/* code adapted from https://gnunet.org/svn/gnunet/src/util/crypto_crc.c (public domain) */
static inline uint32_t uint32le(const uint8_t *data) { return data[0] | data[1] << 8 | data[2] << 16 | (uint32_t)data[3] << 24; }

static bool crc_table_ready = false;
static uint32_t crc_table[256];
static uint32_t crc_table[8][256];

uint32_t ar_crc32(uint32_t crc32, const unsigned char *data, size_t data_len)
uint32_t ar_crc32(uint32_t crc32, const uint8_t * data, size_t data_len)
{
if (!crc_table_ready) {
uint32_t i, j;

static const uint32_t crc_poly = 0xEDB88320;

uint32_t h = 1;
crc_table[0] = 0;
for (i = 128; i; i >>= 1) {
h = (h >> 1) ^ ((h & 1) ? 0xEDB88320 : 0);
for (j = 0; j < 256; j += 2 * i) {
crc_table[i + j] = crc_table[j] ^ h;
crc_table[0][0] = 0;

for (unsigned int i = 128; i; i >>= 1) {
h = (h >> 1) ^ ((h & 1) ? crc_poly : 0);
for (unsigned int j = 0; j < 256; j += 2 * i) {
crc_table[0][i+j] = crc_table[0][j] ^ h;
}
}

for (unsigned int i = 0; i < 256; i++) {
for (unsigned int j = 1; j < 8; j++) {
crc_table[j][i] = (crc_table[j-1][i] >> 8) ^ crc_table[0][crc_table[j-1][i] & 0xFF];
}
}

crc_table_ready = true;
}

crc32 = crc32 ^ 0xFFFFFFFF;
crc32 ^= 0xFFFFFFFF;

while (data_len >= 8) {

uint32_t tmp = crc32 ^ uint32le(data);

crc32 = crc_table[7][ tmp & 0xFF ] ^
crc_table[6][(tmp >> 8) & 0xFF ] ^
crc_table[5][(tmp >> 16) & 0xFF ] ^
crc_table[4][ tmp >> 24 ];

tmp = uint32le(data + 4);

crc32 ^= crc_table[3][tmp & 0xFF] ^
crc_table[2][(tmp >> 8) & 0xFF] ^
crc_table[1][(tmp >> 16) & 0xFF] ^
crc_table[0][ tmp >> 24 ];

data += 8;
data_len -= 8;
}

while (data_len-- > 0) {
crc32 = (crc32 >> 8) ^ crc_table[(crc32 ^ *data++) & 0xFF];
crc32 = (crc32 >> 8) ^ crc_table[0][(crc32 ^ *data++) & 0xFF];
}

return crc32 ^ 0xFFFFFFFF;
}

Expand Down
4 changes: 2 additions & 2 deletions unarrc/external/unarr/rar/filter-rar.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ bool rar_run_filters(ar_archive_rar *rar)
uint32_t lastfilterlength;

filters->filterstart = SIZE_MAX;
end = (size_t)rar_expand(rar, end);
if (end != start + filter->blocklength) {

if ((size_t)rar_expand(rar, end) != end) {
warn("Failed to expand the expected amout of bytes");
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion unarrc/external/unarr/rar/parse-rar.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

static inline uint8_t uint8le(unsigned char *data) { return data[0]; }
static inline uint16_t uint16le(unsigned char *data) { return data[0] | data[1] << 8; }
static inline uint32_t uint32le(unsigned char *data) { return data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24; }
static inline uint32_t uint32le(unsigned char *data) { return data[0] | data[1] << 8 | data[2] << 16 | (uint32_t)data[3] << 24; }

bool rar_parse_header(ar_archive *ar, struct rar_header *header)
{
Expand Down
2 changes: 2 additions & 0 deletions unarrc/external/unarr/rar/rarvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ bool RARProgramAddInstr(RARProgram *prog, uint8_t instruction, bool bytemode)
if (prog->length + 1 >= prog->capacity) {
/* in my small file sample, 16 is the value needed most often */
uint32_t newCapacity = prog->capacity ? prog->capacity * 4 : 32;
if (!prog->opcodes)
return false;
RAROpcode *newCodes = calloc(newCapacity, sizeof(*prog->opcodes));
if (!newCodes)
return false;
Expand Down
12 changes: 6 additions & 6 deletions unarrc/external/unarr/zip/inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <string.h>

#ifdef _MSC_VER
#define MY_FORCE_INLINE __forceinline
#define UNARR_FORCE_INLINE __forceinline
#else
#define MY_FORCE_INLINE inline __attribute__((always_inline))
#define UNARR_FORCE_INLINE inline __attribute__((always_inline))
#endif

#define MAX_BITS 16
Expand Down Expand Up @@ -97,7 +97,7 @@ static const struct {

static const int table_code_length_idxs[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };

static MY_FORCE_INLINE bool br_ensure(inflate_state *state, int bits)
static UNARR_FORCE_INLINE bool br_ensure(inflate_state *state, int bits)
{
while (state->in.available < bits) {
if (*state->in.avail_in == 0)
Expand All @@ -109,15 +109,15 @@ static MY_FORCE_INLINE bool br_ensure(inflate_state *state, int bits)
return true;
}

static MY_FORCE_INLINE uint64_t br_bits(inflate_state *state, int bits)
static UNARR_FORCE_INLINE uint64_t br_bits(inflate_state *state, int bits)
{
uint64_t res = state->in.bits & (((uint64_t)1 << bits) - 1);
state->in.available -= bits;
state->in.bits >>= bits;
return res;
}

static MY_FORCE_INLINE void output(inflate_state *state, uint8_t value)
static UNARR_FORCE_INLINE void output(inflate_state *state, uint8_t value)
{
*state->out.data_out++ = value;
(*state->out.avail_out)--;
Expand Down Expand Up @@ -174,7 +174,7 @@ static bool tree_add_value(struct tree *tree, int key, int bits, int value)
return true;
}

static MY_FORCE_INLINE int tree_get_value(inflate_state *state, const struct tree *tree, bool not_fast)
static UNARR_FORCE_INLINE int tree_get_value(inflate_state *state, const struct tree *tree, bool not_fast)
{
if (state->state.tree_idx == 0) {
int key = state->in.bits & ((1 << TREE_FAST_BITS) - 1);
Expand Down
10 changes: 7 additions & 3 deletions unarrc/external/unarr/zip/parse-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#endif

static inline uint16_t uint16le(unsigned char *data) { return data[0] | data[1] << 8; }
static inline uint32_t uint32le(unsigned char *data) { return data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24; }
static inline uint32_t uint32le(unsigned char *data) { return data[0] | data[1] << 8 | data[2] << 16 | (uint32_t) data[3] << 24; }
static inline uint64_t uint64le(unsigned char *data) { return (uint64_t)uint32le(data) | (uint64_t)uint32le(data + 4) << 32; }

bool zip_seek_to_compressed_data(ar_archive_zip *zip)
Expand Down Expand Up @@ -38,7 +38,6 @@ bool zip_seek_to_compressed_data(ar_archive_zip *zip)
static bool zip_parse_extra_fields(ar_archive_zip *zip, struct zip_entry *entry)
{
uint8_t *extra;
uint32_t idx;

if (!entry->extralen)
return true;
Expand All @@ -51,9 +50,14 @@ static bool zip_parse_extra_fields(ar_archive_zip *zip, struct zip_entry *entry)
free(extra);
return false;
}
for (idx = 0; idx + 4 < entry->extralen; idx += 4 + uint16le(&extra[idx + 2])) {
for (uint32_t idx = 0; idx + 4 < entry->extralen; idx += 4 + uint16le(&extra[idx + 2])) {
if (uint16le(&extra[idx]) == 0x0001) {
uint16_t size = uint16le(&extra[idx + 2]);
if (size + idx + 1 > entry->extralen) {
free(extra);
return false;
}

uint16_t offset = 0;
if (entry->uncompressed == UINT32_MAX && offset + 8 <= size) {
entry->uncompressed = uint64le(&extra[idx + 4 + offset]);
Expand Down
4 changes: 3 additions & 1 deletion unarrc/external/unarr/zip/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ static bool zip_parse_local_entry(ar_archive *ar, off64_t offset)
zip->progress.data_left = (size_t)entry.datasize;
zip_clear_uncompress(&zip->uncomp);

if (entry.datasize == 0 && ar_entry_get_name(ar) && *zip->entry.name && zip->entry.name[strlen(zip->entry.name) - 1] == '/') {
if (entry.datasize == 0 && ar_entry_get_name(ar) &&
zip->entry.name != NULL && *zip->entry.name &&
zip->entry.name[strlen(zip->entry.name) - 1] == '/') {
log("Skipping directory entry \"%s\"", zip->entry.name);
return zip_parse_local_entry(ar, ar->entry_offset_next);
}
Expand Down

0 comments on commit 640db1b

Please sign in to comment.