51 char *XDRStreamMarshaller::d_buf = 0;
53 #define XDR_DAP_BUFF_SIZE 256
63 XDRStreamMarshaller::XDRStreamMarshaller(ostream &out) : d_out(out)
68 throw Error(
"Failed to allocate memory for data serialization.");
74 _MD_CTX = EVP_MD_CTX_create();
79 XDRStreamMarshaller::XDRStreamMarshaller() :
82 throw InternalErr(__FILE__, __LINE__,
"Default constructor not implemented.");
85 XDRStreamMarshaller::XDRStreamMarshaller(
const XDRStreamMarshaller &m) :
86 Marshaller(m), d_out(cout)
88 throw InternalErr(__FILE__, __LINE__,
"Copy constructor not implemented.");
92 XDRStreamMarshaller::operator=(
const XDRStreamMarshaller &)
94 throw InternalErr(__FILE__, __LINE__,
"Copy operator not implemented.");
101 xdr_destroy(&d_sink);
105 EVP_MD_CTX_destroy(_MD_CTX);
114 void XDRStreamMarshaller::reset_checksum()
118 throw InternalErr( __FILE__, __LINE__,
"reset_checksum() called by checksum is not enabled.");
120 if (EVP_DigestInit_ex(_MD_CTX, EVP_sha1(), 0) == 0)
121 throw Error(
"Failed to initialize checksum object.");
123 _checksum_ctx_valid =
true;
132 string XDRStreamMarshaller::get_checksum()
136 throw InternalErr(__FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
138 if (_checksum_ctx_valid) {
141 _checksum_ctx_valid =
false;
143 vector<unsigned char> md(EVP_MAX_MD_SIZE);
145 if (EVP_DigestFinal_ex(_MD_CTX, &md[0], &md_len) == 0)
146 throw Error(
"Error computing the checksum (checksum computation).");
149 oss.setf(ios::hex, ios::basefield);
150 for (
unsigned int i = 0; i < md_len; ++i) {
151 oss << setfill(
'0') << setw(2) << (
unsigned int) md[i];
154 _checksum = oss.str();
163 void XDRStreamMarshaller::checksum_update(
const void *data,
unsigned long len)
167 throw InternalErr( __FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
169 if (!_checksum_ctx_valid)
170 throw InternalErr( __FILE__, __LINE__,
"Invalid checksum context (checksum update).");
172 if (EVP_DigestUpdate(_MD_CTX, data, len) == 0) {
173 _checksum_ctx_valid =
false;
174 throw Error(
"Error computing the checksum (checksum update).");
184 checksum_update(&val,
sizeof(
dods_byte));
186 DBG( std::cerr <<
"put_byte: " << val << std::endl );
188 if (!xdr_setpos( &d_sink, 0 ))
189 throw Error(
"Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
191 if (!xdr_char(&d_sink, (
char *) &val))
192 throw Error(
"Network I/O Error. Could not send byte data.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
194 unsigned int bytes_written = xdr_getpos( &d_sink );
196 throw Error(
"Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
198 d_out.write(d_buf, bytes_written);
207 if (!xdr_setpos( &d_sink, 0 ))
208 throw Error(
"Network I/O Error. Could not send int 16 data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
211 throw Error(
"Network I/O Error. Could not send int 16 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
213 unsigned int bytes_written = xdr_getpos( &d_sink );
215 throw Error(
"Network I/O Error. Could not send int 16 data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
217 d_out.write(d_buf, bytes_written);
226 if (!xdr_setpos( &d_sink, 0 ))
227 throw Error(
"Network I/O Error. Could not send int 32 data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
230 throw Error(
"Network I/O Error. Culd not read int 32 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
232 unsigned int bytes_written = xdr_getpos( &d_sink );
234 throw Error(
"Network I/O Error. Could not send int 32 data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
236 d_out.write(d_buf, bytes_written);
245 if (!xdr_setpos( &d_sink, 0 ))
246 throw Error(
"Network I/O Error. Could not send float 32 data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
248 if (!xdr_float(&d_sink, &val))
249 throw Error(
"Network I/O Error. Could not send float 32 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
251 unsigned int bytes_written = xdr_getpos( &d_sink );
253 throw Error(
"Network I/O Error. Could not send float 32 data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
255 d_out.write(d_buf, bytes_written);
264 if (!xdr_setpos( &d_sink, 0 ))
265 throw Error(
"Network I/O Error. Could not send float 64 data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
267 if (!xdr_double(&d_sink, &val))
268 throw Error(
"Network I/O Error. Could not send float 64 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
270 unsigned int bytes_written = xdr_getpos( &d_sink );
272 throw Error(
"Network I/O Error. Could not send float 64 data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
274 d_out.write(d_buf, bytes_written);
283 if (!xdr_setpos( &d_sink, 0 ))
284 throw Error(
"Network I/O Error. Could not send uint 16 data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
287 throw Error(
"Network I/O Error. Could not send uint 16 data. This may be due to a\nbug in libdap or a problem with the network connection.");
289 unsigned int bytes_written = xdr_getpos( &d_sink );
291 throw Error(
"Network I/O Error. Could not send uint 16 data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
293 d_out.write(d_buf, bytes_written);
302 if (!xdr_setpos( &d_sink, 0 ))
303 throw Error(
"Network I/O Error. Could not send uint 32 data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
306 throw Error(
"Network I/O Error. Could not send uint 32 data. This may be due to a\nbug in libdap or a problem with the network connection.");
308 unsigned int bytes_written = xdr_getpos( &d_sink );
310 throw Error(
"Network I/O Error. Could not send uint 32 data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
312 d_out.write(d_buf, bytes_written);
319 checksum_update(val.c_str(), val.length());
321 int size = val.length() + 8;
323 char *str_buf = (
char *) malloc(size);
326 throw Error(
"Failed to allocate memory for string data serialization.");
331 vector<char> str_buf(size);
334 xdrmem_create(&str_sink, &str_buf[0], size, XDR_ENCODE);
336 if (!xdr_setpos( &str_sink, 0 ))
338 "Network I/O Error. Could not send string data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
340 const char *out_tmp = val.c_str();
341 if (!xdr_string(&str_sink, (
char **) &out_tmp, size))
343 "Network I/O Error. Could not send string data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
345 unsigned int bytes_written = xdr_getpos( &str_sink );
348 "Network I/O Error. Could not send string data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
350 d_out.write(&str_buf[0], bytes_written);
352 xdr_destroy(&str_sink);
355 xdr_destroy(&str_sink);
369 checksum_update(&val, len);
372 throw Error(
"Network I/O Error. Could not send opaque data - length of opaque data larger than allowed");
374 if (!xdr_setpos( &d_sink, 0 ))
375 throw Error(
"Network I/O Error. Could not send opaque data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
377 if (!xdr_opaque(&d_sink, val, len))
378 throw Error(
"Network I/O Error. Could not send opaque data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
380 unsigned int bytes_written = xdr_getpos( &d_sink );
382 throw Error(
"Network I/O Error. Could not send opaque data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
384 d_out.write(d_buf, bytes_written);
391 checksum_update(&val,
sizeof(
int));
393 if (!xdr_setpos( &d_sink, 0 ))
394 throw Error(
"Network I/O Error. Could not send int data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
396 if (!xdr_int(&d_sink, &val))
397 throw Error(
"Network I/O Error(1). Could not send int data.\nThis may be due to a bug in libdap or a\nproblem with the network connection.");
399 unsigned int bytes_written = xdr_getpos( &d_sink );
401 throw Error(
"Network I/O Error. Could not send int data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
403 d_out.write(d_buf, bytes_written);
408 if (!val)
throw InternalErr(__FILE__, __LINE__,
"Could not send byte vector data. Buffer pointer is not set.");
411 checksum_update(val, num);
417 const unsigned int add_to = 8;
419 char *byte_buf = (
char *) malloc(num + add_to);
420 if (!byte_buf)
throw Error(
"Failed to allocate memory for byte vector data serialization.");
422 vector<char> byte_buf(num + add_to);
425 xdrmem_create(&byte_sink, &byte_buf[0], num + add_to, XDR_ENCODE);
426 if (!xdr_setpos( &byte_sink, 0 ))
428 "Network I/O Error. Could not send byte vector data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
430 if (!xdr_bytes(&byte_sink, (
char **) &val, (
unsigned int *) &num, num + add_to))
432 "Network I/O Error(2). Could not send byte vector data.\nThis may be due to a bug in libdap or a\nproblem with the network connection.");
434 unsigned int bytes_written = xdr_getpos( &byte_sink );
437 "Network I/O Error. Could not send byte vector data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
439 d_out.write(&byte_buf[0], bytes_written);
441 xdr_destroy(&byte_sink);
444 xdr_destroy(&byte_sink);
458 if (!val)
throw InternalErr(__FILE__, __LINE__,
"Buffer pointer is not set.");
461 checksum_update(val, num * width);
466 int use_width = width;
467 if (use_width < 4) use_width = 4;
471 int size = (num * use_width) + 4;
475 char *vec_buf = (
char *) malloc(size);
477 throw Error(
"Failed to allocate memory for vector data serialization.");
479 vector<char> vec_buf(size);
482 xdrmem_create(&vec_sink, &vec_buf[0], size, XDR_ENCODE);
485 if (!xdr_setpos( &vec_sink, 0 ))
487 "Network I/O Error. Could not send vector data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
490 if (!xdr_array(&vec_sink, (
char **) &val, (
unsigned int *) &num, size, width,
XDRUtils::xdr_coder(type)))
492 "Network I/O Error(2). Could not send vector data.\nThis may be due to a bug in libdap or a\nproblem with the network connection.");
495 unsigned int bytes_written = xdr_getpos( &vec_sink );
498 "Network I/O Error. Could not send vector data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
501 d_out.write(&vec_buf[0], bytes_written);
503 xdr_destroy(&vec_sink);
506 xdr_destroy(&vec_sink);
513 strm <<
DapIndent::LMarg <<
"XDRStreamMarshaller::dump - (" << (
void *)
this <<
")" << endl;
virtual ~XDRStreamMarshaller()
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
Holds a one-dimensional collection of DAP2 data types.
virtual void put_int16(dods_int16 val)
Type
Identifies the data type.
Type type() const
Returns the type of the class instance.
virtual void put_uint16(dods_uint16 val)
virtual void put_float64(dods_float64 val)
const int XDR_DAP_BUFF_SIZE
A class for software fault reporting.
virtual void put_byte(dods_byte val)
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
virtual void put_int(int val)
virtual void put_float32(dods_float32 val)
virtual void put_opaque(char *val, unsigned int len)
virtual void put_str(const string &val)
static ostream & LMarg(ostream &strm)
abstract base class used to marshal/serialize dap data objects
virtual void put_vector(char *val, int num, Vector &vec)
static xdrproc_t xdr_coder(const Type &t)
Returns a function used to encode elements of an array.
A class for error processing.
virtual void put_int32(dods_int32 val)
virtual void put_uint32(dods_uint32 val)
virtual void put_url(const string &val)