Skip to content

Commit

Permalink
Merge pull request #29 from tobozo/fix-issue-27
Browse files Browse the repository at this point in the history
More uzlib Error handling
  • Loading branch information
tobozo authored Mar 18, 2021
2 parents 0c98a84 + 8904a84 commit 4ba4e11
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Usage
//#define DEST_FS_USES_SD_MMC
#define DEST_FS_USES_LITTLEFS
#include <ESP32-targz.h>
// filesystem object will be available as "tarGzFs"
// filesystem object will be available as "tarGzFS"
```


Expand All @@ -59,7 +59,7 @@ Extract content from `.gz` file
```C

// mount spiffs (or any other filesystem)
tarGzFs.begin();
tarGzFS.begin();

GzUnpacker *GZUnpacker = new GzUnpacker();

Expand All @@ -74,7 +74,7 @@ Extract content from `.gz` file
}

// expand another file
if( ! gzExpander(tarGzFs, "/blah.gz", tarGzFs, "/blah.jpg") ) {
if( ! gzExpander(tarGzFS, "/blah.gz", tarGzFS, "/blah.jpg") ) {
Serial.printf("operation failed with return code #%d", GZUnpacker->tarGzGetError() );
}

Expand All @@ -88,7 +88,7 @@ Expand contents from `.tar` file to `/tmp` folder
```C
// mount spiffs (or any other filesystem)
tarGzFs.begin();
tarGzFS.begin();
TarUnpacker *TARUnpacker = new TarUnpacker();
Expand All @@ -114,7 +114,7 @@ Expand contents from `.tar.gz` to `/tmp` folder
```C

// mount spiffs (or any other filesystem)
tarGzFs.begin();
tarGzFS.begin();

TarGzUnpacker *TARGZUnpacker = new TarGzUnpacker();

Expand Down Expand Up @@ -147,7 +147,7 @@ Flash the ESP with contents from `.gz` file
```C
// mount spiffs (or any other filesystem)
tarGzFs.begin();
tarGzFS.begin();
GzUnpacker *GZUnpacker = new GzUnpacker();
Expand Down
4 changes: 1 addition & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
name=ESP32-targz
version=1.0.2-beta
version=1.0.3-beta
author=tobozo <[email protected]>
maintainer=tobozo <[email protected]>
sentence=A library to unpack/uncompress tar, gz, and tar.gz files on ESP32 and ESP8266
paragraph=ESP32-targz is a wrapper for TinyUntar and uzLib to use with fs::FS. It supports streaming and will use ~36KB ram.
category=Data Processing
url=https://github.com/tobozo/ESP32-targz/
architectures=esp32,esp8266


6 changes: 6 additions & 0 deletions src/ESP32-targz-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ int GzUnpacker::gzUncompress( bool isupdate, bool stream_to_tar, bool use_dict,
uzLibDecompressor.readSourceByte = gzReadSourceByte;
uzLibDecompressor.destSize = 1;
uzLibDecompressor.log = targzPrintLoggerCallback;
uzLibDecompressor.readSourceErrors = 0;

res = GZ::uzlib_gzip_parse_header(&uzLibDecompressor);
if (res != TINF_OK) {
Expand Down Expand Up @@ -1152,6 +1153,11 @@ int GzUnpacker::gzUncompress( bool isupdate, bool stream_to_tar, bool use_dict,
} while ( res == TINF_OK );

if (res != TINF_DONE) {
if( uzLibDecompressor.readSourceErrors > 0 ) {
free( output_buffer );
gzExpanderCleanup();
return ESP32_TARGZ_STREAM_ERROR;
}
log_w("[GZ WARNING] uzlib_uncompress_chksum return code=%d, premature end at position %d while %d bytes left", res, output_position, (int)uzlib_bytesleft);
}

Expand Down
15 changes: 12 additions & 3 deletions src/uzlib/tinflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ unsigned char uzlib_get_byte(TINF_DATA *d)
unsigned char out;
//d->log("Using readsource\n");
int ret = d->readSourceByte(d, &out);
if(! ret ) {
if( ret == -1 ) {
d->readSourceErrors++;
d->log("uzlib_get_byte/readSourceByte Errors: %d\n", d->readSourceErrors );
// TODO: handle read errors !
}
return out;
Expand Down Expand Up @@ -307,6 +309,8 @@ static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t)
sum += t->table[len];
cur -= t->table[len];

if( d->readSourceErrors > 0 ) return TINF_DATA_ERROR;

} while (cur >= 0);

sum += cur;
Expand All @@ -316,6 +320,8 @@ static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t)
}
#endif



return t->trans[sum];
}

Expand Down Expand Up @@ -612,6 +618,7 @@ void uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen)
d->dict_ring = dict;
d->dict_idx = 0;
d->curlen = 0;
d->readSourceErrors = 0;
}

/* inflate next output bytes from compressed stream */
Expand All @@ -622,7 +629,7 @@ int uzlib_uncompress(TINF_DATA *d)

/* start a new block */
if (d->btype == -1) {
next_blk:
next_blk:
/* read final block flag */
d->bfinal = tinf_getbit(d);
/* read block type (2 bits) */
Expand Down Expand Up @@ -666,14 +673,16 @@ int uzlib_uncompress(TINF_DATA *d)

if (res == TINF_DONE && !d->bfinal) {
/* the block has ended (without producing more data), but we
can't return without data, so start procesing next block */
can't return without data, so start processing next block */
goto next_blk;
}

if (res != TINF_OK) {
return res;
}

if( d->readSourceErrors > 0 ) return TINF_DATA_ERROR;

} while (--d->destRemaining);

return TINF_OK;
Expand Down
2 changes: 2 additions & 0 deletions src/uzlib/tinfzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ int uzlib_zlib_parse_header(TINF_DATA *d)
cmf = uzlib_get_byte(d);
flg = uzlib_get_byte(d);

//if( d->readSourceErrors > 0 ) return TINF_DATA_ERROR;

/* -- check format -- */

/* check checksum */
Expand Down
9 changes: 4 additions & 5 deletions src/uzlib/uzlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ typedef struct TINF_DATA {
source_limit fields, thus allowing for buffered operation. */
int (*source_read_cb)(struct TINF_DATA *uncomp);
unsigned int (*readSourceByte)(struct TINF_DATA *data, unsigned char *out);
//unsigned int readSourceErrors = 0;
unsigned int readSourceErrors;

void (*log)( const char* format, ... );

Expand All @@ -97,11 +99,8 @@ typedef struct TINF_DATA {
reading from the output stream, rather than assuming
'dest' contains the entire output stream in memory
*/
unsigned int (*readDestByte)(int offset, unsigned char *out);
unsigned int (*writeDestWord)(unsigned long data);



unsigned int (*readDestByte)(int offset, unsigned char *out);
unsigned int (*writeDestWord)(unsigned long data);

/* Accumulating checksum */
unsigned int checksum;
Expand Down

0 comments on commit 4ba4e11

Please sign in to comment.