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