1 /******************************************************************************** 2 * Nepenthes 3 * - finest collection - 4 * 5 * 6 * 7 * Copyright (C) 2005 Paul Baecher & Markus Koetter 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License 11 * as published by the Free Software Foundation; either version 2 12 * of the License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22 * 23 * 24 * contact nepenthesdev@users.sourceforge.net 25 * 26 *******************************************************************************/ 27 28 /* $Id: bencoding.h 666 2006-10-14 22:52:57Z common $ */ 29 30 #ifndef HAVE_BENCODING_H 31 #define HAVE_BENCODING_H 32 33 #include <stdint.h> 34 #include <stddef.h> 35 36 #define ERRLEN 0xff 37 38 typedef enum 39 { 40 Bencoding_TypeInt, 41 Bencoding_TypeString, 42 Bencoding_TypeList, 43 Bencoding_TypeDict 44 } Bencoding_ItemType; 45 46 47 typedef struct 48 { 49 void * m_data; 50 size_t m_len; 51 } Bencoding_String; 52 53 typedef int32_t Bencoding_Int; 54 55 struct Bencoding_Item_s; 56 typedef struct Bencoding_Item_s Bencoding_Item; 57 58 typedef struct 59 { 60 uint16_t m_size; 61 uint16_t m_capacity; 62 Bencoding_Item * m_vector; 63 } Bencoding_List; 64 65 typedef struct 66 { 67 uint16_t m_size; 68 uint16_t m_capacity; 69 Bencoding_String * m_keys; 70 Bencoding_Item * m_values; 71 } Bencoding_Dict; 72 73 struct Bencoding_Item_s 74 { 75 Bencoding_ItemType m_type; 76 77 union 78 { 79 Bencoding_Int m_int; 80 Bencoding_String m_string; 81 Bencoding_List m_list; 82 Bencoding_Dict m_dict; 83 }; 84 }; 85 86 /* used for the top-level only. */ 87 typedef struct 88 { 89 uint16_t m_size; 90 uint16_t m_capacity; 91 uint16_t m_iterator; 92 Bencoding_Item * m_vector; 93 } Bencoding_ItemVector; 94 95 typedef struct 96 { 97 void * m_buffer; 98 size_t m_len; 99 100 uint8_t * m_ptr; 101 size_t m_offset; 102 103 Bencoding_ItemVector m_items; 104 105 char m_errorMessage[ERRLEN]; 106 } Bencoding_Context; 107 108 109 110 /* Create a new Bencoding context, return 0 on oom. */ 111 extern Bencoding_Context * Bencoding_createContext(); 112 113 /* Destroy/free a Bencoding context. */ 114 extern void Bencoding_destroyContext(Bencoding_Context *c); 115 116 /* Decode a buffer with Bencoded data, returns -1 on error. */ 117 extern int32_t Bencoding_decodeBuffer(Bencoding_Context *c, const void *ptr, size_t len); 118 119 /* Return the next item from a (decoded) context, returns NULL if there are no more items. */ 120 extern Bencoding_Item * Bencoding_getNext(Bencoding_Context *c); 121 122 /* Get the last error message on a context. */ 123 extern const char * Bencoding_getErrorMessage(Bencoding_Context *c); 124 125 126 #endif 127