cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

parser.h (6375B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20
     21#ifndef _GUAC_PARSER_H
     22#define _GUAC_PARSER_H
     23
     24/**
     25 * Provides functions and structures for parsing the Guacamole protocol.
     26 *
     27 * @file parser.h
     28 */
     29
     30#include "parser-types.h"
     31#include "parser-constants.h"
     32#include "socket-types.h"
     33
     34struct guac_parser {
     35
     36    /**
     37     * The opcode of the instruction.
     38     */
     39    char* opcode;
     40
     41    /**
     42     * The number of arguments passed to this instruction.
     43     */
     44    int argc;
     45
     46    /**
     47     * Array of all arguments passed to this instruction.
     48     */
     49    char** argv;
     50
     51    /**
     52     * The parse state of the instruction.
     53     */
     54    guac_parse_state state;
     55
     56    /**
     57     * The length of the current element, if known.
     58     */
     59    int __element_length;
     60
     61    /**
     62     * The number of elements currently parsed.
     63     */
     64    int __elementc;
     65
     66    /**
     67     * All currently parsed elements.
     68     */
     69    char* __elementv[GUAC_INSTRUCTION_MAX_ELEMENTS];
     70
     71    /**
     72     * Pointer to the first character of the current in-progress instruction
     73     * within the buffer.
     74     */
     75    char* __instructionbuf_unparsed_start;
     76
     77    /**
     78     * Pointer to the first unused section of the instruction buffer.
     79     */
     80    char* __instructionbuf_unparsed_end;
     81
     82    /**
     83     * The instruction buffer. This is essentially the input buffer,
     84     * provided as a convenience to be used to buffer instructions until
     85     * those instructions are complete and ready to be parsed.
     86     */
     87    char __instructionbuf[32768];
     88
     89};
     90
     91/**
     92 * Allocates a new parser.
     93 *
     94 * @return The newly allocated parser, or NULL if an error occurs during
     95 *         allocation, in which case guac_error will be set appropriately.
     96 */
     97guac_parser* guac_parser_alloc();
     98
     99/**
    100 * Appends data from the given buffer to the given parser. The data will be
    101 * appended, if possible, to the in-progress instruction as a reference and
    102 * thus the buffer must remain valid throughout the life of the current
    103 * instruction. This function may modify the contents of the buffer when those
    104 * contents are part of an element within the instruction being read.
    105 *
    106 * @param parser The parser to append data to.
    107 * @param buffer A buffer containing data that should be appended to this
    108 *               parser.
    109 * @param length The number of bytes available for appending within the buffer.
    110 * @return The number of bytes appended to this parser, which may be
    111 *         zero if more data is needed.
    112 */
    113int guac_parser_append(guac_parser* parser, void* buffer, int length);
    114
    115/**
    116 * Returns the number of unparsed bytes stored in the given parser's internal
    117 * buffers.
    118 *
    119 * @param parser The parser to return the length of.
    120 * @return The number of unparsed bytes stored in the given parser.
    121 */
    122int guac_parser_length(guac_parser* parser);
    123
    124/**
    125 * Removes up to length bytes from internal buffer of unparsed bytes, storing
    126 * them in the given buffer.
    127 *
    128 * @param parser The parser to remove unparsed bytes from.
    129 * @param buffer The buffer to store the unparsed bytes within.
    130 * @param length The length of the given buffer.
    131 * @return The number of bytes stored in the given buffer.
    132 */
    133int guac_parser_shift(guac_parser* parser, void* buffer, int length);
    134
    135/**
    136 * Frees all memory allocated to the given parser.
    137 *
    138 * @param parser The parser to free.
    139 */
    140void guac_parser_free(guac_parser* parser);
    141
    142/**
    143 * Reads a single instruction from the given guac_socket connection. This
    144 * may result in additional data being read from the guac_socket, stored
    145 * internally within a buffer for future parsing. Future calls to
    146 * guac_parser_read() will read from the interal buffer before reading
    147 * from the guac_socket. Data from the internal buffer can be removed
    148 * and used elsewhere through guac_parser_shift().
    149 *
    150 * If an error occurs reading the instruction, non-zero is returned,
    151 * and guac_error is set appropriately.
    152 *
    153 * @param parser The guac_parser to read instruction data from.
    154 * @param socket The guac_socket connection to use.
    155 * @param usec_timeout The maximum number of microseconds to wait before
    156 *                     giving up.
    157 * @return Zero if an instruction was read within the time allowed, or
    158 *         non-zero if no instruction could be read. If the instruction
    159 *         could not be read completely because the timeout elapsed, in
    160 *         which case guac_error will be set to GUAC_STATUS_INPUT_TIMEOUT
    161 *         and additional calls to guac_parser_read() will be required.
    162 */
    163int guac_parser_read(guac_parser* parser, guac_socket* socket, int usec_timeout);
    164
    165/**
    166 * Reads a single instruction from the given guac_socket. This operates
    167 * identically to guac_parser_read(), except that an error is returned if
    168 * the expected opcode is not received.
    169 *
    170 * If an error occurs reading the instruction, NULL is returned,
    171 * and guac_error is set appropriately.
    172 *
    173 * If the instruction read is not the expected instruction, NULL is returned,
    174 * and guac_error is set to GUAC_STATUS_BAD_STATE.
    175 *
    176 * @param parser The guac_parser to read instruction data from.
    177 * @param socket The guac_socket connection to use.
    178 * @param usec_timeout The maximum number of microseconds to wait before
    179 *                     giving up.
    180 * @param opcode The opcode of the instruction to read.
    181 * @return Zero if an instruction with the given opcode was read, non-zero
    182 *         otherwise. If an instruction was read, but the instruction had a
    183 *         different opcode, non-zero is returned and guac_error is set to
    184 *         GUAC_STATUS_BAD_STATE.
    185 */
    186int guac_parser_expect(guac_parser* parser, guac_socket* socket, int usec_timeout, const char* opcode);
    187
    188#endif
    189