1 /* 2 3 Copyright (C) 2010 Alex Andreotti <alex.andreotti@gmail.com> 4 5 This file is part of chmc. 6 7 chmc is free software: you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation, either version 3 of the License, or 10 (at your option) any later version. 11 12 chmc is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with chmc. If not, see <http://www.gnu.org/licenses/>. 19 20 */ 21 #ifndef CHMC_CHMC_H 22 #define CHMC_CHMC_H 23 24 #include <stdlib.h> 25 #include <limits.h> 26 27 #include "chm.h" 28 #include "list.h" 29 30 #ifndef PATH_MAX 31 #define PATH_MAX 260 32 #endif 33 34 #define CHMC_DIR_UUID \ 35 "\x10\xfd\x01\x7c\xaa\x7b\xd0\x11\x9e\x0c\x00\xa0\xc9\x22\xe6\xec" 36 #define CHMC_STREAM_UUID \ 37 "\x11\xfd\x01\x7c\xaa\x7b\xd0\x11\x9e\x0c\x00\xa0\xc9\x22\xe6\xec" 38 #define CHMC_SYSTEM_UUID \ 39 "\x6a\x92\x02\x5d\x2e\x21\xd0\x11\x9d\xf9\x00\xa0\xc9\x22\xe6\xec" 40 41 struct chmcIndexHeader { 42 char signature[4]; 43 Int32 unknown_4; 44 Int32 unknown_8; 45 Int32 num_of_topic; 46 Int32 unknown_10; 47 Int32 off_img_list; 48 Int32 unknown_18; 49 Int32 img_type_folder; 50 Int32 background; 51 Int32 foreground; 52 Int32 off_font; 53 Int32 win_style; 54 Int32 ex_win_style; 55 Int32 unknown_34; 56 Int32 off_frame_name; 57 Int32 off_win_name; 58 Int32 num_of_info; 59 Int32 unknown_44; 60 Int32 num_of_merge_files; 61 Int32 unknown_4c; 62 Int32 merge_files_offs[1004]; 63 }; 64 65 /* Sys Info Entry codes */ 66 #define SIEC_DEFTOPIC 2 67 #define SIEC_TITLE 3 68 #define SIEC_LCASEFILE 6 69 #define SIEC_DEFWINDOW 5 70 71 /* present in files with Binary Index turned on. (eg: af 08 63 ac) 72 The entry in the #URLTBL file that points to the sitemap index had 73 the same first DWORD */ 74 #define SIEC_HAVE_BINDX 7 75 #define SIEC_NUMOFINFOT 12 76 77 /* The #IDXHDR file contains exactly the same bytes (len 4096) */ 78 #define SIEC_IDXHDR 13 79 80 #define SIEC_INFOCHKSUM 15 81 #define SIEC_DEFFONT 16 82 83 #define SIEC_TIMESTAMP 10 84 #define SIEC_COMPVER 9 85 #define SIEC_SYSINFO 4 86 87 /* NOTE use only as pointer */ 88 #define _CHMC_SYS_ENTRY_HDR_LEN (sizeof(UInt16)*2) 89 struct chmcSystemEntry { 90 UInt16 code; /* FIXME check unsigned */ 91 UInt16 len; /* FIXME check unsigned */ 92 UChar data[65535]; 93 }; 94 95 /* NOTE use only as pointer */ 96 #define _CHMC_SYS_ENTRY_NODE_HDR_LEN \ 97 (sizeof(struct chmcSystemEntryNode *)+_CHMC_SYS_ENTRY_HDR_LEN) 98 99 struct chmcSystemEntryNode { 100 struct chmcSystemEntryNode *next; 101 struct chmcSystemEntry entry; 102 }; 103 104 /* HHA Version 4.72.7294 and earlier */ 105 #define _CHMC_SYS_INFO_V4_72_7294_LEN (28) 106 /* HHA Version 4.72.8086 and later */ 107 #define _CHMC_SYS_INFO_V4_72_8086_LEN (36) 108 struct chmcSystemInfo { 109 UInt32 lcid; 110 UInt32 dbcs; 111 UInt32 full_search; 112 UInt32 klinks; 113 UInt32 alinks; 114 UInt64 timestamp; 115 UInt32 unknown_1c; // >= 8086 only 116 UInt32 unknown_20; // >= 8086 only 117 }; 118 119 120 /* /usr/include/freetype2/freetype/ttnameid.h maybe useful */ 121 #define CHMC_MS_LCID_EN_US (0x0409) 122 123 #define _CHMC_SYSTEM_HDR_LEN (sizeof(Int32)+sizeof(struct chmcSystemInfo)) 124 struct chmcSystem { 125 Int32 version; 126 struct chmcSystemInfo info; 127 128 /* private: */ 129 struct chmcSystemEntryNode *_entries; 130 UInt32 _size; /* keep track for alloc before save */ 131 }; 132 133 #define _CHMC_CHUNK_LEN (4096) 134 #define CHMC_PMGL_DATA_LEN (_CHMC_CHUNK_LEN - _CHMC_PMGL_LEN - 2) 135 136 struct chmcPmglChunk { 137 struct chmcPmglHeader header; 138 UChar data[CHMC_PMGL_DATA_LEN]; 139 UInt16 entries_count; 140 }; 141 142 struct chmcPmglChunkNode { 143 struct list_head list; 144 int data_len; 145 int index_len; 146 struct chmcPmglChunk chunk; 147 }; 148 149 #define CHMC_PMGI_DATA_LEN (_CHMC_CHUNK_LEN - _CHMC_PMGI_LEN - 2) 150 151 struct chmcPmgiChunk { 152 struct chmcPmgiHeader header; 153 UChar data[CHMC_PMGI_DATA_LEN]; 154 UInt16 entries_count; 155 }; 156 157 struct chmcPmgiChunkNode { 158 struct list_head list; 159 int data_len; 160 int index_len; 161 struct chmcPmgiChunk chunk; 162 }; 163 164 #define CHMC_TNFL_STATIC (1 << 0) /* don't free() */ 165 166 struct chmcTreeNode { 167 struct list_head list; 168 UInt32 flags; 169 UInt32 sect_id; 170 char *name; 171 UInt16 prefixlen; 172 UChar *buf; 173 UInt64 offset; 174 UInt64 len; 175 }; 176 177 struct chmcStringChunk { 178 struct list_head list; 179 UInt16 used; 180 UChar data[4096]; 181 }; 182 183 struct chmcConfig { 184 const char *title; 185 const char *tmpdir; 186 const char *hhc; 187 const char *hhk; 188 const char *deftopic; 189 UInt16 language; 190 }; 191 192 struct chmcFile { 193 int fd; 194 struct chmcItsfHeader itsf; 195 struct chmcSect0 sect0; 196 struct chmcItspHeader itsp; 197 int sections_num; 198 struct list_head sections_list; 199 struct chmcSection **sections; 200 struct list_head pmgl_list; 201 struct chmcPmglChunkNode *pmgl_last; 202 struct list_head entries_list; 203 int entries_num; 204 struct chmcTreeNode **sort_entries; 205 struct list_head pmgi_list; 206 struct chmcPmgiChunkNode *pmgi_last; 207 struct chmcSystem system; 208 struct chmcIndexHeader idxhdr; 209 UChar *strings; 210 UInt32 strings_offset; 211 UInt32 strings_len; 212 struct chmcConfig *config; 213 }; 214 215 #define CHMC_SECTNAME_MAXLEN (64) 216 217 struct chmcSection { 218 struct list_head list; 219 char name[CHMC_SECTNAME_MAXLEN]; 220 UInt64 offset; 221 UInt64 len; 222 char filename[PATH_MAX]; 223 int fd; 224 struct chmcLzxcResetTable reset_table_header; 225 struct chmcLzxcControlData control_data; 226 struct list_head mark_list; 227 int mark_count; 228 }; 229 230 #define _CHMC_RSTTBL_MARK (sizeof(struct chmcResetTableMark)) 231 232 struct chmcResetTableMark { 233 UInt64 at; 234 struct list_head list; 235 }; 236 237 struct chmcUrlStrEntry { 238 UInt32 url_offset; 239 UInt32 framename_offset; 240 }; 241 242 struct chmcUtlTblEntry { 243 UInt32 unknown; 244 UInt32 topic_index; 245 UInt32 urlstr_offset; 246 }; 247 248 struct chmcTopicEntry { 249 UInt32 tocidx_offset; 250 UInt32 strings_offset; 251 UInt32 urltbl_offset; 252 short in_content; 253 short unknown; 254 }; 255 256 257 int chmc_init(struct chmcFile *chm, const char *filename, 258 struct chmcConfig *config); 259 void chmc_sections_done(struct chmcFile *chm); 260 void chmc_term(struct chmcFile *chm); 261 int chmc_tree_done(struct chmcFile *chm); 262 263 #define chmc_dump(fmt, ...) fprintf(stderr, fmt , ## __VA_ARGS__) 264 265 #endif /* CHMC_CHMC_H */ 266