libdap++  Updated for version 3.14.0
XDRStreamUnMarshaller.cc
Go to the documentation of this file.
1 // XDRStreamUnMarshaller.cc
2 
3 // -*- mode: c++; c-basic-offset:4 -*-
4 
5 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
6 // Access Protocol.
7 
8 // Copyright (c) 2002,2003 OPeNDAP, Inc.
9 // Author: Patrick West <pwest@ucar.edu>
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
26 
27 // (c) COPYRIGHT URI/MIT 1994-1999
28 // Please read the full copyright statement in the file COPYRIGHT_URI.
29 //
30 // Authors:
31 // pwest Patrick West <pwest@ucar.edu>
32 #include "config.h"
33 #include "XDRStreamUnMarshaller.h"
34 
35 #include <cstring> // for memcpy
36 #include <string>
37 #include <sstream>
38 
39 //#define DODS_DEBUG2 1
40 //#define DODS_DEBUG 1
41 
42 #include "Str.h"
43 // #include "Vector.h"
44 #include "Array.h"
45 #include "util.h"
46 #include "InternalErr.h"
47 #include "debug.h"
48 
49 namespace libdap {
50 
51 char *XDRStreamUnMarshaller::d_buf = 0;
52 
53 XDRStreamUnMarshaller::XDRStreamUnMarshaller(istream &in) : /*&d_source( 0 ),*/
54  d_in(in)
55 {
56  if (!d_buf)
57  d_buf = (char *) malloc(XDR_DAP_BUFF_SIZE);
58  if (!d_buf)
59  throw Error("Failed to allocate memory for data serialization.");
60 
61  //&d_source = new XDR;
62  xdrmem_create(&d_source, d_buf, XDR_DAP_BUFF_SIZE, XDR_DECODE);
63 }
64 
65 XDRStreamUnMarshaller::XDRStreamUnMarshaller() :
66  UnMarshaller(), /*&d_source( 0 ),*/d_in(cin)
67 {
68  throw InternalErr(__FILE__, __LINE__, "Default constructor not implemented.");
69 }
70 
71 XDRStreamUnMarshaller::XDRStreamUnMarshaller(const XDRStreamUnMarshaller &um) :
72  UnMarshaller(um), /*&d_source( 0 ),*/d_in(cin)
73 {
74  throw InternalErr(__FILE__, __LINE__, "Copy constructor not implemented.");
75 }
76 
77 XDRStreamUnMarshaller &
78 XDRStreamUnMarshaller::operator=(const XDRStreamUnMarshaller &)
79 {
80  throw InternalErr(__FILE__, __LINE__, "Copy operator not implemented.");
81 
82  return *this;
83 }
84 
86 {
87  xdr_destroy( &d_source );
88  //&d_source = 0;
89 }
90 
92 {
93  if (xdr_setpos( &d_source, 0 ) < 0)
94  throw Error("Failed to reposition input stream");
95  if (!(d_in.read(d_buf, 4))) {
96  if (d_in.eof())
97  throw Error("Premature EOF in input stream");
98  else {
99  ostringstream ss("Error reading from input stream: ");
100  ss << d_in.rdstate();
101  throw Error(ss.str());
102  }
103  }
104 
105  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]
106  << "; _buf[2]: " << d_buf[2] << "; _buf[3]: " << d_buf[3]
107  << dec << std::endl );
108 
109  if (!xdr_char(&d_source, (char *) &val))
110  throw Error("Network I/O Error. Could not read byte data.");
111 
112  DBG2(std::cerr << "get_byte: " << val << std::endl );
113 }
114 
116 {
117  xdr_setpos( &d_source, 0);
118  d_in.read(d_buf, 4);
119 
120  if (!XDR_INT16(&d_source, &val))
121  throw Error("Network I/O Error. Could not read int 16 data.");
122 }
123 
125 {
126  xdr_setpos( &d_source, 0);
127  d_in.read(d_buf, 4);
128 
129  if (!XDR_INT32(&d_source, &val))
130  throw Error("Network I/O Error. Could not read int 32 data.");
131 }
132 
134 {
135  xdr_setpos( &d_source, 0);
136  d_in.read(d_buf, 4);
137 
138  if (!xdr_float(&d_source, &val))
139  throw Error("Network I/O Error. Could not read float 32 data.");
140 }
141 
143 {
144  xdr_setpos( &d_source, 0);
145  d_in.read(d_buf, 8);
146 
147  if (!xdr_double(&d_source, &val))
148  throw Error("Network I/O Error. Could not read float 64 data.");
149 }
150 
152 {
153  xdr_setpos( &d_source, 0);
154  d_in.read(d_buf, 4);
155 
156  if (!XDR_UINT16(&d_source, &val))
157  throw Error("Network I/O Error. Could not read uint 16 data.");
158 }
159 
161 {
162  xdr_setpos( &d_source, 0);
163  d_in.read(d_buf, 4);
164 
165  if (!XDR_UINT32(&d_source, &val))
166  throw Error("Network I/O Error. Could not read uint 32 data.");
167 }
168 
170 {
171  int i;
172  get_int(i);
173  DBG(std::cerr << "i: " << i << std::endl);
174 
175  // Must round up string size to next 4
176  i = ((i + 3) / 4) * 4;
177  DBG(std::cerr << "i: " << i << std::endl);
178 
179  char *in_tmp = 0;
180  //char *buf = 0;
181  //XDR *source = 0;
182  // Must address the case where the string is larger than the buffer
183  if (i + 4 > XDR_DAP_BUFF_SIZE) {
184 #if 0
185  char *buf = (char *) malloc(i + 4);
186  if (!buf)
187  throw InternalErr(__FILE__, __LINE__, "Error allocating memory");
188 #endif
189  vector<char> buf(i+4);
190 
191  XDR source;// = new XDR;
192  xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
193  memcpy(&buf[0], d_buf, 4);
194 
195  d_in.read(&buf[0] + 4, i);
196 
197  xdr_setpos( &source, 0);
198  if (!xdr_string( &source, &in_tmp, max_str_len)) {
199  xdr_destroy( &source );
200  throw Error("Network I/O Error. Could not read string data.");
201  }
202 
203  xdr_destroy( &source );
204  }
205  else {
206  d_in.read(d_buf + 4, i);
207 
208  xdr_setpos( &d_source, 0);
209  if (!xdr_string(&d_source, &in_tmp, max_str_len))
210  throw Error("Network I/O Error. Could not read string data.");
211  }
212 
213  val = in_tmp;
214 
215  free(in_tmp);
216 }
217 
219 {
220  get_str(val);
221 }
222 
223 void XDRStreamUnMarshaller::get_opaque(char *val, unsigned int len)
224 {
225  xdr_setpos( &d_source, 0);
226 
227  // Round len up to the next multiple of 4. There is also the RNDUP()
228  // macro in xdr.h, at least on OS/X.
229  len += len & 3;
230  if (static_cast<int>(len) > XDR_DAP_BUFF_SIZE)
231  throw Error("Network I/O Error. Length of opaque data larger than allowed");
232 
233  d_in.read(d_buf, len);
234 
235  xdr_opaque(&d_source, val, len);
236 }
237 
239 {
240  xdr_setpos( &d_source, 0);
241  d_in.read(d_buf, 4);
242 
243  if (!xdr_int(&d_source, &val))
244  throw Error("Network I/O Error(1).");
245 
246  DBG(std::cerr << "get_int: " << val << std::endl);
247 }
248 
249 void XDRStreamUnMarshaller::get_vector(char **val, unsigned int &num, Vector &)
250 {
251  int i;
252  get_int(i); // This leaves the XDR encoded value in d_buf; used later
253  DBG(std::cerr << "i: " << i << std::endl);
254 
255  // Must round up string size to next 4
256  i += i & 3;
257  DBG(std::cerr << "i: " << i << std::endl);
258 
259  //char *buf = 0;
260  //XDR *source = 0;
261  // Must address the case where the string is larger than the buffer
262  if (i + 4 > XDR_DAP_BUFF_SIZE) {
263  vector<char> buf(i+4);
264  XDR source;
265  xdrmem_create(&source, &buf[0], i + 4, XDR_DECODE);
266  memcpy(&buf[0], d_buf, 4);
267 
268  d_in.read(&buf[0] + 4, i);
269  DBG2(cerr << "bytes read: " << d_in.gcount() << endl);
270 
271  xdr_setpos(&source, 0);
272  if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY)) {
273  xdr_destroy(&source);
274  throw Error("Network I/O Error. Could not read byte array data.");
275  }
276 
277  xdr_destroy( &source );
278  }
279  else {
280  d_in.read(d_buf + 4, i);
281  DBG2(cerr << "bytes read: " << d_in.gcount() << endl);
282 
283  xdr_setpos(&d_source, 0);
284  if (!xdr_bytes(&d_source, val, &num, DODS_MAX_ARRAY))
285  throw Error("Network I/O Error. Could not read byte array data.");
286  }
287 }
288 
289 void XDRStreamUnMarshaller::get_vector(char **val, unsigned int &num, int width, Vector &vec)
290 {
291  get_vector(val, num, width, vec.var()->type());
292 }
293 
294 void XDRStreamUnMarshaller::get_vector(char **val, unsigned int &num, int width, Type type)
295 {
296  int i;
297  get_int(i); // This leaves the XDR encoded value in d_buf; used later
298  DBG(std::cerr << "i: " << i << std::endl);
299 
300  width += width & 3;
301  DBG(std::cerr << "width: " << width << std::endl);
302 
303  int size = i * width; // + 4; // '+ 4' to hold the int already read
304 
305  // Must address the case where the string is larger than the buffer
306  if (size > XDR_DAP_BUFF_SIZE) {
307  vector<char> buf(size+4);
308  XDR source;
309  xdrmem_create(&source, &buf[0], size + 4, XDR_DECODE);
310  DBG(cerr << "size: " << size << endl);
311  memcpy(&buf[0], d_buf, 4);
312 
313  d_in.read(&buf[0] + 4, size); // +4 for the int already read
314  DBG(cerr << "bytes read: " << d_in.gcount() << endl);
315 
316  xdr_setpos(&source, 0);
317  if (!xdr_array(&source, val, &num, DODS_MAX_ARRAY, width, XDRUtils::xdr_coder(type))) {
318  xdr_destroy( &source );
319  throw Error("Network I/O Error. Could not read array data.");
320  }
321 
322  xdr_destroy( &source );
323  }
324  else {
325  d_in.read(d_buf + 4, size);
326  DBG(cerr << "bytes read (2): " << d_in.gcount() << endl);
327 
328  xdr_setpos( &d_source, 0);
329  if (!xdr_array(&d_source, val, &num, DODS_MAX_ARRAY, width, XDRUtils::xdr_coder(type)))
330  throw Error("Network I/O Error. Could not read array data.");
331  }
332 }
333 
334 void XDRStreamUnMarshaller::dump(ostream &strm) const
335 {
336  strm << DapIndent::LMarg << "XDRStreamUnMarshaller::dump - (" << (void *) this << ")" << endl;
337 }
338 
339 } // namespace libdap
340 
#define XDR_UINT32
Definition: config.h:968
abstract base class used to unmarshall/deserialize dap data objects
Definition: UnMarshaller.h:54
virtual void get_uint32(dods_uint32 &val)
uint8_t dods_byte
virtual void get_vector(char **val, unsigned int &num, Vector &vec)
Holds a one-dimensional collection of DAP2 data types.
Definition: Vector.h:80
#define XDR_UINT16
Definition: config.h:965
virtual void get_uint16(dods_uint16 &val)
virtual void get_int16(dods_int16 &val)
Type
Identifies the data type.
Definition: Type.h:94
Type type() const
Returns the type of the class instance.
Definition: BaseType.cc:306
#define XDR_INT32
Definition: config.h:962
uint16_t dods_uint16
#define DBG2(x)
Definition: debug.h:73
const int XDR_DAP_BUFF_SIZE
A class for software fault reporting.
Definition: InternalErr.h:64
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
Definition: Vector.cc:387
virtual void get_int32(dods_int32 &val)
#define DBG(x)
Definition: debug.h:58
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
double dods_float64
const unsigned int max_str_len
Definition: Str.h:54
virtual void get_byte(dods_byte &val)
uint32_t dods_uint32
virtual void get_float64(dods_float64 &val)
#define XDR_INT16
Definition: config.h:959
virtual void get_str(string &val)
static ostream & LMarg(ostream &strm)
Definition: DapIndent.cc:80
virtual void get_float32(dods_float32 &val)
virtual void get_url(string &val)
int16_t dods_int16
const int DODS_MAX_ARRAY
Definition: Array.h:61
static xdrproc_t xdr_coder(const Type &t)
Returns a function used to encode elements of an array.
Definition: XDRUtils.cc:145
A class for error processing.
Definition: Error.h:90
virtual void get_opaque(char *val, unsigned int len)
int32_t dods_int32