2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
41 #ifdef GMX_INTERNAL_XDR
48 #include "gmx_system_xdr.h"
51 /* NB - THIS FILE IS ONLY USED ON MICROSOFT WINDOWS, since that
52 * system doesn't provide any standard XDR system libraries. It will
53 * most probably work on other platforms too, but make sure you
54 * test that the xtc files produced are ok before using it.
56 * This header file contains Gromacs versions of the definitions for
57 * Sun External Data Representation (XDR) headers and routines.
59 * On most UNIX systems this is already present as part of your
60 * system libraries, but since we want to make Gromacs portable to
61 * platforms like Microsoft Windows we have created a private version
62 * of the necessary routines and distribute them with the Gromacs source.
64 * Although the rest of Gromacs is LGPL, you can copy and use the XDR
65 * routines in any way you want as long as you obey Sun's license:
67 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
68 * unrestricted use provided that this legend is included on all tape
69 * media and as a part of the software program in whole or part. Users
70 * may copy or modify Sun RPC without charge, but are not authorized
71 * to license or distribute it to anyone else except as part of a product or
72 * program developed by the user.
74 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
75 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
76 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
78 * Sun RPC is provided with no support and without any obligation on the
79 * part of Sun Microsystems, Inc. to assist in its use, correction,
80 * modification or enhancement.
82 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
83 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
84 * OR ANY PART THEREOF.
86 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
87 * or profits or other special, indirect and consequential damages, even if
88 * Sun has been advised of the possibility of such damages.
90 * Sun Microsystems, Inc.
92 * Mountain View, California 94043
100 static char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
102 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
106 char *px = (char *)&x;
107 char *py = (char *)&y;
109 for (i = 0; i < 4; i++)
117 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
120 if (*((char *)&s) == (char)0x0F)
122 /* bigendian, do nothing */
127 /* smallendian,swap bytes */
128 return xdr_swapbytes(x);
132 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
135 if (*((char *)&s) == (char)0x0F)
137 /* bigendian, do nothing */
142 /* smallendian, swap bytes */
143 return xdr_swapbytes(x);
149 * Free a data structure using XDR
150 * Not a filter, but a convenient utility nonetheless
153 xdr_free (xdrproc_t proc, char *objp)
174 xdr_int (XDR *xdrs, int *ip)
181 l = (xdr_int32_t) (*ip);
182 return xdr_putint32 (xdrs, &l);
185 if (!xdr_getint32 (xdrs, &l))
199 * XDR unsigned integers
202 xdr_u_int (XDR *xdrs, unsigned int *up)
209 l = (xdr_uint32_t) (*up);
210 return xdr_putuint32 (xdrs, &l);
213 if (!xdr_getuint32 (xdrs, &l))
217 *up = (unsigned int) l;
232 xdr_short (XDR *xdrs, short *sp)
239 l = (xdr_int32_t) *sp;
240 return xdr_putint32 (xdrs, &l);
243 if (!xdr_getint32 (xdrs, &l))
258 * XDR unsigned short integers
261 xdr_u_short (XDR *xdrs, unsigned short *usp)
268 l = (xdr_uint32_t) *usp;
269 return xdr_putuint32 (xdrs, &l);
272 if (!xdr_getuint32 (xdrs, &l))
276 *usp = (unsigned short) l;
290 xdr_char (XDR *xdrs, char *cp)
295 if (!xdr_int (xdrs, &i))
304 * XDR an unsigned char
307 xdr_u_char (XDR *xdrs, unsigned char *cp)
312 if (!xdr_u_int (xdrs, &u))
324 xdr_bool (XDR *xdrs, int *bp)
326 #define XDR_FALSE ((xdr_int32_t) 0)
327 #define XDR_TRUE ((xdr_int32_t) 1)
334 lb = *bp ? XDR_TRUE : XDR_FALSE;
335 return xdr_putint32 (xdrs, &lb);
338 if (!xdr_getint32 (xdrs, &lb))
342 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
357 * Allows the specification of a fixed size sequence of opaque bytes.
358 * cp points to the opaque object and cnt gives the byte length.
361 xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt)
364 char crud[BYTES_PER_XDR_UNIT];
367 * if no data we are done
375 * round byte count to full xdr units
377 rndup = cnt % BYTES_PER_XDR_UNIT;
380 rndup = BYTES_PER_XDR_UNIT - rndup;
386 if (!xdr_getbytes (xdrs, cp, cnt))
394 return xdr_getbytes (xdrs, (char *)crud, rndup);
397 if (!xdr_putbytes (xdrs, cp, cnt))
405 return xdr_putbytes (xdrs, xdr_zero, rndup);
415 * XDR null terminated ASCII strings
416 * xdr_string deals with "C strings" - arrays of bytes that are
417 * terminated by a NULL character. The parameter cpp references a
418 * pointer to storage; If the pointer is null, then the necessary
419 * storage is allocated. The last parameter is the max allowed length
420 * of the string as specified by a protocol.
423 xdr_string (xdrs, cpp, maxsize)
426 unsigned int maxsize;
428 char *sp = *cpp; /* sp is the actual string pointer */
429 unsigned int size = 0;
430 unsigned int nodesize = 0;
433 * first deal with the length since xdr strings are counted-strings
440 return TRUE; /* already free */
442 /* fall through... */
454 if (!xdr_u_int (xdrs, &size))
465 * now deal with the actual bytes
476 *cpp = sp = (char *) malloc (nodesize);
480 (void) fputs ("xdr_string: out of memory\n", stderr);
487 return xdr_opaque (xdrs, sp, size);
499 /* Floating-point stuff */
512 tmp = *(xdr_int32_t *)fp;
513 return (xdr_putint32(xdrs, &tmp));
518 if (xdr_getint32(xdrs, &tmp))
520 *(xdr_int32_t *)fp = tmp;
539 /* Windows and some other systems dont define double-precision
540 * word order in the header files, so unfortunately we have
543 * For thread safety, we calculate it every time: locking this would
546 /*static int LSW=-1;*/ /* Least significant fp word */
547 int LSW = -1; /* Least significant fp word */
555 double x = 0.987654321; /* Just a number */
557 /* Possible representations in IEEE double precision:
558 * (S=small endian, B=big endian)
560 * Byte order, Word order, Hex
561 * S S b8 56 0e 3c dd 9a ef 3f
562 * B S 3c 0e 56 b8 3f ef 9a dd
563 * S B dd 9a ef 3f b8 56 0e 3c
564 * B B 3f ef 9a dd 3c 0e 56 b8
567 unsigned char ix = *((char *)&x);
569 if (ix == 0xdd || ix == 0x3f)
571 LSW = 1; /* Big endian word order */
573 else if (ix == 0xb8 || ix == 0x3c)
575 LSW = 0; /* Small endian word order */
577 else /* Catch strange errors */
579 printf("Error when detecting floating-point word order.\n"
580 "Do you have a non-IEEE system?\n"
581 "If possible, use the XDR libraries provided with your system,\n"
582 "instead of the Gromacs fallback XDR source.\n");
594 return (xdr_putint32(xdrs, tmp) &&
595 xdr_putint32(xdrs, tmp+1));
601 if (xdr_getint32(xdrs, tmp+!LSW) &&
602 xdr_getint32(xdrs, tmp+LSW))
623 * XDR a fixed length array. Unlike variable-length arrays,
624 * the storage of fixed length arrays is static and unfreeable.
625 * > basep: base of the array
626 * > size: size of the array
627 * > elemsize: size of each element
628 * > xdr_elem: routine to XDR each element
631 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
635 unsigned int elemsize;
638 #define LASTUNSIGNED ((unsigned int)0-1)
643 for (i = 0; i < nelem; i++)
645 if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
657 static bool_t xdrstdio_getbytes (XDR *, char *, unsigned int);
658 static bool_t xdrstdio_putbytes (XDR *, char *, unsigned int);
659 static unsigned int xdrstdio_getpos (XDR *);
660 static bool_t xdrstdio_setpos (XDR *, unsigned int);
661 static xdr_int32_t *xdrstdio_inline (XDR *, int);
662 static void xdrstdio_destroy (XDR *);
663 static bool_t xdrstdio_getint32 (XDR *, xdr_int32_t *);
664 static bool_t xdrstdio_putint32 (XDR *, xdr_int32_t *);
665 static bool_t xdrstdio_getuint32 (XDR *, xdr_uint32_t *);
666 static bool_t xdrstdio_putuint32 (XDR *, xdr_uint32_t *);
669 * Ops vector for stdio type XDR
671 static const struct xdr_ops xdrstdio_ops =
673 xdrstdio_getbytes, /* deserialize counted bytes */
674 xdrstdio_putbytes, /* serialize counted bytes */
675 xdrstdio_getpos, /* get offset in the stream */
676 xdrstdio_setpos, /* set offset in the stream */
677 xdrstdio_inline, /* prime stream for inline macros */
678 xdrstdio_destroy, /* destroy stream */
679 xdrstdio_getint32, /* deserialize a int */
680 xdrstdio_putint32, /* serialize a int */
681 xdrstdio_getuint32, /* deserialize a int */
682 xdrstdio_putuint32 /* serialize a int */
686 * Initialize a stdio xdr stream.
687 * Sets the xdr stream handle xdrs for use on the stream file.
688 * Operation flag is set to op.
691 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
694 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
696 xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
697 xdrs->x_private = (char *) file;
703 * Destroy a stdio xdr stream.
704 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
707 xdrstdio_destroy (XDR *xdrs)
709 (void) fflush ((FILE *) xdrs->x_private);
710 /* xx should we close the file ?? */
715 xdrstdio_getbytes (XDR *xdrs, char *addr, unsigned int len)
717 if ((len != 0) && (fread (addr, (int) len, 1,
718 (FILE *) xdrs->x_private) != 1))
726 xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len)
728 if ((len != 0) && (fwrite (addr, (int) len, 1,
729 (FILE *) xdrs->x_private) != 1))
737 xdrstdio_getpos (XDR *xdrs)
739 return (unsigned int) ftell ((FILE *) xdrs->x_private);
743 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
745 return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
749 xdrstdio_inline (XDR *xdrs, int len)
754 * Must do some work to implement this: must insure
755 * enough data in the underlying stdio buffer,
756 * that the buffer is aligned so that we can indirect through a
757 * long *, and stuff this pointer in xdrs->x_buf. Doing
758 * a fread or fwrite to a scratch buffer would defeat
759 * most of the gains to be had here and require storage
760 * management on this buffer, so we don't do this.
766 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
770 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
774 *ip = xdr_ntohl (mycopy);
779 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
781 xdr_int32_t mycopy = xdr_htonl (*ip);
784 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
792 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
796 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
800 *ip = xdr_ntohl (mycopy);
805 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
807 xdr_uint32_t mycopy = xdr_htonl (*ip);
810 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
819 gmx_system_xdr_empty;
820 #endif /* GMX_SYSTEM_XDR */