Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
z64me committed May 3, 2021
1 parent 2a5d593 commit af889bf
Show file tree
Hide file tree
Showing 21 changed files with 3,005 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
[*.{c,h,ch}]
charset = utf-8
indent_style = tab
indent_size = 3
trim_trailing_whitespace = false

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin/
o/
test/
z64decompress
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions debug-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# build decoder functions
gcc -Og -g -lm -c -Wall -Wextra src/decoder/*.c
mkdir -p o
mv *.o o

# build everything else
gcc -o z64decompress src/*.c o/*.o -Wall -Wextra -Og -g

14 changes: 14 additions & 0 deletions release-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# build decoder functions
gcc -DNDEBUG -s -Ofast -flto -lm -c -Wall -Wextra src/decoder/*.c
mkdir -p o
mv *.o o

# build everything else
gcc -o z64decompress -DNDEBUG src/*.c o/*.o -Wall -Wextra -s -Os -flto

# move to bin directory
mkdir -p bin/linux64
mv z64decompress bin/linux64



14 changes: 14 additions & 0 deletions release-linux32.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# build decoder functions
gcc -m32 -DNDEBUG -s -Ofast -flto -lm -c -Wall src/decoder/*.c
mkdir -p o
mv *.o o

# build everything else
gcc -m32 -o z64decompress -DNDEBUG src/*.c o/*.o -Wall -Wextra -s -Os -flto

# move to bin directory
mkdir -p bin/linux32
mv z64decompress bin/linux32



12 changes: 12 additions & 0 deletions release-win32.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# build decoder functions
~/c/mxe/usr/bin/i686-w64-mingw32.static-gcc -DNDEBUG -s -Ofast -flto -lm -c -Wall src/decoder/*.c
mkdir -p o
mv *.o o

# build everything else
~/c/mxe/usr/bin/i686-w64-mingw32.static-gcc -o z64decompress.exe -DNDEBUG src/*.c o/*.o -Wall -Wextra -s -Os -flto -mconsole -municode

# move to bin directory
mkdir -p bin/win32
mv z64decompress.exe bin/win32

4 changes: 4 additions & 0 deletions src/decoder/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
these decoder functions were adapted from those in my z64enc repo

https://github.com/z64me/z64enc

229 changes: 229 additions & 0 deletions src/decoder/aplib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/* <z64.me> adapted from aPLib's depack.c */

/*
* aPLib compression library - the smaller the better :)
*
* C depacker
*
* Copyright (c) 1998-2014 Joergen Ibsen
* All Rights Reserved
*
* http://www.ibsensoftware.com/
*/

#include "private.h"

struct decoder
{
unsigned char buf[1024]; /* intermediate buffer for loading */
unsigned char *buf_end; /* pointer that exists for the sole *
* purpose of getting size of `buf` */
unsigned char *pstart; /* offset of next read from rom */
unsigned int remaining; /* remaining size of file */
unsigned char *buf_limit; /* points to end of scannable area *
* of buf; this prevents yaz parser *
* from overflowing */
#if MAJORA
unsigned char *dst_end; /* end of decompressed block */
#endif
};

/* internal data structure */
struct APDSTATE {
unsigned char *source;
unsigned char *destination;
unsigned int tag;
unsigned int bitcount;
};

static struct decoder dec;

static void *refill(unsigned char *ip)
{
unsigned offset;
unsigned size;

/* intermediate buffer is not yet due for a refill */
if (ip < dec.buf_end - 8)
return ip;

/* the number 8 is used throughout to ensure *
* dma transfers are always 8 byte aligned */
offset = dec.buf_end - ip;
size = sizeof(dec.buf) - 8;

/* the last eight bytes wrap around */
Bcopy(dec.buf_end - 8, dec.buf, 8);

/* transfer data from rom */
DMARomToRam(dec.pstart, dec.buf + 8, size);
dec.pstart += size;

return dec.buf + (8 - offset);
}

static unsigned int aP_getbit(struct APDSTATE *ud)
{
unsigned int bit;

/* check if tag is empty */
if (!ud->bitcount--) {
/* load next tag */
ud->tag = *ud->source++;
ud->bitcount = 7;
}

/* shift bit out of tag */
bit = (ud->tag >> 7) & 0x01;
ud->tag <<= 1;

return bit;
}

static unsigned int aP_getgamma(struct APDSTATE *ud)
{
unsigned int result = 1;

/* input gamma2-encoded bits */
do {
result = (result << 1) + aP_getbit(ud);
} while (aP_getbit(ud));

return result;
}

static inline void *aP_depack(void *source, unsigned char *destination)
{
struct APDSTATE ud;
unsigned int offs, len, R0, LWM;
int done;
int i;

ud.source = source;
ud.bitcount = 0;

R0 = (unsigned int) -1;
LWM = 0;
done = 0;

/* initial buffer fill */
ud.source = refill(ud.source);

/* skip header */
ud.source += 8;

/* first byte verbatim */
*destination++ = *ud.source++;

/* main decompression loop */
while (!done) {
ud.source = refill(ud.source);
if (aP_getbit(&ud)) {
if (aP_getbit(&ud)) {
if (aP_getbit(&ud)) {
offs = 0;

for (i = 4; i; i--) {
offs = (offs << 1) + aP_getbit(&ud);
}

if (offs) {
*destination = *(destination - offs);
destination++;
}
else {
*destination++ = 0x00;
}

LWM = 0;
}
else {
offs = *ud.source++;

len = 2 + (offs & 0x0001);

offs >>= 1;

if (offs) {
for (; len; len--) {
*destination = *(destination - offs);
destination++;
}
}
else {
done = 1;
}

R0 = offs;
LWM = 1;
}
}
else {
offs = aP_getgamma(&ud);

if ((LWM == 0) && (offs == 2)) {
offs = R0;

len = aP_getgamma(&ud);

for (; len; len--) {
*destination = *(destination - offs);
destination++;
}
}
else {
if (LWM == 0) {
offs -= 3;
}
else {
offs -= 2;
}

offs <<= 8;
offs += *ud.source++;

len = aP_getgamma(&ud);

if (offs >= 32000) {
len++;
}
if (offs >= 1280) {
len++;
}
if (offs < 128) {
len += 2;
}

for (; len; len--) {
*destination = *(destination - offs);
destination++;
}

R0 = offs;
}

LWM = 1;
}
}
else {
*destination++ = *ud.source++;
LWM = 0;
}
}

return destination;
}

/* main driver */
void apldec(void *src, void *dst, unsigned sz)
{
dec.pstart = src;
dec.buf_end = dec.buf + sizeof(dec.buf);
dst = aP_depack(dec.buf_end, dst);
(void)sz; /* unused parameter */
#if MAJORA
dec.dst_end = dst;
dec.buf_end = 0;
#endif
}

10 changes: 10 additions & 0 deletions src/decoder/decoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef Z64DECOMPRESS_DECODER_H_INCLUDED
#define Z64DECOMPRESS_DECODER_H_INCLUDED

void yazdec(void *src, void *dst, unsigned sz);
void lzodec(void *src, void *dst, unsigned sz);
void ucldec(void *src, void *dst, unsigned sz);
void apldec(void *src, void *dst, unsigned sz);

#endif /* Z64DECOMPRESS_DECODER_H_INCLUDED */

Loading

0 comments on commit af889bf

Please sign in to comment.