-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdrfile.c
2620 lines (2314 loc) · 62.8 KB
/
xdrfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
*
* $Id$
*
* Copyright (c) 2009-2014, Erik Lindahl & David van der Spoel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Get HAVE_RPC_XDR_H, F77_FUNC from config.h if available */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#define _FILE_OFFSET_BITS 64
/* get fixed-width types if we are using ANSI C99 */
#ifdef HAVE_STDINT_H
# include <stdint.h>
#elif (defined HAVE_INTTYPES_H)
# include <inttypes.h>
#endif
#ifdef HAVE_RPC_XDR_H
# include <rpc/rpc.h>
# include <rpc/xdr.h>
#endif
#include "xdrfile.h"
/* Default FORTRAN name mangling is: lower case name, append underscore */
#ifndef F77_FUNC
#define F77_FUNC(name,NAME) name ## _
#endif
char *exdr_message[exdrNR] = {
"OK",
"Header",
"String",
"Double",
"Integer",
"Float",
"Unsigned integer",
"Compressed 3D coordinate",
"Closing file",
"Magic number",
"Not enough memory",
"End of file",
"File not found"
};
/*
* Declare our own XDR routines statically if no libraries are present.
* Actual implementation is at the end of this file.
*
* We don't want the low-level XDR implementation as part of the Gromacs
* documentation, so skip it for doxygen too...
*/
#if (!defined HAVE_RPC_XDR_H && !defined DOXYGEN)
enum xdr_op
{
XDR_ENCODE = 0,
XDR_DECODE = 1,
XDR_FREE = 2
};
/* We need integer types that are guaranteed to be 4 bytes wide.
* If ANSI C99 headers were included they are already defined
* as int32_t and uint32_t. Check, and if not define them ourselves.
* Since it is just our workaround for missing ANSI C99 types, avoid adding
* it to the doxygen documentation.
*/
#if !(defined INT32_MAX || defined DOXYGEN)
# if (INT_MAX == 2147483647)
# define int32_t int
# define uint32_t unsigned int
# define INT32_MAX 2147483647
# elif (LONG_MAX == 2147483647)
# define int32_t long
# define uint32_t unsigned long
# define INT32_MAX 2147483647L
# else
# error ERROR: No 32 bit wide integer type found!
# error Use system XDR libraries instead, or update xdrfile.c
# endif
#endif
typedef struct XDR XDR;
struct XDR
{
enum xdr_op x_op;
struct xdr_ops
{
int (*x_getlong) (XDR *__xdrs, int32_t *__lp);
int (*x_putlong) (XDR *__xdrs, int32_t *__lp);
int (*x_getbytes) (XDR *__xdrs, char *__addr, unsigned int __len);
int (*x_putbytes) (XDR *__xdrs, char *__addr, unsigned int __len);
/* two next routines are not 64-bit IO safe - don't use! */
unsigned int (*x_getpostn) (XDR *__xdrs);
int (*x_setpostn) (XDR *__xdrs, unsigned int __pos);
void (*x_destroy) (XDR *__xdrs);
}
*x_ops;
char *x_private;
};
static int xdr_char (XDR *xdrs, char *ip);
static int xdr_u_char (XDR *xdrs, unsigned char *ip);
static int xdr_short (XDR *xdrs, short *ip);
static int xdr_u_short (XDR *xdrs, unsigned short *ip);
static int xdr_int (XDR *xdrs, int *ip);
static int xdr_u_int (XDR *xdrs, unsigned int *ip);
static int xdr_float (XDR *xdrs, float *ip);
static int xdr_double (XDR *xdrs, double *ip);
static int xdr_string (XDR *xdrs, char **ip, unsigned int maxsize);
static int xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt);
static void xdrstdio_create (XDR *xdrs, FILE *fp, enum xdr_op xop);
#define xdr_getpos(xdrs) \
(*(xdrs)->x_ops->x_getpostn)(xdrs)
#define xdr_setpos(xdrs, pos) \
(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
#define xdr_destroy(xdrs) \
do { \
if ((xdrs)->x_ops->x_destroy) \
(*(xdrs)->x_ops->x_destroy)(xdrs); \
} while (0)
#endif /* end of our own XDR declarations */
/** Contents of the abstract XDRFILE data structure.
*
* @internal
*
* This structure is used to provide an XDR file interface that is
* virtual identical to the standard UNIX fopen/fread/fwrite/fclose.
*/
struct XDRFILE
{
FILE * fp; /**< pointer to standard C library file handle */
XDR * xdr; /**< pointer to corresponding XDR handle */
char mode; /**< r=read, w=write, a=append */
int * buf1; /**< Buffer for internal use */
int buf1size; /**< Current allocated length of buf1 */
int * buf2; /**< Buffer for internal use */
int buf2size; /**< Current allocated length of buf2 */
};
/*************************************************************
* Implementation of higher-level routines to read/write *
* portable data based on the XDR standard. These should be *
* called from C - see further down for Fortran77 wrappers. *
*************************************************************/
XDRFILE *
xdrfile_open(const char *path, const char *mode)
{
char newmode[5];
enum xdr_op xdrmode;
XDRFILE *xfp;
/* make sure XDR files are opened in binary mode... */
if(*mode=='w' || *mode=='W')
{
sprintf(newmode,"wb+");
xdrmode=XDR_ENCODE;
} else if(*mode == 'a' || *mode == 'A')
{
sprintf(newmode,"ab+");
xdrmode = XDR_ENCODE;
} else if(*mode == 'r' || *mode == 'R')
{
sprintf(newmode,"rb");
xdrmode = XDR_DECODE;
} else /* cannot determine mode */
return NULL;
if((xfp=(XDRFILE *)malloc(sizeof(XDRFILE)))==NULL)
{
return NULL;
}
if((xfp->fp=fopen(path,newmode))==NULL)
{
free(xfp);
return NULL;
}
if((xfp->xdr=(XDR *)malloc(sizeof(XDR)))==NULL)
{
fclose(xfp->fp);
free(xfp);
return NULL;
}
xfp->mode=*mode;
xdrstdio_create((XDR *)(xfp->xdr),xfp->fp,xdrmode);
xfp->buf1 = xfp->buf2 = NULL;
xfp->buf1size = xfp->buf2size = 0;
return xfp;
}
int
xdrfile_close(XDRFILE *xfp)
{
int ret=exdrCLOSE;
if(xfp)
{
/* flush and destroy XDR stream */
if(xfp->xdr)
xdr_destroy((XDR *)(xfp->xdr));
free(xfp->xdr);
/* close the file */
ret=fclose(xfp->fp);
if(xfp->buf1size)
free(xfp->buf1);
if(xfp->buf2size)
free(xfp->buf2);
free(xfp);
}
return ret; /* return 0 if ok */
}
int
xdrfile_read_int(int *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_int((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_int(int *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_int((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_uint(unsigned int *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_u_int((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_uint(unsigned int *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_u_int((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_char(char *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_char((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_char(char *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_char((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_uchar(unsigned char *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_u_char((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_uchar(unsigned char *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_u_char((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_short(short *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_short((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_short(short *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_short((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_ushort(unsigned short *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_u_short((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_ushort(unsigned short *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_u_short((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_float(float *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_float((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_float(float *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_float((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_double(double *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_double((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_write_double(double *ptr, int ndata, XDRFILE* xfp)
{
int i=0;
/* read write is encoded in the XDR struct */
while(i<ndata && xdr_double((XDR *)(xfp->xdr),ptr+i))
i++;
return i;
}
int
xdrfile_read_string(char *ptr, int maxlen, XDRFILE* xfp)
{
int i;
if(xdr_string((XDR *)(xfp->xdr),&ptr,maxlen)) {
i=0;
while(i<maxlen && ptr[i]!=0)
i++;
if(i==maxlen)
return maxlen;
else
return i+1;
} else
return 0;
}
int
xdrfile_write_string(char *ptr, XDRFILE* xfp)
{
int len=strlen(ptr)+1;
if(xdr_string((XDR *)(xfp->xdr),&ptr,len))
return len;
else
return 0;
}
int
xdrfile_read_opaque(char *ptr, int cnt, XDRFILE* xfp)
{
if(xdr_opaque((XDR *)(xfp->xdr),ptr,cnt))
return cnt;
else
return 0;
}
int
xdrfile_write_opaque(char *ptr, int cnt, XDRFILE* xfp)
{
if(xdr_opaque((XDR *)(xfp->xdr),ptr,cnt))
return cnt;
else
return 0;
}
/* Internal support routines for reading/writing compressed coordinates
* sizeofint - calculate smallest number of bits necessary
* to represent a certain integer.
*/
static int
sizeofint(int size) {
unsigned int num = 1;
int num_of_bits = 0;
while (size >= num && num_of_bits < 32)
{
num_of_bits++;
num <<= 1;
}
return num_of_bits;
}
/*
* sizeofints - calculate 'bitsize' of compressed ints
*
* given a number of small unsigned integers and the maximum value
* return the number of bits needed to read or write them with the
* routines encodeints/decodeints. You need this parameter when
* calling those routines.
* (However, in some cases we can just use the variable 'smallidx'
* which is the exact number of bits, and them we dont need to call
* this routine).
*/
static int
sizeofints(int num_of_ints, unsigned int sizes[])
{
int i, num;
unsigned int num_of_bytes, num_of_bits, bytes[32], bytecnt, tmp;
num_of_bytes = 1;
bytes[0] = 1;
num_of_bits = 0;
for (i=0; i < num_of_ints; i++)
{
tmp = 0;
for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++)
{
tmp = bytes[bytecnt] * sizes[i] + tmp;
bytes[bytecnt] = tmp & 0xff;
tmp >>= 8;
}
while (tmp != 0)
{
bytes[bytecnt++] = tmp & 0xff;
tmp >>= 8;
}
num_of_bytes = bytecnt;
}
num = 1;
num_of_bytes--;
while (bytes[num_of_bytes] >= num)
{
num_of_bits++;
num *= 2;
}
return num_of_bits + num_of_bytes * 8;
}
/*
* encodebits - encode num into buf using the specified number of bits
*
* This routines appends the value of num to the bits already present in
* the array buf. You need to give it the number of bits to use and you had
* better make sure that this number of bits is enough to hold the value.
* Num must also be positive.
*/
static void
encodebits(int buf[], int num_of_bits, int num)
{
unsigned int cnt, lastbyte;
int lastbits;
unsigned char * cbuf;
cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
cnt = (unsigned int) buf[0];
lastbits = buf[1];
lastbyte =(unsigned int) buf[2];
while (num_of_bits >= 8)
{
lastbyte = (lastbyte << 8) | ((num >> (num_of_bits -8)) /* & 0xff*/);
cbuf[cnt++] = lastbyte >> lastbits;
num_of_bits -= 8;
}
if (num_of_bits > 0)
{
lastbyte = (lastbyte << num_of_bits) | num;
lastbits += num_of_bits;
if (lastbits >= 8)
{
lastbits -= 8;
cbuf[cnt++] = lastbyte >> lastbits;
}
}
buf[0] = cnt;
buf[1] = lastbits;
buf[2] = lastbyte;
if (lastbits>0)
{
cbuf[cnt] = lastbyte << (8 - lastbits);
}
}
/*
* encodeints - encode a small set of small integers in compressed format
*
* this routine is used internally by xdr3dfcoord, to encode a set of
* small integers to the buffer for writing to a file.
* Multiplication with fixed (specified maximum) sizes is used to get
* to one big, multibyte integer. Allthough the routine could be
* modified to handle sizes bigger than 16777216, or more than just
* a few integers, this is not done because the gain in compression
* isn't worth the effort. Note that overflowing the multiplication
* or the byte buffer (32 bytes) is unchecked and whould cause bad results.
* THese things are checked in the calling routines, so make sure not
* to remove those checks...
*/
static void
encodeints(int buf[], int num_of_ints, int num_of_bits,
unsigned int sizes[], unsigned int nums[])
{
int i;
unsigned int bytes[32], num_of_bytes, bytecnt, tmp;
tmp = nums[0];
num_of_bytes = 0;
do
{
bytes[num_of_bytes++] = tmp & 0xff;
tmp >>= 8;
} while (tmp != 0);
for (i = 1; i < num_of_ints; i++)
{
if (nums[i] >= sizes[i])
{
fprintf(stderr,"major breakdown in encodeints - num %u doesn't "
"match size %u\n", nums[i], sizes[i]);
abort();
}
/* use one step multiply */
tmp = nums[i];
for (bytecnt = 0; bytecnt < num_of_bytes; bytecnt++)
{
tmp = bytes[bytecnt] * sizes[i] + tmp;
bytes[bytecnt] = tmp & 0xff;
tmp >>= 8;
}
while (tmp != 0)
{
bytes[bytecnt++] = tmp & 0xff;
tmp >>= 8;
}
num_of_bytes = bytecnt;
}
if (num_of_bits >= num_of_bytes * 8)
{
for (i = 0; i < num_of_bytes; i++)
{
encodebits(buf, 8, bytes[i]);
}
encodebits(buf, num_of_bits - num_of_bytes * 8, 0);
}
else
{
for (i = 0; i < num_of_bytes-1; i++)
{
encodebits(buf, 8, bytes[i]);
}
encodebits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
}
}
/*
* decodebits - decode number from buf using specified number of bits
*
* extract the number of bits from the array buf and construct an integer
* from it. Return that value.
*
*/
static int
decodebits(int buf[], int num_of_bits)
{
int cnt, num;
unsigned int lastbits, lastbyte;
unsigned char * cbuf;
int mask = (1 << num_of_bits) -1;
cbuf = ((unsigned char *)buf) + 3 * sizeof(*buf);
cnt = buf[0];
lastbits = (unsigned int) buf[1];
lastbyte = (unsigned int) buf[2];
num = 0;
while (num_of_bits >= 8)
{
lastbyte = ( lastbyte << 8 ) | cbuf[cnt++];
num |= (lastbyte >> lastbits) << (num_of_bits - 8);
num_of_bits -=8;
}
if (num_of_bits > 0)
{
if (lastbits < num_of_bits)
{
lastbits += 8;
lastbyte = (lastbyte << 8) | cbuf[cnt++];
}
lastbits -= num_of_bits;
num |= (lastbyte >> lastbits) & ((1 << num_of_bits) -1);
}
num &= mask;
buf[0] = cnt;
buf[1] = lastbits;
buf[2] = lastbyte;
return num;
}
/*
* decodeints - decode 'small' integers from the buf array
*
* this routine is the inverse from encodeints() and decodes the small integers
* written to buf by calculating the remainder and doing divisions with
* the given sizes[]. You need to specify the total number of bits to be
* used from buf in num_of_bits.
*
*/
static void
decodeints(int buf[], int num_of_ints, int num_of_bits,
unsigned int sizes[], int nums[])
{
int bytes[32];
int i, j, num_of_bytes, p, num;
bytes[1] = bytes[2] = bytes[3] = 0;
num_of_bytes = 0;
while (num_of_bits > 8)
{
bytes[num_of_bytes++] = decodebits(buf, 8);
num_of_bits -= 8;
}
if (num_of_bits > 0)
{
bytes[num_of_bytes++] = decodebits(buf, num_of_bits);
}
for (i = num_of_ints-1; i > 0; i--)
{
num = 0;
for (j = num_of_bytes-1; j >=0; j--)
{
num = (num << 8) | bytes[j];
p = num / sizes[i];
bytes[j] = p;
num = num - p * sizes[i];
}
nums[i] = num;
}
nums[0] = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
}
static const int magicints[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64,
80, 101, 128, 161, 203, 256, 322, 406, 512, 645, 812, 1024, 1290,
1625, 2048, 2580, 3250, 4096, 5060, 6501, 8192, 10321, 13003,
16384, 20642, 26007, 32768, 41285, 52015, 65536,82570, 104031,
131072, 165140, 208063, 262144, 330280, 416127, 524287, 660561,
832255, 1048576, 1321122, 1664510, 2097152, 2642245, 3329021,
4194304, 5284491, 6658042, 8388607, 10568983, 13316085, 16777216
};
#define FIRSTIDX 9
/* note that magicints[FIRSTIDX-1] == 0 */
#define LASTIDX (sizeof(magicints) / sizeof(*magicints))
/* Compressed coordinate routines - modified from the original
* implementation by Frans v. Hoesel to make them threadsafe.
*/
int
xdrfile_decompress_coord_float(float *ptr,
int *size,
float *precision,
XDRFILE* xfp)
{
int minint[3], maxint[3], *lip;
int smallidx, minidx, maxidx;
unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3;
int k, *buf1, *buf2, lsize, flag;
int smallnum, smaller, larger, i, is_smaller, run;
float *lfp, inv_precision;
int tmp, *thiscoord, prevcoord[3];
unsigned int bitsize;
bitsizeint[0] = 0;
bitsizeint[1] = 0;
bitsizeint[2] = 0;
if(xfp==NULL || ptr==NULL)
return -1;
tmp=xdrfile_read_int(&lsize,1,xfp);
if(tmp==0)
return -1; /* return if we could not read size */
if (*size < lsize)
{
fprintf(stderr, "Requested to decompress %d coords, file contains %d\n",
*size, lsize);
return -1;
}
*size = lsize;
size3 = *size * 3;
if(size3>xfp->buf1size)
{
if((xfp->buf1=(int *)malloc(sizeof(int)*size3))==NULL)
{
fprintf(stderr,"Cannot allocate memory for decompressing coordinates.\n");
return -1;
}
xfp->buf1size=size3;
xfp->buf2size=size3*1.2;
if((xfp->buf2=(int *)malloc(sizeof(int)*xfp->buf2size))==NULL)
{
fprintf(stderr,"Cannot allocate memory for decompressing coordinates.\n");
return -1;
}
}
/* Dont bother with compression for three atoms or less */
if(*size<=9)
{
return xdrfile_read_float(ptr,size3,xfp)/3;
/* return number of coords, not floats */
}
/* Compression-time if we got here. Read precision first */
xdrfile_read_float(precision,1,xfp);
/* avoid repeated pointer dereferencing. */
buf1=xfp->buf1;
buf2=xfp->buf2;
/* buf2[0-2] are special and do not contain actual data */
buf2[0] = buf2[1] = buf2[2] = 0;
xdrfile_read_int(minint,3,xfp);
xdrfile_read_int(maxint,3,xfp);
sizeint[0] = maxint[0] - minint[0]+1;
sizeint[1] = maxint[1] - minint[1]+1;
sizeint[2] = maxint[2] - minint[2]+1;
/* check if one of the sizes is to big to be multiplied */
if ((sizeint[0] | sizeint[1] | sizeint[2] ) > 0xffffff)
{
bitsizeint[0] = sizeofint(sizeint[0]);
bitsizeint[1] = sizeofint(sizeint[1]);
bitsizeint[2] = sizeofint(sizeint[2]);
bitsize = 0; /* flag the use of large sizes */
}
else
{
bitsize = sizeofints(3, sizeint);
}
if (xdrfile_read_int(&smallidx,1,xfp) == 0)
return 0; /* not sure what has happened here or why we return... */
tmp=smallidx+8;
maxidx = (LASTIDX<tmp) ? LASTIDX : tmp;
minidx = maxidx - 8; /* often this equal smallidx */
tmp = smallidx-1;
tmp = (FIRSTIDX>tmp) ? FIRSTIDX : tmp;
smaller = magicints[tmp] / 2;
smallnum = magicints[smallidx] / 2;
sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
larger = magicints[maxidx];
/* buf2[0] holds the length in bytes */
if (xdrfile_read_int(buf2,1,xfp) == 0)
return 0;
if (xdrfile_read_opaque((char *)&(buf2[3]),(unsigned int)buf2[0],xfp) == 0)
return 0;
buf2[0] = buf2[1] = buf2[2] = 0;
lfp = ptr;
inv_precision = 1.0 / * precision;
run = 0;
i = 0;
lip = buf1;
while ( i < lsize )
{
thiscoord = (int *)(lip) + i * 3;
if (bitsize == 0)
{
thiscoord[0] = decodebits(buf2, bitsizeint[0]);
thiscoord[1] = decodebits(buf2, bitsizeint[1]);
thiscoord[2] = decodebits(buf2, bitsizeint[2]);
}
else
{
decodeints(buf2, 3, bitsize, sizeint, thiscoord);
}
i++;
thiscoord[0] += minint[0];
thiscoord[1] += minint[1];
thiscoord[2] += minint[2];
prevcoord[0] = thiscoord[0];
prevcoord[1] = thiscoord[1];
prevcoord[2] = thiscoord[2];
flag = decodebits(buf2, 1);
is_smaller = 0;
if (flag == 1)
{
run = decodebits(buf2, 5);
is_smaller = run % 3;
run -= is_smaller;
is_smaller--;
}
if (run > 0)
{
thiscoord += 3;
for (k = 0; k < run; k+=3)
{
decodeints(buf2, 3, smallidx, sizesmall, thiscoord);
i++;
thiscoord[0] += prevcoord[0] - smallnum;
thiscoord[1] += prevcoord[1] - smallnum;
thiscoord[2] += prevcoord[2] - smallnum;
if (k == 0) {
/* interchange first with second atom for better
* compression of water molecules
*/
tmp = thiscoord[0]; thiscoord[0] = prevcoord[0];
prevcoord[0] = tmp;
tmp = thiscoord[1]; thiscoord[1] = prevcoord[1];
prevcoord[1] = tmp;
tmp = thiscoord[2]; thiscoord[2] = prevcoord[2];
prevcoord[2] = tmp;
*lfp++ = prevcoord[0] * inv_precision;
*lfp++ = prevcoord[1] * inv_precision;
*lfp++ = prevcoord[2] * inv_precision;
} else {
prevcoord[0] = thiscoord[0];
prevcoord[1] = thiscoord[1];
prevcoord[2] = thiscoord[2];
}
*lfp++ = thiscoord[0] * inv_precision;
*lfp++ = thiscoord[1] * inv_precision;
*lfp++ = thiscoord[2] * inv_precision;
}
}
else
{
*lfp++ = thiscoord[0] * inv_precision;
*lfp++ = thiscoord[1] * inv_precision;
*lfp++ = thiscoord[2] * inv_precision;
}
smallidx += is_smaller;
if (is_smaller < 0)
{
smallnum = smaller;
if (smallidx > FIRSTIDX)
{
smaller = magicints[smallidx - 1] /2;
}
else
{
smaller = 0;
}
}
else if (is_smaller > 0)
{
smaller = smallnum;
smallnum = magicints[smallidx] / 2;
}
sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
}
return *size;
}
int
xdrfile_compress_coord_float(float *ptr,
int size,
float precision,
XDRFILE* xfp)
{
int minint[3], maxint[3], mindiff, *lip, diff;
int lint1, lint2, lint3, oldlint1, oldlint2, oldlint3, smallidx;
int minidx, maxidx;
unsigned sizeint[3], sizesmall[3], bitsizeint[3], size3, *luip;
int k, *buf1, *buf2;
int smallnum, smaller, larger, i, j, is_small, is_smaller, run, prevrun;
float *lfp, lf;
int tmp, tmpsum, *thiscoord, prevcoord[3];