1 // XDRFileUnMarshaller.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 
33 #include "config.h"
34 
35 #include "XDRFileUnMarshaller.h"
36 
37 #include "Byte.h"
38 #include "Int16.h"
39 #include "UInt16.h"
40 #include "Int32.h"
41 #include "UInt32.h"
42 #include "Float32.h"
43 #include "Float64.h"
44 #include "Str.h"
45 #include "Url.h"
46 #include "Array.h"
47 #include "Structure.h"
48 #include "Sequence.h"
49 #include "Grid.h"
50 
51 #include "util.h"
52 #include "InternalErr.h"
53 #include "DapIndent.h"
54 
55 namespace libdap {
56 
XDRFileUnMarshaller(FILE * out)57 XDRFileUnMarshaller::XDRFileUnMarshaller( FILE *out )
58     : _source( 0 )
59 {
60     _source = new_xdrstdio( out, XDR_DECODE ) ;
61 }
62 
XDRFileUnMarshaller()63 XDRFileUnMarshaller::XDRFileUnMarshaller()
64     : UnMarshaller(), _source( 0 )
65 {
66     throw InternalErr( __FILE__, __LINE__, "Default constructor not implemented." ) ;
67 }
68 
XDRFileUnMarshaller(const XDRFileUnMarshaller & um)69 XDRFileUnMarshaller::XDRFileUnMarshaller( const XDRFileUnMarshaller &um )
70     : UnMarshaller( um ), _source( 0 )
71 {
72     throw InternalErr( __FILE__, __LINE__, "Copy constructor not implemented." ) ;
73 }
74 
75 XDRFileUnMarshaller &
operator =(const XDRFileUnMarshaller &)76 XDRFileUnMarshaller::operator=( const XDRFileUnMarshaller & )
77 {
78     throw InternalErr( __FILE__, __LINE__, "Copy operator not implemented." ) ;
79 }
80 
~XDRFileUnMarshaller()81 XDRFileUnMarshaller::~XDRFileUnMarshaller( )
82 {
83     // Some static code analysis tools complain that delete_xdrstdio
84     // does not close the FILE* it holds, but that's not true with
85     // modern XDR libraries. Don't try to close that FILE*. jhrg 8/27/13
86 
87     delete_xdrstdio( _source ) ;
88 }
89 
90 void
get_byte(dods_byte & val)91 XDRFileUnMarshaller::get_byte( dods_byte &val )
92 {
93     if( !xdr_char( _source, (char *)&val ) )
94         throw Error("Network I/O Error. Could not read byte data.");
95 }
96 
97 void
get_int16(dods_int16 & val)98 XDRFileUnMarshaller::get_int16( dods_int16 &val )
99 {
100     if( !XDR_INT16( _source, &val ) )
101         throw Error("Network I/O Error. Could not read int 16 data.");
102 }
103 
104 void
get_int32(dods_int32 & val)105 XDRFileUnMarshaller::get_int32( dods_int32 &val )
106 {
107     if( !XDR_INT32( _source, &val ) )
108         throw Error("Network I/O Error. Could not read int 32 data.");
109 }
110 
111 void
get_float32(dods_float32 & val)112 XDRFileUnMarshaller::get_float32( dods_float32 &val )
113 {
114     if( !xdr_float( _source, &val ) )
115         throw Error("Network I/O Error. Could not read float 32 data.");
116 }
117 
118 void
get_float64(dods_float64 & val)119 XDRFileUnMarshaller::get_float64( dods_float64 &val )
120 {
121     if( !xdr_double( _source, &val ) )
122         throw Error("Network I/O Error.Could not read float 64 data.");
123 }
124 
125 void
get_uint16(dods_uint16 & val)126 XDRFileUnMarshaller::get_uint16( dods_uint16 &val )
127 {
128     if( !XDR_UINT16( _source, &val ) )
129         throw Error("Network I/O Error. Could not read uint 16 data.");
130 }
131 
132 void
get_uint32(dods_uint32 & val)133 XDRFileUnMarshaller::get_uint32( dods_uint32 &val )
134 {
135     if( !XDR_UINT32( _source, &val ) )
136         throw Error("Network I/O Error. Could not read uint 32 data.");
137 }
138 
139 void
get_str(string & val)140 XDRFileUnMarshaller::get_str( string &val )
141 {
142     char *in_tmp = NULL ;
143 
144     if( !xdr_string( _source, &in_tmp, max_str_len ) )
145         throw Error("Network I/O Error. Could not read string data.");
146 
147     val = in_tmp ;
148 
149     free( in_tmp ) ;
150 }
151 
152 void
get_url(string & val)153 XDRFileUnMarshaller::get_url( string &val )
154 {
155     get_str( val ) ;
156 }
157 
158 void
get_opaque(char * val,unsigned int len)159 XDRFileUnMarshaller::get_opaque( char *val, unsigned int len )
160 {
161     xdr_opaque( _source, val, len ) ;
162 }
163 
164 void
get_int(int & val)165 XDRFileUnMarshaller::get_int( int &val )
166 {
167     if( !xdr_int( _source, &val ) )
168 	throw Error("Network I/O Error(1). This may be due to a bug in libdap or a\nproblem with the network connection.");
169 }
170 
171 void
get_vector(char ** val,unsigned int & num,Vector &)172 XDRFileUnMarshaller::get_vector( char **val, unsigned int &num, Vector & )
173 {
174     if( !xdr_bytes( _source, val, &num, DODS_MAX_ARRAY) )
175 	throw Error("Network I/O error (1).");
176 }
177 
178 void
get_vector(char ** val,unsigned int & num,int width,Vector & vec)179 XDRFileUnMarshaller::get_vector( char **val, unsigned int &num, int width, Vector &vec )
180 {
181     BaseType *var = vec.var() ;
182 
183     if( !xdr_array( _source, val, &num, DODS_MAX_ARRAY, width,
184 		    XDRUtils::xdr_coder( var->type() ) ) )
185     {
186 	throw Error("Network I/O error (2).");
187     }
188 }
189 
190 void
dump(ostream & strm) const191 XDRFileUnMarshaller::dump(ostream &strm) const
192 {
193     strm << DapIndent::LMarg << "XDRFileUnMarshaller::dump - ("
194          << (void *)this << ")" << endl ;
195 }
196 
197 } // namespace libdap
198 
199