51 char *XDRStreamMarshaller::d_buf = 0;
53 #define XDR_DAP_BUFF_SIZE 256
63 XDRStreamMarshaller::XDRStreamMarshaller(ostream &out) :
69 throw Error(
"Failed to allocate memory for data serialization.");
76 _MD_CTX = EVP_MD_CTX_create();
81 XDRStreamMarshaller::XDRStreamMarshaller() :
84 throw InternalErr(__FILE__, __LINE__,
"Default constructor not implemented.");
87 XDRStreamMarshaller::XDRStreamMarshaller(
const XDRStreamMarshaller &m) :
88 Marshaller(m), d_out(cout)
90 throw InternalErr(__FILE__, __LINE__,
"Copy constructor not implemented.");
94 XDRStreamMarshaller::operator=(
const XDRStreamMarshaller &)
96 throw InternalErr(__FILE__, __LINE__,
"Copy operator not implemented.");
103 xdr_destroy(&d_sink);
109 EVP_MD_CTX_destroy(_MD_CTX);
118 void XDRStreamMarshaller::reset_checksum()
122 throw InternalErr( __FILE__, __LINE__,
"reset_checksum() called by checksum is not enabled.");
124 if (EVP_DigestInit_ex(_MD_CTX, EVP_sha1(), 0) == 0)
125 throw Error(
"Failed to initialize checksum object.");
127 _checksum_ctx_valid =
true;
136 string XDRStreamMarshaller::get_checksum()
140 throw InternalErr(__FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
142 if (_checksum_ctx_valid) {
145 _checksum_ctx_valid =
false;
147 vector<unsigned char> md(EVP_MAX_MD_SIZE);
149 if (EVP_DigestFinal_ex(_MD_CTX, &md[0], &md_len) == 0)
150 throw Error(
"Error computing the checksum (checksum computation).");
153 oss.setf(ios::hex, ios::basefield);
154 for (
unsigned int i = 0; i < md_len; ++i) {
155 oss << setfill(
'0') << setw(2) << (
unsigned int) md[i];
158 _checksum = oss.str();
167 void XDRStreamMarshaller::checksum_update(
const void *data,
unsigned long len)
171 throw InternalErr( __FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
173 if (!_checksum_ctx_valid)
174 throw InternalErr( __FILE__, __LINE__,
"Invalid checksum context (checksum update).");
176 if (EVP_DigestUpdate(_MD_CTX, data, len) == 0) {
177 _checksum_ctx_valid =
false;
178 throw Error(
"Error computing the checksum (checksum update).");
188 checksum_update(&val,
sizeof(
dods_byte));
191 DBG( std::cerr <<
"put_byte: " << val << std::endl );
193 if (!xdr_setpos( &d_sink, 0 ))
194 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.");
196 if (!xdr_char(&d_sink, (
char *) &val))
197 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.");
199 unsigned int bytes_written = xdr_getpos( &d_sink );
201 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.");
203 d_out.write(d_buf, bytes_written);
214 if (!xdr_setpos( &d_sink, 0 ))
215 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.");
218 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.");
220 unsigned int bytes_written = xdr_getpos( &d_sink );
222 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.");
224 d_out.write(d_buf, bytes_written);
235 if (!xdr_setpos( &d_sink, 0 ))
236 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.");
239 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.");
241 unsigned int bytes_written = xdr_getpos( &d_sink );
243 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.");
245 d_out.write(d_buf, bytes_written);
256 if (!xdr_setpos( &d_sink, 0 ))
257 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.");
259 if (!xdr_float(&d_sink, &val))
260 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.");
262 unsigned int bytes_written = xdr_getpos( &d_sink );
264 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.");
266 d_out.write(d_buf, bytes_written);
277 if (!xdr_setpos( &d_sink, 0 ))
278 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.");
280 if (!xdr_double(&d_sink, &val))
281 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.");
283 unsigned int bytes_written = xdr_getpos( &d_sink );
285 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.");
287 d_out.write(d_buf, bytes_written);
298 if (!xdr_setpos( &d_sink, 0 ))
299 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.");
302 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.");
304 unsigned int bytes_written = xdr_getpos( &d_sink );
306 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.");
308 d_out.write(d_buf, bytes_written);
319 if (!xdr_setpos( &d_sink, 0 ))
320 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.");
323 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.");
325 unsigned int bytes_written = xdr_getpos( &d_sink );
327 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.");
329 d_out.write(d_buf, bytes_written);
337 checksum_update(val.c_str(), val.length());
339 int size = val.length() + 8;
341 char *str_buf = (
char *) malloc(size);
344 throw Error(
"Failed to allocate memory for string data serialization.");
349 vector<char> str_buf(size);
352 xdrmem_create(&str_sink, &str_buf[0], size, XDR_ENCODE);
354 if (!xdr_setpos( &str_sink, 0 ))
356 "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.");
358 const char *out_tmp = val.c_str();
359 if (!xdr_string(&str_sink, (
char **) &out_tmp, size))
361 "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.");
363 unsigned int bytes_written = xdr_getpos( &str_sink );
366 "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.");
368 d_out.write(&str_buf[0], bytes_written);
370 xdr_destroy(&str_sink);
373 xdr_destroy(&str_sink);
387 checksum_update(&val, len);
391 throw Error(
"Network I/O Error. Could not send opaque data - length of opaque data larger than allowed");
393 if (!xdr_setpos( &d_sink, 0 ))
394 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.");
396 if (!xdr_opaque(&d_sink, val, len))
397 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.");
399 unsigned int bytes_written = xdr_getpos( &d_sink );
401 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.");
403 d_out.write(d_buf, bytes_written);
411 checksum_update(&val,
sizeof(
int));
414 if (!xdr_setpos( &d_sink, 0 ))
415 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.");
417 if (!xdr_int(&d_sink, &val))
418 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.");
420 unsigned int bytes_written = xdr_getpos( &d_sink );
422 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.");
424 d_out.write(d_buf, bytes_written);
430 if (!val)
throw InternalErr(__FILE__, __LINE__,
"Could not send byte vector data. Buffer pointer is not set.");
433 checksum_update(val, num);
438 const unsigned int add_to = 8;
440 char *byte_buf = (
char *) malloc(num + add_to);
441 if (!byte_buf)
throw Error(
"Failed to allocate memory for byte vector data serialization.");
443 vector<char> byte_buf(num + add_to);
446 xdrmem_create(&byte_sink, &byte_buf[0], num + add_to, XDR_ENCODE);
447 if (!xdr_setpos( &byte_sink, 0 ))
449 "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.");
451 if (!xdr_bytes(&byte_sink, (
char **) &val, (
unsigned int *) &num, num + add_to))
453 "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.");
455 unsigned int bytes_written = xdr_getpos( &byte_sink );
458 "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.");
460 d_out.write(&byte_buf[0], bytes_written);
462 xdr_destroy(&byte_sink);
465 xdr_destroy(&byte_sink);
479 if (!val)
throw InternalErr(__FILE__, __LINE__,
"Buffer pointer is not set.");
482 checksum_update(val, num * width);
486 int use_width = width;
487 if (use_width < 4) use_width = 4;
491 int size = (num * use_width) + 4;
495 char *vec_buf = (
char *) malloc(size);
497 throw Error(
"Failed to allocate memory for vector data serialization.");
499 vector<char> vec_buf(size);
502 xdrmem_create(&vec_sink, &vec_buf[0], size, XDR_ENCODE);
505 if (!xdr_setpos( &vec_sink, 0 ))
507 "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.");
510 if (!xdr_array(&vec_sink, (
char **) &val, (
unsigned int *) &num, size, width,
XDRUtils::xdr_coder(type)))
512 "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.");
515 unsigned int bytes_written = xdr_getpos( &vec_sink );
518 "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.");
521 d_out.write(&vec_buf[0], bytes_written);
523 xdr_destroy(&vec_sink);
526 xdr_destroy(&vec_sink);
533 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)