Re-organize BlueGene toolchain files
[gromacs.git] / src / gmxlib / gmx_system_xdr.c
blob7694368137a8f7e707c6bcce7f97e95fd53df8ef
1 /*
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.
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
42 #ifdef GMX_INTERNAL_XDR
45 #include <stdlib.h>
46 #include <limits.h>
47 #include <string.h>
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.
92 * 2550 Garcia Avenue
93 * Mountain View, California 94043
94 */
99 * for unit alignment
101 static char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
103 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
105 xdr_uint32_t y;
106 int i;
107 char *px=(char *)&x;
108 char *py=(char *)&y;
110 for(i=0;i<4;i++)
111 py[i]=px[3-i];
113 return y;
116 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
118 short s=0x0F00;
119 if( *((char *)&s)==(char)0x0F) {
120 /* bigendian, do nothing */
121 return x;
122 } else {
123 /* smallendian,swap bytes */
124 return xdr_swapbytes(x);
128 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
130 short s=0x0F00;
131 if( *((char *)&s)==(char)0x0F) {
132 /* bigendian, do nothing */
133 return x;
134 } else {
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
145 void
146 xdr_free (xdrproc_t proc, char *objp)
148 XDR x;
150 x.x_op = XDR_FREE;
151 (*proc) (&x, objp);
155 * XDR nothing
157 bool_t
158 xdr_void (void)
160 return TRUE;
164 * XDR integers
166 bool_t
167 xdr_int (XDR *xdrs, int *ip)
169 xdr_int32_t l;
171 switch (xdrs->x_op)
173 case XDR_ENCODE:
174 l = (xdr_int32_t) (*ip);
175 return xdr_putint32 (xdrs, &l);
177 case XDR_DECODE:
178 if (!xdr_getint32 (xdrs, &l))
180 return FALSE;
182 *ip = (int) l;
184 case XDR_FREE:
185 return TRUE;
187 return FALSE;
192 * XDR unsigned integers
194 bool_t
195 xdr_u_int (XDR *xdrs, unsigned int *up)
197 xdr_uint32_t l;
199 switch (xdrs->x_op)
201 case XDR_ENCODE:
202 l = (xdr_uint32_t) (*up);
203 return xdr_putuint32 (xdrs, &l);
205 case XDR_DECODE:
206 if (!xdr_getuint32 (xdrs, &l))
208 return FALSE;
210 *up = (unsigned int) l;
212 case XDR_FREE:
213 return TRUE;
215 return FALSE;
222 * XDR short integers
224 bool_t
225 xdr_short (XDR *xdrs, short *sp)
227 xdr_int32_t l;
229 switch (xdrs->x_op)
231 case XDR_ENCODE:
232 l = (xdr_int32_t) *sp;
233 return xdr_putint32 (xdrs, &l);
235 case XDR_DECODE:
236 if (!xdr_getint32 (xdrs, &l))
238 return FALSE;
240 *sp = (short) l;
241 return TRUE;
243 case XDR_FREE:
244 return TRUE;
246 return FALSE;
251 * XDR unsigned short integers
253 bool_t
254 xdr_u_short (XDR *xdrs, unsigned short *usp)
256 xdr_uint32_t l;
258 switch (xdrs->x_op)
260 case XDR_ENCODE:
261 l = (xdr_uint32_t) *usp;
262 return xdr_putuint32 (xdrs, &l);
264 case XDR_DECODE:
265 if (!xdr_getuint32 (xdrs, &l))
267 return FALSE;
269 *usp = (unsigned short) l;
270 return TRUE;
272 case XDR_FREE:
273 return TRUE;
275 return FALSE;
280 * XDR a char
282 bool_t
283 xdr_char (XDR *xdrs, char *cp)
285 int i;
287 i = (*cp);
288 if (!xdr_int (xdrs, &i))
290 return FALSE;
292 *cp = i;
293 return TRUE;
297 * XDR an unsigned char
299 bool_t
300 xdr_u_char (XDR *xdrs, unsigned char *cp)
302 unsigned int u;
304 u = (*cp);
305 if (!xdr_u_int (xdrs, &u))
307 return FALSE;
309 *cp = u;
310 return TRUE;
314 * XDR booleans
316 bool_t
317 xdr_bool (XDR *xdrs, int *bp)
319 #define XDR_FALSE ((xdr_int32_t) 0)
320 #define XDR_TRUE ((xdr_int32_t) 1)
322 xdr_int32_t lb;
324 switch (xdrs->x_op)
326 case XDR_ENCODE:
327 lb = *bp ? XDR_TRUE : XDR_FALSE;
328 return xdr_putint32 (xdrs, &lb);
330 case XDR_DECODE:
331 if (!xdr_getint32 (xdrs, &lb))
333 return FALSE;
335 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
336 return TRUE;
338 case XDR_FREE:
339 return TRUE;
341 return FALSE;
342 #undef XDR_FALSE
343 #undef XDR_TRUE
349 * XDR opaque data
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.
353 bool_t
354 xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt)
356 unsigned int rndup;
357 char crud[BYTES_PER_XDR_UNIT];
360 * if no data we are done
362 if (cnt == 0)
363 return TRUE;
366 * round byte count to full xdr units
368 rndup = cnt % BYTES_PER_XDR_UNIT;
369 if (rndup > 0)
370 rndup = BYTES_PER_XDR_UNIT - rndup;
372 switch (xdrs->x_op)
374 case XDR_DECODE:
375 if (!xdr_getbytes (xdrs, cp, cnt))
377 return FALSE;
379 if (rndup == 0)
380 return TRUE;
381 return xdr_getbytes (xdrs, (char *)crud, rndup);
383 case XDR_ENCODE:
384 if (!xdr_putbytes (xdrs, cp, cnt))
386 return FALSE;
388 if (rndup == 0)
389 return TRUE;
390 return xdr_putbytes (xdrs, xdr_zero, rndup);
392 case XDR_FREE:
393 return TRUE;
395 return FALSE;
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.
407 bool_t
408 xdr_string (xdrs, cpp, maxsize)
409 XDR *xdrs;
410 char **cpp;
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
420 switch (xdrs->x_op)
422 case XDR_FREE:
423 if (sp == NULL)
425 return TRUE; /* already free */
427 /* fall through... */
428 case XDR_ENCODE:
429 if (sp == NULL)
430 return FALSE;
431 size = strlen (sp);
432 break;
433 case XDR_DECODE:
434 break;
437 if (!xdr_u_int (xdrs, &size))
439 return FALSE;
441 if (size > maxsize)
443 return FALSE;
445 nodesize = size + 1;
448 * now deal with the actual bytes
450 switch (xdrs->x_op)
452 case XDR_DECODE:
453 if (nodesize == 0)
455 return TRUE;
457 if (sp == NULL)
458 *cpp = sp = (char *) malloc (nodesize);
459 if (sp == NULL)
461 (void) fputs ("xdr_string: out of memory\n", stderr);
462 return FALSE;
464 sp[size] = 0;
465 /* fall into ... */
467 case XDR_ENCODE:
468 return xdr_opaque (xdrs, sp, size);
470 case XDR_FREE:
471 free (sp);
472 *cpp = NULL;
473 return TRUE;
475 return FALSE;
480 /* Floating-point stuff */
482 bool_t
483 xdr_float(xdrs, fp)
484 XDR *xdrs;
485 float *fp;
487 xdr_int32_t tmp;
489 switch (xdrs->x_op) {
491 case XDR_ENCODE:
492 tmp = *(xdr_int32_t *)fp;
493 return (xdr_putint32(xdrs, &tmp));
495 break;
497 case XDR_DECODE:
498 if (xdr_getint32(xdrs, &tmp)) {
499 *(xdr_int32_t *)fp = tmp;
500 return (TRUE);
503 break;
505 case XDR_FREE:
506 return (TRUE);
508 return (FALSE);
512 bool_t
513 xdr_double(xdrs, dp)
514 XDR *xdrs;
515 double *dp;
518 /* Windows and some other systems dont define double-precision
519 * word order in the header files, so unfortunately we have
520 * to calculate it!
522 * For thread safety, we calculate it every time: locking this would
523 * be more expensive.
525 /*static int LSW=-1;*/ /* Least significant fp word */
526 int LSW=-1; /* Least significant fp word */
529 int *ip;
530 xdr_int32_t tmp[2];
532 if(LSW<0) {
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");
556 exit(0);
560 switch (xdrs->x_op) {
562 case XDR_ENCODE:
563 ip = (int *)dp;
564 tmp[0] = ip[!LSW];
565 tmp[1] = ip[LSW];
566 return (xdr_putint32(xdrs, tmp) &&
567 xdr_putint32(xdrs, tmp+1));
569 break;
571 case XDR_DECODE:
572 ip = (int *)dp;
573 if (xdr_getint32(xdrs, tmp+!LSW) &&
574 xdr_getint32(xdrs, tmp+LSW)) {
575 ip[0] = tmp[0];
576 ip[1] = tmp[1];
577 return (TRUE);
580 break;
582 case XDR_FREE:
583 return (TRUE);
585 return (FALSE);
589 /* Array routines */
592 * xdr_vector():
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
601 bool_t
602 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
603 XDR *xdrs;
604 char *basep;
605 unsigned int nelem;
606 unsigned int elemsize;
607 xdrproc_t xdr_elem;
609 #define LASTUNSIGNED ((unsigned int)0-1)
610 unsigned int i;
611 char *elptr;
613 elptr = basep;
614 for (i = 0; i < nelem; i++)
616 if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
618 return FALSE;
620 elptr += elemsize;
622 return TRUE;
623 #undef 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.
661 void
662 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
664 xdrs->x_op = op;
665 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
666 is not `const'. */
667 xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
668 xdrs->x_private = (char *) file;
669 xdrs->x_handy = 0;
670 xdrs->x_base = 0;
674 * Destroy a stdio xdr stream.
675 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
677 static void
678 xdrstdio_destroy (XDR *xdrs)
680 (void) fflush ((FILE *) xdrs->x_private);
681 /* xx should we close the file ?? */
685 static bool_t
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))
690 return FALSE;
691 return TRUE;
694 static bool_t
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))
699 return FALSE;
700 return TRUE;
703 static unsigned int
704 xdrstdio_getpos (XDR *xdrs)
706 return (unsigned int) ftell ((FILE *) xdrs->x_private);
709 static bool_t
710 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
712 return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
715 static xdr_int32_t *
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.
727 return NULL;
730 static bool_t
731 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
733 xdr_int32_t mycopy;
735 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
736 return FALSE;
737 *ip = xdr_ntohl (mycopy);
738 return TRUE;
741 static bool_t
742 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
744 xdr_int32_t mycopy = xdr_htonl (*ip);
746 ip = &mycopy;
747 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
748 return FALSE;
749 return TRUE;
752 static bool_t
753 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
755 xdr_uint32_t mycopy;
757 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
758 return FALSE;
759 *ip = xdr_ntohl (mycopy);
760 return TRUE;
763 static bool_t
764 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
766 xdr_uint32_t mycopy = xdr_htonl (*ip);
768 ip = &mycopy;
769 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
770 return FALSE;
771 return TRUE;
774 #else
776 gmx_system_xdr_empty;
777 #endif /* GMX_SYSTEM_XDR */