Reorder structs, alignment

This commit is contained in:
twinaphex 2020-08-16 01:07:42 +02:00
parent faedbf4368
commit b69ceaea1b
5 changed files with 76 additions and 113 deletions

View File

@ -171,8 +171,8 @@ typedef byte DecoderState;
/* Decoder data. */ /* Decoder data. */
typedef struct tag_DecoderData typedef struct tag_DecoderData
{ {
DecoderState state;
uint32_t bits; uint32_t bits;
DecoderState state; /* byte alignment */
} DecoderData; } DecoderData;
typedef DecoderData* Decoder; typedef DecoderData* Decoder;
@ -223,18 +223,14 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
{ {
case DECODER_RESET: case DECODER_RESET:
if (IS_UTF8_SINGLE_BYTE(b)) if (IS_UTF8_SINGLE_BYTE(b))
{
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 1, b); output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 1, b);
}
else if (IS_UTF8_FIRST_BYTE_OF_2(b)) else if (IS_UTF8_FIRST_BYTE_OF_2(b))
{ {
/* UTF-8 2-byte sequences that are overlong encodings can be /* UTF-8 2-byte sequences that are overlong encodings can be
detected from just the first byte (C0 or C1). */ detected from just the first byte (C0 or C1). */
decoder->bits = (uint32_t)BOTTOM_5_BITS(b) << 6; decoder->bits = (uint32_t)BOTTOM_5_BITS(b) << 6;
if (decoder->bits < FIRST_2_BYTE_UTF8_CODEPOINT) if (decoder->bits < FIRST_2_BYTE_UTF8_CODEPOINT)
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 1, 0);
}
else else
{ {
decoder->state = DECODED_1_OF_2; decoder->state = DECODED_1_OF_2;
@ -253,9 +249,7 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
codepoints can be detected from the first byte (F5 - FF). */ codepoints can be detected from the first byte (F5 - FF). */
decoder->bits = (uint32_t)BOTTOM_3_BITS(b) << 18; decoder->bits = (uint32_t)BOTTOM_3_BITS(b) << 18;
if (decoder->bits > MAX_CODEPOINT) if (decoder->bits > MAX_CODEPOINT)
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 1, 0);
}
else else
{ {
decoder->state = DECODED_1_OF_4; decoder->state = DECODED_1_OF_4;
@ -263,35 +257,28 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
} }
} }
else else
{
/* The byte is of the form 11111xxx or 10xxxxxx, and is not /* The byte is of the form 11111xxx or 10xxxxxx, and is not
a valid first byte for a UTF-8 sequence. */ a valid first byte for a UTF-8 sequence. */
output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 1, 0);
}
break; break;
case DECODED_1_OF_2: case DECODED_1_OF_2:
if (IS_UTF8_CONTINUATION_BYTE(b)) if (IS_UTF8_CONTINUATION_BYTE(b))
{
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 2, decoder->bits | BOTTOM_6_BITS(b)); output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 2, decoder->bits | BOTTOM_6_BITS(b));
}
else else
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0);
}
break; break;
case DECODED_1_OF_3: case DECODED_1_OF_3:
if (IS_UTF8_CONTINUATION_BYTE(b)) if (IS_UTF8_CONTINUATION_BYTE(b))
{ {
/* UTF-8 3-byte sequences that are overlong encodings or encode /* UTF-8 3-byte sequences that are overlong
surrogate codepoints can be detected after 2 bytes. */ * encodings or encode surrogate codepoints
* can be detected after 2 bytes. */
decoder->bits |= (uint32_t)BOTTOM_6_BITS(b) << 6; decoder->bits |= (uint32_t)BOTTOM_6_BITS(b) << 6;
if ((decoder->bits < FIRST_3_BYTE_UTF8_CODEPOINT) || IS_SURROGATE(decoder->bits)) if ((decoder->bits < FIRST_3_BYTE_UTF8_CODEPOINT) ||
{ IS_SURROGATE(decoder->bits))
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0);
}
else else
{ {
decoder->state = DECODED_2_OF_3; decoder->state = DECODED_2_OF_3;
@ -299,20 +286,14 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
} }
} }
else else
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0);
}
break; break;
case DECODED_2_OF_3: case DECODED_2_OF_3:
if (IS_UTF8_CONTINUATION_BYTE(b)) if (IS_UTF8_CONTINUATION_BYTE(b))
{
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 3, decoder->bits | BOTTOM_6_BITS(b)); output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 3, decoder->bits | BOTTOM_6_BITS(b));
}
else else
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0);
}
break; break;
case DECODED_1_OF_4: case DECODED_1_OF_4:
@ -321,10 +302,9 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
/* UTF-8 4-byte sequences that are overlong encodings or encode /* UTF-8 4-byte sequences that are overlong encodings or encode
out-of-range codepoints can be detected after 2 bytes. */ out-of-range codepoints can be detected after 2 bytes. */
decoder->bits |= (uint32_t)BOTTOM_6_BITS(b) << 12; decoder->bits |= (uint32_t)BOTTOM_6_BITS(b) << 12;
if ((decoder->bits < FIRST_4_BYTE_UTF8_CODEPOINT) || (decoder->bits > MAX_CODEPOINT)) if ( (decoder->bits < FIRST_4_BYTE_UTF8_CODEPOINT) ||
{ (decoder->bits > MAX_CODEPOINT))
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0);
}
else else
{ {
decoder->state = DECODED_2_OF_4; decoder->state = DECODED_2_OF_4;
@ -332,9 +312,7 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
} }
} }
else else
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 1, 0);
}
break; break;
case DECODED_2_OF_4: case DECODED_2_OF_4:
@ -344,21 +322,15 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
decoder->state = DECODED_3_OF_4; decoder->state = DECODED_3_OF_4;
goto noreset; goto noreset;
} }
else
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0);
}
break; break;
case DECODED_3_OF_4: case DECODED_3_OF_4:
if (IS_UTF8_CONTINUATION_BYTE(b)) if (IS_UTF8_CONTINUATION_BYTE(b))
{
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, decoder->bits | BOTTOM_6_BITS(b)); output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, decoder->bits | BOTTOM_6_BITS(b));
}
else else
{
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 3, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 3, 0);
}
break; break;
} }
break; break;
@ -379,11 +351,9 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
case DECODED_1_OF_2: case DECODED_1_OF_2:
decoder->bits |= (uint32_t)b << 8; decoder->bits |= (uint32_t)b << 8;
if (IS_TRAILING_SURROGATE(decoder->bits))
{
/* A trailing surrogate cannot appear on its own. */ /* A trailing surrogate cannot appear on its own. */
if (IS_TRAILING_SURROGATE(decoder->bits))
output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 2, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 2, 0);
}
else if (IS_LEADING_SURROGATE(decoder->bits)) else if (IS_LEADING_SURROGATE(decoder->bits))
{ {
/* A leading surrogate implies a 4-byte surrogate pair. */ /* A leading surrogate implies a 4-byte surrogate pair. */
@ -392,9 +362,7 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
goto noreset; goto noreset;
} }
else else
{
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 2, decoder->bits); output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 2, decoder->bits);
}
break; break;
case DECODED_2_OF_4: case DECODED_2_OF_4:
@ -414,10 +382,8 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0);
goto noreset; goto noreset;
} }
else
{
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, CODEPOINT_FROM_SURROGATES(decoder->bits)); output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, CODEPOINT_FROM_SURROGATES(decoder->bits));
}
break; break;
} }
break; break;
@ -438,11 +404,9 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
case DECODED_1_OF_2: case DECODED_1_OF_2:
decoder->bits |= b; decoder->bits |= b;
if (IS_TRAILING_SURROGATE(decoder->bits))
{
/* A trailing surrogate cannot appear on its own. */ /* A trailing surrogate cannot appear on its own. */
if (IS_TRAILING_SURROGATE(decoder->bits))
output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 2, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 2, 0);
}
else if (IS_LEADING_SURROGATE(decoder->bits)) else if (IS_LEADING_SURROGATE(decoder->bits))
{ {
/* A leading surrogate implies a 4-byte surrogate pair. */ /* A leading surrogate implies a 4-byte surrogate pair. */
@ -451,9 +415,7 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
goto noreset; goto noreset;
} }
else else
{
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 2, decoder->bits); output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 2, decoder->bits);
}
break; break;
case DECODED_2_OF_4: case DECODED_2_OF_4:
@ -473,10 +435,9 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0); output = DECODER_OUTPUT(SEQUENCE_INVALID_EXCLUSIVE, 2, 0);
goto noreset; goto noreset;
} }
else
{ output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 4,
output = DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, CODEPOINT_FROM_SURROGATES(decoder->bits)); CODEPOINT_FROM_SURROGATES(decoder->bits));
}
break; break;
} }
break; break;
@ -503,7 +464,9 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
case DECODED_3_OF_4: case DECODED_3_OF_4:
decoder->bits |= (uint32_t)b << 24; decoder->bits |= (uint32_t)b << 24;
output = (IS_SURROGATE(decoder->bits) || (decoder->bits > MAX_CODEPOINT)) output = (
IS_SURROGATE(decoder->bits) ||
(decoder->bits > MAX_CODEPOINT))
? DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 4, 0) ? DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 4, 0)
: DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, decoder->bits); : DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, decoder->bits);
break; break;
@ -532,7 +495,8 @@ static DecoderOutput Decoder_ProcessByte(Decoder decoder, Encoding encoding, byt
case DECODED_3_OF_4: case DECODED_3_OF_4:
decoder->bits |= b; decoder->bits |= b;
output = (IS_SURROGATE(decoder->bits) || (decoder->bits > MAX_CODEPOINT)) output = (IS_SURROGATE(decoder->bits) ||
(decoder->bits > MAX_CODEPOINT))
? DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 4, 0) ? DECODER_OUTPUT(SEQUENCE_INVALID_INCLUSIVE, 4, 0)
: DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, decoder->bits); : DECODER_OUTPUT(SEQUENCE_COMPLETE, 4, decoder->bits);
break; break;
@ -794,10 +758,10 @@ typedef byte GrammarianOutput;
/* Grammar rule used by the grammarian to process a token. */ /* Grammar rule used by the grammarian to process a token. */
typedef struct tag_GrammarRule typedef struct tag_GrammarRule
{ {
Symbol symbolToPush1; Symbol symbolToPush1; /* byte alignment */
Symbol symbolToPush2; Symbol symbolToPush2; /* byte alignment */
byte reprocess; byte reprocess;
GrammarEvent emit; GrammarEvent emit; /* byte alignment */
} GrammarRule; } GrammarRule;
/* Grammarian functions. */ /* Grammarian functions. */
@ -996,35 +960,12 @@ typedef struct tag_MemberNames
/* A parser instance. */ /* A parser instance. */
struct JSON_Parser_Data struct JSON_Parser_Data
{ {
JSON_MemorySuite memorySuite; JSON_MemorySuite memorySuite; /* ptr alignment */
void* userData; void* userData;
ParserState state;
ParserFlags flags;
Encoding inputEncoding;
Encoding stringEncoding;
Encoding numberEncoding;
Symbol token;
TokenAttributes tokenAttributes;
Error error;
byte errorOffset;
LexerState lexerState;
uint32_t lexerBits;
size_t codepointLocationByte;
size_t codepointLocationLine;
size_t codepointLocationColumn;
size_t tokenLocationByte;
size_t tokenLocationLine;
size_t tokenLocationColumn;
size_t depth;
byte* pTokenBytes; byte* pTokenBytes;
size_t tokenBytesLength;
size_t tokenBytesUsed;
size_t maxStringLength;
size_t maxNumberLength;
MemberNames* pMemberNames; MemberNames* pMemberNames;
DecoderData decoderData; GrammarianData grammarianData; /* ptr alignment */
GrammarianData grammarianData; JSON_Parser_EncodingDetectedHandler encodingDetectedHandler; /* ptr alignment */
JSON_Parser_EncodingDetectedHandler encodingDetectedHandler;
JSON_Parser_NullHandler nullHandler; JSON_Parser_NullHandler nullHandler;
JSON_Parser_BooleanHandler booleanHandler; JSON_Parser_BooleanHandler booleanHandler;
JSON_Parser_StringHandler stringHandler; JSON_Parser_StringHandler stringHandler;
@ -1036,6 +977,30 @@ struct JSON_Parser_Data
JSON_Parser_StartArrayHandler startArrayHandler; JSON_Parser_StartArrayHandler startArrayHandler;
JSON_Parser_EndArrayHandler endArrayHandler; JSON_Parser_EndArrayHandler endArrayHandler;
JSON_Parser_ArrayItemHandler arrayItemHandler; JSON_Parser_ArrayItemHandler arrayItemHandler;
uint32_t lexerBits;
DecoderData decoderData;
/* uint32 alignment */
size_t codepointLocationByte;
size_t codepointLocationLine;
size_t codepointLocationColumn;
size_t tokenLocationByte;
size_t tokenLocationLine;
size_t tokenLocationColumn;
size_t depth;
size_t tokenBytesLength;
size_t tokenBytesUsed;
size_t maxStringLength;
size_t maxNumberLength;
ParserState state; /* byte alignment */
ParserFlags flags; /* byte alignment */
Encoding inputEncoding; /* byte alignment */
Encoding stringEncoding; /* byte alignment */
Encoding numberEncoding; /* byte alignment */
Symbol token; /* byte alignment */
TokenAttributes tokenAttributes; /* byte alignment */
Error error; /* byte alignment */
byte errorOffset;
LexerState lexerState; /* byte alignment */
byte defaultTokenBytes[DEFAULT_TOKEN_BYTES_LENGTH]; byte defaultTokenBytes[DEFAULT_TOKEN_BYTES_LENGTH];
}; };

