GIFTranslator: Replace magic constants

Introduce GIFPrivate.h to define these magic constants

Also, make sure that we don't go over LZ_MAX_CODE bits
This commit is contained in:
John Scipione
2014-03-04 01:22:53 -05:00
parent c14b552e41
commit 5a7193d558
3 changed files with 46 additions and 19 deletions
+14 -11
View File
@@ -26,6 +26,8 @@
#include <TranslatorFormats.h>
#include <InterfaceDefs.h>
#include "GIFPrivate.h"
extern bool debug;
@@ -57,8 +59,8 @@ GIFLoad::GIFLoad(BPositionIO* input, BPositionIO* output)
return;
}
while (c != 0x3b) {
if (c == 0x2c) {
while (c != TERMINATOR_INTRODUCER) {
if (c == DESCRIPTOR_INTRODUCER) {
if ((!ReadGIFImageHeader()) || (!ReadGIFImageData())) {
if (debug) {
syslog(LOG_ERR, "GIFLoad::GIFLoad() - "
@@ -75,23 +77,23 @@ GIFLoad::GIFLoad(BPositionIO* input, BPositionIO* output)
free(fScanLine);
fScanLine = NULL;
return;
} else if (c == 0x21) {
} else if (c == EXTENSION_INTRODUCER) {
unsigned char d;
if (fInput->Read(&d, 1) < 1) {
fatalerror = true;
return;
}
if (d == 0xff) {
if (d == LOOP_BLOCK_LABEL) {
if (!ReadGIFLoopBlock()) {
fatalerror = true;
return;
}
} else if (d == 0xf9) {
} else if (d == GRAPHIC_CONTROL_LABEL) {
if (!ReadGIFControlBlock()) {
fatalerror = true;
return;
}
} else if (d == 0xfe) {
} else if (d == COMMENT_EXTENSION_LABEL) {
if (!ReadGIFCommentBlock()) {
fatalerror = true;
return;
@@ -102,7 +104,7 @@ GIFLoad::GIFLoad(BPositionIO* input, BPositionIO* output)
return;
}
}
} else if (c != 0x00) {
} else if (c != BLOCK_TERMINATOR) {
if (!ReadGIFUnknownBlock(c)) {
fatalerror = true;
return;
@@ -224,10 +226,10 @@ GIFLoad::ReadGIFCommentBlock()
if (fInput->Read(comment_data, length) < length)
return false;
comment_data[length] = 0x00;
comment_data[length] = BLOCK_TERMINATOR;
if (debug)
syslog(LOG_INFO, "%s", comment_data);
} while (length != 0x00);
} while (length != BLOCK_TERMINATOR);
if (debug)
syslog(LOG_INFO, "\n");
@@ -248,7 +250,7 @@ GIFLoad::ReadGIFUnknownBlock(unsigned char c)
return false;
fInput->Seek(length, SEEK_CUR);
} while (length != 0x00);
} while (length != BLOCK_TERMINATOR);
return true;
}
@@ -428,7 +430,7 @@ GIFLoad::ReadGIFImageData()
fOldCodeLength = fEntrySize[fNewCode];
fNextCode++;
if (fNextCode > fMaxCode && fBits != 12) {
if (fNextCode > fMaxCode && fBits < LZ_MAX_BITS) {
fBits++;
fMaxCode = (1 << fBits) - 1;
}
@@ -514,6 +516,7 @@ GIFLoad::InitFrame(int size)
fEndCode = fClearCode + 1;
fNextCode = fClearCode + 2;
fMaxCode = (1 << fBits) - 1;
fPass = 0;
if (fInterlaced)
+19
View File
@@ -0,0 +1,19 @@
#ifndef GIF_PRIVATE_H
#define GIF_PRIVATE_H
#define BLOCK_TERMINATOR 0x00
#define EXTENSION_INTRODUCER 0x21
#define DESCRIPTOR_INTRODUCER 0x2c
#define TERMINATOR_INTRODUCER 0x3b
#define GRAPHIC_CONTROL_LABEL 0xf9
#define COMMENT_EXTENSION_LABEL 0xfe
#define LOOP_BLOCK_LABEL 0xff
#define LZ_MAX_CODE 4095
#define LZ_MAX_BITS 12
#endif // GIF_PRIVATE_H
+13 -8
View File
@@ -24,6 +24,8 @@
#include <stdlib.h>
#include <syslog.h>
#include "GIFPrivate.h"
const int gs_pass_starts_at[] = {0, 4, 2, 1, 0};
const int gs_increment_pass_by[] = {8, 8, 4, 2, 0};
@@ -173,7 +175,7 @@ GIFSave::GIFSave(BBitmap* bitmap, BPositionIO* output,
delete hash;
// Terminating character
char t = 0x3b;
char t = TERMINATOR_INTRODUCER;
output->Write(&t, 1);
}
@@ -212,7 +214,10 @@ GIFSave::WriteGIFHeader()
void
GIFSave::WriteGIFControlBlock()
{
unsigned char b[8] = { 0x21, 0xf9, 0x04, 0, 0, 0, 0, 0x00 };
unsigned char b[8] = {
EXTENSION_INTRODUCER, GRAPHIC_CONTROL_LABEL, 0x04, 0x00, 0x00, 0x00,
0x00, BLOCK_TERMINATOR
};
if (palette->UseTransparent()) {
b[3] = b[3] | 1;
b[6] = palette->TransparentIndex();
@@ -225,7 +230,7 @@ void
GIFSave::WriteGIFImageHeader()
{
unsigned char header[10];
header[0] = 0x2c;
header[0] = DESCRIPTOR_INTRODUCER;
header[1] = header[2] = 0;
header[3] = header[4] = 0;
@@ -237,7 +242,7 @@ GIFSave::WriteGIFImageHeader()
if (fSettings->SetGetBool(GIF_SETTING_INTERLACED))
header[9] = 0x40;
else
header[9] = 0x00;
header[9] = BLOCK_TERMINATOR;
output->Write(header, 10);
}
@@ -268,8 +273,8 @@ GIFSave::WriteGIFImageData()
if (next_code > max_code) {
BITS++;
if (BITS > 12) {
OutputCode(clear_code, 12);
if (BITS > LZ_MAX_BITS) {
OutputCode(clear_code, LZ_MAX_BITS);
BITS = code_size + 1;
ResetHashtable();
next_code = clear_code + 1;
@@ -285,7 +290,7 @@ GIFSave::WriteGIFImageData()
OutputCode(string_code, BITS);
OutputCode(end_code, BITS);
OutputCode(0, BITS, true);
char t = 0x00;
char t = BLOCK_TERMINATOR;
output->Write(&t, 1);
free(code_value);
free(prefix_code);
@@ -531,7 +536,7 @@ GIFSave::InitFrame()
max_code = (1 << BITS) - 1;
string_code = 0;
character = 0;
table_size = 1 << 12;
table_size = 1 << LZ_MAX_BITS;
bit_count = 0;
bit_buffer = 0;