[409] | 1 | /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc. |
---|
| 2 | See the COPYRIGHT file for more information. |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | /* |
---|
| 6 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
| 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * - Redistributions of source code must retain the above copyright notice, |
---|
| 12 | * this list of conditions and the following disclaimer. |
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
| 14 | * this list of conditions and the following disclaimer in the documentation |
---|
| 15 | * and/or other materials provided with the distribution. |
---|
| 16 | * - Neither the name of Sun Microsystems, Inc. nor the names of its |
---|
| 17 | * contributors may be used to endorse or promote products derived |
---|
| 18 | * from this software without specific prior written permission. |
---|
| 19 | * |
---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 31 | * |
---|
| 32 | * from: @(#)xdr.h 1.19 87/04/22 SMI |
---|
| 33 | * from: @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC |
---|
| 34 | * $FreeBSD: src/include/rpc/xdr.h,v 1.23 2003/03/07 13:19:40 nectar Exp $ |
---|
| 35 | * $NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt Exp $ |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | /* Define our own implementation of the needed |
---|
| 39 | elements of XDR. Assumes read-only |
---|
| 40 | */ |
---|
| 41 | |
---|
| 42 | #ifndef XXDR_H |
---|
| 43 | #define XXDR_H |
---|
| 44 | |
---|
| 45 | /* |
---|
| 46 | * This is the number of bytes per unit of external data. |
---|
| 47 | */ |
---|
| 48 | #define XDRUNIT (4) |
---|
| 49 | |
---|
| 50 | #if 1 |
---|
| 51 | /* faster version when XDRUNIT is a power of two */ |
---|
| 52 | # define RNDUP(x) (((x) + XDRUNIT - 1) & ~(XDRUNIT - 1)) |
---|
| 53 | #else /* old version */ |
---|
| 54 | #define RNDUP(x) ((((x) + XDRUNIT - 1) / XDRUNIT) \ |
---|
| 55 | * XDRUNIT) |
---|
| 56 | #endif |
---|
| 57 | |
---|
| 58 | /* signature: void swapinline32(unsigned int* ip) */ |
---|
| 59 | #define swapinline32(ip) \ |
---|
| 60 | { \ |
---|
| 61 | char dst[4]; \ |
---|
| 62 | char* src = (char*)(ip); \ |
---|
| 63 | dst[0] = src[3]; \ |
---|
| 64 | dst[1] = src[2]; \ |
---|
| 65 | dst[2] = src[1]; \ |
---|
| 66 | dst[3] = src[0]; \ |
---|
| 67 | *(ip) = *((unsigned int*)dst); \ |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /* signature: void swapinline64(unsigned long long* ip) */ |
---|
| 71 | #define swapinline64(ip) \ |
---|
| 72 | { \ |
---|
| 73 | char dst[8]; \ |
---|
| 74 | char* src = (char*)(ip); \ |
---|
| 75 | dst[0] = src[7]; \ |
---|
| 76 | dst[1] = src[6]; \ |
---|
| 77 | dst[2] = src[5]; \ |
---|
| 78 | dst[3] = src[4]; \ |
---|
| 79 | dst[4] = src[3]; \ |
---|
| 80 | dst[5] = src[2]; \ |
---|
| 81 | dst[6] = src[1]; \ |
---|
| 82 | dst[7] = src[0]; \ |
---|
| 83 | *ip = *((unsigned long long*)dst); \ |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | #ifdef OCIGNORE |
---|
| 88 | /* Warning dst and src should not be the same memory (assert &iswap != &i) */ |
---|
| 89 | #define xxdrntoh(dst,src) if(xxdr_network_order){dst=src;}else{swapinline32(dst,src);} |
---|
| 90 | #define xxdrntohll(dst,src) if(xxdr_network_order){dst=src;}else{swapinline64(dst,src);} |
---|
| 91 | #define xxdrhton(dst,src) xxdrntoh(dst,src) |
---|
| 92 | #define xxdrhtonll(dst,src) xxdrntohll(dst,src) |
---|
| 93 | #endif |
---|
| 94 | |
---|
| 95 | /* Double needs special handling */ |
---|
| 96 | extern void xxdrntohdouble(char*,double*); |
---|
| 97 | |
---|
| 98 | /* |
---|
| 99 | * The XDR handle. |
---|
| 100 | * Contains operation which is being applied to the stream, |
---|
| 101 | * an operations vector for the particular implementation (e.g. see xdr_mem.c), |
---|
| 102 | * and two private fields for the use of the particular implementation. |
---|
| 103 | */ |
---|
| 104 | typedef struct XXDR XXDR; /* forward */ |
---|
| 105 | |
---|
| 106 | /* Assume |off_t| == |void*| */ |
---|
| 107 | struct XXDR { |
---|
| 108 | char* data; |
---|
| 109 | off_t pos; /* relative to data; |
---|
| 110 | may be a cache of underlying stream pos */ |
---|
| 111 | int valid; /* 1=>underlying stream pos == pos */ |
---|
| 112 | off_t base; /* beginning of data in case bod != 0*/ |
---|
| 113 | off_t length; /* total size of available data (relative to base)*/ |
---|
| 114 | /* Define minimum needed case specific operators */ |
---|
| 115 | int (*getbytes)(XXDR*,char*,off_t); |
---|
| 116 | int (*setpos)(XXDR*,off_t); |
---|
| 117 | off_t (*getpos)(XXDR*); |
---|
| 118 | off_t (*getavail)(XXDR*); |
---|
| 119 | void (*free)(XXDR*); /* xdr kind specific free function */ |
---|
| 120 | }; |
---|
| 121 | |
---|
| 122 | /* Track network order */ |
---|
| 123 | extern int xxdr_network_order; /* does this machine match network order? */ |
---|
| 124 | |
---|
| 125 | /* Read-only operations */ |
---|
| 126 | |
---|
| 127 | /* Get exactly count bytes from underlying |
---|
| 128 | stream; unlike opaque, this will |
---|
| 129 | not round up to the XDRUNIT boundary |
---|
| 130 | */ |
---|
| 131 | extern int xxdr_getbytes(XXDR*,char*,off_t); |
---|
| 132 | |
---|
| 133 | /* get an int from underlying stream*/ |
---|
| 134 | extern int xxdr_uint(XXDR* , unsigned int*); |
---|
| 135 | |
---|
| 136 | /* get an int from underlying stream*/ |
---|
| 137 | extern int xxdr_ulonglong(XXDR* , unsigned long*); |
---|
| 138 | |
---|
| 139 | /* get a float from underlying stream*/ |
---|
| 140 | extern int xxdr_float(XXDR* , float*); |
---|
| 141 | |
---|
| 142 | /* get a double from underlying stream*/ |
---|
| 143 | extern int xxdr_double(XXDR* , double*); |
---|
| 144 | |
---|
| 145 | /* get some bytes from underlying stream; |
---|
| 146 | Warning: will read upto the next XDRUNIT boundary |
---|
| 147 | */ |
---|
| 148 | extern int xxdr_opaque(XXDR*, char*, off_t); |
---|
| 149 | |
---|
| 150 | /* get counted string from underlying stream*/ |
---|
| 151 | extern int xxdr_string(XXDR*, char**, off_t*); |
---|
| 152 | |
---|
| 153 | /* returns bytes off from beginning*/ |
---|
| 154 | extern off_t xxdr_getpos(XXDR*); |
---|
| 155 | |
---|
| 156 | /* reposition the stream*/ |
---|
| 157 | extern int xxdr_setpos(XXDR*, off_t); |
---|
| 158 | |
---|
| 159 | /* get remaining data available starting at current position */ |
---|
| 160 | extern off_t xxdr_getavail(XXDR*); |
---|
| 161 | |
---|
| 162 | /* free up XXDR structure */ |
---|
| 163 | void xxdr_free(XXDR*); |
---|
| 164 | |
---|
| 165 | /* File and memory creators */ |
---|
| 166 | extern XXDR* xxdr_filecreate(FILE* file, off_t bod); |
---|
| 167 | extern XXDR* xxdr_memcreate(char* mem, off_t memsize, off_t bod); |
---|
| 168 | |
---|
| 169 | /* Misc */ |
---|
| 170 | extern int xxdr_skip(XXDR* xdrs, off_t len); /* WARNING: will skip exactly len bytes; |
---|
| 171 | any rounding must be done by caller */ |
---|
| 172 | |
---|
| 173 | extern int xxdr_skip_strings(XXDR* xdrs, off_t n); |
---|
| 174 | |
---|
| 175 | extern unsigned int xxdr_roundup(off_t n); /* procedural version of RNDUP macro */ |
---|
| 176 | |
---|
| 177 | extern void xxdr_init(); |
---|
| 178 | |
---|
| 179 | /* Define some inlines */ |
---|
| 180 | #define xxdr_length(xdrs) ((xdrs)->length) |
---|
| 181 | |
---|
| 182 | #endif /*XXDR_H*/ |
---|
| 183 | |
---|