Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
cleanup delphine_unpack()
Browse files Browse the repository at this point in the history
  • Loading branch information
cyxx committed Jan 6, 2015
1 parent d0d1b44 commit 97eb3d5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
65 changes: 36 additions & 29 deletions unpack.cpp
Expand Up @@ -6,48 +6,55 @@

#include "unpack.h"

struct UnpackCtx {
int size, datasize;
uint32_t crc;
uint32_t bits;
uint8_t *dst;
const uint8_t *src;
};

static int rcr(UnpackCtx *uc, int CF) {
int rCF = (uc->chk & 1);
uc->chk >>= 1;
static int shiftBit(UnpackCtx *uc, int CF) {
int rCF = (uc->bits & 1);
uc->bits >>= 1;
if (CF) {
uc->chk |= 0x80000000;
uc->bits |= 0x80000000;
}
return rCF;
}

static int next_chunk(UnpackCtx *uc) {
int CF = rcr(uc, 0);
if (uc->chk == 0) {
uc->chk = READ_BE_UINT32(uc->src); uc->src -= 4;
uc->crc ^= uc->chk;
CF = rcr(uc, 1);
static int nextBit(UnpackCtx *uc) {
int CF = shiftBit(uc, 0);
if (uc->bits == 0) {
uc->bits = READ_BE_UINT32(uc->src); uc->src -= 4;
uc->crc ^= uc->bits;
CF = shiftBit(uc, 1);
}
return CF;
}

static uint16_t get_code(UnpackCtx *uc, uint8_t num_chunks) {
static uint16_t getBits(UnpackCtx *uc, uint8_t num_bits) {
uint16_t c = 0;
while (num_chunks--) {
while (num_bits--) {
c <<= 1;
if (next_chunk(uc)) {
if (nextBit(uc)) {
c |= 1;
}
}
return c;
}

static void dec_unk1(UnpackCtx *uc, uint8_t num_chunks, uint8_t add_count) {
uint16_t count = get_code(uc, num_chunks) + add_count + 1;
static void unpackHelper1(UnpackCtx *uc, uint8_t num_bits, uint8_t add_count) {
uint16_t count = getBits(uc, num_bits) + add_count + 1;
uc->datasize -= count;
while (count--) {
*uc->dst = (uint8_t)get_code(uc, 8);
*uc->dst = (uint8_t)getBits(uc, 8);
--uc->dst;
}
}

static void dec_unk2(UnpackCtx *uc, uint8_t num_chunks) {
uint16_t i = get_code(uc, num_chunks);
static void unpackHelper2(UnpackCtx *uc, uint8_t num_bits) {
uint16_t i = getBits(uc, num_bits);
uint16_t count = uc->size + 1;
uc->datasize -= count;
while (count--) {
Expand All @@ -63,26 +70,26 @@ bool delphine_unpack(uint8_t *dst, const uint8_t *src, int len) {
uc.dst = dst + uc.datasize - 1;
uc.size = 0;
uc.crc = READ_BE_UINT32(uc.src); uc.src -= 4;
uc.chk = READ_BE_UINT32(uc.src); uc.src -= 4;
uc.crc ^= uc.chk;
uc.bits = READ_BE_UINT32(uc.src); uc.src -= 4;
uc.crc ^= uc.bits;
do {
if (!next_chunk(&uc)) {
if (!nextBit(&uc)) {
uc.size = 1;
if (!next_chunk(&uc)) {
dec_unk1(&uc, 3, 0);
if (!nextBit(&uc)) {
unpackHelper1(&uc, 3, 0);
} else {
dec_unk2(&uc, 8);
unpackHelper2(&uc, 8);
}
} else {
uint16_t c = get_code(&uc, 2);
uint16_t c = getBits(&uc, 2);
if (c == 3) {
dec_unk1(&uc, 8, 8);
unpackHelper1(&uc, 8, 8);
} else if (c < 2) {
uc.size = c + 2;
dec_unk2(&uc, c + 9);
unpackHelper2(&uc, c + 9);
} else {
uc.size = get_code(&uc, 8);
dec_unk2(&uc, 12);
uc.size = getBits(&uc, 8);
unpackHelper2(&uc, 12);
}
}
} while (uc.datasize > 0);
Expand Down
10 changes: 0 additions & 10 deletions unpack.h
Expand Up @@ -9,16 +9,6 @@

#include "intern.h"


struct UnpackCtx {
int size, datasize;
uint32_t crc;
uint32_t chk;
uint8_t *dst;
const uint8_t *src;
};

extern bool delphine_unpack(uint8_t *dst, const uint8_t *src, int len);


#endif // UNPACK_H__

0 comments on commit 97eb3d5

Please sign in to comment.