Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
af54304
Add .vscode, *.exe and doc/libadplug.html into .gitignore
dmitrysmagin Feb 23, 2024
237e21b
Add a2m/a2t player with unpackers depack.c, sixpack.c and unlzh.c
dmitrysmagin Feb 24, 2024
593f15f
Add testing a2m/a2t modules
dmitrysmagin Feb 26, 2024
01cf1db
Add lzw/lzss unpackers, fix songend when pattern jump
dmitrysmagin Mar 6, 2024
58cc442
Show a2m/a2t version in gettype()
dmitrysmagin Mar 7, 2024
e0bea24
Fix comment about a2m/a2t versions: should be 1 - 14
dmitrysmagin Apr 4, 2024
07a6f7f
Use static_assert instead of C_ASSERT, drop irrelevant one.
dmitrysmagin May 2, 2024
9747a01
Update Makefile.am
mywave82 May 3, 2024
49a0de9
Initialize last_order, and make a2t_play() use rewind() instead of du…
mywave82 May 3, 2024
7e89608
Buffer-size was too small, and use snprintf() instead of sprintf() wh…
mywave82 May 3, 2024
b5e89e3
Fix memory leaks
mywave82 May 3, 2024
fcc4d65
Add protection of buffer-sizes when decompressing. sixpack() did over…
mywave82 May 3, 2024
d4550a5
Use fallback for static_assert() if not available
dmitrysmagin May 4, 2024
1fef6dd
Add buffer length validation when reading file data.
mywave82 May 4, 2024
04068f4
Fix typo in sizeof() in a2t_import()
dmitrysmagin May 4, 2024
658159c
Update various project files (building AppVeyor CI failes without this)
mywave82 May 5, 2024
6852b03
Fix warnings for signed/unsigned comparisons
dmitrysmagin May 5, 2024
943b123
Merge branch 'a2m-v2-sq' of github.com:dmitrysmagin/adplug into a2m-v…
dmitrysmagin May 5, 2024
cbdf8a8
valgrind shows that sixpack depacker uses memory that is not initiali…
mywave82 May 5, 2024
d918cf7
Replace ... in case statements with MSVC compatible syntax
dmitrysmagin May 6, 2024
572c28f
Fix wrong 'for' indices in generate_custom_vibrato()
dmitrysmagin May 6, 2024
eb6bbd5
Small fix for unsigned/signed comparison
dmitrysmagin May 6, 2024
5c4cd63
Update adplug.vcxproj
dmitrysmagin May 6, 2024
d57295a
Don't init arrays inside class to please MSVC
dmitrysmagin May 6, 2024
8bde378
Properly init/deinit vibrato_table and arpeggio_table
dmitrysmagin May 6, 2024
ff9381c
Create vibrato_table and arpeggio_table in C++ way
dmitrysmagin May 6, 2024
f44b1b9
Fix crash when setting volume for non-existing instrument (corridor.a2m)
dmitrysmagin May 6, 2024
26fc5da
Update MSVC6 and Watcom makefiles (not tested though)
dmitrysmagin May 6, 2024
d8c8cb0
Rework allocating/freeing of arpeggio and vibrato tables
dmitrysmagin May 12, 2024
fa2701b
Fix tests for previous commit
dmitrysmagin May 12, 2024
cf89062
adlibemu.c was added two times to the project file.
mywave82 May 22, 2024
fc302c3
Update playertest.cpp: Move closer to existing AT2 test
binarymaster May 23, 2024
8f44595
Update adplug.qpg: Add missing header files
binarymaster May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add protection of buffer-sizes when decompressing. sixpack() did over…
…flow in playertest, most of them had problem in stresstest.
  • Loading branch information
mywave82 committed May 3, 2024
commit fcc4d653972ffdfe833ee20dbcdb4a91f70063c8
38 changes: 21 additions & 17 deletions src/a2m-v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3149,30 +3149,34 @@ bool Ca2mv2Player::a2t_play(char *tune) // start_playing()

/* LOADER FOR A2M/A2T */

