Partial re-implementation of parse.
This commit is contained in:
parent
abc676ef13
commit
1a598db7a1
@ -161,37 +161,21 @@ struct guac_socket {
|
|||||||
char __out_buf[GUAC_SOCKET_OUTPUT_BUFFER_SIZE];
|
char __out_buf[GUAC_SOCKET_OUTPUT_BUFFER_SIZE];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current location of parsing within the instruction buffer.
|
* The number of bytes currently available within the instruction buffer.
|
||||||
*/
|
*/
|
||||||
int __instructionbuf_parse_start;
|
int __instructionbuf_available;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current size of the instruction buffer.
|
* Pointer to the first unused section of the instruction buffer.
|
||||||
*/
|
*/
|
||||||
int __instructionbuf_size;
|
char* __instructionbuf_current;
|
||||||
|
|
||||||
/**
|
|
||||||
* The number of bytes currently in the instruction buffer.
|
|
||||||
*/
|
|
||||||
int __instructionbuf_used_length;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The instruction buffer. This is essentially the input buffer,
|
* The instruction buffer. This is essentially the input buffer,
|
||||||
* provided as a convenience to be used to buffer instructions until
|
* provided as a convenience to be used to buffer instructions until
|
||||||
* those instructions are complete and ready to be parsed.
|
* those instructions are complete and ready to be parsed.
|
||||||
*/
|
*/
|
||||||
char* __instructionbuf;
|
char __instructionbuf[32768];
|
||||||
|
|
||||||
/**
|
|
||||||
* The number of elements parsed so far.
|
|
||||||
*/
|
|
||||||
int __instructionbuf_elementc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Array of pointers into the instruction buffer, where each pointer
|
|
||||||
* points to the start of the corresponding element.
|
|
||||||
*/
|
|
||||||
char* __instructionbuf_elementv[64];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether instructions should be guaranteed atomic across threads using
|
* Whether instructions should be guaranteed atomic across threads using
|
||||||
|
@ -153,229 +153,78 @@ int guac_instruction_append(guac_instruction* instr,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int __guac_fill_instructionbuf(guac_socket* socket) {
|
|
||||||
|
|
||||||
int retval;
|
|
||||||
|
|
||||||
/* Attempt to fill buffer */
|
|
||||||
retval = guac_socket_read(
|
|
||||||
socket,
|
|
||||||
socket->__instructionbuf + socket->__instructionbuf_used_length,
|
|
||||||
socket->__instructionbuf_size - socket->__instructionbuf_used_length
|
|
||||||
);
|
|
||||||
|
|
||||||
/* Set guac_error if recv() unsuccessful */
|
|
||||||
if (retval < 0) {
|
|
||||||
guac_error = GUAC_STATUS_SEE_ERRNO;
|
|
||||||
guac_error_message = "Error filling instruction buffer";
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
socket->__instructionbuf_used_length += retval;
|
|
||||||
|
|
||||||
/* Expand buffer if necessary */
|
|
||||||
if (socket->__instructionbuf_used_length >
|
|
||||||
socket->__instructionbuf_size / 2) {
|
|
||||||
|
|
||||||
socket->__instructionbuf_size *= 2;
|
|
||||||
socket->__instructionbuf = realloc(socket->__instructionbuf,
|
|
||||||
socket->__instructionbuf_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Returns new instruction if one exists, or NULL if no more instructions. */
|
/* Returns new instruction if one exists, or NULL if no more instructions. */
|
||||||
guac_instruction* guac_instruction_read(guac_socket* socket,
|
guac_instruction* guac_instruction_read(guac_socket* socket,
|
||||||
int usec_timeout) {
|
int usec_timeout) {
|
||||||
|
|
||||||
int retval;
|
char* buffer = socket->__instructionbuf_current;
|
||||||
|
int length = socket->__instructionbuf_available;
|
||||||
|
|
||||||
/* Loop until a instruction is read */
|
guac_instruction* instruction = guac_instruction_alloc();
|
||||||
for (;;) {
|
|
||||||
|
|
||||||
/* Length of element, in Unicode characters */
|
while (instruction->state != GUAC_INSTRUCTION_PARSE_COMPLETE
|
||||||
int element_length = 0;
|
&& instruction->state != GUAC_INSTRUCTION_PARSE_ERROR) {
|
||||||
|
|
||||||
/* Length of element, in bytes */
|
/* Add any available data to buffer */
|
||||||
int element_byte_length = 0;
|
int parsed = guac_instruction_append(instruction, buffer, length);
|
||||||
|
|
||||||
/* Current position within the element, in Unicode characters */
|
/* Read more data if not enough data to parse */
|
||||||
int current_unicode_length = 0;
|
if (parsed == 0) {
|
||||||
|
|
||||||
/* Position within buffer */
|
int retval;
|
||||||
int i = socket->__instructionbuf_parse_start;
|
|
||||||
|
|
||||||
/* Parse instruction in buffer */
|
/* If no space left to read, fail */
|
||||||
while (i < socket->__instructionbuf_used_length) {
|
if (length == 0) {
|
||||||
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
||||||
/* Read character from buffer */
|
guac_error_message = "Instruction too long";
|
||||||
char c = socket->__instructionbuf[i++];
|
|
||||||
|
|
||||||
/* If digit, calculate element length */
|
|
||||||
if (c >= '0' && c <= '9')
|
|
||||||
element_length = element_length * 10 + c - '0';
|
|
||||||
|
|
||||||
/* Otherwise, if end of length */
|
|
||||||
else if (c == '.') {
|
|
||||||
|
|
||||||
/* Calculate element byte length by walking buffer */
|
|
||||||
while (i + element_byte_length <
|
|
||||||
socket->__instructionbuf_used_length
|
|
||||||
&& current_unicode_length < element_length) {
|
|
||||||
|
|
||||||
/* Get next byte */
|
|
||||||
c = socket->__instructionbuf[i + element_byte_length];
|
|
||||||
|
|
||||||
/* Update byte and character lengths */
|
|
||||||
element_byte_length += guac_utf8_charsize((unsigned) c);
|
|
||||||
current_unicode_length++;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Verify element is fully read */
|
|
||||||
if (current_unicode_length == element_length) {
|
|
||||||
|
|
||||||
/* Get element value */
|
|
||||||
char* elementv = &(socket->__instructionbuf[i]);
|
|
||||||
|
|
||||||
/* Get terminator, set null terminator of elementv */
|
|
||||||
char terminator = elementv[element_byte_length];
|
|
||||||
elementv[element_byte_length] = '\0';
|
|
||||||
|
|
||||||
/* Move to char after terminator of element */
|
|
||||||
i += element_byte_length+1;
|
|
||||||
|
|
||||||
/* Reset element length */
|
|
||||||
element_length =
|
|
||||||
element_byte_length =
|
|
||||||
current_unicode_length = 0;
|
|
||||||
|
|
||||||
/* As element has been read successfully, update
|
|
||||||
* parse start */
|
|
||||||
socket->__instructionbuf_parse_start = i;
|
|
||||||
|
|
||||||
/* Save element */
|
|
||||||
socket->__instructionbuf_elementv[socket->__instructionbuf_elementc++] = elementv;
|
|
||||||
|
|
||||||
/* Finish parse if terminator is a semicolon */
|
|
||||||
if (terminator == ';') {
|
|
||||||
|
|
||||||
guac_instruction* parsed_instruction;
|
|
||||||
int j;
|
|
||||||
|
|
||||||
/* Allocate instruction */
|
|
||||||
parsed_instruction = malloc(sizeof(guac_instruction));
|
|
||||||
if (parsed_instruction == NULL) {
|
|
||||||
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
||||||
guac_error_message = "Could not allocate memory for parsed instruction";
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Init parsed instruction */
|
|
||||||
parsed_instruction->argc = socket->__instructionbuf_elementc - 1;
|
|
||||||
parsed_instruction->argv = malloc(sizeof(char*) * parsed_instruction->argc);
|
|
||||||
|
|
||||||
/* Fail if memory could not be alloc'd for argv */
|
|
||||||
if (parsed_instruction->argv == NULL) {
|
|
||||||
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
||||||
guac_error_message = "Could not allocate memory for arguments of parsed instruction";
|
|
||||||
free(parsed_instruction);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set opcode */
|
|
||||||
parsed_instruction->opcode = strdup(socket->__instructionbuf_elementv[0]);
|
|
||||||
|
|
||||||
/* Fail if memory could not be alloc'd for opcode */
|
|
||||||
if (parsed_instruction->opcode == NULL) {
|
|
||||||
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
||||||
guac_error_message = "Could not allocate memory for opcode of parsed instruction";
|
|
||||||
free(parsed_instruction->argv);
|
|
||||||
free(parsed_instruction);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Copy element values to parsed instruction */
|
|
||||||
for (j=0; j<parsed_instruction->argc; j++) {
|
|
||||||
parsed_instruction->argv[j] = strdup(socket->__instructionbuf_elementv[j+1]);
|
|
||||||
|
|
||||||
/* Free memory and fail if out of mem */
|
|
||||||
if (parsed_instruction->argv[j] == NULL) {
|
|
||||||
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
||||||
guac_error_message = "Could not allocate memory for single argument of parsed instruction";
|
|
||||||
|
|
||||||
/* Free all alloc'd argv values */
|
|
||||||
while (--j >= 0)
|
|
||||||
free(parsed_instruction->argv[j]);
|
|
||||||
|
|
||||||
free(parsed_instruction->opcode);
|
|
||||||
free(parsed_instruction->argv);
|
|
||||||
free(parsed_instruction);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Reset buffer */
|
|
||||||
memmove(socket->__instructionbuf, socket->__instructionbuf + i, socket->__instructionbuf_used_length - i);
|
|
||||||
socket->__instructionbuf_used_length -= i;
|
|
||||||
socket->__instructionbuf_parse_start = 0;
|
|
||||||
socket->__instructionbuf_elementc = 0;
|
|
||||||
|
|
||||||
/* Done */
|
|
||||||
return parsed_instruction;
|
|
||||||
|
|
||||||
} /* end if terminator */
|
|
||||||
|
|
||||||
/* Error if expected comma is not present */
|
|
||||||
else if (terminator != ',') {
|
|
||||||
guac_error = GUAC_STATUS_BAD_ARGUMENT;
|
|
||||||
guac_error_message = "Element terminator of instruction was not ';' nor ','";
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
} /* end if element fully read */
|
|
||||||
|
|
||||||
/* Otherwise, read more data */
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Error if length is non-numeric or does not end in a period */
|
|
||||||
else {
|
|
||||||
guac_error = GUAC_STATUS_BAD_ARGUMENT;
|
|
||||||
guac_error_message = "Non-numeric character in element length";
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* No instruction yet? Get more data ... */
|
||||||
|
retval = guac_socket_select(socket, usec_timeout);
|
||||||
|
if (retval <= 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* Attempt to fill buffer */
|
||||||
|
retval = guac_socket_read(socket, buffer, length);
|
||||||
|
|
||||||
|
/* Set guac_error if read unsuccessful */
|
||||||
|
if (retval < 0) {
|
||||||
|
guac_error = GUAC_STATUS_SEE_ERRNO;
|
||||||
|
guac_error_message = "Error filling instruction buffer";
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* EOF */
|
||||||
|
if (retval == 0) {
|
||||||
|
guac_error = GUAC_STATUS_NO_INPUT;
|
||||||
|
guac_error_message = "End of stream reached while "
|
||||||
|
"reading instruction";
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update length of internal buffer */
|
||||||
|
length -= retval;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* No instruction yet? Get more data ... */
|
/* If data was parsed, advance buffer */
|
||||||
retval = guac_socket_select(socket, usec_timeout);
|
else
|
||||||
if (retval <= 0)
|
buffer += parsed;
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/* If more data is available, fill into buffer */
|
} /* end while parsing data */
|
||||||
retval = __guac_fill_instructionbuf(socket);
|
|
||||||
|
|
||||||
/* Error, guac_error already set */
|
|
||||||
if (retval < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
/* EOF */
|
|
||||||
if (retval == 0) {
|
|
||||||
guac_error = GUAC_STATUS_NO_INPUT;
|
|
||||||
guac_error_message = "End of stream reached while reading instruction";
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Fail on error */
|
||||||
|
if (instruction->state == GUAC_INSTRUCTION_PARSE_ERROR) {
|
||||||
|
guac_error = GUAC_STATUS_BAD_ARGUMENT;
|
||||||
|
guac_error_message = "Instruction parse error";
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
socket->__instructionbuf_current = buffer;
|
||||||
|
socket->__instructionbuf_available = length;
|
||||||
|
return instruction;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -408,33 +257,13 @@ guac_instruction* guac_instruction_expect(guac_socket* socket, int usec_timeout,
|
|||||||
|
|
||||||
|
|
||||||
void guac_instruction_free(guac_instruction* instruction) {
|
void guac_instruction_free(guac_instruction* instruction) {
|
||||||
|
|
||||||
int argc = instruction->argc;
|
|
||||||
|
|
||||||
/* Free opcode */
|
|
||||||
free(instruction->opcode);
|
|
||||||
|
|
||||||
/* Free argv if set (may be NULL of argc is 0) */
|
|
||||||
if (instruction->argv) {
|
|
||||||
|
|
||||||
/* All argument values */
|
|
||||||
while (argc > 0)
|
|
||||||
free(instruction->argv[--argc]);
|
|
||||||
|
|
||||||
/* Free actual array */
|
|
||||||
free(instruction->argv);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Free instruction */
|
|
||||||
free(instruction);
|
free(instruction);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int guac_instruction_waiting(guac_socket* socket, int usec_timeout) {
|
int guac_instruction_waiting(guac_socket* socket, int usec_timeout) {
|
||||||
|
|
||||||
if (socket->__instructionbuf_used_length > 0)
|
if (socket->__instructionbuf_available < sizeof(socket->__instructionbuf))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return guac_socket_select(socket, usec_timeout);
|
return guac_socket_select(socket, usec_timeout);
|
||||||
|
@ -139,22 +139,9 @@ guac_socket* guac_socket_alloc() {
|
|||||||
socket->__written = 0;
|
socket->__written = 0;
|
||||||
socket->data = NULL;
|
socket->data = NULL;
|
||||||
|
|
||||||
/* Allocate instruction buffer */
|
|
||||||
socket->__instructionbuf_size = 1024;
|
|
||||||
socket->__instructionbuf = malloc(socket->__instructionbuf_size);
|
|
||||||
|
|
||||||
/* If no memory available, return with error */
|
|
||||||
if (socket->__instructionbuf == NULL) {
|
|
||||||
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
||||||
guac_error_message = "Could not allocate memory for instruction buffer";
|
|
||||||
free(socket);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Init members */
|
/* Init members */
|
||||||
socket->__instructionbuf_used_length = 0;
|
socket->__instructionbuf_current = socket->__instructionbuf;
|
||||||
socket->__instructionbuf_parse_start = 0;
|
socket->__instructionbuf_available = sizeof(socket->__instructionbuf);
|
||||||
socket->__instructionbuf_elementc = 0;
|
|
||||||
|
|
||||||
/* Default to unsafe threading */
|
/* Default to unsafe threading */
|
||||||
socket->__threadsafe_instructions = 0;
|
socket->__threadsafe_instructions = 0;
|
||||||
@ -219,7 +206,6 @@ void guac_socket_free(guac_socket* socket) {
|
|||||||
|
|
||||||
guac_socket_flush(socket);
|
guac_socket_flush(socket);
|
||||||
pthread_mutex_destroy(&(socket->__instruction_write_lock));
|
pthread_mutex_destroy(&(socket->__instruction_write_lock));
|
||||||
free(socket->__instructionbuf);
|
|
||||||
free(socket);
|
free(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user