2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
2012-09-06 00:13:05 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2012-09-06 00:13:05 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2012-09-06 00:13:05 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2012-09-06 00:13:05 +00:00
|
|
|
|
|
|
|
#include "error.h"
|
2016-03-01 05:35:32 +00:00
|
|
|
#include "parser.h"
|
2012-09-06 00:13:05 +00:00
|
|
|
#include "socket.h"
|
|
|
|
#include "unicode.h"
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
static void guac_parser_reset(guac_parser* parser) {
|
|
|
|
parser->opcode = NULL;
|
|
|
|
parser->argc = 0;
|
|
|
|
parser->state = GUAC_PARSE_LENGTH;
|
|
|
|
parser->__elementc = 0;
|
|
|
|
parser->__element_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_parser* guac_parser_alloc() {
|
2013-10-02 01:06:19 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Allocate space for parser */
|
|
|
|
guac_parser* parser = malloc(sizeof(guac_parser));
|
|
|
|
if (parser == NULL) {
|
2013-10-02 01:06:19 +00:00
|
|
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
2016-03-01 05:35:32 +00:00
|
|
|
guac_error_message = "Insufficient memory to allocate parser";
|
2013-10-02 01:06:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-01 20:21:00 +00:00
|
|
|
/* Init parse start/end markers */
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->__instructionbuf_unparsed_start = parser->__instructionbuf;
|
|
|
|
parser->__instructionbuf_unparsed_end = parser->__instructionbuf;
|
2013-10-03 23:48:01 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
guac_parser_reset(parser);
|
|
|
|
return parser;
|
2013-10-03 23:48:01 +00:00
|
|
|
|
2013-10-02 01:06:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_parser_append(guac_parser* parser, void* buffer, int length) {
|
2013-10-02 01:06:19 +00:00
|
|
|
|
2013-10-02 03:23:20 +00:00
|
|
|
char* char_buffer = (char*) buffer;
|
2013-10-02 01:06:19 +00:00
|
|
|
int bytes_parsed = 0;
|
|
|
|
|
2013-10-02 03:23:20 +00:00
|
|
|
/* Do not exceed maximum number of elements */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (parser->__elementc == GUAC_INSTRUCTION_MAX_ELEMENTS
|
|
|
|
&& parser->state != GUAC_PARSE_COMPLETE) {
|
|
|
|
parser->state = GUAC_PARSE_ERROR;
|
2013-10-02 03:23:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-02 01:06:19 +00:00
|
|
|
/* Parse element length */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (parser->state == GUAC_PARSE_LENGTH) {
|
2013-10-02 03:23:20 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int parsed_length = parser->__element_length;
|
2013-10-02 03:23:20 +00:00
|
|
|
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 == '.') {
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->__elementv[parser->__elementc++] = char_buffer;
|
|
|
|
parser->state = GUAC_PARSE_CONTENT;
|
2013-10-02 03:23:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not digit, parse error */
|
|
|
|
else {
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->state = GUAC_PARSE_ERROR;
|
2013-10-02 03:23:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If too long, parse error */
|
|
|
|
if (parsed_length > GUAC_INSTRUCTION_MAX_LENGTH) {
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->state = GUAC_PARSE_ERROR;
|
2013-10-02 03:23:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-04 00:16:16 +00:00
|
|
|
/* Save length */
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->__element_length = parsed_length;
|
2013-10-04 00:16:16 +00:00
|
|
|
|
2013-10-02 03:57:54 +00:00
|
|
|
} /* end parse length */
|
2013-10-02 01:06:19 +00:00
|
|
|
|
|
|
|
/* Parse element content */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (parser->state == GUAC_PARSE_CONTENT) {
|
2013-10-02 03:57:54 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
while (bytes_parsed < length && parser->__element_length >= 0) {
|
2013-10-02 03:57:54 +00:00
|
|
|
|
2013-10-03 22:38:22 +00:00
|
|
|
/* Get length of current character */
|
|
|
|
char c = *char_buffer;
|
|
|
|
int char_length = guac_utf8_charsize((unsigned char) c);
|
2013-10-02 03:57:54 +00:00
|
|
|
|
2013-10-03 22:38:22 +00:00
|
|
|
/* If full character not present in buffer, stop now */
|
|
|
|
if (char_length + bytes_parsed > length)
|
|
|
|
break;
|
2013-10-02 03:57:54 +00:00
|
|
|
|
2013-10-03 22:38:22 +00:00
|
|
|
/* Record character as parsed */
|
|
|
|
bytes_parsed += char_length;
|
2013-10-02 03:57:54 +00:00
|
|
|
|
2013-10-03 22:38:22 +00:00
|
|
|
/* If end of element, handle terminator */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (parser->__element_length == 0) {
|
2013-10-02 03:57:54 +00:00
|
|
|
|
2013-10-03 22:38:22 +00:00
|
|
|
*char_buffer = '\0';
|
2013-10-02 03:57:54 +00:00
|
|
|
|
2013-10-03 22:38:22 +00:00
|
|
|
/* If semicolon, store end-of-instruction */
|
|
|
|
if (c == ';') {
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->state = GUAC_PARSE_COMPLETE;
|
|
|
|
parser->opcode = parser->__elementv[0];
|
|
|
|
parser->argv = &(parser->__elementv[1]);
|
|
|
|
parser->argc = parser->__elementc - 1;
|
2013-10-03 22:38:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If comma, move on to next element */
|
|
|
|
else if (c == ',') {
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->state = GUAC_PARSE_LENGTH;
|
2013-10-03 22:38:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, parse error */
|
|
|
|
else {
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->state = GUAC_PARSE_ERROR;
|
2013-10-03 22:38:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* end if end of element */
|
|
|
|
|
|
|
|
/* Advance to next character */
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->__element_length--;
|
2013-10-03 22:38:22 +00:00
|
|
|
char_buffer += char_length;
|
2013-10-02 03:57:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* end parse content */
|
2013-10-02 01:06:19 +00:00
|
|
|
|
2013-10-04 00:16:16 +00:00
|
|
|
return bytes_parsed;
|
2013-10-02 01:06:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_parser_read(guac_parser* parser, guac_socket* socket, int usec_timeout) {
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
char* unparsed_end = parser->__instructionbuf_unparsed_end;
|
|
|
|
char* unparsed_start = parser->__instructionbuf_unparsed_start;
|
|
|
|
char* instr_start = parser->__instructionbuf_unparsed_start;
|
|
|
|
char* buffer_end = parser->__instructionbuf + sizeof(parser->__instructionbuf);
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Begin next instruction if previous was ended */
|
|
|
|
if (parser->state == GUAC_PARSE_COMPLETE)
|
|
|
|
guac_parser_reset(parser);
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
while (parser->state != GUAC_PARSE_COMPLETE
|
|
|
|
&& parser->state != GUAC_PARSE_ERROR) {
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
/* Add any available data to buffer */
|
2016-03-01 05:35:32 +00:00
|
|
|
int parsed = guac_parser_append(parser, unparsed_start, unparsed_end - unparsed_start);
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
/* Read more data if not enough data to parse */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (parsed == 0 && parser->state != GUAC_PARSE_ERROR) {
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
int retval;
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
/* If no space left to read, fail */
|
2013-10-02 18:10:21 +00:00
|
|
|
if (unparsed_end == buffer_end) {
|
2013-10-03 23:48:01 +00:00
|
|
|
|
|
|
|
/* Shift backward if possible */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (instr_start != parser->__instructionbuf) {
|
2013-10-04 00:45:31 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Shift buffer */
|
2016-03-01 05:35:32 +00:00
|
|
|
int offset = instr_start - parser->__instructionbuf;
|
|
|
|
memmove(parser->__instructionbuf, instr_start,
|
2013-10-03 23:48:01 +00:00
|
|
|
unparsed_end - instr_start);
|
2013-10-04 00:45:31 +00:00
|
|
|
|
|
|
|
/* Update tracking pointers */
|
|
|
|
unparsed_end -= offset;
|
|
|
|
unparsed_start -= offset;
|
2016-03-01 05:35:32 +00:00
|
|
|
instr_start = parser->__instructionbuf;
|
2013-10-04 00:45:31 +00:00
|
|
|
|
|
|
|
/* Update parsed elements, if any */
|
2016-03-01 05:35:32 +00:00
|
|
|
for (i=0; i < parser->__elementc; i++)
|
|
|
|
parser->__elementv[i] -= offset;
|
2013-10-04 00:45:31 +00:00
|
|
|
|
2013-10-03 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, no memory to read */
|
|
|
|
else {
|
|
|
|
guac_error = GUAC_STATUS_NO_MEMORY;
|
|
|
|
guac_error_message = "Instruction too long";
|
2016-03-01 05:35:32 +00:00
|
|
|
return -1;
|
2013-10-03 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 00:13:05 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
/* No instruction yet? Get more data ... */
|
|
|
|
retval = guac_socket_select(socket, usec_timeout);
|
|
|
|
if (retval <= 0)
|
2016-03-01 05:35:32 +00:00
|
|
|
return -1;
|
2013-10-02 06:21:49 +00:00
|
|
|
|
|
|
|
/* Attempt to fill buffer */
|
2013-10-02 18:10:21 +00:00
|
|
|
retval = guac_socket_read(socket, unparsed_end,
|
|
|
|
buffer_end - unparsed_end);
|
2013-10-02 06:21:49 +00:00
|
|
|
|
|
|
|
/* Set guac_error if read unsuccessful */
|
|
|
|
if (retval < 0) {
|
|
|
|
guac_error = GUAC_STATUS_SEE_ERRNO;
|
|
|
|
guac_error_message = "Error filling instruction buffer";
|
2016-03-01 05:35:32 +00:00
|
|
|
return -1;
|
2012-09-06 00:13:05 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
/* EOF */
|
|
|
|
if (retval == 0) {
|
2014-11-10 06:59:53 +00:00
|
|
|
guac_error = GUAC_STATUS_CLOSED;
|
2013-10-02 06:21:49 +00:00
|
|
|
guac_error_message = "End of stream reached while "
|
|
|
|
"reading instruction";
|
2016-03-01 05:35:32 +00:00
|
|
|
return -1;
|
2013-10-02 06:21:49 +00:00
|
|
|
}
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 18:10:21 +00:00
|
|
|
/* Update internal buffer */
|
|
|
|
unparsed_end += retval;
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
}
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
/* If data was parsed, advance buffer */
|
|
|
|
else
|
2013-10-02 18:10:21 +00:00
|
|
|
unparsed_start += parsed;
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
} /* end while parsing data */
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2013-10-02 06:21:49 +00:00
|
|
|
/* Fail on error */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (parser->state == GUAC_PARSE_ERROR) {
|
2014-11-10 06:59:53 +00:00
|
|
|
guac_error = GUAC_STATUS_PROTOCOL_ERROR;
|
2013-10-02 06:21:49 +00:00
|
|
|
guac_error_message = "Instruction parse error";
|
2016-03-01 05:35:32 +00:00
|
|
|
return -1;
|
2012-09-06 00:13:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
parser->__instructionbuf_unparsed_start = unparsed_start;
|
|
|
|
parser->__instructionbuf_unparsed_end = unparsed_end;
|
|
|
|
return 0;
|
2013-10-02 06:21:49 +00:00
|
|
|
|
2012-09-06 00:13:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_parser_expect(guac_parser* parser, guac_socket* socket, int usec_timeout, const char* opcode) {
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Read next instruction */
|
|
|
|
if (guac_parser_read(parser, socket, usec_timeout) != 0)
|
|
|
|
return -1;
|
2012-09-06 00:13:05 +00:00
|
|
|
|
|
|
|
/* Validate instruction */
|
2016-03-01 05:35:32 +00:00
|
|
|
if (strcmp(parser->opcode, opcode) != 0) {
|
2014-11-10 06:59:53 +00:00
|
|
|
guac_error = GUAC_STATUS_PROTOCOL_ERROR;
|
2012-09-06 00:13:05 +00:00
|
|
|
guac_error_message = "Instruction read did not have expected opcode";
|
2016-03-01 05:35:32 +00:00
|
|
|
return -1;
|
2012-09-06 00:13:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Return non-zero only if valid instruction read */
|
|
|
|
return parser->state != GUAC_PARSE_COMPLETE;
|
2012-09-06 00:13:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_parser_length(guac_parser* parser) {
|
|
|
|
|
|
|
|
char* unparsed_end = parser->__instructionbuf_unparsed_end;
|
|
|
|
char* unparsed_start = parser->__instructionbuf_unparsed_start;
|
|
|
|
|
|
|
|
return unparsed_end - unparsed_start;
|
|
|
|
|
2012-09-06 00:13:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
int guac_parser_shift(guac_parser* parser, void* buffer, int length) {
|
|
|
|
|
|
|
|
char* copy_end = parser->__instructionbuf_unparsed_end;
|
|
|
|
char* copy_start = parser->__instructionbuf_unparsed_start;
|
|
|
|
|
|
|
|
/* Contain copy region within length */
|
|
|
|
if (copy_end - copy_start > length)
|
|
|
|
copy_end = copy_start + length;
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
/* Copy buffer */
|
|
|
|
length = copy_end - copy_start;
|
|
|
|
memcpy(buffer, copy_start, length);
|
|
|
|
|
|
|
|
parser->__instructionbuf_unparsed_start = copy_end;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
|
|
|
}
|
2012-09-06 00:13:05 +00:00
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
void guac_parser_free(guac_parser* parser) {
|
|
|
|
free(parser);
|
2012-09-06 00:13:05 +00:00
|
|
|
}
|
|
|
|
|