void Ca2mv2Player::a2t_depack(char *src, int srcsize, char *dst)
void Ca2mv2Player::a2t_depack(char *src, int srcsize, char *dst, int dstsize)
{
switch (ffver) {
case 1:
case 5: // sixpack
sixdepak((unsigned short *)src, (unsigned char *)dst, srcsize);
sixdepak((unsigned short *)src, (unsigned char *)dst, srcsize, dstsize);
break;
case 2:
case 6: // lzw
LZW_decompress(src, dst, srcsize);
LZW_decompress(src, dst, srcsize, dstsize);
break;
case 3:
case 7: // lzss
LZSS_decompress(src, dst, srcsize);
LZSS_decompress(src, dst, srcsize, dstsize);
break;
case 4:
case 8: // unpacked
memcpy(dst, src, srcsize);
if (dstsize < srcsize)
{
srcsize = dstsize;
memcpy(dst, src, srcsize);
}
break;
case 9 ... 11: // apack (aPlib)
aP_depack(src, dst);
aP_depack(src, dst, srcsize, dstsize);
break;
case 12 ... 14: // lzh
LZH_decompress(src, dst, srcsize);
LZH_decompress(src, dst, srcsize, dstsize);
break;
}
}
Expand Down Expand Up @@ -3253,7 +3257,7 @@ int Ca2mv2Player::a2t_read_instruments(char *src)
(ffver > 11 ? sizeof(tBPM_DATA) + sizeof(tINS_4OP_FLAGS) + sizeof(tRESERVED) : 0);
char *dst = (char *)calloc(dstsize, 1);

a2t_depack(src, len[0], dst);
a2t_depack(src, len[0], dst, dstsize);

