Skip to content

Commit 9de9349

Browse files
committed
foundation: rewrite inccompress statement to use less expensive file IO
1 parent 6844d4a commit 9de9349

1 file changed

Lines changed: 38 additions & 45 deletions

File tree

statements.c

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6937,7 +6937,6 @@ void decompress (char **statement)
69376937
printf (" jsr lzsa1_unpack\n");
69386938
}
69396939

6940-
69416940
void inccompress (char **statement)
69426941
{
69436942
// creates compresses data from a data file.
@@ -6951,8 +6950,8 @@ void inccompress (char **statement)
69516950
char datalabelname[256];
69526951
int t;
69536952

6954-
unsigned char *lzsa_buf_uncomp, *lzsa_buf_comp;
6955-
long uncompsize, compsize, position;
6953+
unsigned char *lzsa_buf_comp;
6954+
long uncompsize, compsize;
69566955

69576956
assertminimumargs (statement, "inccompress", 1);
69586957

@@ -6982,73 +6981,67 @@ void inccompress (char **statement)
69826981

69836982
backupthisfile(statement[2]);
69846983

6985-
char magic[6];
6986-
FILE *fp = fopen (statement[2], "rb");
6987-
if(fp==NULL)
6988-
prerror ("couldn't open %s",statement[2]);
6984+
FILE *fp = fopen(statement[2], "rb");
6985+
if (fp == NULL)
6986+
prerror("couldn't open %s", statement[2]);
69896987

6990-
// setup and scan for the 'RMT4' signature in the file
6991-
memset(magic,0,6);
6992-
int c;
6993-
while ((c = fgetc(fp)) != EOF)
6994-
{
6995-
magic[0]=magic[1];
6996-
magic[1]=magic[2];
6997-
magic[2]=magic[3];
6998-
magic[3]=c;
6999-
if (!strncmp(magic,"RMT4",4))
7000-
break;
7001-
}
7002-
if (c == EOF) // it's not an RMT4, so rewind to the start.
7003-
rewind(fp);
7004-
else
7005-
fseek(fp,-4,SEEK_CUR); // rewind to the start of the RMT4 header.
7006-
7007-
// Get the size of the file, excluding any bytes we skipped to reach
7008-
// the RMT4 header...
7009-
position=ftell(fp);
7010-
fseek(fp,0,SEEK_END);
7011-
uncompsize=ftell(fp)-position;
7012-
fseek(fp,position,SEEK_SET); // and then restore the stream position
7013-
7014-
lzsa_buf_uncomp = malloc(uncompsize);
7015-
if(lzsa_buf_uncomp==NULL)
7016-
prerror ("couldn't allocate memory to read %s",statement[2]);
7017-
if(fread(lzsa_buf_uncomp,1,uncompsize,fp)!=uncompsize)
7018-
prerror ("couldn't read %s into memory",statement[2]);
7019-
fclose(fp);
6988+
// 1. Get file size and read entire file into memory
6989+
fseek(fp, 0, SEEK_END);
6990+
long file_size = ftell(fp);
6991+
rewind(fp);
6992+
6993+
unsigned char *file_buffer = malloc(file_size);
6994+
if (file_buffer == NULL)
6995+
prerror("couldn't allocate memory to read %s", statement[2]);
6996+
6997+
if (fread(file_buffer, 1, file_size, fp) != file_size)
6998+
prerror("couldn't read %s into memory", statement[2]);
6999+
fclose(fp);
7000+
7001+
// 2. Scan for 'RMT4' signature in memory
7002+
unsigned char *data_to_compress = file_buffer;
7003+
uncompsize = file_size;
7004+
7005+
for (long i = 0; i < file_size - 4; i++) {
7006+
if (memcmp(file_buffer + i, "RMT4", 4) == 0) {
7007+
data_to_compress = file_buffer + i;
7008+
uncompsize = file_size - i;
7009+
break;
7010+
}
7011+
}
70207012

70217013
// allocate the destination buffer
7022-
lzsa_buf_comp = malloc(uncompsize*2);
7014+
lzsa_buf_comp = malloc(uncompsize*2);
70237015
if(lzsa_buf_comp==NULL)
70247016
prerror ("couldn't allocate memory to compress %s",statement[2]);
70257017

70267018
// call the lzsa library in-memory compression routine
70277019
// see libs.h for argument details
7028-
compsize=lzsa_compress_inmem(lzsa_buf_uncomp,lzsa_buf_comp,uncompsize,uncompsize*2,(LZSA_FLAG_RAW_BLOCK|LZSA_FLAG_FAVOR_RATIO),3,1);
7020+
compsize=lzsa_compress_inmem(data_to_compress,lzsa_buf_comp,uncompsize,uncompsize*2,(LZSA_FLAG_RAW_BLOCK|LZSA_FLAG_FAVOR_RATIO),3,1);
70297021

70307022
if (compsize<1)
70317023
prerror ("liblzsa couldn't compress %s",statement[2]);
70327024

70337025
if (compsize>uncompsize)
70347026
prwarn ("compressing %s wastes more rom than the uncompressed file",statement[2]);
70357027

7036-
for(t=0;t<compsize;t++)
7028+
for(long i=0;i<compsize;i++)
70377029
{
7038-
if (t%16>0)
7030+
if (i%16>0)
70397031
printf(",");
7040-
if (t%16==0)
7032+
if (i%16==0)
70417033
printf("\n .byte ");
7042-
printf("$%02x",lzsa_buf_comp[t]);
7034+
printf("$%02x",lzsa_buf_comp[i]);
70437035
}
70447036

70457037
printf("\n");
70467038
prout (" %s compressed, %ld->%ld bytes, %02.2f ratio\n",statement[2],uncompsize,compsize,((float)uncompsize/(float)compsize));
7047-
free(lzsa_buf_uncomp);
7039+
7040+
// Free the allocated memory
7041+
free(file_buffer);
70487042
free(lzsa_buf_comp);
70497043
}
70507044

7051-
70527045
void speechdata (char **statement)
70537046
{
70547047
char data[501], word[501], wordphonemes[501];

0 commit comments

Comments
 (0)