Coverage Report

Created: 2026-10-28 07:03

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