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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
42 #ifdef GMX_INTERNAL_XDR
49 #include "gmx_system_xdr.h"
52 /* NB - THIS FILE IS ONLY USED ON MICROSOFT WINDOWS, since that
53 * system doesn't provide any standard XDR system libraries. It will
54 * most probably work on other platforms too, but make sure you
55 * test that the xtc files produced are ok before using it.
57 * This header file contains Gromacs versions of the definitions for
58 * Sun External Data Representation (XDR) headers and routines.
60 * On most UNIX systems this is already present as part of your
61 * system libraries, but since we want to make Gromacs portable to
62 * platforms like Microsoft Windows we have created a private version
63 * of the necessary routines and distribute them with the Gromacs source.
65 * Although the rest of Gromacs is GPL, you can copy and use the XDR
66 * routines in any way you want as long as you obey Sun's license:
68 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
69 * unrestricted use provided that this legend is included on all tape
70 * media and as a part of the software program in whole or part. Users
71 * may copy or modify Sun RPC without charge, but are not authorized
72 * to license or distribute it to anyone else except as part of a product or
73 * program developed by the user.
75 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
76 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
77 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
79 * Sun RPC is provided with no support and without any obligation on the
80 * part of Sun Microsystems, Inc. to assist in its use, correction,
81 * modification or enhancement.
83 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
84 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
85 * OR ANY PART THEREOF.
87 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
88 * or profits or other special, indirect and consequential damages, even if
89 * Sun has been advised of the possibility of such damages.
91 * Sun Microsystems, Inc.
93 * Mountain View, California 94043
101 static char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
103 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
116 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
119 if( *((char *)&s)==(char)0x0F) {
120 /* bigendian, do nothing */
123 /* smallendian,swap bytes */
124 return xdr_swapbytes(x);
128 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
131 if( *((char *)&s)==(char)0x0F) {
132 /* bigendian, do nothing */
135 /* smallendian, swap bytes */
136 return xdr_swapbytes(x);
142 * Free a data structure using XDR
143 * Not a filter, but a convenient utility nonetheless
146 xdr_free (xdrproc_t proc, char *objp)
167 xdr_int (XDR *xdrs, int *ip)
174 l = (xdr_int32_t) (*ip);
175 return xdr_putint32 (xdrs, &l);
178 if (!xdr_getint32 (xdrs, &l))
192 * XDR unsigned integers
195 xdr_u_int (XDR *xdrs, unsigned int *up)
202 l = (xdr_uint32_t) (*up);
203 return xdr_putuint32 (xdrs, &l);
206 if (!xdr_getuint32 (xdrs, &l))
210 *up = (unsigned int) l;
225 xdr_short (XDR *xdrs, short *sp)
232 l = (xdr_int32_t) *sp;
233 return xdr_putint32 (xdrs, &l);
236 if (!xdr_getint32 (xdrs, &l))
251 * XDR unsigned short integers
254 xdr_u_short (XDR *xdrs, unsigned short *usp)
261 l = (xdr_uint32_t) *usp;
262 return xdr_putuint32 (xdrs, &l);
265 if (!xdr_getuint32 (xdrs, &l))
269 *usp = (unsigned short) l;
283 xdr_char (XDR *xdrs, char *cp)
288 if (!xdr_int (xdrs, &i))
297 * XDR an unsigned char
300 xdr_u_char (XDR *xdrs, unsigned char *cp)
305 if (!xdr_u_int (xdrs, &u))
317 xdr_bool (XDR *xdrs, int *bp)
319 #define XDR_FALSE ((xdr_int32_t) 0)
320 #define XDR_TRUE ((xdr_int32_t) 1)
327 lb = *bp ? XDR_TRUE : XDR_FALSE;
328 return xdr_putint32 (xdrs, &lb);
331 if (!xdr_getint32 (xdrs, &lb))
335 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
350 * Allows the specification of a fixed size sequence of opaque bytes.
351 * cp points to the opaque object and cnt gives the byte length.
354 xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt)
357 char crud[BYTES_PER_XDR_UNIT];
360 * if no data we are done
366 * round byte count to full xdr units
368 rndup = cnt % BYTES_PER_XDR_UNIT;
370 rndup = BYTES_PER_XDR_UNIT - rndup;
375 if (!xdr_getbytes (xdrs, cp, cnt))
381 return xdr_getbytes (xdrs, (char *)crud, rndup);
384 if (!xdr_putbytes (xdrs, cp, cnt))
390 return xdr_putbytes (xdrs, xdr_zero, rndup);
400 * XDR null terminated ASCII strings
401 * xdr_string deals with "C strings" - arrays of bytes that are
402 * terminated by a NULL character. The parameter cpp references a
403 * pointer to storage; If the pointer is null, then the necessary
404 * storage is allocated. The last parameter is the max allowed length
405 * of the string as specified by a protocol.
408 xdr_string (xdrs, cpp, maxsize)
411 unsigned int maxsize;
413 char *sp = *cpp; /* sp is the actual string pointer */
414 unsigned int size = 0;
415 unsigned int nodesize = 0;
418 * first deal with the length since xdr strings are counted-strings
425 return TRUE; /* already free */
427 /* fall through... */
437 if (!xdr_u_int (xdrs, &size))
448 * now deal with the actual bytes
458 *cpp = sp = (char *) malloc (nodesize);
461 (void) fputs ("xdr_string: out of memory\n", stderr);
468 return xdr_opaque (xdrs, sp, size);
480 /* Floating-point stuff */
489 switch (xdrs->x_op) {
492 tmp = *(xdr_int32_t *)fp;
493 return (xdr_putint32(xdrs, &tmp));
498 if (xdr_getint32(xdrs, &tmp)) {
499 *(xdr_int32_t *)fp = tmp;
518 /* Windows and some other systems dont define double-precision
519 * word order in the header files, so unfortunately we have
522 * For thread safety, we calculate it every time: locking this would
525 /*static int LSW=-1;*/ /* Least significant fp word */
526 int LSW=-1; /* Least significant fp word */
533 double x=0.987654321; /* Just a number */
535 /* Possible representations in IEEE double precision:
536 * (S=small endian, B=big endian)
538 * Byte order, Word order, Hex
539 * S S b8 56 0e 3c dd 9a ef 3f
540 * B S 3c 0e 56 b8 3f ef 9a dd
541 * S B dd 9a ef 3f b8 56 0e 3c
542 * B B 3f ef 9a dd 3c 0e 56 b8
545 unsigned char ix = *((char *)&x);
547 if(ix==0xdd || ix==0x3f)
548 LSW=1; /* Big endian word order */
549 else if(ix==0xb8 || ix==0x3c)
550 LSW=0; /* Small endian word order */
551 else { /* Catch strange errors */
552 printf("Error when detecting floating-point word order.\n"
553 "Do you have a non-IEEE system?\n"
554 "If possible, use the XDR libraries provided with your system,\n"
555 "instead of the Gromacs fallback XDR source.\n");
560 switch (xdrs->x_op) {
566 return (xdr_putint32(xdrs, tmp) &&
567 xdr_putint32(xdrs, tmp+1));
573 if (xdr_getint32(xdrs, tmp+!LSW) &&
574 xdr_getint32(xdrs, tmp+LSW)) {
594 * XDR a fixed length array. Unlike variable-length arrays,
595 * the storage of fixed length arrays is static and unfreeable.
596 * > basep: base of the array
597 * > size: size of the array
598 * > elemsize: size of each element
599 * > xdr_elem: routine to XDR each element
602 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
606 unsigned int elemsize;
609 #define LASTUNSIGNED ((unsigned int)0-1)
614 for (i = 0; i < nelem; i++)
616 if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
628 static bool_t xdrstdio_getbytes (XDR *, char *, unsigned int);
629 static bool_t xdrstdio_putbytes (XDR *, char *, unsigned int);
630 static unsigned int xdrstdio_getpos (XDR *);
631 static bool_t xdrstdio_setpos (XDR *, unsigned int);
632 static xdr_int32_t *xdrstdio_inline (XDR *, int);
633 static void xdrstdio_destroy (XDR *);
634 static bool_t xdrstdio_getint32 (XDR *, xdr_int32_t *);
635 static bool_t xdrstdio_putint32 (XDR *, xdr_int32_t *);
636 static bool_t xdrstdio_getuint32 (XDR *, xdr_uint32_t *);
637 static bool_t xdrstdio_putuint32 (XDR *, xdr_uint32_t *);
640 * Ops vector for stdio type XDR
642 static const struct xdr_ops xdrstdio_ops =
644 xdrstdio_getbytes, /* deserialize counted bytes */
645 xdrstdio_putbytes, /* serialize counted bytes */
646 xdrstdio_getpos, /* get offset in the stream */
647 xdrstdio_setpos, /* set offset in the stream */
648 xdrstdio_inline, /* prime stream for inline macros */
649 xdrstdio_destroy, /* destroy stream */
650 xdrstdio_getint32, /* deserialize a int */
651 xdrstdio_putint32, /* serialize a int */
652 xdrstdio_getuint32, /* deserialize a int */
653 xdrstdio_putuint32 /* serialize a int */
657 * Initialize a stdio xdr stream.
658 * Sets the xdr stream handle xdrs for use on the stream file.
659 * Operation flag is set to op.
662 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
665 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
667 xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
668 xdrs->x_private = (char *) file;
674 * Destroy a stdio xdr stream.
675 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
678 xdrstdio_destroy (XDR *xdrs)
680 (void) fflush ((FILE *) xdrs->x_private);
681 /* xx should we close the file ?? */
686 xdrstdio_getbytes (XDR *xdrs, char *addr, unsigned int len)
688 if ((len != 0) && (fread (addr, (int) len, 1,
689 (FILE *) xdrs->x_private) != 1))
695 xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len)
697 if ((len != 0) && (fwrite (addr, (int) len, 1,
698 (FILE *) xdrs->x_private) != 1))
704 xdrstdio_getpos (XDR *xdrs)
706 return (unsigned int) ftell ((FILE *) xdrs->x_private);
710 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
712 return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
716 xdrstdio_inline (XDR *xdrs, int len)
719 * Must do some work to implement this: must insure
720 * enough data in the underlying stdio buffer,
721 * that the buffer is aligned so that we can indirect through a
722 * long *, and stuff this pointer in xdrs->x_buf. Doing
723 * a fread or fwrite to a scratch buffer would defeat
724 * most of the gains to be had here and require storage
725 * management on this buffer, so we don't do this.
731 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
735 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
737 *ip = xdr_ntohl (mycopy);
742 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
744 xdr_int32_t mycopy = xdr_htonl (*ip);
747 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
753 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
757 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
759 *ip = xdr_ntohl (mycopy);
764 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
766 xdr_uint32_t mycopy = xdr_htonl (*ip);
769 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
776 gmx_system_xdr_empty;
777 #endif /* GMX_SYSTEM_XDR */