File: XDRStreamUnMarshaller.cc

package info (click to toggle)
libdap 3.12.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,764 kB
  • ctags: 6,626
  • sloc: cpp: 35,470; sh: 22,308; ansic: 15,103; exp: 2,008; yacc: 1,789; makefile: 858; xml: 435; perl: 52
file content (340 lines) | stat: -rw-r--r-- 10,039 bytes parent folder | download
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
// XDRStreamUnMarshaller.cc

// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: Patrick West <pwest@ucar.edu>
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// (c) COPYRIGHT URI/MIT 1994-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
//      pwest       Patrick West <pwest@ucar.edu>
#include "config.h"
#include "XDRStreamUnMarshaller.h"

#include <cstring> // for memcpy
#include <string>
#include <sstream>

//#define DODS_DEBUG2 1
//#define DODS_DEBUG 1

#include "Str.h"
// #include "Vector.h"
#include "Array.h"
#include "util.h"
#include "InternalErr.h"
#include "debug.h"

namespace libdap {

char *XDRStreamUnMarshaller::d_buf = 0;

XDRStreamUnMarshaller::XDRStreamUnMarshaller(istream &in) : /*&d_source( 0 ),*/
        d_in(in)
{
    if (!d_buf)
        d_buf = (char *) malloc(XDR_DAP_BUFF_SIZE);
    if (!d_buf)
        throw Error("Failed to allocate memory for data serialization.");

    //&d_source = new XDR;
    xdrmem_create(&d_source, d_buf, XDR_DAP_BUFF_SIZE, XDR_DECODE);
}

XDRStreamUnMarshaller::XDRStreamUnMarshaller() :
        UnMarshaller(), /*&d_source( 0 ),*/d_in(cin)
{
    throw InternalErr(__FILE__, __LINE__, "Default constructor not implemented.");
}

XDRStreamUnMarshaller::XDRStreamUnMarshaller(const XDRStreamUnMarshaller &um) :
        UnMarshaller(um), /*&d_source( 0 ),*/d_in(cin)
{
    throw InternalErr(__FILE__, __LINE__, "Copy constructor not implemented.");
}

XDRStreamUnMarshaller &
XDRStreamUnMarshaller::operator=(const XDRStreamUnMarshaller &)
{
    throw InternalErr(__FILE__, __LINE__, "Copy operator not implemented.");

    return *this;
}

XDRStreamUnMarshaller::~XDRStreamUnMarshaller()
{
    xdr_destroy( &d_source );
    //&d_source = 0;
}

void XDRStreamUnMarshaller::get_byte(dods_byte &val)
{
    if (xdr_setpos( &d_source, 0 ) < 0)
        throw Error("Failed to reposition input stream");
    if (!(d_in.read(d_buf, 4))) {
        if (d_in.eof())
            throw Error("Premature EOF in input stream");
        else {
            ostringstream ss("Error reading from input stream: ");
            ss << d_in.rdstate();
            throw Error(ss.str());
        }
    }

    DBG2( std::cerr << "_in.gcount(): " << d_in.gcount() << std::endl ); DBG2( std::cerr << "_in.tellg(): " << d_in.tellg() << std::endl ); DBG2( std::cerr << "_buf[0]: " << hex << d_buf[0] << "; _buf[1]: " << d_buf[1]
            << "; _buf[2]: " << d_buf[2] << "; _buf[3]: " << d_buf[3]
            << dec << std::endl );

    if (!xdr_char(&d_source, (char *) &val))
        throw Error("Network I/O Error. Could not read byte data.");

    DBG2(std::cerr << "get_byte: " << val << std::endl );
}

void XDRStreamUnMarshaller::get_int16(dods_int16 &val)
{
    xdr_setpos( &d_source, 0);
    d_in.read(d_buf, 4);

    if (!XDR_INT16(&d_source, &val))
        throw Error("Network I/O Error. Could not read int 16 data.");
}

void XDRStreamUnMarshaller::get_int32(dods_int32 &val)
{
    xdr_setpos( &d_source, 0);
    d_in.read(d_buf, 4);

    if (!XDR_INT32(&d_source, &val))
        throw Error("Network I/O Error. Could not read int 32 data.");
}

void XDRStreamUnMarshaller::get_float32(dods_float32 &val)
{
    xdr_setpos( &d_source, 0);
    d_in.read(d_buf, 4);

    if (!xdr_float(&d_source, &val))
        throw Error("Network I/O Error. Could not read float 32 data.");
}

void XDRStreamUnMarshaller::get_float64(dods_float64 &val)
{
    xdr_setpos( &d_source, 0);
    d_in.read(d_buf, 8);

    if (!xdr_double(&d_source, &val))
        throw Error("Network I/O Error. Could not read float 64 data.");
}

void XDRStreamUnMarshaller::get_uint16(dods_uint16 &val)
{
    xdr_setpos( &d_source, 0);
    d_in.read(d_buf, 4);

    if (!XDR_UINT16(&d_source, &val))
        throw Error("Network I/O Error. Could not read uint 16 data.");
}

void XDRStreamUnMarshaller::get_uint32(dods_uint32 &val)
{
    xdr_setpos( &d_source, 0);
    d_in.read(d_buf, 4);

    if (!XDR_UINT32(&d_source, &val))
        throw Error("Network I/O Error. Could not read uint 32 data.");
}

void XDRStreamUnMarshaller::get_str(string &val)
{
    int i;
    get_int(i);
    DBG(std::cerr << "i: " << i << std::endl);

    // Must round up string size to next 4
    i = ((i + 3) / 4) * 4;
    DBG(std::cerr << "i: " << i << std::endl);

    char *in_tmp = 0;
    //char *buf = 0;
    //XDR *source = 0;
    // Must address the case where the string is larger than the buffer
    if (i + 4 > XDR_DAP_BUFF_SIZE) {
#if 0
        char *buf = (char *) malloc(i + 4);
        if (!buf)
            throw InternalErr(__FILE__, __LINE__, "Error allocating memory");
#endif
        vector<char> buf(i+4);

        XDR source;// = new XDR;
        xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
        memcpy(&buf[0], d_buf, 4);

        d_in.read(&buf[0] + 4, i);

        xdr_setpos( &source, 0);
        if (!xdr_string( &source, &in_tmp, max_str_len)) {
            xdr_destroy( &source );
            throw Error("Network I/O Error. Could not read string data.");
        }

        xdr_destroy( &source );
    }
    else {
        d_in.read(d_buf + 4, i);

        xdr_setpos( &d_source, 0);
        if (!xdr_string(&d_source, &in_tmp, max_str_len))
            throw Error("Network I/O Error. Could not read string data.");
    }

    val = in_tmp;

    free(in_tmp);
}

void XDRStreamUnMarshaller::get_url(string &val)
{
    get_str(val);
}

void XDRStreamUnMarshaller::get_opaque(char *val, unsigned int len)
{
    xdr_setpos( &d_source, 0);

    // Round len up to the next multiple of 4. There is also the RNDUP()
    // macro in xdr.h, at least on OS/X.
    len += len & 3;
    if (static_cast<int>(len) > XDR_DAP_BUFF_SIZE)
        throw Error("Network I/O Error. Length of opaque data larger than allowed");

    d_in.read(d_buf, len);

    xdr_opaque(&d_source, val, len);
}

void XDRStreamUnMarshaller::get_int(int &val)
{
    xdr_setpos( &d_source, 0);
    d_in.read(d_buf, 4);

    if (!xdr_int(&d_source, &val))
        throw Error("Network I/O Error(1).");

    DBG(std::cerr << "get_int: " << val << std::endl);
}

void XDRStreamUnMarshaller::get_vector(char **val, unsigned int &num, Vector &)
{
    int i;
    get_int(i); // This leaves the XDR encoded value in d_buf; used later
    DBG(std::cerr << "i: " << i << std::endl);

    // Must round up string size to next 4
    i += i & 3;
    DBG(std::cerr << "i: " << i << std::endl);

    //char *buf = 0;
    //XDR *source = 0;
    // Must address the case where the string is larger than the buffer
    if (i + 4 > XDR_DAP_BUFF_SIZE) {
    	vector<char> buf(i+4);
        XDR source;
        xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
        memcpy(&buf[0], d_buf, 4);

        d_in.read(&buf[0] + 4, i);
        DBG2(cerr << "bytes read: " << d_in.gcount() << endl);

        xdr_setpos(&source, 0);
        if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY)) {
            xdr_destroy(&source);
            throw Error("Network I/O Error. Could not read byte array data.");
        }

