1 // Copyright 2011 Juri Glass, Mathias Runge, Nadim El Sayed 2 // DAI-Labor, TU-Berlin 3 // 4 // This file is part of libSML. 5 // 6 // libSML is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // libSML is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with libSML. If not, see <http://www.gnu.org/licenses/>. 18 19 #ifndef SML_SHARED_H_ 20 #define SML_SHARED_H_ 21 22 #include <stdlib.h> 23 #include <stdint.h> 24 #include <string.h> 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 typedef uint8_t u8; 31 typedef uint16_t u16; 32 typedef uint32_t u32; 33 typedef uint64_t u64; 34 35 typedef int8_t i8; 36 typedef int16_t i16; 37 typedef int32_t i32; 38 typedef int64_t i64; 39 40 #define SML_MESSAGE_END 0x0 41 42 #define SML_TYPE_FIELD 0x70 43 #define SML_LENGTH_FIELD 0xF 44 #define SML_ANOTHER_TL 0x80 45 46 #define SML_TYPE_OCTET_STRING 0x0 47 #define SML_TYPE_BOOLEAN 0x40 48 #define SML_TYPE_INTEGER 0x50 49 #define SML_TYPE_UNSIGNED 0x60 50 #define SML_TYPE_LIST 0x70 51 52 #define SML_OPTIONAL_SKIPPED 0x1 53 54 #define SML_TYPE_NUMBER_8 sizeof(u8) 55 #define SML_TYPE_NUMBER_16 sizeof(u16) 56 #define SML_TYPE_NUMBER_32 sizeof(u32) 57 #define SML_TYPE_NUMBER_64 sizeof(u64) 58 59 // This sml_buffer is used in two different use-cases. 60 // 61 // Parsing: the raw data is in the buffer field, 62 // the buffer_len is the number of raw bytes received, 63 // the cursor points to the current position during parsing 64 // 65 // Writing: At the beginning the buffer field is malloced and zeroed with 66 // a default length, this default length is stored in buffer_len 67 // (i.e. the maximum bytes one can write to this buffer) 68 // cursor points to the position, where on can write during the 69 // writing process. If a file is completely written to the buffer, 70 // cursor is the number of bytes written to the buffer. 71 typedef struct { 72 unsigned char *buffer; 73 size_t buffer_len; 74 size_t cursor; 75 int error; 76 char *error_msg; 77 } sml_buffer; 78 79 sml_buffer *sml_buffer_init(size_t length); 80 81 void sml_buffer_free(sml_buffer *buf); 82 83 // Returns the length of the following data structure. Sets the cursor position to 84 // the value field. 85 int sml_buf_get_next_length(sml_buffer *buf); 86 87 void sml_buf_set_type_and_length(sml_buffer *buf, unsigned int type, unsigned int l); 88 89 // Checks if a error is occured. 90 int sml_buf_has_errors(sml_buffer *buf); 91 92 // Returns the type field of the current byte. 93 int sml_buf_get_next_type(sml_buffer *buf); 94 95 // Returns the current byte. 96 unsigned char sml_buf_get_current_byte(sml_buffer *buf); 97 98 // Returns a pointer to the current buffer position. 99 unsigned char *sml_buf_get_current_buf(sml_buffer *buf); 100 101 void sml_buf_optional_write(sml_buffer *buf); 102 103 // Sets the number of bytes read (moves the cursor forward) 104 void sml_buf_update_bytes_read(sml_buffer *buf, int bytes); 105 106 // Checks if the next field is a skipped optional field, updates the buffer accordingly 107 int sml_buf_optional_is_skipped(sml_buffer *buf); 108 109 // Prints arbitrarily byte string to stdout with printf 110 void hexdump(unsigned char *buffer, size_t buffer_len); 111 112 #ifdef __cplusplus 113 } 114 #endif 115 116 117 #endif /* SML_SHARED_H_ */ 118 119