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,2015,2018,2019 by the GROMACS development team.
7 * Copyright (c) 2021, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source 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.
45 # include "gmx_internal_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
99 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
101 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
105 char* px = reinterpret_cast<char*>(&x);
106 char* py = reinterpret_cast<char*>(&y);
108 for (i = 0; i < 4; i++)
116 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
119 if (*(reinterpret_cast<char*>(&s)) == static_cast<char>(0x0F))
121 /* bigendian, do nothing */
126 /* smallendian,swap bytes */
127 return xdr_swapbytes(x);
131 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
134 if (*(reinterpret_cast<char*>(&s)) == static_cast<char>(0x0F))
136 /* bigendian, do nothing */
141 /* smallendian, swap bytes */
142 return xdr_swapbytes(x);
148 * Free a data structure using XDR
149 * Not a filter, but a convenient utility nonetheless
151 void xdr_free(xdrproc_t proc, char* objp)
170 bool_t xdr_int(XDR* xdrs, int* ip)
176 case XDR_ENCODE: l = static_cast<xdr_int32_t>(*ip); return xdr_putint32(xdrs, &l);
179 if (!xdr_getint32(xdrs, &l))
183 *ip = static_cast<int>(l);
186 case XDR_FREE: return TRUE;
193 * XDR unsigned integers
195 bool_t xdr_u_int(XDR* xdrs, unsigned int* up)
201 case XDR_ENCODE: l = static_cast<xdr_uint32_t>(*up); return xdr_putuint32(xdrs, &l);
204 if (!xdr_getuint32(xdrs, &l))
208 *up = static_cast<unsigned int>(l);
211 case XDR_FREE: return TRUE;
220 bool_t xdr_short(XDR* xdrs, short* sp)
226 case XDR_ENCODE: l = static_cast<xdr_int32_t>(*sp); return xdr_putint32(xdrs, &l);
229 if (!xdr_getint32(xdrs, &l))
233 *sp = static_cast<short>(l);
236 case XDR_FREE: return TRUE;
243 * XDR unsigned short integers
245 bool_t xdr_u_short(XDR* xdrs, unsigned short* usp)
251 case XDR_ENCODE: l = static_cast<xdr_uint32_t>(*usp); return xdr_putuint32(xdrs, &l);
254 if (!xdr_getuint32(xdrs, &l))
258 *usp = static_cast<unsigned short>(l);
261 case XDR_FREE: return TRUE;
270 bool_t xdr_char(XDR* xdrs, char* cp)
275 if (!xdr_int(xdrs, &i))
284 * XDR an unsigned char
286 bool_t xdr_u_char(XDR* xdrs, unsigned char* cp)
291 if (!xdr_u_int(xdrs, &u))
302 bool_t xdr_bool(XDR* xdrs, int* bp)
304 # define XDR_FALSE static_cast<xdr_int32_t>(0)
305 # define XDR_TRUE static_cast<xdr_int32_t>(1)
311 case XDR_ENCODE: lb = *bp ? XDR_TRUE : XDR_FALSE; return xdr_putint32(xdrs, &lb);
314 if (!xdr_getint32(xdrs, &lb))
318 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
321 case XDR_FREE: return TRUE;
331 * Allows the specification of a fixed size sequence of opaque bytes.
332 * cp points to the opaque object and cnt gives the byte length.
334 bool_t xdr_opaque(XDR* xdrs, char* cp, unsigned int cnt)
337 char crud[BYTES_PER_XDR_UNIT];
340 * if no data we are done
348 * round byte count to full xdr units
350 rndup = cnt % BYTES_PER_XDR_UNIT;
353 rndup = BYTES_PER_XDR_UNIT - rndup;
359 if (!xdr_getbytes(xdrs, cp, cnt))
367 return xdr_getbytes(xdrs, crud, rndup);
370 if (!xdr_putbytes(xdrs, cp, cnt))
378 return xdr_putbytes(xdrs, xdr_zero, rndup);
380 case XDR_FREE: return TRUE;
387 * XDR null terminated ASCII strings
388 * xdr_string deals with "C strings" - arrays of bytes that are
389 * terminated by a NULL character. The parameter cpp references a
390 * pointer to storage; If the pointer is null, then the necessary
391 * storage is allocated. The last parameter is the max allowed length
392 * of the string as specified by a protocol.
394 bool_t xdr_string(XDR* xdrs, char** cpp, unsigned int maxsize)
396 char* sp = *cpp; /* sp is the actual string pointer */
397 unsigned int size = 0;
398 unsigned int nodesize = 0;
401 * first deal with the length since xdr strings are counted-strings
408 return TRUE; /* already free */
410 size = std::strlen(sp);
418 size = std::strlen(sp);
420 case XDR_DECODE: break;
423 if (!xdr_u_int(xdrs, &size))
434 * now deal with the actual bytes
445 *cpp = sp = static_cast<char*>(std::malloc(nodesize));
449 (void)fputs("xdr_string: out of memory\n", stderr);
453 return xdr_opaque(xdrs, sp, size);
455 case XDR_ENCODE: return xdr_opaque(xdrs, sp, size);
466 /* Floating-point stuff */
468 bool_t xdr_float(XDR* xdrs, float* fp)
476 tmp = *(reinterpret_cast<xdr_int32_t*>(fp));
477 return (xdr_putint32(xdrs, &tmp));
480 if (xdr_getint32(xdrs, &tmp))
482 *(reinterpret_cast<xdr_int32_t*>(fp)) = tmp;
488 case XDR_FREE: return (TRUE);
494 bool_t xdr_double(XDR* xdrs, double* dp)
497 /* Windows and some other systems don't define double-precision
498 * word order in the header files, so unfortunately we have
501 * For thread safety, we calculate it every time: locking this would
504 /*static int LSW=-1;*/ /* Least significant fp word */
505 int LSW = -1; /* Least significant fp word */
513 double x = 0.987654321; /* Just a number */
515 /* Possible representations in IEEE double precision:
516 * (S=small endian, B=big endian)
518 * Byte order, Word order, Hex
519 * S S b8 56 0e 3c dd 9a ef 3f
520 * B S 3c 0e 56 b8 3f ef 9a dd
521 * S B dd 9a ef 3f b8 56 0e 3c
522 * B B 3f ef 9a dd 3c 0e 56 b8
525 unsigned char ix = *(reinterpret_cast<char*>(&x));
527 if (ix == 0xdd || ix == 0x3f)
529 LSW = 1; /* Big endian word order */
531 else if (ix == 0xb8 || ix == 0x3c)
533 LSW = 0; /* Small endian word order */
535 else /* Catch strange errors */
537 printf("Error when detecting floating-point word order.\n"
538 "Do you have a non-IEEE system?\n"
539 "If possible, use the XDR libraries provided with your system,\n"
540 "instead of the GROMACS fallback XDR source.\n");
549 ip = reinterpret_cast<int*>(dp);
550 tmp[0] = ip[bool(LSW == 0)];
552 return static_cast<bool_t>(bool(xdr_putint32(xdrs, tmp)) && bool(xdr_putint32(xdrs, tmp + 1)));
555 ip = reinterpret_cast<int*>(dp);
556 if (xdr_getint32(xdrs, tmp + !LSW) && xdr_getint32(xdrs, tmp + LSW))
565 case XDR_FREE: return (TRUE);
576 * XDR a fixed length array. Unlike variable-length arrays,
577 * the storage of fixed length arrays is static and unfreeable.
578 * > basep: base of the array
579 * > size: size of the array
580 * > elemsize: size of each element
581 * > xdr_elem: routine to XDR each element
583 bool_t xdr_vector(XDR* xdrs, char* basep, unsigned int nelem, unsigned int elemsize, xdrproc_t xdr_elem)
585 # define LASTUNSIGNED (static_cast<unsigned int>(0) - 1)
590 for (i = 0; i < nelem; i++)
592 if (!(*xdr_elem)(xdrs, elptr, LASTUNSIGNED))
603 static bool_t xdrstdio_getbytes(XDR* /*xdrs*/, char* /*addr*/, unsigned int /*len*/);
604 static bool_t xdrstdio_putbytes(XDR* /*xdrs*/, char* /*addr*/, unsigned int /*len*/);
605 static unsigned int xdrstdio_getpos(XDR* /*xdrs*/);
606 static bool_t xdrstdio_setpos(XDR* /*xdrs*/, unsigned int /*pos*/);
607 static xdr_int32_t* xdrstdio_inline(XDR* /*xdrs*/, int /*len*/);
608 static void xdrstdio_destroy(XDR* /*xdrs*/);
609 static bool_t xdrstdio_getint32(XDR* /*xdrs*/, xdr_int32_t* /*ip*/);
610 static bool_t xdrstdio_putint32(XDR* /*xdrs*/, xdr_int32_t* /*ip*/);
611 static bool_t xdrstdio_getuint32(XDR* /*xdrs*/, xdr_uint32_t* /*ip*/);
612 static bool_t xdrstdio_putuint32(XDR* /*xdrs*/, xdr_uint32_t* /*ip*/);
615 * Destroy a stdio xdr stream.
616 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
618 static void xdrstdio_destroy(XDR* xdrs)
620 fflush(reinterpret_cast<FILE*>(xdrs->x_private));
621 /* xx should we close the file ?? */
625 static bool_t xdrstdio_getbytes(XDR* xdrs, char* addr, unsigned int len)
628 && (fread(addr, static_cast<int>(len), 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1))
635 static bool_t xdrstdio_putbytes(XDR* xdrs, char* addr, unsigned int len)
638 && (fwrite(addr, static_cast<int>(len), 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1))
645 static unsigned int xdrstdio_getpos(XDR* xdrs)
647 return static_cast<int>(ftell(reinterpret_cast<FILE*>(xdrs->x_private)));
650 static bool_t xdrstdio_setpos(XDR* xdrs, unsigned int pos)
652 return fseek(reinterpret_cast<FILE*>(xdrs->x_private), static_cast<xdr_int32_t>(pos), 0) < 0 ? FALSE
656 static xdr_int32_t* xdrstdio_inline(XDR* xdrs, int len)
661 * Must do some work to implement this: must insure
662 * enough data in the underlying stdio buffer,
663 * that the buffer is aligned so that we can indirect through a
664 * long *, and stuff this pointer in xdrs->x_buf. Doing
665 * a fread or fwrite to a scratch buffer would defeat
666 * most of the gains to be had here and require storage
667 * management on this buffer, so we don't do this.
672 static bool_t xdrstdio_getint32(XDR* xdrs, xdr_int32_t* ip)
676 if (fread(&mycopy, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
680 *ip = xdr_ntohl(mycopy);
684 static bool_t xdrstdio_putint32(XDR* xdrs, xdr_int32_t* ip)
686 xdr_int32_t mycopy = xdr_htonl(*ip);
689 if (fwrite(ip, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
696 static bool_t xdrstdio_getuint32(XDR* xdrs, xdr_uint32_t* ip)
700 if (fread(&mycopy, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
704 *ip = xdr_ntohl(mycopy);
708 static bool_t xdrstdio_putuint32(XDR* xdrs, xdr_uint32_t* ip)
710 xdr_uint32_t mycopy = xdr_htonl(*ip);
713 if (fwrite(ip, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
721 * Ops vector for stdio type XDR
723 static struct XDR::xdr_ops xdrstdio_ops = {
724 xdrstdio_getbytes, /* deserialize counted bytes */
725 xdrstdio_putbytes, /* serialize counted bytes */
726 xdrstdio_getpos, /* get offset in the stream */
727 xdrstdio_setpos, /* set offset in the stream */
728 xdrstdio_inline, /* prime stream for inline macros */
729 xdrstdio_destroy, /* destroy stream */
730 xdrstdio_getint32, /* deserialize a int */
731 xdrstdio_putint32, /* serialize a int */
732 xdrstdio_getuint32, /* deserialize a int */
733 xdrstdio_putuint32 /* serialize a int */
737 * Initialize a stdio xdr stream.
738 * Sets the xdr stream handle xdrs for use on the stream file.
739 * Operation flag is set to op.
741 void xdrstdio_create(XDR* xdrs, FILE* file, enum xdr_op op)
744 xdrs->x_ops = &xdrstdio_ops;
745 xdrs->x_private = reinterpret_cast<char*>(file);
747 xdrs->x_base = nullptr;
749 #endif /* GMX_INTERNAL_XDR */