xxdr.h 5.9 KB

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