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,2013, 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)
107 char *px = (char *)&x;
108 char *py = (char *)&y;
110 for (i = 0; i < 4; i++)
118 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
121 if (*((char *)&s) == (char)0x0F)
123 /* bigendian, do nothing */
128 /* smallendian,swap bytes */
129 return xdr_swapbytes(x);
133 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
136 if (*((char *)&s) == (char)0x0F)
138 /* bigendian, do nothing */
143 /* smallendian, swap bytes */
144 return xdr_swapbytes(x);
150 * Free a data structure using XDR
151 * Not a filter, but a convenient utility nonetheless
154 xdr_free (xdrproc_t proc, char *objp)
175 xdr_int (XDR *xdrs, int *ip)
182 l = (xdr_int32_t) (*ip);
183 return xdr_putint32 (xdrs, &l);
186 if (!xdr_getint32 (xdrs, &l))
200 * XDR unsigned integers
203 xdr_u_int (XDR *xdrs, unsigned int *up)
210 l = (xdr_uint32_t) (*up);
211 return xdr_putuint32 (xdrs, &l);
214 if (!xdr_getuint32 (xdrs, &l))
218 *up = (unsigned int) l;
233 xdr_short (XDR *xdrs, short *sp)
240 l = (xdr_int32_t) *sp;
241 return xdr_putint32 (xdrs, &l);
244 if (!xdr_getint32 (xdrs, &l))
259 * XDR unsigned short integers
262 xdr_u_short (XDR *xdrs, unsigned short *usp)
269 l = (xdr_uint32_t) *usp;
270 return xdr_putuint32 (xdrs, &l);
273 if (!xdr_getuint32 (xdrs, &l))
277 *usp = (unsigned short) l;
291 xdr_char (XDR *xdrs, char *cp)
296 if (!xdr_int (xdrs, &i))
305 * XDR an unsigned char
308 xdr_u_char (XDR *xdrs, unsigned char *cp)
313 if (!xdr_u_int (xdrs, &u))
325 xdr_bool (XDR *xdrs, int *bp)
327 #define XDR_FALSE ((xdr_int32_t) 0)
328 #define XDR_TRUE ((xdr_int32_t) 1)
335 lb = *bp ? XDR_TRUE : XDR_FALSE;
336 return xdr_putint32 (xdrs, &lb);
339 if (!xdr_getint32 (xdrs, &lb))
343 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
358 * Allows the specification of a fixed size sequence of opaque bytes.
359 * cp points to the opaque object and cnt gives the byte length.
362 xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt)
365 char crud[BYTES_PER_XDR_UNIT];
368 * if no data we are done
376 * round byte count to full xdr units
378 rndup = cnt % BYTES_PER_XDR_UNIT;
381 rndup = BYTES_PER_XDR_UNIT - rndup;
387 if (!xdr_getbytes (xdrs, cp, cnt))
395 return xdr_getbytes (xdrs, (char *)crud, rndup);
398 if (!xdr_putbytes (xdrs, cp, cnt))
406 return xdr_putbytes (xdrs, xdr_zero, rndup);
416 * XDR null terminated ASCII strings
417 * xdr_string deals with "C strings" - arrays of bytes that are
418 * terminated by a NULL character. The parameter cpp references a
419 * pointer to storage; If the pointer is null, then the necessary
420 * storage is allocated. The last parameter is the max allowed length
421 * of the string as specified by a protocol.
424 xdr_string (xdrs, cpp, maxsize)
427 unsigned int maxsize;
429 char *sp = *cpp; /* sp is the actual string pointer */
430 unsigned int size = 0;
431 unsigned int nodesize = 0;
434 * first deal with the length since xdr strings are counted-strings
441 return TRUE; /* already free */
443 /* fall through... */
455 if (!xdr_u_int (xdrs, &size))
466 * now deal with the actual bytes
477 *cpp = sp = (char *) malloc (nodesize);
481 (void) fputs ("xdr_string: out of memory\n", stderr);
488 return xdr_opaque (xdrs, sp, size);
500 /* Floating-point stuff */
513 tmp = *(xdr_int32_t *)fp;
514 return (xdr_putint32(xdrs, &tmp));
519 if (xdr_getint32(xdrs, &tmp))
521 *(xdr_int32_t *)fp = tmp;
540 /* Windows and some other systems dont define double-precision
541 * word order in the header files, so unfortunately we have
544 * For thread safety, we calculate it every time: locking this would
547 /*static int LSW=-1;*/ /* Least significant fp word */
548 int LSW = -1; /* Least significant fp word */
556 double x = 0.987654321; /* Just a number */
558 /* Possible representations in IEEE double precision:
559 * (S=small endian, B=big endian)
561 * Byte order, Word order, Hex
562 * S S b8 56 0e 3c dd 9a ef 3f
563 * B S 3c 0e 56 b8 3f ef 9a dd
564 * S B dd 9a ef 3f b8 56 0e 3c
565 * B B 3f ef 9a dd 3c 0e 56 b8
568 unsigned char ix = *((char *)&x);
570 if (ix == 0xdd || ix == 0x3f)
572 LSW = 1; /* Big endian word order */
574 else if (ix == 0xb8 || ix == 0x3c)
576 LSW = 0; /* Small endian word order */
578 else /* Catch strange errors */
580 printf("Error when detecting floating-point word order.\n"
581 "Do you have a non-IEEE system?\n"
582 "If possible, use the XDR libraries provided with your system,\n"
583 "instead of the Gromacs fallback XDR source.\n");
595 return (xdr_putint32(xdrs, tmp) &&
596 xdr_putint32(xdrs, tmp+1));
602 if (xdr_getint32(xdrs, tmp+!LSW) &&
603 xdr_getint32(xdrs, tmp+LSW))
624 * XDR a fixed length array. Unlike variable-length arrays,
625 * the storage of fixed length arrays is static and unfreeable.
626 * > basep: base of the array
627 * > size: size of the array
628 * > elemsize: size of each element
629 * > xdr_elem: routine to XDR each element
632 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
636 unsigned int elemsize;
639 #define LASTUNSIGNED ((unsigned int)0-1)
644 for (i = 0; i < nelem; i++)
646 if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
658 static bool_t xdrstdio_getbytes (XDR *, char *, unsigned int);
659 static bool_t xdrstdio_putbytes (XDR *, char *, unsigned int);
660 static unsigned int xdrstdio_getpos (XDR *);
661 static bool_t xdrstdio_setpos (XDR *, unsigned int);
662 static xdr_int32_t *xdrstdio_inline (XDR *, int);
663 static void xdrstdio_destroy (XDR *);
664 static bool_t xdrstdio_getint32 (XDR *, xdr_int32_t *);
665 static bool_t xdrstdio_putint32 (XDR *, xdr_int32_t *);
666 static bool_t xdrstdio_getuint32 (XDR *, xdr_uint32_t *);
667 static bool_t xdrstdio_putuint32 (XDR *, xdr_uint32_t *);
670 * Ops vector for stdio type XDR
672 static const struct xdr_ops xdrstdio_ops =
674 xdrstdio_getbytes, /* deserialize counted bytes */
675 xdrstdio_putbytes, /* serialize counted bytes */
676 xdrstdio_getpos, /* get offset in the stream */
677 xdrstdio_setpos, /* set offset in the stream */
678 xdrstdio_inline, /* prime stream for inline macros */
679 xdrstdio_destroy, /* destroy stream */
680 xdrstdio_getint32, /* deserialize a int */
681 xdrstdio_putint32, /* serialize a int */
682 xdrstdio_getuint32, /* deserialize a int */
683 xdrstdio_putuint32 /* serialize a int */
687 * Initialize a stdio xdr stream.
688 * Sets the xdr stream handle xdrs for use on the stream file.
689 * Operation flag is set to op.
692 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
695 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
697 xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
698 xdrs->x_private = (char *) file;
704 * Destroy a stdio xdr stream.
705 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
708 xdrstdio_destroy (XDR *xdrs)
710 (void) fflush ((FILE *) xdrs->x_private);
711 /* xx should we close the file ?? */
716 xdrstdio_getbytes (XDR *xdrs, char *addr, unsigned int len)
718 if ((len != 0) && (fread (addr, (int) len, 1,
719 (FILE *) xdrs->x_private) != 1))
727 xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len)
729 if ((len != 0) && (fwrite (addr, (int) len, 1,
730 (FILE *) xdrs->x_private) != 1))
738 xdrstdio_getpos (XDR *xdrs)
740 return (unsigned int) ftell ((FILE *) xdrs->x_private);
744 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
746 return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
750 xdrstdio_inline (XDR *xdrs, int len)
753 * Must do some work to implement this: must insure
754 * enough data in the underlying stdio buffer,
755 * that the buffer is aligned so that we can indirect through a
756 * long *, and stuff this pointer in xdrs->x_buf. Doing
757 * a fread or fwrite to a scratch buffer would defeat
758 * most of the gains to be had here and require storage
759 * management on this buffer, so we don't do this.
765 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
769 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
773 *ip = xdr_ntohl (mycopy);
778 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
780 xdr_int32_t mycopy = xdr_htonl (*ip);
783 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
791 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
795 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
799 *ip = xdr_ntohl (mycopy);
804 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
806 xdr_uint32_t mycopy = xdr_htonl (*ip);
809 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
818 gmx_system_xdr_empty;
819 #endif /* GMX_SYSTEM_XDR */