1
0
Fork 0

(svn r2550) raise 32767 limit of gamma values, and 16MB limit of RIFF chunks in saveload code.

release/0.4.5
ludde 2005-07-12 19:15:56 +00:00
parent 6ea60aba31
commit b5a30ed0fc
1 changed files with 53 additions and 25 deletions

View File

@ -152,8 +152,18 @@ static uint SlReadSimpleGamma(void)
{ {
uint i = SlReadByte(); uint i = SlReadByte();
if (HASBIT(i, 7)) { if (HASBIT(i, 7)) {
i = (i << 8) + SlReadByte(); i &= ~0x80;
CLRBIT(i, 15); if (HASBIT(i, 6)) {
i &= ~0x40;
if (HASBIT(i, 5)) {
i &= ~0x20;
if (HASBIT(i, 4))
SlError("Unsupported gamma");
i = (i << 8) | SlReadByte();
}
i = (i << 8) | SlReadByte();
}
i = (i << 8) | SlReadByte();
} }
return i; return i;
} }
@ -162,25 +172,37 @@ static uint SlReadSimpleGamma(void)
* Write the header descriptor of an object or an array. * Write the header descriptor of an object or an array.
* If the element is bigger than 127, use 2 bytes for saving * If the element is bigger than 127, use 2 bytes for saving
* and use the highest byte of the first written one as a notice * and use the highest byte of the first written one as a notice
* that the length consists of 2 bytes. The length is fixed to a * that the length consists of 2 bytes, etc.. like this:
* maximum of 16384 since any higher value will have bit 15 set * 0xxxxxxx
* and the notice, would obfuscate the real value * 10xxxxxx xxxxxxxx
* 110xxxxx xxxxxxxx xxxxxxxx
* 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
* @param i Index being written * @param i Index being written
* @todo the maximum of 16384 can easily be reached with vehicles, so raise this artificial limit
*/ */
static void SlWriteSimpleGamma(uint i) static void SlWriteSimpleGamma(uint i)
{ {
assert(i < (1 << 14));
if (i >= (1 << 7)) { if (i >= (1 << 7)) {
SlWriteByte((byte)((1 << 7) | (i >> 8))); if (i >= (1 << 14)) {
SlWriteByte((byte)i); if (i >= (1 << 21)) {
} else assert(i < (1 << 28));
SlWriteByte(i); SlWriteByte((byte)0xE0 | (i>>24));
SlWriteByte((byte)(i>>16));
} else {
SlWriteByte((byte)0xC0 | (i>>16));
}
SlWriteByte((byte)(i>>8));
} else {
SlWriteByte((byte)(0x80 | (i>>8)));
}
}
SlWriteByte(i);
} }
/** Return if the length will use up 1 or two bytes in a savegame */ /** Return how many bytes used to encode a gamma value */
static inline uint SlGetGammaLength(uint i) {return (i >= (1 << 7)) ? 2 : 1;} static inline uint SlGetGammaLength(uint i) {
return 1 + (i >= (1 << 7)) + (i >= (1 << 14)) + (i >= (1 << 21));
}
static inline int SlReadSparseIndex(void) {return SlReadSimpleGamma();} static inline int SlReadSparseIndex(void) {return SlReadSimpleGamma();}
static inline void SlWriteSparseIndex(uint index) {SlWriteSimpleGamma(index);} static inline void SlWriteSparseIndex(uint index) {SlWriteSimpleGamma(index);}
@ -241,8 +263,11 @@ void SlSetLength(size_t length)
_sl.need_length = NL_NONE; _sl.need_length = NL_NONE;
switch (_sl.block_mode) { switch (_sl.block_mode) {
case CH_RIFF: case CH_RIFF:
// Really simple to write a RIFF length :) // Ugly encoding of >16M RIFF chunks
SlWriteUint32(length); // The lower 24 bits are normal
// The uppermost 4 bits are bits 24:27
assert(length < (1<<28));
SlWriteUint32((length & 0xFFFFFF) | ((length >> 24) << 28));
break; break;
case CH_ARRAY: case CH_ARRAY:
assert(_sl.last_array_index <= _sl.array_index); assert(_sl.last_array_index <= _sl.array_index);
@ -614,16 +639,19 @@ static void SlLoadChunk(const ChunkHandler *ch)
case CH_SPARSE_ARRAY: case CH_SPARSE_ARRAY:
ch->load_proc(); ch->load_proc();
break; break;
case CH_RIFF: default:
// Read length if ((m & 0xF) == CH_RIFF) {
len = SlReadByte() << 16; // Read length
len += SlReadUint16(); len = (SlReadByte() << 16) | ((m >> 4) << 24);
_sl.obj_len = len; len += SlReadUint16();
endoffs = SlGetOffs() + len; _sl.obj_len = len;
ch->load_proc(); endoffs = SlGetOffs() + len;
assert(SlGetOffs() == endoffs); ch->load_proc();
assert(SlGetOffs() == endoffs);
} else {
SlError("Invalid chunk type");
}
break; break;
default: NOT_REACHED();
} }
} }