a00f2487242e1db44899e3f455993003e7dee34e
[alexxy/gromacs.git] / src / gmxlib / gmx_system_xdr.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifdef GMX_INTERNAL_XDR
43
44
45 #include <stdlib.h>
46 #include <limits.h>
47 #include <string.h>
48
49 #include "gmx_system_xdr.h"
50
51
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.
56  *
57  * This header file contains Gromacs versions of the definitions for
58  * Sun External Data Representation (XDR) headers and routines.
59  *
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.
64  *
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:
67  *
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.
74  *
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.
78  *
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.
82  *
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.
86  *
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.
90  *
91  * Sun Microsystems, Inc.
92  * 2550 Garcia Avenue
93  * Mountain View, California  94043
94  */
95
96
97
98 /*
99  * for unit alignment
100  */
101 static char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
102
103 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
104 {
105     xdr_uint32_t y;
106     int          i;
107     char        *px = (char *)&x;
108     char        *py = (char *)&y;
109
110     for (i = 0; i < 4; i++)
111     {
112         py[i] = px[3-i];
113     }
114
115     return y;
116 }
117
118 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
119 {
120     short s = 0x0F00;
121     if (*((char *)&s) == (char)0x0F)
122     {
123         /* bigendian, do nothing */
124         return x;
125     }
126     else
127     {
128         /* smallendian,swap bytes */
129         return xdr_swapbytes(x);
130     }
131 }
132
133 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
134 {
135     short s = 0x0F00;
136     if (*((char *)&s) == (char)0x0F)
137     {
138         /* bigendian, do nothing */
139         return x;
140     }
141     else
142     {
143         /* smallendian, swap bytes */
144         return xdr_swapbytes(x);
145     }
146 }
147
148
149 /*
150  * Free a data structure using XDR
151  * Not a filter, but a convenient utility nonetheless
152  */
153 void
154 xdr_free (xdrproc_t proc, char *objp)
155 {
156     XDR x;
157
158     x.x_op = XDR_FREE;
159     (*proc) ( &x, objp);
160 }
161
162 /*
163  * XDR nothing
164  */
165 bool_t
166 xdr_void (void)
167 {
168     return TRUE;
169 }
170
171 /*
172  * XDR integers
173  */
174 bool_t
175 xdr_int (XDR *xdrs, int *ip)
176 {
177     xdr_int32_t l;
178
179     switch (xdrs->x_op)
180     {
181         case XDR_ENCODE:
182             l = (xdr_int32_t) (*ip);
183             return xdr_putint32 (xdrs, &l);
184
185         case XDR_DECODE:
186             if (!xdr_getint32 (xdrs, &l))
187             {
188                 return FALSE;
189             }
190             *ip = (int) l;
191
192         case XDR_FREE:
193             return TRUE;
194     }
195     return FALSE;
196 }
197
198
199 /*
200  * XDR unsigned integers
201  */
202 bool_t
203 xdr_u_int (XDR *xdrs, unsigned int *up)
204 {
205     xdr_uint32_t l;
206
207     switch (xdrs->x_op)
208     {
209         case XDR_ENCODE:
210             l = (xdr_uint32_t) (*up);
211             return xdr_putuint32 (xdrs, &l);
212
213         case XDR_DECODE:
214             if (!xdr_getuint32 (xdrs, &l))
215             {
216                 return FALSE;
217             }
218             *up = (unsigned int) l;
219
220         case XDR_FREE:
221             return TRUE;
222     }
223     return FALSE;
224 }
225
226
227
228
229 /*
230  * XDR short integers
231  */
232 bool_t
233 xdr_short (XDR *xdrs, short *sp)
234 {
235     xdr_int32_t l;
236
237     switch (xdrs->x_op)
238     {
239         case XDR_ENCODE:
240             l = (xdr_int32_t) *sp;
241             return xdr_putint32 (xdrs, &l);
242
243         case XDR_DECODE:
244             if (!xdr_getint32 (xdrs, &l))
245             {
246                 return FALSE;
247             }
248             *sp = (short) l;
249             return TRUE;
250
251         case XDR_FREE:
252             return TRUE;
253     }
254     return FALSE;
255 }
256
257
258 /*
259  * XDR unsigned short integers
260  */
261 bool_t
262 xdr_u_short (XDR *xdrs, unsigned short *usp)
263 {
264     xdr_uint32_t l;
265
266     switch (xdrs->x_op)
267     {
268         case XDR_ENCODE:
269             l = (xdr_uint32_t) *usp;
270             return xdr_putuint32 (xdrs, &l);
271
272         case XDR_DECODE:
273             if (!xdr_getuint32 (xdrs, &l))
274             {
275                 return FALSE;
276             }
277             *usp = (unsigned short) l;
278             return TRUE;
279
280         case XDR_FREE:
281             return TRUE;
282     }
283     return FALSE;
284 }
285
286
287 /*
288  * XDR a char
289  */
290 bool_t
291 xdr_char (XDR *xdrs, char *cp)
292 {
293     int i;
294
295     i = (*cp);
296     if (!xdr_int (xdrs, &i))
297     {
298         return FALSE;
299     }
300     *cp = i;
301     return TRUE;
302 }
303
304 /*
305  * XDR an unsigned char
306  */
307 bool_t
308 xdr_u_char (XDR *xdrs, unsigned char *cp)
309 {
310     unsigned int u;
311
312     u = (*cp);
313     if (!xdr_u_int (xdrs, &u))
314     {
315         return FALSE;
316     }
317     *cp = u;
318     return TRUE;
319 }
320
321 /*
322  * XDR booleans
323  */
324 bool_t
325 xdr_bool (XDR *xdrs, int *bp)
326 {
327 #define XDR_FALSE   ((xdr_int32_t) 0)
328 #define XDR_TRUE    ((xdr_int32_t) 1)
329
330     xdr_int32_t lb;
331
332     switch (xdrs->x_op)
333     {
334         case XDR_ENCODE:
335             lb = *bp ? XDR_TRUE : XDR_FALSE;
336             return xdr_putint32 (xdrs, &lb);
337
338         case XDR_DECODE:
339             if (!xdr_getint32 (xdrs, &lb))
340             {
341                 return FALSE;
342             }
343             *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
344             return TRUE;
345
346         case XDR_FREE:
347             return TRUE;
348     }
349     return FALSE;
350 #undef XDR_FALSE
351 #undef XDR_TRUE
352 }
353
354
355
356 /*
357  * XDR opaque data
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.
360  */
361 bool_t
362 xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt)
363 {
364     unsigned int rndup;
365     char         crud[BYTES_PER_XDR_UNIT];
366
367     /*
368      * if no data we are done
369      */
370     if (cnt == 0)
371     {
372         return TRUE;
373     }
374
375     /*
376      * round byte count to full xdr units
377      */
378     rndup = cnt % BYTES_PER_XDR_UNIT;
379     if (rndup > 0)
380     {
381         rndup = BYTES_PER_XDR_UNIT - rndup;
382     }
383
384     switch (xdrs->x_op)
385     {
386         case XDR_DECODE:
387             if (!xdr_getbytes (xdrs, cp, cnt))
388             {
389                 return FALSE;
390             }
391             if (rndup == 0)
392             {
393                 return TRUE;
394             }
395             return xdr_getbytes (xdrs, (char *)crud, rndup);
396
397         case XDR_ENCODE:
398             if (!xdr_putbytes (xdrs, cp, cnt))
399             {
400                 return FALSE;
401             }
402             if (rndup == 0)
403             {
404                 return TRUE;
405             }
406             return xdr_putbytes (xdrs, xdr_zero, rndup);
407
408         case XDR_FREE:
409             return TRUE;
410     }
411     return FALSE;
412 }
413
414
415 /*
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.
422  */
423 bool_t
424 xdr_string (xdrs, cpp, maxsize)
425 XDR *xdrs;
426 char       **cpp;
427 unsigned int maxsize;
428 {
429     char        *sp       = *cpp; /* sp is the actual string pointer */
430     unsigned int size     = 0;
431     unsigned int nodesize = 0;
432
433     /*
434      * first deal with the length since xdr strings are counted-strings
435      */
436     switch (xdrs->x_op)
437     {
438         case XDR_FREE:
439             if (sp == NULL)
440             {
441                 return TRUE; /* already free */
442             }
443         /* fall through... */
444         case XDR_ENCODE:
445             if (sp == NULL)
446             {
447                 return FALSE;
448             }
449             size = strlen (sp);
450             break;
451         case XDR_DECODE:
452             break;
453     }
454
455     if (!xdr_u_int (xdrs, &size))
456     {
457         return FALSE;
458     }
459     if (size > maxsize)
460     {
461         return FALSE;
462     }
463     nodesize = size + 1;
464
465     /*
466      * now deal with the actual bytes
467      */
468     switch (xdrs->x_op)
469     {
470         case XDR_DECODE:
471             if (nodesize == 0)
472             {
473                 return TRUE;
474             }
475             if (sp == NULL)
476             {
477                 *cpp = sp = (char *) malloc (nodesize);
478             }
479             if (sp == NULL)
480             {
481                 (void) fputs ("xdr_string: out of memory\n", stderr);
482                 return FALSE;
483             }
484             sp[size] = 0;
485         /* fall into ... */
486
487         case XDR_ENCODE:
488             return xdr_opaque (xdrs, sp, size);
489
490         case XDR_FREE:
491             free (sp);
492             *cpp = NULL;
493             return TRUE;
494     }
495     return FALSE;
496 }
497
498
499
500 /* Floating-point stuff */
501
502 bool_t
503 xdr_float(xdrs, fp)
504 XDR *xdrs;
505 float *fp;
506 {
507     xdr_int32_t tmp;
508
509     switch (xdrs->x_op)
510     {
511
512         case XDR_ENCODE:
513             tmp = *(xdr_int32_t *)fp;
514             return (xdr_putint32(xdrs, &tmp));
515
516             break;
517
518         case XDR_DECODE:
519             if (xdr_getint32(xdrs, &tmp))
520             {
521                 *(xdr_int32_t *)fp = tmp;
522                 return (TRUE);
523             }
524
525             break;
526
527         case XDR_FREE:
528             return (TRUE);
529     }
530     return (FALSE);
531 }
532
533
534 bool_t
535 xdr_double(xdrs, dp)
536 XDR *xdrs;
537 double *dp;
538 {
539
540     /* Windows and some other systems dont define double-precision
541      * word order in the header files, so unfortunately we have
542      * to calculate it!
543      *
544      * For thread safety, we calculate it every time: locking this would
545      * be more expensive.
546      */
547     /*static int LSW=-1;*/ /* Least significant fp word */
548     int LSW = -1; /* Least significant fp word */
549
550
551     int        *ip;
552     xdr_int32_t tmp[2];
553
554     if (LSW < 0)
555     {
556         double x = 0.987654321; /* Just a number */
557
558         /* Possible representations in IEEE double precision:
559          * (S=small endian, B=big endian)
560          *
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
566          */
567
568         unsigned char ix = *((char *)&x);
569
570         if (ix == 0xdd || ix == 0x3f)
571         {
572             LSW = 1; /* Big endian word order */
573         }
574         else if (ix == 0xb8 || ix == 0x3c)
575         {
576             LSW = 0; /* Small endian word order */
577         }
578         else         /* Catch strange errors */
579         {
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");
584             exit(0);
585         }
586     }
587
588     switch (xdrs->x_op)
589     {
590
591         case XDR_ENCODE:
592             ip     = (int *)dp;
593             tmp[0] = ip[!LSW];
594             tmp[1] = ip[LSW];
595             return (xdr_putint32(xdrs, tmp) &&
596                     xdr_putint32(xdrs, tmp+1));
597
598             break;
599
600         case XDR_DECODE:
601             ip = (int *)dp;
602             if (xdr_getint32(xdrs, tmp+!LSW) &&
603                 xdr_getint32(xdrs, tmp+LSW))
604             {
605                 ip[0] = tmp[0];
606                 ip[1] = tmp[1];
607                 return (TRUE);
608             }
609
610             break;
611
612         case XDR_FREE:
613             return (TRUE);
614     }
615     return (FALSE);
616 }
617
618
619 /* Array routines */
620
621 /*
622  * xdr_vector():
623  *
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
630  */
631 bool_t
632 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
633 XDR *xdrs;
634 char        *basep;
635 unsigned int nelem;
636 unsigned int elemsize;
637 xdrproc_t    xdr_elem;
638 {
639 #define LASTUNSIGNED    ((unsigned int)0-1)
640     unsigned int i;
641     char        *elptr;
642
643     elptr = basep;
644     for (i = 0; i < nelem; i++)
645     {
646         if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
647         {
648             return FALSE;
649         }
650         elptr += elemsize;
651     }
652     return TRUE;
653 #undef LASTUNSIGNED
654 }
655
656
657
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 *);
668
669 /*
670  * Ops vector for stdio type XDR
671  */
672 static const struct xdr_ops xdrstdio_ops =
673 {
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 */
684 };
685
686 /*
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.
690  */
691 void
692 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
693 {
694     xdrs->x_op = op;
695     /* We have to add the const since the `struct xdr_ops' in `struct XDR'
696        is not `const'.  */
697     xdrs->x_ops     = (struct xdr_ops *) &xdrstdio_ops;
698     xdrs->x_private = (char *) file;
699     xdrs->x_handy   = 0;
700     xdrs->x_base    = 0;
701 }
702
703 /*
704  * Destroy a stdio xdr stream.
705  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
706  */
707 static void
708 xdrstdio_destroy (XDR *xdrs)
709 {
710     (void) fflush ((FILE *) xdrs->x_private);
711     /* xx should we close the file ?? */
712 }
713
714
715 static bool_t
716 xdrstdio_getbytes (XDR *xdrs, char *addr, unsigned int len)
717 {
718     if ((len != 0) && (fread (addr, (int) len, 1,
719                               (FILE *) xdrs->x_private) != 1))
720     {
721         return FALSE;
722     }
723     return TRUE;
724 }
725
726 static bool_t
727 xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len)
728 {
729     if ((len != 0) && (fwrite (addr, (int) len, 1,
730                                (FILE *) xdrs->x_private) != 1))
731     {
732         return FALSE;
733     }
734     return TRUE;
735 }
736
737 static unsigned int
738 xdrstdio_getpos (XDR *xdrs)
739 {
740     return (unsigned int) ftell ((FILE *) xdrs->x_private);
741 }
742
743 static bool_t
744 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
745 {
746     return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
747 }
748
749 static xdr_int32_t *
750 xdrstdio_inline (XDR *xdrs, int len)
751 {
752     /*
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.
760      */
761     return NULL;
762 }
763
764 static bool_t
765 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
766 {
767     xdr_int32_t mycopy;
768
769     if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
770     {
771         return FALSE;
772     }
773     *ip = xdr_ntohl (mycopy);
774     return TRUE;
775 }
776
777 static bool_t
778 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
779 {
780     xdr_int32_t mycopy = xdr_htonl (*ip);
781
782     ip = &mycopy;
783     if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
784     {
785         return FALSE;
786     }
787     return TRUE;
788 }
789
790 static bool_t
791 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
792 {
793     xdr_uint32_t mycopy;
794
795     if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
796     {
797         return FALSE;
798     }
799     *ip = xdr_ntohl (mycopy);
800     return TRUE;
801 }
802
803 static bool_t
804 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
805 {
806     xdr_uint32_t mycopy = xdr_htonl (*ip);
807
808     ip = &mycopy;
809     if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
810     {
811         return FALSE;
812     }
813     return TRUE;
814 }
815
816 #else
817 int
818     gmx_system_xdr_empty;
819 #endif /* GMX_SYSTEM_XDR */