Line | Count | Source (jump to first uncovered line) |
1 | | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | | * |
3 | | * Copyright 2013 Bob Colbert |
4 | | * |
5 | | * This file is part of libxls -- A multiplatform, C/C++ library for parsing |
6 | | * Excel(TM) files. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions are met: |
10 | | * |
11 | | * 1. Redistributions of source code must retain the above copyright notice, |
12 | | * this list of conditions and the following disclaimer. |
13 | | * |
14 | | * 2. Redistributions in binary form must reproduce the above copyright |
15 | | * notice, this list of conditions and the following disclaimer in the |
16 | | * documentation and/or other materials provided with the distribution. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS''AS IS'' |
19 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
22 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | | * POSSIBILITY OF SUCH DAMAGE. |
29 | | * |
30 | | */ |
31 | | |
32 | | #include <stdlib.h> |
33 | | |
34 | | #include "../include/libxls/xlstypes.h" |
35 | | #include "../include/libxls/endian.h" |
36 | | #include "../include/libxls/ole.h" |
37 | | |
38 | | int xls_is_bigendian(void) |
39 | 12.2M | { |
40 | | #if defined (__BIG_ENDIAN__) |
41 | | return 1; |
42 | | #elif defined (__LITTLE_ENDIAN__) |
43 | | return 0; |
44 | | #else |
45 | | static int n = 1; |
46 | | |
47 | | return (*(char *)&n == 0); |
48 | | #endif |
49 | 12.2M | } |
50 | | |
51 | | DWORD xlsIntVal (DWORD i) |
52 | 7.07M | { |
53 | 7.07M | unsigned char c1, c2, c3, c4; |
54 | | |
55 | 7.07M | if (xls_is_bigendian()) { |
56 | 0 | c1 = i & 255; |
57 | 0 | c2 = (i >> 8) & 255; |
58 | 0 | c3 = (i >> 16) & 255; |
59 | 0 | c4 = (i >> 24) & 255; |
60 | |
|
61 | 0 | return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4; |
62 | 7.07M | } else { |
63 | 7.07M | return i; |
64 | 7.07M | } |
65 | 7.07M | } |
66 | | |
67 | | unsigned short xlsShortVal (short s) |
68 | 5.19M | { |
69 | 5.19M | unsigned char c1, c2; |
70 | | |
71 | 5.19M | if (xls_is_bigendian()) { |
72 | 0 | c1 = s & 255; |
73 | 0 | c2 = (s >> 8) & 255; |
74 | | |
75 | 0 | return (c1 << 8) + c2; |
76 | 5.19M | } else { |
77 | 5.19M | return s; |
78 | 5.19M | } |
79 | 5.19M | } |
80 | | |
81 | | void xlsConvertDouble(unsigned char *d) |
82 | 2.93k | { |
83 | 2.93k | unsigned char t; |
84 | 2.93k | int i; |
85 | | |
86 | 2.93k | if (xls_is_bigendian()) { |
87 | 0 | for (i=0; i<4; i++) |
88 | 0 | { |
89 | 0 | t = d[7-i]; |
90 | 0 | d[7-i] = d[i]; |
91 | 0 | d[i] = t; |
92 | 0 | } |
93 | 0 | } |
94 | 2.93k | } |
95 | | |
96 | | void xlsConvertBof(BOF *b) |
97 | 2.44M | { |
98 | 2.44M | b->id = xlsShortVal(b->id); |
99 | 2.44M | b->size = xlsShortVal(b->size); |
100 | 2.44M | } |
101 | | |
102 | | void xlsConvertBiff(BIFF *b) |
103 | 0 | { |
104 | 0 | b->ver = xlsShortVal(b->ver); |
105 | 0 | b->type = xlsShortVal(b->type); |
106 | 0 | b->id_make = xlsShortVal(b->id_make); |
107 | 0 | b->year = xlsShortVal(b->year); |
108 | 0 | b->flags = xlsIntVal(b->flags); |
109 | 0 | b->min_ver = xlsIntVal(b->min_ver); |
110 | 0 | } |
111 | | |
112 | | void xlsConvertWindow(WIND1 *w) |
113 | 91 | { |
114 | 91 | w->xWn = xlsShortVal(w->xWn); |
115 | 91 | w->yWn = xlsShortVal(w->yWn); |
116 | 91 | w->dxWn = xlsShortVal(w->dxWn); |
117 | 91 | w->dyWn = xlsShortVal(w->dyWn); |
118 | 91 | w->grbit = xlsShortVal(w->grbit); |
119 | 91 | w->itabCur = xlsShortVal(w->itabCur); |
120 | 91 | w->itabFirst = xlsShortVal(w->itabFirst); |
121 | 91 | w->ctabSel = xlsShortVal(w->ctabSel); |
122 | 91 | w->wTabRatio = xlsShortVal(w->wTabRatio); |
123 | 91 | } |
124 | | |
125 | | void xlsConvertSst(SST *s) |
126 | 353 | { |
127 | 353 | s->num = xlsIntVal(s->num); |
128 | 353 | s->numofstr = xlsIntVal(s->numofstr); |
129 | 353 | } |
130 | | |
131 | | void xlsConvertXf5(XF5 *x) |
132 | 307 | { |
133 | 307 | x->font=xlsShortVal(x->font); |
134 | 307 | x->format=xlsShortVal(x->format); |
135 | 307 | x->type=xlsShortVal(x->type); |
136 | 307 | x->align=xlsShortVal(x->align); |
137 | 307 | x->color=xlsShortVal(x->color); |
138 | 307 | x->fill=xlsShortVal(x->fill); |
139 | 307 | x->border=xlsShortVal(x->border); |
140 | 307 | x->linestyle=xlsShortVal(x->linestyle); |
141 | 307 | } |
142 | | |
143 | | void xlsConvertXf8(XF8 *x) |
144 | 1.05k | { |
145 | 1.05k | W_ENDIAN(x->font); |
146 | 1.05k | W_ENDIAN(x->format); |
147 | 1.05k | W_ENDIAN(x->type); |
148 | 1.05k | D_ENDIAN(x->linestyle); |
149 | 1.05k | D_ENDIAN(x->linecolor); |
150 | 1.05k | W_ENDIAN(x->groundcolor); |
151 | 1.05k | } |
152 | | |
153 | | void xlsConvertFont(FONT *f) |
154 | 784 | { |
155 | 784 | W_ENDIAN(f->height); |
156 | 784 | W_ENDIAN(f->flag); |
157 | 784 | W_ENDIAN(f->color); |
158 | 784 | W_ENDIAN(f->bold); |
159 | 784 | W_ENDIAN(f->escapement); |
160 | 784 | } |
161 | | |
162 | | void xlsConvertFormat(FORMAT *f) |
163 | 468 | { |
164 | 468 | W_ENDIAN(f->index); |
165 | 468 | } |
166 | | |
167 | | void xlsConvertBoundsheet(BOUNDSHEET *b) |
168 | 23.1k | { |
169 | 23.1k | D_ENDIAN(b->filepos); |
170 | 23.1k | } |
171 | | |
172 | | void xlsConvertColinfo(COLINFO *c) |
173 | 1.82k | { |
174 | 1.82k | W_ENDIAN(c->first); |
175 | 1.82k | W_ENDIAN(c->last); |
176 | 1.82k | W_ENDIAN(c->width); |
177 | 1.82k | W_ENDIAN(c->xf); |
178 | 1.82k | W_ENDIAN(c->flags); |
179 | 1.82k | } |
180 | | |
181 | | void xlsConvertRow(ROW *r) |
182 | 1.45k | { |
183 | 1.45k | W_ENDIAN(r->index); |
184 | 1.45k | W_ENDIAN(r->fcell); |
185 | 1.45k | W_ENDIAN(r->lcell); |
186 | 1.45k | W_ENDIAN(r->height); |
187 | 1.45k | W_ENDIAN(r->notused); |
188 | 1.45k | W_ENDIAN(r->notused2); |
189 | 1.45k | W_ENDIAN(r->flags); |
190 | 1.45k | W_ENDIAN(r->xf); |
191 | 1.45k | } |
192 | | |
193 | | void xlsConvertMergedcells(MERGEDCELLS *m) |
194 | 3.73k | { |
195 | 3.73k | W_ENDIAN(m->rowf); |
196 | 3.73k | W_ENDIAN(m->rowl); |
197 | 3.73k | W_ENDIAN(m->colf); |
198 | 3.73k | W_ENDIAN(m->coll); |
199 | 3.73k | } |
200 | | |
201 | | void xlsConvertCol(COL *c) |
202 | 0 | { |
203 | 0 | W_ENDIAN(c->row); |
204 | 0 | W_ENDIAN(c->col); |
205 | 0 | W_ENDIAN(c->xf); |
206 | 0 | } |
207 | | |
208 | | void xlsConvertFormula(FORMULA *f) |
209 | 3.93k | { |
210 | 3.93k | W_ENDIAN(f->row); |
211 | 3.93k | W_ENDIAN(f->col); |
212 | 3.93k | W_ENDIAN(f->xf); |
213 | 3.93k | if(f->res == 0xFFFF) { |
214 | 2.23k | switch(f->resid) { |
215 | 331 | case 0: // string |
216 | 773 | case 1: // bool |
217 | 1.41k | case 2: // error |
218 | 1.87k | case 3: // empty string |
219 | 1.87k | break; |
220 | 361 | default: |
221 | 361 | xlsConvertDouble(&f->resid); |
222 | 361 | break; |
223 | 2.23k | } |
224 | 2.23k | } else { |
225 | 1.69k | xlsConvertDouble(&f->resid); |
226 | 1.69k | } |
227 | | |
228 | 3.93k | W_ENDIAN(f->flags); |
229 | 3.93k | W_ENDIAN(f->len); |
230 | | //fflush(stdout); left over from debugging? |
231 | 3.93k | } |
232 | | |
233 | | void xlsConvertFormulaArray(FARRAY *f) |
234 | 0 | { |
235 | 0 | W_ENDIAN(f->row1); |
236 | 0 | W_ENDIAN(f->row2); |
237 | 0 | W_ENDIAN(f->col1); |
238 | 0 | W_ENDIAN(f->col2); |
239 | 0 | W_ENDIAN(f->flags); |
240 | 0 | W_ENDIAN(f->len); |
241 | 0 | } |
242 | | |
243 | | void xlsConvertHeader(OLE2Header *h) |
244 | 3.36k | { |
245 | 3.36k | unsigned long i; |
246 | 10.1k | for (i=0; i<sizeof(h->id)/sizeof(h->id[0]); i++) |
247 | 6.73k | h->id[i] = xlsIntVal(h->id[i]); |
248 | 16.8k | for (i=0; i<sizeof(h->clid)/sizeof(h->clid[0]); i++) |
249 | 13.4k | h->clid[i] = xlsIntVal(h->clid[i]); |
250 | 3.36k | h->verminor = xlsShortVal(h->verminor); |
251 | 3.36k | h->verdll = xlsShortVal(h->verdll); |
252 | 3.36k | h->byteorder = xlsShortVal(h->byteorder); |
253 | 3.36k | h->lsectorB = xlsShortVal(h->lsectorB); |
254 | 3.36k | h->lssectorB = xlsShortVal(h->lssectorB); |
255 | 3.36k | h->reserved1 = xlsShortVal(h->reserved1); |
256 | 3.36k | h->reserved2 = xlsIntVal(h->reserved2); |
257 | 3.36k | h->reserved3 = xlsIntVal(h->reserved3); |
258 | | |
259 | 3.36k | h->cfat = xlsIntVal(h->cfat); |
260 | 3.36k | h->dirstart = xlsIntVal(h->dirstart); |
261 | | |
262 | 3.36k | h->reserved4 = xlsIntVal(h->reserved4); |
263 | | |
264 | 3.36k | h->sectorcutoff = xlsIntVal(h->sectorcutoff); |
265 | 3.36k | h->sfatstart = xlsIntVal(h->sfatstart); |
266 | 3.36k | h->csfat = xlsIntVal(h->csfat); |
267 | 3.36k | h->difstart = xlsIntVal(h->difstart); |
268 | 3.36k | h->cdif = xlsIntVal(h->cdif); |
269 | 370k | for (i=0; i<sizeof(h->MSAT)/sizeof(h->MSAT[0]); i++) |
270 | 367k | h->MSAT[i] = xlsIntVal(h->MSAT[i]); |
271 | 3.36k | } |
272 | | |
273 | | void xlsConvertPss(PSS* pss) |
274 | 11.1k | { |
275 | 11.1k | int i; |
276 | 11.1k | pss->bsize = xlsShortVal(pss->bsize); |
277 | 11.1k | pss->left = xlsIntVal(pss->left); |
278 | 11.1k | pss->right = xlsIntVal(pss->right); |
279 | 11.1k | pss->child = xlsIntVal(pss->child); |
280 | | |
281 | 100k | for(i=0; i<8; i++) |
282 | 88.9k | pss->guid[i]=xlsShortVal(pss->guid[i]); |
283 | 11.1k | pss->userflags = xlsIntVal(pss->userflags); |
284 | | /* TIME_T time[2]; */ |
285 | 11.1k | pss->sstart = xlsIntVal(pss->sstart); |
286 | 11.1k | pss->size = xlsIntVal(pss->size); |
287 | 11.1k | pss->proptype = xlsIntVal(pss->proptype); |
288 | 11.1k | } |