libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
XDRStreamUnMarshaller.cc
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 "Array.h"
44 #include "util.h"
45 #include "InternalErr.h"
46 #include "debug.h"
47 #include "DapIndent.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(internal_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 
83 XDRStreamUnMarshaller::~XDRStreamUnMarshaller()
84 {
85  xdr_destroy( &d_source );
86  //&d_source = 0;
87 }
88 
89 void XDRStreamUnMarshaller::get_byte(dods_byte &val)
90 {
91  if (xdr_setpos( &d_source, 0 ) < 0)
92  throw Error("Failed to reposition input stream");
93  if (!(d_in.read(d_buf, 4))) {
94  if (d_in.eof())
95  throw Error("Premature EOF in input stream");
96  else {
97  ostringstream ss("Error reading from input stream: ");
98  ss << d_in.rdstate();
99  throw Error(ss.str());
100  }
101  }
102 
103  DBG2( std::cerr << "_in.gcount(): " << d_in.gcount() << std::endl );
104  DBG2( std::cerr << "_in.tellg(): " << d_in.tellg() << std::endl );
105  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 
115 void XDRStreamUnMarshaller::get_int16(dods_int16 &val)
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 
124 void XDRStreamUnMarshaller::get_int32(dods_int32 &val)
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 
133 void XDRStreamUnMarshaller::get_float32(dods_float32 &val)
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 
142 void XDRStreamUnMarshaller::get_float64(dods_float64 &val)
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 
151 void XDRStreamUnMarshaller::get_uint16(dods_uint16 &val)
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 
160 void XDRStreamUnMarshaller::get_uint32(dods_uint32 &val)
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 
169 void XDRStreamUnMarshaller::get_str(string &val)
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 
218 void XDRStreamUnMarshaller::get_url(string &val)
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 
238 void XDRStreamUnMarshaller::get_int(int &val)
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 
Type
Identifies the data type.
Definition: Type.h:94
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
top level DAP object to house generic methods
Definition: AISConnect.cc:30
static xdrproc_t xdr_coder(const Type &t)
Returns a function used to encode elements of an array.
Definition: XDRUtils.cc:145