[go: up one dir, main page]

File: memory.c

package info (click to toggle)
libmobi 0.11%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,916 kB
  • sloc: ansic: 19,055; sh: 4,650; makefile: 102
file content (445 lines) | stat: -rw-r--r-- 10,770 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/** @file memory.c
 *  @brief Functions for initializing and releasing structures and data containers
 *
 * Copyright (c) 2014 Bartek Fabiszewski
 * http://www.fabiszewski.net
 *
 * This file is part of libmobi.
 * Licensed under LGPL, either version 3, or any later.
 * See <http://www.gnu.org/licenses/>
 */

#include <stdlib.h>
#include "memory.h"
#include "debug.h"
#include "util.h"

/**
 @brief Initializer for MOBIData structure
 
 It allocates memory for structure.
 Memory should be freed with mobi_free().
 
 @return MOBIData on success, NULL otherwise
 */
MOBIData * mobi_init(void) {
    MOBIData *m = NULL;
    m = calloc(1, sizeof(MOBIData));
    if (m == NULL) { return NULL; }
    m->use_kf8 = true;
    m->kf8_boundary_offset = MOBI_NOTSET;
    m->drm_key = NULL;
    m->ph = NULL;
    m->rh = NULL;
    m->mh = NULL;
    m->eh = NULL;
    m->rec = NULL;
    m->next = NULL;
    m->internals = NULL;
    return m;
}

/**
 @brief Free MOBIMobiHeader structure
 
 @param[in] mh MOBIMobiHeader structure
 */
void mobi_free_mh(MOBIMobiHeader *mh) {
    if (mh == NULL) {
        return;
    }
    free(mh->header_length);
    free(mh->mobi_type);
    free(mh->text_encoding);
    free(mh->uid);
    free(mh->version);
    free(mh->orth_index);
    free(mh->infl_index);
    free(mh->names_index);
    free(mh->keys_index);
    free(mh->extra0_index);
    free(mh->extra1_index);
    free(mh->extra2_index);
    free(mh->extra3_index);
    free(mh->extra4_index);
    free(mh->extra5_index);
    free(mh->non_text_index);
    free(mh->full_name_offset);
    free(mh->full_name_length);
    free(mh->locale);
    free(mh->dict_input_lang);
    free(mh->dict_output_lang);
    free(mh->min_version);
    free(mh->image_index);
    free(mh->huff_rec_index);
    free(mh->huff_rec_count);
    free(mh->datp_rec_index);
    free(mh->datp_rec_count);
    free(mh->exth_flags);
    free(mh->unknown6);
    free(mh->drm_offset);
    free(mh->drm_count);
    free(mh->drm_size);
    free(mh->drm_flags);
    free(mh->fdst_index);
    free(mh->first_text_index);
    free(mh->last_text_index);
    free(mh->fdst_section_count);
    //free(mh->unknown9);
    free(mh->fcis_index);
    free(mh->fcis_count);
    free(mh->flis_index);
    free(mh->flis_count);
    free(mh->unknown10);
    free(mh->unknown11);
    free(mh->srcs_index);
    free(mh->srcs_count);
    free(mh->unknown12);
    free(mh->unknown13);
    free(mh->extra_flags);
    free(mh->ncx_index);
    free(mh->fragment_index);
    free(mh->skeleton_index);
    free(mh->unknown14);
    free(mh->unknown15);
    free(mh->datp_index);
    free(mh->guide_index);
    free(mh->unknown16);
    free(mh->unknown17);
    free(mh->unknown18);
    free(mh->unknown19);
    free(mh->unknown20);
    free(mh->full_name);
    free(mh);
    mh = NULL;
}

/**
 @brief Free all MOBIPdbRecord structures and its respective data attached to MOBIData structure
 
 Each MOBIPdbRecord structure holds metadata and data for each pdb record
 
 @param[in,out] m MOBIData structure
 */
void mobi_free_rec(MOBIData *m) {
    MOBIPdbRecord *curr, *tmp;
    curr = m->rec;
    while (curr != NULL) {
        tmp = curr;
        curr = curr->next;
        free(tmp->data);
        free(tmp);
        tmp = NULL;
    }
    m->rec = NULL;
}

