Implement length parse.

This commit is contained in:
Michael Jumper 2013-10-01 20:23:20 -07:00
parent aff1252ff7
commit 4485771df9
2 changed files with 57 additions and 6 deletions

View File

@ -48,9 +48,9 @@
*/ */
/** /**
* The maximum number of bytes per instruction. * The maximum number of characters per instruction.
*/ */
#define GUAC_INSTRUCTION_MAX_LENGTH 32768 #define GUAC_INSTRUCTION_MAX_LENGTH 8192
/** /**
* The maximum number of digits to allow per length prefix. * The maximum number of digits to allow per length prefix.
@ -121,6 +121,16 @@ typedef struct guac_instruction {
*/ */
int __element_remaining_length; int __element_remaining_length;
/**
* The number of elements currently parsed.
*/
int __elementc;
/**
* All currently parsed elements.
*/
char* __elementv[GUAC_INSTRUCTION_MAX_ELEMENTS];
} guac_instruction; } guac_instruction;
/** /**

View File

@ -62,18 +62,59 @@ guac_instruction* guac_instruction_alloc() {
} }
int guac_instruction_append(guac_instruction* instruction, int guac_instruction_append(guac_instruction* instr,
void* buffer, int length) { void* buffer, int length) {
char* char_buffer = (char*) buffer;
int bytes_parsed = 0; int bytes_parsed = 0;
/* Do not exceed maximum number of elements */
if (instr->__elementc == GUAC_INSTRUCTION_MAX_ELEMENTS
&& instr->state != GUAC_INSTRUCTION_PARSE_COMPLETE) {
instr->state = GUAC_INSTRUCTION_PARSE_ERROR;
return 0;
}
/* Parse element length */ /* Parse element length */
if (instruction->state == GUAC_INSTRUCTION_PARSE_LENGTH) { if (instr->state == GUAC_INSTRUCTION_PARSE_LENGTH) {
/* STUB */
int parsed_length = 0;
while (bytes_parsed < length) {
/* Pull next character */
char c = *(char_buffer++);
bytes_parsed++;
/* If digit, add to length */
if (c >= '0' && c <= '9')
parsed_length = parsed_length*10 + c - '0';
/* If period, switch to parsing content */
else if (c == '.') {
instr->__elementv[instr->__elementc++] = char_buffer;
instr->__element_remaining_length = parsed_length;
instr->state = GUAC_INSTRUCTION_PARSE_CONTENT;
break;
}
/* If not digit, parse error */
else {
instr->state = GUAC_INSTRUCTION_PARSE_ERROR;
return 0;
}
}
/* If too long, parse error */
if (parsed_length > GUAC_INSTRUCTION_MAX_LENGTH) {
instr->state = GUAC_INSTRUCTION_PARSE_ERROR;
return 0;
}
} }
/* Parse element content */ /* Parse element content */
if (instruction->state == GUAC_INSTRUCTION_PARSE_CONTENT) { if (instr->state == GUAC_INSTRUCTION_PARSE_CONTENT) {
/* STUB */ /* STUB */
} }