xdr.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
- /*
- * linux/net/sunrpc/xdr.c
- *
- * Generic XDR support.
- *
- * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
- */
- #include <linux/types.h>
- #include <linux/socket.h>
- #include <linux/string.h>
- #include <linux/kernel.h>
- #include <linux/pagemap.h>
- #include <linux/errno.h>
- #include <linux/in.h>
- #include <linux/sunrpc/xdr.h>
- #include <linux/sunrpc/msg_prot.h>
- /*
- * XDR functions for basic NFS types
- */
- u32 *
- xdr_encode_netobj(u32 *p, const struct xdr_netobj *obj)
- {
- unsigned int quadlen = XDR_QUADLEN(obj->len);
- p[quadlen] = 0; /* zero trailing bytes */
- *p++ = htonl(obj->len);
- memcpy(p, obj->data, obj->len);
- return p + XDR_QUADLEN(obj->len);
- }
- u32 *
- xdr_decode_netobj_fixed(u32 *p, void *obj, unsigned int len)
- {
- if (ntohl(*p++) != len)
- return NULL;
- memcpy(obj, p, len);
- return p + XDR_QUADLEN(len);
- }
- u32 *
- xdr_decode_netobj(u32 *p, struct xdr_netobj *obj)
- {
- unsigned int len;
- if ((len = ntohl(*p++)) > XDR_MAX_NETOBJ)
- return NULL;
- obj->len = len;
- obj->data = (u8 *) p;
- return p + XDR_QUADLEN(len);
- }
- u32 *
- xdr_encode_array(u32 *p, const char *array, unsigned int len)
- {
- int quadlen = XDR_QUADLEN(len);
- p[quadlen] = 0;
- *p++ = htonl(len);
- memcpy(p, array, len);
- return p + quadlen;
- }
- u32 *
- xdr_encode_string(u32 *p, const char *string)
- {
- return xdr_encode_array(p, string, strlen(string));
- }
- u32 *
- xdr_decode_string(u32 *p, char **sp, int *lenp, int maxlen)
- {
- unsigned int len;
- char *string;
- if ((len = ntohl(*p++)) > maxlen)
- return NULL;
- if (lenp)
- *lenp = len;
- if ((len % 4) != 0) {
- string = (char *) p;
- } else {
- string = (char *) (p - 1);
- memmove(string, p, len);
- }
- string[len] = ' ';
- *sp = string;
- return p + XDR_QUADLEN(len);
- }
- u32 *
- xdr_decode_string_inplace(u32 *p, char **sp, int *lenp, int maxlen)
- {
- unsigned int len;
- if ((len = ntohl(*p++)) > maxlen)
- return NULL;
- *lenp = len;
- *sp = (char *) p;
- return p + XDR_QUADLEN(len);
- }
- void
- xdr_encode_pages(struct xdr_buf *xdr, struct page **pages, unsigned int base,
- unsigned int len)
- {
- xdr->pages = pages;
- xdr->page_base = base;
- xdr->page_len = len;
- if (len & 3) {
- struct iovec *iov = xdr->tail;
- unsigned int pad = 4 - (len & 3);
- iov->iov_base = (void *) "