3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
39 #ifdef GMX_INTERNAL_XDR
46 #include "gmx_system_xdr.h"
49 /* NB - THIS FILE IS ONLY USED ON MICROSOFT WINDOWS, since that
50 * system doesn't provide any standard XDR system libraries. It will
51 * most probably work on other platforms too, but make sure you
52 * test that the xtc files produced are ok before using it.
54 * This header file contains Gromacs versions of the definitions for
55 * Sun External Data Representation (XDR) headers and routines.
57 * On most UNIX systems this is already present as part of your
58 * system libraries, but since we want to make Gromacs portable to
59 * platforms like Microsoft Windows we have created a private version
60 * of the necessary routines and distribute them with the Gromacs source.
62 * Although the rest of Gromacs is GPL, you can copy and use the XDR
63 * routines in any way you want as long as you obey Sun's license:
65 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
66 * unrestricted use provided that this legend is included on all tape
67 * media and as a part of the software program in whole or part. Users
68 * may copy or modify Sun RPC without charge, but are not authorized
69 * to license or distribute it to anyone else except as part of a product or
70 * program developed by the user.
72 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
73 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
74 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
76 * Sun RPC is provided with no support and without any obligation on the
77 * part of Sun Microsystems, Inc. to assist in its use, correction,
78 * modification or enhancement.
80 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
81 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
82 * OR ANY PART THEREOF.
84 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
85 * or profits or other special, indirect and consequential damages, even if
86 * Sun has been advised of the possibility of such damages.
88 * Sun Microsystems, Inc.
90 * Mountain View, California 94043
98 static char xdr_zero
[BYTES_PER_XDR_UNIT
] = {0, 0, 0, 0};
100 static xdr_uint32_t
xdr_swapbytes(xdr_uint32_t x
)
113 static xdr_uint32_t
xdr_htonl(xdr_uint32_t x
)
116 if( *((char *)&s
)==(char)0x0F) {
117 /* bigendian, do nothing */
120 /* smallendian,swap bytes */
121 return xdr_swapbytes(x
);
125 static xdr_uint32_t
xdr_ntohl(xdr_uint32_t x
)
128 if( *((char *)&s
)==(char)0x0F) {
129 /* bigendian, do nothing */
132 /* smallendian, swap bytes */
133 return xdr_swapbytes(x
);
139 * Free a data structure using XDR
140 * Not a filter, but a convenient utility nonetheless
143 xdr_free (xdrproc_t proc
, char *objp
)
164 xdr_int (XDR
*xdrs
, int *ip
)
171 l
= (xdr_int32_t
) (*ip
);
172 return xdr_putint32 (xdrs
, &l
);
175 if (!xdr_getint32 (xdrs
, &l
))
189 * XDR unsigned integers
192 xdr_u_int (XDR
*xdrs
, unsigned int *up
)
199 l
= (xdr_uint32_t
) (*up
);
200 return xdr_putuint32 (xdrs
, &l
);
203 if (!xdr_getuint32 (xdrs
, &l
))
207 *up
= (unsigned int) l
;
222 xdr_short (XDR
*xdrs
, short *sp
)
229 l
= (xdr_int32_t
) *sp
;
230 return xdr_putint32 (xdrs
, &l
);
233 if (!xdr_getint32 (xdrs
, &l
))
248 * XDR unsigned short integers
251 xdr_u_short (XDR
*xdrs
, unsigned short *usp
)
258 l
= (xdr_uint32_t
) *usp
;
259 return xdr_putuint32 (xdrs
, &l
);
262 if (!xdr_getuint32 (xdrs
, &l
))
266 *usp
= (unsigned short) l
;
280 xdr_char (XDR
*xdrs
, char *cp
)
285 if (!xdr_int (xdrs
, &i
))
294 * XDR an unsigned char
297 xdr_u_char (XDR
*xdrs
, unsigned char *cp
)
302 if (!xdr_u_int (xdrs
, &u
))
314 xdr_bool (XDR
*xdrs
, int *bp
)
316 #define XDR_FALSE ((xdr_int32_t) 0)
317 #define XDR_TRUE ((xdr_int32_t) 1)
324 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
325 return xdr_putint32 (xdrs
, &lb
);
328 if (!xdr_getint32 (xdrs
, &lb
))
332 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
347 * Allows the specification of a fixed size sequence of opaque bytes.
348 * cp points to the opaque object and cnt gives the byte length.
351 xdr_opaque (XDR
*xdrs
, char *cp
, unsigned int cnt
)
354 static char crud
[BYTES_PER_XDR_UNIT
];
357 * if no data we are done
363 * round byte count to full xdr units
365 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
367 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
372 if (!xdr_getbytes (xdrs
, cp
, cnt
))
378 return xdr_getbytes (xdrs
, (char *)crud
, rndup
);
381 if (!xdr_putbytes (xdrs
, cp
, cnt
))
387 return xdr_putbytes (xdrs
, xdr_zero
, rndup
);
397 * XDR null terminated ASCII strings
398 * xdr_string deals with "C strings" - arrays of bytes that are
399 * terminated by a NULL character. The parameter cpp references a
400 * pointer to storage; If the pointer is null, then the necessary
401 * storage is allocated. The last parameter is the max allowed length
402 * of the string as specified by a protocol.
405 xdr_string (xdrs
, cpp
, maxsize
)
408 unsigned int maxsize
;
410 char *sp
= *cpp
; /* sp is the actual string pointer */
411 unsigned int size
= 0;
412 unsigned int nodesize
= 0;
415 * first deal with the length since xdr strings are counted-strings
422 return TRUE
; /* already free */
424 /* fall through... */
434 if (!xdr_u_int (xdrs
, &size
))
445 * now deal with the actual bytes
455 *cpp
= sp
= (char *) malloc (nodesize
);
458 (void) fputs ("xdr_string: out of memory\n", stderr
);
465 return xdr_opaque (xdrs
, sp
, size
);
477 /* Floating-point stuff */
486 switch (xdrs
->x_op
) {
489 tmp
= *(xdr_int32_t
*)fp
;
490 return (xdr_putint32(xdrs
, &tmp
));
495 if (xdr_getint32(xdrs
, &tmp
)) {
496 *(xdr_int32_t
*)fp
= tmp
;
515 /* Windows and some other systems dont define double-precision
516 * word order in the header files, so unfortunately we have
519 static int LSW
=-1; /* Least significant fp word */
524 double x
=0.987654321; /* Just a number */
526 /* Possible representations in IEEE double precision:
527 * (S=small endian, B=big endian)
529 * Byte order, Word order, Hex
530 * S S b8 56 0e 3c dd 9a ef 3f
531 * B S 3c 0e 56 b8 3f ef 9a dd
532 * S B dd 9a ef 3f b8 56 0e 3c
533 * B B 3f ef 9a dd 3c 0e 56 b8
536 unsigned char ix
= *((char *)&x
);
538 if(ix
==0xdd || ix
==0x3f)
539 LSW
=1; /* Big endian word order */
540 else if(ix
==0xb8 || ix
==0x3c)
541 LSW
=0; /* Small endian word order */
542 else { /* Catch strange errors */
543 printf("Error when detecting floating-point word order.\n"
544 "Do you have a non-IEEE system?\n"
545 "If possible, use the XDR libraries provided with your system,\n"
546 "instead of the Gromacs fallback XDR source.\n");
551 switch (xdrs
->x_op
) {
557 return (xdr_putint32(xdrs
, tmp
) &&
558 xdr_putint32(xdrs
, tmp
+1));
564 if (xdr_getint32(xdrs
, tmp
+!LSW
) &&
565 xdr_getint32(xdrs
, tmp
+LSW
)) {
585 * XDR a fixed length array. Unlike variable-length arrays,
586 * the storage of fixed length arrays is static and unfreeable.
587 * > basep: base of the array
588 * > size: size of the array
589 * > elemsize: size of each element
590 * > xdr_elem: routine to XDR each element
593 xdr_vector (xdrs
, basep
, nelem
, elemsize
, xdr_elem
)
597 unsigned int elemsize
;
600 #define LASTUNSIGNED ((unsigned int)0-1)
605 for (i
= 0; i
< nelem
; i
++)
607 if (!(*xdr_elem
) (xdrs
, elptr
, LASTUNSIGNED
))
619 static bool_t
xdrstdio_getbytes (XDR
*, char *, unsigned int);
620 static bool_t
xdrstdio_putbytes (XDR
*, char *, unsigned int);
621 static unsigned int xdrstdio_getpos (XDR
*);
622 static bool_t
xdrstdio_setpos (XDR
*, unsigned int);
623 static xdr_int32_t
*xdrstdio_inline (XDR
*, int);
624 static void xdrstdio_destroy (XDR
*);
625 static bool_t
xdrstdio_getint32 (XDR
*, xdr_int32_t
*);
626 static bool_t
xdrstdio_putint32 (XDR
*, xdr_int32_t
*);
627 static bool_t
xdrstdio_getuint32 (XDR
*, xdr_uint32_t
*);
628 static bool_t
xdrstdio_putuint32 (XDR
*, xdr_uint32_t
*);
631 * Ops vector for stdio type XDR
633 static const struct xdr_ops xdrstdio_ops
=
635 xdrstdio_getbytes
, /* deserialize counted bytes */
636 xdrstdio_putbytes
, /* serialize counted bytes */
637 xdrstdio_getpos
, /* get offset in the stream */
638 xdrstdio_setpos
, /* set offset in the stream */
639 xdrstdio_inline
, /* prime stream for inline macros */
640 xdrstdio_destroy
, /* destroy stream */
641 xdrstdio_getint32
, /* deserialize a int */
642 xdrstdio_putint32
, /* serialize a int */
643 xdrstdio_getuint32
, /* deserialize a int */
644 xdrstdio_putuint32
/* serialize a int */
648 * Initialize a stdio xdr stream.
649 * Sets the xdr stream handle xdrs for use on the stream file.
650 * Operation flag is set to op.
653 xdrstdio_create (XDR
*xdrs
, FILE *file
, enum xdr_op op
)
656 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
658 xdrs
->x_ops
= (struct xdr_ops
*) &xdrstdio_ops
;
659 xdrs
->x_private
= (char *) file
;
665 * Destroy a stdio xdr stream.
666 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
669 xdrstdio_destroy (XDR
*xdrs
)
671 (void) fflush ((FILE *) xdrs
->x_private
);
672 /* xx should we close the file ?? */
677 xdrstdio_getbytes (XDR
*xdrs
, char *addr
, unsigned int len
)
679 if ((len
!= 0) && (fread (addr
, (int) len
, 1,
680 (FILE *) xdrs
->x_private
) != 1))
686 xdrstdio_putbytes (XDR
*xdrs
, char *addr
, unsigned int len
)
688 if ((len
!= 0) && (fwrite (addr
, (int) len
, 1,
689 (FILE *) xdrs
->x_private
) != 1))
695 xdrstdio_getpos (XDR
*xdrs
)
697 return (unsigned int) ftell ((FILE *) xdrs
->x_private
);
701 xdrstdio_setpos (XDR
*xdrs
, unsigned int pos
)
703 return fseek ((FILE *) xdrs
->x_private
, (xdr_int32_t
) pos
, 0) < 0 ? FALSE
: TRUE
;
707 xdrstdio_inline (XDR
*xdrs
, int len
)
710 * Must do some work to implement this: must insure
711 * enough data in the underlying stdio buffer,
712 * that the buffer is aligned so that we can indirect through a
713 * long *, and stuff this pointer in xdrs->x_buf. Doing
714 * a fread or fwrite to a scratch buffer would defeat
715 * most of the gains to be had here and require storage
716 * management on this buffer, so we don't do this.
722 xdrstdio_getint32 (XDR
*xdrs
, xdr_int32_t
*ip
)
726 if (fread ((char *) &mycopy
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
728 *ip
= xdr_ntohl (mycopy
);
733 xdrstdio_putint32 (XDR
*xdrs
, xdr_int32_t
*ip
)
735 xdr_int32_t mycopy
= xdr_htonl (*ip
);
738 if (fwrite ((char *) ip
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
744 xdrstdio_getuint32 (XDR
*xdrs
, xdr_uint32_t
*ip
)
748 if (fread ((char *) &mycopy
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
750 *ip
= xdr_ntohl (mycopy
);
755 xdrstdio_putuint32 (XDR
*xdrs
, xdr_uint32_t
*ip
)
757 xdr_uint32_t mycopy
= xdr_htonl (*ip
);
760 if (fwrite ((char *) ip
, 4, 1, (FILE *) xdrs
->x_private
) != 1)
767 gmx_system_xdr_empty
;
768 #endif /* GMX_SYSTEM_XDR */