Branch data Line data Source code
1 : : // $Id: XDR.cc 6219 2008-10-01 05:39:07Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #include "config.h"
6 : :
7 : : #include "XDR.h"
8 : :
9 : 0 : uint32 extract_XDR_uint32(const u_char*& buf, int& len)
10 : : {
11 [ # # ]: 0 : if ( ! buf )
12 : 0 : return 0;
13 : :
14 [ # # ]: 0 : if ( len < 4 )
15 : : {
16 : 0 : buf = 0;
17 : 0 : return 0;
18 : : }
19 : :
20 : : uint32 bits32 = XDR_aligned(buf) ? *(uint32*) buf :
21 [ # # ]: 0 : ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
22 : :
23 : 0 : buf += 4;
24 : 0 : len -= 4;
25 : :
26 : 0 : return ntohl(bits32);
27 : : }
28 : :
29 : 0 : double extract_XDR_uint64_as_double(const u_char*& buf, int& len)
30 : : {
31 [ # # ][ # # ]: 0 : if ( ! buf || len < 8 )
32 : : {
33 : 0 : buf = 0;
34 : 0 : return 0.0;
35 : : }
36 : :
37 : 0 : uint32 uhi = extract_XDR_uint32(buf, len);
38 : 0 : uint32 ulo = extract_XDR_uint32(buf, len);
39 : :
40 : 0 : return double(uhi) * 4294967296.0 + double(ulo);
41 : : }
42 : :
43 : 0 : double extract_XDR_time(const u_char*& buf, int& len)
44 : : {
45 [ # # ][ # # ]: 0 : if ( ! buf || len < 8 )
46 : : {
47 : 0 : buf = 0;
48 : 0 : return 0.0;
49 : : }
50 : :
51 : 0 : uint32 uhi = extract_XDR_uint32(buf, len);
52 : 0 : uint32 ulo = extract_XDR_uint32(buf, len);
53 : :
54 : 0 : return double(uhi) + double(ulo) / 1e9;
55 : : }
56 : :
57 : 0 : const u_char* extract_XDR_opaque(const u_char*& buf, int& len, int& n, int max_len)
58 : : {
59 : 0 : n = int(extract_XDR_uint32(buf, len));
60 [ # # ]: 0 : if ( ! buf )
61 : 0 : return 0;
62 : :
63 [ # # ][ # # ]: 0 : if ( n < 0 || n > len || n > max_len )
[ # # ]
64 : : { // ### Should really flag this as a different sort of error.
65 : 0 : buf = 0;
66 : 0 : return 0;
67 : : }
68 : :
69 : 0 : int n4 = ((n + 3) >> 2) << 2; // n rounded up to next multiple of 4
70 : :
71 : 0 : len -= n4;
72 : 0 : const u_char* opaque = buf;
73 : 0 : buf += n4;
74 : :
75 : 0 : return opaque;
76 : : }
77 : :
78 : 0 : uint32 skip_XDR_opaque_auth(const u_char*& buf, int& len)
79 : : {
80 : 0 : uint32 auth_flavor = extract_XDR_uint32(buf, len);
81 [ # # ]: 0 : if ( ! buf )
82 : 0 : return 0;
83 : :
84 : : int n;
85 : 0 : (void) extract_XDR_opaque(buf, len, n, 400);
86 : :
87 : 0 : return auth_flavor;
88 : : }
|