Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | /* * Copyright (c) 2000-2011 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Rick Macklem at The University of Guelph. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)xdr_subs.h 8.3 (Berkeley) 3/30/95 * FreeBSD-Id: xdr_subs.h,v 1.9 1997/02/22 09:42:53 peter Exp $ */ #ifndef _NFS_XDR_SUBS_H_ #define _NFS_XDR_SUBS_H_ #include <sys/appleapiopts.h> #ifdef __APPLE_API_PRIVATE /* * Macros used for conversion to/from xdr representation by nfs... * These use the MACHINE DEPENDENT routines ntohl, htonl * As defined by "XDR: External Data Representation Standard" RFC1014 * * To simplify the implementation, we use ntohl/htonl even on big-endian * machines, and count on them being `#define'd away. Some of these * might be slightly more efficient as quad_t copies on a big-endian, * but we cannot count on their alignment anyway. */ #define fxdr_unsigned(t, v) ((t)ntohl((uint32_t)(v))) #define txdr_unsigned(v) (htonl((uint32_t)(v))) #define fxdr_hyper(f, t) { \ ((uint32_t *)(t))[_QUAD_HIGHWORD] = ntohl(((uint32_t *)(f))[0]); \ ((uint32_t *)(t))[_QUAD_LOWWORD] = ntohl(((uint32_t *)(f))[1]); \ } #define txdr_hyper(f, t) { \ ((uint32_t *)(t))[0] = htonl(((uint32_t *)(f))[_QUAD_HIGHWORD]); \ ((uint32_t *)(t))[1] = htonl(((uint32_t *)(f))[_QUAD_LOWWORD]); \ } /* * xdrbuf * * generalized functionality for managing the building/dissecting of XDR data */ typedef enum xdrbuf_type { XDRBUF_NONE = 0, XDRBUF_BUFFER = 1, } xdrbuf_type; struct xdrbuf { union { struct { char * xbb_base; /* base address of buffer */ size_t xbb_size; /* size of buffer */ size_t xbb_len; /* length of data in buffer */ } xb_buffer; } xb_u; char * xb_ptr; /* pointer to current position */ size_t xb_left; /* bytes remaining in current buffer */ size_t xb_growsize; /* bytes to allocate when growing */ xdrbuf_type xb_type; /* type of xdr buffer */ uint32_t xb_flags; /* XB_* (see below) */ }; #define XB_CLEANUP 0x0001 /* needs cleanup */ #define XDRWORD 4 /* the basic XDR building block is a 4 byte (32 bit) word */ #define xdr_rndup(a) (((a)+3)&(~0x3)) /* round up to XDRWORD size */ #define xdr_pad(a) (xdr_rndup(a) - (a)) /* calculate round up padding */ void xb_init(struct xdrbuf *, xdrbuf_type); void xb_init_buffer(struct xdrbuf *, char *, size_t); void xb_cleanup(struct xdrbuf *); void *xb_malloc(size_t) __attribute__((alloc_size(1))); void *xb_realloc(void *, size_t, size_t) __attribute__((alloc_size(3))); void xb_free(void *); void xb_free_size(void *, size_t); int xb_grow(struct xdrbuf *); void xb_set_cur_buf_len(struct xdrbuf *); char *xb_buffer_base(struct xdrbuf *); int xb_advance(struct xdrbuf *, size_t); size_t xb_offset(struct xdrbuf *); int xb_seek(struct xdrbuf *, size_t); int xb_add_bytes(struct xdrbuf *, const char *, size_t, int); int xb_get_bytes(struct xdrbuf *, char *, uint32_t, int); #ifdef _NFS_XDR_SUBS_FUNCS_ /* * basic initialization of xdrbuf structure */ void xb_init(struct xdrbuf *xbp, xdrbuf_type type) { bzero(xbp, sizeof(*xbp)); xbp->xb_type = type; xbp->xb_flags |= XB_CLEANUP; } /* * initialize a single-buffer xdrbuf */ void xb_init_buffer(struct xdrbuf *xbp, char *buf, size_t buflen) { xb_init(xbp, XDRBUF_BUFFER); xbp->xb_u.xb_buffer.xbb_base = buf; xbp->xb_u.xb_buffer.xbb_size = buflen; xbp->xb_u.xb_buffer.xbb_len = buflen; xbp->xb_growsize = 512; xbp->xb_ptr = buf; xbp->xb_left = buflen; if (buf) { /* when using an existing buffer, xb code should skip cleanup */ xbp->xb_flags &= ~XB_CLEANUP; } } /* * get the pointer to the single-buffer xdrbuf's buffer */ char * xb_buffer_base(struct xdrbuf *xbp) { return xbp->xb_u.xb_buffer.xbb_base; } /* * clean up any resources held by an xdrbuf */ void xb_cleanup(struct xdrbuf *xbp) { if (!(xbp->xb_flags & XB_CLEANUP)) { return; } switch (xbp->xb_type) { case XDRBUF_BUFFER: if (xbp->xb_u.xb_buffer.xbb_base) { xb_free(xbp->xb_u.xb_buffer.xbb_base); } break; default: break; } xbp->xb_flags &= ~XB_CLEANUP; } /* * set the length of valid data in the current buffer to * be up to the current location within the buffer */ void xb_set_cur_buf_len(struct xdrbuf *xbp) { switch (xbp->xb_type) { case XDRBUF_BUFFER: xbp->xb_u.xb_buffer.xbb_len = xbp->xb_ptr - xbp->xb_u.xb_buffer.xbb_base; break; default: break; } } /* * advance forward through existing data in xdrbuf */ int xb_advance(struct xdrbuf *xbp, size_t len) { size_t tlen; while (len) { if (xbp->xb_left <= 0) { return EBADRPC; } tlen = MIN(xbp->xb_left, len); if (tlen) { xbp->xb_ptr += tlen; xbp->xb_left -= tlen; len -= tlen; } } return 0; } /* * Calculate the current offset in the XDR buffer. */ size_t xb_offset(struct xdrbuf *xbp) { size_t offset = 0; switch (xbp->xb_type) { case XDRBUF_BUFFER: offset = xbp->xb_ptr - xbp->xb_u.xb_buffer.xbb_base; break; default: break; } return offset; } /* * Seek to the given offset in the existing data in the XDR buffer. */ int xb_seek(struct xdrbuf *xbp, size_t offset) { switch (xbp->xb_type) { case XDRBUF_BUFFER: xbp->xb_ptr = xbp->xb_u.xb_buffer.xbb_base + offset; xbp->xb_left = xbp->xb_u.xb_buffer.xbb_len - offset; break; default: break; } return 0; } /* * allocate memory */ void * xb_malloc(size_t size) { void *buf = NULL; #ifdef KERNEL buf = kalloc_data(size, Z_WAITOK); #else buf = malloc(size); #endif return buf; } void * xb_realloc(void *oldbuf, size_t old_size, size_t new_size) { void *buf = NULL; #ifdef KERNEL buf = krealloc_data(oldbuf, old_size, new_size, Z_WAITOK); #else (void)old_size; buf = realloc(oldbuf, new_size); #endif return buf; } /* * free a chunk of memory allocated with xb_malloc() */ void xb_free_size(void *buf, size_t len) { #ifdef KERNEL kfree_data(buf, len); #else (void)len; free(buf); #endif } /* * free a chunk of memory allocated with xb_malloc() */ void xb_free(void *buf) { #ifdef KERNEL kfree_data_addr(buf); #else free(buf); #endif } /* * Increase space available for new data in XDR buffer. */ int xb_grow(struct xdrbuf *xbp) { char *newbuf, *oldbuf; size_t newsize, oldsize; switch (xbp->xb_type) { case XDRBUF_BUFFER: oldsize = xbp->xb_u.xb_buffer.xbb_size; oldbuf = xbp->xb_u.xb_buffer.xbb_base; newsize = oldsize + xbp->xb_growsize; if (newsize < oldsize) { return ENOMEM; } newbuf = xb_realloc(oldbuf, oldsize, newsize); if (newbuf == NULL) { return ENOMEM; } xbp->xb_u.xb_buffer.xbb_base = newbuf; xbp->xb_u.xb_buffer.xbb_size = newsize; xbp->xb_ptr = newbuf + oldsize; xbp->xb_left = xbp->xb_growsize; break; default: break; } return 0; } /* * xb_add_bytes() * * Add "count" bytes of opaque data pointed to by "buf" to the given XDR buffer. */ int xb_add_bytes(struct xdrbuf *xbp, const char *buf, size_t count, int nopad) { size_t len, tlen; int error; len = nopad ? count : xdr_rndup(count); /* copy in "count" bytes and zero out any pad bytes */ while (len) { if (xbp->xb_left <= 0) { /* need more space */ if ((error = xb_grow(xbp))) { return error; } if (xbp->xb_left <= 0) { return ENOMEM; } } tlen = MIN(xbp->xb_left, len); if (tlen) { if (count) { if (tlen > count) { tlen = count; } bcopy(buf, xbp->xb_ptr, tlen); } else { bzero(xbp->xb_ptr, tlen); } xbp->xb_ptr += tlen; xbp->xb_left -= tlen; len -= tlen; if (count) { buf += tlen; count -= tlen; } } } return 0; } /* * xb_get_bytes() * * Get "count" bytes of opaque data from the given XDR buffer. */ int xb_get_bytes(struct xdrbuf *xbp, char *buf, uint32_t count, int nopad) { size_t len, tlen; len = nopad ? count : xdr_rndup(count); /* copy in "count" bytes and zero out any pad bytes */ while (len) { if (xbp->xb_left <= 0) { return ENOMEM; } tlen = MIN(xbp->xb_left, len); if (tlen) { if (count) { if (tlen > count) { tlen = count; } bcopy(xbp->xb_ptr, buf, tlen); } xbp->xb_ptr += tlen; xbp->xb_left -= tlen; len -= tlen; if (count) { buf += tlen; count -= tlen; } } } return 0; } #endif /* _NFS_XDR_SUBS_FUNCS_ */ /* * macros for building XDR data */ /* finalize the data that has been added to the buffer */ #define xb_build_done(E, XB) \ do { \ if (E) break; \ xb_set_cur_buf_len(XB); \ } while (0) /* add a 32-bit value */ #define xb_add_32(E, XB, VAL) \ do { \ uint32_t __tmp; \ if (E) break; \ __tmp = txdr_unsigned(VAL); \ (E) = xb_add_bytes((XB), (void*)&__tmp, XDRWORD, 0); \ } while (0) /* add a 64-bit value */ #define xb_add_64(E, XB, VAL) \ do { \ uint64_t __tmp1, __tmp2; \ if (E) break; \ __tmp1 = (VAL); \ txdr_hyper(&__tmp1, &__tmp2); \ (E) = xb_add_bytes((XB), (char*)&__tmp2, 2 * XDRWORD, 0); \ } while (0) /* add an array of XDR words */ #define xb_add_word_array(E, XB, A, LEN) \ do { \ uint32_t __i; \ xb_add_32((E), (XB), (LEN)); \ for (__i=0; __i < (uint32_t)(LEN); __i++) \ xb_add_32((E), (XB), (A)[__i]); \ } while (0) #define xb_add_bitmap(E, XB, B, LEN) xb_add_word_array((E), (XB), (B), (LEN)) /* add a file handle */ #define xb_add_fh(E, XB, FHP, FHLEN) \ do { \ xb_add_32((E), (XB), (FHLEN)); \ if (E) break; \ (E) = xb_add_bytes((XB), (char*)(FHP), (FHLEN), 0); \ } while (0) /* add a string */ #define xb_add_string(E, XB, S, LEN) \ do { \ xb_add_32((E), (XB), (LEN)); \ if (E) break; \ (E) = xb_add_bytes((XB), (const char*)(S), (LEN), 0); \ } while (0) /* * macros for decoding XDR data */ /* skip past data in the buffer */ #define xb_skip(E, XB, LEN) \ do { \ if (E) break; \ (E) = xb_advance((XB), (LEN)); \ } while (0) /* get a 32-bit value */ #define xb_get_32(E, XB, LVAL) \ do { \ uint32_t __tmp; \ (LVAL) = (typeof((LVAL))) 0; \ if (E) break; \ (E) = xb_get_bytes((XB), (char*)&__tmp, XDRWORD, 0); \ if (E) break; \ (LVAL) = fxdr_unsigned(uint32_t, __tmp); \ } while (0) /* get a 64-bit value */ #define xb_get_64(E, XB, LVAL) \ do { \ uint64_t __tmp; \ (LVAL) = 0; \ if (E) break; \ (E) = xb_get_bytes((XB), (char*)&__tmp, 2 * XDRWORD, 0); \ if (E) break; \ fxdr_hyper(&__tmp, &(LVAL)); \ } while (0) /* get an array of XDR words (of a given expected/maximum length) */ #define xb_get_word_array(E, XB, A, LEN) \ do { \ uint32_t __len = 0, __i; \ xb_get_32((E), (XB), __len); \ if (E) break; \ for (__i=0; __i < MIN(__len, (uint32_t)(LEN)); __i++) \ xb_get_32((E), (XB), (A)[__i]); \ if (E) break; \ for (; __i < __len; __i++) \ xb_skip((E), (XB), XDRWORD); \ for (; __i < (uint32_t)(LEN); __i++) \ (A)[__i] = 0; \ (LEN) = __len; \ } while (0) #define xb_get_bitmap(E, XB, B, LEN) xb_get_word_array((E), (XB), (B), (LEN)) #endif /* __APPLE_API_PRIVATE */ #endif /* _NFS_XDR_SUBS_H_ */ |