-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathD4StreamUnMarshaller.cc
412 lines (345 loc) · 12.3 KB
/
D4StreamUnMarshaller.cc
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// D4StreamUnMarshaller.cc
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2012 OPeNDAP, Inc.
// Author: James Gallagher <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#include "config.h"
#include <byteswap.h>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
// #define DODS_DEBUG2 1
// #define DODS_DEBUG 1
#include "D4StreamUnMarshaller.h"
#include "DapIndent.h"
#include "InternalErr.h"
#include "debug.h"
#include "util.h"
namespace libdap {
/**
* @brief Build a DAP4 Stream unMarshaller.
*
* Build a DAP4 Stream UnMarshaller initialed to read from am istream object.
* Figure out if the words read for values need to be 'twiddled' based on the
* byte-order of the stream an this host (see set_twiddle_bytes()).
*
* @param in Read from this input stream
* @param is_stream_bigendian The byte order of the data in the stream
*/
D4StreamUnMarshaller::D4StreamUnMarshaller(istream &in, bool twiddle_bytes) : d_in(in), d_twiddle_bytes(twiddle_bytes) {
assert(sizeof(std::streamsize) >= sizeof(int64_t));
#if USE_XDR_FOR_IEEE754_ENCODING
// XDR is used to handle transforming non-ieee754 reals, nothing else.
xdrmem_create(&d_source, d_buf, sizeof(dods_float64), XDR_DECODE);
#endif
// This will cause exceptions to be thrown on i/o errors. The exception
// will be ostream::failure
d_in.exceptions(istream::failbit | istream::badbit);
}
/**
* When using this constructor, set_twiddle_bytes() should be called
* before data are processed.
*
* @param in
*/
D4StreamUnMarshaller::D4StreamUnMarshaller(istream &in) : d_in(in), d_twiddle_bytes(false) {
assert(sizeof(std::streamsize) >= sizeof(int64_t));
#if USE_XDR_FOR_IEEE754_ENCODING
// XDR is used to handle transforming non-ieee754 reals, nothing else.
xdrmem_create(&d_source, d_buf, sizeof(dods_float64), XDR_DECODE);
#endif
// This will cause exceptions to be thrown on i/o errors. The exception
// will be ostream::failure
d_in.exceptions(istream::failbit | istream::badbit);
}
D4StreamUnMarshaller::~D4StreamUnMarshaller() {
#if USE_XDR_FOR_IEEE754_ENCODING
xdr_destroy(&d_source);
#endif
}
Crc32::checksum D4StreamUnMarshaller::get_checksum() {
Crc32::checksum c;
d_in.read(reinterpret_cast<char *>(&c), sizeof(Crc32::checksum));
return c;
}
string D4StreamUnMarshaller::get_checksum_str() {
ostringstream oss;
oss.setf(ios::hex, ios::basefield);
oss << setfill('0') << setw(8) << get_checksum();
return oss.str();
}
void D4StreamUnMarshaller::get_byte(dods_byte &val) { d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_byte)); }
void D4StreamUnMarshaller::get_int8(dods_int8 &val) { d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_int8)); }
void D4StreamUnMarshaller::get_int16(dods_int16 &val) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_int16));
if (d_twiddle_bytes)
val = bswap_16(val);
}
void D4StreamUnMarshaller::get_int32(dods_int32 &val) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_int32));
if (d_twiddle_bytes)
val = bswap_32(val);
}
void D4StreamUnMarshaller::get_int64(dods_int64 &val) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_int64));
if (d_twiddle_bytes)
val = bswap_64(val);
}
void D4StreamUnMarshaller::get_float32(dods_float32 &val) {
#if !USE_XDR_FOR_IEEE754_ENCODING
assert(std::numeric_limits<float>::is_iec559);
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_float32));
if (d_twiddle_bytes) {
dods_int32 *i = reinterpret_cast<dods_int32 *>(&val);
*i = bswap_32(*i);
}
#else
if (std::numeric_limits<float>::is_iec559) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_float32));
if (d_twiddle_bytes) {
dods_int32 *i = reinterpret_cast<dods_int32 *>(&val);
*i = bswap_32(*i);
}
} else {
xdr_setpos(&d_source, 0);
d_in.read(d_buf, sizeof(dods_float32));
if (!xdr_float(&d_source, &val))
throw Error("Network I/O Error. Could not read float 64 data.");
}
#endif
}
void D4StreamUnMarshaller::get_float64(dods_float64 &val) {
#if !USE_XDR_FOR_IEEE754_ENCODING
assert(std::numeric_limits<double>::is_iec559);
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_float64));
if (d_twiddle_bytes) {
dods_int64 *i = reinterpret_cast<dods_int64 *>(&val);
*i = bswap_64(*i);
}
#else
if (std::numeric_limits<float>::is_iec559) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_float64));
if (d_twiddle_bytes) {
dods_int64 *i = reinterpret_cast<dods_int64 *>(&val);
*i = bswap_64(*i);
}
} else {
xdr_setpos(&d_source, 0);
d_in.read(d_buf, sizeof(dods_float64));
if (!xdr_double(&d_source, &val))
throw Error("Network I/O Error. Could not read float 64 data.");
}
#endif
}
void D4StreamUnMarshaller::get_uint16(dods_uint16 &val) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_uint16));
if (d_twiddle_bytes)
val = bswap_16(val);
}
void D4StreamUnMarshaller::get_uint32(dods_uint32 &val) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_uint32));
if (d_twiddle_bytes)
val = bswap_32(val);
}
void D4StreamUnMarshaller::get_uint64(dods_uint64 &val) {
d_in.read(reinterpret_cast<char *>(&val), sizeof(dods_uint64));
if (d_twiddle_bytes)
val = bswap_64(val);
}
void D4StreamUnMarshaller::get_str(string &val) {
int64_t len;
d_in.read(reinterpret_cast<char *>(&len), sizeof(int64_t));
val.resize(len);
d_in.read(&val[0], len);
}
void D4StreamUnMarshaller::get_url(string &val) { get_str(val); }
/**
* Read a count value from the stream. This is used with D4Sequence
* which needs to use various other 'get' methods to read its fields.
* Methods like get_opaque_dap4() handle reading their count values
* themselves.
*
* @param count The number of elements to follow.
*/
int64_t D4StreamUnMarshaller::get_count() {
int64_t count;
d_in.read(reinterpret_cast<char *>(&count), sizeof(count));
return count;
}
/**
* Get opaque data when the size of the data to be read is not known in
* advance.
*
* @param val Value-result parameter for the data; caller must delete.
* @param len value-result parameter for the length of the data
*/
void D4StreamUnMarshaller::get_opaque_dap4(char **val, int64_t &len) {
// len = get_length_prefix();
d_in.read(reinterpret_cast<char *>(&len), sizeof(len));
*val = new char[len];
d_in.read(*val, len);
}
void D4StreamUnMarshaller::get_opaque_dap4(vector<uint8_t> &val) {
// len = get_length_prefix();
int64_t len;
d_in.read(reinterpret_cast<char *>(&len), sizeof(len));
val.resize(len);
d_in.read(reinterpret_cast<char *>(val.data()), len);
}
void D4StreamUnMarshaller::get_vector(char *val, int64_t bytes) { d_in.read(val, bytes); }
#if USE_XDR_FOR_IEEE754_ENCODING
void D4StreamUnMarshaller::m_deserialize_reals(char *val, int64_t num, int width, Type type) {
int64_t size = num * width;
// char *buf = (char*)malloc(size); jhrg 7/23/13
vector<char> buf(size);
XDR xdr;
xdrmem_create(&xdr, buf.data(), size, XDR_DECODE);
try {
xdr_setpos(&d_source, 0);
d_in.read(buf.data(), size);
if (!xdr_array(&xdr, &val, (unsigned int *)&num, size, width, XDRUtils::xdr_coder(type)))
throw InternalErr(__FILE__, __LINE__, "Error deserializing a Float64 array");
if (xdr_getpos(&xdr) != size)
throw InternalErr(__FILE__, __LINE__, "Error deserializing a Float64 array");
} catch (...) {
xdr_destroy(&xdr);
throw;
}
xdr_destroy(&xdr);
}
#endif
void D4StreamUnMarshaller::m_twidle_vector_elements(char *vals, int64_t num, int width) {
switch (width) {
case 2: {
dods_int16 *local = reinterpret_cast<dods_int16 *>(vals);
while (num--) {
*local = bswap_16(*local);
local++;
}
break;
}
case 4: {
dods_int32 *local = reinterpret_cast<dods_int32 *>(vals);
;
while (num--) {
*local = bswap_32(*local);
local++;
}
break;
}
case 8: {
dods_int64 *local = reinterpret_cast<dods_int64 *>(vals);
;
while (num--) {
*local = bswap_64(*local);
local++;
}
break;
}
default:
throw InternalErr(__FILE__, __LINE__, "Unrecognized word size.");
}
}
void D4StreamUnMarshaller::get_vector(char *val, int64_t num_elem, int elem_size) {
assert(std::numeric_limits<float>::is_iec559);
assert(std::numeric_limits<double>::is_iec559);
assert(val);
assert(num_elem >= 0);
assert(elem_size > 0);
int64_t bytes;
switch (elem_size) {
case 1:
assert(!"Don't call this method for bytes, use put_vector(val, bytes) instead");
bytes = num_elem;
break;
case 2:
// Don't bother testing the sign bit
assert(!(num_elem & 0x4000000000000000)); // 0x 40 00 --> 0100 0000
bytes = num_elem << 1;
break;
case 4:
assert(!(num_elem & 0x6000000000000000)); // 0x 60 00 --> 0110 0000
bytes = num_elem << 2;
break;
case 8:
assert(!(num_elem & 0x7000000000000000)); // 0111 0000
bytes = num_elem << 3;
break;
default:
bytes = num_elem * elem_size;
break;
}
d_in.read(val, bytes);
if (d_twiddle_bytes)
m_twidle_vector_elements(val, num_elem, elem_size);
}
void D4StreamUnMarshaller::get_vector_float32(char *val, int64_t num_elem) {
#if !USE_XDR_FOR_IEEE754_ENCODING
assert(std::numeric_limits<float>::is_iec559);
assert(val);
assert(num_elem >= 0);
assert(!(num_elem & 0x6000000000000000)); // 0x 60 00 --> 0110 0000
int64_t bytes = num_elem << 2;
d_in.read(val, bytes);
if (d_twiddle_bytes)
m_twidle_vector_elements(val, num_elem, sizeof(dods_float32));
#else
if (type == dods_float32_c && !std::numeric_limits<float>::is_iec559) {
// If not using IEEE 754, use XDR to get it that way.
m_deserialize_reals(val, num, 4, type);
} else if (type == dods_float64_c && !std::numeric_limits<double>::is_iec559) {
m_deserialize_reals(val, num, 8, type);
} else {
d_in.read(val, num * width);
if (d_twiddle_bytes)
m_twidle_vector_elements(val, num, width);
}
#endif
}
void D4StreamUnMarshaller::get_vector_float64(char *val, int64_t num_elem) {
#if !USE_XDR_FOR_IEEE754_ENCODING
assert(std::numeric_limits<float>::is_iec559);
assert(val);
assert(num_elem >= 0);
assert(!(num_elem & 0x7000000000000000)); // 0x 70 00 --> 0111 0000
int64_t bytes = num_elem << 3;
d_in.read(val, bytes);
if (d_twiddle_bytes)
m_twidle_vector_elements(val, num_elem, sizeof(dods_float64));
#else
if (type == dods_float32_c && !std::numeric_limits<float>::is_iec559) {
// If not using IEEE 754, use XDR to get it that way.
m_deserialize_reals(val, num, 4, type);
} else if (type == dods_float64_c && !std::numeric_limits<double>::is_iec559) {
m_deserialize_reals(val, num, 8, type);
} else {
d_in.read(val, num * width);
if (d_twiddle_bytes)
m_twidle_vector_elements(val, num, width);
}
#endif
}
void D4StreamUnMarshaller::dump(ostream &strm) const {
strm << DapIndent::LMarg << "D4StreamUnMarshaller::dump - (" << (void *)this << ")" << endl;
}
} // namespace libdap