e66a59970ce22c7d931c4a8f4e5028b482ab50ec
[alexxy/gromacs.git] / src / gmxlib / gmx_system_xdr.c
1 /*
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.2.0
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.
14
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.
19  * 
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.
26  * 
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.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * GROningen Mixture of Alchemy and Childrens' Stories
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifdef GMX_INTERNAL_XDR
40
41
42 #include <stdlib.h>
43 #include <limits.h>
44 #include <string.h>
45
46 #include "gmx_system_xdr.h"
47
48
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. 
53  *
54  * This header file contains Gromacs versions of the definitions for 
55  * Sun External Data Representation (XDR) headers and routines.
56  *
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.
61  * 
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:
64  *
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.
71  *
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.
75  *
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.
79  *
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.
83  *
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.
87  *
88  * Sun Microsystems, Inc.
89  * 2550 Garcia Avenue
90  * Mountain View, California  94043
91  */ 
92
93
94
95 /*
96  * for unit alignment
97  */
98 static char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
99
100 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
101 {
102   xdr_uint32_t y;
103   int i;
104   char *px=(char *)&x;
105   char *py=(char *)&y;
106   
107   for(i=0;i<4;i++)
108     py[i]=px[3-i];
109   
110   return y;
111 }
112
113 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
114 {
115   short s=0x0F00;
116   if( *((char *)&s)==(char)0x0F) {
117     /* bigendian, do nothing */
118     return x;
119   } else {
120     /* smallendian,swap bytes */
121     return xdr_swapbytes(x);
122   }
123 }
124
125 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
126 {
127   short s=0x0F00;
128   if( *((char *)&s)==(char)0x0F) {
129     /* bigendian, do nothing */
130     return x;
131   } else {
132     /* smallendian, swap bytes */
133     return xdr_swapbytes(x);
134   }
135 }
136
137
138 /*
139  * Free a data structure using XDR
140  * Not a filter, but a convenient utility nonetheless
141  */
142 void
143 xdr_free (xdrproc_t proc, char *objp)
144 {
145   XDR x;
146
147   x.x_op = XDR_FREE;
148   (*proc) (&x, objp);
149 }
150
151 /*
152  * XDR nothing
153  */
154 bool_t
155 xdr_void (void)
156 {
157   return TRUE;
158 }
159
160 /*
161  * XDR integers
162  */
163 bool_t
164 xdr_int (XDR *xdrs, int *ip)
165 {
166   xdr_int32_t l;
167
168   switch (xdrs->x_op)
169   {
170     case XDR_ENCODE:
171       l = (xdr_int32_t) (*ip);
172       return xdr_putint32 (xdrs, &l);
173
174     case XDR_DECODE:
175       if (!xdr_getint32 (xdrs, &l))
176           {
177             return FALSE;
178           }
179       *ip = (int) l;
180           
181     case XDR_FREE:
182       return TRUE;
183   }
184   return FALSE;
185 }
186
187
188 /*
189  * XDR unsigned integers
190  */
191 bool_t
192 xdr_u_int (XDR *xdrs, unsigned int *up)
193 {
194   xdr_uint32_t l;
195
196   switch (xdrs->x_op)
197   {
198     case XDR_ENCODE:
199       l = (xdr_uint32_t) (*up);
200       return xdr_putuint32 (xdrs, &l);
201
202     case XDR_DECODE:
203       if (!xdr_getuint32 (xdrs, &l))
204           {
205             return FALSE;
206           }
207       *up = (unsigned int) l;
208           
209     case XDR_FREE:
210       return TRUE;
211   }
212   return FALSE;
213 }
214
215
216
217
218 /*
219  * XDR short integers
220  */
221 bool_t
222 xdr_short (XDR *xdrs, short *sp)
223 {
224   xdr_int32_t l;
225
226   switch (xdrs->x_op)
227     {
228     case XDR_ENCODE:
229       l = (xdr_int32_t) *sp;
230       return xdr_putint32 (xdrs, &l);
231
232     case XDR_DECODE:
233       if (!xdr_getint32 (xdrs, &l))
234         {
235           return FALSE;
236         }
237       *sp = (short) l;
238       return TRUE;
239
240     case XDR_FREE:
241       return TRUE;
242     }
243   return FALSE;
244 }
245
246
247 /*
248  * XDR unsigned short integers
249  */
250 bool_t
251 xdr_u_short (XDR *xdrs, unsigned short *usp)
252 {
253   xdr_uint32_t l;
254
255   switch (xdrs->x_op)
256     {
257     case XDR_ENCODE:
258       l = (xdr_uint32_t) *usp;
259       return xdr_putuint32 (xdrs, &l);
260
261     case XDR_DECODE:
262       if (!xdr_getuint32 (xdrs, &l))
263         {
264           return FALSE;
265         }
266           *usp = (unsigned short) l;
267       return TRUE;
268
269     case XDR_FREE:
270       return TRUE;
271     }
272   return FALSE;
273 }
274
275
276 /*
277  * XDR a char
278  */
279 bool_t
280 xdr_char (XDR *xdrs, char *cp)
281 {
282   int i;
283
284   i = (*cp);
285   if (!xdr_int (xdrs, &i))
286     {
287       return FALSE;
288     }
289   *cp = i;
290   return TRUE;
291 }
292
293 /*
294  * XDR an unsigned char
295  */
296 bool_t
297 xdr_u_char (XDR *xdrs, unsigned char *cp)
298 {
299   unsigned int u;
300
301   u = (*cp);
302   if (!xdr_u_int (xdrs, &u))
303     {
304       return FALSE;
305     }
306   *cp = u;
307   return TRUE;
308 }
309
310 /*
311  * XDR booleans
312  */
313 bool_t
314 xdr_bool (XDR *xdrs, int *bp)
315 {
316 #define XDR_FALSE       ((xdr_int32_t) 0)
317 #define XDR_TRUE        ((xdr_int32_t) 1)
318
319   xdr_int32_t lb;
320
321   switch (xdrs->x_op)
322     {
323     case XDR_ENCODE:
324       lb = *bp ? XDR_TRUE : XDR_FALSE;
325       return xdr_putint32 (xdrs, &lb);
326
327     case XDR_DECODE:
328       if (!xdr_getint32 (xdrs, &lb))
329         {
330           return FALSE;
331         }
332       *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
333       return TRUE;
334
335     case XDR_FREE:
336       return TRUE;
337     }
338   return FALSE;
339 #undef XDR_FALSE
340 #undef XDR_TRUE
341 }
342
343
344
345 /*
346  * XDR opaque data
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.
349  */
350 bool_t
351 xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt)
352 {
353   unsigned int rndup;
354   char crud[BYTES_PER_XDR_UNIT];
355
356   /*
357    * if no data we are done
358    */
359   if (cnt == 0)
360     return TRUE;
361
362   /*
363    * round byte count to full xdr units
364    */
365   rndup = cnt % BYTES_PER_XDR_UNIT;
366   if (rndup > 0)
367     rndup = BYTES_PER_XDR_UNIT - rndup;
368
369   switch (xdrs->x_op)
370     {
371     case XDR_DECODE:
372       if (!xdr_getbytes (xdrs, cp, cnt))
373         {
374           return FALSE;
375         }
376       if (rndup == 0)
377         return TRUE;
378       return xdr_getbytes (xdrs, (char *)crud, rndup);
379
380     case XDR_ENCODE:
381       if (!xdr_putbytes (xdrs, cp, cnt))
382         {
383           return FALSE;
384         }
385       if (rndup == 0)
386         return TRUE;
387       return xdr_putbytes (xdrs, xdr_zero, rndup);
388
389     case XDR_FREE:
390       return TRUE;
391     }
392   return FALSE;
393 }
394
395
396 /*
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.
403  */
404 bool_t
405 xdr_string (xdrs, cpp, maxsize)
406      XDR *xdrs;
407      char **cpp;
408      unsigned int maxsize;
409 {
410   char *sp = *cpp;      /* sp is the actual string pointer */
411   unsigned int size = 0;
412   unsigned int nodesize = 0;
413
414   /*
415    * first deal with the length since xdr strings are counted-strings
416    */
417   switch (xdrs->x_op)
418     {
419     case XDR_FREE:
420       if (sp == NULL)
421         {
422           return TRUE;          /* already free */
423         }
424       /* fall through... */
425     case XDR_ENCODE:
426       if (sp == NULL)
427             return FALSE;
428       size = strlen (sp);
429       break;
430     case XDR_DECODE:
431       break;
432     }
433   
434   if (!xdr_u_int (xdrs, &size))
435     {
436       return FALSE;
437     }
438   if (size > maxsize)
439     {
440       return FALSE;
441     }
442   nodesize = size + 1;
443
444   /*
445    * now deal with the actual bytes
446    */
447   switch (xdrs->x_op)
448     {
449     case XDR_DECODE:
450       if (nodesize == 0)
451         {
452           return TRUE;
453         }
454       if (sp == NULL)
455         *cpp = sp = (char *) malloc (nodesize);
456       if (sp == NULL)
457         {
458           (void) fputs ("xdr_string: out of memory\n", stderr);
459           return FALSE;
460         }
461       sp[size] = 0;
462       /* fall into ... */
463
464     case XDR_ENCODE:
465       return xdr_opaque (xdrs, sp, size);
466
467     case XDR_FREE:
468       free (sp);
469       *cpp = NULL;
470       return TRUE;
471     }
472   return FALSE;
473 }
474
475
476
477 /* Floating-point stuff */
478
479 bool_t
480 xdr_float(xdrs, fp)
481      XDR *xdrs;
482      float *fp;
483 {
484         xdr_int32_t tmp;
485         
486         switch (xdrs->x_op) {
487
488         case XDR_ENCODE:
489                 tmp = *(xdr_int32_t *)fp;
490             return (xdr_putint32(xdrs, &tmp));
491
492                 break;
493
494         case XDR_DECODE:
495                         if (xdr_getint32(xdrs, &tmp)) {
496                                 *(xdr_int32_t *)fp = tmp;
497                                 return (TRUE);
498                         }
499
500                 break;
501
502         case XDR_FREE:
503                 return (TRUE);
504         }
505         return (FALSE);
506 }
507
508
509 bool_t
510 xdr_double(xdrs, dp)
511      XDR *xdrs;
512      double *dp;
513 {
514
515   /* Windows and some other systems dont define double-precision
516    * word order in the header files, so unfortunately we have
517    * to calculate it!
518    *
519    * For thread safety, we calculate it every time: locking this would
520    * be more expensive.
521    */
522   /*static int LSW=-1;*/ /* Least significant fp word */
523   int LSW=-1; /* Least significant fp word */
524   
525
526   int *ip;
527   xdr_int32_t tmp[2];
528
529   if(LSW<0) {
530     double x=0.987654321; /* Just a number */
531
532     /* Possible representations in IEEE double precision: 
533      * (S=small endian, B=big endian)
534      * 
535      * Byte order, Word order, Hex
536      *     S           S       b8 56 0e 3c dd 9a ef 3f    
537      *     B           S       3c 0e 56 b8 3f ef 9a dd
538      *     S           B       dd 9a ef 3f b8 56 0e 3c
539      *     B           B       3f ef 9a dd 3c 0e 56 b8
540      */ 
541     
542     unsigned char ix = *((char *)&x);
543     
544     if(ix==0xdd || ix==0x3f)
545       LSW=1;  /* Big endian word order */
546     else if(ix==0xb8 || ix==0x3c)
547       LSW=0;  /* Small endian word order */
548     else { /* Catch strange errors */
549       printf("Error when detecting floating-point word order.\n"
550              "Do you have a non-IEEE system?\n"
551              "If possible, use the XDR libraries provided with your system,\n"
552              "instead of the Gromacs fallback XDR source.\n");
553       exit(0);
554     }
555   }  
556   
557   switch (xdrs->x_op) {
558     
559   case XDR_ENCODE:
560     ip = (int *)dp;
561     tmp[0] = ip[!LSW];
562     tmp[1] = ip[LSW];
563     return (xdr_putint32(xdrs, tmp) &&
564               xdr_putint32(xdrs, tmp+1));
565  
566     break;
567     
568   case XDR_DECODE:
569     ip = (int *)dp;
570     if (xdr_getint32(xdrs, tmp+!LSW) &&
571           xdr_getint32(xdrs, tmp+LSW)) {
572         ip[0] = tmp[0];
573         ip[1] = tmp[1];
574         return (TRUE);
575     }
576
577     break;
578     
579   case XDR_FREE:
580     return (TRUE);
581   }
582   return (FALSE);
583 }
584
585
586 /* Array routines */
587
588 /*
589  * xdr_vector():
590  *
591  * XDR a fixed length array. Unlike variable-length arrays,
592  * the storage of fixed length arrays is static and unfreeable.
593  * > basep: base of the array
594  * > size: size of the array
595  * > elemsize: size of each element
596  * > xdr_elem: routine to XDR each element
597  */
598 bool_t
599 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
600      XDR *xdrs;
601      char *basep;
602      unsigned int nelem;
603      unsigned int elemsize;
604      xdrproc_t xdr_elem;
605 {
606 #define LASTUNSIGNED    ((unsigned int)0-1)
607   unsigned int i;
608   char *elptr;
609
610   elptr = basep;
611   for (i = 0; i < nelem; i++)
612     {
613       if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
614         {
615           return FALSE;
616         }
617       elptr += elemsize;
618     }
619   return TRUE;
620 #undef LASTUNSIGNED
621 }
622
623
624
625 static bool_t xdrstdio_getbytes (XDR *, char *, unsigned int);
626 static bool_t xdrstdio_putbytes (XDR *, char *, unsigned int);
627 static unsigned int xdrstdio_getpos (XDR *);
628 static bool_t xdrstdio_setpos (XDR *, unsigned int);
629 static xdr_int32_t *xdrstdio_inline (XDR *, int);
630 static void xdrstdio_destroy (XDR *);
631 static bool_t xdrstdio_getint32 (XDR *, xdr_int32_t *);
632 static bool_t xdrstdio_putint32 (XDR *, xdr_int32_t *);
633 static bool_t xdrstdio_getuint32 (XDR *, xdr_uint32_t *);
634 static bool_t xdrstdio_putuint32 (XDR *, xdr_uint32_t *);
635
636 /*
637  * Ops vector for stdio type XDR
638  */
639 static const struct xdr_ops xdrstdio_ops =
640 {
641   xdrstdio_getbytes,            /* deserialize counted bytes */
642   xdrstdio_putbytes,            /* serialize counted bytes */
643   xdrstdio_getpos,              /* get offset in the stream */
644   xdrstdio_setpos,              /* set offset in the stream */
645   xdrstdio_inline,              /* prime stream for inline macros */
646   xdrstdio_destroy,             /* destroy stream */
647   xdrstdio_getint32,    /* deserialize a int */
648   xdrstdio_putint32,    /* serialize a int */
649   xdrstdio_getuint32,   /* deserialize a int */
650   xdrstdio_putuint32            /* serialize a int */
651 };
652
653 /*
654  * Initialize a stdio xdr stream.
655  * Sets the xdr stream handle xdrs for use on the stream file.
656  * Operation flag is set to op.
657  */
658 void
659 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
660 {
661   xdrs->x_op = op;
662   /* We have to add the const since the `struct xdr_ops' in `struct XDR'
663      is not `const'.  */
664   xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
665   xdrs->x_private = (char *) file;
666   xdrs->x_handy = 0;
667   xdrs->x_base = 0;
668 }
669
670 /*
671  * Destroy a stdio xdr stream.
672  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
673  */
674 static void
675 xdrstdio_destroy (XDR *xdrs)
676 {
677   (void) fflush ((FILE *) xdrs->x_private);
678   /* xx should we close the file ?? */
679 }
680
681
682 static bool_t
683 xdrstdio_getbytes (XDR *xdrs, char *addr, unsigned int len)
684 {
685   if ((len != 0) && (fread (addr, (int) len, 1,
686                             (FILE *) xdrs->x_private) != 1))
687     return FALSE;
688   return TRUE;
689 }
690
691 static bool_t
692 xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len)
693 {
694   if ((len != 0) && (fwrite (addr, (int) len, 1,
695                              (FILE *) xdrs->x_private) != 1))
696     return FALSE;
697   return TRUE;
698 }
699
700 static unsigned int
701 xdrstdio_getpos (XDR *xdrs)
702 {
703   return (unsigned int) ftell ((FILE *) xdrs->x_private);
704 }
705
706 static bool_t
707 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
708 {
709   return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
710 }
711
712 static xdr_int32_t *
713 xdrstdio_inline (XDR *xdrs, int len)
714 {
715   /*
716    * Must do some work to implement this: must insure
717    * enough data in the underlying stdio buffer,
718    * that the buffer is aligned so that we can indirect through a
719    * long *, and stuff this pointer in xdrs->x_buf.  Doing
720    * a fread or fwrite to a scratch buffer would defeat
721    * most of the gains to be had here and require storage
722    * management on this buffer, so we don't do this.
723    */
724   return NULL;
725 }
726
727 static bool_t
728 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
729 {
730   xdr_int32_t mycopy;
731
732   if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
733     return FALSE;
734   *ip = xdr_ntohl (mycopy);
735   return TRUE;
736 }
737
738 static bool_t
739 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
740 {
741   xdr_int32_t mycopy = xdr_htonl (*ip);
742
743   ip = &mycopy;
744   if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
745     return FALSE;
746   return TRUE;
747 }
748
749 static bool_t
750 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
751 {
752         xdr_uint32_t mycopy;
753         
754         if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
755                 return FALSE;
756         *ip = xdr_ntohl (mycopy);
757         return TRUE;
758 }
759
760 static bool_t
761 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
762 {
763         xdr_uint32_t mycopy = xdr_htonl (*ip);
764         
765         ip = &mycopy;
766         if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
767                 return FALSE;
768         return TRUE;
769 }
770
771 #else
772 int
773 gmx_system_xdr_empty;
774 #endif /* GMX_SYSTEM_XDR */