Coverage Report

Created: 2025-12-14 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxls/src/endian.c
Line
Count
Source
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
28.0M
{
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
28.0M
}
50
51
DWORD xlsIntVal (DWORD i)
52
17.8M
{
53
17.8M
    unsigned char c1, c2, c3, c4;
54
55
17.8M
    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
17.8M
    } else {
63
17.8M
        return i;
64
17.8M
    }
65
17.8M
}
66
67
unsigned short xlsShortVal (short s)
68
10.2M
{
69
10.2M
    unsigned char c1, c2;
70
    
71
10.2M
    if (xls_is_bigendian()) {
72
0
        c1 = s & 255;
73
0
        c2 = (s >> 8) & 255;
74
    
75
0
        return (c1 << 8) + c2;
76
10.2M
    } else {
77
10.2M
        return s;
78
10.2M
    }
79
10.2M
}
80
81
void xlsConvertDouble(unsigned char *d)
82
10.4k
{
83
10.4k
    unsigned char t;
84
10.4k
    int i;
85
86
10.4k
    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
10.4k
}
95
96
void xlsConvertBof(BOF *b)
97
4.74M
{
98
4.74M
    b->id = xlsShortVal(b->id);
99
4.74M
    b->size = xlsShortVal(b->size);
100
4.74M
}
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
106
{
114
106
    w->xWn = xlsShortVal(w->xWn);
115
106
    w->yWn = xlsShortVal(w->yWn);
116
106
    w->dxWn = xlsShortVal(w->dxWn);
117
106
    w->dyWn = xlsShortVal(w->dyWn);
118
106
    w->grbit = xlsShortVal(w->grbit);
119
106
    w->itabCur = xlsShortVal(w->itabCur);
120
106
    w->itabFirst = xlsShortVal(w->itabFirst);
121
106
    w->ctabSel = xlsShortVal(w->ctabSel);
122
106
    w->wTabRatio = xlsShortVal(w->wTabRatio);
123
106
}
124
125
void xlsConvertSst(SST *s)
126
315
{
127
315
    s->num = xlsIntVal(s->num);
128
315
    s->numofstr = xlsIntVal(s->numofstr);
129
315
}
130
131
void xlsConvertXf5(XF5 *x)
132
566
{
133
566
    x->font=xlsShortVal(x->font);
134
566
    x->format=xlsShortVal(x->format);
135
566
    x->type=xlsShortVal(x->type);
136
566
    x->align=xlsShortVal(x->align);
137
566
    x->color=xlsShortVal(x->color);
138
566
    x->fill=xlsShortVal(x->fill);
139
566
    x->border=xlsShortVal(x->border);
140
566
    x->linestyle=xlsShortVal(x->linestyle);
141
566
}
142
143
void xlsConvertXf8(XF8 *x)
144
407
{
145
407
    W_ENDIAN(x->font);
146
407
    W_ENDIAN(x->format);
147
407
    W_ENDIAN(x->type);
148
407
    D_ENDIAN(x->linestyle);
149
407
    D_ENDIAN(x->linecolor);
150
407
    W_ENDIAN(x->groundcolor);
151
407
}
152
153
void xlsConvertFont(FONT *f)
154
1.03k
{
155
1.03k
    W_ENDIAN(f->height);
156
1.03k
    W_ENDIAN(f->flag);
157
1.03k
    W_ENDIAN(f->color);
158
1.03k
    W_ENDIAN(f->bold);
159
1.03k
    W_ENDIAN(f->escapement);
160
1.03k
}
161
162
void xlsConvertFormat(FORMAT *f)
163
1.04k
{
164
1.04k
    W_ENDIAN(f->index);
165
1.04k
}
166
167
void xlsConvertBoundsheet(BOUNDSHEET *b)
168
28.1k
{
169
28.1k
    D_ENDIAN(b->filepos);
170
28.1k
}
171
172
void xlsConvertColinfo(COLINFO *c)
173
3.90k
{
174
3.90k
    W_ENDIAN(c->first);
175
3.90k
    W_ENDIAN(c->last);
176
3.90k
    W_ENDIAN(c->width);
177
3.90k
    W_ENDIAN(c->xf);
178
3.90k
    W_ENDIAN(c->flags);
179
3.90k
}
180
181
void xlsConvertRow(ROW *r)
182
20.1k
{
183
20.1k
    W_ENDIAN(r->index);
184
20.1k
    W_ENDIAN(r->fcell);
185
20.1k
    W_ENDIAN(r->lcell);
186
20.1k
    W_ENDIAN(r->height);
187
20.1k
    W_ENDIAN(r->notused);
188
20.1k
    W_ENDIAN(r->notused2);
189
20.1k
    W_ENDIAN(r->flags);
190
20.1k
    W_ENDIAN(r->xf);
191
20.1k
}
192
193
void xlsConvertMergedcells(MERGEDCELLS *m)
194
3.68k
{
195
3.68k
    W_ENDIAN(m->rowf);
196
3.68k
    W_ENDIAN(m->rowl);
197
3.68k
    W_ENDIAN(m->colf);
198
3.68k
    W_ENDIAN(m->coll);
199
3.68k
}
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
11.7k
{
210
11.7k
    W_ENDIAN(f->row);
211
11.7k
    W_ENDIAN(f->col);
212
11.7k
    W_ENDIAN(f->xf);
213
11.7k
  if(f->res == 0xFFFF) {
214
7.13k
    switch(f->resid) {
215
1.92k
    case 0: // string
216
4.36k
    case 1: // bool
217
5.54k
    case 2: // error
218
6.73k
    case 3: // empty string
219
6.73k
      break;
220
403
    default:
221
403
      xlsConvertDouble(&f->resid);
222
403
      break;
223
7.13k
    }
224
7.13k
  } else {
225
4.57k
    xlsConvertDouble(&f->resid);
226
4.57k
  }
227
228
11.7k
    W_ENDIAN(f->flags);
229
11.7k
    W_ENDIAN(f->len);
230
    //fflush(stdout); left over from debugging?
231
11.7k
}
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.30k
{
245
3.30k
    unsigned long i;
246
9.92k
    for (i=0; i<sizeof(h->id)/sizeof(h->id[0]); i++)
247
6.61k
        h->id[i] = xlsIntVal(h->id[i]);
248
16.5k
    for (i=0; i<sizeof(h->clid)/sizeof(h->clid[0]); i++)
249
13.2k
        h->clid[i] = xlsIntVal(h->clid[i]);
250
3.30k
    h->verminor  = xlsShortVal(h->verminor);
251
3.30k
    h->verdll    = xlsShortVal(h->verdll);
252
3.30k
    h->byteorder = xlsShortVal(h->byteorder);
253
3.30k
    h->lsectorB  = xlsShortVal(h->lsectorB);
254
3.30k
    h->lssectorB = xlsShortVal(h->lssectorB);
255
3.30k
    h->reserved1 = xlsShortVal(h->reserved1);
256
3.30k
    h->reserved2 = xlsIntVal(h->reserved2);
257
3.30k
    h->reserved3 = xlsIntVal(h->reserved3);
258
259
3.30k
    h->cfat      = xlsIntVal(h->cfat);
260
3.30k
    h->dirstart  = xlsIntVal(h->dirstart);
261
262
3.30k
    h->reserved4 = xlsIntVal(h->reserved4);
263
264
3.30k
    h->sectorcutoff = xlsIntVal(h->sectorcutoff);
265
3.30k
    h->sfatstart = xlsIntVal(h->sfatstart);
266
3.30k
    h->csfat = xlsIntVal(h->csfat);
267
3.30k
    h->difstart = xlsIntVal(h->difstart);
268
3.30k
    h->cdif = xlsIntVal(h->cdif);
269
363k
    for (i=0; i<sizeof(h->MSAT)/sizeof(h->MSAT[0]); i++)
270
360k
        h->MSAT[i] = xlsIntVal(h->MSAT[i]);
271
3.30k
}
272
273
void xlsConvertPss(PSS* pss)
274
11.5k
{
275
11.5k
    int i;
276
11.5k
    pss->bsize = xlsShortVal(pss->bsize);
277
11.5k
    pss->left  = xlsIntVal(pss->left);
278
11.5k
    pss->right  = xlsIntVal(pss->right);
279
11.5k
    pss->child  = xlsIntVal(pss->child);
280
281
103k
    for(i=0; i<8; i++)
282
92.2k
        pss->guid[i]=xlsShortVal(pss->guid[i]);
283
11.5k
    pss->userflags  = xlsIntVal(pss->userflags);
284
/*    TIME_T  time[2]; */
285
11.5k
    pss->sstart  = xlsIntVal(pss->sstart);
286
11.5k
    pss->size  = xlsIntVal(pss->size);
287
11.5k
    pss->proptype  = xlsIntVal(pss->proptype);
288
11.5k
}