forked from Unidata/netcdf-c
/
d4swap.c
260 lines (235 loc) · 7.05 KB
/
d4swap.c
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
/*********************************************************************
* Copyright 2016, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
#include "d4includes.h"
#include <stdarg.h>
#include "d4includes.h"
#include "ezxml.h"
/*
The primary purpose of this code is to recursivly traverse
the incoming data to get the endianness correct.
*/
/* Forward */
static int walkAtomicVar(NCD4meta*, NCD4node*, NCD4node*, void** offsetp);
static int walkOpaqueVar(NCD4meta*,NCD4node*, NCD4node*, void** offsetp);
static int walkStructArray(NCD4meta*,NCD4node*, NCD4node*, void** offsetp);
static int walkStruct(NCD4meta*, NCD4node*, NCD4node*, void** offsetp);
static int walkSeqArray(NCD4meta*, NCD4node*, NCD4node*, void** offsetp);
static int walkSeq(NCD4meta*,NCD4node*, NCD4node*, void** offsetp);
/**************************************************/
/*
Assumes that compiler->swap is true; does necessary
byte swapping.
*/
int
NCD4_swapdata(NCD4meta* compiler, NClist* topvars)
{
int ret = NC_NOERR;
int i;
void* offset;
offset = compiler->serial.dap;
for(i=0;i<nclistlength(topvars);i++) {
NCD4node* var = (NCD4node*)nclistget(topvars,i);
var->data.dap4data.memory = offset;
switch (var->subsort) {
default:
if((ret=walkAtomicVar(compiler,var,var,&offset))) goto done;
break;
case NC_OPAQUE:
/* The only thing we need to do is swap the counts */
if((ret=walkOpaqueVar(compiler,var,var,&offset))) goto done;
break;
case NC_STRUCT:
if((ret=walkStructArray(compiler,var,var,&offset))) goto done;
break;
case NC_SEQ:
if((ret=walkSeqArray(compiler,var,var,&offset))) goto done;
break;
}
var->data.dap4data.size = DELTA(offset,var->data.dap4data.memory);
/* skip checksum, if there is one */
if(compiler->serial.remotechecksumming)
offset = INCR(offset,CHECKSUMSIZE);
}
done:
return THROW(ret);
}
static int
walkAtomicVar(NCD4meta* compiler, NCD4node* topvar, NCD4node* var, void** offsetp)
{
int ret = NC_NOERR;
void* offset;
d4size_t i;
nc_type subsort;
d4size_t dimproduct;
NCD4node* basetype;
basetype = (var->sort == NCD4_TYPE ? var : var->basetype);
subsort = basetype->subsort;
dimproduct = (var->sort == NCD4_TYPE ? 1 : NCD4_dimproduct(var));
offset = *offsetp;
if(subsort == NC_ENUM)
subsort = var->basetype->basetype->subsort;
/* Only need to swap multi-byte integers and floats */
if(subsort != NC_STRING) {
int typesize = NCD4_typesize(subsort);
d4size_t totalsize = typesize*dimproduct;
if(typesize == 1) {
offset = INCR(offset,totalsize);
} else { /*(typesize > 1)*/
for(i=0;i<dimproduct;i++) {
char* sp = (char*)offset;
if(compiler->swap) {
switch (typesize) {
case 2: swapinline16(sp); break;
case 4: swapinline32(sp); break;
case 8: swapinline64(sp); break;
default: break;
}
}
offset = INCR(offset,typesize);
}
}
} else if(subsort == NC_STRING) { /* remaining case; just convert the counts */
COUNTERTYPE count;
for(i=0;i<dimproduct;i++) {
/* Get string count */
if(compiler->swap)
swapinline64(offset);
count = GETCOUNTER(offset);
SKIPCOUNTER(offset);
/* skip count bytes */
offset = INCR(offset,count);
}
}
*offsetp = offset;
return THROW(ret);
}
static int
walkOpaqueVar(NCD4meta* compiler, NCD4node* topvar, NCD4node* var, void** offsetp)
{
int ret = NC_NOERR;
void* offset;
d4size_t i;
unsigned long long count;
d4size_t dimproduct = NCD4_dimproduct(var);
dimproduct = (var->sort == NCD4_TYPE ? 1 : NCD4_dimproduct(var));
offset = *offsetp;
for(i=0;i<dimproduct;i++) {
/* Get and swap opaque count */
if(compiler->swap)
swapinline64(offset);
count = GETCOUNTER(offset);
SKIPCOUNTER(offset);
offset = INCR(offset,count);
}
*offsetp = offset;
return THROW(ret);
}
static int
walkStructArray(NCD4meta* compiler, NCD4node* topvar, NCD4node* var, void** offsetp)
{
int ret = NC_NOERR;
void* offset;
d4size_t i;
d4size_t dimproduct = NCD4_dimproduct(var);
NCD4node* basetype = var->basetype;
offset = *offsetp;
for(i=0;i<dimproduct;i++) {
/* Swap, possibly recursively, the single struct pointed to by offset*/
if((ret=walkStruct(compiler,topvar,basetype,&offset))) goto done;
}
*offsetp = offset;
done:
return THROW(ret);
}
static int
walkStruct(NCD4meta* compiler, NCD4node* topvar, NCD4node* structtype, void** offsetp)
{
int ret = NC_NOERR;
int i;
void* offset;
offset = *offsetp;
for(i=0;i<nclistlength(structtype->vars);i++) {
NCD4node* field = (NCD4node*)nclistget(structtype->vars,i);
NCD4node* fieldbase = field->basetype;
switch (fieldbase->subsort) {
default:
if((ret=walkAtomicVar(compiler,topvar,field,&offset))) goto done;
break;
case NC_OPAQUE:
/* The only thing we need to do is swap the counts */
if((ret=walkOpaqueVar(compiler,topvar,field,&offset))) goto done;
break;
case NC_STRUCT:
if((ret=walkStructArray(compiler,topvar,field,&offset))) goto done;
break;
case NC_SEQ:
if((ret=walkSeqArray(compiler,topvar,field,&offset))) goto done;
break;
}
}
*offsetp = offset;
done:
return THROW(ret);
}
static int
walkSeqArray(NCD4meta* compiler, NCD4node* topvar, NCD4node* var, void** offsetp)
{
int ret = NC_NOERR;
void* offset;
d4size_t i;
d4size_t dimproduct;
NCD4node* seqtype;
assert(var->sort == NCD4_VAR);
dimproduct = NCD4_dimproduct(var);
seqtype = var->basetype;
offset = *offsetp;
for(i=0;i<dimproduct;i++) {
/* Swap, possibly recursively, the single seq pointed to by offset*/
if((ret=walkSeq(compiler,topvar,seqtype,&offset))) goto done;
}
*offsetp = offset;
done:
return THROW(ret);
}
/*
Remember that the base type of var is a vlen.
*/
static int
walkSeq(NCD4meta* compiler, NCD4node* topvar, NCD4node* vlentype, void** offsetp)
{
int ret = NC_NOERR;
int i;
void* offset;
d4size_t recordcount;
NCD4node* basetype;
offset = *offsetp;
/* process the record count */
recordcount = GETCOUNTER(offset);
SKIPCOUNTER(offset);
if(compiler->swap)
swapinline64(&recordcount);
basetype = vlentype->basetype; /* This may be of any type potentially */
assert(basetype->sort == NCD4_TYPE);
for(i=0;i<recordcount;i++) {
switch(basetype->subsort) {
default: /* atomic basetype */
if((ret=walkAtomicVar(compiler,topvar,basetype,&offset))) goto done;
break;
case NC_OPAQUE:
if((ret=walkOpaqueVar(compiler,topvar,basetype,&offset))) goto done;
break;
case NC_STRUCT:
/* We can treat each record like a structure instance */
if((ret=walkStruct(compiler,topvar,basetype,&offset))) goto done;
break;
case NC_SEQ:
if((ret=walkSeq(compiler,topvar,basetype,&offset))) goto done;
break;
}
}
*offsetp = offset;
done:
return THROW(ret);
}