nfs-ganesha 1.4
|
00001 /* 00002 Copyright (c) 2004 The Regents of the University of Michigan. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions 00007 are met: 00008 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. Neither the name of the University nor the names of its 00015 contributors may be used to endorse or promote products derived 00016 from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 00019 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00021 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00025 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00026 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 #ifndef _WRITE_BYTES_H_ 00032 #define _WRITE_BYTES_H_ 00033 00034 #include <stdlib.h> 00035 #include <sys/types.h> 00036 #include <netinet/in.h> /* for ntohl */ 00037 00038 inline static int 00039 write_bytes(char **ptr, const char *end, const void *arg, int arg_len) 00040 { 00041 char *p = *ptr, *arg_end; 00042 00043 arg_end = p + arg_len; 00044 if (arg_end > end || arg_end < p) 00045 return -1; 00046 memcpy(p, arg, arg_len); 00047 *ptr = arg_end; 00048 return 0; 00049 } 00050 00051 #define WRITE_BYTES(p, end, arg) write_bytes(p, end, &arg, sizeof(arg)) 00052 00053 inline static int 00054 write_buffer(char **p, char *end, gss_buffer_desc *arg) 00055 { 00056 int len = (int)arg->length; /* make an int out of size_t */ 00057 if (WRITE_BYTES(p, end, len)) 00058 return -1; 00059 if (*p + len > end) 00060 return -1; 00061 memcpy(*p, arg->value, len); 00062 *p += len; 00063 return 0; 00064 } 00065 00066 inline static int 00067 write_oid(char **p, char *end, gss_OID_desc *arg) 00068 { 00069 int len = (int)arg->length; /* make an int out of size_t */ 00070 if (WRITE_BYTES(p, end, len)) 00071 return -1; 00072 if (*p + arg->length > end) 00073 return -1; 00074 memcpy(*p, arg->elements, len); 00075 *p += len; 00076 return 0; 00077 } 00078 00079 static inline int 00080 get_bytes(char **ptr, const char *end, void *res, int len) 00081 { 00082 char *p, *q; 00083 p = *ptr; 00084 q = p + len; 00085 if (q > end || q < p) 00086 return -1; 00087 memcpy(res, p, len); 00088 *ptr = q; 00089 return 0; 00090 } 00091 00092 static inline int 00093 get_buffer(char **ptr, const char *end, gss_buffer_desc *res) 00094 { 00095 char *p, *q; 00096 p = *ptr; 00097 int len; 00098 if (get_bytes(&p, end, &len, sizeof(len))) 00099 return -1; 00100 res->length = len; /* promote to size_t if necessary */ 00101 q = p + res->length; 00102 if (q > end || q < p) 00103 return -1; 00104 if (!(res->value = malloc(res->length))) 00105 return -1; 00106 memcpy(res->value, p, res->length); 00107 *ptr = q; 00108 return 0; 00109 } 00110 00111 static inline int 00112 xdr_get_u32(u_int32_t **ptr, const u_int32_t *end, u_int32_t *res) 00113 { 00114 if (get_bytes((char **)ptr, (char *)end, res, sizeof(res))) 00115 return -1; 00116 *res = ntohl(*res); 00117 return 0; 00118 } 00119 00120 static inline int 00121 xdr_get_buffer(u_int32_t **ptr, const u_int32_t *end, gss_buffer_desc *res) 00122 { 00123 u_int32_t *p, *q; 00124 u_int32_t len; 00125 p = *ptr; 00126 if (xdr_get_u32(&p, end, &len)) 00127 return -1; 00128 res->length = len; 00129 q = p + ((res->length + 3) >> 2); 00130 if (q > end || q < p) 00131 return -1; 00132 if (!(res->value = malloc(res->length))) 00133 return -1; 00134 memcpy(res->value, p, res->length); 00135 *ptr = q; 00136 return 0; 00137 } 00138 00139 static inline int 00140 xdr_write_u32(u_int32_t **ptr, const u_int32_t *end, u_int32_t arg) 00141 { 00142 u_int32_t tmp; 00143 00144 tmp = htonl(arg); 00145 return WRITE_BYTES((char **)ptr, (char *)end, tmp); 00146 } 00147 00148 static inline int 00149 xdr_write_buffer(u_int32_t **ptr, const u_int32_t *end, gss_buffer_desc *arg) 00150 { 00151 int len = arg->length; 00152 if (xdr_write_u32(ptr, end, len)) 00153 return -1; 00154 return write_bytes((char **)ptr, (char *)end, arg->value, 00155 (arg->length + 3) & ~3); 00156 } 00157 00158 #endif /* _WRITE_BYTES_H_ */