void GIFLoad::ResetTable() { fBits = fCodeSize + 1; fNextCode = fClearCode + 2; fMaxCode = (1 << fBits) - 1; MemblockDeleteAll(); for (int x = 0; x < 4096; x++) { fTable[x] = NULL; if (x < (1 << fCodeSize)) { fTable[x] = MemblockAllocate(1); fTable[x][0] = x; fEntrySize[x] = 1; } } }
bool GIFLoad::ReadGIFImageData() { unsigned char newEntry[ENTRY_COUNT]; unsigned char codeSize; if (fInput->Read(&codeSize, 1) < 1) return false; if (codeSize > fPalette->size_in_bits) { if (debug) { syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - " "Code_size should be %d, not %d, allowing it\n", fCodeSize, codeSize); } if (!InitFrame(codeSize)) return false; } else if (codeSize < fPalette->size_in_bits) { if (debug) { syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - " "Code_size should be %d, not %d\n", fCodeSize, codeSize); } return false; } else if (!InitFrame(fPalette->size_in_bits)) return false; if (debug) syslog(LOG_INFO, "GIFLoad::ReadGIFImageData() - Starting LZW\n"); while ((fNewCode = NextCode()) != -1 && fNewCode != fEndCode) { if (fNewCode == fClearCode) { ResetTable(); fNewCode = NextCode(); fOldCode[0] = fNewCode; fOldCodeLength = 1; if (!OutputColor(fOldCode, 1)) goto bad_end; if (fNewCode == -1 || fNewCode == fEndCode) { if (debug) { syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - " "Premature fEndCode or error reading fNewCode\n"); } goto bad_end; } continue; } // explicitly check for lack of clear code at start of file if (fOldCodeLength == 0) { fOldCode[0] = fNewCode; fOldCodeLength = 1; if (!OutputColor(fOldCode, 1)) goto bad_end; continue; } // error out if we're trying to access an out-of-bounds index if (fNextCode >= ENTRY_COUNT) goto bad_end; if (fTable[fNewCode] != NULL) { // exists in table if (!OutputColor(fTable[fNewCode], fEntrySize[fNewCode])) goto bad_end; //memcpy(newEntry, fOldCode, fOldCodeLength); for (unsigned int x = 0; x < fOldCodeLength; x++) newEntry[x] = fOldCode[x]; //memcpy(newEntry + fOldCodeLength, fTable[fNewCode], 1); newEntry[fOldCodeLength] = fTable[fNewCode][0]; } else { // does not exist in table //memcpy(newEntry, fOldCode, fOldCodeLength); for (unsigned int x = 0; x < fOldCodeLength; x++) newEntry[x] = fOldCode[x]; //memcpy(newEntry + fOldCodeLength, fOldCode, 1); newEntry[fOldCodeLength] = fOldCode[0]; if (!OutputColor(newEntry, fOldCodeLength + 1)) goto bad_end; } fTable[fNextCode] = MemblockAllocate(fOldCodeLength + 1); if (fTable[fNextCode] == NULL) goto bad_end; //memcpy(fTable[fNextCode], newEntry, fOldCodeLength + 1); for (unsigned int x = 0; x < fOldCodeLength + 1; x++) fTable[fNextCode][x] = newEntry[x]; fEntrySize[fNextCode] = fOldCodeLength + 1; //memcpy(fOldCode, fTable[fNewCode], fEntrySize[fNewCode]); for (int x = 0; x < fEntrySize[fNewCode]; x++) fOldCode[x] = fTable[fNewCode][x]; fOldCodeLength = fEntrySize[fNewCode]; fNextCode++; if (fNextCode > fMaxCode && fBits < LZ_MAX_BITS) { fBits++; fMaxCode = (1 << fBits) - 1; } } MemblockDeleteAll(); if (fNewCode == -1) return false; if (debug) syslog(LOG_INFO, "GIFLoad::ReadGIFImageData() - Done\n"); return true; bad_end: if (debug) syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Reached a bad end\n"); MemblockDeleteAll(); return false; }
bool GIFLoad::ReadGIFImageData() { unsigned char newEntry[4096]; unsigned char cs; fInput->Read(&cs, 1); if (cs == fPalette->size_in_bits) { if (!InitFrame(fPalette->size_in_bits)) return false; } else if (cs > fPalette->size_in_bits) { if (debug) syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Code_size should be %d, not " "%d, allowing it\n", fCodeSize, cs); if (!InitFrame(cs)) return false; } else if (cs < fPalette->size_in_bits) { if (debug) syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Code_size should be %d, not " "%d\n", fCodeSize, cs); return false; } if (debug) syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Starting LZW\n"); while ((fNewCode = NextCode()) != -1 && fNewCode != fEndCode) { if (fNewCode == fClearCode) { ResetTable(); fNewCode = NextCode(); fOldCode[0] = fNewCode; fOldCodeLength = 1; if (!OutputColor(fOldCode, 1)) goto bad_end; if (fNewCode == -1 || fNewCode == fEndCode) { if (debug) syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Premature fEndCode " "or error\n"); goto bad_end; } continue; } // Explicitly check for lack of clear code at start of file if (fOldCodeLength == 0) { fOldCode[0] = fNewCode; fOldCodeLength = 1; if (!OutputColor(fOldCode, 1)) goto bad_end; continue; } if (fTable[fNewCode] != NULL) { // Does exist in table if (!OutputColor(fTable[fNewCode], fEntrySize[fNewCode])) goto bad_end; //memcpy(newEntry, fOldCode, fOldCodeLength); for (int x = 0; x < fOldCodeLength; x++) { newEntry[x] = fOldCode[x]; } //memcpy(newEntry + fOldCodeLength, fTable[fNewCode], 1); newEntry[fOldCodeLength] = *fTable[fNewCode]; } else { // Does not exist in table //memcpy(newEntry, fOldCode, fOldCodeLength); for (int x = 0; x < fOldCodeLength; x++) { newEntry[x] = fOldCode[x]; } //memcpy(newEntry + fOldCodeLength, fOldCode, 1); newEntry[fOldCodeLength] = *fOldCode; if (!OutputColor(newEntry, fOldCodeLength + 1)) goto bad_end; } fTable[fNextCode] = MemblockAllocate(fOldCodeLength + 1); //memcpy(fTable[fNextCode], newEntry, fOldCodeLength + 1); for (int x = 0; x < fOldCodeLength + 1; x++) { fTable[fNextCode][x] = newEntry[x]; } fEntrySize[fNextCode] = fOldCodeLength + 1; //memcpy(fOldCode, fTable[fNewCode], fEntrySize[fNewCode]); for (int x = 0; x < fEntrySize[fNewCode]; x++) { fOldCode[x] = fTable[fNewCode][x]; } fOldCodeLength = fEntrySize[fNewCode]; fNextCode++; if (fNextCode > fMaxCode && fBits != 12) { fBits++; fMaxCode = (1 << fBits) - 1; } } MemblockDeleteAll(); if (fNewCode == -1) return false; if (debug) syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Done\n"); return true; bad_end: if (debug) syslog(LOG_ERR, "GIFLoad::ReadGIFImageData() - Reached a bad end\n"); MemblockDeleteAll(); return false; }