/**
 @brief Free all MOBIExthHeader structures and its respective data attached to MOBIData structure
 
 Each MOBIExthHeader structure holds metadata and data for each EXTH record
 
 @param[in,out] m MOBIData structure
 */
void mobi_free_eh(MOBIData *m) {
    MOBIExthHeader *curr, *tmp;
    curr = m->eh;
    while (curr != NULL) {
        tmp = curr;
        curr = curr->next;
        free(tmp->data);
        free(tmp);
        tmp = NULL;
    }
    m->eh = NULL;
}

/**
 @brief Free MOBIData structure for currenly unused hybrid part and all its children
 
 @param[in] m MOBIData structure
 */
void mobi_free_next(MOBIData *m) {
    if (m && m->next) {
        mobi_free_mh(m->next->mh);
        mobi_free_eh(m->next);
        free(m->next->rh);
        free(m->next);
        m->next = NULL;
    }
}

/**
 @brief Free MOBIData structure and all its children
 
 @param[in] m MOBIData structure
 */
void mobi_free(MOBIData *m) {
    if (m == NULL) {
        return;
    }
    mobi_free_mh(m->mh);
    mobi_free_eh(m);
    mobi_free_rec(m);
    free(m->ph);
    free(m->rh);
    mobi_free_next(m);
    mobi_free_internals(m);
    free(m);
    m = NULL;
}

/**
 @brief Initialize and return MOBIHuffCdic structure.
 
 MOBIHuffCdic structure holds parsed data from HUFF, CDIC records.
 It is used for huffman decompression.
 Initialized structure is a child of MOBIData structure.
 It must be freed with mobi_free_huffcdic().
 
 @return MOBIHuffCdic on success, NULL otherwise
 */
MOBIHuffCdic * mobi_init_huffcdic(void) {
    MOBIHuffCdic *huffcdic = calloc(1, sizeof(MOBIHuffCdic));
    if (huffcdic == NULL) {
        debug_print("%s", "Memory allocation for huffcdic structure failed\n");
        return NULL;
    }
    return huffcdic;
}

/**
 @brief Free MOBIHuffCdic structure and all its children
 
 @param[in] huffcdic MOBIData structure
 */
void mobi_free_huffcdic(MOBIHuffCdic *huffcdic) {
    if (huffcdic == NULL) {
        return;
    }
    free(huffcdic->symbol_offsets);
    free(huffcdic->symbols);
    free(huffcdic);
    huffcdic = NULL;
}

/**
 @brief Initialize and return MOBIRawml structure.
 
 MOBIRawml structure holds parsed text record metadata.
 It is used in the process of parsing rawml text data.
 It must be freed with mobi_free_rawml().
 
 @param[in] m Initialized MOBIData structure
 @return MOBIRawml on success, NULL otherwise
 */
MOBIRawml * mobi_init_rawml(const MOBIData *m) {
    MOBIRawml *rawml = malloc(sizeof(MOBIRawml));
    if (rawml == NULL) {
        debug_print("%s", "Memory allocation failed for rawml structure\n");
        return NULL;
    }
    rawml->version = mobi_get_fileversion(m);
    rawml->fdst = NULL;
    rawml->skel = NULL;
    rawml->frag = NULL;
    rawml->guide = NULL;
    rawml->ncx = NULL;
    rawml->orth = NULL;
    rawml->infl = NULL;
    rawml->flow = NULL;
    rawml->markup = NULL;
    rawml->resources = NULL;
    return rawml;
}

/**
 @brief Free MOBIFdst structure and all its children
 
 @param[in] fdst MOBIFdst structure
 */
void mobi_free_fdst(MOBIFdst *fdst) {
    if (fdst == NULL) {
        return;
    }
    if (fdst->fdst_section_count > 0) {
        free(fdst->fdst_section_starts);
        free(fdst->fdst_section_ends);
    }
    free(fdst);
    fdst = NULL;
}

/**
 @brief Initialize and return MOBIIndx structure.
 
 MOBIIndx structure holds INDX index record entries.
 Must be freed with mobi_free_indx()
 
 @return MOBIIndx on success, NULL otherwise
 */
MOBIIndx * mobi_init_indx(void) {
    MOBIIndx *indx = calloc(1, sizeof(MOBIIndx));
    if (indx == NULL) {
        debug_print("%s", "Memory allocation failed for indx structure\n");
        return NULL;
    }
    indx->entries = NULL;
    indx->cncx_record = NULL;
    indx->orth_index_name = NULL;
    return indx;
}

