49 char *XDRStreamMarshaller::d_buf = 0;
51 #define XDR_DAP_BUFF_SIZE 256
61 XDRStreamMarshaller::XDRStreamMarshaller(ostream &out) :
67 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);
107 EVP_MD_CTX_destroy(_MD_CTX);
116 void XDRStreamMarshaller::reset_checksum()
120 throw InternalErr( __FILE__, __LINE__,
"reset_checksum() called by checksum is not enabled.");
122 if (EVP_DigestInit_ex(_MD_CTX, EVP_sha1(), 0) == 0)
123 throw Error(
"Failed to initialize checksum object.");
125 _checksum_ctx_valid =
true;
134 string XDRStreamMarshaller::get_checksum()
138 throw InternalErr(__FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
140 if (_checksum_ctx_valid) {
143 _checksum_ctx_valid =
false;
145 vector<unsigned char> md(EVP_MAX_MD_SIZE);
147 if (EVP_DigestFinal_ex(_MD_CTX, &md[0], &md_len) == 0)
148 throw Error(
"Error computing the checksum (checksum computation).");
151 oss.setf(ios::hex, ios::basefield);
152 for (
unsigned int i = 0; i < md_len; ++i) {
153 oss << setfill(
'0') << setw(2) << (
unsigned int) md[i];
156 _checksum = oss.str();
165 void XDRStreamMarshaller::checksum_update(
const void *data,
unsigned long len)
169 throw InternalErr( __FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
171 if (!_checksum_ctx_valid)
172 throw InternalErr( __FILE__, __LINE__,
"Invalid checksum context (checksum update).");
174 if (EVP_DigestUpdate(_MD_CTX, data, len) == 0) {
175 _checksum_ctx_valid =
false;
176 throw Error(
"Error computing the checksum (checksum update).");
186 checksum_update(&val,
sizeof(
dods_byte));
189 DBG( std::cerr <<
"put_byte: " << val << std::endl );
191 if (!xdr_setpos( &d_sink, 0 ))
192 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.");
194 if (!xdr_char(&d_sink, (
char *) &val))
195 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.");
197 unsigned int bytes_written = xdr_getpos( &d_sink );
199 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.");
201 d_out.write(d_buf, bytes_written);
212 if (!xdr_setpos( &d_sink, 0 ))
213 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.");
216 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.");
218 unsigned int bytes_written = xdr_getpos( &d_sink );
220 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.");
222 d_out.write(d_buf, bytes_written);
233 if (!xdr_setpos( &d_sink, 0 ))
234 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.");
237 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.");
239 unsigned int bytes_written = xdr_getpos( &d_sink );
241 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.");
243 d_out.write(d_buf, bytes_written);
254 if (!xdr_setpos( &d_sink, 0 ))
255 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.");
257 if (!xdr_float(&d_sink, &val))
258 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.");
260 unsigned int bytes_written = xdr_getpos( &d_sink );
262 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.");
264 d_out.write(d_buf, bytes_written);
275 if (!xdr_setpos( &d_sink, 0 ))
276 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.");
278 if (!xdr_double(&d_sink, &val))
279 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.");
281 unsigned int bytes_written = xdr_getpos( &d_sink );
283 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.");
285 d_out.write(d_buf, bytes_written);
296 if (!xdr_setpos( &d_sink, 0 ))
297 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.");
300 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.");
302 unsigned int bytes_written = xdr_getpos( &d_sink );
304 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.");
306 d_out.write(d_buf, bytes_written);
317 if (!xdr_setpos( &d_sink, 0 ))
318 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.");
321 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.");
323 unsigned int bytes_written = xdr_getpos( &d_sink );
325 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.");
327 d_out.write(d_buf, bytes_written);
335 checksum_update(val.c_str(), val.length());
337 int size = val.length() + 8;
339 char *str_buf = (
char *) malloc(size);
342 throw Error(
"Failed to allocate memory for string data serialization.");
347 vector<char> str_buf(size);
350 xdrmem_create(&str_sink, &str_buf[0], size, XDR_ENCODE);
352 if (!xdr_setpos( &str_sink, 0 ))
354 "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.");
356 const char *out_tmp = val.c_str();
357 if (!xdr_string(&str_sink, (
char **) &out_tmp, size))
359 "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.");
361 unsigned int bytes_written = xdr_getpos( &str_sink );
364 "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.");
366 d_out.write(&str_buf[0], bytes_written);
368 xdr_destroy(&str_sink);
371 xdr_destroy(&str_sink);
385 checksum_update(&val, len);
389 throw Error(
"Network I/O Error. Could not send opaque data - length of opaque data larger than allowed");
391 if (!xdr_setpos( &d_sink, 0 ))
392 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.");
394 if (!xdr_opaque(&d_sink, val, len))
395 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.");
397 unsigned int bytes_written = xdr_getpos( &d_sink );
399 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.");
401 d_out.write(d_buf, bytes_written);
409 checksum_update(&val,
sizeof(
int));
412 if (!xdr_setpos( &d_sink, 0 ))
413 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.");
415 if (!xdr_int(&d_sink, &val))
416 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.");
418 unsigned int bytes_written = xdr_getpos( &d_sink );
420 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.");
422 d_out.write(d_buf, bytes_written);
428 if (!val)
throw InternalErr(__FILE__, __LINE__,
"Could not send byte vector data. Buffer pointer is not set.");
431 checksum_update(val, num);
436 const unsigned int add_to = 8;
438 char *byte_buf = (
char *) malloc(num + add_to);
439 if (!byte_buf)
throw Error(
"Failed to allocate memory for byte vector data serialization.");
441 vector<char> byte_buf(num + add_to);
444 xdrmem_create(&byte_sink, &byte_buf[0], num + add_to, XDR_ENCODE);
445 if (!xdr_setpos( &byte_sink, 0 ))
447 "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.");
449 if (!xdr_bytes(&byte_sink, (
char **) &val, (
unsigned int *) &num, num + add_to))
451 "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.");
453 unsigned int bytes_written = xdr_getpos( &byte_sink );
456 "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.");
458 d_out.write(&byte_buf[0], bytes_written);
460 xdr_destroy(&byte_sink);
463 xdr_destroy(&byte_sink);
477 if (!val)
throw InternalErr(__FILE__, __LINE__,
"Buffer pointer is not set.");
480 checksum_update(val, num * width);
484 int use_width = width;
485 if (use_width < 4) use_width = 4;
489 int size = (num * use_width) + 4;
493 char *vec_buf = (
char *) malloc(size);
495 throw Error(
"Failed to allocate memory for vector data serialization.");
497 vector<char> vec_buf(size);
500 xdrmem_create(&vec_sink, &vec_buf[0], size, XDR_ENCODE);
503 if (!xdr_setpos( &vec_sink, 0 ))
505 "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.");
508 if (!xdr_array(&vec_sink, (
char **) &val, (
unsigned int *) &num, size, width,
XDRUtils::xdr_coder(type)))
510 "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.");
513 unsigned int bytes_written = xdr_getpos( &vec_sink );
516 "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.");
519 d_out.write(&vec_buf[0], bytes_written);
521 xdr_destroy(&vec_sink);
524 xdr_destroy(&vec_sink);
531 strm <<
DapIndent::LMarg <<
"XDRStreamMarshaller::dump - (" << (
void *)
this <<
")" << endl;