1 /* wvWare
2 * Copyright (C) Caolan McNamara, Dom Lachowicz, and others
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include "wv.h"
27
28 /*
29 this function retrieves a generic PLCF; this is useful since it
30 means we do not have to add specific functions for retrieving
31 various simple PLCFs (for instance the PLCFs that store information
32 about footnotes, endnotes and annotations are just simple arrays of
33 U32s)
34
35 plcf - a pointer to a pointer where we should allocate the structure
36 the caller needs to free these using wvFree() once not needed
37
38 offset - an offset in the stream fd where the PLCF starts
39 len - a length in bytes (!!!) of the PLCF
40 fd - the stream from which to read the PLCF
41 */
42 int
wvGetPLCF(void ** plcf,U32 offset,U32 len,wvStream * fd)43 wvGetPLCF (void ** plcf, U32 offset, U32 len, wvStream * fd)
44 {
45 U32 i, i32, i8;
46
47 if (len == 0)
48 {
49 *plcf = NULL;
50 }
51 else
52 {
53 *plcf = wvMalloc (len);
54 if (*plcf == NULL)
55 {
56 wvError (("NO MEM 1, failed to alloc %d bytes\n",len));
57 return (1);
58 }
59
60 wvStream_goto (fd, offset);
61
62 i32 = len / 4;
63 i8 = len % 4;
64
65 for (i = 0; i < i32; i++)
66 ((U32*)(*plcf))[i] = read_32ubit (fd);
67
68 for (i = i32*4; i < i32*4 + i8; i++)
69 ((U8*)(*plcf))[i] = read_8ubit (fd);
70 }
71 return (0);
72 }
73