19ec7b004SRick Macklem /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
49ec7b004SRick Macklem * Copyright (c) 1989, 1993
59ec7b004SRick Macklem * The Regents of the University of California. All rights reserved.
69ec7b004SRick Macklem *
79ec7b004SRick Macklem * This code is derived from software contributed to Berkeley by
89ec7b004SRick Macklem * Rick Macklem at The University of Guelph.
99ec7b004SRick Macklem *
109ec7b004SRick Macklem * Redistribution and use in source and binary forms, with or without
119ec7b004SRick Macklem * modification, are permitted provided that the following conditions
129ec7b004SRick Macklem * are met:
139ec7b004SRick Macklem * 1. Redistributions of source code must retain the above copyright
149ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer.
159ec7b004SRick Macklem * 2. Redistributions in binary form must reproduce the above copyright
169ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer in the
179ec7b004SRick Macklem * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
199ec7b004SRick Macklem * may be used to endorse or promote products derived from this software
209ec7b004SRick Macklem * without specific prior written permission.
219ec7b004SRick Macklem *
229ec7b004SRick Macklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239ec7b004SRick Macklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249ec7b004SRick Macklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259ec7b004SRick Macklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269ec7b004SRick Macklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279ec7b004SRick Macklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289ec7b004SRick Macklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299ec7b004SRick Macklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309ec7b004SRick Macklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319ec7b004SRick Macklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329ec7b004SRick Macklem * SUCH DAMAGE.
339ec7b004SRick Macklem */
349ec7b004SRick Macklem
359ec7b004SRick Macklem #ifndef _NFS_XDR_SUBS_H_
369ec7b004SRick Macklem #define _NFS_XDR_SUBS_H_
379ec7b004SRick Macklem
389ec7b004SRick Macklem /*
399ec7b004SRick Macklem * Macros used for conversion to/from xdr representation by nfs...
409ec7b004SRick Macklem * These use the MACHINE DEPENDENT routines ntohl, htonl
419ec7b004SRick Macklem * As defined by "XDR: External Data Representation Standard" RFC1014
429ec7b004SRick Macklem *
439ec7b004SRick Macklem * To simplify the implementation, we use ntohl/htonl even on big-endian
449ec7b004SRick Macklem * machines, and count on them being `#define'd away. Some of these
459ec7b004SRick Macklem * might be slightly more efficient as quad_t copies on a big-endian,
469ec7b004SRick Macklem * but we cannot count on their alignment anyway.
479ec7b004SRick Macklem */
489ec7b004SRick Macklem
499ec7b004SRick Macklem #define fxdr_unsigned(t, v) ((t)ntohl((int32_t)(v)))
509ec7b004SRick Macklem #define txdr_unsigned(v) (htonl((int32_t)(v)))
519ec7b004SRick Macklem
529ec7b004SRick Macklem #define fxdr_nfsv2time(f, t) do { \
539ec7b004SRick Macklem (t)->tv_sec = ntohl(((struct nfsv2_time *)(f))->nfsv2_sec); \
549ec7b004SRick Macklem if (((struct nfsv2_time *)(f))->nfsv2_usec != 0xffffffff) \
559ec7b004SRick Macklem (t)->tv_nsec = 1000 * ntohl(((struct nfsv2_time *)(f))->nfsv2_usec); \
569ec7b004SRick Macklem else \
579ec7b004SRick Macklem (t)->tv_nsec = 0; \
589ec7b004SRick Macklem } while (0)
599ec7b004SRick Macklem
609ec7b004SRick Macklem #define txdr_nfsv2time(f, t) do { \
619ec7b004SRick Macklem ((struct nfsv2_time *)(t))->nfsv2_sec = htonl((f)->tv_sec); \
629ec7b004SRick Macklem if ((f)->tv_nsec != -1) \
639ec7b004SRick Macklem ((struct nfsv2_time *)(t))->nfsv2_usec = htonl((f)->tv_nsec / 1000); \
649ec7b004SRick Macklem else \
659ec7b004SRick Macklem ((struct nfsv2_time *)(t))->nfsv2_usec = 0xffffffff; \
669ec7b004SRick Macklem } while (0)
679ec7b004SRick Macklem
689ec7b004SRick Macklem #define fxdr_nfsv3time(f, t) do { \
699ec7b004SRick Macklem (t)->tv_sec = ntohl(((struct nfsv3_time *)(f))->nfsv3_sec); \
709ec7b004SRick Macklem (t)->tv_nsec = ntohl(((struct nfsv3_time *)(f))->nfsv3_nsec); \
719ec7b004SRick Macklem } while (0)
729ec7b004SRick Macklem
739ec7b004SRick Macklem #define txdr_nfsv3time(f, t) do { \
749ec7b004SRick Macklem ((struct nfsv3_time *)(t))->nfsv3_sec = htonl((f)->tv_sec); \
759ec7b004SRick Macklem ((struct nfsv3_time *)(t))->nfsv3_nsec = htonl((f)->tv_nsec); \
769ec7b004SRick Macklem } while (0)
779ec7b004SRick Macklem
789ec7b004SRick Macklem #define fxdr_nfsv4time(f, t) do { \
799ec7b004SRick Macklem (t)->tv_sec = ntohl(((struct nfsv4_time *)(f))->nfsv4_sec); \
809ec7b004SRick Macklem (t)->tv_nsec = (ntohl(((struct nfsv4_time *)(f))->nfsv4_nsec) % \
819ec7b004SRick Macklem 1000000000); \
829ec7b004SRick Macklem } while (0)
839ec7b004SRick Macklem
849ec7b004SRick Macklem #define txdr_nfsv4time(f, t) do { \
859ec7b004SRick Macklem ((struct nfsv4_time *)(t))->nfsv4_highsec = 0; \
869ec7b004SRick Macklem ((struct nfsv4_time *)(t))->nfsv4_sec = htonl((f)->tv_sec); \
879ec7b004SRick Macklem ((struct nfsv4_time *)(t))->nfsv4_nsec = htonl((f)->tv_nsec); \
889ec7b004SRick Macklem } while (0)
899ec7b004SRick Macklem
909ec7b004SRick Macklem #define fxdr_hyper(f) \
919ec7b004SRick Macklem ((((u_quad_t)ntohl(((u_int32_t *)(f))[0])) << 32) | \
929ec7b004SRick Macklem (u_quad_t)(ntohl(((u_int32_t *)(f))[1])))
939ec7b004SRick Macklem
94*32fbc5d8SAlan Somers static inline void
txdr_hyper(uint64_t f,uint32_t * t)95*32fbc5d8SAlan Somers txdr_hyper(uint64_t f, uint32_t* t)
96*32fbc5d8SAlan Somers {
97*32fbc5d8SAlan Somers t[0] = htonl((u_int32_t)(f >> 32));
98*32fbc5d8SAlan Somers t[1] = htonl((u_int32_t)(f & 0xffffffff));
99*32fbc5d8SAlan Somers }
1009ec7b004SRick Macklem
1019ec7b004SRick Macklem #endif /* _NFS_XDR_SUBS_H_ */
102