Blame Xml/config_compression.c

Packit 857059
/* BEGIN_ICS_COPYRIGHT5 ****************************************
Packit 857059
Packit Service 3f7ca0
Copyright (c) 2015-2020, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
      documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
 * ** END_ICS_COPYRIGHT5   ****************************************/
Packit 857059
/* [ICS VERSION STRING: unknown] */
Packit 857059
Packit 857059
/* THIS IS USED ONLY IN STL1 AND LATER */
Packit 857059
Packit 857059
#include "vxWorks.h"
Packit 857059
#include "stdio.h"
Packit 857059
#include "stdlib.h"
Packit 857059
#include "string.h"
Packit 857059
#include "taskLib.h"
Packit 857059
#include "time.h"
Packit 857059
#include "tms/common/rdHelper.h"
Packit 857059
#include "tms/idb/icsUiConfigMib.h"
Packit 857059
#include "tms/common/usrSecLib.h"
Packit 857059
#include "bspcommon/h/sysPrintf.h"
Packit 857059
#include "bspcommon/h/icsBspUtil.h"
Packit 857059
#include "bspcommon/h/sysFlash.h"
Packit 857059
Packit 857059
#include "config_compression.h"
Packit 857059
Packit 857059
extern int UiUtil_GetLocalTime ();
Packit 857059
Packit 857059
static int debug_comp;
Packit 857059
Packit 857059
#define COPYBUFFER_SIZE			2048
Packit 857059
#define UNCOMPRESSED_ARRAY_SIZE 2048
Packit 857059
#define	COMPRESSED_ARRAY_SIZE	2048
Packit 857059
Packit 857059
#define SCP_LOG( fmt, func, args... ) \
Packit 857059
	do{ \
Packit 857059
		if( debug_comp ) { \
Packit 857059
			sysPrintf("task=%s %s : " fmt "\n", taskName(taskIdSelf()), func, ## args ); \
Packit 857059
		} \
Packit 857059
	} while(0)
Packit 857059
Packit 857059
// unfortunately we can't #include zlib.h or we have multiple definitions for crc32
Packit 857059
#undef compress
Packit 857059
extern int compress(char *dest, int *destLen, char *src, int sourceLen);
Packit 857059
Packit 857059
typedef struct compBlockHeader {
Packit 857059
	int compressedBytes;
Packit 857059
	int uncompressedBytes;
Packit 857059
} compBlockHeader_t;
Packit 857059
Packit 857059
#define COMP_VERSION 0
Packit 857059
Packit 857059
typedef struct compFileHeader {
Packit 857059
	int version;
Packit 857059
	time_t creationTime;
Packit 857059
} compFileHeader_t;
Packit 857059
Packit 857059
typedef struct {
Packit 857059
	FILE *fdin;
Packit 857059
	int leftOverBytes;
Packit 857059
	int endOfCompressedFile;
Packit 857059
	char uncompressedBytesArray[UNCOMPRESSED_ARRAY_SIZE];
Packit 857059
	char compressedBytesArray[COMPRESSED_ARRAY_SIZE];
Packit 857059
} compFile_t;
Packit 857059
Packit 857059
// TEST CODE START
Packit 857059
//SCP_LOG("ATTEMPTING TEST TO TEST.XML", __FUNCTION__);
Packit 857059
//	FILE *fileIn = openUncompressedFile(outfilename);
Packit 857059
//	FILE *fileOut = fopen("/firmware/test.xml", "wb");
Packit 857059
//	int totalBytes = 0;
Packit 857059
//	while (!feof(fileIn)) {
Packit 857059
//		char testBuffer[2049];
Packit 857059
//		int bytesRead = readUncompressedBytes(fileIn, testBuffer, sizeof(testBuffer));
Packit 857059
//		totalBytes += bytesRead;
Packit 857059
//		SCP_LOG("READ %d BYTES UNCOMPRESSED", bytesRead, __FUNCTION__);
Packit 857059
//		fwrite((void*)testBuffer, 1, bytesRead, fileOut);
Packit 857059
//		if (bytesRead < sizeof(testBuffer))
Packit 857059
//			 break;
Packit 857059
//	};
Packit 857059
// 	SCP_LOG("TOTAL BYTES %d", totalBytes, __FUNCTION__);
Packit 857059
//	fclose(fileOut);
Packit 857059
//	closeUncompressedFile(fileIn);
Packit 857059
// TES TCODE END
Packit 857059
Packit 857059
Packit 857059
int copyFile(char *src, char*dst, int compressFlag, long *compressedFileSize) {
Packit 857059
	// compress = 1 means inflate
Packit 857059
	// compress = 0 means just copy
Packit 857059
	// compress = -1 means deflate
Packit 857059
	time_t timeStamp;
Packit 857059
	char *copyBuffer1 = NULL;
Packit 857059
	char *copyBuffer2 = NULL;
Packit 857059
	char *uncompressedBytesArray = NULL;
Packit 857059
	char *compressedBytesArray = NULL;
Packit 857059
Packit 857059
	copyBuffer1 = malloc(COPYBUFFER_SIZE);
Packit 857059
	copyBuffer2 = malloc(COPYBUFFER_SIZE);
Packit 857059
	uncompressedBytesArray = malloc(UNCOMPRESSED_ARRAY_SIZE);
Packit 857059
	compressedBytesArray = malloc(COMPRESSED_ARRAY_SIZE);
Packit 857059
Packit 857059
	if ((copyBuffer1 == NULL) || (copyBuffer2 == NULL) ||
Packit 857059
			(uncompressedBytesArray == NULL) || (compressedBytesArray == NULL)) {
Packit 857059
		if (copyBuffer1 != NULL)
Packit 857059
			free(copyBuffer1);
Packit 857059
		if (copyBuffer2 != NULL)
Packit 857059
			free(copyBuffer2);
Packit 857059
		if (uncompressedBytesArray != NULL)
Packit 857059
			free(uncompressedBytesArray);
Packit 857059
		if (compressedBytesArray != NULL)
Packit 857059
			free(compressedBytesArray);
Packit 857059
		SCP_LOG("out of memory in copyFile", __FUNCTION__);
Packit 857059
	}
Packit 857059
	SCP_LOG("CopyFile src:%s, dst:%s, compressFlag:%d", __FUNCTION__, src, dst, compressFlag);
Packit 857059
	size_t bufsize = COPYBUFFER_SIZE;
Packit 857059
Packit 857059
	int retVal = 0;
Packit 857059
	int didFileHeader = 0;
Packit 857059
Packit 857059
	FILE *fdout;
Packit 857059
Packit 857059
	if (compressedFileSize != NULL)
Packit 857059
		*compressedFileSize = 0;
Packit 857059
Packit 857059
	fdout = fopen(dst, "w");
Packit 857059
	// if we are out of space, try removing the original first and reopening the file
Packit 857059
	if (fdout == NULL) {
Packit 857059
		FileRemove(dst);
Packit 857059
		fdout = fopen(dst, "w");
Packit 857059
	}
Packit 857059
	if (fdout != NULL) {
Packit 857059
		SCP_LOG("OPENED! output file %s", __FUNCTION__, dst);
Packit 857059
		FILE *fdin;
Packit 857059
		if (NULL != (fdin = fopen(src, "r"))) {
Packit 857059
			SCP_LOG("OPENED! input %s", __FUNCTION__, src);
Packit 857059
			while (!feof(fdin)) {
Packit 857059
				int bytes = 0;
Packit 857059
				if (compressFlag == 0) {
Packit 857059
					bytes = fread((void*)copyBuffer1, 1, bufsize, fdin);
Packit 857059
					SCP_LOG("READ %d bytes when tried %d bytes", __FUNCTION__, bytes, bufsize);
Packit 857059
				}
Packit 857059
				if (compressFlag < 0) {
Packit 857059
					if (!didFileHeader) {
Packit 857059
						compFileHeader_t header;
Packit 857059
						if(UiUtil_GetLocalTime( &timeStamp) == 0){
Packit 857059
							header.creationTime = timeStamp;
Packit 857059
						}
Packit 857059
						else {
Packit 857059
							header.creationTime = time(NULL);
Packit 857059
						}
Packit 857059
						header.version = COMP_VERSION;
Packit 857059
						int bout = fwrite((void*)&header, 1, sizeof(compFileHeader_t), fdout);
Packit 857059
						if (bout != sizeof(compFileHeader_t)) {
Packit 857059
							SCP_LOG("Tried to write %d bytes to %s and only wrote %d", __FUNCTION__, sizeof(compFileHeader_t), dst, bout);
Packit 857059
							retVal = -1;
Packit 857059
							break;
Packit 857059
						} else {
Packit 857059
							SCP_LOG("Wrote file header to %s", __FUNCTION__, dst);
Packit 857059
						}
Packit 857059
						didFileHeader = 1;
Packit 857059
					}
Packit 857059
Packit 857059
					bytes = fread((void*)copyBuffer1, 1, bufsize, fdin);
Packit 857059
					SCP_LOG("READ %d bytes when tried %d bytes", __FUNCTION__, bytes, bufsize);
Packit 857059
					int compLen = bytes;
Packit 857059
					compress(copyBuffer2, &compLen, copyBuffer1, bytes);
Packit 857059
					SCP_LOG("Compressed %d bytes into %d bytes", __FUNCTION__, bytes, compLen);
Packit 857059
					if (bytes == compLen) {
Packit 857059
						SCP_LOG("No compression needed for this block.", __FUNCTION__);
Packit 857059
						memcpy(copyBuffer2, copyBuffer1, bytes);
Packit 857059
					}
Packit 857059
					compBlockHeader_t *header = (compBlockHeader_t*)copyBuffer1;
Packit 857059
					header->compressedBytes = compLen;
Packit 857059
					header->uncompressedBytes = bytes;
Packit 857059
					char *startOfData = copyBuffer1+sizeof(compBlockHeader_t);
Packit 857059
					memcpy(startOfData, copyBuffer2, compLen);
Packit 857059
					bytes = sizeof(compBlockHeader_t)+compLen;
Packit 857059
					if (compressedFileSize != NULL) {
Packit 857059
						*compressedFileSize += (long)bytes;;
Packit 857059
					}
Packit 857059
				}
Packit 857059
				if (compressFlag > 0) {
Packit 857059
					if (!didFileHeader) {
Packit 857059
						compFileHeader_t header;
Packit 857059
						if (fread((void*)&header, 1, sizeof(compFileHeader_t), fdin) != sizeof(compFileHeader_t)) {
Packit 857059
							SCP_LOG("Tried to READ %d bytes of header and didn't", __FUNCTION__, sizeof(compFileHeader_t));
Packit 857059
							break;
Packit 857059
						}
Packit 857059
						if (header.version != COMP_VERSION) {
Packit Service 3f7ca0
							SCP_LOG("Unsuported compress file version %d", __FUNCTION__, header.version);
Packit 857059
							retVal = -1;
Packit 857059
							break;
Packit 857059
						}
Packit 857059
						didFileHeader = 1;
Packit 857059
					}
Packit 857059
					compBlockHeader_t header;
Packit 857059
					if (fread((void*)&header, 1, sizeof(compBlockHeader_t), fdin) != sizeof(compBlockHeader_t)) {
Packit 857059
						SCP_LOG("Tried to READ %d bytes of header and didn't", __FUNCTION__, sizeof(compBlockHeader_t));
Packit 857059
						break;
Packit 857059
					}
Packit 857059
					SCP_LOG("READ %d bytes of header. %d compressed bytes and %d uncompressed bytes", __FUNCTION__, sizeof(compBlockHeader_t), header.compressedBytes, header.uncompressedBytes);
Packit 857059
					if ((header.compressedBytes == 0) && (header.uncompressedBytes == 0)) {
Packit 857059
						SCP_LOG("Tail header read. EOF!", __FUNCTION__);
Packit 857059
						break;
Packit 857059
					}
Packit 857059
					if ((header.compressedBytes > COMPRESSED_ARRAY_SIZE) || (header.compressedBytes <= 0)) {
Packit 857059
						SCP_LOG("Compressed bytes should be 0-%d and it's %d", __FUNCTION__, COMPRESSED_ARRAY_SIZE, header.compressedBytes);
Packit 857059
						retVal = -1;
Packit 857059
						break;
Packit 857059
					}
Packit 857059
					if ((header.uncompressedBytes > UNCOMPRESSED_ARRAY_SIZE) || (header.uncompressedBytes <= 0)) {
Packit 857059
						SCP_LOG("Uncompressed bytes should be 1-%d and it's %d", __FUNCTION__, UNCOMPRESSED_ARRAY_SIZE, header.uncompressedBytes);
Packit 857059
						retVal = -1;
Packit 857059
						break;
Packit 857059
					}
Packit 857059
					if (fread(copyBuffer1, 1, header.compressedBytes, fdin) != header.compressedBytes) {
Packit 857059
						SCP_LOG("Tried to READ %d bytes of data and didn't", __FUNCTION__, header.compressedBytes);
Packit 857059
						retVal = -1;
Packit 857059
						break;
Packit 857059
					}
Packit 857059
					SCP_LOG("READ %d bytes of data.", __FUNCTION__, header.compressedBytes);
Packit 857059
					if (header.uncompressedBytes > header.compressedBytes) {
Packit 857059
							SCP_LOG("About to decompress %d bytes", __FUNCTION__, header.compressedBytes);
Packit 857059
						int uncompBytes = inflater(copyBuffer2, COPYBUFFER_SIZE, copyBuffer1, header.compressedBytes);
Packit 857059
						SCP_LOG("Decompressed %d bytes into %d bytes", __FUNCTION__, header.compressedBytes, uncompBytes);
Packit 857059
						memcpy(copyBuffer1, copyBuffer2, uncompBytes);
Packit 857059
						SCP_LOG("DECOMP:%2048s", __FUNCTION__, dst);
Packit 857059
						bytes = uncompBytes;
Packit 857059
					} else {
Packit 857059
						SCP_LOG("NO DECOMP NEEDED.", __FUNCTION__);
Packit 857059
						bytes = header.compressedBytes;
Packit 857059
					}
Packit 857059
				}
Packit 857059
				if (bytes > 0) {
Packit 857059
					SCP_LOG("Writing %d bytes to %s", __FUNCTION__, bytes, dst);
Packit 857059
					int bout = fwrite((void*)copyBuffer1, 1, bytes, fdout);
Packit 857059
					if (bout != bytes) {
Packit 857059
						SCP_LOG("Tried to write %d bytes to %s and only wrote %d", __FUNCTION__, bytes, dst, bout);
Packit 857059
						retVal = -1;
Packit 857059
						break;
Packit 857059
					} else {
Packit 857059
						SCP_LOG("Wrote %d bytes to %s", __FUNCTION__, bytes, dst);
Packit 857059
					}
Packit 857059
				} else {break;}
Packit 857059
			}
Packit 857059
			if (compressFlag < 0) {
Packit 857059
				compBlockHeader_t tailHeader;
Packit 857059
				tailHeader.compressedBytes = 0;
Packit 857059
				tailHeader.uncompressedBytes = 0;
Packit 857059
				int bout = fwrite((void*)&tailHeader, 1, sizeof(compBlockHeader_t), fdout);
Packit 857059
				if (bout != sizeof(compBlockHeader_t)) {
Packit 857059
					SCP_LOG("Tried to write tail header to %s and only wrote %d", __FUNCTION__, dst, bout);
Packit 857059
					retVal = -1;
Packit 857059
				} else {
Packit 857059
					if (compressedFileSize != NULL) {
Packit 857059
						*compressedFileSize += (long)bout;
Packit 857059
					}
Packit 857059
					SCP_LOG("Wrote tail header to %s", __FUNCTION__, dst);
Packit 857059
				}
Packit 857059
			}
Packit 857059
			fclose(fdin);
Packit 857059
			SCP_LOG("Closed fdin", __FUNCTION__);
Packit 857059
		} else {
Packit 857059
			SCP_LOG("Unable to fopen input file %s", __FUNCTION__, src);
Packit 857059
			retVal = -1;
Packit 857059
		}
Packit 857059
Packit 857059
		fclose(fdout);
Packit 857059
		SCP_LOG("Closed fdout", __FUNCTION__);
Packit 857059
	} else {
Packit 857059
		SCP_LOG("Unable to fopen %s as writeable.", __FUNCTION__, dst);
Packit 857059
		retVal = -1;
Packit 857059
	}
Packit 857059
	SCP_LOG("CopyFile returning %d", __FUNCTION__, retVal);
Packit 857059
	free(compressedBytesArray);
Packit 857059
	free(uncompressedBytesArray);
Packit 857059
	free(copyBuffer2);
Packit 857059
	free(copyBuffer1);
Packit 857059
Packit 857059
	return retVal;
Packit 857059
}
Packit 857059
Packit 857059
FILE* openUncompressedFile(char *filename, time_t *time) {
Packit 857059
	compFile_t *cf;
Packit 857059
	SCP_LOG("openUncompressedFile:%s", __FUNCTION__, filename);
Packit 857059
Packit 857059
	cf = (compFile_t *)calloc(1, sizeof(compFile_t));
Packit 857059
	if (cf == NULL)
Packit 857059
		return NULL;
Packit 857059
Packit 857059
	cf->fdin = fopen(filename, "r");
Packit 857059
	if (cf->fdin != NULL) {
Packit 857059
		compFileHeader_t header;
Packit 857059
		if (fread(&header, 1, sizeof(compFileHeader_t), cf->fdin) == sizeof(compFileHeader_t)) {
Packit 857059
			if (time != NULL)
Packit 857059
				*time = header.creationTime;
Packit 857059
			return (FILE *)cf;
Packit 857059
		}
Packit 857059
		fclose(cf->fdin);
Packit 857059
		SCP_LOG("openUncompressedFile: Unable to read file header for %s.", __FUNCTION__, filename);
Packit 857059
	}
Packit 857059
	free(cf);
Packit 857059
	return NULL;
Packit 857059
}
Packit 857059
Packit 857059
void closeUncompressedFile(FILE *fileIn) {
Packit 857059
	compFile_t *cf = (compFile_t *)fileIn;
Packit 857059
	SCP_LOG("closeUncompressedFile", __FUNCTION__);
Packit 857059
	fclose(cf->fdin);
Packit 857059
	free(cf);
Packit 857059
}
Packit 857059
Packit 857059
int readUncompressedBytes(FILE *fileIn, char *buffer, int bufsize) {
Packit 857059
	compFile_t *cf = (compFile_t *)fileIn;
Packit 857059
	compBlockHeader_t header;
Packit 857059
	int bufferIndex = 0;
Packit 857059
Packit 857059
	SCP_LOG("readUncompressedBytes bufsize:%d", __FUNCTION__, bufsize);
Packit 857059
	if (cf->leftOverBytes > 0) {
Packit 857059
		SCP_LOG("LEFT OVER BYTES: %d bytes", __FUNCTION__, cf->leftOverBytes);
Packit 857059
		if (cf->leftOverBytes >= bufsize) {
Packit 857059
			SCP_LOG("COPYING %d BYTES TO BUFFER", __FUNCTION__, bufsize);
Packit 857059
			memcpy(buffer, cf->uncompressedBytesArray, bufsize);
Packit 857059
			cf->leftOverBytes -= bufsize;
Packit 857059
			SCP_LOG("SLIDDING UP uncompressedBytesArray buffer from %d to 0.", __FUNCTION__, bufsize);
Packit 857059
			memcpy(cf->uncompressedBytesArray, &cf->uncompressedBytesArray[bufsize], cf->leftOverBytes);
Packit 857059
			SCP_LOG("RETURNING %d BYTES READ", __FUNCTION__, bufsize);
Packit 857059
			return bufsize;
Packit 857059
		} else {
Packit 857059
			SCP_LOG("COPYING %d BYTES TO BUFFER", __FUNCTION__, cf->leftOverBytes);
Packit 857059
			memcpy(buffer, cf->uncompressedBytesArray, cf->leftOverBytes);
Packit 857059
			bufferIndex = cf->leftOverBytes;
Packit 857059
			SCP_LOG("bufferIndex=%d", __FUNCTION__, bufferIndex);
Packit 857059
			cf->leftOverBytes = 0;
Packit 857059
		}
Packit 857059
	}
Packit 857059
Packit 857059
	while((bufferIndex < bufsize) && !feof(cf->fdin) && !cf->endOfCompressedFile) {
Packit 857059
		SCP_LOG("bufferIndex=%d", __FUNCTION__, bufferIndex);
Packit 857059
		int bread = fread(&header, 1, sizeof(compBlockHeader_t), cf->fdin);
Packit 857059
		if (bread != sizeof(compBlockHeader_t)) {
Packit 857059
			SCP_LOG("Tried to read header of %d bytes and read %d!", __FUNCTION__, sizeof(compBlockHeader_t), bread);
Packit 857059
			// should have got a tail header!
Packit 857059
			return -1;
Packit 857059
		}
Packit 857059
		SCP_LOG("header.uncompressedBytes=%d header.compressedBytes=%d", __FUNCTION__, header.uncompressedBytes, header.compressedBytes);
Packit 857059
		if ((header.uncompressedBytes == 0) && (header.compressedBytes == 0)) {
Packit 857059
			// no more blocks
Packit 857059
			cf->endOfCompressedFile = 1;
Packit 857059
			SCP_LOG("Read tail header. EOF!", __FUNCTION__);
Packit 857059
			break;
Packit 857059
		}
Packit 857059
		if ((header.uncompressedBytes > UNCOMPRESSED_ARRAY_SIZE) || (header.uncompressedBytes <= 0)) {
Packit 857059
			SCP_LOG("Uncompressed bytes should be 1-%d and it's %d", __FUNCTION__, UNCOMPRESSED_ARRAY_SIZE, header.uncompressedBytes);
Packit 857059
			return -1;
Packit 857059
		}
Packit 857059
		if ((header.compressedBytes > COMPRESSED_ARRAY_SIZE) || (header.compressedBytes <= 0)) {
Packit 857059
			SCP_LOG("Compressed bytes should be 1-%d and it's %d", __FUNCTION__, COMPRESSED_ARRAY_SIZE, header.compressedBytes);
Packit 857059
			return -1;
Packit 857059
		}
Packit 857059
		if (fread(cf->compressedBytesArray, 1, header.compressedBytes, cf->fdin) != header.compressedBytes) {
Packit 857059
			SCP_LOG("Unable to read %d compressed bytes", __FUNCTION__, header.compressedBytes);
Packit 857059
			return -1;
Packit 857059
		}
Packit 857059
		SCP_LOG("READ %d COMPRESSED BYTES", __FUNCTION__, header.compressedBytes);
Packit 857059
		int bytesToCopy = 0;
Packit 857059
		int uncompBytes = header.compressedBytes;
Packit 857059
		if (header.compressedBytes < header.uncompressedBytes) {
Packit 857059
			uncompBytes = inflater(cf->uncompressedBytesArray, UNCOMPRESSED_ARRAY_SIZE,
Packit 857059
					cf->compressedBytesArray, header.compressedBytes);
Packit 857059
			SCP_LOG("DECOMPRESSED %d BYTES INTO %d BYTES", __FUNCTION__, header.compressedBytes, uncompBytes);
Packit 857059
			if (uncompBytes < 0)
Packit 857059
				return -1;
Packit 857059
			bytesToCopy = uncompBytes;
Packit 857059
		} else {
Packit 857059
			// this block is not compressed, just copy it
Packit 857059
			memcpy(cf->uncompressedBytesArray, cf->compressedBytesArray, header.compressedBytes);
Packit 857059
			bytesToCopy = header.compressedBytes;
Packit 857059
		}
Packit 857059
		if (uncompBytes + bufferIndex > bufsize) {
Packit 857059
			// we have extra bytes. Save them for the next call.
Packit 857059
			bytesToCopy = bufsize - bufferIndex;
Packit 857059
			cf->leftOverBytes = uncompBytes - bytesToCopy;
Packit 857059
		}
Packit 857059
		SCP_LOG("COPYING %d BYTES FROM uncompressedBytesArray TO BUFFER[%d]", __FUNCTION__, bytesToCopy, bufferIndex);
Packit 857059
		memcpy(&buffer[bufferIndex], cf->uncompressedBytesArray, bytesToCopy);
Packit 857059
		if (cf->leftOverBytes > 0) {
Packit 857059
			SCP_LOG("LEFT OVER BYTES. COPYING %d BYTES FROM %d TO 0.", __FUNCTION__, cf->leftOverBytes, bytesToCopy);
Packit 857059
			memcpy(cf->uncompressedBytesArray, &cf->uncompressedBytesArray[bytesToCopy], cf->leftOverBytes);
Packit 857059
		}
Packit 857059
		bufferIndex += bytesToCopy;
Packit 857059
		SCP_LOG("bufferIndex is now:%d", __FUNCTION__, bufferIndex);
Packit 857059
	}
Packit 857059
	SCP_LOG("EXIT: bufferIndex is now:%d", __FUNCTION__, bufferIndex);
Packit 857059
	if (bufferIndex > 0 ) {
Packit 857059
		if (bufferIndex < bufsize) {
Packit 857059
			buffer[bufferIndex] = 0;
Packit 857059
		}
Packit 857059
		SCP_LOG("RETURNING: %s", __FUNCTION__, buffer);
Packit 857059
	}
Packit 857059
	return bufferIndex;
Packit 857059
}