        xdr_destroy( &source );
    }
    else {
        d_in.read(d_buf + 4, i);
        DBG2(cerr << "bytes read: " << d_in.gcount() << endl);

        xdr_setpos(&d_source, 0);
        if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY))
            throw Error("Network I/O Error. Could not read byte array data.");
    }
}

void XDRStreamUnMarshaller::get_vector(char **val, unsigned int &num, int width, Vector &vec)
{
    get_vector(val, num, width, vec.var()->type());
}

void XDRStreamUnMarshaller::get_vector(char **val, unsigned int &num, int width, Type type)
{
    int i;
    get_int(i); // This leaves the XDR encoded value in d_buf; used later
    DBG(std::cerr << "i: " << i << std::endl);

    width += width & 3;
    DBG(std::cerr << "width: " << width << std::endl);

    int size = i * width; // + 4; // '+ 4' to hold the int already read

    // Must address the case where the string is larger than the buffer
    if (size > XDR_DAP_BUFF_SIZE) {
    	vector<char> buf(size+4);
        XDR source;
        xdrmem_create(&source, &buf[0], size + 4, XDR_DECODE);
        DBG(cerr << "size: " << size << endl);
        memcpy(&buf[0], d_buf, 4);

        d_in.read(&buf[0] + 4, size); // +4 for the int already read
        DBG(cerr << "bytes read: " << d_in.gcount() << endl);

        xdr_setpos(&source, 0);
        if (!xdr_array(&source, val, &num, DODS_MAX_ARRAY, width, XDRUtils::xdr_coder(type))) {
            xdr_destroy( &source );
            throw Error("Network I/O Error. Could not read array data.");
        }

        xdr_destroy( &source );
    }
    else {
        d_in.read(d_buf + 4, size);
        DBG(cerr << "bytes read (2): " << d_in.gcount() << endl);

        xdr_setpos( &d_source, 0);
        if (!xdr_array(&d_source, val, &num, DODS_MAX_ARRAY, width, XDRUtils::xdr_coder(type)))
            throw Error("Network I/O Error. Could not read array data.");
    }
}

void XDRStreamUnMarshaller::dump(ostream &strm) const
{
    strm << DapIndent::LMarg << "XDRStreamUnMarshaller::dump - (" << (void *) this << ")" << endl;
}

} // namespace libdap