if (ffver == 14) {
//memcpy(&songinfo->bpm_data, dst, sizeof(songinfo->bpm_data));
Expand Down Expand Up @@ -3298,7 +3302,7 @@ int Ca2mv2Player::a2t_read_fmregtable(char *src)
if (ffver < 9) return 0;

tFMREG_TABLE *data = (tFMREG_TABLE *)calloc(255, sizeof(tFMREG_TABLE));
a2t_depack(src, len[1], (char *)data);
a2t_depack(src, len[1], (char *)data, 255 * sizeof(tFMREG_TABLE));

int count = instrinfo->count;

Expand All @@ -3323,7 +3327,7 @@ int Ca2mv2Player::a2t_read_arpvibtable(char *src)
if (ffver < 9) return 0;

tARPVIB_TABLE *arpvib_table = (tARPVIB_TABLE *)calloc(255, sizeof(tARPVIB_TABLE));
a2t_depack(src, len[2], (char *)arpvib_table);
a2t_depack(src, len[2], (char *)arpvib_table, 255 * sizeof(tARPVIB_TABLE));

arpvib_tables_allocate(255, arpvib_table);

Expand All @@ -3338,7 +3342,7 @@ int Ca2mv2Player::a2t_read_disabled_fmregs(char *src)

bool (*dis_fmregs)[255][28] = (bool (*)[255][28])calloc(255, 28);

a2t_depack(src, len[3], (char *)*dis_fmregs);
a2t_depack(src, len[3], (char *)*dis_fmregs, 255 * 28);

disabled_fmregs_import(instrinfo->count, *dis_fmregs);

Expand All @@ -3352,7 +3356,7 @@ int Ca2mv2Player::a2t_read_order(char *src)
int blocknum[14] = {1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 4};
int i = blocknum[ffver - 1];

a2t_depack(src, len[i], (char *)songinfo->pattern_order);
a2t_depack(src, len[i], (char *)songinfo->pattern_order, sizeof (songinfo->pattern_order));

return len[i];
}
Expand Down Expand Up @@ -3524,7 +3528,7 @@ int Ca2mv2Player::a2_read_patterns(char *src, int s)
for (int i = 0; i < 4; i++) {
if (!len[i+s]) continue;

a2t_depack(src, len[i+s], (char *)old);
a2t_depack(src, len[i+s], (char *)old, 16 * sizeof (*old));

for (int p = 0; p < 16; p++) { // pattern
if (i * 8 + p >= eventsinfo->patterns)
Expand Down Expand Up @@ -3556,7 +3560,7 @@ int Ca2mv2Player::a2_read_patterns(char *src, int s)
for (int i = 0; i < 8; i++) {
if (!len[i+s]) continue;

a2t_depack(src, len[i+s], (char *)old);
a2t_depack(src, len[i+s], (char *)old, 8 * sizeof (*old));

for (int p = 0; p < 8; p++) { // pattern
if (i * 8 + p >= eventsinfo->patterns)
Expand Down Expand Up @@ -3586,7 +3590,7 @@ int Ca2mv2Player::a2_read_patterns(char *src, int s)
// 16 groups of 8 patterns
for (int i = 0; i < 16; i++) {
if (!len[i+s]) continue;
a2t_depack(src, len[i+s], (char *)old);
a2t_depack(src, len[i+s], (char *)old, 8 * sizeof (*old));
src += len[i+s];

for (int p = 0; p < 8; p++) { // pattern
Expand Down Expand Up @@ -3716,7 +3720,7 @@ int Ca2mv2Player::a2m_read_songdata(char *src)
{
if (ffver < 9) { // 1 - 8
A2M_SONGDATA_V1_8 *data = (A2M_SONGDATA_V1_8 *)malloc(sizeof(*data));
a2t_depack(src, len[0], (char *)data);
a2t_depack(src, len[0], (char *)data, sizeof (*data));

memcpy(songinfo->songname, data->songname + 1, 42);
memcpy(songinfo->composer, data->composer + 1, 42);
Expand Down Expand Up @@ -3747,7 +3751,7 @@ int Ca2mv2Player::a2m_read_songdata(char *src)
free(data);
} else { // 9 - 14
A2M_SONGDATA_V9_14 *data = (A2M_SONGDATA_V9_14 *)malloc(sizeof(*data));
a2t_depack(src, len[0], (char *)data);
a2t_depack(src, len[0], (char *)data, sizeof (*data));

memcpy(songinfo->songname, data->songname + 1, 42);
memcpy(songinfo->composer, data->composer + 1, 42);
Expand Down
2 changes: 1 addition & 1 deletion src/a2m-v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ class Ca2mv2Player : public CPlayer
void init_songdata();

// Loader
void a2t_depack(char *src, int srcsize, char *dst);
void a2t_depack(char *src, int srcsize, char *dst, int dstsize);
int a2t_read_varheader(char *blockptr);
void instrument_import_v1_8(int ins, tINSTR_DATA_V1_8 *instr_s);
void instrument_import(int ins, tINSTR_DATA *instr_s);
Expand Down
32 changes: 30 additions & 2 deletions src/depack.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
struct APDSTATE {
const unsigned char *source;
unsigned char *destination;
unsigned int destlen;
unsigned int tag;
unsigned int bitcount;
};
Expand Down Expand Up @@ -53,7 +54,7 @@ static unsigned int aP_getgamma(struct APDSTATE *ud)
return result;
}

unsigned int aP_depack(const void *source, void *destination)
unsigned int aP_depack(const void *source, void *destination, int srcsize, int dstsize)
{
struct APDSTATE ud;
unsigned int offs, len, R0/*, LWM*/;
Expand All @@ -62,14 +63,19 @@ unsigned int aP_depack(const void *source, void *destination)

ud.source = (const unsigned char *) source;
ud.destination = (unsigned char *) destination;
ud.destlen = 0;
ud.bitcount = 0;

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

/* first byte verbatim */
if (!srcsize) return ud.destlen;
if (!dstsize) return ud.destlen;
*ud.destination++ = *ud.source++;
srcsize--;
dstsize--; ud.destlen++;

/* main decompression loop */
while (!done) {
Expand All @@ -83,25 +89,35 @@ unsigned int aP_depack(const void *source, void *destination)
}

if (offs) {
if (!dstsize) return ud.destlen;
if (offs > ud.destlen) return ud.destlen;
*ud.destination = *(ud.destination - offs);
ud.destination++;
dstsize--; ud.destlen++;
}
else {
if (!dstsize) return ud.destlen;
*ud.destination++ = 0x00;
dstsize--; ud.destlen++;
}

//LWM = 0;
} else {
if (!srcsize) return ud.destlen;
offs = *ud.source++;
srcsize--;

len = 2 + (offs & 0x0001);

offs >>= 1;

if (offs) {
for (; len; len--) {
if (!dstsize) return ud.destlen;
if (offs > ud.destlen) return ud.destlen;
*ud.destination = *(ud.destination - offs);
ud.destination++;
dstsize--; ud.destlen++;
}
}
else {
Expand All @@ -122,8 +138,11 @@ unsigned int aP_depack(const void *source, void *destination)
len = aP_getgamma(&ud);

for (; len; len--) {
if (!dstsize) return ud.destlen;
if (offs > ud.destlen) return ud.destlen;
*ud.destination = *(ud.destination - offs);
ud.destination++;
dstsize--; ud.destlen++;
}
} else {
/*
Expand All @@ -138,7 +157,9 @@ unsigned int aP_depack(const void *source, void *destination)
//-------------------

offs <<= 8;
if (!srcsize) return ud.destlen;
offs += *ud.source++;
srcsize--;

len = aP_getgamma(&ud);

Expand All @@ -153,8 +174,11 @@ unsigned int aP_depack(const void *source, void *destination)
}

for (; len; len--) {
if (!dstsize) return ud.destlen;
if (offs > ud.destlen) return ud.destlen;
*ud.destination = *(ud.destination - offs);
ud.destination++;
dstsize--; ud.destlen++;
}

R0 = offs;
Expand All @@ -164,10 +188,14 @@ unsigned int aP_depack(const void *source, void *destination)
}
}
else {
if (!srcsize) return ud.destlen;
if (!dstsize) return ud.destlen;
*ud.destination++ = *ud.source++;
srcsize--;
dstsize--; ud.destlen++;
//LWM = 0;
}
}

return (unsigned int) (ud.destination - (unsigned char *) destination);
return ud.destlen;
}
2 changes: 1 addition & 1 deletion src/depack.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" {
#endif

/* function prototype */
unsigned int aP_depack(const void *source, void *destination);
unsigned int aP_depack(const void *source, void *destination, int srcsize, int dstsize);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
14 changes: 11 additions & 3 deletions src/sixpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void decode();
static unsigned short ibitcount, ibitbuffer, ibufcount, obufcount, input_size,
output_size, leftc[MAXCHAR+1], rghtc[MAXCHAR+1],
dad[TWICEMAX+1], freq[TWICEMAX+1], *wdbuf;
static unsigned int obufsize;
static unsigned char *obuf, *buf;

void inittree()
Expand Down Expand Up @@ -133,6 +134,8 @@ unsigned short inputcode(unsigned short bits)
if(!ibitcount) {
if(ibitcount == MAXBUF)
ibufcount = 0;
if (ibufcount >= input_size)
return 0; /* overflow input buffer */
ibitbuffer = wdbuf[ibufcount];
ibufcount++;
ibitcount = 15;
Expand All @@ -155,6 +158,8 @@ unsigned short uncompress()
if(!ibitcount) {
if(ibufcount == MAXBUF)
ibufcount = 0;
if (ibufcount >= input_size)
return TERMINATE; /* overflow input buffer */
ibitbuffer = wdbuf[ibufcount];
ibufcount++;
ibitcount = 15;
Expand Down Expand Up @@ -182,7 +187,8 @@ void decode()

while(c != TERMINATE) {
if(c < 256) {
obuf[obufcount] = (unsigned char)c;
if (obufcount < obufsize) /* check for overflow if output buffer */
obuf[obufcount] = (unsigned char)c;
obufcount++;
if(obufcount == MAXBUF) {
output_size = MAXBUF;
Expand All @@ -205,7 +211,8 @@ void decode()
k += MAXSIZE;

for(i=0;i<=len-1;i++) {
obuf[obufcount] = buf[k];
if (obufcount < obufsize) /* check for overflow if output buffer */
obuf[obufcount] = buf[k];
obufcount++;
if(obufcount == MAXBUF) {
output_size = MAXBUF;
Expand All @@ -228,7 +235,7 @@ void decode()
}

unsigned short sixdepak(unsigned short *source, unsigned char *dest,
unsigned short size)
unsigned short size, unsigned int destsize)
{
if((unsigned int)size + 4096 > MAXBUF)
return 0;
Expand All @@ -237,6 +244,7 @@ unsigned short sixdepak(unsigned short *source, unsigned char *dest,
input_size = size;
ibitcount = 0; ibitbuffer = 0;
obufcount = 0; ibufcount = 0;
obufsize = destsize;
wdbuf = source; obuf = dest;

decode();
Expand Down
2 changes: 1 addition & 1 deletion src/sixpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern "C" {
#endif

unsigned short sixdepak(unsigned short *source, unsigned char *dest,
unsigned short size);
unsigned short size, unsigned int destsize);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
16 changes: 12 additions & 4 deletions src/unlzh.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,17 @@ int unlzh(in, out)
}
#endif

int LZH_decompress(char *source, char *dest, int size)
int LZH_decompress(char *source, char *dest, int source_size, int dest_size)
{
unsigned char *ptr;
int size_temp;
char ultra;
uint32_t size_unpacked = 0;
int size;

input_buffer = (unsigned char *)source;
input_buffer_idx = 0;
input_buffer_size = size;
input_buffer_size = source_size;

ultra = input_buffer[input_buffer_idx++] & 1;

Expand All @@ -439,15 +440,22 @@ int LZH_decompress(char *source, char *dest, int size)

decode_start();
size = size_unpacked;
while (size > 0) {
while ((size > 0) && dest_size) {
if (size > (int)DIC_SIZE) {
size_temp = DIC_SIZE;
} else {
size_temp = size;
}

decode(size_temp, ptr);
write_buf(ptr, size_temp);
if (dest_size >= size_temp)
{
write_buf(ptr, size_temp);
dest_size -= size_temp;
} else {
write_buf(ptr, dest_size);
dest_size = 0;
}
size -= size_temp;
}

Expand Down
2 changes: 1 addition & 1 deletion src/unlzh.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
extern "C" {
#endif

int LZH_decompress(char *source, char *dest, int size);
int LZH_decompress(char *source, char *dest, int source_size, int dest_size);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
Loading