/**
 @brief Free index entries data and all its children
 
 @param[in] indx MOBIIndx structure that holds indx->entries
 */
void mobi_free_index_entries(MOBIIndx *indx) {
    if (indx == NULL || indx->entries == NULL) {
        return;
    }
    size_t i = 0;
    while (i < indx->entries_count) {
        free(indx->entries[i].label);
        if (indx->entries[i].tags != NULL) {
            size_t j = 0;
            while (j < indx->entries[i].tags_count) {
                free(indx->entries[i].tags[j++].tagvalues);
            }
            free(indx->entries[i].tags);
        }
        i++;
    }
    free(indx->entries);
    indx->entries = NULL;
}

/**
 @brief Free MOBIIndx structure and all its children
 
 @param[in] indx MOBIIndx structure that holds indx->entries
 */
void mobi_free_indx(MOBIIndx *indx) {
    if (indx == NULL) {
        return;
    }
    mobi_free_index_entries(indx);
    if (indx->orth_index_name) {
        free(indx->orth_index_name);
    }
    free(indx);
    indx = NULL;
}

/**
 @brief Free MOBITagx structure and all its children
 
 @param[in] tagx MOBITagx structure
 */
void mobi_free_tagx(MOBITagx *tagx) {
    if (tagx == NULL) {
        return;
    }
    free(tagx->tags);
    free(tagx);
    tagx = NULL;
}

/**
 @brief Free MOBIOrdt structure and all its children
 
 @param[in] ordt MOBIOrdt structure
 */
void mobi_free_ordt(MOBIOrdt *ordt) {
    if (ordt == NULL) {
        return;
    }
    free(ordt->ordt1);
    free(ordt->ordt2);
    free(ordt);
    ordt = NULL;
}

/**
 @brief Free MOBIPart structure
 
 Pointer to data may point to memory area also used by record->data. 
 So we need a flag to leave the memory allocated, while freeing MOBIPart structure
 
 @param[in] part MOBIPart structure
 @param[in] free_data Flag, if set - a pointer to part->data is also released, otherwise not released
 */
void mobi_free_part(MOBIPart *part, int free_data) {
    MOBIPart *curr, *tmp;
    curr = part;
    while (curr != NULL) {
        tmp = curr;
        curr = curr->next;
        if (free_data) { free(tmp->data); }
        free(tmp);
        tmp = NULL;
    }
    part = NULL;
}

/**
 @brief Free MOBIPart structure for opf and ncx data
 
 @param[in] part MOBIPart structure
 */
void mobi_free_opf_data(MOBIPart *part) {
    while (part != NULL) {
        if (part->type == T_NCX || part->type == T_OPF) {
            free(part->data);
        }
        part = part->next;
    }
}

/**
 @brief Free MOBIPart structure for decoded font data
 
 @param[in] part MOBIPart structure
 */
void mobi_free_font_data(MOBIPart *part) {
    while (part != NULL) {
        if (part->type == T_OTF || part->type == T_TTF) {
            free(part->data);
        }
        part = part->next;
    }
}

/**
 @brief Free MOBIRawml structure allocated by mobi_init_rawml()
 
 Pointer to data may point to memory area also used by record->data.
 So we need a flag to leave the memory allocated, while freeing MOBIPart structure
 
 @param[in] rawml MOBIRawml structure
 */
void mobi_free_rawml(MOBIRawml *rawml) {
    if (rawml == NULL) {
        return;
    }
    mobi_free_fdst(rawml->fdst);
    mobi_free_indx(rawml->skel);
    mobi_free_indx(rawml->frag);
    mobi_free_indx(rawml->guide);
    mobi_free_indx(rawml->ncx);
    mobi_free_indx(rawml->orth);
    mobi_free_indx(rawml->infl);
    mobi_free_part(rawml->flow, true);
    mobi_free_part(rawml->markup,true);
    /* do not free resources data, these are links to records data */
    /* only free opf and ncx data */
    mobi_free_opf_data(rawml->resources);
    /* and free decoded fonts data */
    mobi_free_font_data(rawml->resources);
    mobi_free_part(rawml->resources, false);
    free(rawml);
    rawml = NULL;
}