static int CompGo(int bFinish) { int nResult = 0; int nAvailOut = 0; bool bRetry, bOverflow; do { bRetry = false; // Point to the remainder of out buffer Zstr.next_out = Comp + nCompFill; nAvailOut = nCompLen - nCompFill; if (nAvailOut < 0) { nAvailOut = 0; } Zstr.avail_out = nAvailOut; // Try to deflate into the buffer (there may not be enough room though) if (bFinish) { nResult = deflate(&Zstr, Z_FINISH); // deflate and finish if (nResult != Z_OK && nResult != Z_STREAM_END) { return 1; } } else { nResult = deflate(&Zstr, 0); // deflate if (nResult != Z_OK) { return 1; } } nCompFill = Zstr.next_out - Comp; // Update how much has been filled // Check for overflow bOverflow = bFinish ? (nResult == Z_OK) : (Zstr.avail_out <= 0); if (bOverflow) { if (CompEnlarge(4 * 1024)) { return 1; } bRetry = true; } } while (bRetry); return 0; }
// Compress a state using deflate int StateCompress(unsigned char **pDef,int *pnDefLen,int bAll) { int nRet=0; void *NewMem=NULL; memset(&Zstr,0,sizeof(Zstr)); Comp=NULL; nCompLen=0; nCompFill=0; // Begin with a zero-length buffer nRet=CompEnlarge(8*1024); if (nRet!=0) { return 1; } nRet=deflateInit(&Zstr,Z_DEFAULT_COMPRESSION); BurnAcb=StateCompressAcb; // callback our function with each area BurnAreaScan(8|1,NULL); // scan nvram, read (from driver -> compress) if (bAll) { BurnAreaScan(4|1,NULL); // scan volatile ram, read (from driver -> compress) } // Finish off CompGo(1); nRet=deflateEnd(&Zstr); // Size down NewMem=realloc(Comp,nCompFill); if (NewMem!=NULL) { Comp=(unsigned char *)NewMem; nCompLen=nCompFill; } // Return the buffer if (pDef !=NULL) { *pDef =Comp; } if (pnDefLen!=NULL) { *pnDefLen=nCompFill; } return 0; }
// Compress a state using deflate int BurnStateCompress(unsigned char** pDef, int* pnDefLen, int bAll) { void* NewMem = NULL; memset(&Zstr, 0, sizeof(Zstr)); Comp = NULL; nCompLen = 0; nCompFill = 0; // Begin with a zero-length buffer if (CompEnlarge(8 * 1024)) { return 1; } deflateInit(&Zstr, Z_DEFAULT_COMPRESSION); BurnAcb = StateCompressAcb; // callback our function with each area if (bAll) BurnAreaScan(ACB_FULLSCAN | ACB_READ, NULL); // scan all ram, read (from driver <- decompress) else BurnAreaScan(ACB_NVRAM | ACB_READ, NULL); // scan nvram, read (from driver <- decompress) // Finish off CompGo(1); deflateEnd(&Zstr); // Size down NewMem = realloc(Comp, nCompFill); if (NewMem) { Comp = (unsigned char*)NewMem; nCompLen = nCompFill; } // Return the buffer if (pDef) { *pDef = Comp; } if (pnDefLen) { *pnDefLen = nCompFill; } return 0; }
static int CompGo(int bFinish) { int bNoRoom=0; int nResult=0; int nAvailOut=0; int nRet=0; GoAgain: // Point to the remainder of out buffer Zstr.next_out=Comp+nCompFill; nAvailOut=nCompLen-nCompFill; if (nAvailOut<0) { nAvailOut=0; } Zstr.avail_out=nAvailOut; // Try to deflate into the buffer (there may not be enough room though) if (bFinish) { nResult=deflate(&Zstr,Z_FINISH); // Try to deflate and finish } else { nResult=deflate(&Zstr,0); // deflate } if (bFinish) { if (nResult!=Z_OK && nResult!=Z_STREAM_END) { return 1; // Error } } else { if (nResult!=Z_OK) { return 1; // Error } } nCompFill=Zstr.next_out-Comp; // Update how much has been filled bNoRoom=0; if (bFinish==0 && Zstr.avail_out<=0) { bNoRoom=1; // output buffer is full } if (bFinish && nResult==Z_OK) { bNoRoom=1; // output buffer is full (there wasn't room to finish) } if (bNoRoom) { // There wasn't enough room in the output buffer nRet=CompEnlarge(4*1024); if (nRet!=0) { return 1; } goto GoAgain; } return 0; }