summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cc4group.c76
1 files changed, 46 insertions, 30 deletions
diff --git a/src/cc4group.c b/src/cc4group.c
index 25e354d..0bc1029 100644
--- a/src/cc4group.c
+++ b/src/cc4group.c
@@ -46,6 +46,9 @@ static bool cc4group_getEntryData(CC4Group* const this, const char* const entryP
#define C4GroupMagic1 0x1e
#define C4GroupMagic2 0x8c
+#define CC4GROUP_UNEXPECTED_EOD -100
+#define CC4GROUP_MEM_ERROR -101
+
typedef enum {
Take,
Copy,
@@ -136,10 +139,37 @@ static char* cc4group_strerrorFormatter(int32_t const code, const char* const me
static char* cc4group_zerrorFormatter(int32_t const code, const char* const method, const char* const causer, void* const data)
{
+ int storedErrno = (size_t)data;
// restore errno for the case that code is Z_ERRNO
- errno = (size_t)data;
+ if(code == Z_ERRNO)
+ {
+ errno = storedErrno;
+ }
+
char* message;
- asprintf(&message, "%s: %s: %s (%d)", method, causer, zError(code), code);
+ const char* errorString;
+
+ if(code == CC4GROUP_MEM_ERROR)
+ {
+ errorString = "";
+ asprintf(&message, "%s: %s: Can't allocate memory for copying the input data: %s (%d)", method, causer, strerror(storedErrno), storedErrno);
+ return message;
+ }
+
+ if(code == CC4GROUP_UNEXPECTED_EOD)
+ {
+ errorString = "Unexpected end of group data";
+ }
+ else if(code == Z_OK)
+ {
+ errorString = "Z_OK";
+ }
+ else
+ {
+ errorString = zError(code);
+ }
+
+ asprintf(&message, "%s: %s: %s (%d)", method, causer, errorString, code);
return message;
}
@@ -371,7 +401,7 @@ static bool cc4group_inflateFillOutput(z_stream* const strm, CC4Group_ReadCallba
{
if(*eof)
{
- *inflateRet = Z_OK;
+ *inflateRet = CC4GROUP_UNEXPECTED_EOD;
return false;
}
@@ -389,7 +419,11 @@ static bool cc4group_inflateFillOutput(z_stream* const strm, CC4Group_ReadCallba
continue;
}
- cc4group_applyMemoryManagementStart(memoryManagement, lastData, readSize);
+ if(!cc4group_applyMemoryManagementStart(memoryManagement, lastData, readSize))
+ {
+ *inflateRet = CC4GROUP_MEM_ERROR;
+ return false;
+ }
strm->avail_in = readSize;
strm->next_in = (uint8_t*)*lastData; // I don't know why this must be non-const; anyway a read-only mapped input doesn't segfault. so it seems to be used read-only
@@ -462,8 +496,11 @@ static bool cc4group_uncompressGroup(CC4Group* const this, CC4Group_ReadCallback
continue;
}
- // TODO: check for success at every call of this
- cc4group_applyMemoryManagementStart(memoryManagement, &readData, readSize);
+ if(!cc4group_applyMemoryManagementStart(memoryManagement, &readData, readSize))
+ {
+ SET_ZERROR_ERROR("reading the group magic", CC4GROUP_MEM_ERROR);
+ goto ret;
+ }
size_t newTotalReadSize = totalReadSize + readSize;
if(totalReadSize < 1 && newTotalReadSize > 0)
@@ -538,14 +575,7 @@ static bool cc4group_uncompressGroup(CC4Group* const this, CC4Group_ReadCallback
if(!cc4group_inflateFillOutput(&strm, readCallback, callbackArg, memoryManagement, &eof, &readData, &ret))
{
- if(eof && ret == Z_BUF_ERROR)
- {
- SET_EOD_ERROR("inflating group header");
- }
- else
- {
- SET_ZERROR_ERROR("inflate: inflating the group header", ret);
- }
+ SET_ZERROR_ERROR("inflate: inflating the group header", ret);
goto ret;
}
@@ -598,14 +628,7 @@ static bool cc4group_uncompressGroup(CC4Group* const this, CC4Group_ReadCallback
if(!cc4group_inflateFillOutput(&strm, readCallback, callbackArg, memoryManagement, &eof, &readData, &ret))
{
- if(eof && ret == Z_BUF_ERROR)
- {
- SET_EOD_ERROR("inflating the group entry dictionary");
- }
- else
- {
- SET_ZERROR_ERROR("inflate: inflating the group entry dictionary", ret);
- }
+ SET_ZERROR_ERROR("inflate: inflating the group entry dictionary", ret);
goto ret;
}
@@ -626,14 +649,7 @@ static bool cc4group_uncompressGroup(CC4Group* const this, CC4Group_ReadCallback
if(!cc4group_inflateFillOutput(&strm, readCallback, callbackArg, memoryManagement, &eof, &readData, &ret))
{
- if(eof && ret == Z_BUF_ERROR)
- {
- SET_EOD_ERROR("inflating the group contents");
- }
- else
- {
- SET_ZERROR_ERROR("inflate: inflating the group contents", ret);
- }
+ SET_ZERROR_ERROR("inflate: inflating the group contents", ret);
goto ret;
}