View File

@ -45,15 +45,13 @@ struct rtga
typedef struct typedef struct
{ {
uint32_t img_x, img_y;
int img_n, img_out_n;
int buflen;
uint8_t buffer_start[128];
uint8_t *img_buffer; uint8_t *img_buffer;
uint8_t *img_buffer_end; uint8_t *img_buffer_end;
uint8_t *img_buffer_original; uint8_t *img_buffer_original;
int buflen;
int img_n, img_out_n;
uint32_t img_x, img_y;
uint8_t buffer_start[128];
} rtga_context; } rtga_context;
static INLINE uint8_t rtga_get8(rtga_context *s) static INLINE uint8_t rtga_get8(rtga_context *s)

View File

@ -33,9 +33,9 @@
struct rxml_parse_buffer struct rxml_parse_buffer
{ {
rxml_node_t *stack[32];
char xml[BUFSIZE]; char xml[BUFSIZE];
char val[BUFSIZE]; char val[BUFSIZE];
rxml_node_t* stack[32];
}; };
struct rxml_document struct rxml_document

View File

@ -41,18 +41,19 @@ typedef struct cdfs_track_t
typedef struct cdfs_file_t typedef struct cdfs_file_t
{ {
struct cdfs_track_t* track;
int first_sector; int first_sector;
int current_sector; int current_sector;
unsigned int current_sector_offset;
int sector_buffer_valid; int sector_buffer_valid;
unsigned int current_sector_offset;
unsigned int size; unsigned int size;
unsigned int pos; unsigned int pos;
uint8_t sector_buffer[2048]; uint8_t sector_buffer[2048];
struct cdfs_track_t* track;
} cdfs_file_t; } cdfs_file_t;
/* opens the specified file within the CD or virtual CD. /* opens the specified file within the CD or virtual CD.
* if path is NULL, will open the raw CD (useful for reading CD without having to worry about sector sizes, * if path is NULL, will open the raw CD (useful for
* reading CD without having to worry about sector sizes,
* headers, or checksum data) * headers, or checksum data)
*/ */
int cdfs_open_file(cdfs_file_t* file, cdfs_track_t* stream, const char* path); int cdfs_open_file(cdfs_file_t* file, cdfs_track_t* stream, const char* path);

View File

@ -88,6 +88,8 @@ typedef struct
enum playlist_sort_mode *current_meta_sort_mode_val; enum playlist_sort_mode *current_meta_sort_mode_val;
intfstream_t *file; intfstream_t *file;
playlist_t *playlist; playlist_t *playlist;
JSON_Parser parser; /* ptr alignment */
JSON_Writer writer; /* ptr alignment */
unsigned array_depth; unsigned array_depth;
unsigned object_depth; unsigned object_depth;
@ -96,9 +98,6 @@ typedef struct
bool in_subsystem_roms; bool in_subsystem_roms;
bool capacity_exceeded; bool capacity_exceeded;
bool out_of_memory; bool out_of_memory;
JSON_Parser parser;
JSON_Writer writer;
} JSONContext; } JSONContext;
/* TODO/FIXME - global state - perhaps move outside this file */ /* TODO/FIXME - global state - perhaps move outside this file */