#=======================================
+add_subdirectory(source/lib/xdrf)
+
+
if(UNRES_NA_MMCE)
if(UNRES_WITH_MPI)
add_subdirectory(source/unres/src_MD)
add_subdirectory(source/unres/src_MD-M)
add_subdirectory(source/unres/src_CSA)
+ add_subdirectory(source/xdrfpdb/src)
+ add_subdirectory(source/xdrfpdb/src-M)
endif (Fortran_COMPILER_NAME STREQUAL "ifort")
else()
add_subdirectory(source/unres/src_MD)
if(UNRES_WITH_MPI)
add_subdirectory(source/unres/src_MD-M)
add_subdirectory(source/unres/src_CSA)
+ add_subdirectory(source/xdrfpdb/src)
+ add_subdirectory(source/xdrfpdb/src-M)
+
endif(UNRES_WITH_MPI)
add_subdirectory(source/unres/src_MIN)
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+CC = cc
+CFLAGS = -O
+
+M4 = m4
+M4FILE = underscore.m4
+
+libxdrf.a: libxdrf.o ftocstr.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f libxdrf.o ftocstr.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+ rm -f libxdrf.c
+
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
+
+CC = /usr/bin/blrts_xlc
+CPPC = /usr/bin/blrts_xlc
+
+CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
+
+M4 = m4
+M4FILE = RS6K.m4
+
+libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f *.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+# rm -f libxdrf.c
+
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+CC = cc
+CFLAGS = -O
+
+M4 = m4
+M4FILE = underscore.m4
+
+libxdrf.a: libxdrf.o ftocstr.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f libxdrf.o ftocstr.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+ rm -f libxdrf.c
+
--- /dev/null
+divert(-1)
+undefine(`len')
+#
+# do nothing special to FORTRAN function names
+#
+define(`FUNCTION',`$1')
+#
+# FORTRAN character strings are passed as follows:
+# a pointer to the base of the string is passed in the normal
+# argument list, and the length is passed by value as an extra
+# argument, after all of the other arguments.
+#
+define(`ARGS',`($1`'undivert(1))')
+define(`SAVE',`divert(1)$1`'divert(0)')
+define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
+define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
+define(`STRING_LEN',`$1_len')
+define(`STRING_PTR',`$1_ptr')
+divert(0)
+
--- /dev/null
+
+
+int ftocstr(ds, dl, ss, sl)
+ char *ds, *ss; /* dst, src ptrs */
+ int dl; /* dst max len */
+ int sl; /* src len */
+{
+ char *p;
+
+ for (p = ss + sl; --p >= ss && *p == ' '; ) ;
+ sl = p - ss + 1;
+ dl--;
+ ds[0] = 0;
+ if (sl > dl)
+ return 1;
+ while (sl--)
+ (*ds++ = *ss++);
+ *ds = '\0';
+ return 0;
+}
+
+
+int ctofstr(ds, dl, ss)
+ char *ds; /* dest space */
+ int dl; /* max dest length */
+ char *ss; /* src string (0-term) */
+{
+ while (dl && *ss) {
+ *ds++ = *ss++;
+ dl--;
+ }
+ while (dl--)
+ *ds++ = ' ';
+ return 0;
+}
--- /dev/null
+/*____________________________________________________________________________
+ |
+ | libxdrf - portable fortran interface to xdr. some xdr routines
+ | are C routines for compressed coordinates
+ |
+ | version 1.1
+ |
+ | This collection of routines is intended to write and read
+ | data in a portable way to a file, so data written on one type
+ | of machine can be read back on a different type.
+ |
+ | all fortran routines use an integer 'xdrid', which is an id to the
+ | current xdr file, and is set by xdrfopen.
+ | most routines have in integer 'ret' which is the return value.
+ | The value of 'ret' is zero on failure, and most of the time one
+ | on succes.
+ |
+ | There are three routines useful for C users:
+ | xdropen(), xdrclose(), xdr3dfcoord().
+ | The first two replace xdrstdio_create and xdr_destroy, and *must* be
+ | used when you plan to use xdr3dfcoord(). (they are also a bit
+ | easier to interface). For writing data other than compressed coordinates
+ | you should use the standard C xdr routines (see xdr man page)
+ |
+ | xdrfopen(xdrid, filename, mode, ret)
+ | character *(*) filename
+ | character *(*) mode
+ |
+ | this will open the file with the given filename (string)
+ | and the given mode, it returns an id in xdrid, which is
+ | to be used in all other calls to xdrf routines.
+ | mode is 'w' to create, or update an file, for all other
+ | values of mode the file is opened for reading
+ |
+ | you need to call xdrfclose to flush the output and close
+ | the file.
+ | Note that you should not use xdrstdio_create, which comes with the
+ | standard xdr library
+ |
+ | xdrfclose(xdrid, ret)
+ | flush the data to the file, and closes the file;
+ | You should not use xdr_destroy (which comes standard with
+ | the xdr libraries.
+ |
+ | xdrfbool(xdrid, bp, ret)
+ | integer pb
+ |
+ | This filter produces values of either 1 or 0
+ |
+ | xdrfchar(xdrid, cp, ret)
+ | character cp
+ |
+ | filter that translate between characters and their xdr representation
+ | Note that the characters in not compressed and occupies 4 bytes.
+ |
+ | xdrfdouble(xdrid, dp, ret)
+ | double dp
+ |
+ | read/write a double.
+ |
+ | xdrffloat(xdrid, fp, ret)
+ | float fp
+ |
+ | read/write a float.
+ |
+ | xdrfint(xdrid, ip, ret)
+ | integer ip
+ |
+ | read/write integer.
+ |
+ | xdrflong(xdrid, lp, ret)
+ | integer lp
+ |
+ | this routine has a possible portablility problem due to 64 bits longs.
+ |
+ | xdrfshort(xdrid, sp, ret)
+ | integer *2 sp
+ |
+ | xdrfstring(xdrid, sp, maxsize, ret)
+ | character *(*)
+ | integer maxsize
+ |
+ | read/write a string, with maximum length given by maxsize
+ |
+ | xdrfwrapstring(xdris, sp, ret)
+ | character *(*)
+ |
+ | read/write a string (it is the same as xdrfstring accept that it finds
+ | the stringlength itself.
+ |
+ | xdrfvector(xdrid, cp, size, xdrfproc, ret)
+ | character *(*)
+ | integer size
+ | external xdrfproc
+ |
+ | read/write an array pointed to by cp, with number of elements
+ | defined by 'size'. the routine 'xdrfproc' is the name
+ | of one of the above routines to read/write data (like xdrfdouble)
+ | In contrast with the c-version you don't need to specify the
+ | byte size of an element.
+ | xdrfstring is not allowed here (it is in the c version)
+ |
+ | xdrf3dfcoord(xdrid, fp, size, precision, ret)
+ | real (*) fp
+ | real precision
+ | integer size
+ |
+ | this is *NOT* a standard xdr routine. I named it this way, because
+ | it invites people to use the other xdr routines.
+ | It is introduced to store specifically 3d coordinates of molecules
+ | (as found in molecular dynamics) and it writes it in a compressed way.
+ | It starts by multiplying all numbers by precision and
+ | rounding the result to integer. effectively converting
+ | all floating point numbers to fixed point.
+ | it uses an algorithm for compression that is optimized for
+ | molecular data, but could be used for other 3d coordinates
+ | as well. There is subtantial overhead involved, so call this
+ | routine only if you have a large number of coordinates to read/write
+ |
+ | ________________________________________________________________________
+ |
+ | Below are the routines to be used by C programmers. Use the 'normal'
+ | xdr routines to write integers, floats, etc (see man xdr)
+ |
+ | int xdropen(XDR *xdrs, const char *filename, const char *type)
+ | This will open the file with the given filename and the
+ | given mode. You should pass it an allocated XDR struct
+ | in xdrs, to be used in all other calls to xdr routines.
+ | Mode is 'w' to create, or update an file, and for all
+ | other values of mode the file is opened for reading.
+ | You need to call xdrclose to flush the output and close
+ | the file.
+ |
+ | Note that you should not use xdrstdio_create, which
+ | comes with the standard xdr library.
+ |
+ | int xdrclose(XDR *xdrs)
+ | Flush the data to the file, and close the file;
+ | You should not use xdr_destroy (which comes standard
+ | with the xdr libraries).
+ |
+ | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
+ | This is \fInot\fR a standard xdr routine. I named it this
+ | way, because it invites people to use the other xdr
+ | routines.
+ |
+ | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+*/
+
+
+#include <limits.h>
+#include <malloc.h>
+#include <math.h>
+/* #include <rpc/rpc.h>
+#include <rpc/xdr.h> */
+#include "xdr.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "xdrf.h"
+
+int ftocstr(char *, int, char *, int);
+int ctofstr(char *, int, char *);
+
+#define MAXID 20
+static FILE *xdrfiles[MAXID];
+static XDR *xdridptr[MAXID];
+static char xdrmodes[MAXID];
+static unsigned int cnt;
+
+typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
+
+void
+FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
+int *xdrid, *ret;
+int *pb;
+{
+ *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
+int *xdrid, *ret;
+char *cp;
+{
+ *ret = xdr_char(xdridptr[*xdrid], cp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
+int *xdrid, *ret;
+double *dp;
+{
+ *ret = xdr_double(xdridptr[*xdrid], dp);
+ cnt += sizeof(double);
+}
+
+void
+FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
+int *xdrid, *ret;
+float *fp;
+{
+ *ret = xdr_float(xdridptr[*xdrid], fp);
+ cnt += sizeof(float);
+}
+
+void
+FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
+int *xdrid, *ret;
+int *ip;
+{
+ *ret = xdr_int(xdridptr[*xdrid], ip);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
+int *xdrid, *ret;
+long *lp;
+{
+ *ret = xdr_long(xdridptr[*xdrid], lp);
+ cnt += sizeof(long);
+}
+
+void
+FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
+int *xdrid, *ret;
+short *sp;
+{
+ *ret = xdr_short(xdridptr[*xdrid], sp);
+ cnt += sizeof(sp);
+}
+
+void
+FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
+int *xdrid, *ret;
+char *ucp;
+{
+ *ret = xdr_u_char(xdridptr[*xdrid], ucp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
+int *xdrid, *ret;
+unsigned long *ulp;
+{
+ *ret = xdr_u_long(xdridptr[*xdrid], ulp);
+ cnt += sizeof(unsigned long);
+}
+
+void
+FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
+int *xdrid, *ret;
+unsigned short *usp;
+{
+ *ret = xdr_u_short(xdridptr[*xdrid], usp);
+ cnt += sizeof(unsigned short);
+}
+
+void
+FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
+int *xdrid, *ret;
+float *fp;
+int *size;
+float *precision;
+{
+ *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
+}
+
+void
+FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+int *maxsize;
+{
+ char *tsp;
+
+ tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += *maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+{
+ char *tsp;
+ int maxsize;
+ maxsize = (STRING_LEN(sp)) + 1;
+ tsp = (char*) malloc(maxsize * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
+int *xdrid, *ret;
+caddr_t *cp;
+int *ccnt;
+{
+ *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
+ cnt += *ccnt;
+}
+
+void
+FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
+int *xdrid, *ret;
+int *pos;
+{
+ *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
+}
+
+void
+FUNCTION(xdrf) ARGS(`xdrid, pos')
+int *xdrid, *pos;
+{
+ *pos = xdr_getpos(xdridptr[*xdrid]);
+}
+
+void
+FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
+int *xdrid, *ret;
+char *cp;
+int *size;
+FUNCTION(xdrfproc) elproc;
+{
+ int lcnt;
+ cnt = 0;
+ for (lcnt = 0; lcnt < *size; lcnt++) {
+ elproc(xdrid, (cp+cnt) , ret);
+ }
+}
+
+
+void
+FUNCTION(xdrfclose) ARGS(`xdrid, ret')
+int *xdrid;
+int *ret;
+{
+ *ret = xdrclose(xdridptr[*xdrid]);
+ cnt = 0;
+}
+
+void
+FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
+int *xdrid;
+STRING_ARG_DECL(fp);
+STRING_ARG_DECL(mode);
+int *ret;
+{
+ char fname[512];
+ char fmode[3];
+
+ if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
+ *ret = 0;
+ }
+ if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
+ STRING_LEN(mode))) {
+ *ret = 0;
+ }
+
+ *xdrid = xdropen(NULL, fname, fmode);
+ if (*xdrid == 0)
+ *ret = 0;
+ else
+ *ret = 1;
+}
+
+/*___________________________________________________________________________
+ |
+ | what follows are the C routines for opening, closing xdr streams
+ | and the routine to read/write compressed coordinates together
+ | with some routines to assist in this task (those are marked
+ | static and cannot be called from user programs)
+*/
+#define MAXABS INT_MAX-2
+
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x):(y))
+#endif
+#ifndef MAX
+#define MAX(x,y) ((x) > (y) ? (x):(y))
+#endif
+#ifndef SQR
+#define SQR(x) ((x)*(x))
+#endif
+static 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))
+
+
+/*__________________________________________________________________________
+ |
+ | xdropen - open xdr file
+ |
+ | This versions differs from xdrstdio_create, because I need to know
+ | the state of the file (read or write) so I can use xdr3dfcoord
+ | in eigther read or write mode, and the file descriptor
+ | so I can close the file (something xdr_destroy doesn't do).
+ |
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type) {
+ static int init_done = 0;
+ enum xdr_op lmode;
+ const char *type1;
+ int xdrid;
+
+ if (init_done == 0) {
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ xdridptr[xdrid] = NULL;
+ }
+ init_done = 1;
+ }
+ xdrid = 1;
+ while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
+ xdrid++;
+ }
+ if (xdrid == MAXID) {
+ return 0;
+ }
+ if (*type == 'w' || *type == 'W') {
+ type = "w+";
+ type1 = "w+";
+ lmode = XDR_ENCODE;
+ } else if (*type == 'a' || *type == 'A') {
+ type = "w+";
+ type1 = "a+";
+ lmode = XDR_ENCODE;
+ } else {
+ type = "r";
+ type1 = "r";
+ lmode = XDR_DECODE;
+ }
+ xdrfiles[xdrid] = fopen(filename, type1);
+ if (xdrfiles[xdrid] == NULL) {
+ xdrs = NULL;
+ return 0;
+ }
+ xdrmodes[xdrid] = *type;
+ /* next test isn't usefull in the case of C language
+ * but is used for the Fortran interface
+ * (C users are expected to pass the address of an already allocated
+ * XDR staructure)
+ */
+ if (xdrs == NULL) {
+ xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
+ xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
+ } else {
+ xdridptr[xdrid] = xdrs;
+ xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
+ }
+ return xdrid;
+}
+
+/*_________________________________________________________________________
+ |
+ | xdrclose - close a xdr file
+ |
+ | This will flush the xdr buffers, and destroy the xdr stream.
+ | It also closes the associated file descriptor (this is *not*
+ | done by xdr_destroy).
+ |
+*/
+
+int xdrclose(XDR *xdrs) {
+ int xdrid;
+
+ if (xdrs == NULL) {
+ fprintf(stderr, "xdrclose: passed a NULL pointer\n");
+ exit(1);
+ }
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ if (xdridptr[xdrid] == xdrs) {
+
+ xdr_destroy(xdrs);
+ fclose(xdrfiles[xdrid]);
+ xdridptr[xdrid] = NULL;
+ return 1;
+ }
+ }
+ fprintf(stderr, "xdrclose: no such open xdr file\n");
+ exit(1);
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendbits - 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
+ | better make sure that this number of bits is enough to hold the value
+ | Also num must be positive.
+ |
+*/
+
+static void sendbits(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);
+ }
+}
+
+/*_________________________________________________________________________
+ |
+ | sizeofint - calculate bitsize of an integer
+ |
+ | return the number of bits needed to store an integer with given max size
+ |
+*/
+
+static int sizeofint(const 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 the number of small unsigned integers and the maximum value
+ | return the number of bits needed to read or write them with the
+ | routines receiveints and sendints. You need this parameter when
+ | calling these routines. Note that for many calls I can use
+ | the variable 'smallidx' which is exactly the number of bits, and
+ | So I don't need to call 'sizeofints for those calls.
+*/
+
+static int sizeofints( const 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;
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendints - send a small set of small integers in compressed format
+ |
+ | this routine is used internally by xdr3dfcoord, to send a set of
+ | small integers to the buffer.
+ | 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 causes bad results.
+ |
+ */
+
+static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
+ "match size %d\n", nums[i], sizes[i]);
+ exit(1);
+ }
+ /* 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++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
+ } else {
+ for (i = 0; i < num_of_bytes-1; i++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
+ }
+}
+
+
+/*___________________________________________________________________________
+ |
+ | receivebits - 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 receivebits(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;
+}
+
+/*____________________________________________________________________________
+ |
+ | receiveints - decode 'small' integers from the buf array
+ |
+ | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
+ num_of_bits -= 8;
+ }
+ if (num_of_bits > 0) {
+ bytes[num_of_bytes++] = receivebits(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);
+}
+
+/*____________________________________________________________________________
+ |
+ | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
+ |
+ | this routine reads or writes (depending on how you opened the file with
+ | xdropen() ) a large number of 3d coordinates (stored in *fp).
+ | The number of coordinates triplets to write is given by *size. On
+ | read this number may be zero, in which case it reads as many as were written
+ | or it may specify the number if triplets to read (which should match the
+ | number written).
+ | Compression is achieved by first converting all floating numbers to integer
+ | using multiplication by *precision and rounding to the nearest integer.
+ | Then the minimum and maximum value are calculated to determine the range.
+ | The limited range of integers so found, is used to compress the coordinates.
+ | In addition the differences between succesive coordinates is calculated.
+ | If the difference happens to be 'small' then only the difference is saved,
+ | compressing the data even more. The notion of 'small' is changed dynamically
+ | and is enlarged or reduced whenever needed or possible.
+ | Extra compression is achieved in the case of GROMOS and coordinates of
+ | water molecules. GROMOS first writes out the Oxygen position, followed by
+ | the two hydrogens. In order to make the differences smaller (and thereby
+ | compression the data better) the order is changed into first one hydrogen
+ | then the oxygen, followed by the other hydrogen. This is rather special, but
+ | it shouldn't harm in the general case.
+ |
+ */
+
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
+
+
+ static int *ip = NULL;
+ static int oldsize;
+ static int *buf;
+
+ 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 flag, k;
+ int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
+ float *lfp, lf;
+ int tmp, *thiscoord, prevcoord[3];
+ unsigned int tmpcoord[30];
+
+ int bufsize, xdrid, lsize;
+ unsigned int bitsize;
+ float inv_precision;
+ int errval = 1;
+
+ /* find out if xdrs is opened for reading or for writing */
+ xdrid = 0;
+ while (xdridptr[xdrid] != xdrs) {
+ xdrid++;
+ if (xdrid >= MAXID) {
+ fprintf(stderr, "xdr error. no open xdr stream\n");
+ exit (1);
+ }
+ }
+ if (xdrmodes[xdrid] == 'w') {
+
+ /* xdrs is open for writing */
+
+ if (xdr_int(xdrs, size) == 0)
+ return 0;
+ size3 = *size * 3;
+ /* when the number of coordinates is small, don't try to compress; just
+ * write them as floats using xdr_vector
+ */
+ if (*size <= 9 ) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ /* buf[0-2] are special and do not contain actual data */
+ buf[0] = buf[1] = buf[2] = 0;
+ minint[0] = minint[1] = minint[2] = INT_MAX;
+ maxint[0] = maxint[1] = maxint[2] = INT_MIN;
+ prevrun = -1;
+ lfp = fp;
+ lip = ip;
+ mindiff = INT_MAX;
+ oldlint1 = oldlint2 = oldlint3 = 0;
+ while(lfp < fp + size3 ) {
+ /* find nearest integer */
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint1 = lf;
+ if (lint1 < minint[0]) minint[0] = lint1;
+ if (lint1 > maxint[0]) maxint[0] = lint1;
+ *lip++ = lint1;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint2 = lf;
+ if (lint2 < minint[1]) minint[1] = lint2;
+ if (lint2 > maxint[1]) maxint[1] = lint2;
+ *lip++ = lint2;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint3 = lf;
+ if (lint3 < minint[2]) minint[2] = lint3;
+ if (lint3 > maxint[2]) maxint[2] = lint3;
+ *lip++ = lint3;
+ lfp++;
+ diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
+ if (diff < mindiff && lfp > fp + 3)
+ mindiff = diff;
+ oldlint1 = lint1;
+ oldlint2 = lint2;
+ oldlint3 = lint3;
+ }
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
+ (float)maxint[1] - (float)minint[1] >= MAXABS ||
+ (float)maxint[2] - (float)minint[2] >= MAXABS) {
+ /* turning value in unsigned by subtracting minint
+ * would cause overflow
+ */
+ errval = 0;
+ }
+ 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);
+ }
+ lip = ip;
+ luip = (unsigned int *) ip;
+ smallidx = FIRSTIDX;
+ while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
+ smallidx++;
+ }
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ larger = magicints[maxidx] / 2;
+ i = 0;
+ while (i < *size) {
+ is_small = 0;
+ thiscoord = (int *)(luip) + i * 3;
+ if (smallidx < maxidx && i >= 1 &&
+ abs(thiscoord[0] - prevcoord[0]) < larger &&
+ abs(thiscoord[1] - prevcoord[1]) < larger &&
+ abs(thiscoord[2] - prevcoord[2]) < larger) {
+ is_smaller = 1;
+ } else if (smallidx > minidx) {
+ is_smaller = -1;
+ } else {
+ is_smaller = 0;
+ }
+ if (i + 1 < *size) {
+ if (abs(thiscoord[0] - thiscoord[3]) < small &&
+ abs(thiscoord[1] - thiscoord[4]) < small &&
+ abs(thiscoord[2] - thiscoord[5]) < small) {
+ /* interchange first with second atom for better
+ * compression of water molecules
+ */
+ tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
+ thiscoord[3] = tmp;
+ tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
+ thiscoord[4] = tmp;
+ tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
+ thiscoord[5] = tmp;
+ is_small = 1;
+ }
+
+ }
+ tmpcoord[0] = thiscoord[0] - minint[0];
+ tmpcoord[1] = thiscoord[1] - minint[1];
+ tmpcoord[2] = thiscoord[2] - minint[2];
+ if (bitsize == 0) {
+ sendbits(buf, bitsizeint[0], tmpcoord[0]);
+ sendbits(buf, bitsizeint[1], tmpcoord[1]);
+ sendbits(buf, bitsizeint[2], tmpcoord[2]);
+ } else {
+ sendints(buf, 3, bitsize, sizeint, tmpcoord);
+ }
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+ thiscoord = thiscoord + 3;
+ i++;
+
+ run = 0;
+ if (is_small == 0 && is_smaller == -1)
+ is_smaller = 0;
+ while (is_small && run < 8*3) {
+ if (is_smaller == -1 && (
+ SQR(thiscoord[0] - prevcoord[0]) +
+ SQR(thiscoord[1] - prevcoord[1]) +
+ SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
+ is_smaller = 0;
+ }
+
+ tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
+ tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
+ tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
+
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+
+ i++;
+ thiscoord = thiscoord + 3;
+ is_small = 0;
+ if (i < *size &&
+ abs(thiscoord[0] - prevcoord[0]) < small &&
+ abs(thiscoord[1] - prevcoord[1]) < small &&
+ abs(thiscoord[2] - prevcoord[2]) < small) {
+ is_small = 1;
+ }
+ }
+ if (run != prevrun || is_smaller != 0) {
+ prevrun = run;
+ sendbits(buf, 1, 1); /* flag the change in run-length */
+ sendbits(buf, 5, run+is_smaller+1);
+ } else {
+ sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
+ }
+ for (k=0; k < run; k+=3) {
+ sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
+ }
+ if (is_smaller != 0) {
+ smallidx += is_smaller;
+ if (is_smaller < 0) {
+ small = smaller;
+ smaller = magicints[smallidx-1] / 2;
+ } else {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ }
+ }
+ if (buf[1] != 0) buf[0]++;;
+ xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
+ return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
+ } else {
+
+ /* xdrs is open for reading */
+
+ if (xdr_int(xdrs, &lsize) == 0)
+ return 0;
+ if (*size != 0 && lsize != *size) {
+ fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
+ "%d arg vs %d in file", *size, lsize);
+ }
+ *size = lsize;
+ size3 = *size * 3;
+ if (*size <= 9) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ buf[0] = buf[1] = buf[2] = 0;
+
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ 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);
+ }
+
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ larger = magicints[maxidx];
+
+ /* buf[0] holds the length in bytes */
+
+ if (xdr_int(xdrs, &(buf[0])) == 0)
+ return 0;
+ if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
+ return 0;
+ buf[0] = buf[1] = buf[2] = 0;
+
+ lfp = fp;
+ inv_precision = 1.0 / * precision;
+ run = 0;
+ i = 0;
+ lip = ip;
+ while ( i < lsize ) {
+ thiscoord = (int *)(lip) + i * 3;
+
+ if (bitsize == 0) {
+ thiscoord[0] = receivebits(buf, bitsizeint[0]);
+ thiscoord[1] = receivebits(buf, bitsizeint[1]);
+ thiscoord[2] = receivebits(buf, bitsizeint[2]);
+ } else {
+ receiveints(buf, 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 = receivebits(buf, 1);
+ is_smaller = 0;
+ if (flag == 1) {
+ run = receivebits(buf, 5);
+ is_smaller = run % 3;
+ run -= is_smaller;
+ is_smaller--;
+ }
+ if (run > 0) {
+ thiscoord += 3;
+ for (k = 0; k < run; k+=3) {
+ receiveints(buf, 3, smallidx, sizesmall, thiscoord);
+ i++;
+ thiscoord[0] += prevcoord[0] - small;
+ thiscoord[1] += prevcoord[1] - small;
+ thiscoord[2] += prevcoord[2] - small;
+ 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) {
+ small = smaller;
+ if (smallidx > FIRSTIDX) {
+ smaller = magicints[smallidx - 1] /2;
+ } else {
+ smaller = 0;
+ }
+ } else if (is_smaller > 0) {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ }
+ }
+ return 1;
+}
+
+
+
--- /dev/null
+/*____________________________________________________________________________
+ |
+ | libxdrf - portable fortran interface to xdr. some xdr routines
+ | are C routines for compressed coordinates
+ |
+ | version 1.1
+ |
+ | This collection of routines is intended to write and read
+ | data in a portable way to a file, so data written on one type
+ | of machine can be read back on a different type.
+ |
+ | all fortran routines use an integer 'xdrid', which is an id to the
+ | current xdr file, and is set by xdrfopen.
+ | most routines have in integer 'ret' which is the return value.
+ | The value of 'ret' is zero on failure, and most of the time one
+ | on succes.
+ |
+ | There are three routines useful for C users:
+ | xdropen(), xdrclose(), xdr3dfcoord().
+ | The first two replace xdrstdio_create and xdr_destroy, and *must* be
+ | used when you plan to use xdr3dfcoord(). (they are also a bit
+ | easier to interface). For writing data other than compressed coordinates
+ | you should use the standard C xdr routines (see xdr man page)
+ |
+ | xdrfopen(xdrid, filename, mode, ret)
+ | character *(*) filename
+ | character *(*) mode
+ |
+ | this will open the file with the given filename (string)
+ | and the given mode, it returns an id in xdrid, which is
+ | to be used in all other calls to xdrf routines.
+ | mode is 'w' to create, or update an file, for all other
+ | values of mode the file is opened for reading
+ |
+ | you need to call xdrfclose to flush the output and close
+ | the file.
+ | Note that you should not use xdrstdio_create, which comes with the
+ | standard xdr library
+ |
+ | xdrfclose(xdrid, ret)
+ | flush the data to the file, and closes the file;
+ | You should not use xdr_destroy (which comes standard with
+ | the xdr libraries.
+ |
+ | xdrfbool(xdrid, bp, ret)
+ | integer pb
+ |
+ | This filter produces values of either 1 or 0
+ |
+ | xdrfchar(xdrid, cp, ret)
+ | character cp
+ |
+ | filter that translate between characters and their xdr representation
+ | Note that the characters in not compressed and occupies 4 bytes.
+ |
+ | xdrfdouble(xdrid, dp, ret)
+ | double dp
+ |
+ | read/write a double.
+ |
+ | xdrffloat(xdrid, fp, ret)
+ | float fp
+ |
+ | read/write a float.
+ |
+ | xdrfint(xdrid, ip, ret)
+ | integer ip
+ |
+ | read/write integer.
+ |
+ | xdrflong(xdrid, lp, ret)
+ | integer lp
+ |
+ | this routine has a possible portablility problem due to 64 bits longs.
+ |
+ | xdrfshort(xdrid, sp, ret)
+ | integer *2 sp
+ |
+ | xdrfstring(xdrid, sp, maxsize, ret)
+ | character *(*)
+ | integer maxsize
+ |
+ | read/write a string, with maximum length given by maxsize
+ |
+ | xdrfwrapstring(xdris, sp, ret)
+ | character *(*)
+ |
+ | read/write a string (it is the same as xdrfstring accept that it finds
+ | the stringlength itself.
+ |
+ | xdrfvector(xdrid, cp, size, xdrfproc, ret)
+ | character *(*)
+ | integer size
+ | external xdrfproc
+ |
+ | read/write an array pointed to by cp, with number of elements
+ | defined by 'size'. the routine 'xdrfproc' is the name
+ | of one of the above routines to read/write data (like xdrfdouble)
+ | In contrast with the c-version you don't need to specify the
+ | byte size of an element.
+ | xdrfstring is not allowed here (it is in the c version)
+ |
+ | xdrf3dfcoord(xdrid, fp, size, precision, ret)
+ | real (*) fp
+ | real precision
+ | integer size
+ |
+ | this is *NOT* a standard xdr routine. I named it this way, because
+ | it invites people to use the other xdr routines.
+ | It is introduced to store specifically 3d coordinates of molecules
+ | (as found in molecular dynamics) and it writes it in a compressed way.
+ | It starts by multiplying all numbers by precision and
+ | rounding the result to integer. effectively converting
+ | all floating point numbers to fixed point.
+ | it uses an algorithm for compression that is optimized for
+ | molecular data, but could be used for other 3d coordinates
+ | as well. There is subtantial overhead involved, so call this
+ | routine only if you have a large number of coordinates to read/write
+ |
+ | ________________________________________________________________________
+ |
+ | Below are the routines to be used by C programmers. Use the 'normal'
+ | xdr routines to write integers, floats, etc (see man xdr)
+ |
+ | int xdropen(XDR *xdrs, const char *filename, const char *type)
+ | This will open the file with the given filename and the
+ | given mode. You should pass it an allocated XDR struct
+ | in xdrs, to be used in all other calls to xdr routines.
+ | Mode is 'w' to create, or update an file, and for all
+ | other values of mode the file is opened for reading.
+ | You need to call xdrclose to flush the output and close
+ | the file.
+ |
+ | Note that you should not use xdrstdio_create, which
+ | comes with the standard xdr library.
+ |
+ | int xdrclose(XDR *xdrs)
+ | Flush the data to the file, and close the file;
+ | You should not use xdr_destroy (which comes standard
+ | with the xdr libraries).
+ |
+ | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
+ | This is \fInot\fR a standard xdr routine. I named it this
+ | way, because it invites people to use the other xdr
+ | routines.
+ |
+ | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+*/
+
+
+#include <limits.h>
+#include <malloc.h>
+#include <math.h>
+#include <rpc/rpc.h>
+#include <rpc/xdr.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "xdrf.h"
+
+int ftocstr(char *, int, char *, int);
+int ctofstr(char *, int, char *);
+
+#define MAXID 20
+static FILE *xdrfiles[MAXID];
+static XDR *xdridptr[MAXID];
+static char xdrmodes[MAXID];
+static unsigned int cnt;
+
+typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
+
+void
+FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
+int *xdrid, *ret;
+int *pb;
+{
+ *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
+int *xdrid, *ret;
+char *cp;
+{
+ *ret = xdr_char(xdridptr[*xdrid], cp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
+int *xdrid, *ret;
+double *dp;
+{
+ *ret = xdr_double(xdridptr[*xdrid], dp);
+ cnt += sizeof(double);
+}
+
+void
+FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
+int *xdrid, *ret;
+float *fp;
+{
+ *ret = xdr_float(xdridptr[*xdrid], fp);
+ cnt += sizeof(float);
+}
+
+void
+FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
+int *xdrid, *ret;
+int *ip;
+{
+ *ret = xdr_int(xdridptr[*xdrid], ip);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
+int *xdrid, *ret;
+long *lp;
+{
+ *ret = xdr_long(xdridptr[*xdrid], lp);
+ cnt += sizeof(long);
+}
+
+void
+FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
+int *xdrid, *ret;
+short *sp;
+{
+ *ret = xdr_short(xdridptr[*xdrid], sp);
+ cnt += sizeof(sp);
+}
+
+void
+FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
+int *xdrid, *ret;
+char *ucp;
+{
+ *ret = xdr_u_char(xdridptr[*xdrid], ucp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
+int *xdrid, *ret;
+unsigned long *ulp;
+{
+ *ret = xdr_u_long(xdridptr[*xdrid], ulp);
+ cnt += sizeof(unsigned long);
+}
+
+void
+FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
+int *xdrid, *ret;
+unsigned short *usp;
+{
+ *ret = xdr_u_short(xdridptr[*xdrid], usp);
+ cnt += sizeof(unsigned short);
+}
+
+void
+FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
+int *xdrid, *ret;
+float *fp;
+int *size;
+float *precision;
+{
+ *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
+}
+
+void
+FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+int *maxsize;
+{
+ char *tsp;
+
+ tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += *maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+{
+ char *tsp;
+ int maxsize;
+ maxsize = (STRING_LEN(sp)) + 1;
+ tsp = (char*) malloc(maxsize * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
+int *xdrid, *ret;
+caddr_t *cp;
+int *ccnt;
+{
+ *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
+ cnt += *ccnt;
+}
+
+void
+FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
+int *xdrid, *ret;
+int *pos;
+{
+ *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
+}
+
+void
+FUNCTION(xdrf) ARGS(`xdrid, pos')
+int *xdrid, *pos;
+{
+ *pos = xdr_getpos(xdridptr[*xdrid]);
+}
+
+void
+FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
+int *xdrid, *ret;
+char *cp;
+int *size;
+FUNCTION(xdrfproc) elproc;
+{
+ int lcnt;
+ cnt = 0;
+ for (lcnt = 0; lcnt < *size; lcnt++) {
+ elproc(xdrid, (cp+cnt) , ret);
+ }
+}
+
+
+void
+FUNCTION(xdrfclose) ARGS(`xdrid, ret')
+int *xdrid;
+int *ret;
+{
+ *ret = xdrclose(xdridptr[*xdrid]);
+ cnt = 0;
+}
+
+void
+FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
+int *xdrid;
+STRING_ARG_DECL(fp);
+STRING_ARG_DECL(mode);
+int *ret;
+{
+ char fname[512];
+ char fmode[3];
+
+ if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
+ *ret = 0;
+ }
+ if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
+ STRING_LEN(mode))) {
+ *ret = 0;
+ }
+
+ *xdrid = xdropen(NULL, fname, fmode);
+ if (*xdrid == 0)
+ *ret = 0;
+ else
+ *ret = 1;
+}
+
+/*___________________________________________________________________________
+ |
+ | what follows are the C routines for opening, closing xdr streams
+ | and the routine to read/write compressed coordinates together
+ | with some routines to assist in this task (those are marked
+ | static and cannot be called from user programs)
+*/
+#define MAXABS INT_MAX-2
+
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x):(y))
+#endif
+#ifndef MAX
+#define MAX(x,y) ((x) > (y) ? (x):(y))
+#endif
+#ifndef SQR
+#define SQR(x) ((x)*(x))
+#endif
+static 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))
+
+
+/*__________________________________________________________________________
+ |
+ | xdropen - open xdr file
+ |
+ | This versions differs from xdrstdio_create, because I need to know
+ | the state of the file (read or write) so I can use xdr3dfcoord
+ | in eigther read or write mode, and the file descriptor
+ | so I can close the file (something xdr_destroy doesn't do).
+ |
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type) {
+ static int init_done = 0;
+ enum xdr_op lmode;
+ int xdrid;
+
+ if (init_done == 0) {
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ xdridptr[xdrid] = NULL;
+ }
+ init_done = 1;
+ }
+ xdrid = 1;
+ while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
+ xdrid++;
+ }
+ if (xdrid == MAXID) {
+ return 0;
+ }
+ if (*type == 'w' || *type == 'W') {
+ type = "w+";
+ lmode = XDR_ENCODE;
+ } else {
+ type = "r";
+ lmode = XDR_DECODE;
+ }
+ xdrfiles[xdrid] = fopen(filename, type);
+ if (xdrfiles[xdrid] == NULL) {
+ xdrs = NULL;
+ return 0;
+ }
+ xdrmodes[xdrid] = *type;
+ /* next test isn't usefull in the case of C language
+ * but is used for the Fortran interface
+ * (C users are expected to pass the address of an already allocated
+ * XDR staructure)
+ */
+ if (xdrs == NULL) {
+ xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
+ xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
+ } else {
+ xdridptr[xdrid] = xdrs;
+ xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
+ }
+ return xdrid;
+}
+
+/*_________________________________________________________________________
+ |
+ | xdrclose - close a xdr file
+ |
+ | This will flush the xdr buffers, and destroy the xdr stream.
+ | It also closes the associated file descriptor (this is *not*
+ | done by xdr_destroy).
+ |
+*/
+
+int xdrclose(XDR *xdrs) {
+ int xdrid;
+
+ if (xdrs == NULL) {
+ fprintf(stderr, "xdrclose: passed a NULL pointer\n");
+ exit(1);
+ }
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ if (xdridptr[xdrid] == xdrs) {
+
+ xdr_destroy(xdrs);
+ fclose(xdrfiles[xdrid]);
+ xdridptr[xdrid] = NULL;
+ return 1;
+ }
+ }
+ fprintf(stderr, "xdrclose: no such open xdr file\n");
+ exit(1);
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendbits - 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
+ | better make sure that this number of bits is enough to hold the value
+ | Also num must be positive.
+ |
+*/
+
+static void sendbits(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);
+ }
+}
+
+/*_________________________________________________________________________
+ |
+ | sizeofint - calculate bitsize of an integer
+ |
+ | return the number of bits needed to store an integer with given max size
+ |
+*/
+
+static int sizeofint(const 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 the number of small unsigned integers and the maximum value
+ | return the number of bits needed to read or write them with the
+ | routines receiveints and sendints. You need this parameter when
+ | calling these routines. Note that for many calls I can use
+ | the variable 'smallidx' which is exactly the number of bits, and
+ | So I don't need to call 'sizeofints for those calls.
+*/
+
+static int sizeofints( const 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;
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendints - send a small set of small integers in compressed format
+ |
+ | this routine is used internally by xdr3dfcoord, to send a set of
+ | small integers to the buffer.
+ | 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 causes bad results.
+ |
+ */
+
+static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
+ "match size %d\n", nums[i], sizes[i]);
+ exit(1);
+ }
+ /* 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++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
+ } else {
+ for (i = 0; i < num_of_bytes-1; i++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
+ }
+}
+
+
+/*___________________________________________________________________________
+ |
+ | receivebits - 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 receivebits(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;
+}
+
+/*____________________________________________________________________________
+ |
+ | receiveints - decode 'small' integers from the buf array
+ |
+ | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
+ num_of_bits -= 8;
+ }
+ if (num_of_bits > 0) {
+ bytes[num_of_bytes++] = receivebits(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);
+}
+
+/*____________________________________________________________________________
+ |
+ | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
+ |
+ | this routine reads or writes (depending on how you opened the file with
+ | xdropen() ) a large number of 3d coordinates (stored in *fp).
+ | The number of coordinates triplets to write is given by *size. On
+ | read this number may be zero, in which case it reads as many as were written
+ | or it may specify the number if triplets to read (which should match the
+ | number written).
+ | Compression is achieved by first converting all floating numbers to integer
+ | using multiplication by *precision and rounding to the nearest integer.
+ | Then the minimum and maximum value are calculated to determine the range.
+ | The limited range of integers so found, is used to compress the coordinates.
+ | In addition the differences between succesive coordinates is calculated.
+ | If the difference happens to be 'small' then only the difference is saved,
+ | compressing the data even more. The notion of 'small' is changed dynamically
+ | and is enlarged or reduced whenever needed or possible.
+ | Extra compression is achieved in the case of GROMOS and coordinates of
+ | water molecules. GROMOS first writes out the Oxygen position, followed by
+ | the two hydrogens. In order to make the differences smaller (and thereby
+ | compression the data better) the order is changed into first one hydrogen
+ | then the oxygen, followed by the other hydrogen. This is rather special, but
+ | it shouldn't harm in the general case.
+ |
+ */
+
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
+
+
+ static int *ip = NULL;
+ static int oldsize;
+ static int *buf;
+
+ 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 flag, k;
+ int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
+ float *lfp, lf;
+ int tmp, *thiscoord, prevcoord[3];
+ unsigned int tmpcoord[30];
+
+ int bufsize, xdrid, lsize;
+ unsigned int bitsize;
+ float inv_precision;
+ int errval = 1;
+
+ /* find out if xdrs is opened for reading or for writing */
+ xdrid = 0;
+ while (xdridptr[xdrid] != xdrs) {
+ xdrid++;
+ if (xdrid >= MAXID) {
+ fprintf(stderr, "xdr error. no open xdr stream\n");
+ exit (1);
+ }
+ }
+ if (xdrmodes[xdrid] == 'w') {
+
+ /* xdrs is open for writing */
+
+ if (xdr_int(xdrs, size) == 0)
+ return 0;
+ size3 = *size * 3;
+ /* when the number of coordinates is small, don't try to compress; just
+ * write them as floats using xdr_vector
+ */
+ if (*size <= 9 ) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ /* buf[0-2] are special and do not contain actual data */
+ buf[0] = buf[1] = buf[2] = 0;
+ minint[0] = minint[1] = minint[2] = INT_MAX;
+ maxint[0] = maxint[1] = maxint[2] = INT_MIN;
+ prevrun = -1;
+ lfp = fp;
+ lip = ip;
+ mindiff = INT_MAX;
+ oldlint1 = oldlint2 = oldlint3 = 0;
+ while(lfp < fp + size3 ) {
+ /* find nearest integer */
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint1 = lf;
+ if (lint1 < minint[0]) minint[0] = lint1;
+ if (lint1 > maxint[0]) maxint[0] = lint1;
+ *lip++ = lint1;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint2 = lf;
+ if (lint2 < minint[1]) minint[1] = lint2;
+ if (lint2 > maxint[1]) maxint[1] = lint2;
+ *lip++ = lint2;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint3 = lf;
+ if (lint3 < minint[2]) minint[2] = lint3;
+ if (lint3 > maxint[2]) maxint[2] = lint3;
+ *lip++ = lint3;
+ lfp++;
+ diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
+ if (diff < mindiff && lfp > fp + 3)
+ mindiff = diff;
+ oldlint1 = lint1;
+ oldlint2 = lint2;
+ oldlint3 = lint3;
+ }
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
+ (float)maxint[1] - (float)minint[1] >= MAXABS ||
+ (float)maxint[2] - (float)minint[2] >= MAXABS) {
+ /* turning value in unsigned by subtracting minint
+ * would cause overflow
+ */
+ errval = 0;
+ }
+ 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);
+ }
+ lip = ip;
+ luip = (unsigned int *) ip;
+ smallidx = FIRSTIDX;
+ while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
+ smallidx++;
+ }
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ larger = magicints[maxidx] / 2;
+ i = 0;
+ while (i < *size) {
+ is_small = 0;
+ thiscoord = (int *)(luip) + i * 3;
+ if (smallidx < maxidx && i >= 1 &&
+ abs(thiscoord[0] - prevcoord[0]) < larger &&
+ abs(thiscoord[1] - prevcoord[1]) < larger &&
+ abs(thiscoord[2] - prevcoord[2]) < larger) {
+ is_smaller = 1;
+ } else if (smallidx > minidx) {
+ is_smaller = -1;
+ } else {
+ is_smaller = 0;
+ }
+ if (i + 1 < *size) {
+ if (abs(thiscoord[0] - thiscoord[3]) < small &&
+ abs(thiscoord[1] - thiscoord[4]) < small &&
+ abs(thiscoord[2] - thiscoord[5]) < small) {
+ /* interchange first with second atom for better
+ * compression of water molecules
+ */
+ tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
+ thiscoord[3] = tmp;
+ tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
+ thiscoord[4] = tmp;
+ tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
+ thiscoord[5] = tmp;
+ is_small = 1;
+ }
+
+ }
+ tmpcoord[0] = thiscoord[0] - minint[0];
+ tmpcoord[1] = thiscoord[1] - minint[1];
+ tmpcoord[2] = thiscoord[2] - minint[2];
+ if (bitsize == 0) {
+ sendbits(buf, bitsizeint[0], tmpcoord[0]);
+ sendbits(buf, bitsizeint[1], tmpcoord[1]);
+ sendbits(buf, bitsizeint[2], tmpcoord[2]);
+ } else {
+ sendints(buf, 3, bitsize, sizeint, tmpcoord);
+ }
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+ thiscoord = thiscoord + 3;
+ i++;
+
+ run = 0;
+ if (is_small == 0 && is_smaller == -1)
+ is_smaller = 0;
+ while (is_small && run < 8*3) {
+ if (is_smaller == -1 && (
+ SQR(thiscoord[0] - prevcoord[0]) +
+ SQR(thiscoord[1] - prevcoord[1]) +
+ SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
+ is_smaller = 0;
+ }
+
+ tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
+ tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
+ tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
+
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+
+ i++;
+ thiscoord = thiscoord + 3;
+ is_small = 0;
+ if (i < *size &&
+ abs(thiscoord[0] - prevcoord[0]) < small &&
+ abs(thiscoord[1] - prevcoord[1]) < small &&
+ abs(thiscoord[2] - prevcoord[2]) < small) {
+ is_small = 1;
+ }
+ }
+ if (run != prevrun || is_smaller != 0) {
+ prevrun = run;
+ sendbits(buf, 1, 1); /* flag the change in run-length */
+ sendbits(buf, 5, run+is_smaller+1);
+ } else {
+ sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
+ }
+ for (k=0; k < run; k+=3) {
+ sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
+ }
+ if (is_smaller != 0) {
+ smallidx += is_smaller;
+ if (is_smaller < 0) {
+ small = smaller;
+ smaller = magicints[smallidx-1] / 2;
+ } else {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ }
+ }
+ if (buf[1] != 0) buf[0]++;;
+ xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
+ return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
+ } else {
+
+ /* xdrs is open for reading */
+
+ if (xdr_int(xdrs, &lsize) == 0)
+ return 0;
+ if (*size != 0 && lsize != *size) {
+ fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
+ "%d arg vs %d in file", *size, lsize);
+ }
+ *size = lsize;
+ size3 = *size * 3;
+ if (*size <= 9) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ buf[0] = buf[1] = buf[2] = 0;
+
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ 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);
+ }
+
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ larger = magicints[maxidx];
+
+ /* buf[0] holds the length in bytes */
+
+ if (xdr_int(xdrs, &(buf[0])) == 0)
+ return 0;
+ if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
+ return 0;
+ buf[0] = buf[1] = buf[2] = 0;
+
+ lfp = fp;
+ inv_precision = 1.0 / * precision;
+ run = 0;
+ i = 0;
+ lip = ip;
+ while ( i < lsize ) {
+ thiscoord = (int *)(lip) + i * 3;
+
+ if (bitsize == 0) {
+ thiscoord[0] = receivebits(buf, bitsizeint[0]);
+ thiscoord[1] = receivebits(buf, bitsizeint[1]);
+ thiscoord[2] = receivebits(buf, bitsizeint[2]);
+ } else {
+ receiveints(buf, 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 = receivebits(buf, 1);
+ is_smaller = 0;
+ if (flag == 1) {
+ run = receivebits(buf, 5);
+ is_smaller = run % 3;
+ run -= is_smaller;
+ is_smaller--;
+ }
+ if (run > 0) {
+ thiscoord += 3;
+ for (k = 0; k < run; k+=3) {
+ receiveints(buf, 3, smallidx, sizesmall, thiscoord);
+ i++;
+ thiscoord[0] += prevcoord[0] - small;
+ thiscoord[1] += prevcoord[1] - small;
+ thiscoord[2] += prevcoord[2] - small;
+ 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) {
+ small = smaller;
+ if (smallidx > FIRSTIDX) {
+ smaller = magicints[smallidx - 1] /2;
+ } else {
+ smaller = 0;
+ }
+ } else if (is_smaller > 0) {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ }
+ }
+ return 1;
+}
+
+
+
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+/* fixincludes should not add extern "C" to this file */
+/*
+ * Rpc additions to <sys/types.h>
+ */
+#ifndef _RPC_TYPES_H
+#define _RPC_TYPES_H 1
+
+typedef int bool_t;
+typedef int enum_t;
+/* This needs to be changed to uint32_t in the future */
+typedef unsigned long rpcprog_t;
+typedef unsigned long rpcvers_t;
+typedef unsigned long rpcproc_t;
+typedef unsigned long rpcprot_t;
+typedef unsigned long rpcport_t;
+
+#define __dontcare__ -1
+
+#ifndef FALSE
+# define FALSE (0)
+#endif
+
+#ifndef TRUE
+# define TRUE (1)
+#endif
+
+#ifndef NULL
+# define NULL 0
+#endif
+
+#include <stdlib.h> /* For malloc decl. */
+#define mem_alloc(bsize) malloc(bsize)
+/*
+ * XXX: This must not use the second argument, or code in xdr_array.c needs
+ * to be modified.
+ */
+#define mem_free(ptr, bsize) free(ptr)
+
+#ifndef makedev /* ie, we haven't already included it */
+#include <sys/types.h>
+#endif
+
+#ifndef __u_char_defined
+typedef __u_char u_char;
+typedef __u_short u_short;
+typedef __u_int u_int;
+typedef __u_long u_long;
+typedef __quad_t quad_t;
+typedef __u_quad_t u_quad_t;
+typedef __fsid_t fsid_t;
+# define __u_char_defined
+#endif
+#ifndef __daddr_t_defined
+typedef __daddr_t daddr_t;
+typedef __caddr_t caddr_t;
+# define __daddr_t_defined
+#endif
+
+#include <sys/time.h>
+#include <sys/param.h>
+
+#include <netinet/in.h>
+
+#ifndef INADDR_LOOPBACK
+#define INADDR_LOOPBACK (u_long)0x7F000001
+#endif
+#ifndef MAXHOSTNAMELEN
+#define MAXHOSTNAMELEN 64
+#endif
+
+#endif /* rpc/types.h */
--- /dev/null
+divert(-1)
+undefine(`len')
+#
+# append an underscore to FORTRAN function names
+#
+define(`FUNCTION',`$1_')
+#
+# FORTRAN character strings are passed as follows:
+# a pointer to the base of the string is passed in the normal
+# argument list, and the length is passed by value as an extra
+# argument, after all of the other arguments.
+#
+define(`ARGS',`($1`'undivert(1))')
+define(`SAVE',`divert(1)$1`'divert(0)')
+define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
+define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
+define(`STRING_LEN',`$1_len')
+define(`STRING_PTR',`$1_ptr')
+divert(0)
--- /dev/null
+# define INTUSE(name) name
+# define INTDEF(name)
+/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
+#endif
+
+/*
+ * xdr.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1986, Sun Microsystems, Inc.
+ *
+ * These are the "generic" xdr routines used to serialize and de-serialize
+ * most common data items. See xdr.h for more info on the interface to
+ * xdr.
+ */
+
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+#include <libintl.h>
+
+#include "types.h"
+#include "xdr.h"
+
+#ifdef USE_IN_LIBIO
+# include <wchar.h>
+#endif
+
+/*
+ * constants specific to the xdr "protocol"
+ */
+#define XDR_FALSE ((long) 0)
+#define XDR_TRUE ((long) 1)
+#define LASTUNSIGNED ((u_int) 0-1)
+
+/*
+ * for unit alignment
+ */
+static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
+
+/*
+ * Free a data structure using XDR
+ * Not a filter, but a convenient utility nonetheless
+ */
+void
+xdr_free (xdrproc_t proc, char *objp)
+{
+ XDR x;
+
+ x.x_op = XDR_FREE;
+ (*proc) (&x, objp);
+}
+
+/*
+ * XDR nothing
+ */
+bool_t
+xdr_void (void)
+{
+ return TRUE;
+}
+INTDEF(xdr_void)
+
+/*
+ * XDR integers
+ */
+bool_t
+xdr_int (XDR *xdrs, int *ip)
+{
+
+#if INT_MAX < LONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (long) *ip;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *ip = (int) l;
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+#elif INT_MAX == LONG_MAX
+ return INTUSE(xdr_long) (xdrs, (long *) ip);
+#elif INT_MAX == SHRT_MAX
+ return INTUSE(xdr_short) (xdrs, (short *) ip);
+#else
+#error unexpected integer sizes in_xdr_int()
+#endif
+}
+INTDEF(xdr_int)
+
+/*
+ * XDR unsigned integers
+ */
+bool_t
+xdr_u_int (XDR *xdrs, u_int *up)
+{
+#if UINT_MAX < ULONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (u_long) * up;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *up = (u_int) (u_long) l;
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+#elif UINT_MAX == ULONG_MAX
+ return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
+#elif UINT_MAX == USHRT_MAX
+ return INTUSE(xdr_short) (xdrs, (short *) up);
+#else
+#error unexpected integer sizes in_xdr_u_int()
+#endif
+}
+INTDEF(xdr_u_int)
+
+/*
+ * XDR long integers
+ * The definition of xdr_long() is kept for backward
+ * compatibility. Instead xdr_int() should be used.
+ */
+bool_t
+xdr_long (XDR *xdrs, long *lp)
+{
+
+ if (xdrs->x_op == XDR_ENCODE
+ && (sizeof (int32_t) == sizeof (long)
+ || (int32_t) *lp == *lp))
+ return XDR_PUTLONG (xdrs, lp);
+
+ if (xdrs->x_op == XDR_DECODE)
+ return XDR_GETLONG (xdrs, lp);
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_long)
+
+/*
+ * XDR unsigned long integers
+ * The definition of xdr_u_long() is kept for backward
+ * compatibility. Instead xdr_u_int() should be used.
+ */
+bool_t
+xdr_u_long (XDR *xdrs, u_long *ulp)
+{
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ {
+ long int tmp;
+
+ if (XDR_GETLONG (xdrs, &tmp) == FALSE)
+ return FALSE;
+
+ *ulp = (uint32_t) tmp;
+ return TRUE;
+ }
+
+ case XDR_ENCODE:
+ if (sizeof (uint32_t) != sizeof (u_long)
+ && (uint32_t) *ulp != *ulp)
+ return FALSE;
+
+ return XDR_PUTLONG (xdrs, (long *) ulp);
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_u_long)
+
+/*
+ * XDR hyper integers
+ * same as xdr_u_hyper - open coded to save a proc call!
+ */
+bool_t
+xdr_hyper (XDR *xdrs, quad_t *llp)
+{
+ long int t1, t2;
+
+ if (xdrs->x_op == XDR_ENCODE)
+ {
+ t1 = (long) ((*llp) >> 32);
+ t2 = (long) (*llp);
+ return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
+ }
+
+ if (xdrs->x_op == XDR_DECODE)
+ {
+ if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
+ return FALSE;
+ *llp = ((quad_t) t1) << 32;
+ *llp |= (uint32_t) t2;
+ return TRUE;
+ }
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_hyper)
+
+
+/*
+ * XDR hyper integers
+ * same as xdr_hyper - open coded to save a proc call!
+ */
+bool_t
+xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
+{
+ long int t1, t2;
+
+ if (xdrs->x_op == XDR_ENCODE)
+ {
+ t1 = (unsigned long) ((*ullp) >> 32);
+ t2 = (unsigned long) (*ullp);
+ return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
+ }
+
+ if (xdrs->x_op == XDR_DECODE)
+ {
+ if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
+ return FALSE;
+ *ullp = ((u_quad_t) t1) << 32;
+ *ullp |= (uint32_t) t2;
+ return TRUE;
+ }
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_u_hyper)
+
+bool_t
+xdr_longlong_t (XDR *xdrs, quad_t *llp)
+{
+ return INTUSE(xdr_hyper) (xdrs, llp);
+}
+
+bool_t
+xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
+{
+ return INTUSE(xdr_u_hyper) (xdrs, ullp);
+}
+
+/*
+ * XDR short integers
+ */
+bool_t
+xdr_short (XDR *xdrs, short *sp)
+{
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (long) *sp;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *sp = (short) l;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_short)
+
+/*
+ * XDR unsigned short integers
+ */
+bool_t
+xdr_u_short (XDR *xdrs, u_short *usp)
+{
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (u_long) * usp;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *usp = (u_short) (u_long) l;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_u_short)
+
+
+/*
+ * XDR a char
+ */
+bool_t
+xdr_char (XDR *xdrs, char *cp)
+{
+ int i;
+
+ i = (*cp);
+ if (!INTUSE(xdr_int) (xdrs, &i))
+ {
+ return FALSE;
+ }
+ *cp = i;
+ return TRUE;
+}
+
+/*
+ * XDR an unsigned char
+ */
+bool_t
+xdr_u_char (XDR *xdrs, u_char *cp)
+{
+ u_int u;
+
+ u = (*cp);
+ if (!INTUSE(xdr_u_int) (xdrs, &u))
+ {
+ return FALSE;
+ }
+ *cp = u;
+ return TRUE;
+}
+
+/*
+ * XDR booleans
+ */
+bool_t
+xdr_bool (XDR *xdrs, bool_t *bp)
+{
+ long lb;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ lb = *bp ? XDR_TRUE : XDR_FALSE;
+ return XDR_PUTLONG (xdrs, &lb);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &lb))
+ {
+ return FALSE;
+ }
+ *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_bool)
+
+/*
+ * XDR enumerations
+ */
+bool_t
+xdr_enum (XDR *xdrs, enum_t *ep)
+{
+ enum sizecheck
+ {
+ SIZEVAL
+ }; /* used to find the size of an enum */
+
+ /*
+ * enums are treated as ints
+ */
+ if (sizeof (enum sizecheck) == 4)
+ {
+#if INT_MAX < LONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = *ep;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *ep = l;
+ case XDR_FREE:
+ return TRUE;
+
+ }
+ return FALSE;
+#else
+ return INTUSE(xdr_long) (xdrs, (long *) ep);
+#endif
+ }
+ else if (sizeof (enum sizecheck) == sizeof (short))
+ {
+ return INTUSE(xdr_short) (xdrs, (short *) ep);
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+INTDEF(xdr_enum)
+
+/*
+ * XDR opaque data
+ * Allows the specification of a fixed size sequence of opaque bytes.
+ * cp points to the opaque object and cnt gives the byte length.
+ */
+bool_t
+xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
+{
+ u_int rndup;
+ static char crud[BYTES_PER_XDR_UNIT];
+
+ /*
+ * if no data we are done
+ */
+ if (cnt == 0)
+ return TRUE;
+
+ /*
+ * round byte count to full xdr units
+ */
+ rndup = cnt % BYTES_PER_XDR_UNIT;
+ if (rndup > 0)
+ rndup = BYTES_PER_XDR_UNIT - rndup;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (!XDR_GETBYTES (xdrs, cp, cnt))
+ {
+ return FALSE;
+ }
+ if (rndup == 0)
+ return TRUE;
+ return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
+
+ case XDR_ENCODE:
+ if (!XDR_PUTBYTES (xdrs, cp, cnt))
+ {
+ return FALSE;
+ }
+ if (rndup == 0)
+ return TRUE;
+ return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_opaque)
+
+/*
+ * XDR counted bytes
+ * *cpp is a pointer to the bytes, *sizep is the count.
+ * If *cpp is NULL maxsize bytes are allocated
+ */
+bool_t
+xdr_bytes (xdrs, cpp, sizep, maxsize)
+ XDR *xdrs;
+ char **cpp;
+ u_int *sizep;
+ u_int maxsize;
+{
+ char *sp = *cpp; /* sp is the actual string pointer */
+ u_int nodesize;
+
+ /*
+ * first deal with the length since xdr bytes are counted
+ */
+ if (!INTUSE(xdr_u_int) (xdrs, sizep))
+ {
+ return FALSE;
+ }
+ nodesize = *sizep;
+ if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
+ {
+ return FALSE;
+ }
+
+ /*
+ * now deal with the actual bytes
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (nodesize == 0)
+ {
+ return TRUE;
+ }
+ if (sp == NULL)
+ {
+ *cpp = sp = (char *) mem_alloc (nodesize);
+ }
+ if (sp == NULL)
+ {
+ fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
+ return FALSE;
+ }
+ /* fall into ... */
+
+ case XDR_ENCODE:
+ return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
+
+ case XDR_FREE:
+ if (sp != NULL)
+ {
+ mem_free (sp, nodesize);
+ *cpp = NULL;
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_bytes)
+
+/*
+ * Implemented here due to commonality of the object.
+ */
+bool_t
+xdr_netobj (xdrs, np)
+ XDR *xdrs;
+ struct netobj *np;
+{
+
+ return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
+}
+INTDEF(xdr_netobj)
+
+/*
+ * XDR a discriminated union
+ * Support routine for discriminated unions.
+ * You create an array of xdrdiscrim structures, terminated with
+ * an entry with a null procedure pointer. The routine gets
+ * the discriminant value and then searches the array of xdrdiscrims
+ * looking for that value. It calls the procedure given in the xdrdiscrim
+ * to handle the discriminant. If there is no specific routine a default
+ * routine may be called.
+ * If there is no specific or default routine an error is returned.
+ */
+bool_t
+xdr_union (xdrs, dscmp, unp, choices, dfault)
+ XDR *xdrs;
+ enum_t *dscmp; /* enum to decide which arm to work on */
+ char *unp; /* the union itself */
+ const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
+ xdrproc_t dfault; /* default xdr routine */
+{
+ enum_t dscm;
+
+ /*
+ * we deal with the discriminator; it's an enum
+ */
+ if (!INTUSE(xdr_enum) (xdrs, dscmp))
+ {
+ return FALSE;
+ }
+ dscm = *dscmp;
+
+ /*
+ * search choices for a value that matches the discriminator.
+ * if we find one, execute the xdr routine for that value.
+ */
+ for (; choices->proc != NULL_xdrproc_t; choices++)
+ {
+ if (choices->value == dscm)
+ return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
+ }
+
+ /*
+ * no match - execute the default xdr routine if there is one
+ */
+ return ((dfault == NULL_xdrproc_t) ? FALSE :
+ (*dfault) (xdrs, unp, LASTUNSIGNED));
+}
+INTDEF(xdr_union)
+
+
+/*
+ * Non-portable xdr primitives.
+ * Care should be taken when moving these routines to new architectures.
+ */
+
+
+/*
+ * XDR null terminated ASCII strings
+ * xdr_string deals with "C strings" - arrays of bytes that are
+ * terminated by a NULL character. The parameter cpp references a
+ * pointer to storage; If the pointer is null, then the necessary
+ * storage is allocated. The last parameter is the max allowed length
+ * of the string as specified by a protocol.
+ */
+bool_t
+xdr_string (xdrs, cpp, maxsize)
+ XDR *xdrs;
+ char **cpp;
+ u_int maxsize;
+{
+ char *sp = *cpp; /* sp is the actual string pointer */
+ u_int size;
+ u_int nodesize;
+
+ /*
+ * first deal with the length since xdr strings are counted-strings
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_FREE:
+ if (sp == NULL)
+ {
+ return TRUE; /* already free */
+ }
+ /* fall through... */
+ case XDR_ENCODE:
+ if (sp == NULL)
+ return FALSE;
+ size = strlen (sp);
+ break;
+ case XDR_DECODE:
+ break;
+ }
+ if (!INTUSE(xdr_u_int) (xdrs, &size))
+ {
+ return FALSE;
+ }
+ if (size > maxsize)
+ {
+ return FALSE;
+ }
+ nodesize = size + 1;
+ if (nodesize == 0)
+ {
+ /* This means an overflow. It a bug in the caller which
+ provided a too large maxsize but nevertheless catch it
+ here. */
+ return FALSE;
+ }
+
+ /*
+ * now deal with the actual bytes
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (sp == NULL)
+ *cpp = sp = (char *) mem_alloc (nodesize);
+ if (sp == NULL)
+ {
+ fprintf (NULL, "%s", "xdr_string: out of memory\n");
+ return FALSE;
+ }
+ sp[size] = 0;
+ /* fall into ... */
+
+ case XDR_ENCODE:
+ return INTUSE(xdr_opaque) (xdrs, sp, size);
+
+ case XDR_FREE:
+ mem_free (sp, nodesize);
+ *cpp = NULL;
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_string)
+
+/*
+ * Wrapper for xdr_string that can be called directly from
+ * routines like clnt_call
+ */
+bool_t
+xdr_wrapstring (xdrs, cpp)
+ XDR *xdrs;
+ char **cpp;
+{
+ if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
+ {
+ return TRUE;
+ }
+ return FALSE;
+}
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * xdr.h, External Data Representation Serialization Routines.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ */
+
+#ifndef _RPC_XDR_H
+#define _RPC_XDR_H 1
+
+#include <features.h>
+#include <sys/types.h>
+#include "types.h"
+
+/* We need FILE. */
+#include <stdio.h>
+
+__BEGIN_DECLS
+
+/*
+ * XDR provides a conventional way for converting between C data
+ * types and an external bit-string representation. Library supplied
+ * routines provide for the conversion on built-in C data types. These
+ * routines and utility routines defined here are used to help implement
+ * a type encode/decode routine for each user-defined type.
+ *
+ * Each data type provides a single procedure which takes two arguments:
+ *
+ * bool_t
+ * xdrproc(xdrs, argresp)
+ * XDR *xdrs;
+ * <type> *argresp;
+ *
+ * xdrs is an instance of a XDR handle, to which or from which the data
+ * type is to be converted. argresp is a pointer to the structure to be
+ * converted. The XDR handle contains an operation field which indicates
+ * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
+ *
+ * XDR_DECODE may allocate space if the pointer argresp is null. This
+ * data can be freed with the XDR_FREE operation.
+ *
+ * We write only one procedure per data type to make it easy
+ * to keep the encode and decode procedures for a data type consistent.
+ * In many cases the same code performs all operations on a user defined type,
+ * because all the hard work is done in the component type routines.
+ * decode as a series of calls on the nested data types.
+ */
+
+/*
+ * Xdr operations. XDR_ENCODE causes the type to be encoded into the
+ * stream. XDR_DECODE causes the type to be extracted from the stream.
+ * XDR_FREE can be used to release the space allocated by an XDR_DECODE
+ * request.
+ */
+enum xdr_op {
+ XDR_ENCODE = 0,
+ XDR_DECODE = 1,
+ XDR_FREE = 2
+};
+
+/*
+ * This is the number of bytes per unit of external data.
+ */
+#define BYTES_PER_XDR_UNIT (4)
+/*
+ * This only works if the above is a power of 2. But it's defined to be
+ * 4 by the appropriate RFCs. So it will work. And it's normally quicker
+ * than the old routine.
+ */
+#if 1
+#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
+#else /* this is the old routine */
+#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
+ * BYTES_PER_XDR_UNIT)
+#endif
+
+/*
+ * The XDR handle.
+ * Contains operation which is being applied to the stream,
+ * an operations vector for the particular implementation (e.g. see xdr_mem.c),
+ * and two private fields for the use of the particular implementation.
+ */
+typedef struct XDR XDR;
+struct XDR
+ {
+ enum xdr_op x_op; /* operation; fast additional param */
+ struct xdr_ops
+ {
+ bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
+ /* get a long from underlying stream */
+ bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
+ /* put a long to " */
+ bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
+ /* get some bytes from " */
+ bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
+ /* put some bytes to " */
+ u_int (*x_getpostn) (__const XDR *__xdrs);
+ /* returns bytes off from beginning */
+ bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
+ /* lets you reposition the stream */
+ int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
+ /* buf quick ptr to buffered data */
+ void (*x_destroy) (XDR *__xdrs);
+ /* free privates of this xdr_stream */
+ bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
+ /* get a int from underlying stream */
+ bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
+ /* put a int to " */
+ }
+ *x_ops;
+ caddr_t x_public; /* users' data */
+ caddr_t x_private; /* pointer to private data */
+ caddr_t x_base; /* private used for position info */
+ u_int x_handy; /* extra private word */
+ };
+
+/*
+ * A xdrproc_t exists for each data type which is to be encoded or decoded.
+ *
+ * The second argument to the xdrproc_t is a pointer to an opaque pointer.
+ * The opaque pointer generally points to a structure of the data type
+ * to be decoded. If this pointer is 0, then the type routines should
+ * allocate dynamic storage of the appropriate size and return it.
+ * bool_t (*xdrproc_t)(XDR *, caddr_t *);
+ */
+typedef bool_t (*xdrproc_t) (XDR *, void *,...);
+
+
+/*
+ * Operations defined on a XDR handle
+ *
+ * XDR *xdrs;
+ * int32_t *int32p;
+ * long *longp;
+ * caddr_t addr;
+ * u_int len;
+ * u_int pos;
+ */
+#define XDR_GETINT32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
+#define xdr_getint32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
+
+#define XDR_PUTINT32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
+#define xdr_putint32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
+
+#define XDR_GETLONG(xdrs, longp) \
+ (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
+#define xdr_getlong(xdrs, longp) \
+ (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
+
+#define XDR_PUTLONG(xdrs, longp) \
+ (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
+#define xdr_putlong(xdrs, longp) \
+ (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
+
+#define XDR_GETBYTES(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
+#define xdr_getbytes(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
+
+#define XDR_PUTBYTES(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
+#define xdr_putbytes(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
+
+#define XDR_GETPOS(xdrs) \
+ (*(xdrs)->x_ops->x_getpostn)(xdrs)
+#define xdr_getpos(xdrs) \
+ (*(xdrs)->x_ops->x_getpostn)(xdrs)
+
+#define XDR_SETPOS(xdrs, pos) \
+ (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
+#define xdr_setpos(xdrs, pos) \
+ (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
+
+#define XDR_INLINE(xdrs, len) \
+ (*(xdrs)->x_ops->x_inline)(xdrs, len)
+#define xdr_inline(xdrs, len) \
+ (*(xdrs)->x_ops->x_inline)(xdrs, len)
+
+#define XDR_DESTROY(xdrs) \
+ do { \
+ if ((xdrs)->x_ops->x_destroy) \
+ (*(xdrs)->x_ops->x_destroy)(xdrs); \
+ } while (0)
+#define xdr_destroy(xdrs) \
+ do { \
+ if ((xdrs)->x_ops->x_destroy) \
+ (*(xdrs)->x_ops->x_destroy)(xdrs); \
+ } while (0)
+
+/*
+ * Support struct for discriminated unions.
+ * You create an array of xdrdiscrim structures, terminated with
+ * a entry with a null procedure pointer. The xdr_union routine gets
+ * the discriminant value and then searches the array of structures
+ * for a matching value. If a match is found the associated xdr routine
+ * is called to handle that part of the union. If there is
+ * no match, then a default routine may be called.
+ * If there is no match and no default routine it is an error.
+ */
+#define NULL_xdrproc_t ((xdrproc_t)0)
+struct xdr_discrim
+{
+ int value;
+ xdrproc_t proc;
+};
+
+/*
+ * Inline routines for fast encode/decode of primitive data types.
+ * Caveat emptor: these use single memory cycles to get the
+ * data from the underlying buffer, and will fail to operate
+ * properly if the data is not aligned. The standard way to use these
+ * is to say:
+ * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
+ * return (FALSE);
+ * <<< macro calls >>>
+ * where ``count'' is the number of bytes of data occupied
+ * by the primitive data types.
+ *
+ * N.B. and frozen for all time: each data type here uses 4 bytes
+ * of external representation.
+ */
+
+#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
+#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
+#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
+#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
+
+/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
+ * and shouldn't be used any longer. Code which use this defines or longs
+ * in the RPC code will not work on 64bit Solaris platforms !
+ */
+#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
+#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
+#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
+#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+
+
+#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
+#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
+#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
+#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
+
+#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+
+/*
+ * These are the "generic" xdr routines.
+ * None of these can have const applied because it's not possible to
+ * know whether the call is a read or a write to the passed parameter
+ * also, the XDR structure is always updated by some of these calls.
+ */
+extern bool_t xdr_void (void) __THROW;
+extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
+extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
+extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
+extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
+extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
+extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
+extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
+extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
+extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
+extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
+extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
+extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
+extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
+extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
+extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
+extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
+extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
+extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
+extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
+extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
+extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
+extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
+extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
+ u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
+ __THROW;
+extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
+ u_int __maxsize) __THROW;
+extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
+extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
+extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
+ __const struct xdr_discrim *__choices,
+ xdrproc_t dfault) __THROW;
+extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
+extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
+extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
+ u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
+extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
+extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
+extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
+ xdrproc_t __proc) __THROW;
+extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
+ u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
+extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
+extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
+
+/*
+ * Common opaque bytes objects used by many rpc protocols;
+ * declared here due to commonality.
+ */
+#define MAX_NETOBJ_SZ 1024
+struct netobj
+{
+ u_int n_len;
+ char *n_bytes;
+};
+typedef struct netobj netobj;
+extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
+
+/*
+ * These are the public routines for the various implementations of
+ * xdr streams.
+ */
+
+/* XDR using memory buffers */
+extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
+ u_int __size, enum xdr_op __xop) __THROW;
+
+/* XDR using stdio library */
+extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
+ __THROW;
+
+/* XDR pseudo records for tcp */
+extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
+ u_int __recvsize, caddr_t __tcp_handle,
+ int (*__readit) (char *, char *, int),
+ int (*__writeit) (char *, char *, int)) __THROW;
+
+/* make end of xdr record */
+extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
+
+/* move to beginning of next record */
+extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
+
+/* true if no more input */
+extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
+
+/* free memory buffers for xdr */
+extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
+
+__END_DECLS
+
+#endif /* rpc/xdr.h */
--- /dev/null
+# define INTUSE(name) name
+# define INTDEF(name)
+/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
+#endif
+
+/*
+ * xdr_array.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * These are the "non-trivial" xdr primitives used to serialize and de-serialize
+ * arrays. See xdr.h for more info on the interface to xdr.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "types.h"
+#include "xdr.h"
+#include <libintl.h>
+#include <limits.h>
+
+#ifdef USE_IN_LIBIO
+# include <wchar.h>
+#endif
+
+#define LASTUNSIGNED ((u_int)0-1)
+
+
+/*
+ * XDR an array of arbitrary elements
+ * *addrp is a pointer to the array, *sizep is the number of elements.
+ * If addrp is NULL (*sizep * elsize) bytes are allocated.
+ * elsize is the size (in bytes) of each element, and elproc is the
+ * xdr procedure to call to handle each element of the array.
+ */
+bool_t
+xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
+ XDR *xdrs;
+ caddr_t *addrp; /* array pointer */
+ u_int *sizep; /* number of elements */
+ u_int maxsize; /* max numberof elements */
+ u_int elsize; /* size in bytes of each element */
+ xdrproc_t elproc; /* xdr routine to handle each element */
+{
+ u_int i;
+ caddr_t target = *addrp;
+ u_int c; /* the actual element count */
+ bool_t stat = TRUE;
+ u_int nodesize;
+
+ /* like strings, arrays are really counted arrays */
+ if (!INTUSE(xdr_u_int) (xdrs, sizep))
+ {
+ return FALSE;
+ }
+ c = *sizep;
+ /*
+ * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
+ * doesn't actually use its second argument anyway.
+ */
+ if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
+ {
+ return FALSE;
+ }
+ nodesize = c * elsize;
+
+ /*
+ * if we are deserializing, we may need to allocate an array.
+ * We also save time by checking for a null array if we are freeing.
+ */
+ if (target == NULL)
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (c == 0)
+ return TRUE;
+ *addrp = target = mem_alloc (nodesize);
+ if (target == NULL)
+ {
+ fprintf (stderr, "%s", "xdr_array: out of memory\n");
+ return FALSE;
+ }
+ __bzero (target, nodesize);
+ break;
+
+ case XDR_FREE:
+ return TRUE;
+ default:
+ break;
+ }
+
+ /*
+ * now we xdr each element of array
+ */
+ for (i = 0; (i < c) && stat; i++)
+ {
+ stat = (*elproc) (xdrs, target, LASTUNSIGNED);
+ target += elsize;
+ }
+
+ /*
+ * the array may need freeing
+ */
+ if (xdrs->x_op == XDR_FREE)
+ {
+ mem_free (*addrp, nodesize);
+ *addrp = NULL;
+ }
+ return stat;
+}
+INTDEF(xdr_array)
+
+/*
+ * xdr_vector():
+ *
+ * XDR a fixed length array. Unlike variable-length arrays,
+ * the storage of fixed length arrays is static and unfreeable.
+ * > basep: base of the array
+ * > size: size of the array
+ * > elemsize: size of each element
+ * > xdr_elem: routine to XDR each element
+ */
+bool_t
+xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
+ XDR *xdrs;
+ char *basep;
+ u_int nelem;
+ u_int elemsize;
+ xdrproc_t xdr_elem;
+{
+ u_int i;
+ char *elptr;
+
+ elptr = basep;
+ for (i = 0; i < nelem; i++)
+ {
+ if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
+ {
+ return FALSE;
+ }
+ elptr += elemsize;
+ }
+ return TRUE;
+}
--- /dev/null
+/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
+#endif
+
+/*
+ * xdr_float.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * These are the "floating point" xdr routines used to (de)serialize
+ * most common data items. See xdr.h for more info on the interface to
+ * xdr.
+ */
+
+#include <stdio.h>
+#include <endian.h>
+
+#include "types.h"
+#include "xdr.h"
+
+/*
+ * NB: Not portable.
+ * This routine works on Suns (Sky / 68000's) and Vaxen.
+ */
+
+#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
+
+#ifdef vax
+
+/* What IEEE single precision floating point looks like on a Vax */
+struct ieee_single {
+ unsigned int mantissa: 23;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+};
+
+/* Vax single precision floating point */
+struct vax_single {
+ unsigned int mantissa1 : 7;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 16;
+};
+
+#define VAX_SNG_BIAS 0x81
+#define IEEE_SNG_BIAS 0x7f
+
+static struct sgl_limits {
+ struct vax_single s;
+ struct ieee_single ieee;
+} sgl_limits[2] = {
+ {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
+ { 0x0, 0xff, 0x0 }}, /* Max IEEE */
+ {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
+ { 0x0, 0x0, 0x0 }} /* Min IEEE */
+};
+#endif /* vax */
+
+bool_t
+xdr_float(xdrs, fp)
+ XDR *xdrs;
+ float *fp;
+{
+#ifdef vax
+ struct ieee_single is;
+ struct vax_single vs, *vsp;
+ struct sgl_limits *lim;
+ int i;
+#endif
+ switch (xdrs->x_op) {
+
+ case XDR_ENCODE:
+#ifdef vax
+ vs = *((struct vax_single *)fp);
+ for (i = 0, lim = sgl_limits;
+ i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
+ i++, lim++) {
+ if ((vs.mantissa2 == lim->s.mantissa2) &&
+ (vs.exp == lim->s.exp) &&
+ (vs.mantissa1 == lim->s.mantissa1)) {
+ is = lim->ieee;
+ goto shipit;
+ }
+ }
+ is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
+ is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
+ shipit:
+ is.sign = vs.sign;
+ return (XDR_PUTLONG(xdrs, (long *)&is));
+#else
+ if (sizeof(float) == sizeof(long))
+ return (XDR_PUTLONG(xdrs, (long *)fp));
+ else if (sizeof(float) == sizeof(int)) {
+ long tmp = *(int *)fp;
+ return (XDR_PUTLONG(xdrs, &tmp));
+ }
+ break;
+#endif
+
+ case XDR_DECODE:
+#ifdef vax
+ vsp = (struct vax_single *)fp;
+ if (!XDR_GETLONG(xdrs, (long *)&is))
+ return (FALSE);
+ for (i = 0, lim = sgl_limits;
+ i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
+ i++, lim++) {
+ if ((is.exp == lim->ieee.exp) &&
+ (is.mantissa == lim->ieee.mantissa)) {
+ *vsp = lim->s;
+ goto doneit;
+ }
+ }
+ vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
+ vsp->mantissa2 = is.mantissa;
+ vsp->mantissa1 = (is.mantissa >> 16);
+ doneit:
+ vsp->sign = is.sign;
+ return (TRUE);
+#else
+ if (sizeof(float) == sizeof(long))
+ return (XDR_GETLONG(xdrs, (long *)fp));
+ else if (sizeof(float) == sizeof(int)) {
+ long tmp;
+ if (XDR_GETLONG(xdrs, &tmp)) {
+ *(int *)fp = tmp;
+ return (TRUE);
+ }
+ }
+ break;
+#endif
+
+ case XDR_FREE:
+ return (TRUE);
+ }
+ return (FALSE);
+}
+
+/*
+ * This routine works on Suns (Sky / 68000's) and Vaxen.
+ */
+
+#ifdef vax
+/* What IEEE double precision floating point looks like on a Vax */
+struct ieee_double {
+ unsigned int mantissa1 : 20;
+ unsigned int exp : 11;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 32;
+};
+
+/* Vax double precision floating point */
+struct vax_double {
+ unsigned int mantissa1 : 7;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 16;
+ unsigned int mantissa3 : 16;
+ unsigned int mantissa4 : 16;
+};
+
+#define VAX_DBL_BIAS 0x81
+#define IEEE_DBL_BIAS 0x3ff
+#define MASK(nbits) ((1 << nbits) - 1)
+
+static struct dbl_limits {
+ struct vax_double d;
+ struct ieee_double ieee;
+} dbl_limits[2] = {
+ {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
+ { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
+ {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
+ { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
+};
+
+#endif /* vax */
+
+
+bool_t
+xdr_double(xdrs, dp)
+ XDR *xdrs;
+ double *dp;
+{
+#ifdef vax
+ struct ieee_double id;
+ struct vax_double vd;
+ register struct dbl_limits *lim;
+ int i;
+#endif
+
+ switch (xdrs->x_op) {
+
+ case XDR_ENCODE:
+#ifdef vax
+ vd = *((struct vax_double *)dp);
+ for (i = 0, lim = dbl_limits;
+ i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
+ i++, lim++) {
+ if ((vd.mantissa4 == lim->d.mantissa4) &&
+ (vd.mantissa3 == lim->d.mantissa3) &&
+ (vd.mantissa2 == lim->d.mantissa2) &&
+ (vd.mantissa1 == lim->d.mantissa1) &&
+ (vd.exp == lim->d.exp)) {
+ id = lim->ieee;
+ goto shipit;
+ }
+ }
+ id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
+ id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
+ id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
+ (vd.mantissa3 << 13) |
+ ((vd.mantissa4 >> 3) & MASK(13));
+ shipit:
+ id.sign = vd.sign;
+ dp = (double *)&id;
+#endif
+ if (2*sizeof(long) == sizeof(double)) {
+ long *lp = (long *)dp;
+ return (XDR_PUTLONG(xdrs, lp+!LSW) &&
+ XDR_PUTLONG(xdrs, lp+LSW));
+ } else if (2*sizeof(int) == sizeof(double)) {
+ int *ip = (int *)dp;
+ long tmp[2];
+ tmp[0] = ip[!LSW];
+ tmp[1] = ip[LSW];
+ return (XDR_PUTLONG(xdrs, tmp) &&
+ XDR_PUTLONG(xdrs, tmp+1));
+ }
+ break;
+
+ case XDR_DECODE:
+#ifdef vax
+ lp = (long *)&id;
+ if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
+ return (FALSE);
+ for (i = 0, lim = dbl_limits;
+ i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
+ i++, lim++) {
+ if ((id.mantissa2 == lim->ieee.mantissa2) &&
+ (id.mantissa1 == lim->ieee.mantissa1) &&
+ (id.exp == lim->ieee.exp)) {
+ vd = lim->d;
+ goto doneit;
+ }
+ }
+ vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
+ vd.mantissa1 = (id.mantissa1 >> 13);
+ vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
+ (id.mantissa2 >> 29);
+ vd.mantissa3 = (id.mantissa2 >> 13);
+ vd.mantissa4 = (id.mantissa2 << 3);
+ doneit:
+ vd.sign = id.sign;
+ *dp = *((double *)&vd);
+ return (TRUE);
+#else
+ if (2*sizeof(long) == sizeof(double)) {
+ long *lp = (long *)dp;
+ return (XDR_GETLONG(xdrs, lp+!LSW) &&
+ XDR_GETLONG(xdrs, lp+LSW));
+ } else if (2*sizeof(int) == sizeof(double)) {
+ int *ip = (int *)dp;
+ long tmp[2];
+ if (XDR_GETLONG(xdrs, tmp+!LSW) &&
+ XDR_GETLONG(xdrs, tmp+LSW)) {
+ ip[0] = tmp[0];
+ ip[1] = tmp[1];
+ return (TRUE);
+ }
+ }
+ break;
+#endif
+
+ case XDR_FREE:
+ return (TRUE);
+ }
+ return (FALSE);
+}
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * xdr_stdio.c, XDR implementation on standard i/o file.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * This set of routines implements a XDR on a stdio stream.
+ * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
+ * from the stream.
+ */
+
+#include "types.h"
+#include <stdio.h>
+#include "xdr.h"
+
+#ifdef USE_IN_LIBIO
+# include <libio/iolibio.h>
+# define fflush(s) INTUSE(_IO_fflush) (s)
+# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
+# define ftell(s) INTUSE(_IO_ftell) (s)
+# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
+#endif
+
+static bool_t xdrstdio_getlong (XDR *, long *);
+static bool_t xdrstdio_putlong (XDR *, const long *);
+static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
+static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
+static u_int xdrstdio_getpos (const XDR *);
+static bool_t xdrstdio_setpos (XDR *, u_int);
+static int32_t *xdrstdio_inline (XDR *, u_int);
+static void xdrstdio_destroy (XDR *);
+static bool_t xdrstdio_getint32 (XDR *, int32_t *);
+static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
+
+/*
+ * Ops vector for stdio type XDR
+ */
+static const struct xdr_ops xdrstdio_ops =
+{
+ xdrstdio_getlong, /* deserialize a long int */
+ xdrstdio_putlong, /* serialize a long int */
+ xdrstdio_getbytes, /* deserialize counted bytes */
+ xdrstdio_putbytes, /* serialize counted bytes */
+ xdrstdio_getpos, /* get offset in the stream */
+ xdrstdio_setpos, /* set offset in the stream */
+ xdrstdio_inline, /* prime stream for inline macros */
+ xdrstdio_destroy, /* destroy stream */
+ xdrstdio_getint32, /* deserialize a int */
+ xdrstdio_putint32 /* serialize a int */
+};
+
+/*
+ * Initialize a stdio xdr stream.
+ * Sets the xdr stream handle xdrs for use on the stream file.
+ * Operation flag is set to op.
+ */
+void
+xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
+{
+ xdrs->x_op = op;
+ /* We have to add the const since the `struct xdr_ops' in `struct XDR'
+ is not `const'. */
+ xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
+ xdrs->x_private = (caddr_t) file;
+ xdrs->x_handy = 0;
+ xdrs->x_base = 0;
+}
+
+/*
+ * Destroy a stdio xdr stream.
+ * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
+ */
+static void
+xdrstdio_destroy (XDR *xdrs)
+{
+ (void) fflush ((FILE *) xdrs->x_private);
+ /* xx should we close the file ?? */
+};
+
+static bool_t
+xdrstdio_getlong (XDR *xdrs, long *lp)
+{
+ u_int32_t mycopy;
+
+ if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ *lp = (long) ntohl (mycopy);
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putlong (XDR *xdrs, const long *lp)
+{
+ int32_t mycopy = htonl ((u_int32_t) *lp);
+
+ if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
+{
+ if ((len != 0) && (fread (addr, (int) len, 1,
+ (FILE *) xdrs->x_private) != 1))
+ return FALSE;
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
+{
+ if ((len != 0) && (fwrite (addr, (int) len, 1,
+ (FILE *) xdrs->x_private) != 1))
+ return FALSE;
+ return TRUE;
+}
+
+static u_int
+xdrstdio_getpos (const XDR *xdrs)
+{
+ return (u_int) ftell ((FILE *) xdrs->x_private);
+}
+
+static bool_t
+xdrstdio_setpos (XDR *xdrs, u_int pos)
+{
+ return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
+}
+
+static int32_t *
+xdrstdio_inline (XDR *xdrs, u_int len)
+{
+ /*
+ * Must do some work to implement this: must insure
+ * enough data in the underlying stdio buffer,
+ * that the buffer is aligned so that we can indirect through a
+ * long *, and stuff this pointer in xdrs->x_buf. Doing
+ * a fread or fwrite to a scratch buffer would defeat
+ * most of the gains to be had here and require storage
+ * management on this buffer, so we don't do this.
+ */
+ return NULL;
+}
+
+static bool_t
+xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
+{
+ int32_t mycopy;
+
+ if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ *ip = ntohl (mycopy);
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
+{
+ int32_t mycopy = htonl (*ip);
+
+ ip = &mycopy;
+ if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ return TRUE;
+}
+
+/* libc_hidden_def (xdrstdio_create) */
--- /dev/null
+/*_________________________________________________________________
+ |
+ | xdrf.h - include file for C routines that want to use the
+ | functions below.
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type);
+int xdrclose(XDR *xdrs) ;
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
+
--- /dev/null
+#
+# CMake project file for UNRESPACK
+#
+
+# m4 macro processor
+add_custom_command(
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c
+ COMMAND m4
+ ARGS ${CMAKE_CURRENT_SOURCE_DIR}/underscore.m4 ${CMAKE_CURRENT_SOURCE_DIR}/libxdrf.m4 > ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c
+ VERBATIM
+)
+
+# add headers from current dir
+include_directories(${CMAKE_CURRENT_SOURCE_DIR})
+# compile the libxdrf library
+add_library(xdrf STATIC ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c ftocstr.c)
+set(UNRES_XDRFLIB ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.a PARENT_SCOPE)
+
+#add_dependencies( ${UNRES_BIN} xdrf )
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+CC = gcc
+CFLAGS = -O
+
+M4 = m4
+M4FILE = underscore.m4
+
+libxdrf.a: libxdrf.o ftocstr.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f libxdrf.o ftocstr.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+ rm -f libxdrf.c
+
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
+
+CC = /usr/bin/blrts_xlc
+CPPC = /usr/bin/blrts_xlc
+
+CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
+
+M4 = m4
+M4FILE = RS6K.m4
+
+libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f *.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+# rm -f libxdrf.c
+
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+CC = cc
+CFLAGS = -O
+
+M4 = m4
+M4FILE = underscore.m4
+
+libxdrf.a: libxdrf.o ftocstr.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f libxdrf.o ftocstr.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+ rm -f libxdrf.c
+
--- /dev/null
+divert(-1)
+undefine(`len')
+#
+# do nothing special to FORTRAN function names
+#
+define(`FUNCTION',`$1')
+#
+# FORTRAN character strings are passed as follows:
+# a pointer to the base of the string is passed in the normal
+# argument list, and the length is passed by value as an extra
+# argument, after all of the other arguments.
+#
+define(`ARGS',`($1`'undivert(1))')
+define(`SAVE',`divert(1)$1`'divert(0)')
+define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
+define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
+define(`STRING_LEN',`$1_len')
+define(`STRING_PTR',`$1_ptr')
+divert(0)
+
--- /dev/null
+
+
+int ftocstr(ds, dl, ss, sl)
+ char *ds, *ss; /* dst, src ptrs */
+ int dl; /* dst max len */
+ int sl; /* src len */
+{
+ char *p;
+
+ for (p = ss + sl; --p >= ss && *p == ' '; ) ;
+ sl = p - ss + 1;
+ dl--;
+ ds[0] = 0;
+ if (sl > dl)
+ return 1;
+ while (sl--)
+ (*ds++ = *ss++);
+ *ds = '\0';
+ return 0;
+}
+
+
+int ctofstr(ds, dl, ss)
+ char *ds; /* dest space */
+ int dl; /* max dest length */
+ char *ss; /* src string (0-term) */
+{
+ while (dl && *ss) {
+ *ds++ = *ss++;
+ dl--;
+ }
+ while (dl--)
+ *ds++ = ' ';
+ return 0;
+}
--- /dev/null
+/*____________________________________________________________________________
+ |
+ | libxdrf - portable fortran interface to xdr. some xdr routines
+ | are C routines for compressed coordinates
+ |
+ | version 1.1
+ |
+ | This collection of routines is intended to write and read
+ | data in a portable way to a file, so data written on one type
+ | of machine can be read back on a different type.
+ |
+ | all fortran routines use an integer 'xdrid', which is an id to the
+ | current xdr file, and is set by xdrfopen.
+ | most routines have in integer 'ret' which is the return value.
+ | The value of 'ret' is zero on failure, and most of the time one
+ | on succes.
+ |
+ | There are three routines useful for C users:
+ | xdropen(), xdrclose(), xdr3dfcoord().
+ | The first two replace xdrstdio_create and xdr_destroy, and *must* be
+ | used when you plan to use xdr3dfcoord(). (they are also a bit
+ | easier to interface). For writing data other than compressed coordinates
+ | you should use the standard C xdr routines (see xdr man page)
+ |
+ | xdrfopen(xdrid, filename, mode, ret)
+ | character *(*) filename
+ | character *(*) mode
+ |
+ | this will open the file with the given filename (string)
+ | and the given mode, it returns an id in xdrid, which is
+ | to be used in all other calls to xdrf routines.
+ | mode is 'w' to create, or update an file, for all other
+ | values of mode the file is opened for reading
+ |
+ | you need to call xdrfclose to flush the output and close
+ | the file.
+ | Note that you should not use xdrstdio_create, which comes with the
+ | standard xdr library
+ |
+ | xdrfclose(xdrid, ret)
+ | flush the data to the file, and closes the file;
+ | You should not use xdr_destroy (which comes standard with
+ | the xdr libraries.
+ |
+ | xdrfbool(xdrid, bp, ret)
+ | integer pb
+ |
+ | This filter produces values of either 1 or 0
+ |
+ | xdrfchar(xdrid, cp, ret)
+ | character cp
+ |
+ | filter that translate between characters and their xdr representation
+ | Note that the characters in not compressed and occupies 4 bytes.
+ |
+ | xdrfdouble(xdrid, dp, ret)
+ | double dp
+ |
+ | read/write a double.
+ |
+ | xdrffloat(xdrid, fp, ret)
+ | float fp
+ |
+ | read/write a float.
+ |
+ | xdrfint(xdrid, ip, ret)
+ | integer ip
+ |
+ | read/write integer.
+ |
+ | xdrflong(xdrid, lp, ret)
+ | integer lp
+ |
+ | this routine has a possible portablility problem due to 64 bits longs.
+ |
+ | xdrfshort(xdrid, sp, ret)
+ | integer *2 sp
+ |
+ | xdrfstring(xdrid, sp, maxsize, ret)
+ | character *(*)
+ | integer maxsize
+ |
+ | read/write a string, with maximum length given by maxsize
+ |
+ | xdrfwrapstring(xdris, sp, ret)
+ | character *(*)
+ |
+ | read/write a string (it is the same as xdrfstring accept that it finds
+ | the stringlength itself.
+ |
+ | xdrfvector(xdrid, cp, size, xdrfproc, ret)
+ | character *(*)
+ | integer size
+ | external xdrfproc
+ |
+ | read/write an array pointed to by cp, with number of elements
+ | defined by 'size'. the routine 'xdrfproc' is the name
+ | of one of the above routines to read/write data (like xdrfdouble)
+ | In contrast with the c-version you don't need to specify the
+ | byte size of an element.
+ | xdrfstring is not allowed here (it is in the c version)
+ |
+ | xdrf3dfcoord(xdrid, fp, size, precision, ret)
+ | real (*) fp
+ | real precision
+ | integer size
+ |
+ | this is *NOT* a standard xdr routine. I named it this way, because
+ | it invites people to use the other xdr routines.
+ | It is introduced to store specifically 3d coordinates of molecules
+ | (as found in molecular dynamics) and it writes it in a compressed way.
+ | It starts by multiplying all numbers by precision and
+ | rounding the result to integer. effectively converting
+ | all floating point numbers to fixed point.
+ | it uses an algorithm for compression that is optimized for
+ | molecular data, but could be used for other 3d coordinates
+ | as well. There is subtantial overhead involved, so call this
+ | routine only if you have a large number of coordinates to read/write
+ |
+ | ________________________________________________________________________
+ |
+ | Below are the routines to be used by C programmers. Use the 'normal'
+ | xdr routines to write integers, floats, etc (see man xdr)
+ |
+ | int xdropen(XDR *xdrs, const char *filename, const char *type)
+ | This will open the file with the given filename and the
+ | given mode. You should pass it an allocated XDR struct
+ | in xdrs, to be used in all other calls to xdr routines.
+ | Mode is 'w' to create, or update an file, and for all
+ | other values of mode the file is opened for reading.
+ | You need to call xdrclose to flush the output and close
+ | the file.
+ |
+ | Note that you should not use xdrstdio_create, which
+ | comes with the standard xdr library.
+ |
+ | int xdrclose(XDR *xdrs)
+ | Flush the data to the file, and close the file;
+ | You should not use xdr_destroy (which comes standard
+ | with the xdr libraries).
+ |
+ | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
+ | This is \fInot\fR a standard xdr routine. I named it this
+ | way, because it invites people to use the other xdr
+ | routines.
+ |
+ | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+*/
+
+
+#include <limits.h>
+#include <malloc.h>
+#include <math.h>
+/* #include <rpc/rpc.h>
+#include <rpc/xdr.h> */
+#include "xdr.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "xdrf.h"
+
+int ftocstr(char *, int, char *, int);
+int ctofstr(char *, int, char *);
+
+#define MAXID 20
+static FILE *xdrfiles[MAXID];
+static XDR *xdridptr[MAXID];
+static char xdrmodes[MAXID];
+static unsigned int cnt;
+
+typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
+
+void
+FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
+int *xdrid, *ret;
+int *pb;
+{
+ *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
+int *xdrid, *ret;
+char *cp;
+{
+ *ret = xdr_char(xdridptr[*xdrid], cp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
+int *xdrid, *ret;
+double *dp;
+{
+ *ret = xdr_double(xdridptr[*xdrid], dp);
+ cnt += sizeof(double);
+}
+
+void
+FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
+int *xdrid, *ret;
+float *fp;
+{
+ *ret = xdr_float(xdridptr[*xdrid], fp);
+ cnt += sizeof(float);
+}
+
+void
+FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
+int *xdrid, *ret;
+int *ip;
+{
+ *ret = xdr_int(xdridptr[*xdrid], ip);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
+int *xdrid, *ret;
+long *lp;
+{
+ *ret = xdr_long(xdridptr[*xdrid], lp);
+ cnt += sizeof(long);
+}
+
+void
+FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
+int *xdrid, *ret;
+short *sp;
+{
+ *ret = xdr_short(xdridptr[*xdrid], sp);
+ cnt += sizeof(sp);
+}
+
+void
+FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
+int *xdrid, *ret;
+char *ucp;
+{
+ *ret = xdr_u_char(xdridptr[*xdrid], ucp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
+int *xdrid, *ret;
+unsigned long *ulp;
+{
+ *ret = xdr_u_long(xdridptr[*xdrid], ulp);
+ cnt += sizeof(unsigned long);
+}
+
+void
+FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
+int *xdrid, *ret;
+unsigned short *usp;
+{
+ *ret = xdr_u_short(xdridptr[*xdrid], usp);
+ cnt += sizeof(unsigned short);
+}
+
+void
+FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
+int *xdrid, *ret;
+float *fp;
+int *size;
+float *precision;
+{
+ *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
+}
+
+void
+FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+int *maxsize;
+{
+ char *tsp;
+
+ tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += *maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+{
+ char *tsp;
+ int maxsize;
+ maxsize = (STRING_LEN(sp)) + 1;
+ tsp = (char*) malloc(maxsize * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
+int *xdrid, *ret;
+caddr_t *cp;
+int *ccnt;
+{
+ *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
+ cnt += *ccnt;
+}
+
+void
+FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
+int *xdrid, *ret;
+int *pos;
+{
+ *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
+}
+
+void
+FUNCTION(xdrf) ARGS(`xdrid, pos')
+int *xdrid, *pos;
+{
+ *pos = xdr_getpos(xdridptr[*xdrid]);
+}
+
+void
+FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
+int *xdrid, *ret;
+char *cp;
+int *size;
+FUNCTION(xdrfproc) elproc;
+{
+ int lcnt;
+ cnt = 0;
+ for (lcnt = 0; lcnt < *size; lcnt++) {
+ elproc(xdrid, (cp+cnt) , ret);
+ }
+}
+
+
+void
+FUNCTION(xdrfclose) ARGS(`xdrid, ret')
+int *xdrid;
+int *ret;
+{
+ *ret = xdrclose(xdridptr[*xdrid]);
+ cnt = 0;
+}
+
+void
+FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
+int *xdrid;
+STRING_ARG_DECL(fp);
+STRING_ARG_DECL(mode);
+int *ret;
+{
+ char fname[512];
+ char fmode[3];
+
+ if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
+ *ret = 0;
+ }
+ if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
+ STRING_LEN(mode))) {
+ *ret = 0;
+ }
+
+ *xdrid = xdropen(NULL, fname, fmode);
+ if (*xdrid == 0)
+ *ret = 0;
+ else
+ *ret = 1;
+}
+
+/*___________________________________________________________________________
+ |
+ | what follows are the C routines for opening, closing xdr streams
+ | and the routine to read/write compressed coordinates together
+ | with some routines to assist in this task (those are marked
+ | static and cannot be called from user programs)
+*/
+#define MAXABS INT_MAX-2
+
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x):(y))
+#endif
+#ifndef MAX
+#define MAX(x,y) ((x) > (y) ? (x):(y))
+#endif
+#ifndef SQR
+#define SQR(x) ((x)*(x))
+#endif
+static 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))
+
+
+/*__________________________________________________________________________
+ |
+ | xdropen - open xdr file
+ |
+ | This versions differs from xdrstdio_create, because I need to know
+ | the state of the file (read or write) so I can use xdr3dfcoord
+ | in eigther read or write mode, and the file descriptor
+ | so I can close the file (something xdr_destroy doesn't do).
+ |
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type) {
+ static int init_done = 0;
+ enum xdr_op lmode;
+ const char *type1;
+ int xdrid;
+
+ if (init_done == 0) {
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ xdridptr[xdrid] = NULL;
+ }
+ init_done = 1;
+ }
+ xdrid = 1;
+ while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
+ xdrid++;
+ }
+ if (xdrid == MAXID) {
+ return 0;
+ }
+ if (*type == 'w' || *type == 'W') {
+ type = "w+";
+ type1 = "w+";
+ lmode = XDR_ENCODE;
+ } else if (*type == 'a' || *type == 'A') {
+ type = "w+";
+ type1 = "a+";
+ lmode = XDR_ENCODE;
+ } else {
+ type = "r";
+ type1 = "r";
+ lmode = XDR_DECODE;
+ }
+ xdrfiles[xdrid] = fopen(filename, type1);
+ if (xdrfiles[xdrid] == NULL) {
+ xdrs = NULL;
+ return 0;
+ }
+ xdrmodes[xdrid] = *type;
+ /* next test isn't usefull in the case of C language
+ * but is used for the Fortran interface
+ * (C users are expected to pass the address of an already allocated
+ * XDR staructure)
+ */
+ if (xdrs == NULL) {
+ xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
+ xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
+ } else {
+ xdridptr[xdrid] = xdrs;
+ xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
+ }
+ return xdrid;
+}
+
+/*_________________________________________________________________________
+ |
+ | xdrclose - close a xdr file
+ |
+ | This will flush the xdr buffers, and destroy the xdr stream.
+ | It also closes the associated file descriptor (this is *not*
+ | done by xdr_destroy).
+ |
+*/
+
+int xdrclose(XDR *xdrs) {
+ int xdrid;
+
+ if (xdrs == NULL) {
+ fprintf(stderr, "xdrclose: passed a NULL pointer\n");
+ exit(1);
+ }
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ if (xdridptr[xdrid] == xdrs) {
+
+ xdr_destroy(xdrs);
+ fclose(xdrfiles[xdrid]);
+ xdridptr[xdrid] = NULL;
+ return 1;
+ }
+ }
+ fprintf(stderr, "xdrclose: no such open xdr file\n");
+ exit(1);
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendbits - 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
+ | better make sure that this number of bits is enough to hold the value
+ | Also num must be positive.
+ |
+*/
+
+static void sendbits(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);
+ }
+}
+
+/*_________________________________________________________________________
+ |
+ | sizeofint - calculate bitsize of an integer
+ |
+ | return the number of bits needed to store an integer with given max size
+ |
+*/
+
+static int sizeofint(const 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 the number of small unsigned integers and the maximum value
+ | return the number of bits needed to read or write them with the
+ | routines receiveints and sendints. You need this parameter when
+ | calling these routines. Note that for many calls I can use
+ | the variable 'smallidx' which is exactly the number of bits, and
+ | So I don't need to call 'sizeofints for those calls.
+*/
+
+static int sizeofints( const 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;
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendints - send a small set of small integers in compressed format
+ |
+ | this routine is used internally by xdr3dfcoord, to send a set of
+ | small integers to the buffer.
+ | 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 causes bad results.
+ |
+ */
+
+static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
+ "match size %d\n", nums[i], sizes[i]);
+ exit(1);
+ }
+ /* 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++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
+ } else {
+ for (i = 0; i < num_of_bytes-1; i++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
+ }
+}
+
+
+/*___________________________________________________________________________
+ |
+ | receivebits - 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 receivebits(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;
+}
+
+/*____________________________________________________________________________
+ |
+ | receiveints - decode 'small' integers from the buf array
+ |
+ | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
+ num_of_bits -= 8;
+ }
+ if (num_of_bits > 0) {
+ bytes[num_of_bytes++] = receivebits(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);
+}
+
+/*____________________________________________________________________________
+ |
+ | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
+ |
+ | this routine reads or writes (depending on how you opened the file with
+ | xdropen() ) a large number of 3d coordinates (stored in *fp).
+ | The number of coordinates triplets to write is given by *size. On
+ | read this number may be zero, in which case it reads as many as were written
+ | or it may specify the number if triplets to read (which should match the
+ | number written).
+ | Compression is achieved by first converting all floating numbers to integer
+ | using multiplication by *precision and rounding to the nearest integer.
+ | Then the minimum and maximum value are calculated to determine the range.
+ | The limited range of integers so found, is used to compress the coordinates.
+ | In addition the differences between succesive coordinates is calculated.
+ | If the difference happens to be 'small' then only the difference is saved,
+ | compressing the data even more. The notion of 'small' is changed dynamically
+ | and is enlarged or reduced whenever needed or possible.
+ | Extra compression is achieved in the case of GROMOS and coordinates of
+ | water molecules. GROMOS first writes out the Oxygen position, followed by
+ | the two hydrogens. In order to make the differences smaller (and thereby
+ | compression the data better) the order is changed into first one hydrogen
+ | then the oxygen, followed by the other hydrogen. This is rather special, but
+ | it shouldn't harm in the general case.
+ |
+ */
+
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
+
+
+ static int *ip = NULL;
+ static int oldsize;
+ static int *buf;
+
+ 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 flag, k;
+ int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
+ float *lfp, lf;
+ int tmp, *thiscoord, prevcoord[3];
+ unsigned int tmpcoord[30];
+
+ int bufsize, xdrid, lsize;
+ unsigned int bitsize;
+ float inv_precision;
+ int errval = 1;
+
+ /* find out if xdrs is opened for reading or for writing */
+ xdrid = 0;
+ while (xdridptr[xdrid] != xdrs) {
+ xdrid++;
+ if (xdrid >= MAXID) {
+ fprintf(stderr, "xdr error. no open xdr stream\n");
+ exit (1);
+ }
+ }
+ if (xdrmodes[xdrid] == 'w') {
+
+ /* xdrs is open for writing */
+
+ if (xdr_int(xdrs, size) == 0)
+ return 0;
+ size3 = *size * 3;
+ /* when the number of coordinates is small, don't try to compress; just
+ * write them as floats using xdr_vector
+ */
+ if (*size <= 9 ) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ /* buf[0-2] are special and do not contain actual data */
+ buf[0] = buf[1] = buf[2] = 0;
+ minint[0] = minint[1] = minint[2] = INT_MAX;
+ maxint[0] = maxint[1] = maxint[2] = INT_MIN;
+ prevrun = -1;
+ lfp = fp;
+ lip = ip;
+ mindiff = INT_MAX;
+ oldlint1 = oldlint2 = oldlint3 = 0;
+ while(lfp < fp + size3 ) {
+ /* find nearest integer */
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint1 = lf;
+ if (lint1 < minint[0]) minint[0] = lint1;
+ if (lint1 > maxint[0]) maxint[0] = lint1;
+ *lip++ = lint1;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint2 = lf;
+ if (lint2 < minint[1]) minint[1] = lint2;
+ if (lint2 > maxint[1]) maxint[1] = lint2;
+ *lip++ = lint2;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint3 = lf;
+ if (lint3 < minint[2]) minint[2] = lint3;
+ if (lint3 > maxint[2]) maxint[2] = lint3;
+ *lip++ = lint3;
+ lfp++;
+ diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
+ if (diff < mindiff && lfp > fp + 3)
+ mindiff = diff;
+ oldlint1 = lint1;
+ oldlint2 = lint2;
+ oldlint3 = lint3;
+ }
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
+ (float)maxint[1] - (float)minint[1] >= MAXABS ||
+ (float)maxint[2] - (float)minint[2] >= MAXABS) {
+ /* turning value in unsigned by subtracting minint
+ * would cause overflow
+ */
+ errval = 0;
+ }
+ 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);
+ }
+ lip = ip;
+ luip = (unsigned int *) ip;
+ smallidx = FIRSTIDX;
+ while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
+ smallidx++;
+ }
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ larger = magicints[maxidx] / 2;
+ i = 0;
+ while (i < *size) {
+ is_small = 0;
+ thiscoord = (int *)(luip) + i * 3;
+ if (smallidx < maxidx && i >= 1 &&
+ abs(thiscoord[0] - prevcoord[0]) < larger &&
+ abs(thiscoord[1] - prevcoord[1]) < larger &&
+ abs(thiscoord[2] - prevcoord[2]) < larger) {
+ is_smaller = 1;
+ } else if (smallidx > minidx) {
+ is_smaller = -1;
+ } else {
+ is_smaller = 0;
+ }
+ if (i + 1 < *size) {
+ if (abs(thiscoord[0] - thiscoord[3]) < small &&
+ abs(thiscoord[1] - thiscoord[4]) < small &&
+ abs(thiscoord[2] - thiscoord[5]) < small) {
+ /* interchange first with second atom for better
+ * compression of water molecules
+ */
+ tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
+ thiscoord[3] = tmp;
+ tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
+ thiscoord[4] = tmp;
+ tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
+ thiscoord[5] = tmp;
+ is_small = 1;
+ }
+
+ }
+ tmpcoord[0] = thiscoord[0] - minint[0];
+ tmpcoord[1] = thiscoord[1] - minint[1];
+ tmpcoord[2] = thiscoord[2] - minint[2];
+ if (bitsize == 0) {
+ sendbits(buf, bitsizeint[0], tmpcoord[0]);
+ sendbits(buf, bitsizeint[1], tmpcoord[1]);
+ sendbits(buf, bitsizeint[2], tmpcoord[2]);
+ } else {
+ sendints(buf, 3, bitsize, sizeint, tmpcoord);
+ }
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+ thiscoord = thiscoord + 3;
+ i++;
+
+ run = 0;
+ if (is_small == 0 && is_smaller == -1)
+ is_smaller = 0;
+ while (is_small && run < 8*3) {
+ if (is_smaller == -1 && (
+ SQR(thiscoord[0] - prevcoord[0]) +
+ SQR(thiscoord[1] - prevcoord[1]) +
+ SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
+ is_smaller = 0;
+ }
+
+ tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
+ tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
+ tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
+
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+
+ i++;
+ thiscoord = thiscoord + 3;
+ is_small = 0;
+ if (i < *size &&
+ abs(thiscoord[0] - prevcoord[0]) < small &&
+ abs(thiscoord[1] - prevcoord[1]) < small &&
+ abs(thiscoord[2] - prevcoord[2]) < small) {
+ is_small = 1;
+ }
+ }
+ if (run != prevrun || is_smaller != 0) {
+ prevrun = run;
+ sendbits(buf, 1, 1); /* flag the change in run-length */
+ sendbits(buf, 5, run+is_smaller+1);
+ } else {
+ sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
+ }
+ for (k=0; k < run; k+=3) {
+ sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
+ }
+ if (is_smaller != 0) {
+ smallidx += is_smaller;
+ if (is_smaller < 0) {
+ small = smaller;
+ smaller = magicints[smallidx-1] / 2;
+ } else {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ }
+ }
+ if (buf[1] != 0) buf[0]++;;
+ xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
+ return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
+ } else {
+
+ /* xdrs is open for reading */
+
+ if (xdr_int(xdrs, &lsize) == 0)
+ return 0;
+ if (*size != 0 && lsize != *size) {
+ fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
+ "%d arg vs %d in file", *size, lsize);
+ }
+ *size = lsize;
+ size3 = *size * 3;
+ if (*size <= 9) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ buf[0] = buf[1] = buf[2] = 0;
+
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ 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);
+ }
+
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ larger = magicints[maxidx];
+
+ /* buf[0] holds the length in bytes */
+
+ if (xdr_int(xdrs, &(buf[0])) == 0)
+ return 0;
+ if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
+ return 0;
+ buf[0] = buf[1] = buf[2] = 0;
+
+ lfp = fp;
+ inv_precision = 1.0 / * precision;
+ run = 0;
+ i = 0;
+ lip = ip;
+ while ( i < lsize ) {
+ thiscoord = (int *)(lip) + i * 3;
+
+ if (bitsize == 0) {
+ thiscoord[0] = receivebits(buf, bitsizeint[0]);
+ thiscoord[1] = receivebits(buf, bitsizeint[1]);
+ thiscoord[2] = receivebits(buf, bitsizeint[2]);
+ } else {
+ receiveints(buf, 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 = receivebits(buf, 1);
+ is_smaller = 0;
+ if (flag == 1) {
+ run = receivebits(buf, 5);
+ is_smaller = run % 3;
+ run -= is_smaller;
+ is_smaller--;
+ }
+ if (run > 0) {
+ thiscoord += 3;
+ for (k = 0; k < run; k+=3) {
+ receiveints(buf, 3, smallidx, sizesmall, thiscoord);
+ i++;
+ thiscoord[0] += prevcoord[0] - small;
+ thiscoord[1] += prevcoord[1] - small;
+ thiscoord[2] += prevcoord[2] - small;
+ 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) {
+ small = smaller;
+ if (smallidx > FIRSTIDX) {
+ smaller = magicints[smallidx - 1] /2;
+ } else {
+ smaller = 0;
+ }
+ } else if (is_smaller > 0) {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ }
+ }
+ return 1;
+}
+
+
+
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+/* fixincludes should not add extern "C" to this file */
+/*
+ * Rpc additions to <sys/types.h>
+ */
+#ifndef _RPC_TYPES_H
+#define _RPC_TYPES_H 1
+
+typedef int bool_t;
+typedef int enum_t;
+/* This needs to be changed to uint32_t in the future */
+typedef unsigned long rpcprog_t;
+typedef unsigned long rpcvers_t;
+typedef unsigned long rpcproc_t;
+typedef unsigned long rpcprot_t;
+typedef unsigned long rpcport_t;
+
+#define __dontcare__ -1
+
+#ifndef FALSE
+# define FALSE (0)
+#endif
+
+#ifndef TRUE
+# define TRUE (1)
+#endif
+
+#ifndef NULL
+# define NULL 0
+#endif
+
+#include <stdlib.h> /* For malloc decl. */
+#define mem_alloc(bsize) malloc(bsize)
+/*
+ * XXX: This must not use the second argument, or code in xdr_array.c needs
+ * to be modified.
+ */
+#define mem_free(ptr, bsize) free(ptr)
+
+#ifndef makedev /* ie, we haven't already included it */
+#include <sys/types.h>
+#endif
+
+#ifndef __u_char_defined
+typedef __u_char u_char;
+typedef __u_short u_short;
+typedef __u_int u_int;
+typedef __u_long u_long;
+typedef __quad_t quad_t;
+typedef __u_quad_t u_quad_t;
+typedef __fsid_t fsid_t;
+# define __u_char_defined
+#endif
+#ifndef __daddr_t_defined
+typedef __daddr_t daddr_t;
+typedef __caddr_t caddr_t;
+# define __daddr_t_defined
+#endif
+
+#include <sys/time.h>
+#include <sys/param.h>
+
+#include <netinet/in.h>
+
+#ifndef INADDR_LOOPBACK
+#define INADDR_LOOPBACK (u_long)0x7F000001
+#endif
+#ifndef MAXHOSTNAMELEN
+#define MAXHOSTNAMELEN 64
+#endif
+
+#endif /* rpc/types.h */
--- /dev/null
+divert(-1)
+undefine(`len')
+#
+# append an underscore to FORTRAN function names
+#
+define(`FUNCTION',`$1_')
+#
+# FORTRAN character strings are passed as follows:
+# a pointer to the base of the string is passed in the normal
+# argument list, and the length is passed by value as an extra
+# argument, after all of the other arguments.
+#
+define(`ARGS',`($1`'undivert(1))')
+define(`SAVE',`divert(1)$1`'divert(0)')
+define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
+define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
+define(`STRING_LEN',`$1_len')
+define(`STRING_PTR',`$1_ptr')
+divert(0)
--- /dev/null
+# define INTUSE(name) name
+# define INTDEF(name)
+/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
+#endif
+
+/*
+ * xdr.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1986, Sun Microsystems, Inc.
+ *
+ * These are the "generic" xdr routines used to serialize and de-serialize
+ * most common data items. See xdr.h for more info on the interface to
+ * xdr.
+ */
+
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+#include <libintl.h>
+
+#include "types.h"
+#include "xdr.h"
+
+#ifdef USE_IN_LIBIO
+# include <wchar.h>
+#endif
+
+/*
+ * constants specific to the xdr "protocol"
+ */
+#define XDR_FALSE ((long) 0)
+#define XDR_TRUE ((long) 1)
+#define LASTUNSIGNED ((u_int) 0-1)
+
+/*
+ * for unit alignment
+ */
+static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
+
+/*
+ * Free a data structure using XDR
+ * Not a filter, but a convenient utility nonetheless
+ */
+void
+xdr_free (xdrproc_t proc, char *objp)
+{
+ XDR x;
+
+ x.x_op = XDR_FREE;
+ (*proc) (&x, objp);
+}
+
+/*
+ * XDR nothing
+ */
+bool_t
+xdr_void (void)
+{
+ return TRUE;
+}
+INTDEF(xdr_void)
+
+/*
+ * XDR integers
+ */
+bool_t
+xdr_int (XDR *xdrs, int *ip)
+{
+
+#if INT_MAX < LONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (long) *ip;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *ip = (int) l;
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+#elif INT_MAX == LONG_MAX
+ return INTUSE(xdr_long) (xdrs, (long *) ip);
+#elif INT_MAX == SHRT_MAX
+ return INTUSE(xdr_short) (xdrs, (short *) ip);
+#else
+#error unexpected integer sizes in_xdr_int()
+#endif
+}
+INTDEF(xdr_int)
+
+/*
+ * XDR unsigned integers
+ */
+bool_t
+xdr_u_int (XDR *xdrs, u_int *up)
+{
+#if UINT_MAX < ULONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (u_long) * up;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *up = (u_int) (u_long) l;
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+#elif UINT_MAX == ULONG_MAX
+ return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
+#elif UINT_MAX == USHRT_MAX
+ return INTUSE(xdr_short) (xdrs, (short *) up);
+#else
+#error unexpected integer sizes in_xdr_u_int()
+#endif
+}
+INTDEF(xdr_u_int)
+
+/*
+ * XDR long integers
+ * The definition of xdr_long() is kept for backward
+ * compatibility. Instead xdr_int() should be used.
+ */
+bool_t
+xdr_long (XDR *xdrs, long *lp)
+{
+
+ if (xdrs->x_op == XDR_ENCODE
+ && (sizeof (int32_t) == sizeof (long)
+ || (int32_t) *lp == *lp))
+ return XDR_PUTLONG (xdrs, lp);
+
+ if (xdrs->x_op == XDR_DECODE)
+ return XDR_GETLONG (xdrs, lp);
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_long)
+
+/*
+ * XDR unsigned long integers
+ * The definition of xdr_u_long() is kept for backward
+ * compatibility. Instead xdr_u_int() should be used.
+ */
+bool_t
+xdr_u_long (XDR *xdrs, u_long *ulp)
+{
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ {
+ long int tmp;
+
+ if (XDR_GETLONG (xdrs, &tmp) == FALSE)
+ return FALSE;
+
+ *ulp = (uint32_t) tmp;
+ return TRUE;
+ }
+
+ case XDR_ENCODE:
+ if (sizeof (uint32_t) != sizeof (u_long)
+ && (uint32_t) *ulp != *ulp)
+ return FALSE;
+
+ return XDR_PUTLONG (xdrs, (long *) ulp);
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_u_long)
+
+/*
+ * XDR hyper integers
+ * same as xdr_u_hyper - open coded to save a proc call!
+ */
+bool_t
+xdr_hyper (XDR *xdrs, quad_t *llp)
+{
+ long int t1, t2;
+
+ if (xdrs->x_op == XDR_ENCODE)
+ {
+ t1 = (long) ((*llp) >> 32);
+ t2 = (long) (*llp);
+ return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
+ }
+
+ if (xdrs->x_op == XDR_DECODE)
+ {
+ if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
+ return FALSE;
+ *llp = ((quad_t) t1) << 32;
+ *llp |= (uint32_t) t2;
+ return TRUE;
+ }
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_hyper)
+
+
+/*
+ * XDR hyper integers
+ * same as xdr_hyper - open coded to save a proc call!
+ */
+bool_t
+xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
+{
+ long int t1, t2;
+
+ if (xdrs->x_op == XDR_ENCODE)
+ {
+ t1 = (unsigned long) ((*ullp) >> 32);
+ t2 = (unsigned long) (*ullp);
+ return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
+ }
+
+ if (xdrs->x_op == XDR_DECODE)
+ {
+ if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
+ return FALSE;
+ *ullp = ((u_quad_t) t1) << 32;
+ *ullp |= (uint32_t) t2;
+ return TRUE;
+ }
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_u_hyper)
+
+bool_t
+xdr_longlong_t (XDR *xdrs, quad_t *llp)
+{
+ return INTUSE(xdr_hyper) (xdrs, llp);
+}
+
+bool_t
+xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
+{
+ return INTUSE(xdr_u_hyper) (xdrs, ullp);
+}
+
+/*
+ * XDR short integers
+ */
+bool_t
+xdr_short (XDR *xdrs, short *sp)
+{
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (long) *sp;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *sp = (short) l;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_short)
+
+/*
+ * XDR unsigned short integers
+ */
+bool_t
+xdr_u_short (XDR *xdrs, u_short *usp)
+{
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (u_long) * usp;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *usp = (u_short) (u_long) l;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_u_short)
+
+
+/*
+ * XDR a char
+ */
+bool_t
+xdr_char (XDR *xdrs, char *cp)
+{
+ int i;
+
+ i = (*cp);
+ if (!INTUSE(xdr_int) (xdrs, &i))
+ {
+ return FALSE;
+ }
+ *cp = i;
+ return TRUE;
+}
+
+/*
+ * XDR an unsigned char
+ */
+bool_t
+xdr_u_char (XDR *xdrs, u_char *cp)
+{
+ u_int u;
+
+ u = (*cp);
+ if (!INTUSE(xdr_u_int) (xdrs, &u))
+ {
+ return FALSE;
+ }
+ *cp = u;
+ return TRUE;
+}
+
+/*
+ * XDR booleans
+ */
+bool_t
+xdr_bool (XDR *xdrs, bool_t *bp)
+{
+ long lb;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ lb = *bp ? XDR_TRUE : XDR_FALSE;
+ return XDR_PUTLONG (xdrs, &lb);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &lb))
+ {
+ return FALSE;
+ }
+ *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_bool)
+
+/*
+ * XDR enumerations
+ */
+bool_t
+xdr_enum (XDR *xdrs, enum_t *ep)
+{
+ enum sizecheck
+ {
+ SIZEVAL
+ }; /* used to find the size of an enum */
+
+ /*
+ * enums are treated as ints
+ */
+ if (sizeof (enum sizecheck) == 4)
+ {
+#if INT_MAX < LONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = *ep;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *ep = l;
+ case XDR_FREE:
+ return TRUE;
+
+ }
+ return FALSE;
+#else
+ return INTUSE(xdr_long) (xdrs, (long *) ep);
+#endif
+ }
+ else if (sizeof (enum sizecheck) == sizeof (short))
+ {
+ return INTUSE(xdr_short) (xdrs, (short *) ep);
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+INTDEF(xdr_enum)
+
+/*
+ * XDR opaque data
+ * Allows the specification of a fixed size sequence of opaque bytes.
+ * cp points to the opaque object and cnt gives the byte length.
+ */
+bool_t
+xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
+{
+ u_int rndup;
+ static char crud[BYTES_PER_XDR_UNIT];
+
+ /*
+ * if no data we are done
+ */
+ if (cnt == 0)
+ return TRUE;
+
+ /*
+ * round byte count to full xdr units
+ */
+ rndup = cnt % BYTES_PER_XDR_UNIT;
+ if (rndup > 0)
+ rndup = BYTES_PER_XDR_UNIT - rndup;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (!XDR_GETBYTES (xdrs, cp, cnt))
+ {
+ return FALSE;
+ }
+ if (rndup == 0)
+ return TRUE;
+ return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
+
+ case XDR_ENCODE:
+ if (!XDR_PUTBYTES (xdrs, cp, cnt))
+ {
+ return FALSE;
+ }
+ if (rndup == 0)
+ return TRUE;
+ return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_opaque)
+
+/*
+ * XDR counted bytes
+ * *cpp is a pointer to the bytes, *sizep is the count.
+ * If *cpp is NULL maxsize bytes are allocated
+ */
+bool_t
+xdr_bytes (xdrs, cpp, sizep, maxsize)
+ XDR *xdrs;
+ char **cpp;
+ u_int *sizep;
+ u_int maxsize;
+{
+ char *sp = *cpp; /* sp is the actual string pointer */
+ u_int nodesize;
+
+ /*
+ * first deal with the length since xdr bytes are counted
+ */
+ if (!INTUSE(xdr_u_int) (xdrs, sizep))
+ {
+ return FALSE;
+ }
+ nodesize = *sizep;
+ if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
+ {
+ return FALSE;
+ }
+
+ /*
+ * now deal with the actual bytes
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (nodesize == 0)
+ {
+ return TRUE;
+ }
+ if (sp == NULL)
+ {
+ *cpp = sp = (char *) mem_alloc (nodesize);
+ }
+ if (sp == NULL)
+ {
+ fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
+ return FALSE;
+ }
+ /* fall into ... */
+
+ case XDR_ENCODE:
+ return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
+
+ case XDR_FREE:
+ if (sp != NULL)
+ {
+ mem_free (sp, nodesize);
+ *cpp = NULL;
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_bytes)
+
+/*
+ * Implemented here due to commonality of the object.
+ */
+bool_t
+xdr_netobj (xdrs, np)
+ XDR *xdrs;
+ struct netobj *np;
+{
+
+ return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
+}
+INTDEF(xdr_netobj)
+
+/*
+ * XDR a discriminated union
+ * Support routine for discriminated unions.
+ * You create an array of xdrdiscrim structures, terminated with
+ * an entry with a null procedure pointer. The routine gets
+ * the discriminant value and then searches the array of xdrdiscrims
+ * looking for that value. It calls the procedure given in the xdrdiscrim
+ * to handle the discriminant. If there is no specific routine a default
+ * routine may be called.
+ * If there is no specific or default routine an error is returned.
+ */
+bool_t
+xdr_union (xdrs, dscmp, unp, choices, dfault)
+ XDR *xdrs;
+ enum_t *dscmp; /* enum to decide which arm to work on */
+ char *unp; /* the union itself */
+ const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
+ xdrproc_t dfault; /* default xdr routine */
+{
+ enum_t dscm;
+
+ /*
+ * we deal with the discriminator; it's an enum
+ */
+ if (!INTUSE(xdr_enum) (xdrs, dscmp))
+ {
+ return FALSE;
+ }
+ dscm = *dscmp;
+
+ /*
+ * search choices for a value that matches the discriminator.
+ * if we find one, execute the xdr routine for that value.
+ */
+ for (; choices->proc != NULL_xdrproc_t; choices++)
+ {
+ if (choices->value == dscm)
+ return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
+ }
+
+ /*
+ * no match - execute the default xdr routine if there is one
+ */
+ return ((dfault == NULL_xdrproc_t) ? FALSE :
+ (*dfault) (xdrs, unp, LASTUNSIGNED));
+}
+INTDEF(xdr_union)
+
+
+/*
+ * Non-portable xdr primitives.
+ * Care should be taken when moving these routines to new architectures.
+ */
+
+
+/*
+ * XDR null terminated ASCII strings
+ * xdr_string deals with "C strings" - arrays of bytes that are
+ * terminated by a NULL character. The parameter cpp references a
+ * pointer to storage; If the pointer is null, then the necessary
+ * storage is allocated. The last parameter is the max allowed length
+ * of the string as specified by a protocol.
+ */
+bool_t
+xdr_string (xdrs, cpp, maxsize)
+ XDR *xdrs;
+ char **cpp;
+ u_int maxsize;
+{
+ char *sp = *cpp; /* sp is the actual string pointer */
+ u_int size;
+ u_int nodesize;
+
+ /*
+ * first deal with the length since xdr strings are counted-strings
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_FREE:
+ if (sp == NULL)
+ {
+ return TRUE; /* already free */
+ }
+ /* fall through... */
+ case XDR_ENCODE:
+ if (sp == NULL)
+ return FALSE;
+ size = strlen (sp);
+ break;
+ case XDR_DECODE:
+ break;
+ }
+ if (!INTUSE(xdr_u_int) (xdrs, &size))
+ {
+ return FALSE;
+ }
+ if (size > maxsize)
+ {
+ return FALSE;
+ }
+ nodesize = size + 1;
+ if (nodesize == 0)
+ {
+ /* This means an overflow. It a bug in the caller which
+ provided a too large maxsize but nevertheless catch it
+ here. */
+ return FALSE;
+ }
+
+ /*
+ * now deal with the actual bytes
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (sp == NULL)
+ *cpp = sp = (char *) mem_alloc (nodesize);
+ if (sp == NULL)
+ {
+ fprintf (NULL, "%s", "xdr_string: out of memory\n");
+ return FALSE;
+ }
+ sp[size] = 0;
+ /* fall into ... */
+
+ case XDR_ENCODE:
+ return INTUSE(xdr_opaque) (xdrs, sp, size);
+
+ case XDR_FREE:
+ mem_free (sp, nodesize);
+ *cpp = NULL;
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_string)
+
+/*
+ * Wrapper for xdr_string that can be called directly from
+ * routines like clnt_call
+ */
+bool_t
+xdr_wrapstring (xdrs, cpp)
+ XDR *xdrs;
+ char **cpp;
+{
+ if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
+ {
+ return TRUE;
+ }
+ return FALSE;
+}
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * xdr.h, External Data Representation Serialization Routines.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ */
+
+#ifndef _RPC_XDR_H
+#define _RPC_XDR_H 1
+
+#include <features.h>
+#include <sys/types.h>
+#include "types.h"
+
+/* We need FILE. */
+#include <stdio.h>
+
+__BEGIN_DECLS
+
+/*
+ * XDR provides a conventional way for converting between C data
+ * types and an external bit-string representation. Library supplied
+ * routines provide for the conversion on built-in C data types. These
+ * routines and utility routines defined here are used to help implement
+ * a type encode/decode routine for each user-defined type.
+ *
+ * Each data type provides a single procedure which takes two arguments:
+ *
+ * bool_t
+ * xdrproc(xdrs, argresp)
+ * XDR *xdrs;
+ * <type> *argresp;
+ *
+ * xdrs is an instance of a XDR handle, to which or from which the data
+ * type is to be converted. argresp is a pointer to the structure to be
+ * converted. The XDR handle contains an operation field which indicates
+ * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
+ *
+ * XDR_DECODE may allocate space if the pointer argresp is null. This
+ * data can be freed with the XDR_FREE operation.
+ *
+ * We write only one procedure per data type to make it easy
+ * to keep the encode and decode procedures for a data type consistent.
+ * In many cases the same code performs all operations on a user defined type,
+ * because all the hard work is done in the component type routines.
+ * decode as a series of calls on the nested data types.
+ */
+
+/*
+ * Xdr operations. XDR_ENCODE causes the type to be encoded into the
+ * stream. XDR_DECODE causes the type to be extracted from the stream.
+ * XDR_FREE can be used to release the space allocated by an XDR_DECODE
+ * request.
+ */
+enum xdr_op {
+ XDR_ENCODE = 0,
+ XDR_DECODE = 1,
+ XDR_FREE = 2
+};
+
+/*
+ * This is the number of bytes per unit of external data.
+ */
+#define BYTES_PER_XDR_UNIT (4)
+/*
+ * This only works if the above is a power of 2. But it's defined to be
+ * 4 by the appropriate RFCs. So it will work. And it's normally quicker
+ * than the old routine.
+ */
+#if 1
+#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
+#else /* this is the old routine */
+#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
+ * BYTES_PER_XDR_UNIT)
+#endif
+
+/*
+ * The XDR handle.
+ * Contains operation which is being applied to the stream,
+ * an operations vector for the particular implementation (e.g. see xdr_mem.c),
+ * and two private fields for the use of the particular implementation.
+ */
+typedef struct XDR XDR;
+struct XDR
+ {
+ enum xdr_op x_op; /* operation; fast additional param */
+ struct xdr_ops
+ {
+ bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
+ /* get a long from underlying stream */
+ bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
+ /* put a long to " */
+ bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
+ /* get some bytes from " */
+ bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
+ /* put some bytes to " */
+ u_int (*x_getpostn) (__const XDR *__xdrs);
+ /* returns bytes off from beginning */
+ bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
+ /* lets you reposition the stream */
+ int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
+ /* buf quick ptr to buffered data */
+ void (*x_destroy) (XDR *__xdrs);
+ /* free privates of this xdr_stream */
+ bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
+ /* get a int from underlying stream */
+ bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
+ /* put a int to " */
+ }
+ *x_ops;
+ caddr_t x_public; /* users' data */
+ caddr_t x_private; /* pointer to private data */
+ caddr_t x_base; /* private used for position info */
+ u_int x_handy; /* extra private word */
+ };
+
+/*
+ * A xdrproc_t exists for each data type which is to be encoded or decoded.
+ *
+ * The second argument to the xdrproc_t is a pointer to an opaque pointer.
+ * The opaque pointer generally points to a structure of the data type
+ * to be decoded. If this pointer is 0, then the type routines should
+ * allocate dynamic storage of the appropriate size and return it.
+ * bool_t (*xdrproc_t)(XDR *, caddr_t *);
+ */
+typedef bool_t (*xdrproc_t) (XDR *, void *,...);
+
+
+/*
+ * Operations defined on a XDR handle
+ *
+ * XDR *xdrs;
+ * int32_t *int32p;
+ * long *longp;
+ * caddr_t addr;
+ * u_int len;
+ * u_int pos;
+ */
+#define XDR_GETINT32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
+#define xdr_getint32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
+
+#define XDR_PUTINT32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
+#define xdr_putint32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
+
+#define XDR_GETLONG(xdrs, longp) \
+ (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
+#define xdr_getlong(xdrs, longp) \
+ (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
+
+#define XDR_PUTLONG(xdrs, longp) \
+ (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
+#define xdr_putlong(xdrs, longp) \
+ (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
+
+#define XDR_GETBYTES(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
+#define xdr_getbytes(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
+
+#define XDR_PUTBYTES(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
+#define xdr_putbytes(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
+
+#define XDR_GETPOS(xdrs) \
+ (*(xdrs)->x_ops->x_getpostn)(xdrs)
+#define xdr_getpos(xdrs) \
+ (*(xdrs)->x_ops->x_getpostn)(xdrs)
+
+#define XDR_SETPOS(xdrs, pos) \
+ (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
+#define xdr_setpos(xdrs, pos) \
+ (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
+
+#define XDR_INLINE(xdrs, len) \
+ (*(xdrs)->x_ops->x_inline)(xdrs, len)
+#define xdr_inline(xdrs, len) \
+ (*(xdrs)->x_ops->x_inline)(xdrs, len)
+
+#define XDR_DESTROY(xdrs) \
+ do { \
+ if ((xdrs)->x_ops->x_destroy) \
+ (*(xdrs)->x_ops->x_destroy)(xdrs); \
+ } while (0)
+#define xdr_destroy(xdrs) \
+ do { \
+ if ((xdrs)->x_ops->x_destroy) \
+ (*(xdrs)->x_ops->x_destroy)(xdrs); \
+ } while (0)
+
+/*
+ * Support struct for discriminated unions.
+ * You create an array of xdrdiscrim structures, terminated with
+ * a entry with a null procedure pointer. The xdr_union routine gets
+ * the discriminant value and then searches the array of structures
+ * for a matching value. If a match is found the associated xdr routine
+ * is called to handle that part of the union. If there is
+ * no match, then a default routine may be called.
+ * If there is no match and no default routine it is an error.
+ */
+#define NULL_xdrproc_t ((xdrproc_t)0)
+struct xdr_discrim
+{
+ int value;
+ xdrproc_t proc;
+};
+
+/*
+ * Inline routines for fast encode/decode of primitive data types.
+ * Caveat emptor: these use single memory cycles to get the
+ * data from the underlying buffer, and will fail to operate
+ * properly if the data is not aligned. The standard way to use these
+ * is to say:
+ * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
+ * return (FALSE);
+ * <<< macro calls >>>
+ * where ``count'' is the number of bytes of data occupied
+ * by the primitive data types.
+ *
+ * N.B. and frozen for all time: each data type here uses 4 bytes
+ * of external representation.
+ */
+
+#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
+#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
+#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
+#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
+
+/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
+ * and shouldn't be used any longer. Code which use this defines or longs
+ * in the RPC code will not work on 64bit Solaris platforms !
+ */
+#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
+#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
+#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
+#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+
+
+#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
+#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
+#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
+#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
+
+#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+
+/*
+ * These are the "generic" xdr routines.
+ * None of these can have const applied because it's not possible to
+ * know whether the call is a read or a write to the passed parameter
+ * also, the XDR structure is always updated by some of these calls.
+ */
+extern bool_t xdr_void (void) __THROW;
+extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
+extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
+extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
+extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
+extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
+extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
+extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
+extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
+extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
+extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
+extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
+extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
+extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
+extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
+extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
+extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
+extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
+extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
+extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
+extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
+extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
+extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
+extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
+ u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
+ __THROW;
+extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
+ u_int __maxsize) __THROW;
+extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
+extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
+extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
+ __const struct xdr_discrim *__choices,
+ xdrproc_t dfault) __THROW;
+extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
+extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
+extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
+ u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
+extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
+extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
+extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
+ xdrproc_t __proc) __THROW;
+extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
+ u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
+extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
+extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
+
+/*
+ * Common opaque bytes objects used by many rpc protocols;
+ * declared here due to commonality.
+ */
+#define MAX_NETOBJ_SZ 1024
+struct netobj
+{
+ u_int n_len;
+ char *n_bytes;
+};
+typedef struct netobj netobj;
+extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
+
+/*
+ * These are the public routines for the various implementations of
+ * xdr streams.
+ */
+
+/* XDR using memory buffers */
+extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
+ u_int __size, enum xdr_op __xop) __THROW;
+
+/* XDR using stdio library */
+extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
+ __THROW;
+
+/* XDR pseudo records for tcp */
+extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
+ u_int __recvsize, caddr_t __tcp_handle,
+ int (*__readit) (char *, char *, int),
+ int (*__writeit) (char *, char *, int)) __THROW;
+
+/* make end of xdr record */
+extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
+
+/* move to beginning of next record */
+extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
+
+/* true if no more input */
+extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
+
+/* free memory buffers for xdr */
+extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
+
+__END_DECLS
+
+#endif /* rpc/xdr.h */
--- /dev/null
+# define INTUSE(name) name
+# define INTDEF(name)
+/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
+#endif
+
+/*
+ * xdr_array.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * These are the "non-trivial" xdr primitives used to serialize and de-serialize
+ * arrays. See xdr.h for more info on the interface to xdr.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "types.h"
+#include "xdr.h"
+#include <libintl.h>
+#include <limits.h>
+
+#ifdef USE_IN_LIBIO
+# include <wchar.h>
+#endif
+
+#define LASTUNSIGNED ((u_int)0-1)
+
+
+/*
+ * XDR an array of arbitrary elements
+ * *addrp is a pointer to the array, *sizep is the number of elements.
+ * If addrp is NULL (*sizep * elsize) bytes are allocated.
+ * elsize is the size (in bytes) of each element, and elproc is the
+ * xdr procedure to call to handle each element of the array.
+ */
+bool_t
+xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
+ XDR *xdrs;
+ caddr_t *addrp; /* array pointer */
+ u_int *sizep; /* number of elements */
+ u_int maxsize; /* max numberof elements */
+ u_int elsize; /* size in bytes of each element */
+ xdrproc_t elproc; /* xdr routine to handle each element */
+{
+ u_int i;
+ caddr_t target = *addrp;
+ u_int c; /* the actual element count */
+ bool_t stat = TRUE;
+ u_int nodesize;
+
+ /* like strings, arrays are really counted arrays */
+ if (!INTUSE(xdr_u_int) (xdrs, sizep))
+ {
+ return FALSE;
+ }
+ c = *sizep;
+ /*
+ * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
+ * doesn't actually use its second argument anyway.
+ */
+ if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
+ {
+ return FALSE;
+ }
+ nodesize = c * elsize;
+
+ /*
+ * if we are deserializing, we may need to allocate an array.
+ * We also save time by checking for a null array if we are freeing.
+ */
+ if (target == NULL)
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (c == 0)
+ return TRUE;
+ *addrp = target = mem_alloc (nodesize);
+ if (target == NULL)
+ {
+ fprintf (stderr, "%s", "xdr_array: out of memory\n");
+ return FALSE;
+ }
+ __bzero (target, nodesize);
+ break;
+
+ case XDR_FREE:
+ return TRUE;
+ default:
+ break;
+ }
+
+ /*
+ * now we xdr each element of array
+ */
+ for (i = 0; (i < c) && stat; i++)
+ {
+ stat = (*elproc) (xdrs, target, LASTUNSIGNED);
+ target += elsize;
+ }
+
+ /*
+ * the array may need freeing
+ */
+ if (xdrs->x_op == XDR_FREE)
+ {
+ mem_free (*addrp, nodesize);
+ *addrp = NULL;
+ }
+ return stat;
+}
+INTDEF(xdr_array)
+
+/*
+ * xdr_vector():
+ *
+ * XDR a fixed length array. Unlike variable-length arrays,
+ * the storage of fixed length arrays is static and unfreeable.
+ * > basep: base of the array
+ * > size: size of the array
+ * > elemsize: size of each element
+ * > xdr_elem: routine to XDR each element
+ */
+bool_t
+xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
+ XDR *xdrs;
+ char *basep;
+ u_int nelem;
+ u_int elemsize;
+ xdrproc_t xdr_elem;
+{
+ u_int i;
+ char *elptr;
+
+ elptr = basep;
+ for (i = 0; i < nelem; i++)
+ {
+ if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
+ {
+ return FALSE;
+ }
+ elptr += elemsize;
+ }
+ return TRUE;
+}
--- /dev/null
+/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
+#endif
+
+/*
+ * xdr_float.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * These are the "floating point" xdr routines used to (de)serialize
+ * most common data items. See xdr.h for more info on the interface to
+ * xdr.
+ */
+
+#include <stdio.h>
+#include <endian.h>
+
+#include "types.h"
+#include "xdr.h"
+
+/*
+ * NB: Not portable.
+ * This routine works on Suns (Sky / 68000's) and Vaxen.
+ */
+
+#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
+
+#ifdef vax
+
+/* What IEEE single precision floating point looks like on a Vax */
+struct ieee_single {
+ unsigned int mantissa: 23;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+};
+
+/* Vax single precision floating point */
+struct vax_single {
+ unsigned int mantissa1 : 7;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 16;
+};
+
+#define VAX_SNG_BIAS 0x81
+#define IEEE_SNG_BIAS 0x7f
+
+static struct sgl_limits {
+ struct vax_single s;
+ struct ieee_single ieee;
+} sgl_limits[2] = {
+ {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
+ { 0x0, 0xff, 0x0 }}, /* Max IEEE */
+ {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
+ { 0x0, 0x0, 0x0 }} /* Min IEEE */
+};
+#endif /* vax */
+
+bool_t
+xdr_float(xdrs, fp)
+ XDR *xdrs;
+ float *fp;
+{
+#ifdef vax
+ struct ieee_single is;
+ struct vax_single vs, *vsp;
+ struct sgl_limits *lim;
+ int i;
+#endif
+ switch (xdrs->x_op) {
+
+ case XDR_ENCODE:
+#ifdef vax
+ vs = *((struct vax_single *)fp);
+ for (i = 0, lim = sgl_limits;
+ i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
+ i++, lim++) {
+ if ((vs.mantissa2 == lim->s.mantissa2) &&
+ (vs.exp == lim->s.exp) &&
+ (vs.mantissa1 == lim->s.mantissa1)) {
+ is = lim->ieee;
+ goto shipit;
+ }
+ }
+ is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
+ is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
+ shipit:
+ is.sign = vs.sign;
+ return (XDR_PUTLONG(xdrs, (long *)&is));
+#else
+ if (sizeof(float) == sizeof(long))
+ return (XDR_PUTLONG(xdrs, (long *)fp));
+ else if (sizeof(float) == sizeof(int)) {
+ long tmp = *(int *)fp;
+ return (XDR_PUTLONG(xdrs, &tmp));
+ }
+ break;
+#endif
+
+ case XDR_DECODE:
+#ifdef vax
+ vsp = (struct vax_single *)fp;
+ if (!XDR_GETLONG(xdrs, (long *)&is))
+ return (FALSE);
+ for (i = 0, lim = sgl_limits;
+ i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
+ i++, lim++) {
+ if ((is.exp == lim->ieee.exp) &&
+ (is.mantissa == lim->ieee.mantissa)) {
+ *vsp = lim->s;
+ goto doneit;
+ }
+ }
+ vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
+ vsp->mantissa2 = is.mantissa;
+ vsp->mantissa1 = (is.mantissa >> 16);
+ doneit:
+ vsp->sign = is.sign;
+ return (TRUE);
+#else
+ if (sizeof(float) == sizeof(long))
+ return (XDR_GETLONG(xdrs, (long *)fp));
+ else if (sizeof(float) == sizeof(int)) {
+ long tmp;
+ if (XDR_GETLONG(xdrs, &tmp)) {
+ *(int *)fp = tmp;
+ return (TRUE);
+ }
+ }
+ break;
+#endif
+
+ case XDR_FREE:
+ return (TRUE);
+ }
+ return (FALSE);
+}
+
+/*
+ * This routine works on Suns (Sky / 68000's) and Vaxen.
+ */
+
+#ifdef vax
+/* What IEEE double precision floating point looks like on a Vax */
+struct ieee_double {
+ unsigned int mantissa1 : 20;
+ unsigned int exp : 11;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 32;
+};
+
+/* Vax double precision floating point */
+struct vax_double {
+ unsigned int mantissa1 : 7;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 16;
+ unsigned int mantissa3 : 16;
+ unsigned int mantissa4 : 16;
+};
+
+#define VAX_DBL_BIAS 0x81
+#define IEEE_DBL_BIAS 0x3ff
+#define MASK(nbits) ((1 << nbits) - 1)
+
+static struct dbl_limits {
+ struct vax_double d;
+ struct ieee_double ieee;
+} dbl_limits[2] = {
+ {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
+ { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
+ {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
+ { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
+};
+
+#endif /* vax */
+
+
+bool_t
+xdr_double(xdrs, dp)
+ XDR *xdrs;
+ double *dp;
+{
+#ifdef vax
+ struct ieee_double id;
+ struct vax_double vd;
+ register struct dbl_limits *lim;
+ int i;
+#endif
+
+ switch (xdrs->x_op) {
+
+ case XDR_ENCODE:
+#ifdef vax
+ vd = *((struct vax_double *)dp);
+ for (i = 0, lim = dbl_limits;
+ i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
+ i++, lim++) {
+ if ((vd.mantissa4 == lim->d.mantissa4) &&
+ (vd.mantissa3 == lim->d.mantissa3) &&
+ (vd.mantissa2 == lim->d.mantissa2) &&
+ (vd.mantissa1 == lim->d.mantissa1) &&
+ (vd.exp == lim->d.exp)) {
+ id = lim->ieee;
+ goto shipit;
+ }
+ }
+ id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
+ id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
+ id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
+ (vd.mantissa3 << 13) |
+ ((vd.mantissa4 >> 3) & MASK(13));
+ shipit:
+ id.sign = vd.sign;
+ dp = (double *)&id;
+#endif
+ if (2*sizeof(long) == sizeof(double)) {
+ long *lp = (long *)dp;
+ return (XDR_PUTLONG(xdrs, lp+!LSW) &&
+ XDR_PUTLONG(xdrs, lp+LSW));
+ } else if (2*sizeof(int) == sizeof(double)) {
+ int *ip = (int *)dp;
+ long tmp[2];
+ tmp[0] = ip[!LSW];
+ tmp[1] = ip[LSW];
+ return (XDR_PUTLONG(xdrs, tmp) &&
+ XDR_PUTLONG(xdrs, tmp+1));
+ }
+ break;
+
+ case XDR_DECODE:
+#ifdef vax
+ lp = (long *)&id;
+ if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
+ return (FALSE);
+ for (i = 0, lim = dbl_limits;
+ i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
+ i++, lim++) {
+ if ((id.mantissa2 == lim->ieee.mantissa2) &&
+ (id.mantissa1 == lim->ieee.mantissa1) &&
+ (id.exp == lim->ieee.exp)) {
+ vd = lim->d;
+ goto doneit;
+ }
+ }
+ vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
+ vd.mantissa1 = (id.mantissa1 >> 13);
+ vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
+ (id.mantissa2 >> 29);
+ vd.mantissa3 = (id.mantissa2 >> 13);
+ vd.mantissa4 = (id.mantissa2 << 3);
+ doneit:
+ vd.sign = id.sign;
+ *dp = *((double *)&vd);
+ return (TRUE);
+#else
+ if (2*sizeof(long) == sizeof(double)) {
+ long *lp = (long *)dp;
+ return (XDR_GETLONG(xdrs, lp+!LSW) &&
+ XDR_GETLONG(xdrs, lp+LSW));
+ } else if (2*sizeof(int) == sizeof(double)) {
+ int *ip = (int *)dp;
+ long tmp[2];
+ if (XDR_GETLONG(xdrs, tmp+!LSW) &&
+ XDR_GETLONG(xdrs, tmp+LSW)) {
+ ip[0] = tmp[0];
+ ip[1] = tmp[1];
+ return (TRUE);
+ }
+ }
+ break;
+#endif
+
+ case XDR_FREE:
+ return (TRUE);
+ }
+ return (FALSE);
+}
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * xdr_stdio.c, XDR implementation on standard i/o file.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * This set of routines implements a XDR on a stdio stream.
+ * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
+ * from the stream.
+ */
+
+#include "types.h"
+#include <stdio.h>
+#include "xdr.h"
+
+#ifdef USE_IN_LIBIO
+# include <libio/iolibio.h>
+# define fflush(s) INTUSE(_IO_fflush) (s)
+# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
+# define ftell(s) INTUSE(_IO_ftell) (s)
+# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
+#endif
+
+static bool_t xdrstdio_getlong (XDR *, long *);
+static bool_t xdrstdio_putlong (XDR *, const long *);
+static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
+static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
+static u_int xdrstdio_getpos (const XDR *);
+static bool_t xdrstdio_setpos (XDR *, u_int);
+static int32_t *xdrstdio_inline (XDR *, u_int);
+static void xdrstdio_destroy (XDR *);
+static bool_t xdrstdio_getint32 (XDR *, int32_t *);
+static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
+
+/*
+ * Ops vector for stdio type XDR
+ */
+static const struct xdr_ops xdrstdio_ops =
+{
+ xdrstdio_getlong, /* deserialize a long int */
+ xdrstdio_putlong, /* serialize a long int */
+ xdrstdio_getbytes, /* deserialize counted bytes */
+ xdrstdio_putbytes, /* serialize counted bytes */
+ xdrstdio_getpos, /* get offset in the stream */
+ xdrstdio_setpos, /* set offset in the stream */
+ xdrstdio_inline, /* prime stream for inline macros */
+ xdrstdio_destroy, /* destroy stream */
+ xdrstdio_getint32, /* deserialize a int */
+ xdrstdio_putint32 /* serialize a int */
+};
+
+/*
+ * Initialize a stdio xdr stream.
+ * Sets the xdr stream handle xdrs for use on the stream file.
+ * Operation flag is set to op.
+ */
+void
+xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
+{
+ xdrs->x_op = op;
+ /* We have to add the const since the `struct xdr_ops' in `struct XDR'
+ is not `const'. */
+ xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
+ xdrs->x_private = (caddr_t) file;
+ xdrs->x_handy = 0;
+ xdrs->x_base = 0;
+}
+
+/*
+ * Destroy a stdio xdr stream.
+ * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
+ */
+static void
+xdrstdio_destroy (XDR *xdrs)
+{
+ (void) fflush ((FILE *) xdrs->x_private);
+ /* xx should we close the file ?? */
+};
+
+static bool_t
+xdrstdio_getlong (XDR *xdrs, long *lp)
+{
+ u_int32_t mycopy;
+
+ if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ *lp = (long) ntohl (mycopy);
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putlong (XDR *xdrs, const long *lp)
+{
+ int32_t mycopy = htonl ((u_int32_t) *lp);
+
+ if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
+{
+ if ((len != 0) && (fread (addr, (int) len, 1,
+ (FILE *) xdrs->x_private) != 1))
+ return FALSE;
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
+{
+ if ((len != 0) && (fwrite (addr, (int) len, 1,
+ (FILE *) xdrs->x_private) != 1))
+ return FALSE;
+ return TRUE;
+}
+
+static u_int
+xdrstdio_getpos (const XDR *xdrs)
+{
+ return (u_int) ftell ((FILE *) xdrs->x_private);
+}
+
+static bool_t
+xdrstdio_setpos (XDR *xdrs, u_int pos)
+{
+ return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
+}
+
+static int32_t *
+xdrstdio_inline (XDR *xdrs, u_int len)
+{
+ /*
+ * Must do some work to implement this: must insure
+ * enough data in the underlying stdio buffer,
+ * that the buffer is aligned so that we can indirect through a
+ * long *, and stuff this pointer in xdrs->x_buf. Doing
+ * a fread or fwrite to a scratch buffer would defeat
+ * most of the gains to be had here and require storage
+ * management on this buffer, so we don't do this.
+ */
+ return NULL;
+}
+
+static bool_t
+xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
+{
+ int32_t mycopy;
+
+ if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ *ip = ntohl (mycopy);
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
+{
+ int32_t mycopy = htonl (*ip);
+
+ ip = &mycopy;
+ if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ return TRUE;
+}
+
+/* libc_hidden_def (xdrstdio_create) */
--- /dev/null
+/*_________________________________________________________________
+ |
+ | xdrf.h - include file for C routines that want to use the
+ | functions below.
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type);
+int xdrclose(XDR *xdrs) ;
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
+
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+CC = cc
+CFLAGS = -O
+
+M4 = m4
+M4FILE = underscore.m4
+
+libxdrf.a: libxdrf.o ftocstr.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f libxdrf.o ftocstr.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+ rm -f libxdrf.c
+
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
+
+CC = /usr/bin/blrts_xlc
+CPPC = /usr/bin/blrts_xlc
+
+CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
+
+M4 = m4
+M4FILE = RS6K.m4
+
+libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f *.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+# rm -f libxdrf.c
+
--- /dev/null
+# This make file is part of the xdrf package.
+#
+# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+#
+# 2006 modified by Cezary Czaplewski
+
+# Set C compiler and flags for ARCH
+CC = cc
+CFLAGS = -O
+
+M4 = m4
+M4FILE = underscore.m4
+
+libxdrf.a: libxdrf.o ftocstr.o
+ ar cr libxdrf.a $?
+
+clean:
+ rm -f libxdrf.o ftocstr.o libxdrf.a
+
+ftocstr.o: ftocstr.c
+ $(CC) $(CFLAGS) -c ftocstr.c
+
+libxdrf.o: libxdrf.m4 $(M4FILE)
+ $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
+ $(CC) $(CFLAGS) -c libxdrf.c
+ rm -f libxdrf.c
+
--- /dev/null
+divert(-1)
+undefine(`len')
+#
+# do nothing special to FORTRAN function names
+#
+define(`FUNCTION',`$1')
+#
+# FORTRAN character strings are passed as follows:
+# a pointer to the base of the string is passed in the normal
+# argument list, and the length is passed by value as an extra
+# argument, after all of the other arguments.
+#
+define(`ARGS',`($1`'undivert(1))')
+define(`SAVE',`divert(1)$1`'divert(0)')
+define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
+define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
+define(`STRING_LEN',`$1_len')
+define(`STRING_PTR',`$1_ptr')
+divert(0)
+
--- /dev/null
+
+
+int ftocstr(ds, dl, ss, sl)
+ char *ds, *ss; /* dst, src ptrs */
+ int dl; /* dst max len */
+ int sl; /* src len */
+{
+ char *p;
+
+ for (p = ss + sl; --p >= ss && *p == ' '; ) ;
+ sl = p - ss + 1;
+ dl--;
+ ds[0] = 0;
+ if (sl > dl)
+ return 1;
+ while (sl--)
+ (*ds++ = *ss++);
+ *ds = '\0';
+ return 0;
+}
+
+
+int ctofstr(ds, dl, ss)
+ char *ds; /* dest space */
+ int dl; /* max dest length */
+ char *ss; /* src string (0-term) */
+{
+ while (dl && *ss) {
+ *ds++ = *ss++;
+ dl--;
+ }
+ while (dl--)
+ *ds++ = ' ';
+ return 0;
+}
--- /dev/null
+/*____________________________________________________________________________
+ |
+ | libxdrf - portable fortran interface to xdr. some xdr routines
+ | are C routines for compressed coordinates
+ |
+ | version 1.1
+ |
+ | This collection of routines is intended to write and read
+ | data in a portable way to a file, so data written on one type
+ | of machine can be read back on a different type.
+ |
+ | all fortran routines use an integer 'xdrid', which is an id to the
+ | current xdr file, and is set by xdrfopen.
+ | most routines have in integer 'ret' which is the return value.
+ | The value of 'ret' is zero on failure, and most of the time one
+ | on succes.
+ |
+ | There are three routines useful for C users:
+ | xdropen(), xdrclose(), xdr3dfcoord().
+ | The first two replace xdrstdio_create and xdr_destroy, and *must* be
+ | used when you plan to use xdr3dfcoord(). (they are also a bit
+ | easier to interface). For writing data other than compressed coordinates
+ | you should use the standard C xdr routines (see xdr man page)
+ |
+ | xdrfopen(xdrid, filename, mode, ret)
+ | character *(*) filename
+ | character *(*) mode
+ |
+ | this will open the file with the given filename (string)
+ | and the given mode, it returns an id in xdrid, which is
+ | to be used in all other calls to xdrf routines.
+ | mode is 'w' to create, or update an file, for all other
+ | values of mode the file is opened for reading
+ |
+ | you need to call xdrfclose to flush the output and close
+ | the file.
+ | Note that you should not use xdrstdio_create, which comes with the
+ | standard xdr library
+ |
+ | xdrfclose(xdrid, ret)
+ | flush the data to the file, and closes the file;
+ | You should not use xdr_destroy (which comes standard with
+ | the xdr libraries.
+ |
+ | xdrfbool(xdrid, bp, ret)
+ | integer pb
+ |
+ | This filter produces values of either 1 or 0
+ |
+ | xdrfchar(xdrid, cp, ret)
+ | character cp
+ |
+ | filter that translate between characters and their xdr representation
+ | Note that the characters in not compressed and occupies 4 bytes.
+ |
+ | xdrfdouble(xdrid, dp, ret)
+ | double dp
+ |
+ | read/write a double.
+ |
+ | xdrffloat(xdrid, fp, ret)
+ | float fp
+ |
+ | read/write a float.
+ |
+ | xdrfint(xdrid, ip, ret)
+ | integer ip
+ |
+ | read/write integer.
+ |
+ | xdrflong(xdrid, lp, ret)
+ | integer lp
+ |
+ | this routine has a possible portablility problem due to 64 bits longs.
+ |
+ | xdrfshort(xdrid, sp, ret)
+ | integer *2 sp
+ |
+ | xdrfstring(xdrid, sp, maxsize, ret)
+ | character *(*)
+ | integer maxsize
+ |
+ | read/write a string, with maximum length given by maxsize
+ |
+ | xdrfwrapstring(xdris, sp, ret)
+ | character *(*)
+ |
+ | read/write a string (it is the same as xdrfstring accept that it finds
+ | the stringlength itself.
+ |
+ | xdrfvector(xdrid, cp, size, xdrfproc, ret)
+ | character *(*)
+ | integer size
+ | external xdrfproc
+ |
+ | read/write an array pointed to by cp, with number of elements
+ | defined by 'size'. the routine 'xdrfproc' is the name
+ | of one of the above routines to read/write data (like xdrfdouble)
+ | In contrast with the c-version you don't need to specify the
+ | byte size of an element.
+ | xdrfstring is not allowed here (it is in the c version)
+ |
+ | xdrf3dfcoord(xdrid, fp, size, precision, ret)
+ | real (*) fp
+ | real precision
+ | integer size
+ |
+ | this is *NOT* a standard xdr routine. I named it this way, because
+ | it invites people to use the other xdr routines.
+ | It is introduced to store specifically 3d coordinates of molecules
+ | (as found in molecular dynamics) and it writes it in a compressed way.
+ | It starts by multiplying all numbers by precision and
+ | rounding the result to integer. effectively converting
+ | all floating point numbers to fixed point.
+ | it uses an algorithm for compression that is optimized for
+ | molecular data, but could be used for other 3d coordinates
+ | as well. There is subtantial overhead involved, so call this
+ | routine only if you have a large number of coordinates to read/write
+ |
+ | ________________________________________________________________________
+ |
+ | Below are the routines to be used by C programmers. Use the 'normal'
+ | xdr routines to write integers, floats, etc (see man xdr)
+ |
+ | int xdropen(XDR *xdrs, const char *filename, const char *type)
+ | This will open the file with the given filename and the
+ | given mode. You should pass it an allocated XDR struct
+ | in xdrs, to be used in all other calls to xdr routines.
+ | Mode is 'w' to create, or update an file, and for all
+ | other values of mode the file is opened for reading.
+ | You need to call xdrclose to flush the output and close
+ | the file.
+ |
+ | Note that you should not use xdrstdio_create, which
+ | comes with the standard xdr library.
+ |
+ | int xdrclose(XDR *xdrs)
+ | Flush the data to the file, and close the file;
+ | You should not use xdr_destroy (which comes standard
+ | with the xdr libraries).
+ |
+ | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
+ | This is \fInot\fR a standard xdr routine. I named it this
+ | way, because it invites people to use the other xdr
+ | routines.
+ |
+ | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+*/
+
+
+#include <limits.h>
+#include <malloc.h>
+#include <math.h>
+/* #include <rpc/rpc.h>
+#include <rpc/xdr.h> */
+#include "xdr.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "xdrf.h"
+
+int ftocstr(char *, int, char *, int);
+int ctofstr(char *, int, char *);
+
+#define MAXID 20
+static FILE *xdrfiles[MAXID];
+static XDR *xdridptr[MAXID];
+static char xdrmodes[MAXID];
+static unsigned int cnt;
+
+typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
+
+void
+FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
+int *xdrid, *ret;
+int *pb;
+{
+ *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
+int *xdrid, *ret;
+char *cp;
+{
+ *ret = xdr_char(xdridptr[*xdrid], cp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
+int *xdrid, *ret;
+double *dp;
+{
+ *ret = xdr_double(xdridptr[*xdrid], dp);
+ cnt += sizeof(double);
+}
+
+void
+FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
+int *xdrid, *ret;
+float *fp;
+{
+ *ret = xdr_float(xdridptr[*xdrid], fp);
+ cnt += sizeof(float);
+}
+
+void
+FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
+int *xdrid, *ret;
+int *ip;
+{
+ *ret = xdr_int(xdridptr[*xdrid], ip);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
+int *xdrid, *ret;
+long *lp;
+{
+ *ret = xdr_long(xdridptr[*xdrid], lp);
+ cnt += sizeof(long);
+}
+
+void
+FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
+int *xdrid, *ret;
+short *sp;
+{
+ *ret = xdr_short(xdridptr[*xdrid], sp);
+ cnt += sizeof(sp);
+}
+
+void
+FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
+int *xdrid, *ret;
+char *ucp;
+{
+ *ret = xdr_u_char(xdridptr[*xdrid], ucp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
+int *xdrid, *ret;
+unsigned long *ulp;
+{
+ *ret = xdr_u_long(xdridptr[*xdrid], ulp);
+ cnt += sizeof(unsigned long);
+}
+
+void
+FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
+int *xdrid, *ret;
+unsigned short *usp;
+{
+ *ret = xdr_u_short(xdridptr[*xdrid], usp);
+ cnt += sizeof(unsigned short);
+}
+
+void
+FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
+int *xdrid, *ret;
+float *fp;
+int *size;
+float *precision;
+{
+ *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
+}
+
+void
+FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+int *maxsize;
+{
+ char *tsp;
+
+ tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += *maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+{
+ char *tsp;
+ int maxsize;
+ maxsize = (STRING_LEN(sp)) + 1;
+ tsp = (char*) malloc(maxsize * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
+int *xdrid, *ret;
+caddr_t *cp;
+int *ccnt;
+{
+ *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
+ cnt += *ccnt;
+}
+
+void
+FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
+int *xdrid, *ret;
+int *pos;
+{
+ *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
+}
+
+void
+FUNCTION(xdrf) ARGS(`xdrid, pos')
+int *xdrid, *pos;
+{
+ *pos = xdr_getpos(xdridptr[*xdrid]);
+}
+
+void
+FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
+int *xdrid, *ret;
+char *cp;
+int *size;
+FUNCTION(xdrfproc) elproc;
+{
+ int lcnt;
+ cnt = 0;
+ for (lcnt = 0; lcnt < *size; lcnt++) {
+ elproc(xdrid, (cp+cnt) , ret);
+ }
+}
+
+
+void
+FUNCTION(xdrfclose) ARGS(`xdrid, ret')
+int *xdrid;
+int *ret;
+{
+ *ret = xdrclose(xdridptr[*xdrid]);
+ cnt = 0;
+}
+
+void
+FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
+int *xdrid;
+STRING_ARG_DECL(fp);
+STRING_ARG_DECL(mode);
+int *ret;
+{
+ char fname[512];
+ char fmode[3];
+
+ if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
+ *ret = 0;
+ }
+ if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
+ STRING_LEN(mode))) {
+ *ret = 0;
+ }
+
+ *xdrid = xdropen(NULL, fname, fmode);
+ if (*xdrid == 0)
+ *ret = 0;
+ else
+ *ret = 1;
+}
+
+/*___________________________________________________________________________
+ |
+ | what follows are the C routines for opening, closing xdr streams
+ | and the routine to read/write compressed coordinates together
+ | with some routines to assist in this task (those are marked
+ | static and cannot be called from user programs)
+*/
+#define MAXABS INT_MAX-2
+
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x):(y))
+#endif
+#ifndef MAX
+#define MAX(x,y) ((x) > (y) ? (x):(y))
+#endif
+#ifndef SQR
+#define SQR(x) ((x)*(x))
+#endif
+static 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))
+
+
+/*__________________________________________________________________________
+ |
+ | xdropen - open xdr file
+ |
+ | This versions differs from xdrstdio_create, because I need to know
+ | the state of the file (read or write) so I can use xdr3dfcoord
+ | in eigther read or write mode, and the file descriptor
+ | so I can close the file (something xdr_destroy doesn't do).
+ |
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type) {
+ static int init_done = 0;
+ enum xdr_op lmode;
+ const char *type1;
+ int xdrid;
+
+ if (init_done == 0) {
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ xdridptr[xdrid] = NULL;
+ }
+ init_done = 1;
+ }
+ xdrid = 1;
+ while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
+ xdrid++;
+ }
+ if (xdrid == MAXID) {
+ return 0;
+ }
+ if (*type == 'w' || *type == 'W') {
+ type = "w+";
+ type1 = "w+";
+ lmode = XDR_ENCODE;
+ } else if (*type == 'a' || *type == 'A') {
+ type = "w+";
+ type1 = "a+";
+ lmode = XDR_ENCODE;
+ } else {
+ type = "r";
+ type1 = "r";
+ lmode = XDR_DECODE;
+ }
+ xdrfiles[xdrid] = fopen(filename, type1);
+ if (xdrfiles[xdrid] == NULL) {
+ xdrs = NULL;
+ return 0;
+ }
+ xdrmodes[xdrid] = *type;
+ /* next test isn't usefull in the case of C language
+ * but is used for the Fortran interface
+ * (C users are expected to pass the address of an already allocated
+ * XDR staructure)
+ */
+ if (xdrs == NULL) {
+ xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
+ xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
+ } else {
+ xdridptr[xdrid] = xdrs;
+ xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
+ }
+ return xdrid;
+}
+
+/*_________________________________________________________________________
+ |
+ | xdrclose - close a xdr file
+ |
+ | This will flush the xdr buffers, and destroy the xdr stream.
+ | It also closes the associated file descriptor (this is *not*
+ | done by xdr_destroy).
+ |
+*/
+
+int xdrclose(XDR *xdrs) {
+ int xdrid;
+
+ if (xdrs == NULL) {
+ fprintf(stderr, "xdrclose: passed a NULL pointer\n");
+ exit(1);
+ }
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ if (xdridptr[xdrid] == xdrs) {
+
+ xdr_destroy(xdrs);
+ fclose(xdrfiles[xdrid]);
+ xdridptr[xdrid] = NULL;
+ return 1;
+ }
+ }
+ fprintf(stderr, "xdrclose: no such open xdr file\n");
+ exit(1);
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendbits - 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
+ | better make sure that this number of bits is enough to hold the value
+ | Also num must be positive.
+ |
+*/
+
+static void sendbits(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);
+ }
+}
+
+/*_________________________________________________________________________
+ |
+ | sizeofint - calculate bitsize of an integer
+ |
+ | return the number of bits needed to store an integer with given max size
+ |
+*/
+
+static int sizeofint(const 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 the number of small unsigned integers and the maximum value
+ | return the number of bits needed to read or write them with the
+ | routines receiveints and sendints. You need this parameter when
+ | calling these routines. Note that for many calls I can use
+ | the variable 'smallidx' which is exactly the number of bits, and
+ | So I don't need to call 'sizeofints for those calls.
+*/
+
+static int sizeofints( const 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;
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendints - send a small set of small integers in compressed format
+ |
+ | this routine is used internally by xdr3dfcoord, to send a set of
+ | small integers to the buffer.
+ | 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 causes bad results.
+ |
+ */
+
+static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
+ "match size %d\n", nums[i], sizes[i]);
+ exit(1);
+ }
+ /* 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++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
+ } else {
+ for (i = 0; i < num_of_bytes-1; i++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
+ }
+}
+
+
+/*___________________________________________________________________________
+ |
+ | receivebits - 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 receivebits(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;
+}
+
+/*____________________________________________________________________________
+ |
+ | receiveints - decode 'small' integers from the buf array
+ |
+ | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
+ num_of_bits -= 8;
+ }
+ if (num_of_bits > 0) {
+ bytes[num_of_bytes++] = receivebits(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);
+}
+
+/*____________________________________________________________________________
+ |
+ | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
+ |
+ | this routine reads or writes (depending on how you opened the file with
+ | xdropen() ) a large number of 3d coordinates (stored in *fp).
+ | The number of coordinates triplets to write is given by *size. On
+ | read this number may be zero, in which case it reads as many as were written
+ | or it may specify the number if triplets to read (which should match the
+ | number written).
+ | Compression is achieved by first converting all floating numbers to integer
+ | using multiplication by *precision and rounding to the nearest integer.
+ | Then the minimum and maximum value are calculated to determine the range.
+ | The limited range of integers so found, is used to compress the coordinates.
+ | In addition the differences between succesive coordinates is calculated.
+ | If the difference happens to be 'small' then only the difference is saved,
+ | compressing the data even more. The notion of 'small' is changed dynamically
+ | and is enlarged or reduced whenever needed or possible.
+ | Extra compression is achieved in the case of GROMOS and coordinates of
+ | water molecules. GROMOS first writes out the Oxygen position, followed by
+ | the two hydrogens. In order to make the differences smaller (and thereby
+ | compression the data better) the order is changed into first one hydrogen
+ | then the oxygen, followed by the other hydrogen. This is rather special, but
+ | it shouldn't harm in the general case.
+ |
+ */
+
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
+
+
+ static int *ip = NULL;
+ static int oldsize;
+ static int *buf;
+
+ 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 flag, k;
+ int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
+ float *lfp, lf;
+ int tmp, *thiscoord, prevcoord[3];
+ unsigned int tmpcoord[30];
+
+ int bufsize, xdrid, lsize;
+ unsigned int bitsize;
+ float inv_precision;
+ int errval = 1;
+
+ /* find out if xdrs is opened for reading or for writing */
+ xdrid = 0;
+ while (xdridptr[xdrid] != xdrs) {
+ xdrid++;
+ if (xdrid >= MAXID) {
+ fprintf(stderr, "xdr error. no open xdr stream\n");
+ exit (1);
+ }
+ }
+ if (xdrmodes[xdrid] == 'w') {
+
+ /* xdrs is open for writing */
+
+ if (xdr_int(xdrs, size) == 0)
+ return 0;
+ size3 = *size * 3;
+ /* when the number of coordinates is small, don't try to compress; just
+ * write them as floats using xdr_vector
+ */
+ if (*size <= 9 ) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ /* buf[0-2] are special and do not contain actual data */
+ buf[0] = buf[1] = buf[2] = 0;
+ minint[0] = minint[1] = minint[2] = INT_MAX;
+ maxint[0] = maxint[1] = maxint[2] = INT_MIN;
+ prevrun = -1;
+ lfp = fp;
+ lip = ip;
+ mindiff = INT_MAX;
+ oldlint1 = oldlint2 = oldlint3 = 0;
+ while(lfp < fp + size3 ) {
+ /* find nearest integer */
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint1 = lf;
+ if (lint1 < minint[0]) minint[0] = lint1;
+ if (lint1 > maxint[0]) maxint[0] = lint1;
+ *lip++ = lint1;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint2 = lf;
+ if (lint2 < minint[1]) minint[1] = lint2;
+ if (lint2 > maxint[1]) maxint[1] = lint2;
+ *lip++ = lint2;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint3 = lf;
+ if (lint3 < minint[2]) minint[2] = lint3;
+ if (lint3 > maxint[2]) maxint[2] = lint3;
+ *lip++ = lint3;
+ lfp++;
+ diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
+ if (diff < mindiff && lfp > fp + 3)
+ mindiff = diff;
+ oldlint1 = lint1;
+ oldlint2 = lint2;
+ oldlint3 = lint3;
+ }
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
+ (float)maxint[1] - (float)minint[1] >= MAXABS ||
+ (float)maxint[2] - (float)minint[2] >= MAXABS) {
+ /* turning value in unsigned by subtracting minint
+ * would cause overflow
+ */
+ errval = 0;
+ }
+ 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);
+ }
+ lip = ip;
+ luip = (unsigned int *) ip;
+ smallidx = FIRSTIDX;
+ while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
+ smallidx++;
+ }
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ larger = magicints[maxidx] / 2;
+ i = 0;
+ while (i < *size) {
+ is_small = 0;
+ thiscoord = (int *)(luip) + i * 3;
+ if (smallidx < maxidx && i >= 1 &&
+ abs(thiscoord[0] - prevcoord[0]) < larger &&
+ abs(thiscoord[1] - prevcoord[1]) < larger &&
+ abs(thiscoord[2] - prevcoord[2]) < larger) {
+ is_smaller = 1;
+ } else if (smallidx > minidx) {
+ is_smaller = -1;
+ } else {
+ is_smaller = 0;
+ }
+ if (i + 1 < *size) {
+ if (abs(thiscoord[0] - thiscoord[3]) < small &&
+ abs(thiscoord[1] - thiscoord[4]) < small &&
+ abs(thiscoord[2] - thiscoord[5]) < small) {
+ /* interchange first with second atom for better
+ * compression of water molecules
+ */
+ tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
+ thiscoord[3] = tmp;
+ tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
+ thiscoord[4] = tmp;
+ tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
+ thiscoord[5] = tmp;
+ is_small = 1;
+ }
+
+ }
+ tmpcoord[0] = thiscoord[0] - minint[0];
+ tmpcoord[1] = thiscoord[1] - minint[1];
+ tmpcoord[2] = thiscoord[2] - minint[2];
+ if (bitsize == 0) {
+ sendbits(buf, bitsizeint[0], tmpcoord[0]);
+ sendbits(buf, bitsizeint[1], tmpcoord[1]);
+ sendbits(buf, bitsizeint[2], tmpcoord[2]);
+ } else {
+ sendints(buf, 3, bitsize, sizeint, tmpcoord);
+ }
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+ thiscoord = thiscoord + 3;
+ i++;
+
+ run = 0;
+ if (is_small == 0 && is_smaller == -1)
+ is_smaller = 0;
+ while (is_small && run < 8*3) {
+ if (is_smaller == -1 && (
+ SQR(thiscoord[0] - prevcoord[0]) +
+ SQR(thiscoord[1] - prevcoord[1]) +
+ SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
+ is_smaller = 0;
+ }
+
+ tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
+ tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
+ tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
+
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+
+ i++;
+ thiscoord = thiscoord + 3;
+ is_small = 0;
+ if (i < *size &&
+ abs(thiscoord[0] - prevcoord[0]) < small &&
+ abs(thiscoord[1] - prevcoord[1]) < small &&
+ abs(thiscoord[2] - prevcoord[2]) < small) {
+ is_small = 1;
+ }
+ }
+ if (run != prevrun || is_smaller != 0) {
+ prevrun = run;
+ sendbits(buf, 1, 1); /* flag the change in run-length */
+ sendbits(buf, 5, run+is_smaller+1);
+ } else {
+ sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
+ }
+ for (k=0; k < run; k+=3) {
+ sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
+ }
+ if (is_smaller != 0) {
+ smallidx += is_smaller;
+ if (is_smaller < 0) {
+ small = smaller;
+ smaller = magicints[smallidx-1] / 2;
+ } else {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ }
+ }
+ if (buf[1] != 0) buf[0]++;;
+ xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
+ return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
+ } else {
+
+ /* xdrs is open for reading */
+
+ if (xdr_int(xdrs, &lsize) == 0)
+ return 0;
+ if (*size != 0 && lsize != *size) {
+ fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
+ "%d arg vs %d in file", *size, lsize);
+ }
+ *size = lsize;
+ size3 = *size * 3;
+ if (*size <= 9) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ buf[0] = buf[1] = buf[2] = 0;
+
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ 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);
+ }
+
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ larger = magicints[maxidx];
+
+ /* buf[0] holds the length in bytes */
+
+ if (xdr_int(xdrs, &(buf[0])) == 0)
+ return 0;
+ if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
+ return 0;
+ buf[0] = buf[1] = buf[2] = 0;
+
+ lfp = fp;
+ inv_precision = 1.0 / * precision;
+ run = 0;
+ i = 0;
+ lip = ip;
+ while ( i < lsize ) {
+ thiscoord = (int *)(lip) + i * 3;
+
+ if (bitsize == 0) {
+ thiscoord[0] = receivebits(buf, bitsizeint[0]);
+ thiscoord[1] = receivebits(buf, bitsizeint[1]);
+ thiscoord[2] = receivebits(buf, bitsizeint[2]);
+ } else {
+ receiveints(buf, 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 = receivebits(buf, 1);
+ is_smaller = 0;
+ if (flag == 1) {
+ run = receivebits(buf, 5);
+ is_smaller = run % 3;
+ run -= is_smaller;
+ is_smaller--;
+ }
+ if (run > 0) {
+ thiscoord += 3;
+ for (k = 0; k < run; k+=3) {
+ receiveints(buf, 3, smallidx, sizesmall, thiscoord);
+ i++;
+ thiscoord[0] += prevcoord[0] - small;
+ thiscoord[1] += prevcoord[1] - small;
+ thiscoord[2] += prevcoord[2] - small;
+ 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) {
+ small = smaller;
+ if (smallidx > FIRSTIDX) {
+ smaller = magicints[smallidx - 1] /2;
+ } else {
+ smaller = 0;
+ }
+ } else if (is_smaller > 0) {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ }
+ }
+ return 1;
+}
+
+
+
--- /dev/null
+/*____________________________________________________________________________
+ |
+ | libxdrf - portable fortran interface to xdr. some xdr routines
+ | are C routines for compressed coordinates
+ |
+ | version 1.1
+ |
+ | This collection of routines is intended to write and read
+ | data in a portable way to a file, so data written on one type
+ | of machine can be read back on a different type.
+ |
+ | all fortran routines use an integer 'xdrid', which is an id to the
+ | current xdr file, and is set by xdrfopen.
+ | most routines have in integer 'ret' which is the return value.
+ | The value of 'ret' is zero on failure, and most of the time one
+ | on succes.
+ |
+ | There are three routines useful for C users:
+ | xdropen(), xdrclose(), xdr3dfcoord().
+ | The first two replace xdrstdio_create and xdr_destroy, and *must* be
+ | used when you plan to use xdr3dfcoord(). (they are also a bit
+ | easier to interface). For writing data other than compressed coordinates
+ | you should use the standard C xdr routines (see xdr man page)
+ |
+ | xdrfopen(xdrid, filename, mode, ret)
+ | character *(*) filename
+ | character *(*) mode
+ |
+ | this will open the file with the given filename (string)
+ | and the given mode, it returns an id in xdrid, which is
+ | to be used in all other calls to xdrf routines.
+ | mode is 'w' to create, or update an file, for all other
+ | values of mode the file is opened for reading
+ |
+ | you need to call xdrfclose to flush the output and close
+ | the file.
+ | Note that you should not use xdrstdio_create, which comes with the
+ | standard xdr library
+ |
+ | xdrfclose(xdrid, ret)
+ | flush the data to the file, and closes the file;
+ | You should not use xdr_destroy (which comes standard with
+ | the xdr libraries.
+ |
+ | xdrfbool(xdrid, bp, ret)
+ | integer pb
+ |
+ | This filter produces values of either 1 or 0
+ |
+ | xdrfchar(xdrid, cp, ret)
+ | character cp
+ |
+ | filter that translate between characters and their xdr representation
+ | Note that the characters in not compressed and occupies 4 bytes.
+ |
+ | xdrfdouble(xdrid, dp, ret)
+ | double dp
+ |
+ | read/write a double.
+ |
+ | xdrffloat(xdrid, fp, ret)
+ | float fp
+ |
+ | read/write a float.
+ |
+ | xdrfint(xdrid, ip, ret)
+ | integer ip
+ |
+ | read/write integer.
+ |
+ | xdrflong(xdrid, lp, ret)
+ | integer lp
+ |
+ | this routine has a possible portablility problem due to 64 bits longs.
+ |
+ | xdrfshort(xdrid, sp, ret)
+ | integer *2 sp
+ |
+ | xdrfstring(xdrid, sp, maxsize, ret)
+ | character *(*)
+ | integer maxsize
+ |
+ | read/write a string, with maximum length given by maxsize
+ |
+ | xdrfwrapstring(xdris, sp, ret)
+ | character *(*)
+ |
+ | read/write a string (it is the same as xdrfstring accept that it finds
+ | the stringlength itself.
+ |
+ | xdrfvector(xdrid, cp, size, xdrfproc, ret)
+ | character *(*)
+ | integer size
+ | external xdrfproc
+ |
+ | read/write an array pointed to by cp, with number of elements
+ | defined by 'size'. the routine 'xdrfproc' is the name
+ | of one of the above routines to read/write data (like xdrfdouble)
+ | In contrast with the c-version you don't need to specify the
+ | byte size of an element.
+ | xdrfstring is not allowed here (it is in the c version)
+ |
+ | xdrf3dfcoord(xdrid, fp, size, precision, ret)
+ | real (*) fp
+ | real precision
+ | integer size
+ |
+ | this is *NOT* a standard xdr routine. I named it this way, because
+ | it invites people to use the other xdr routines.
+ | It is introduced to store specifically 3d coordinates of molecules
+ | (as found in molecular dynamics) and it writes it in a compressed way.
+ | It starts by multiplying all numbers by precision and
+ | rounding the result to integer. effectively converting
+ | all floating point numbers to fixed point.
+ | it uses an algorithm for compression that is optimized for
+ | molecular data, but could be used for other 3d coordinates
+ | as well. There is subtantial overhead involved, so call this
+ | routine only if you have a large number of coordinates to read/write
+ |
+ | ________________________________________________________________________
+ |
+ | Below are the routines to be used by C programmers. Use the 'normal'
+ | xdr routines to write integers, floats, etc (see man xdr)
+ |
+ | int xdropen(XDR *xdrs, const char *filename, const char *type)
+ | This will open the file with the given filename and the
+ | given mode. You should pass it an allocated XDR struct
+ | in xdrs, to be used in all other calls to xdr routines.
+ | Mode is 'w' to create, or update an file, and for all
+ | other values of mode the file is opened for reading.
+ | You need to call xdrclose to flush the output and close
+ | the file.
+ |
+ | Note that you should not use xdrstdio_create, which
+ | comes with the standard xdr library.
+ |
+ | int xdrclose(XDR *xdrs)
+ | Flush the data to the file, and close the file;
+ | You should not use xdr_destroy (which comes standard
+ | with the xdr libraries).
+ |
+ | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
+ | This is \fInot\fR a standard xdr routine. I named it this
+ | way, because it invites people to use the other xdr
+ | routines.
+ |
+ | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
+*/
+
+
+#include <limits.h>
+#include <malloc.h>
+#include <math.h>
+#include <rpc/rpc.h>
+#include <rpc/xdr.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "xdrf.h"
+
+int ftocstr(char *, int, char *, int);
+int ctofstr(char *, int, char *);
+
+#define MAXID 20
+static FILE *xdrfiles[MAXID];
+static XDR *xdridptr[MAXID];
+static char xdrmodes[MAXID];
+static unsigned int cnt;
+
+typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
+
+void
+FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
+int *xdrid, *ret;
+int *pb;
+{
+ *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
+int *xdrid, *ret;
+char *cp;
+{
+ *ret = xdr_char(xdridptr[*xdrid], cp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
+int *xdrid, *ret;
+double *dp;
+{
+ *ret = xdr_double(xdridptr[*xdrid], dp);
+ cnt += sizeof(double);
+}
+
+void
+FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
+int *xdrid, *ret;
+float *fp;
+{
+ *ret = xdr_float(xdridptr[*xdrid], fp);
+ cnt += sizeof(float);
+}
+
+void
+FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
+int *xdrid, *ret;
+int *ip;
+{
+ *ret = xdr_int(xdridptr[*xdrid], ip);
+ cnt += sizeof(int);
+}
+
+void
+FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
+int *xdrid, *ret;
+long *lp;
+{
+ *ret = xdr_long(xdridptr[*xdrid], lp);
+ cnt += sizeof(long);
+}
+
+void
+FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
+int *xdrid, *ret;
+short *sp;
+{
+ *ret = xdr_short(xdridptr[*xdrid], sp);
+ cnt += sizeof(sp);
+}
+
+void
+FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
+int *xdrid, *ret;
+char *ucp;
+{
+ *ret = xdr_u_char(xdridptr[*xdrid], ucp);
+ cnt += sizeof(char);
+}
+
+void
+FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
+int *xdrid, *ret;
+unsigned long *ulp;
+{
+ *ret = xdr_u_long(xdridptr[*xdrid], ulp);
+ cnt += sizeof(unsigned long);
+}
+
+void
+FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
+int *xdrid, *ret;
+unsigned short *usp;
+{
+ *ret = xdr_u_short(xdridptr[*xdrid], usp);
+ cnt += sizeof(unsigned short);
+}
+
+void
+FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
+int *xdrid, *ret;
+float *fp;
+int *size;
+float *precision;
+{
+ *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
+}
+
+void
+FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+int *maxsize;
+{
+ char *tsp;
+
+ tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += *maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
+int *xdrid, *ret;
+STRING_ARG_DECL(sp);
+{
+ char *tsp;
+ int maxsize;
+ maxsize = (STRING_LEN(sp)) + 1;
+ tsp = (char*) malloc(maxsize * sizeof(char));
+ if (tsp == NULL) {
+ *ret = -1;
+ return;
+ }
+ if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
+ *ret = -1;
+ free(tsp);
+ return;
+ }
+ *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
+ ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
+ cnt += maxsize;
+ free(tsp);
+}
+
+void
+FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
+int *xdrid, *ret;
+caddr_t *cp;
+int *ccnt;
+{
+ *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
+ cnt += *ccnt;
+}
+
+void
+FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
+int *xdrid, *ret;
+int *pos;
+{
+ *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
+}
+
+void
+FUNCTION(xdrf) ARGS(`xdrid, pos')
+int *xdrid, *pos;
+{
+ *pos = xdr_getpos(xdridptr[*xdrid]);
+}
+
+void
+FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
+int *xdrid, *ret;
+char *cp;
+int *size;
+FUNCTION(xdrfproc) elproc;
+{
+ int lcnt;
+ cnt = 0;
+ for (lcnt = 0; lcnt < *size; lcnt++) {
+ elproc(xdrid, (cp+cnt) , ret);
+ }
+}
+
+
+void
+FUNCTION(xdrfclose) ARGS(`xdrid, ret')
+int *xdrid;
+int *ret;
+{
+ *ret = xdrclose(xdridptr[*xdrid]);
+ cnt = 0;
+}
+
+void
+FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
+int *xdrid;
+STRING_ARG_DECL(fp);
+STRING_ARG_DECL(mode);
+int *ret;
+{
+ char fname[512];
+ char fmode[3];
+
+ if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
+ *ret = 0;
+ }
+ if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
+ STRING_LEN(mode))) {
+ *ret = 0;
+ }
+
+ *xdrid = xdropen(NULL, fname, fmode);
+ if (*xdrid == 0)
+ *ret = 0;
+ else
+ *ret = 1;
+}
+
+/*___________________________________________________________________________
+ |
+ | what follows are the C routines for opening, closing xdr streams
+ | and the routine to read/write compressed coordinates together
+ | with some routines to assist in this task (those are marked
+ | static and cannot be called from user programs)
+*/
+#define MAXABS INT_MAX-2
+
+#ifndef MIN
+#define MIN(x,y) ((x) < (y) ? (x):(y))
+#endif
+#ifndef MAX
+#define MAX(x,y) ((x) > (y) ? (x):(y))
+#endif
+#ifndef SQR
+#define SQR(x) ((x)*(x))
+#endif
+static 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))
+
+
+/*__________________________________________________________________________
+ |
+ | xdropen - open xdr file
+ |
+ | This versions differs from xdrstdio_create, because I need to know
+ | the state of the file (read or write) so I can use xdr3dfcoord
+ | in eigther read or write mode, and the file descriptor
+ | so I can close the file (something xdr_destroy doesn't do).
+ |
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type) {
+ static int init_done = 0;
+ enum xdr_op lmode;
+ int xdrid;
+
+ if (init_done == 0) {
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ xdridptr[xdrid] = NULL;
+ }
+ init_done = 1;
+ }
+ xdrid = 1;
+ while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
+ xdrid++;
+ }
+ if (xdrid == MAXID) {
+ return 0;
+ }
+ if (*type == 'w' || *type == 'W') {
+ type = "w+";
+ lmode = XDR_ENCODE;
+ } else {
+ type = "r";
+ lmode = XDR_DECODE;
+ }
+ xdrfiles[xdrid] = fopen(filename, type);
+ if (xdrfiles[xdrid] == NULL) {
+ xdrs = NULL;
+ return 0;
+ }
+ xdrmodes[xdrid] = *type;
+ /* next test isn't usefull in the case of C language
+ * but is used for the Fortran interface
+ * (C users are expected to pass the address of an already allocated
+ * XDR staructure)
+ */
+ if (xdrs == NULL) {
+ xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
+ xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
+ } else {
+ xdridptr[xdrid] = xdrs;
+ xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
+ }
+ return xdrid;
+}
+
+/*_________________________________________________________________________
+ |
+ | xdrclose - close a xdr file
+ |
+ | This will flush the xdr buffers, and destroy the xdr stream.
+ | It also closes the associated file descriptor (this is *not*
+ | done by xdr_destroy).
+ |
+*/
+
+int xdrclose(XDR *xdrs) {
+ int xdrid;
+
+ if (xdrs == NULL) {
+ fprintf(stderr, "xdrclose: passed a NULL pointer\n");
+ exit(1);
+ }
+ for (xdrid = 1; xdrid < MAXID; xdrid++) {
+ if (xdridptr[xdrid] == xdrs) {
+
+ xdr_destroy(xdrs);
+ fclose(xdrfiles[xdrid]);
+ xdridptr[xdrid] = NULL;
+ return 1;
+ }
+ }
+ fprintf(stderr, "xdrclose: no such open xdr file\n");
+ exit(1);
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendbits - 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
+ | better make sure that this number of bits is enough to hold the value
+ | Also num must be positive.
+ |
+*/
+
+static void sendbits(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);
+ }
+}
+
+/*_________________________________________________________________________
+ |
+ | sizeofint - calculate bitsize of an integer
+ |
+ | return the number of bits needed to store an integer with given max size
+ |
+*/
+
+static int sizeofint(const 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 the number of small unsigned integers and the maximum value
+ | return the number of bits needed to read or write them with the
+ | routines receiveints and sendints. You need this parameter when
+ | calling these routines. Note that for many calls I can use
+ | the variable 'smallidx' which is exactly the number of bits, and
+ | So I don't need to call 'sizeofints for those calls.
+*/
+
+static int sizeofints( const 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;
+
+}
+
+/*____________________________________________________________________________
+ |
+ | sendints - send a small set of small integers in compressed format
+ |
+ | this routine is used internally by xdr3dfcoord, to send a set of
+ | small integers to the buffer.
+ | 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 causes bad results.
+ |
+ */
+
+static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
+ "match size %d\n", nums[i], sizes[i]);
+ exit(1);
+ }
+ /* 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++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
+ } else {
+ for (i = 0; i < num_of_bytes-1; i++) {
+ sendbits(buf, 8, bytes[i]);
+ }
+ sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
+ }
+}
+
+
+/*___________________________________________________________________________
+ |
+ | receivebits - 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 receivebits(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;
+}
+
+/*____________________________________________________________________________
+ |
+ | receiveints - decode 'small' integers from the buf array
+ |
+ | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
+ num_of_bits -= 8;
+ }
+ if (num_of_bits > 0) {
+ bytes[num_of_bytes++] = receivebits(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);
+}
+
+/*____________________________________________________________________________
+ |
+ | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
+ |
+ | this routine reads or writes (depending on how you opened the file with
+ | xdropen() ) a large number of 3d coordinates (stored in *fp).
+ | The number of coordinates triplets to write is given by *size. On
+ | read this number may be zero, in which case it reads as many as were written
+ | or it may specify the number if triplets to read (which should match the
+ | number written).
+ | Compression is achieved by first converting all floating numbers to integer
+ | using multiplication by *precision and rounding to the nearest integer.
+ | Then the minimum and maximum value are calculated to determine the range.
+ | The limited range of integers so found, is used to compress the coordinates.
+ | In addition the differences between succesive coordinates is calculated.
+ | If the difference happens to be 'small' then only the difference is saved,
+ | compressing the data even more. The notion of 'small' is changed dynamically
+ | and is enlarged or reduced whenever needed or possible.
+ | Extra compression is achieved in the case of GROMOS and coordinates of
+ | water molecules. GROMOS first writes out the Oxygen position, followed by
+ | the two hydrogens. In order to make the differences smaller (and thereby
+ | compression the data better) the order is changed into first one hydrogen
+ | then the oxygen, followed by the other hydrogen. This is rather special, but
+ | it shouldn't harm in the general case.
+ |
+ */
+
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
+
+
+ static int *ip = NULL;
+ static int oldsize;
+ static int *buf;
+
+ 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 flag, k;
+ int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
+ float *lfp, lf;
+ int tmp, *thiscoord, prevcoord[3];
+ unsigned int tmpcoord[30];
+
+ int bufsize, xdrid, lsize;
+ unsigned int bitsize;
+ float inv_precision;
+ int errval = 1;
+
+ /* find out if xdrs is opened for reading or for writing */
+ xdrid = 0;
+ while (xdridptr[xdrid] != xdrs) {
+ xdrid++;
+ if (xdrid >= MAXID) {
+ fprintf(stderr, "xdr error. no open xdr stream\n");
+ exit (1);
+ }
+ }
+ if (xdrmodes[xdrid] == 'w') {
+
+ /* xdrs is open for writing */
+
+ if (xdr_int(xdrs, size) == 0)
+ return 0;
+ size3 = *size * 3;
+ /* when the number of coordinates is small, don't try to compress; just
+ * write them as floats using xdr_vector
+ */
+ if (*size <= 9 ) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ /* buf[0-2] are special and do not contain actual data */
+ buf[0] = buf[1] = buf[2] = 0;
+ minint[0] = minint[1] = minint[2] = INT_MAX;
+ maxint[0] = maxint[1] = maxint[2] = INT_MIN;
+ prevrun = -1;
+ lfp = fp;
+ lip = ip;
+ mindiff = INT_MAX;
+ oldlint1 = oldlint2 = oldlint3 = 0;
+ while(lfp < fp + size3 ) {
+ /* find nearest integer */
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint1 = lf;
+ if (lint1 < minint[0]) minint[0] = lint1;
+ if (lint1 > maxint[0]) maxint[0] = lint1;
+ *lip++ = lint1;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint2 = lf;
+ if (lint2 < minint[1]) minint[1] = lint2;
+ if (lint2 > maxint[1]) maxint[1] = lint2;
+ *lip++ = lint2;
+ lfp++;
+ if (*lfp >= 0.0)
+ lf = *lfp * *precision + 0.5;
+ else
+ lf = *lfp * *precision - 0.5;
+ if (fabs(lf) > MAXABS) {
+ /* scaling would cause overflow */
+ errval = 0;
+ }
+ lint3 = lf;
+ if (lint3 < minint[2]) minint[2] = lint3;
+ if (lint3 > maxint[2]) maxint[2] = lint3;
+ *lip++ = lint3;
+ lfp++;
+ diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
+ if (diff < mindiff && lfp > fp + 3)
+ mindiff = diff;
+ oldlint1 = lint1;
+ oldlint2 = lint2;
+ oldlint3 = lint3;
+ }
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
+ (float)maxint[1] - (float)minint[1] >= MAXABS ||
+ (float)maxint[2] - (float)minint[2] >= MAXABS) {
+ /* turning value in unsigned by subtracting minint
+ * would cause overflow
+ */
+ errval = 0;
+ }
+ 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);
+ }
+ lip = ip;
+ luip = (unsigned int *) ip;
+ smallidx = FIRSTIDX;
+ while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
+ smallidx++;
+ }
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ larger = magicints[maxidx] / 2;
+ i = 0;
+ while (i < *size) {
+ is_small = 0;
+ thiscoord = (int *)(luip) + i * 3;
+ if (smallidx < maxidx && i >= 1 &&
+ abs(thiscoord[0] - prevcoord[0]) < larger &&
+ abs(thiscoord[1] - prevcoord[1]) < larger &&
+ abs(thiscoord[2] - prevcoord[2]) < larger) {
+ is_smaller = 1;
+ } else if (smallidx > minidx) {
+ is_smaller = -1;
+ } else {
+ is_smaller = 0;
+ }
+ if (i + 1 < *size) {
+ if (abs(thiscoord[0] - thiscoord[3]) < small &&
+ abs(thiscoord[1] - thiscoord[4]) < small &&
+ abs(thiscoord[2] - thiscoord[5]) < small) {
+ /* interchange first with second atom for better
+ * compression of water molecules
+ */
+ tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
+ thiscoord[3] = tmp;
+ tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
+ thiscoord[4] = tmp;
+ tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
+ thiscoord[5] = tmp;
+ is_small = 1;
+ }
+
+ }
+ tmpcoord[0] = thiscoord[0] - minint[0];
+ tmpcoord[1] = thiscoord[1] - minint[1];
+ tmpcoord[2] = thiscoord[2] - minint[2];
+ if (bitsize == 0) {
+ sendbits(buf, bitsizeint[0], tmpcoord[0]);
+ sendbits(buf, bitsizeint[1], tmpcoord[1]);
+ sendbits(buf, bitsizeint[2], tmpcoord[2]);
+ } else {
+ sendints(buf, 3, bitsize, sizeint, tmpcoord);
+ }
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+ thiscoord = thiscoord + 3;
+ i++;
+
+ run = 0;
+ if (is_small == 0 && is_smaller == -1)
+ is_smaller = 0;
+ while (is_small && run < 8*3) {
+ if (is_smaller == -1 && (
+ SQR(thiscoord[0] - prevcoord[0]) +
+ SQR(thiscoord[1] - prevcoord[1]) +
+ SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
+ is_smaller = 0;
+ }
+
+ tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
+ tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
+ tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
+
+ prevcoord[0] = thiscoord[0];
+ prevcoord[1] = thiscoord[1];
+ prevcoord[2] = thiscoord[2];
+
+ i++;
+ thiscoord = thiscoord + 3;
+ is_small = 0;
+ if (i < *size &&
+ abs(thiscoord[0] - prevcoord[0]) < small &&
+ abs(thiscoord[1] - prevcoord[1]) < small &&
+ abs(thiscoord[2] - prevcoord[2]) < small) {
+ is_small = 1;
+ }
+ }
+ if (run != prevrun || is_smaller != 0) {
+ prevrun = run;
+ sendbits(buf, 1, 1); /* flag the change in run-length */
+ sendbits(buf, 5, run+is_smaller+1);
+ } else {
+ sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
+ }
+ for (k=0; k < run; k+=3) {
+ sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
+ }
+ if (is_smaller != 0) {
+ smallidx += is_smaller;
+ if (is_smaller < 0) {
+ small = smaller;
+ smaller = magicints[smallidx-1] / 2;
+ } else {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
+ }
+ }
+ if (buf[1] != 0) buf[0]++;;
+ xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
+ return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
+ } else {
+
+ /* xdrs is open for reading */
+
+ if (xdr_int(xdrs, &lsize) == 0)
+ return 0;
+ if (*size != 0 && lsize != *size) {
+ fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
+ "%d arg vs %d in file", *size, lsize);
+ }
+ *size = lsize;
+ size3 = *size * 3;
+ if (*size <= 9) {
+ return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
+ (xdrproc_t)xdr_float));
+ }
+ xdr_float(xdrs, precision);
+ if (ip == NULL) {
+ ip = (int *)malloc(size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)malloc(bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ } else if (*size > oldsize) {
+ ip = (int *)realloc(ip, size3 * sizeof(*ip));
+ if (ip == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ bufsize = size3 * 1.2;
+ buf = (int *)realloc(buf, bufsize * sizeof(*buf));
+ if (buf == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ oldsize = *size;
+ }
+ buf[0] = buf[1] = buf[2] = 0;
+
+ xdr_int(xdrs, &(minint[0]));
+ xdr_int(xdrs, &(minint[1]));
+ xdr_int(xdrs, &(minint[2]));
+
+ xdr_int(xdrs, &(maxint[0]));
+ xdr_int(xdrs, &(maxint[1]));
+ xdr_int(xdrs, &(maxint[2]));
+
+ 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);
+ }
+
+ xdr_int(xdrs, &smallidx);
+ maxidx = MIN(LASTIDX, smallidx + 8) ;
+ minidx = maxidx - 8; /* often this equal smallidx */
+ smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
+ small = magicints[smallidx] / 2;
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ larger = magicints[maxidx];
+
+ /* buf[0] holds the length in bytes */
+
+ if (xdr_int(xdrs, &(buf[0])) == 0)
+ return 0;
+ if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
+ return 0;
+ buf[0] = buf[1] = buf[2] = 0;
+
+ lfp = fp;
+ inv_precision = 1.0 / * precision;
+ run = 0;
+ i = 0;
+ lip = ip;
+ while ( i < lsize ) {
+ thiscoord = (int *)(lip) + i * 3;
+
+ if (bitsize == 0) {
+ thiscoord[0] = receivebits(buf, bitsizeint[0]);
+ thiscoord[1] = receivebits(buf, bitsizeint[1]);
+ thiscoord[2] = receivebits(buf, bitsizeint[2]);
+ } else {
+ receiveints(buf, 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 = receivebits(buf, 1);
+ is_smaller = 0;
+ if (flag == 1) {
+ run = receivebits(buf, 5);
+ is_smaller = run % 3;
+ run -= is_smaller;
+ is_smaller--;
+ }
+ if (run > 0) {
+ thiscoord += 3;
+ for (k = 0; k < run; k+=3) {
+ receiveints(buf, 3, smallidx, sizesmall, thiscoord);
+ i++;
+ thiscoord[0] += prevcoord[0] - small;
+ thiscoord[1] += prevcoord[1] - small;
+ thiscoord[2] += prevcoord[2] - small;
+ 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) {
+ small = smaller;
+ if (smallidx > FIRSTIDX) {
+ smaller = magicints[smallidx - 1] /2;
+ } else {
+ smaller = 0;
+ }
+ } else if (is_smaller > 0) {
+ smaller = small;
+ small = magicints[smallidx] / 2;
+ }
+ sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
+ }
+ }
+ return 1;
+}
+
+
+
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+/* fixincludes should not add extern "C" to this file */
+/*
+ * Rpc additions to <sys/types.h>
+ */
+#ifndef _RPC_TYPES_H
+#define _RPC_TYPES_H 1
+
+typedef int bool_t;
+typedef int enum_t;
+/* This needs to be changed to uint32_t in the future */
+typedef unsigned long rpcprog_t;
+typedef unsigned long rpcvers_t;
+typedef unsigned long rpcproc_t;
+typedef unsigned long rpcprot_t;
+typedef unsigned long rpcport_t;
+
+#define __dontcare__ -1
+
+#ifndef FALSE
+# define FALSE (0)
+#endif
+
+#ifndef TRUE
+# define TRUE (1)
+#endif
+
+#ifndef NULL
+# define NULL 0
+#endif
+
+#include <stdlib.h> /* For malloc decl. */
+#define mem_alloc(bsize) malloc(bsize)
+/*
+ * XXX: This must not use the second argument, or code in xdr_array.c needs
+ * to be modified.
+ */
+#define mem_free(ptr, bsize) free(ptr)
+
+#ifndef makedev /* ie, we haven't already included it */
+#include <sys/types.h>
+#endif
+
+#ifndef __u_char_defined
+typedef __u_char u_char;
+typedef __u_short u_short;
+typedef __u_int u_int;
+typedef __u_long u_long;
+typedef __quad_t quad_t;
+typedef __u_quad_t u_quad_t;
+typedef __fsid_t fsid_t;
+# define __u_char_defined
+#endif
+#ifndef __daddr_t_defined
+typedef __daddr_t daddr_t;
+typedef __caddr_t caddr_t;
+# define __daddr_t_defined
+#endif
+
+#include <sys/time.h>
+#include <sys/param.h>
+
+#include <netinet/in.h>
+
+#ifndef INADDR_LOOPBACK
+#define INADDR_LOOPBACK (u_long)0x7F000001
+#endif
+#ifndef MAXHOSTNAMELEN
+#define MAXHOSTNAMELEN 64
+#endif
+
+#endif /* rpc/types.h */
--- /dev/null
+divert(-1)
+undefine(`len')
+#
+# append an underscore to FORTRAN function names
+#
+define(`FUNCTION',`$1_')
+#
+# FORTRAN character strings are passed as follows:
+# a pointer to the base of the string is passed in the normal
+# argument list, and the length is passed by value as an extra
+# argument, after all of the other arguments.
+#
+define(`ARGS',`($1`'undivert(1))')
+define(`SAVE',`divert(1)$1`'divert(0)')
+define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
+define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
+define(`STRING_LEN',`$1_len')
+define(`STRING_PTR',`$1_ptr')
+divert(0)
--- /dev/null
+# define INTUSE(name) name
+# define INTDEF(name)
+/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
+#endif
+
+/*
+ * xdr.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1986, Sun Microsystems, Inc.
+ *
+ * These are the "generic" xdr routines used to serialize and de-serialize
+ * most common data items. See xdr.h for more info on the interface to
+ * xdr.
+ */
+
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+#include <libintl.h>
+
+#include "types.h"
+#include "xdr.h"
+
+#ifdef USE_IN_LIBIO
+# include <wchar.h>
+#endif
+
+/*
+ * constants specific to the xdr "protocol"
+ */
+#define XDR_FALSE ((long) 0)
+#define XDR_TRUE ((long) 1)
+#define LASTUNSIGNED ((u_int) 0-1)
+
+/*
+ * for unit alignment
+ */
+static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
+
+/*
+ * Free a data structure using XDR
+ * Not a filter, but a convenient utility nonetheless
+ */
+void
+xdr_free (xdrproc_t proc, char *objp)
+{
+ XDR x;
+
+ x.x_op = XDR_FREE;
+ (*proc) (&x, objp);
+}
+
+/*
+ * XDR nothing
+ */
+bool_t
+xdr_void (void)
+{
+ return TRUE;
+}
+INTDEF(xdr_void)
+
+/*
+ * XDR integers
+ */
+bool_t
+xdr_int (XDR *xdrs, int *ip)
+{
+
+#if INT_MAX < LONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (long) *ip;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *ip = (int) l;
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+#elif INT_MAX == LONG_MAX
+ return INTUSE(xdr_long) (xdrs, (long *) ip);
+#elif INT_MAX == SHRT_MAX
+ return INTUSE(xdr_short) (xdrs, (short *) ip);
+#else
+#error unexpected integer sizes in_xdr_int()
+#endif
+}
+INTDEF(xdr_int)
+
+/*
+ * XDR unsigned integers
+ */
+bool_t
+xdr_u_int (XDR *xdrs, u_int *up)
+{
+#if UINT_MAX < ULONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (u_long) * up;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *up = (u_int) (u_long) l;
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+#elif UINT_MAX == ULONG_MAX
+ return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
+#elif UINT_MAX == USHRT_MAX
+ return INTUSE(xdr_short) (xdrs, (short *) up);
+#else
+#error unexpected integer sizes in_xdr_u_int()
+#endif
+}
+INTDEF(xdr_u_int)
+
+/*
+ * XDR long integers
+ * The definition of xdr_long() is kept for backward
+ * compatibility. Instead xdr_int() should be used.
+ */
+bool_t
+xdr_long (XDR *xdrs, long *lp)
+{
+
+ if (xdrs->x_op == XDR_ENCODE
+ && (sizeof (int32_t) == sizeof (long)
+ || (int32_t) *lp == *lp))
+ return XDR_PUTLONG (xdrs, lp);
+
+ if (xdrs->x_op == XDR_DECODE)
+ return XDR_GETLONG (xdrs, lp);
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_long)
+
+/*
+ * XDR unsigned long integers
+ * The definition of xdr_u_long() is kept for backward
+ * compatibility. Instead xdr_u_int() should be used.
+ */
+bool_t
+xdr_u_long (XDR *xdrs, u_long *ulp)
+{
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ {
+ long int tmp;
+
+ if (XDR_GETLONG (xdrs, &tmp) == FALSE)
+ return FALSE;
+
+ *ulp = (uint32_t) tmp;
+ return TRUE;
+ }
+
+ case XDR_ENCODE:
+ if (sizeof (uint32_t) != sizeof (u_long)
+ && (uint32_t) *ulp != *ulp)
+ return FALSE;
+
+ return XDR_PUTLONG (xdrs, (long *) ulp);
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_u_long)
+
+/*
+ * XDR hyper integers
+ * same as xdr_u_hyper - open coded to save a proc call!
+ */
+bool_t
+xdr_hyper (XDR *xdrs, quad_t *llp)
+{
+ long int t1, t2;
+
+ if (xdrs->x_op == XDR_ENCODE)
+ {
+ t1 = (long) ((*llp) >> 32);
+ t2 = (long) (*llp);
+ return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
+ }
+
+ if (xdrs->x_op == XDR_DECODE)
+ {
+ if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
+ return FALSE;
+ *llp = ((quad_t) t1) << 32;
+ *llp |= (uint32_t) t2;
+ return TRUE;
+ }
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_hyper)
+
+
+/*
+ * XDR hyper integers
+ * same as xdr_hyper - open coded to save a proc call!
+ */
+bool_t
+xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
+{
+ long int t1, t2;
+
+ if (xdrs->x_op == XDR_ENCODE)
+ {
+ t1 = (unsigned long) ((*ullp) >> 32);
+ t2 = (unsigned long) (*ullp);
+ return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
+ }
+
+ if (xdrs->x_op == XDR_DECODE)
+ {
+ if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
+ return FALSE;
+ *ullp = ((u_quad_t) t1) << 32;
+ *ullp |= (uint32_t) t2;
+ return TRUE;
+ }
+
+ if (xdrs->x_op == XDR_FREE)
+ return TRUE;
+
+ return FALSE;
+}
+INTDEF(xdr_u_hyper)
+
+bool_t
+xdr_longlong_t (XDR *xdrs, quad_t *llp)
+{
+ return INTUSE(xdr_hyper) (xdrs, llp);
+}
+
+bool_t
+xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
+{
+ return INTUSE(xdr_u_hyper) (xdrs, ullp);
+}
+
+/*
+ * XDR short integers
+ */
+bool_t
+xdr_short (XDR *xdrs, short *sp)
+{
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (long) *sp;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *sp = (short) l;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_short)
+
+/*
+ * XDR unsigned short integers
+ */
+bool_t
+xdr_u_short (XDR *xdrs, u_short *usp)
+{
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = (u_long) * usp;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *usp = (u_short) (u_long) l;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_u_short)
+
+
+/*
+ * XDR a char
+ */
+bool_t
+xdr_char (XDR *xdrs, char *cp)
+{
+ int i;
+
+ i = (*cp);
+ if (!INTUSE(xdr_int) (xdrs, &i))
+ {
+ return FALSE;
+ }
+ *cp = i;
+ return TRUE;
+}
+
+/*
+ * XDR an unsigned char
+ */
+bool_t
+xdr_u_char (XDR *xdrs, u_char *cp)
+{
+ u_int u;
+
+ u = (*cp);
+ if (!INTUSE(xdr_u_int) (xdrs, &u))
+ {
+ return FALSE;
+ }
+ *cp = u;
+ return TRUE;
+}
+
+/*
+ * XDR booleans
+ */
+bool_t
+xdr_bool (XDR *xdrs, bool_t *bp)
+{
+ long lb;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ lb = *bp ? XDR_TRUE : XDR_FALSE;
+ return XDR_PUTLONG (xdrs, &lb);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &lb))
+ {
+ return FALSE;
+ }
+ *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
+ return TRUE;
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_bool)
+
+/*
+ * XDR enumerations
+ */
+bool_t
+xdr_enum (XDR *xdrs, enum_t *ep)
+{
+ enum sizecheck
+ {
+ SIZEVAL
+ }; /* used to find the size of an enum */
+
+ /*
+ * enums are treated as ints
+ */
+ if (sizeof (enum sizecheck) == 4)
+ {
+#if INT_MAX < LONG_MAX
+ long l;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_ENCODE:
+ l = *ep;
+ return XDR_PUTLONG (xdrs, &l);
+
+ case XDR_DECODE:
+ if (!XDR_GETLONG (xdrs, &l))
+ {
+ return FALSE;
+ }
+ *ep = l;
+ case XDR_FREE:
+ return TRUE;
+
+ }
+ return FALSE;
+#else
+ return INTUSE(xdr_long) (xdrs, (long *) ep);
+#endif
+ }
+ else if (sizeof (enum sizecheck) == sizeof (short))
+ {
+ return INTUSE(xdr_short) (xdrs, (short *) ep);
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+INTDEF(xdr_enum)
+
+/*
+ * XDR opaque data
+ * Allows the specification of a fixed size sequence of opaque bytes.
+ * cp points to the opaque object and cnt gives the byte length.
+ */
+bool_t
+xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
+{
+ u_int rndup;
+ static char crud[BYTES_PER_XDR_UNIT];
+
+ /*
+ * if no data we are done
+ */
+ if (cnt == 0)
+ return TRUE;
+
+ /*
+ * round byte count to full xdr units
+ */
+ rndup = cnt % BYTES_PER_XDR_UNIT;
+ if (rndup > 0)
+ rndup = BYTES_PER_XDR_UNIT - rndup;
+
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (!XDR_GETBYTES (xdrs, cp, cnt))
+ {
+ return FALSE;
+ }
+ if (rndup == 0)
+ return TRUE;
+ return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
+
+ case XDR_ENCODE:
+ if (!XDR_PUTBYTES (xdrs, cp, cnt))
+ {
+ return FALSE;
+ }
+ if (rndup == 0)
+ return TRUE;
+ return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
+
+ case XDR_FREE:
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_opaque)
+
+/*
+ * XDR counted bytes
+ * *cpp is a pointer to the bytes, *sizep is the count.
+ * If *cpp is NULL maxsize bytes are allocated
+ */
+bool_t
+xdr_bytes (xdrs, cpp, sizep, maxsize)
+ XDR *xdrs;
+ char **cpp;
+ u_int *sizep;
+ u_int maxsize;
+{
+ char *sp = *cpp; /* sp is the actual string pointer */
+ u_int nodesize;
+
+ /*
+ * first deal with the length since xdr bytes are counted
+ */
+ if (!INTUSE(xdr_u_int) (xdrs, sizep))
+ {
+ return FALSE;
+ }
+ nodesize = *sizep;
+ if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
+ {
+ return FALSE;
+ }
+
+ /*
+ * now deal with the actual bytes
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (nodesize == 0)
+ {
+ return TRUE;
+ }
+ if (sp == NULL)
+ {
+ *cpp = sp = (char *) mem_alloc (nodesize);
+ }
+ if (sp == NULL)
+ {
+ fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
+ return FALSE;
+ }
+ /* fall into ... */
+
+ case XDR_ENCODE:
+ return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
+
+ case XDR_FREE:
+ if (sp != NULL)
+ {
+ mem_free (sp, nodesize);
+ *cpp = NULL;
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_bytes)
+
+/*
+ * Implemented here due to commonality of the object.
+ */
+bool_t
+xdr_netobj (xdrs, np)
+ XDR *xdrs;
+ struct netobj *np;
+{
+
+ return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
+}
+INTDEF(xdr_netobj)
+
+/*
+ * XDR a discriminated union
+ * Support routine for discriminated unions.
+ * You create an array of xdrdiscrim structures, terminated with
+ * an entry with a null procedure pointer. The routine gets
+ * the discriminant value and then searches the array of xdrdiscrims
+ * looking for that value. It calls the procedure given in the xdrdiscrim
+ * to handle the discriminant. If there is no specific routine a default
+ * routine may be called.
+ * If there is no specific or default routine an error is returned.
+ */
+bool_t
+xdr_union (xdrs, dscmp, unp, choices, dfault)
+ XDR *xdrs;
+ enum_t *dscmp; /* enum to decide which arm to work on */
+ char *unp; /* the union itself */
+ const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
+ xdrproc_t dfault; /* default xdr routine */
+{
+ enum_t dscm;
+
+ /*
+ * we deal with the discriminator; it's an enum
+ */
+ if (!INTUSE(xdr_enum) (xdrs, dscmp))
+ {
+ return FALSE;
+ }
+ dscm = *dscmp;
+
+ /*
+ * search choices for a value that matches the discriminator.
+ * if we find one, execute the xdr routine for that value.
+ */
+ for (; choices->proc != NULL_xdrproc_t; choices++)
+ {
+ if (choices->value == dscm)
+ return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
+ }
+
+ /*
+ * no match - execute the default xdr routine if there is one
+ */
+ return ((dfault == NULL_xdrproc_t) ? FALSE :
+ (*dfault) (xdrs, unp, LASTUNSIGNED));
+}
+INTDEF(xdr_union)
+
+
+/*
+ * Non-portable xdr primitives.
+ * Care should be taken when moving these routines to new architectures.
+ */
+
+
+/*
+ * XDR null terminated ASCII strings
+ * xdr_string deals with "C strings" - arrays of bytes that are
+ * terminated by a NULL character. The parameter cpp references a
+ * pointer to storage; If the pointer is null, then the necessary
+ * storage is allocated. The last parameter is the max allowed length
+ * of the string as specified by a protocol.
+ */
+bool_t
+xdr_string (xdrs, cpp, maxsize)
+ XDR *xdrs;
+ char **cpp;
+ u_int maxsize;
+{
+ char *sp = *cpp; /* sp is the actual string pointer */
+ u_int size;
+ u_int nodesize;
+
+ /*
+ * first deal with the length since xdr strings are counted-strings
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_FREE:
+ if (sp == NULL)
+ {
+ return TRUE; /* already free */
+ }
+ /* fall through... */
+ case XDR_ENCODE:
+ if (sp == NULL)
+ return FALSE;
+ size = strlen (sp);
+ break;
+ case XDR_DECODE:
+ break;
+ }
+ if (!INTUSE(xdr_u_int) (xdrs, &size))
+ {
+ return FALSE;
+ }
+ if (size > maxsize)
+ {
+ return FALSE;
+ }
+ nodesize = size + 1;
+ if (nodesize == 0)
+ {
+ /* This means an overflow. It a bug in the caller which
+ provided a too large maxsize but nevertheless catch it
+ here. */
+ return FALSE;
+ }
+
+ /*
+ * now deal with the actual bytes
+ */
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (sp == NULL)
+ *cpp = sp = (char *) mem_alloc (nodesize);
+ if (sp == NULL)
+ {
+ fprintf (NULL, "%s", "xdr_string: out of memory\n");
+ return FALSE;
+ }
+ sp[size] = 0;
+ /* fall into ... */
+
+ case XDR_ENCODE:
+ return INTUSE(xdr_opaque) (xdrs, sp, size);
+
+ case XDR_FREE:
+ mem_free (sp, nodesize);
+ *cpp = NULL;
+ return TRUE;
+ }
+ return FALSE;
+}
+INTDEF(xdr_string)
+
+/*
+ * Wrapper for xdr_string that can be called directly from
+ * routines like clnt_call
+ */
+bool_t
+xdr_wrapstring (xdrs, cpp)
+ XDR *xdrs;
+ char **cpp;
+{
+ if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
+ {
+ return TRUE;
+ }
+ return FALSE;
+}
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * xdr.h, External Data Representation Serialization Routines.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ */
+
+#ifndef _RPC_XDR_H
+#define _RPC_XDR_H 1
+
+#include <features.h>
+#include <sys/types.h>
+#include "types.h"
+
+/* We need FILE. */
+#include <stdio.h>
+
+__BEGIN_DECLS
+
+/*
+ * XDR provides a conventional way for converting between C data
+ * types and an external bit-string representation. Library supplied
+ * routines provide for the conversion on built-in C data types. These
+ * routines and utility routines defined here are used to help implement
+ * a type encode/decode routine for each user-defined type.
+ *
+ * Each data type provides a single procedure which takes two arguments:
+ *
+ * bool_t
+ * xdrproc(xdrs, argresp)
+ * XDR *xdrs;
+ * <type> *argresp;
+ *
+ * xdrs is an instance of a XDR handle, to which or from which the data
+ * type is to be converted. argresp is a pointer to the structure to be
+ * converted. The XDR handle contains an operation field which indicates
+ * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
+ *
+ * XDR_DECODE may allocate space if the pointer argresp is null. This
+ * data can be freed with the XDR_FREE operation.
+ *
+ * We write only one procedure per data type to make it easy
+ * to keep the encode and decode procedures for a data type consistent.
+ * In many cases the same code performs all operations on a user defined type,
+ * because all the hard work is done in the component type routines.
+ * decode as a series of calls on the nested data types.
+ */
+
+/*
+ * Xdr operations. XDR_ENCODE causes the type to be encoded into the
+ * stream. XDR_DECODE causes the type to be extracted from the stream.
+ * XDR_FREE can be used to release the space allocated by an XDR_DECODE
+ * request.
+ */
+enum xdr_op {
+ XDR_ENCODE = 0,
+ XDR_DECODE = 1,
+ XDR_FREE = 2
+};
+
+/*
+ * This is the number of bytes per unit of external data.
+ */
+#define BYTES_PER_XDR_UNIT (4)
+/*
+ * This only works if the above is a power of 2. But it's defined to be
+ * 4 by the appropriate RFCs. So it will work. And it's normally quicker
+ * than the old routine.
+ */
+#if 1
+#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
+#else /* this is the old routine */
+#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
+ * BYTES_PER_XDR_UNIT)
+#endif
+
+/*
+ * The XDR handle.
+ * Contains operation which is being applied to the stream,
+ * an operations vector for the particular implementation (e.g. see xdr_mem.c),
+ * and two private fields for the use of the particular implementation.
+ */
+typedef struct XDR XDR;
+struct XDR
+ {
+ enum xdr_op x_op; /* operation; fast additional param */
+ struct xdr_ops
+ {
+ bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
+ /* get a long from underlying stream */
+ bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
+ /* put a long to " */
+ bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
+ /* get some bytes from " */
+ bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
+ /* put some bytes to " */
+ u_int (*x_getpostn) (__const XDR *__xdrs);
+ /* returns bytes off from beginning */
+ bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
+ /* lets you reposition the stream */
+ int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
+ /* buf quick ptr to buffered data */
+ void (*x_destroy) (XDR *__xdrs);
+ /* free privates of this xdr_stream */
+ bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
+ /* get a int from underlying stream */
+ bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
+ /* put a int to " */
+ }
+ *x_ops;
+ caddr_t x_public; /* users' data */
+ caddr_t x_private; /* pointer to private data */
+ caddr_t x_base; /* private used for position info */
+ u_int x_handy; /* extra private word */
+ };
+
+/*
+ * A xdrproc_t exists for each data type which is to be encoded or decoded.
+ *
+ * The second argument to the xdrproc_t is a pointer to an opaque pointer.
+ * The opaque pointer generally points to a structure of the data type
+ * to be decoded. If this pointer is 0, then the type routines should
+ * allocate dynamic storage of the appropriate size and return it.
+ * bool_t (*xdrproc_t)(XDR *, caddr_t *);
+ */
+typedef bool_t (*xdrproc_t) (XDR *, void *,...);
+
+
+/*
+ * Operations defined on a XDR handle
+ *
+ * XDR *xdrs;
+ * int32_t *int32p;
+ * long *longp;
+ * caddr_t addr;
+ * u_int len;
+ * u_int pos;
+ */
+#define XDR_GETINT32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
+#define xdr_getint32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
+
+#define XDR_PUTINT32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
+#define xdr_putint32(xdrs, int32p) \
+ (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
+
+#define XDR_GETLONG(xdrs, longp) \
+ (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
+#define xdr_getlong(xdrs, longp) \
+ (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
+
+#define XDR_PUTLONG(xdrs, longp) \
+ (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
+#define xdr_putlong(xdrs, longp) \
+ (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
+
+#define XDR_GETBYTES(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
+#define xdr_getbytes(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
+
+#define XDR_PUTBYTES(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
+#define xdr_putbytes(xdrs, addr, len) \
+ (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
+
+#define XDR_GETPOS(xdrs) \
+ (*(xdrs)->x_ops->x_getpostn)(xdrs)
+#define xdr_getpos(xdrs) \
+ (*(xdrs)->x_ops->x_getpostn)(xdrs)
+
+#define XDR_SETPOS(xdrs, pos) \
+ (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
+#define xdr_setpos(xdrs, pos) \
+ (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
+
+#define XDR_INLINE(xdrs, len) \
+ (*(xdrs)->x_ops->x_inline)(xdrs, len)
+#define xdr_inline(xdrs, len) \
+ (*(xdrs)->x_ops->x_inline)(xdrs, len)
+
+#define XDR_DESTROY(xdrs) \
+ do { \
+ if ((xdrs)->x_ops->x_destroy) \
+ (*(xdrs)->x_ops->x_destroy)(xdrs); \
+ } while (0)
+#define xdr_destroy(xdrs) \
+ do { \
+ if ((xdrs)->x_ops->x_destroy) \
+ (*(xdrs)->x_ops->x_destroy)(xdrs); \
+ } while (0)
+
+/*
+ * Support struct for discriminated unions.
+ * You create an array of xdrdiscrim structures, terminated with
+ * a entry with a null procedure pointer. The xdr_union routine gets
+ * the discriminant value and then searches the array of structures
+ * for a matching value. If a match is found the associated xdr routine
+ * is called to handle that part of the union. If there is
+ * no match, then a default routine may be called.
+ * If there is no match and no default routine it is an error.
+ */
+#define NULL_xdrproc_t ((xdrproc_t)0)
+struct xdr_discrim
+{
+ int value;
+ xdrproc_t proc;
+};
+
+/*
+ * Inline routines for fast encode/decode of primitive data types.
+ * Caveat emptor: these use single memory cycles to get the
+ * data from the underlying buffer, and will fail to operate
+ * properly if the data is not aligned. The standard way to use these
+ * is to say:
+ * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
+ * return (FALSE);
+ * <<< macro calls >>>
+ * where ``count'' is the number of bytes of data occupied
+ * by the primitive data types.
+ *
+ * N.B. and frozen for all time: each data type here uses 4 bytes
+ * of external representation.
+ */
+
+#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
+#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
+#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
+#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
+
+/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
+ * and shouldn't be used any longer. Code which use this defines or longs
+ * in the RPC code will not work on 64bit Solaris platforms !
+ */
+#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
+#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
+#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
+#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+
+
+#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
+#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
+#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
+#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
+
+#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
+
+/*
+ * These are the "generic" xdr routines.
+ * None of these can have const applied because it's not possible to
+ * know whether the call is a read or a write to the passed parameter
+ * also, the XDR structure is always updated by some of these calls.
+ */
+extern bool_t xdr_void (void) __THROW;
+extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
+extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
+extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
+extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
+extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
+extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
+extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
+extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
+extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
+extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
+extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
+extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
+extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
+extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
+extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
+extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
+extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
+extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
+extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
+extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
+extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
+extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
+extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
+ u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
+ __THROW;
+extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
+ u_int __maxsize) __THROW;
+extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
+extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
+extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
+ __const struct xdr_discrim *__choices,
+ xdrproc_t dfault) __THROW;
+extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
+extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
+extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
+ u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
+extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
+extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
+extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
+ xdrproc_t __proc) __THROW;
+extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
+ u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
+extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
+extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
+
+/*
+ * Common opaque bytes objects used by many rpc protocols;
+ * declared here due to commonality.
+ */
+#define MAX_NETOBJ_SZ 1024
+struct netobj
+{
+ u_int n_len;
+ char *n_bytes;
+};
+typedef struct netobj netobj;
+extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
+
+/*
+ * These are the public routines for the various implementations of
+ * xdr streams.
+ */
+
+/* XDR using memory buffers */
+extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
+ u_int __size, enum xdr_op __xop) __THROW;
+
+/* XDR using stdio library */
+extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
+ __THROW;
+
+/* XDR pseudo records for tcp */
+extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
+ u_int __recvsize, caddr_t __tcp_handle,
+ int (*__readit) (char *, char *, int),
+ int (*__writeit) (char *, char *, int)) __THROW;
+
+/* make end of xdr record */
+extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
+
+/* move to beginning of next record */
+extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
+
+/* true if no more input */
+extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
+
+/* free memory buffers for xdr */
+extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
+
+__END_DECLS
+
+#endif /* rpc/xdr.h */
--- /dev/null
+# define INTUSE(name) name
+# define INTDEF(name)
+/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
+#endif
+
+/*
+ * xdr_array.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * These are the "non-trivial" xdr primitives used to serialize and de-serialize
+ * arrays. See xdr.h for more info on the interface to xdr.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "types.h"
+#include "xdr.h"
+#include <libintl.h>
+#include <limits.h>
+
+#ifdef USE_IN_LIBIO
+# include <wchar.h>
+#endif
+
+#define LASTUNSIGNED ((u_int)0-1)
+
+
+/*
+ * XDR an array of arbitrary elements
+ * *addrp is a pointer to the array, *sizep is the number of elements.
+ * If addrp is NULL (*sizep * elsize) bytes are allocated.
+ * elsize is the size (in bytes) of each element, and elproc is the
+ * xdr procedure to call to handle each element of the array.
+ */
+bool_t
+xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
+ XDR *xdrs;
+ caddr_t *addrp; /* array pointer */
+ u_int *sizep; /* number of elements */
+ u_int maxsize; /* max numberof elements */
+ u_int elsize; /* size in bytes of each element */
+ xdrproc_t elproc; /* xdr routine to handle each element */
+{
+ u_int i;
+ caddr_t target = *addrp;
+ u_int c; /* the actual element count */
+ bool_t stat = TRUE;
+ u_int nodesize;
+
+ /* like strings, arrays are really counted arrays */
+ if (!INTUSE(xdr_u_int) (xdrs, sizep))
+ {
+ return FALSE;
+ }
+ c = *sizep;
+ /*
+ * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
+ * doesn't actually use its second argument anyway.
+ */
+ if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
+ {
+ return FALSE;
+ }
+ nodesize = c * elsize;
+
+ /*
+ * if we are deserializing, we may need to allocate an array.
+ * We also save time by checking for a null array if we are freeing.
+ */
+ if (target == NULL)
+ switch (xdrs->x_op)
+ {
+ case XDR_DECODE:
+ if (c == 0)
+ return TRUE;
+ *addrp = target = mem_alloc (nodesize);
+ if (target == NULL)
+ {
+ fprintf (stderr, "%s", "xdr_array: out of memory\n");
+ return FALSE;
+ }
+ __bzero (target, nodesize);
+ break;
+
+ case XDR_FREE:
+ return TRUE;
+ default:
+ break;
+ }
+
+ /*
+ * now we xdr each element of array
+ */
+ for (i = 0; (i < c) && stat; i++)
+ {
+ stat = (*elproc) (xdrs, target, LASTUNSIGNED);
+ target += elsize;
+ }
+
+ /*
+ * the array may need freeing
+ */
+ if (xdrs->x_op == XDR_FREE)
+ {
+ mem_free (*addrp, nodesize);
+ *addrp = NULL;
+ }
+ return stat;
+}
+INTDEF(xdr_array)
+
+/*
+ * xdr_vector():
+ *
+ * XDR a fixed length array. Unlike variable-length arrays,
+ * the storage of fixed length arrays is static and unfreeable.
+ * > basep: base of the array
+ * > size: size of the array
+ * > elemsize: size of each element
+ * > xdr_elem: routine to XDR each element
+ */
+bool_t
+xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
+ XDR *xdrs;
+ char *basep;
+ u_int nelem;
+ u_int elemsize;
+ xdrproc_t xdr_elem;
+{
+ u_int i;
+ char *elptr;
+
+ elptr = basep;
+ for (i = 0; i < nelem; i++)
+ {
+ if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
+ {
+ return FALSE;
+ }
+ elptr += elemsize;
+ }
+ return TRUE;
+}
--- /dev/null
+/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+#if !defined(lint) && defined(SCCSIDS)
+static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
+#endif
+
+/*
+ * xdr_float.c, Generic XDR routines implementation.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * These are the "floating point" xdr routines used to (de)serialize
+ * most common data items. See xdr.h for more info on the interface to
+ * xdr.
+ */
+
+#include <stdio.h>
+#include <endian.h>
+
+#include "types.h"
+#include "xdr.h"
+
+/*
+ * NB: Not portable.
+ * This routine works on Suns (Sky / 68000's) and Vaxen.
+ */
+
+#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
+
+#ifdef vax
+
+/* What IEEE single precision floating point looks like on a Vax */
+struct ieee_single {
+ unsigned int mantissa: 23;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+};
+
+/* Vax single precision floating point */
+struct vax_single {
+ unsigned int mantissa1 : 7;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 16;
+};
+
+#define VAX_SNG_BIAS 0x81
+#define IEEE_SNG_BIAS 0x7f
+
+static struct sgl_limits {
+ struct vax_single s;
+ struct ieee_single ieee;
+} sgl_limits[2] = {
+ {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
+ { 0x0, 0xff, 0x0 }}, /* Max IEEE */
+ {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
+ { 0x0, 0x0, 0x0 }} /* Min IEEE */
+};
+#endif /* vax */
+
+bool_t
+xdr_float(xdrs, fp)
+ XDR *xdrs;
+ float *fp;
+{
+#ifdef vax
+ struct ieee_single is;
+ struct vax_single vs, *vsp;
+ struct sgl_limits *lim;
+ int i;
+#endif
+ switch (xdrs->x_op) {
+
+ case XDR_ENCODE:
+#ifdef vax
+ vs = *((struct vax_single *)fp);
+ for (i = 0, lim = sgl_limits;
+ i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
+ i++, lim++) {
+ if ((vs.mantissa2 == lim->s.mantissa2) &&
+ (vs.exp == lim->s.exp) &&
+ (vs.mantissa1 == lim->s.mantissa1)) {
+ is = lim->ieee;
+ goto shipit;
+ }
+ }
+ is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
+ is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
+ shipit:
+ is.sign = vs.sign;
+ return (XDR_PUTLONG(xdrs, (long *)&is));
+#else
+ if (sizeof(float) == sizeof(long))
+ return (XDR_PUTLONG(xdrs, (long *)fp));
+ else if (sizeof(float) == sizeof(int)) {
+ long tmp = *(int *)fp;
+ return (XDR_PUTLONG(xdrs, &tmp));
+ }
+ break;
+#endif
+
+ case XDR_DECODE:
+#ifdef vax
+ vsp = (struct vax_single *)fp;
+ if (!XDR_GETLONG(xdrs, (long *)&is))
+ return (FALSE);
+ for (i = 0, lim = sgl_limits;
+ i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
+ i++, lim++) {
+ if ((is.exp == lim->ieee.exp) &&
+ (is.mantissa == lim->ieee.mantissa)) {
+ *vsp = lim->s;
+ goto doneit;
+ }
+ }
+ vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
+ vsp->mantissa2 = is.mantissa;
+ vsp->mantissa1 = (is.mantissa >> 16);
+ doneit:
+ vsp->sign = is.sign;
+ return (TRUE);
+#else
+ if (sizeof(float) == sizeof(long))
+ return (XDR_GETLONG(xdrs, (long *)fp));
+ else if (sizeof(float) == sizeof(int)) {
+ long tmp;
+ if (XDR_GETLONG(xdrs, &tmp)) {
+ *(int *)fp = tmp;
+ return (TRUE);
+ }
+ }
+ break;
+#endif
+
+ case XDR_FREE:
+ return (TRUE);
+ }
+ return (FALSE);
+}
+
+/*
+ * This routine works on Suns (Sky / 68000's) and Vaxen.
+ */
+
+#ifdef vax
+/* What IEEE double precision floating point looks like on a Vax */
+struct ieee_double {
+ unsigned int mantissa1 : 20;
+ unsigned int exp : 11;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 32;
+};
+
+/* Vax double precision floating point */
+struct vax_double {
+ unsigned int mantissa1 : 7;
+ unsigned int exp : 8;
+ unsigned int sign : 1;
+ unsigned int mantissa2 : 16;
+ unsigned int mantissa3 : 16;
+ unsigned int mantissa4 : 16;
+};
+
+#define VAX_DBL_BIAS 0x81
+#define IEEE_DBL_BIAS 0x3ff
+#define MASK(nbits) ((1 << nbits) - 1)
+
+static struct dbl_limits {
+ struct vax_double d;
+ struct ieee_double ieee;
+} dbl_limits[2] = {
+ {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
+ { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
+ {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
+ { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
+};
+
+#endif /* vax */
+
+
+bool_t
+xdr_double(xdrs, dp)
+ XDR *xdrs;
+ double *dp;
+{
+#ifdef vax
+ struct ieee_double id;
+ struct vax_double vd;
+ register struct dbl_limits *lim;
+ int i;
+#endif
+
+ switch (xdrs->x_op) {
+
+ case XDR_ENCODE:
+#ifdef vax
+ vd = *((struct vax_double *)dp);
+ for (i = 0, lim = dbl_limits;
+ i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
+ i++, lim++) {
+ if ((vd.mantissa4 == lim->d.mantissa4) &&
+ (vd.mantissa3 == lim->d.mantissa3) &&
+ (vd.mantissa2 == lim->d.mantissa2) &&
+ (vd.mantissa1 == lim->d.mantissa1) &&
+ (vd.exp == lim->d.exp)) {
+ id = lim->ieee;
+ goto shipit;
+ }
+ }
+ id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
+ id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
+ id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
+ (vd.mantissa3 << 13) |
+ ((vd.mantissa4 >> 3) & MASK(13));
+ shipit:
+ id.sign = vd.sign;
+ dp = (double *)&id;
+#endif
+ if (2*sizeof(long) == sizeof(double)) {
+ long *lp = (long *)dp;
+ return (XDR_PUTLONG(xdrs, lp+!LSW) &&
+ XDR_PUTLONG(xdrs, lp+LSW));
+ } else if (2*sizeof(int) == sizeof(double)) {
+ int *ip = (int *)dp;
+ long tmp[2];
+ tmp[0] = ip[!LSW];
+ tmp[1] = ip[LSW];
+ return (XDR_PUTLONG(xdrs, tmp) &&
+ XDR_PUTLONG(xdrs, tmp+1));
+ }
+ break;
+
+ case XDR_DECODE:
+#ifdef vax
+ lp = (long *)&id;
+ if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
+ return (FALSE);
+ for (i = 0, lim = dbl_limits;
+ i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
+ i++, lim++) {
+ if ((id.mantissa2 == lim->ieee.mantissa2) &&
+ (id.mantissa1 == lim->ieee.mantissa1) &&
+ (id.exp == lim->ieee.exp)) {
+ vd = lim->d;
+ goto doneit;
+ }
+ }
+ vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
+ vd.mantissa1 = (id.mantissa1 >> 13);
+ vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
+ (id.mantissa2 >> 29);
+ vd.mantissa3 = (id.mantissa2 >> 13);
+ vd.mantissa4 = (id.mantissa2 << 3);
+ doneit:
+ vd.sign = id.sign;
+ *dp = *((double *)&vd);
+ return (TRUE);
+#else
+ if (2*sizeof(long) == sizeof(double)) {
+ long *lp = (long *)dp;
+ return (XDR_GETLONG(xdrs, lp+!LSW) &&
+ XDR_GETLONG(xdrs, lp+LSW));
+ } else if (2*sizeof(int) == sizeof(double)) {
+ int *ip = (int *)dp;
+ long tmp[2];
+ if (XDR_GETLONG(xdrs, tmp+!LSW) &&
+ XDR_GETLONG(xdrs, tmp+LSW)) {
+ ip[0] = tmp[0];
+ ip[1] = tmp[1];
+ return (TRUE);
+ }
+ }
+ break;
+#endif
+
+ case XDR_FREE:
+ return (TRUE);
+ }
+ return (FALSE);
+}
--- /dev/null
+/*
+ * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
+ * unrestricted use provided that this legend is included on all tape
+ * media and as a part of the software program in whole or part. Users
+ * may copy or modify Sun RPC without charge, but are not authorized
+ * to license or distribute it to anyone else except as part of a product or
+ * program developed by the user.
+ *
+ * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
+ * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun RPC is provided with no support and without any obligation on the
+ * part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California 94043
+ */
+
+/*
+ * xdr_stdio.c, XDR implementation on standard i/o file.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * This set of routines implements a XDR on a stdio stream.
+ * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
+ * from the stream.
+ */
+
+#include "types.h"
+#include <stdio.h>
+#include "xdr.h"
+
+#ifdef USE_IN_LIBIO
+# include <libio/iolibio.h>
+# define fflush(s) INTUSE(_IO_fflush) (s)
+# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
+# define ftell(s) INTUSE(_IO_ftell) (s)
+# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
+#endif
+
+static bool_t xdrstdio_getlong (XDR *, long *);
+static bool_t xdrstdio_putlong (XDR *, const long *);
+static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
+static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
+static u_int xdrstdio_getpos (const XDR *);
+static bool_t xdrstdio_setpos (XDR *, u_int);
+static int32_t *xdrstdio_inline (XDR *, u_int);
+static void xdrstdio_destroy (XDR *);
+static bool_t xdrstdio_getint32 (XDR *, int32_t *);
+static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
+
+/*
+ * Ops vector for stdio type XDR
+ */
+static const struct xdr_ops xdrstdio_ops =
+{
+ xdrstdio_getlong, /* deserialize a long int */
+ xdrstdio_putlong, /* serialize a long int */
+ xdrstdio_getbytes, /* deserialize counted bytes */
+ xdrstdio_putbytes, /* serialize counted bytes */
+ xdrstdio_getpos, /* get offset in the stream */
+ xdrstdio_setpos, /* set offset in the stream */
+ xdrstdio_inline, /* prime stream for inline macros */
+ xdrstdio_destroy, /* destroy stream */
+ xdrstdio_getint32, /* deserialize a int */
+ xdrstdio_putint32 /* serialize a int */
+};
+
+/*
+ * Initialize a stdio xdr stream.
+ * Sets the xdr stream handle xdrs for use on the stream file.
+ * Operation flag is set to op.
+ */
+void
+xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
+{
+ xdrs->x_op = op;
+ /* We have to add the const since the `struct xdr_ops' in `struct XDR'
+ is not `const'. */
+ xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
+ xdrs->x_private = (caddr_t) file;
+ xdrs->x_handy = 0;
+ xdrs->x_base = 0;
+}
+
+/*
+ * Destroy a stdio xdr stream.
+ * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
+ */
+static void
+xdrstdio_destroy (XDR *xdrs)
+{
+ (void) fflush ((FILE *) xdrs->x_private);
+ /* xx should we close the file ?? */
+};
+
+static bool_t
+xdrstdio_getlong (XDR *xdrs, long *lp)
+{
+ u_int32_t mycopy;
+
+ if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ *lp = (long) ntohl (mycopy);
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putlong (XDR *xdrs, const long *lp)
+{
+ int32_t mycopy = htonl ((u_int32_t) *lp);
+
+ if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
+{
+ if ((len != 0) && (fread (addr, (int) len, 1,
+ (FILE *) xdrs->x_private) != 1))
+ return FALSE;
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
+{
+ if ((len != 0) && (fwrite (addr, (int) len, 1,
+ (FILE *) xdrs->x_private) != 1))
+ return FALSE;
+ return TRUE;
+}
+
+static u_int
+xdrstdio_getpos (const XDR *xdrs)
+{
+ return (u_int) ftell ((FILE *) xdrs->x_private);
+}
+
+static bool_t
+xdrstdio_setpos (XDR *xdrs, u_int pos)
+{
+ return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
+}
+
+static int32_t *
+xdrstdio_inline (XDR *xdrs, u_int len)
+{
+ /*
+ * Must do some work to implement this: must insure
+ * enough data in the underlying stdio buffer,
+ * that the buffer is aligned so that we can indirect through a
+ * long *, and stuff this pointer in xdrs->x_buf. Doing
+ * a fread or fwrite to a scratch buffer would defeat
+ * most of the gains to be had here and require storage
+ * management on this buffer, so we don't do this.
+ */
+ return NULL;
+}
+
+static bool_t
+xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
+{
+ int32_t mycopy;
+
+ if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ *ip = ntohl (mycopy);
+ return TRUE;
+}
+
+static bool_t
+xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
+{
+ int32_t mycopy = htonl (*ip);
+
+ ip = &mycopy;
+ if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
+ return FALSE;
+ return TRUE;
+}
+
+/* libc_hidden_def (xdrstdio_create) */
--- /dev/null
+/*_________________________________________________________________
+ |
+ | xdrf.h - include file for C routines that want to use the
+ | functions below.
+*/
+
+int xdropen(XDR *xdrs, const char *filename, const char *type);
+int xdrclose(XDR *xdrs) ;
+int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
+
#================================
# build the xdrf library
#================================
-add_subdirectory(xdrf)
+#add_subdirectory(xdrf)
#================================
# Set source file lists
endif(UNRES_WITH_MPI)
# link libxdrf.a
#message("UNRES_XDRFLIB=${UNRES_XDRFLIB}")
-target_link_libraries( UNRES_BIN-MD-M xdrf-MD-M )
+target_link_libraries( UNRES_BIN-MD-M xdrf )
#=========================================
# TESTS
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
-
-CC = /usr/bin/blrts_xlc
-CPPC = /usr/bin/blrts_xlc
-
-CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
-
-M4 = m4
-M4FILE = RS6K.m4
-
-libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f *.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
-# rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
-
-CC = /usr/bin/blrts_xlc
-CPPC = /usr/bin/blrts_xlc
-
-CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
-
-M4 = m4
-M4FILE = RS6K.m4
-
-libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
-# rm -f libxdrf.c
-
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# do nothing special to FORTRAN function names
-#
-define(`FUNCTION',`$1')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
-
+++ /dev/null
-
-
-int ftocstr(ds, dl, ss, sl)
- char *ds, *ss; /* dst, src ptrs */
- int dl; /* dst max len */
- int sl; /* src len */
-{
- char *p;
-
- for (p = ss + sl; --p >= ss && *p == ' '; ) ;
- sl = p - ss + 1;
- dl--;
- ds[0] = 0;
- if (sl > dl)
- return 1;
- while (sl--)
- (*ds++ = *ss++);
- *ds = '\0';
- return 0;
-}
-
-
-int ctofstr(ds, dl, ss)
- char *ds; /* dest space */
- int dl; /* max dest length */
- char *ss; /* src string (0-term) */
-{
- while (dl && *ss) {
- *ds++ = *ss++;
- dl--;
- }
- while (dl--)
- *ds++ = ' ';
- return 0;
-}
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-/* #include <rpc/rpc.h>
-#include <rpc/xdr.h> */
-#include "xdr.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "w+";
- lmode = XDR_ENCODE;
- } else if (*type == 'a' || *type == 'A') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-/* #include <rpc/rpc.h>
-#include <rpc/xdr.h> */
-#include "xdr.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-/* fixincludes should not add extern "C" to this file */
-/*
- * Rpc additions to <sys/types.h>
- */
-#ifndef _RPC_TYPES_H
-#define _RPC_TYPES_H 1
-
-typedef int bool_t;
-typedef int enum_t;
-/* This needs to be changed to uint32_t in the future */
-typedef unsigned long rpcprog_t;
-typedef unsigned long rpcvers_t;
-typedef unsigned long rpcproc_t;
-typedef unsigned long rpcprot_t;
-typedef unsigned long rpcport_t;
-
-#define __dontcare__ -1
-
-#ifndef FALSE
-# define FALSE (0)
-#endif
-
-#ifndef TRUE
-# define TRUE (1)
-#endif
-
-#ifndef NULL
-# define NULL 0
-#endif
-
-#include <stdlib.h> /* For malloc decl. */
-#define mem_alloc(bsize) malloc(bsize)
-/*
- * XXX: This must not use the second argument, or code in xdr_array.c needs
- * to be modified.
- */
-#define mem_free(ptr, bsize) free(ptr)
-
-#ifndef makedev /* ie, we haven't already included it */
-#include <sys/types.h>
-#endif
-
-#ifndef __u_char_defined
-typedef __u_char u_char;
-typedef __u_short u_short;
-typedef __u_int u_int;
-typedef __u_long u_long;
-typedef __quad_t quad_t;
-typedef __u_quad_t u_quad_t;
-typedef __fsid_t fsid_t;
-# define __u_char_defined
-#endif
-#ifndef __daddr_t_defined
-typedef __daddr_t daddr_t;
-typedef __caddr_t caddr_t;
-# define __daddr_t_defined
-#endif
-
-#include <sys/time.h>
-#include <sys/param.h>
-
-#include <netinet/in.h>
-
-#ifndef INADDR_LOOPBACK
-#define INADDR_LOOPBACK (u_long)0x7F000001
-#endif
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 64
-#endif
-
-#endif /* rpc/types.h */
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# append an underscore to FORTRAN function names
-#
-define(`FUNCTION',`$1_')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
-#endif
-
-/*
- * xdr.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1986, Sun Microsystems, Inc.
- *
- * These are the "generic" xdr routines used to serialize and de-serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <libintl.h>
-
-#include "types.h"
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-/*
- * constants specific to the xdr "protocol"
- */
-#define XDR_FALSE ((long) 0)
-#define XDR_TRUE ((long) 1)
-#define LASTUNSIGNED ((u_int) 0-1)
-
-/*
- * for unit alignment
- */
-static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
-
-/*
- * Free a data structure using XDR
- * Not a filter, but a convenient utility nonetheless
- */
-void
-xdr_free (xdrproc_t proc, char *objp)
-{
- XDR x;
-
- x.x_op = XDR_FREE;
- (*proc) (&x, objp);
-}
-
-/*
- * XDR nothing
- */
-bool_t
-xdr_void (void)
-{
- return TRUE;
-}
-INTDEF(xdr_void)
-
-/*
- * XDR integers
- */
-bool_t
-xdr_int (XDR *xdrs, int *ip)
-{
-
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *ip;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ip = (int) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif INT_MAX == LONG_MAX
- return INTUSE(xdr_long) (xdrs, (long *) ip);
-#elif INT_MAX == SHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) ip);
-#else
-#error unexpected integer sizes in_xdr_int()
-#endif
-}
-INTDEF(xdr_int)
-
-/*
- * XDR unsigned integers
- */
-bool_t
-xdr_u_int (XDR *xdrs, u_int *up)
-{
-#if UINT_MAX < ULONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * up;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *up = (u_int) (u_long) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif UINT_MAX == ULONG_MAX
- return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
-#elif UINT_MAX == USHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) up);
-#else
-#error unexpected integer sizes in_xdr_u_int()
-#endif
-}
-INTDEF(xdr_u_int)
-
-/*
- * XDR long integers
- * The definition of xdr_long() is kept for backward
- * compatibility. Instead xdr_int() should be used.
- */
-bool_t
-xdr_long (XDR *xdrs, long *lp)
-{
-
- if (xdrs->x_op == XDR_ENCODE
- && (sizeof (int32_t) == sizeof (long)
- || (int32_t) *lp == *lp))
- return XDR_PUTLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_DECODE)
- return XDR_GETLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_long)
-
-/*
- * XDR unsigned long integers
- * The definition of xdr_u_long() is kept for backward
- * compatibility. Instead xdr_u_int() should be used.
- */
-bool_t
-xdr_u_long (XDR *xdrs, u_long *ulp)
-{
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- {
- long int tmp;
-
- if (XDR_GETLONG (xdrs, &tmp) == FALSE)
- return FALSE;
-
- *ulp = (uint32_t) tmp;
- return TRUE;
- }
-
- case XDR_ENCODE:
- if (sizeof (uint32_t) != sizeof (u_long)
- && (uint32_t) *ulp != *ulp)
- return FALSE;
-
- return XDR_PUTLONG (xdrs, (long *) ulp);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_long)
-
-/*
- * XDR hyper integers
- * same as xdr_u_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_hyper (XDR *xdrs, quad_t *llp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (long) ((*llp) >> 32);
- t2 = (long) (*llp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *llp = ((quad_t) t1) << 32;
- *llp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_hyper)
-
-
-/*
- * XDR hyper integers
- * same as xdr_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (unsigned long) ((*ullp) >> 32);
- t2 = (unsigned long) (*ullp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *ullp = ((u_quad_t) t1) << 32;
- *ullp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_u_hyper)
-
-bool_t
-xdr_longlong_t (XDR *xdrs, quad_t *llp)
-{
- return INTUSE(xdr_hyper) (xdrs, llp);
-}
-
-bool_t
-xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
-{
- return INTUSE(xdr_u_hyper) (xdrs, ullp);
-}
-
-/*
- * XDR short integers
- */
-bool_t
-xdr_short (XDR *xdrs, short *sp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *sp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *sp = (short) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_short)
-
-/*
- * XDR unsigned short integers
- */
-bool_t
-xdr_u_short (XDR *xdrs, u_short *usp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * usp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *usp = (u_short) (u_long) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_short)
-
-
-/*
- * XDR a char
- */
-bool_t
-xdr_char (XDR *xdrs, char *cp)
-{
- int i;
-
- i = (*cp);
- if (!INTUSE(xdr_int) (xdrs, &i))
- {
- return FALSE;
- }
- *cp = i;
- return TRUE;
-}
-
-/*
- * XDR an unsigned char
- */
-bool_t
-xdr_u_char (XDR *xdrs, u_char *cp)
-{
- u_int u;
-
- u = (*cp);
- if (!INTUSE(xdr_u_int) (xdrs, &u))
- {
- return FALSE;
- }
- *cp = u;
- return TRUE;
-}
-
-/*
- * XDR booleans
- */
-bool_t
-xdr_bool (XDR *xdrs, bool_t *bp)
-{
- long lb;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- lb = *bp ? XDR_TRUE : XDR_FALSE;
- return XDR_PUTLONG (xdrs, &lb);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &lb))
- {
- return FALSE;
- }
- *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bool)
-
-/*
- * XDR enumerations
- */
-bool_t
-xdr_enum (XDR *xdrs, enum_t *ep)
-{
- enum sizecheck
- {
- SIZEVAL
- }; /* used to find the size of an enum */
-
- /*
- * enums are treated as ints
- */
- if (sizeof (enum sizecheck) == 4)
- {
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = *ep;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ep = l;
- case XDR_FREE:
- return TRUE;
-
- }
- return FALSE;
-#else
- return INTUSE(xdr_long) (xdrs, (long *) ep);
-#endif
- }
- else if (sizeof (enum sizecheck) == sizeof (short))
- {
- return INTUSE(xdr_short) (xdrs, (short *) ep);
- }
- else
- {
- return FALSE;
- }
-}
-INTDEF(xdr_enum)
-
-/*
- * XDR opaque data
- * Allows the specification of a fixed size sequence of opaque bytes.
- * cp points to the opaque object and cnt gives the byte length.
- */
-bool_t
-xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
-{
- u_int rndup;
- static char crud[BYTES_PER_XDR_UNIT];
-
- /*
- * if no data we are done
- */
- if (cnt == 0)
- return TRUE;
-
- /*
- * round byte count to full xdr units
- */
- rndup = cnt % BYTES_PER_XDR_UNIT;
- if (rndup > 0)
- rndup = BYTES_PER_XDR_UNIT - rndup;
-
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (!XDR_GETBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
-
- case XDR_ENCODE:
- if (!XDR_PUTBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_opaque)
-
-/*
- * XDR counted bytes
- * *cpp is a pointer to the bytes, *sizep is the count.
- * If *cpp is NULL maxsize bytes are allocated
- */
-bool_t
-xdr_bytes (xdrs, cpp, sizep, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int *sizep;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int nodesize;
-
- /*
- * first deal with the length since xdr bytes are counted
- */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- nodesize = *sizep;
- if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (nodesize == 0)
- {
- return TRUE;
- }
- if (sp == NULL)
- {
- *cpp = sp = (char *) mem_alloc (nodesize);
- }
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
- return FALSE;
- }
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
-
- case XDR_FREE:
- if (sp != NULL)
- {
- mem_free (sp, nodesize);
- *cpp = NULL;
- }
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bytes)
-
-/*
- * Implemented here due to commonality of the object.
- */
-bool_t
-xdr_netobj (xdrs, np)
- XDR *xdrs;
- struct netobj *np;
-{
-
- return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
-}
-INTDEF(xdr_netobj)
-
-/*
- * XDR a discriminated union
- * Support routine for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * an entry with a null procedure pointer. The routine gets
- * the discriminant value and then searches the array of xdrdiscrims
- * looking for that value. It calls the procedure given in the xdrdiscrim
- * to handle the discriminant. If there is no specific routine a default
- * routine may be called.
- * If there is no specific or default routine an error is returned.
- */
-bool_t
-xdr_union (xdrs, dscmp, unp, choices, dfault)
- XDR *xdrs;
- enum_t *dscmp; /* enum to decide which arm to work on */
- char *unp; /* the union itself */
- const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
- xdrproc_t dfault; /* default xdr routine */
-{
- enum_t dscm;
-
- /*
- * we deal with the discriminator; it's an enum
- */
- if (!INTUSE(xdr_enum) (xdrs, dscmp))
- {
- return FALSE;
- }
- dscm = *dscmp;
-
- /*
- * search choices for a value that matches the discriminator.
- * if we find one, execute the xdr routine for that value.
- */
- for (; choices->proc != NULL_xdrproc_t; choices++)
- {
- if (choices->value == dscm)
- return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
- }
-
- /*
- * no match - execute the default xdr routine if there is one
- */
- return ((dfault == NULL_xdrproc_t) ? FALSE :
- (*dfault) (xdrs, unp, LASTUNSIGNED));
-}
-INTDEF(xdr_union)
-
-
-/*
- * Non-portable xdr primitives.
- * Care should be taken when moving these routines to new architectures.
- */
-
-
-/*
- * XDR null terminated ASCII strings
- * xdr_string deals with "C strings" - arrays of bytes that are
- * terminated by a NULL character. The parameter cpp references a
- * pointer to storage; If the pointer is null, then the necessary
- * storage is allocated. The last parameter is the max allowed length
- * of the string as specified by a protocol.
- */
-bool_t
-xdr_string (xdrs, cpp, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int size;
- u_int nodesize;
-
- /*
- * first deal with the length since xdr strings are counted-strings
- */
- switch (xdrs->x_op)
- {
- case XDR_FREE:
- if (sp == NULL)
- {
- return TRUE; /* already free */
- }
- /* fall through... */
- case XDR_ENCODE:
- if (sp == NULL)
- return FALSE;
- size = strlen (sp);
- break;
- case XDR_DECODE:
- break;
- }
- if (!INTUSE(xdr_u_int) (xdrs, &size))
- {
- return FALSE;
- }
- if (size > maxsize)
- {
- return FALSE;
- }
- nodesize = size + 1;
- if (nodesize == 0)
- {
- /* This means an overflow. It a bug in the caller which
- provided a too large maxsize but nevertheless catch it
- here. */
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (sp == NULL)
- *cpp = sp = (char *) mem_alloc (nodesize);
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_string: out of memory\n");
- return FALSE;
- }
- sp[size] = 0;
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, size);
-
- case XDR_FREE:
- mem_free (sp, nodesize);
- *cpp = NULL;
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_string)
-
-/*
- * Wrapper for xdr_string that can be called directly from
- * routines like clnt_call
- */
-bool_t
-xdr_wrapstring (xdrs, cpp)
- XDR *xdrs;
- char **cpp;
-{
- if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
- {
- return TRUE;
- }
- return FALSE;
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr.h, External Data Representation Serialization Routines.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- */
-
-#ifndef _RPC_XDR_H
-#define _RPC_XDR_H 1
-
-#include <features.h>
-#include <sys/types.h>
-#include "types.h"
-
-/* We need FILE. */
-#include <stdio.h>
-
-__BEGIN_DECLS
-
-/*
- * XDR provides a conventional way for converting between C data
- * types and an external bit-string representation. Library supplied
- * routines provide for the conversion on built-in C data types. These
- * routines and utility routines defined here are used to help implement
- * a type encode/decode routine for each user-defined type.
- *
- * Each data type provides a single procedure which takes two arguments:
- *
- * bool_t
- * xdrproc(xdrs, argresp)
- * XDR *xdrs;
- * <type> *argresp;
- *
- * xdrs is an instance of a XDR handle, to which or from which the data
- * type is to be converted. argresp is a pointer to the structure to be
- * converted. The XDR handle contains an operation field which indicates
- * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
- *
- * XDR_DECODE may allocate space if the pointer argresp is null. This
- * data can be freed with the XDR_FREE operation.
- *
- * We write only one procedure per data type to make it easy
- * to keep the encode and decode procedures for a data type consistent.
- * In many cases the same code performs all operations on a user defined type,
- * because all the hard work is done in the component type routines.
- * decode as a series of calls on the nested data types.
- */
-
-/*
- * Xdr operations. XDR_ENCODE causes the type to be encoded into the
- * stream. XDR_DECODE causes the type to be extracted from the stream.
- * XDR_FREE can be used to release the space allocated by an XDR_DECODE
- * request.
- */
-enum xdr_op {
- XDR_ENCODE = 0,
- XDR_DECODE = 1,
- XDR_FREE = 2
-};
-
-/*
- * This is the number of bytes per unit of external data.
- */
-#define BYTES_PER_XDR_UNIT (4)
-/*
- * This only works if the above is a power of 2. But it's defined to be
- * 4 by the appropriate RFCs. So it will work. And it's normally quicker
- * than the old routine.
- */
-#if 1
-#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
-#else /* this is the old routine */
-#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
- * BYTES_PER_XDR_UNIT)
-#endif
-
-/*
- * The XDR handle.
- * Contains operation which is being applied to the stream,
- * an operations vector for the particular implementation (e.g. see xdr_mem.c),
- * and two private fields for the use of the particular implementation.
- */
-typedef struct XDR XDR;
-struct XDR
- {
- enum xdr_op x_op; /* operation; fast additional param */
- struct xdr_ops
- {
- bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
- /* get a long from underlying stream */
- bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
- /* put a long to " */
- bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
- /* get some bytes from " */
- bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
- /* put some bytes to " */
- u_int (*x_getpostn) (__const XDR *__xdrs);
- /* returns bytes off from beginning */
- bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
- /* lets you reposition the stream */
- int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
- /* buf quick ptr to buffered data */
- void (*x_destroy) (XDR *__xdrs);
- /* free privates of this xdr_stream */
- bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
- /* get a int from underlying stream */
- bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
- /* put a int to " */
- }
- *x_ops;
- caddr_t x_public; /* users' data */
- caddr_t x_private; /* pointer to private data */
- caddr_t x_base; /* private used for position info */
- u_int x_handy; /* extra private word */
- };
-
-/*
- * A xdrproc_t exists for each data type which is to be encoded or decoded.
- *
- * The second argument to the xdrproc_t is a pointer to an opaque pointer.
- * The opaque pointer generally points to a structure of the data type
- * to be decoded. If this pointer is 0, then the type routines should
- * allocate dynamic storage of the appropriate size and return it.
- * bool_t (*xdrproc_t)(XDR *, caddr_t *);
- */
-typedef bool_t (*xdrproc_t) (XDR *, void *,...);
-
-
-/*
- * Operations defined on a XDR handle
- *
- * XDR *xdrs;
- * int32_t *int32p;
- * long *longp;
- * caddr_t addr;
- * u_int len;
- * u_int pos;
- */
-#define XDR_GETINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-#define xdr_getint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-
-#define XDR_PUTINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-#define xdr_putint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-
-#define XDR_GETLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-#define xdr_getlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-
-#define XDR_PUTLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-#define xdr_putlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-
-#define XDR_GETBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-#define xdr_getbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-
-#define XDR_PUTBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-#define xdr_putbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-
-#define XDR_GETPOS(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-#define xdr_getpos(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-
-#define XDR_SETPOS(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-#define xdr_setpos(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-
-#define XDR_INLINE(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-#define xdr_inline(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-
-#define XDR_DESTROY(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-#define xdr_destroy(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-
-/*
- * Support struct for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * a entry with a null procedure pointer. The xdr_union routine gets
- * the discriminant value and then searches the array of structures
- * for a matching value. If a match is found the associated xdr routine
- * is called to handle that part of the union. If there is
- * no match, then a default routine may be called.
- * If there is no match and no default routine it is an error.
- */
-#define NULL_xdrproc_t ((xdrproc_t)0)
-struct xdr_discrim
-{
- int value;
- xdrproc_t proc;
-};
-
-/*
- * Inline routines for fast encode/decode of primitive data types.
- * Caveat emptor: these use single memory cycles to get the
- * data from the underlying buffer, and will fail to operate
- * properly if the data is not aligned. The standard way to use these
- * is to say:
- * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
- * return (FALSE);
- * <<< macro calls >>>
- * where ``count'' is the number of bytes of data occupied
- * by the primitive data types.
- *
- * N.B. and frozen for all time: each data type here uses 4 bytes
- * of external representation.
- */
-
-#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
-#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
-#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
-#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
-
-/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
- * and shouldn't be used any longer. Code which use this defines or longs
- * in the RPC code will not work on 64bit Solaris platforms !
- */
-#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
-#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
-#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
-#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-
-#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
-#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
-#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
-#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
-
-#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-/*
- * These are the "generic" xdr routines.
- * None of these can have const applied because it's not possible to
- * know whether the call is a read or a write to the passed parameter
- * also, the XDR structure is always updated by some of these calls.
- */
-extern bool_t xdr_void (void) __THROW;
-extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
-extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
-extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
-extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
-extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
-extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
-extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
-extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
-extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
-extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
-extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
-extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
-extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
-extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
-extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
-extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
-extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
-extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
-extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
- u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
- __THROW;
-extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
- u_int __maxsize) __THROW;
-extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
-extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
-extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
- __const struct xdr_discrim *__choices,
- xdrproc_t dfault) __THROW;
-extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
-extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
-extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
- u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
-extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
-extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
-extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
- xdrproc_t __proc) __THROW;
-extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
- u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
-extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
-extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
-
-/*
- * Common opaque bytes objects used by many rpc protocols;
- * declared here due to commonality.
- */
-#define MAX_NETOBJ_SZ 1024
-struct netobj
-{
- u_int n_len;
- char *n_bytes;
-};
-typedef struct netobj netobj;
-extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
-
-/*
- * These are the public routines for the various implementations of
- * xdr streams.
- */
-
-/* XDR using memory buffers */
-extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
- u_int __size, enum xdr_op __xop) __THROW;
-
-/* XDR using stdio library */
-extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
- __THROW;
-
-/* XDR pseudo records for tcp */
-extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
- u_int __recvsize, caddr_t __tcp_handle,
- int (*__readit) (char *, char *, int),
- int (*__writeit) (char *, char *, int)) __THROW;
-
-/* make end of xdr record */
-extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
-
-/* move to beginning of next record */
-extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
-
-/* true if no more input */
-extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
-
-/* free memory buffers for xdr */
-extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
-
-__END_DECLS
-
-#endif /* rpc/xdr.h */
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_array.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "non-trivial" xdr primitives used to serialize and de-serialize
- * arrays. See xdr.h for more info on the interface to xdr.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include "types.h"
-#include "xdr.h"
-#include <libintl.h>
-#include <limits.h>
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-#define LASTUNSIGNED ((u_int)0-1)
-
-
-/*
- * XDR an array of arbitrary elements
- * *addrp is a pointer to the array, *sizep is the number of elements.
- * If addrp is NULL (*sizep * elsize) bytes are allocated.
- * elsize is the size (in bytes) of each element, and elproc is the
- * xdr procedure to call to handle each element of the array.
- */
-bool_t
-xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
- XDR *xdrs;
- caddr_t *addrp; /* array pointer */
- u_int *sizep; /* number of elements */
- u_int maxsize; /* max numberof elements */
- u_int elsize; /* size in bytes of each element */
- xdrproc_t elproc; /* xdr routine to handle each element */
-{
- u_int i;
- caddr_t target = *addrp;
- u_int c; /* the actual element count */
- bool_t stat = TRUE;
- u_int nodesize;
-
- /* like strings, arrays are really counted arrays */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- c = *sizep;
- /*
- * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
- * doesn't actually use its second argument anyway.
- */
- if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
- nodesize = c * elsize;
-
- /*
- * if we are deserializing, we may need to allocate an array.
- * We also save time by checking for a null array if we are freeing.
- */
- if (target == NULL)
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (c == 0)
- return TRUE;
- *addrp = target = mem_alloc (nodesize);
- if (target == NULL)
- {
- fprintf (stderr, "%s", "xdr_array: out of memory\n");
- return FALSE;
- }
- __bzero (target, nodesize);
- break;
-
- case XDR_FREE:
- return TRUE;
- default:
- break;
- }
-
- /*
- * now we xdr each element of array
- */
- for (i = 0; (i < c) && stat; i++)
- {
- stat = (*elproc) (xdrs, target, LASTUNSIGNED);
- target += elsize;
- }
-
- /*
- * the array may need freeing
- */
- if (xdrs->x_op == XDR_FREE)
- {
- mem_free (*addrp, nodesize);
- *addrp = NULL;
- }
- return stat;
-}
-INTDEF(xdr_array)
-
-/*
- * xdr_vector():
- *
- * XDR a fixed length array. Unlike variable-length arrays,
- * the storage of fixed length arrays is static and unfreeable.
- * > basep: base of the array
- * > size: size of the array
- * > elemsize: size of each element
- * > xdr_elem: routine to XDR each element
- */
-bool_t
-xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
- XDR *xdrs;
- char *basep;
- u_int nelem;
- u_int elemsize;
- xdrproc_t xdr_elem;
-{
- u_int i;
- char *elptr;
-
- elptr = basep;
- for (i = 0; i < nelem; i++)
- {
- if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
- {
- return FALSE;
- }
- elptr += elemsize;
- }
- return TRUE;
-}
+++ /dev/null
-/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_float.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "floating point" xdr routines used to (de)serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <endian.h>
-
-#include "types.h"
-#include "xdr.h"
-
-/*
- * NB: Not portable.
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
-
-#ifdef vax
-
-/* What IEEE single precision floating point looks like on a Vax */
-struct ieee_single {
- unsigned int mantissa: 23;
- unsigned int exp : 8;
- unsigned int sign : 1;
-};
-
-/* Vax single precision floating point */
-struct vax_single {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
-};
-
-#define VAX_SNG_BIAS 0x81
-#define IEEE_SNG_BIAS 0x7f
-
-static struct sgl_limits {
- struct vax_single s;
- struct ieee_single ieee;
-} sgl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
- { 0x0, 0xff, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
- { 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-#endif /* vax */
-
-bool_t
-xdr_float(xdrs, fp)
- XDR *xdrs;
- float *fp;
-{
-#ifdef vax
- struct ieee_single is;
- struct vax_single vs, *vsp;
- struct sgl_limits *lim;
- int i;
-#endif
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vs = *((struct vax_single *)fp);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((vs.mantissa2 == lim->s.mantissa2) &&
- (vs.exp == lim->s.exp) &&
- (vs.mantissa1 == lim->s.mantissa1)) {
- is = lim->ieee;
- goto shipit;
- }
- }
- is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
- is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
- shipit:
- is.sign = vs.sign;
- return (XDR_PUTLONG(xdrs, (long *)&is));
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_PUTLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp = *(int *)fp;
- return (XDR_PUTLONG(xdrs, &tmp));
- }
- break;
-#endif
-
- case XDR_DECODE:
-#ifdef vax
- vsp = (struct vax_single *)fp;
- if (!XDR_GETLONG(xdrs, (long *)&is))
- return (FALSE);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((is.exp == lim->ieee.exp) &&
- (is.mantissa == lim->ieee.mantissa)) {
- *vsp = lim->s;
- goto doneit;
- }
- }
- vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
- vsp->mantissa2 = is.mantissa;
- vsp->mantissa1 = (is.mantissa >> 16);
- doneit:
- vsp->sign = is.sign;
- return (TRUE);
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_GETLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp;
- if (XDR_GETLONG(xdrs, &tmp)) {
- *(int *)fp = tmp;
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
-
-/*
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#ifdef vax
-/* What IEEE double precision floating point looks like on a Vax */
-struct ieee_double {
- unsigned int mantissa1 : 20;
- unsigned int exp : 11;
- unsigned int sign : 1;
- unsigned int mantissa2 : 32;
-};
-
-/* Vax double precision floating point */
-struct vax_double {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
- unsigned int mantissa3 : 16;
- unsigned int mantissa4 : 16;
-};
-
-#define VAX_DBL_BIAS 0x81
-#define IEEE_DBL_BIAS 0x3ff
-#define MASK(nbits) ((1 << nbits) - 1)
-
-static struct dbl_limits {
- struct vax_double d;
- struct ieee_double ieee;
-} dbl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
- { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
- { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-
-#endif /* vax */
-
-
-bool_t
-xdr_double(xdrs, dp)
- XDR *xdrs;
- double *dp;
-{
-#ifdef vax
- struct ieee_double id;
- struct vax_double vd;
- register struct dbl_limits *lim;
- int i;
-#endif
-
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vd = *((struct vax_double *)dp);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((vd.mantissa4 == lim->d.mantissa4) &&
- (vd.mantissa3 == lim->d.mantissa3) &&
- (vd.mantissa2 == lim->d.mantissa2) &&
- (vd.mantissa1 == lim->d.mantissa1) &&
- (vd.exp == lim->d.exp)) {
- id = lim->ieee;
- goto shipit;
- }
- }
- id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
- id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
- id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
- (vd.mantissa3 << 13) |
- ((vd.mantissa4 >> 3) & MASK(13));
- shipit:
- id.sign = vd.sign;
- dp = (double *)&id;
-#endif
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_PUTLONG(xdrs, lp+!LSW) &&
- XDR_PUTLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- tmp[0] = ip[!LSW];
- tmp[1] = ip[LSW];
- return (XDR_PUTLONG(xdrs, tmp) &&
- XDR_PUTLONG(xdrs, tmp+1));
- }
- break;
-
- case XDR_DECODE:
-#ifdef vax
- lp = (long *)&id;
- if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
- return (FALSE);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((id.mantissa2 == lim->ieee.mantissa2) &&
- (id.mantissa1 == lim->ieee.mantissa1) &&
- (id.exp == lim->ieee.exp)) {
- vd = lim->d;
- goto doneit;
- }
- }
- vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
- vd.mantissa1 = (id.mantissa1 >> 13);
- vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
- (id.mantissa2 >> 29);
- vd.mantissa3 = (id.mantissa2 >> 13);
- vd.mantissa4 = (id.mantissa2 << 3);
- doneit:
- vd.sign = id.sign;
- *dp = *((double *)&vd);
- return (TRUE);
-#else
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_GETLONG(xdrs, lp+!LSW) &&
- XDR_GETLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- if (XDR_GETLONG(xdrs, tmp+!LSW) &&
- XDR_GETLONG(xdrs, tmp+LSW)) {
- ip[0] = tmp[0];
- ip[1] = tmp[1];
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr_stdio.c, XDR implementation on standard i/o file.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * This set of routines implements a XDR on a stdio stream.
- * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
- * from the stream.
- */
-
-#include "types.h"
-#include <stdio.h>
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <libio/iolibio.h>
-# define fflush(s) INTUSE(_IO_fflush) (s)
-# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
-# define ftell(s) INTUSE(_IO_ftell) (s)
-# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
-#endif
-
-static bool_t xdrstdio_getlong (XDR *, long *);
-static bool_t xdrstdio_putlong (XDR *, const long *);
-static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
-static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
-static u_int xdrstdio_getpos (const XDR *);
-static bool_t xdrstdio_setpos (XDR *, u_int);
-static int32_t *xdrstdio_inline (XDR *, u_int);
-static void xdrstdio_destroy (XDR *);
-static bool_t xdrstdio_getint32 (XDR *, int32_t *);
-static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
-
-/*
- * Ops vector for stdio type XDR
- */
-static const struct xdr_ops xdrstdio_ops =
-{
- xdrstdio_getlong, /* deserialize a long int */
- xdrstdio_putlong, /* serialize a long int */
- xdrstdio_getbytes, /* deserialize counted bytes */
- xdrstdio_putbytes, /* serialize counted bytes */
- xdrstdio_getpos, /* get offset in the stream */
- xdrstdio_setpos, /* set offset in the stream */
- xdrstdio_inline, /* prime stream for inline macros */
- xdrstdio_destroy, /* destroy stream */
- xdrstdio_getint32, /* deserialize a int */
- xdrstdio_putint32 /* serialize a int */
-};
-
-/*
- * Initialize a stdio xdr stream.
- * Sets the xdr stream handle xdrs for use on the stream file.
- * Operation flag is set to op.
- */
-void
-xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
-{
- xdrs->x_op = op;
- /* We have to add the const since the `struct xdr_ops' in `struct XDR'
- is not `const'. */
- xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
- xdrs->x_private = (caddr_t) file;
- xdrs->x_handy = 0;
- xdrs->x_base = 0;
-}
-
-/*
- * Destroy a stdio xdr stream.
- * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
- */
-static void
-xdrstdio_destroy (XDR *xdrs)
-{
- (void) fflush ((FILE *) xdrs->x_private);
- /* xx should we close the file ?? */
-};
-
-static bool_t
-xdrstdio_getlong (XDR *xdrs, long *lp)
-{
- u_int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *lp = (long) ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putlong (XDR *xdrs, const long *lp)
-{
- int32_t mycopy = htonl ((u_int32_t) *lp);
-
- if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
-{
- if ((len != 0) && (fread (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
-{
- if ((len != 0) && (fwrite (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static u_int
-xdrstdio_getpos (const XDR *xdrs)
-{
- return (u_int) ftell ((FILE *) xdrs->x_private);
-}
-
-static bool_t
-xdrstdio_setpos (XDR *xdrs, u_int pos)
-{
- return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
-}
-
-static int32_t *
-xdrstdio_inline (XDR *xdrs, u_int len)
-{
- /*
- * Must do some work to implement this: must insure
- * enough data in the underlying stdio buffer,
- * that the buffer is aligned so that we can indirect through a
- * long *, and stuff this pointer in xdrs->x_buf. Doing
- * a fread or fwrite to a scratch buffer would defeat
- * most of the gains to be had here and require storage
- * management on this buffer, so we don't do this.
- */
- return NULL;
-}
-
-static bool_t
-xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
-{
- int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *ip = ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
-{
- int32_t mycopy = htonl (*ip);
-
- ip = &mycopy;
- if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-/* libc_hidden_def (xdrstdio_create) */
+++ /dev/null
-/*_________________________________________________________________
- |
- | xdrf.h - include file for C routines that want to use the
- | functions below.
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type);
-int xdrclose(XDR *xdrs) ;
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
-
+++ /dev/null
-#
-# CMake project file for UNRESPACK
-#
-
-# m4 macro processor
-add_custom_command(
- OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c
- COMMAND m4
- ARGS ${CMAKE_CURRENT_SOURCE_DIR}/underscore.m4 ${CMAKE_CURRENT_SOURCE_DIR}/libxdrf.m4 > ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c
- VERBATIM
-)
-
-# add headers from current dir
-include_directories(${CMAKE_CURRENT_SOURCE_DIR})
-# compile the libxdrf library
-add_library(xdrf-MD-M STATIC ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c ftocstr.c)
-set_target_properties(xdrf-MD-M PROPERTIES OUTPUT_NAME xdrf )
-
-set(UNRES_XDRFLIB ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.a PARENT_SCOPE)
-
-#add_dependencies( ${UNRES_BIN} xdrf )
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = gcc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
-
-CC = /usr/bin/blrts_xlc
-CPPC = /usr/bin/blrts_xlc
-
-CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
-
-M4 = m4
-M4FILE = RS6K.m4
-
-libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f *.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
-# rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# do nothing special to FORTRAN function names
-#
-define(`FUNCTION',`$1')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
-
+++ /dev/null
-
-
-int ftocstr(ds, dl, ss, sl)
- char *ds, *ss; /* dst, src ptrs */
- int dl; /* dst max len */
- int sl; /* src len */
-{
- char *p;
-
- for (p = ss + sl; --p >= ss && *p == ' '; ) ;
- sl = p - ss + 1;
- dl--;
- ds[0] = 0;
- if (sl > dl)
- return 1;
- while (sl--)
- (*ds++ = *ss++);
- *ds = '\0';
- return 0;
-}
-
-
-int ctofstr(ds, dl, ss)
- char *ds; /* dest space */
- int dl; /* max dest length */
- char *ss; /* src string (0-term) */
-{
- while (dl && *ss) {
- *ds++ = *ss++;
- dl--;
- }
- while (dl--)
- *ds++ = ' ';
- return 0;
-}
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-/* #include <rpc/rpc.h>
-#include <rpc/xdr.h> */
-#include "xdr.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "w+";
- lmode = XDR_ENCODE;
- } else if (*type == 'a' || *type == 'A') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-/* fixincludes should not add extern "C" to this file */
-/*
- * Rpc additions to <sys/types.h>
- */
-#ifndef _RPC_TYPES_H
-#define _RPC_TYPES_H 1
-
-typedef int bool_t;
-typedef int enum_t;
-/* This needs to be changed to uint32_t in the future */
-typedef unsigned long rpcprog_t;
-typedef unsigned long rpcvers_t;
-typedef unsigned long rpcproc_t;
-typedef unsigned long rpcprot_t;
-typedef unsigned long rpcport_t;
-
-#define __dontcare__ -1
-
-#ifndef FALSE
-# define FALSE (0)
-#endif
-
-#ifndef TRUE
-# define TRUE (1)
-#endif
-
-#ifndef NULL
-# define NULL 0
-#endif
-
-#include <stdlib.h> /* For malloc decl. */
-#define mem_alloc(bsize) malloc(bsize)
-/*
- * XXX: This must not use the second argument, or code in xdr_array.c needs
- * to be modified.
- */
-#define mem_free(ptr, bsize) free(ptr)
-
-#ifndef makedev /* ie, we haven't already included it */
-#include <sys/types.h>
-#endif
-
-#ifndef __u_char_defined
-typedef __u_char u_char;
-typedef __u_short u_short;
-typedef __u_int u_int;
-typedef __u_long u_long;
-typedef __quad_t quad_t;
-typedef __u_quad_t u_quad_t;
-typedef __fsid_t fsid_t;
-# define __u_char_defined
-#endif
-#ifndef __daddr_t_defined
-typedef __daddr_t daddr_t;
-typedef __caddr_t caddr_t;
-# define __daddr_t_defined
-#endif
-
-#include <sys/time.h>
-#include <sys/param.h>
-
-#include <netinet/in.h>
-
-#ifndef INADDR_LOOPBACK
-#define INADDR_LOOPBACK (u_long)0x7F000001
-#endif
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 64
-#endif
-
-#endif /* rpc/types.h */
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# append an underscore to FORTRAN function names
-#
-define(`FUNCTION',`$1_')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
-#endif
-
-/*
- * xdr.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1986, Sun Microsystems, Inc.
- *
- * These are the "generic" xdr routines used to serialize and de-serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <libintl.h>
-
-#include "types.h"
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-/*
- * constants specific to the xdr "protocol"
- */
-#define XDR_FALSE ((long) 0)
-#define XDR_TRUE ((long) 1)
-#define LASTUNSIGNED ((u_int) 0-1)
-
-/*
- * for unit alignment
- */
-static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
-
-/*
- * Free a data structure using XDR
- * Not a filter, but a convenient utility nonetheless
- */
-void
-xdr_free (xdrproc_t proc, char *objp)
-{
- XDR x;
-
- x.x_op = XDR_FREE;
- (*proc) (&x, objp);
-}
-
-/*
- * XDR nothing
- */
-bool_t
-xdr_void (void)
-{
- return TRUE;
-}
-INTDEF(xdr_void)
-
-/*
- * XDR integers
- */
-bool_t
-xdr_int (XDR *xdrs, int *ip)
-{
-
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *ip;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ip = (int) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif INT_MAX == LONG_MAX
- return INTUSE(xdr_long) (xdrs, (long *) ip);
-#elif INT_MAX == SHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) ip);
-#else
-#error unexpected integer sizes in_xdr_int()
-#endif
-}
-INTDEF(xdr_int)
-
-/*
- * XDR unsigned integers
- */
-bool_t
-xdr_u_int (XDR *xdrs, u_int *up)
-{
-#if UINT_MAX < ULONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * up;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *up = (u_int) (u_long) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif UINT_MAX == ULONG_MAX
- return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
-#elif UINT_MAX == USHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) up);
-#else
-#error unexpected integer sizes in_xdr_u_int()
-#endif
-}
-INTDEF(xdr_u_int)
-
-/*
- * XDR long integers
- * The definition of xdr_long() is kept for backward
- * compatibility. Instead xdr_int() should be used.
- */
-bool_t
-xdr_long (XDR *xdrs, long *lp)
-{
-
- if (xdrs->x_op == XDR_ENCODE
- && (sizeof (int32_t) == sizeof (long)
- || (int32_t) *lp == *lp))
- return XDR_PUTLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_DECODE)
- return XDR_GETLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_long)
-
-/*
- * XDR unsigned long integers
- * The definition of xdr_u_long() is kept for backward
- * compatibility. Instead xdr_u_int() should be used.
- */
-bool_t
-xdr_u_long (XDR *xdrs, u_long *ulp)
-{
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- {
- long int tmp;
-
- if (XDR_GETLONG (xdrs, &tmp) == FALSE)
- return FALSE;
-
- *ulp = (uint32_t) tmp;
- return TRUE;
- }
-
- case XDR_ENCODE:
- if (sizeof (uint32_t) != sizeof (u_long)
- && (uint32_t) *ulp != *ulp)
- return FALSE;
-
- return XDR_PUTLONG (xdrs, (long *) ulp);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_long)
-
-/*
- * XDR hyper integers
- * same as xdr_u_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_hyper (XDR *xdrs, quad_t *llp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (long) ((*llp) >> 32);
- t2 = (long) (*llp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *llp = ((quad_t) t1) << 32;
- *llp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_hyper)
-
-
-/*
- * XDR hyper integers
- * same as xdr_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (unsigned long) ((*ullp) >> 32);
- t2 = (unsigned long) (*ullp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *ullp = ((u_quad_t) t1) << 32;
- *ullp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_u_hyper)
-
-bool_t
-xdr_longlong_t (XDR *xdrs, quad_t *llp)
-{
- return INTUSE(xdr_hyper) (xdrs, llp);
-}
-
-bool_t
-xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
-{
- return INTUSE(xdr_u_hyper) (xdrs, ullp);
-}
-
-/*
- * XDR short integers
- */
-bool_t
-xdr_short (XDR *xdrs, short *sp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *sp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *sp = (short) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_short)
-
-/*
- * XDR unsigned short integers
- */
-bool_t
-xdr_u_short (XDR *xdrs, u_short *usp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * usp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *usp = (u_short) (u_long) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_short)
-
-
-/*
- * XDR a char
- */
-bool_t
-xdr_char (XDR *xdrs, char *cp)
-{
- int i;
-
- i = (*cp);
- if (!INTUSE(xdr_int) (xdrs, &i))
- {
- return FALSE;
- }
- *cp = i;
- return TRUE;
-}
-
-/*
- * XDR an unsigned char
- */
-bool_t
-xdr_u_char (XDR *xdrs, u_char *cp)
-{
- u_int u;
-
- u = (*cp);
- if (!INTUSE(xdr_u_int) (xdrs, &u))
- {
- return FALSE;
- }
- *cp = u;
- return TRUE;
-}
-
-/*
- * XDR booleans
- */
-bool_t
-xdr_bool (XDR *xdrs, bool_t *bp)
-{
- long lb;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- lb = *bp ? XDR_TRUE : XDR_FALSE;
- return XDR_PUTLONG (xdrs, &lb);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &lb))
- {
- return FALSE;
- }
- *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bool)
-
-/*
- * XDR enumerations
- */
-bool_t
-xdr_enum (XDR *xdrs, enum_t *ep)
-{
- enum sizecheck
- {
- SIZEVAL
- }; /* used to find the size of an enum */
-
- /*
- * enums are treated as ints
- */
- if (sizeof (enum sizecheck) == 4)
- {
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = *ep;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ep = l;
- case XDR_FREE:
- return TRUE;
-
- }
- return FALSE;
-#else
- return INTUSE(xdr_long) (xdrs, (long *) ep);
-#endif
- }
- else if (sizeof (enum sizecheck) == sizeof (short))
- {
- return INTUSE(xdr_short) (xdrs, (short *) ep);
- }
- else
- {
- return FALSE;
- }
-}
-INTDEF(xdr_enum)
-
-/*
- * XDR opaque data
- * Allows the specification of a fixed size sequence of opaque bytes.
- * cp points to the opaque object and cnt gives the byte length.
- */
-bool_t
-xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
-{
- u_int rndup;
- static char crud[BYTES_PER_XDR_UNIT];
-
- /*
- * if no data we are done
- */
- if (cnt == 0)
- return TRUE;
-
- /*
- * round byte count to full xdr units
- */
- rndup = cnt % BYTES_PER_XDR_UNIT;
- if (rndup > 0)
- rndup = BYTES_PER_XDR_UNIT - rndup;
-
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (!XDR_GETBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
-
- case XDR_ENCODE:
- if (!XDR_PUTBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_opaque)
-
-/*
- * XDR counted bytes
- * *cpp is a pointer to the bytes, *sizep is the count.
- * If *cpp is NULL maxsize bytes are allocated
- */
-bool_t
-xdr_bytes (xdrs, cpp, sizep, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int *sizep;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int nodesize;
-
- /*
- * first deal with the length since xdr bytes are counted
- */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- nodesize = *sizep;
- if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (nodesize == 0)
- {
- return TRUE;
- }
- if (sp == NULL)
- {
- *cpp = sp = (char *) mem_alloc (nodesize);
- }
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
- return FALSE;
- }
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
-
- case XDR_FREE:
- if (sp != NULL)
- {
- mem_free (sp, nodesize);
- *cpp = NULL;
- }
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bytes)
-
-/*
- * Implemented here due to commonality of the object.
- */
-bool_t
-xdr_netobj (xdrs, np)
- XDR *xdrs;
- struct netobj *np;
-{
-
- return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
-}
-INTDEF(xdr_netobj)
-
-/*
- * XDR a discriminated union
- * Support routine for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * an entry with a null procedure pointer. The routine gets
- * the discriminant value and then searches the array of xdrdiscrims
- * looking for that value. It calls the procedure given in the xdrdiscrim
- * to handle the discriminant. If there is no specific routine a default
- * routine may be called.
- * If there is no specific or default routine an error is returned.
- */
-bool_t
-xdr_union (xdrs, dscmp, unp, choices, dfault)
- XDR *xdrs;
- enum_t *dscmp; /* enum to decide which arm to work on */
- char *unp; /* the union itself */
- const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
- xdrproc_t dfault; /* default xdr routine */
-{
- enum_t dscm;
-
- /*
- * we deal with the discriminator; it's an enum
- */
- if (!INTUSE(xdr_enum) (xdrs, dscmp))
- {
- return FALSE;
- }
- dscm = *dscmp;
-
- /*
- * search choices for a value that matches the discriminator.
- * if we find one, execute the xdr routine for that value.
- */
- for (; choices->proc != NULL_xdrproc_t; choices++)
- {
- if (choices->value == dscm)
- return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
- }
-
- /*
- * no match - execute the default xdr routine if there is one
- */
- return ((dfault == NULL_xdrproc_t) ? FALSE :
- (*dfault) (xdrs, unp, LASTUNSIGNED));
-}
-INTDEF(xdr_union)
-
-
-/*
- * Non-portable xdr primitives.
- * Care should be taken when moving these routines to new architectures.
- */
-
-
-/*
- * XDR null terminated ASCII strings
- * xdr_string deals with "C strings" - arrays of bytes that are
- * terminated by a NULL character. The parameter cpp references a
- * pointer to storage; If the pointer is null, then the necessary
- * storage is allocated. The last parameter is the max allowed length
- * of the string as specified by a protocol.
- */
-bool_t
-xdr_string (xdrs, cpp, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int size;
- u_int nodesize;
-
- /*
- * first deal with the length since xdr strings are counted-strings
- */
- switch (xdrs->x_op)
- {
- case XDR_FREE:
- if (sp == NULL)
- {
- return TRUE; /* already free */
- }
- /* fall through... */
- case XDR_ENCODE:
- if (sp == NULL)
- return FALSE;
- size = strlen (sp);
- break;
- case XDR_DECODE:
- break;
- }
- if (!INTUSE(xdr_u_int) (xdrs, &size))
- {
- return FALSE;
- }
- if (size > maxsize)
- {
- return FALSE;
- }
- nodesize = size + 1;
- if (nodesize == 0)
- {
- /* This means an overflow. It a bug in the caller which
- provided a too large maxsize but nevertheless catch it
- here. */
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (sp == NULL)
- *cpp = sp = (char *) mem_alloc (nodesize);
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_string: out of memory\n");
- return FALSE;
- }
- sp[size] = 0;
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, size);
-
- case XDR_FREE:
- mem_free (sp, nodesize);
- *cpp = NULL;
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_string)
-
-/*
- * Wrapper for xdr_string that can be called directly from
- * routines like clnt_call
- */
-bool_t
-xdr_wrapstring (xdrs, cpp)
- XDR *xdrs;
- char **cpp;
-{
- if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
- {
- return TRUE;
- }
- return FALSE;
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr.h, External Data Representation Serialization Routines.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- */
-
-#ifndef _RPC_XDR_H
-#define _RPC_XDR_H 1
-
-#include <features.h>
-#include <sys/types.h>
-#include "types.h"
-
-/* We need FILE. */
-#include <stdio.h>
-
-__BEGIN_DECLS
-
-/*
- * XDR provides a conventional way for converting between C data
- * types and an external bit-string representation. Library supplied
- * routines provide for the conversion on built-in C data types. These
- * routines and utility routines defined here are used to help implement
- * a type encode/decode routine for each user-defined type.
- *
- * Each data type provides a single procedure which takes two arguments:
- *
- * bool_t
- * xdrproc(xdrs, argresp)
- * XDR *xdrs;
- * <type> *argresp;
- *
- * xdrs is an instance of a XDR handle, to which or from which the data
- * type is to be converted. argresp is a pointer to the structure to be
- * converted. The XDR handle contains an operation field which indicates
- * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
- *
- * XDR_DECODE may allocate space if the pointer argresp is null. This
- * data can be freed with the XDR_FREE operation.
- *
- * We write only one procedure per data type to make it easy
- * to keep the encode and decode procedures for a data type consistent.
- * In many cases the same code performs all operations on a user defined type,
- * because all the hard work is done in the component type routines.
- * decode as a series of calls on the nested data types.
- */
-
-/*
- * Xdr operations. XDR_ENCODE causes the type to be encoded into the
- * stream. XDR_DECODE causes the type to be extracted from the stream.
- * XDR_FREE can be used to release the space allocated by an XDR_DECODE
- * request.
- */
-enum xdr_op {
- XDR_ENCODE = 0,
- XDR_DECODE = 1,
- XDR_FREE = 2
-};
-
-/*
- * This is the number of bytes per unit of external data.
- */
-#define BYTES_PER_XDR_UNIT (4)
-/*
- * This only works if the above is a power of 2. But it's defined to be
- * 4 by the appropriate RFCs. So it will work. And it's normally quicker
- * than the old routine.
- */
-#if 1
-#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
-#else /* this is the old routine */
-#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
- * BYTES_PER_XDR_UNIT)
-#endif
-
-/*
- * The XDR handle.
- * Contains operation which is being applied to the stream,
- * an operations vector for the particular implementation (e.g. see xdr_mem.c),
- * and two private fields for the use of the particular implementation.
- */
-typedef struct XDR XDR;
-struct XDR
- {
- enum xdr_op x_op; /* operation; fast additional param */
- struct xdr_ops
- {
- bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
- /* get a long from underlying stream */
- bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
- /* put a long to " */
- bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
- /* get some bytes from " */
- bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
- /* put some bytes to " */
- u_int (*x_getpostn) (__const XDR *__xdrs);
- /* returns bytes off from beginning */
- bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
- /* lets you reposition the stream */
- int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
- /* buf quick ptr to buffered data */
- void (*x_destroy) (XDR *__xdrs);
- /* free privates of this xdr_stream */
- bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
- /* get a int from underlying stream */
- bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
- /* put a int to " */
- }
- *x_ops;
- caddr_t x_public; /* users' data */
- caddr_t x_private; /* pointer to private data */
- caddr_t x_base; /* private used for position info */
- u_int x_handy; /* extra private word */
- };
-
-/*
- * A xdrproc_t exists for each data type which is to be encoded or decoded.
- *
- * The second argument to the xdrproc_t is a pointer to an opaque pointer.
- * The opaque pointer generally points to a structure of the data type
- * to be decoded. If this pointer is 0, then the type routines should
- * allocate dynamic storage of the appropriate size and return it.
- * bool_t (*xdrproc_t)(XDR *, caddr_t *);
- */
-typedef bool_t (*xdrproc_t) (XDR *, void *,...);
-
-
-/*
- * Operations defined on a XDR handle
- *
- * XDR *xdrs;
- * int32_t *int32p;
- * long *longp;
- * caddr_t addr;
- * u_int len;
- * u_int pos;
- */
-#define XDR_GETINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-#define xdr_getint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-
-#define XDR_PUTINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-#define xdr_putint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-
-#define XDR_GETLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-#define xdr_getlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-
-#define XDR_PUTLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-#define xdr_putlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-
-#define XDR_GETBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-#define xdr_getbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-
-#define XDR_PUTBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-#define xdr_putbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-
-#define XDR_GETPOS(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-#define xdr_getpos(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-
-#define XDR_SETPOS(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-#define xdr_setpos(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-
-#define XDR_INLINE(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-#define xdr_inline(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-
-#define XDR_DESTROY(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-#define xdr_destroy(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-
-/*
- * Support struct for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * a entry with a null procedure pointer. The xdr_union routine gets
- * the discriminant value and then searches the array of structures
- * for a matching value. If a match is found the associated xdr routine
- * is called to handle that part of the union. If there is
- * no match, then a default routine may be called.
- * If there is no match and no default routine it is an error.
- */
-#define NULL_xdrproc_t ((xdrproc_t)0)
-struct xdr_discrim
-{
- int value;
- xdrproc_t proc;
-};
-
-/*
- * Inline routines for fast encode/decode of primitive data types.
- * Caveat emptor: these use single memory cycles to get the
- * data from the underlying buffer, and will fail to operate
- * properly if the data is not aligned. The standard way to use these
- * is to say:
- * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
- * return (FALSE);
- * <<< macro calls >>>
- * where ``count'' is the number of bytes of data occupied
- * by the primitive data types.
- *
- * N.B. and frozen for all time: each data type here uses 4 bytes
- * of external representation.
- */
-
-#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
-#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
-#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
-#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
-
-/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
- * and shouldn't be used any longer. Code which use this defines or longs
- * in the RPC code will not work on 64bit Solaris platforms !
- */
-#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
-#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
-#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
-#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-
-#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
-#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
-#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
-#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
-
-#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-/*
- * These are the "generic" xdr routines.
- * None of these can have const applied because it's not possible to
- * know whether the call is a read or a write to the passed parameter
- * also, the XDR structure is always updated by some of these calls.
- */
-extern bool_t xdr_void (void) __THROW;
-extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
-extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
-extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
-extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
-extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
-extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
-extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
-extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
-extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
-extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
-extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
-extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
-extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
-extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
-extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
-extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
-extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
-extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
-extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
- u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
- __THROW;
-extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
- u_int __maxsize) __THROW;
-extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
-extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
-extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
- __const struct xdr_discrim *__choices,
- xdrproc_t dfault) __THROW;
-extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
-extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
-extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
- u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
-extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
-extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
-extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
- xdrproc_t __proc) __THROW;
-extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
- u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
-extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
-extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
-
-/*
- * Common opaque bytes objects used by many rpc protocols;
- * declared here due to commonality.
- */
-#define MAX_NETOBJ_SZ 1024
-struct netobj
-{
- u_int n_len;
- char *n_bytes;
-};
-typedef struct netobj netobj;
-extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
-
-/*
- * These are the public routines for the various implementations of
- * xdr streams.
- */
-
-/* XDR using memory buffers */
-extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
- u_int __size, enum xdr_op __xop) __THROW;
-
-/* XDR using stdio library */
-extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
- __THROW;
-
-/* XDR pseudo records for tcp */
-extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
- u_int __recvsize, caddr_t __tcp_handle,
- int (*__readit) (char *, char *, int),
- int (*__writeit) (char *, char *, int)) __THROW;
-
-/* make end of xdr record */
-extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
-
-/* move to beginning of next record */
-extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
-
-/* true if no more input */
-extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
-
-/* free memory buffers for xdr */
-extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
-
-__END_DECLS
-
-#endif /* rpc/xdr.h */
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_array.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "non-trivial" xdr primitives used to serialize and de-serialize
- * arrays. See xdr.h for more info on the interface to xdr.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include "types.h"
-#include "xdr.h"
-#include <libintl.h>
-#include <limits.h>
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-#define LASTUNSIGNED ((u_int)0-1)
-
-
-/*
- * XDR an array of arbitrary elements
- * *addrp is a pointer to the array, *sizep is the number of elements.
- * If addrp is NULL (*sizep * elsize) bytes are allocated.
- * elsize is the size (in bytes) of each element, and elproc is the
- * xdr procedure to call to handle each element of the array.
- */
-bool_t
-xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
- XDR *xdrs;
- caddr_t *addrp; /* array pointer */
- u_int *sizep; /* number of elements */
- u_int maxsize; /* max numberof elements */
- u_int elsize; /* size in bytes of each element */
- xdrproc_t elproc; /* xdr routine to handle each element */
-{
- u_int i;
- caddr_t target = *addrp;
- u_int c; /* the actual element count */
- bool_t stat = TRUE;
- u_int nodesize;
-
- /* like strings, arrays are really counted arrays */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- c = *sizep;
- /*
- * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
- * doesn't actually use its second argument anyway.
- */
- if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
- nodesize = c * elsize;
-
- /*
- * if we are deserializing, we may need to allocate an array.
- * We also save time by checking for a null array if we are freeing.
- */
- if (target == NULL)
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (c == 0)
- return TRUE;
- *addrp = target = mem_alloc (nodesize);
- if (target == NULL)
- {
- fprintf (stderr, "%s", "xdr_array: out of memory\n");
- return FALSE;
- }
- __bzero (target, nodesize);
- break;
-
- case XDR_FREE:
- return TRUE;
- default:
- break;
- }
-
- /*
- * now we xdr each element of array
- */
- for (i = 0; (i < c) && stat; i++)
- {
- stat = (*elproc) (xdrs, target, LASTUNSIGNED);
- target += elsize;
- }
-
- /*
- * the array may need freeing
- */
- if (xdrs->x_op == XDR_FREE)
- {
- mem_free (*addrp, nodesize);
- *addrp = NULL;
- }
- return stat;
-}
-INTDEF(xdr_array)
-
-/*
- * xdr_vector():
- *
- * XDR a fixed length array. Unlike variable-length arrays,
- * the storage of fixed length arrays is static and unfreeable.
- * > basep: base of the array
- * > size: size of the array
- * > elemsize: size of each element
- * > xdr_elem: routine to XDR each element
- */
-bool_t
-xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
- XDR *xdrs;
- char *basep;
- u_int nelem;
- u_int elemsize;
- xdrproc_t xdr_elem;
-{
- u_int i;
- char *elptr;
-
- elptr = basep;
- for (i = 0; i < nelem; i++)
- {
- if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
- {
- return FALSE;
- }
- elptr += elemsize;
- }
- return TRUE;
-}
+++ /dev/null
-/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_float.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "floating point" xdr routines used to (de)serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <endian.h>
-
-#include "types.h"
-#include "xdr.h"
-
-/*
- * NB: Not portable.
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
-
-#ifdef vax
-
-/* What IEEE single precision floating point looks like on a Vax */
-struct ieee_single {
- unsigned int mantissa: 23;
- unsigned int exp : 8;
- unsigned int sign : 1;
-};
-
-/* Vax single precision floating point */
-struct vax_single {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
-};
-
-#define VAX_SNG_BIAS 0x81
-#define IEEE_SNG_BIAS 0x7f
-
-static struct sgl_limits {
- struct vax_single s;
- struct ieee_single ieee;
-} sgl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
- { 0x0, 0xff, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
- { 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-#endif /* vax */
-
-bool_t
-xdr_float(xdrs, fp)
- XDR *xdrs;
- float *fp;
-{
-#ifdef vax
- struct ieee_single is;
- struct vax_single vs, *vsp;
- struct sgl_limits *lim;
- int i;
-#endif
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vs = *((struct vax_single *)fp);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((vs.mantissa2 == lim->s.mantissa2) &&
- (vs.exp == lim->s.exp) &&
- (vs.mantissa1 == lim->s.mantissa1)) {
- is = lim->ieee;
- goto shipit;
- }
- }
- is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
- is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
- shipit:
- is.sign = vs.sign;
- return (XDR_PUTLONG(xdrs, (long *)&is));
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_PUTLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp = *(int *)fp;
- return (XDR_PUTLONG(xdrs, &tmp));
- }
- break;
-#endif
-
- case XDR_DECODE:
-#ifdef vax
- vsp = (struct vax_single *)fp;
- if (!XDR_GETLONG(xdrs, (long *)&is))
- return (FALSE);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((is.exp == lim->ieee.exp) &&
- (is.mantissa == lim->ieee.mantissa)) {
- *vsp = lim->s;
- goto doneit;
- }
- }
- vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
- vsp->mantissa2 = is.mantissa;
- vsp->mantissa1 = (is.mantissa >> 16);
- doneit:
- vsp->sign = is.sign;
- return (TRUE);
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_GETLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp;
- if (XDR_GETLONG(xdrs, &tmp)) {
- *(int *)fp = tmp;
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
-
-/*
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#ifdef vax
-/* What IEEE double precision floating point looks like on a Vax */
-struct ieee_double {
- unsigned int mantissa1 : 20;
- unsigned int exp : 11;
- unsigned int sign : 1;
- unsigned int mantissa2 : 32;
-};
-
-/* Vax double precision floating point */
-struct vax_double {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
- unsigned int mantissa3 : 16;
- unsigned int mantissa4 : 16;
-};
-
-#define VAX_DBL_BIAS 0x81
-#define IEEE_DBL_BIAS 0x3ff
-#define MASK(nbits) ((1 << nbits) - 1)
-
-static struct dbl_limits {
- struct vax_double d;
- struct ieee_double ieee;
-} dbl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
- { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
- { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-
-#endif /* vax */
-
-
-bool_t
-xdr_double(xdrs, dp)
- XDR *xdrs;
- double *dp;
-{
-#ifdef vax
- struct ieee_double id;
- struct vax_double vd;
- register struct dbl_limits *lim;
- int i;
-#endif
-
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vd = *((struct vax_double *)dp);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((vd.mantissa4 == lim->d.mantissa4) &&
- (vd.mantissa3 == lim->d.mantissa3) &&
- (vd.mantissa2 == lim->d.mantissa2) &&
- (vd.mantissa1 == lim->d.mantissa1) &&
- (vd.exp == lim->d.exp)) {
- id = lim->ieee;
- goto shipit;
- }
- }
- id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
- id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
- id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
- (vd.mantissa3 << 13) |
- ((vd.mantissa4 >> 3) & MASK(13));
- shipit:
- id.sign = vd.sign;
- dp = (double *)&id;
-#endif
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_PUTLONG(xdrs, lp+!LSW) &&
- XDR_PUTLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- tmp[0] = ip[!LSW];
- tmp[1] = ip[LSW];
- return (XDR_PUTLONG(xdrs, tmp) &&
- XDR_PUTLONG(xdrs, tmp+1));
- }
- break;
-
- case XDR_DECODE:
-#ifdef vax
- lp = (long *)&id;
- if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
- return (FALSE);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((id.mantissa2 == lim->ieee.mantissa2) &&
- (id.mantissa1 == lim->ieee.mantissa1) &&
- (id.exp == lim->ieee.exp)) {
- vd = lim->d;
- goto doneit;
- }
- }
- vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
- vd.mantissa1 = (id.mantissa1 >> 13);
- vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
- (id.mantissa2 >> 29);
- vd.mantissa3 = (id.mantissa2 >> 13);
- vd.mantissa4 = (id.mantissa2 << 3);
- doneit:
- vd.sign = id.sign;
- *dp = *((double *)&vd);
- return (TRUE);
-#else
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_GETLONG(xdrs, lp+!LSW) &&
- XDR_GETLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- if (XDR_GETLONG(xdrs, tmp+!LSW) &&
- XDR_GETLONG(xdrs, tmp+LSW)) {
- ip[0] = tmp[0];
- ip[1] = tmp[1];
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr_stdio.c, XDR implementation on standard i/o file.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * This set of routines implements a XDR on a stdio stream.
- * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
- * from the stream.
- */
-
-#include "types.h"
-#include <stdio.h>
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <libio/iolibio.h>
-# define fflush(s) INTUSE(_IO_fflush) (s)
-# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
-# define ftell(s) INTUSE(_IO_ftell) (s)
-# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
-#endif
-
-static bool_t xdrstdio_getlong (XDR *, long *);
-static bool_t xdrstdio_putlong (XDR *, const long *);
-static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
-static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
-static u_int xdrstdio_getpos (const XDR *);
-static bool_t xdrstdio_setpos (XDR *, u_int);
-static int32_t *xdrstdio_inline (XDR *, u_int);
-static void xdrstdio_destroy (XDR *);
-static bool_t xdrstdio_getint32 (XDR *, int32_t *);
-static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
-
-/*
- * Ops vector for stdio type XDR
- */
-static const struct xdr_ops xdrstdio_ops =
-{
- xdrstdio_getlong, /* deserialize a long int */
- xdrstdio_putlong, /* serialize a long int */
- xdrstdio_getbytes, /* deserialize counted bytes */
- xdrstdio_putbytes, /* serialize counted bytes */
- xdrstdio_getpos, /* get offset in the stream */
- xdrstdio_setpos, /* set offset in the stream */
- xdrstdio_inline, /* prime stream for inline macros */
- xdrstdio_destroy, /* destroy stream */
- xdrstdio_getint32, /* deserialize a int */
- xdrstdio_putint32 /* serialize a int */
-};
-
-/*
- * Initialize a stdio xdr stream.
- * Sets the xdr stream handle xdrs for use on the stream file.
- * Operation flag is set to op.
- */
-void
-xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
-{
- xdrs->x_op = op;
- /* We have to add the const since the `struct xdr_ops' in `struct XDR'
- is not `const'. */
- xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
- xdrs->x_private = (caddr_t) file;
- xdrs->x_handy = 0;
- xdrs->x_base = 0;
-}
-
-/*
- * Destroy a stdio xdr stream.
- * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
- */
-static void
-xdrstdio_destroy (XDR *xdrs)
-{
- (void) fflush ((FILE *) xdrs->x_private);
- /* xx should we close the file ?? */
-};
-
-static bool_t
-xdrstdio_getlong (XDR *xdrs, long *lp)
-{
- u_int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *lp = (long) ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putlong (XDR *xdrs, const long *lp)
-{
- int32_t mycopy = htonl ((u_int32_t) *lp);
-
- if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
-{
- if ((len != 0) && (fread (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
-{
- if ((len != 0) && (fwrite (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static u_int
-xdrstdio_getpos (const XDR *xdrs)
-{
- return (u_int) ftell ((FILE *) xdrs->x_private);
-}
-
-static bool_t
-xdrstdio_setpos (XDR *xdrs, u_int pos)
-{
- return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
-}
-
-static int32_t *
-xdrstdio_inline (XDR *xdrs, u_int len)
-{
- /*
- * Must do some work to implement this: must insure
- * enough data in the underlying stdio buffer,
- * that the buffer is aligned so that we can indirect through a
- * long *, and stuff this pointer in xdrs->x_buf. Doing
- * a fread or fwrite to a scratch buffer would defeat
- * most of the gains to be had here and require storage
- * management on this buffer, so we don't do this.
- */
- return NULL;
-}
-
-static bool_t
-xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
-{
- int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *ip = ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
-{
- int32_t mycopy = htonl (*ip);
-
- ip = &mycopy;
- if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-/* libc_hidden_def (xdrstdio_create) */
+++ /dev/null
-/*_________________________________________________________________
- |
- | xdrf.h - include file for C routines that want to use the
- | functions below.
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type);
-int xdrclose(XDR *xdrs) ;
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BINDIR=../../bin
-
-CC = cc
-CFLAGS = -O
-
-#FC=ifort
-FC=f77
-#OPT = -O3 -ip -w -pc64 -tpp6
-OPT= -O3 -w
-FFLAGS = -c ${OPT} -I../
-
-#LIBS = -static-libcxa ../xdrf/libxdrf.a
-LIBS = ../xdrf/libxdrf.a
-
-CPPFLAGS = -DLINUX -DUNRES -DMP -DMPI -DSPLITELE -DPROCOR
-
-.SUFFIXES: .F
-.F.o:
- ${FC} ${FFLAGS} ${CPPFLAGS} $*.F
-
-all:xdrf2pdb xdrf2x
-
-xdrf2pdb: xdrf2pdb.o geomout.o misc.o rescode.o nazwy.o
- ${FC} -o ${BINDIR}/xdrf2pdb xdrf2pdb.o geomout.o rescode.o misc.o nazwy.o ${LIBS}
-
-xdrf2x: xdrf2x.o
- ${FC} -o ${BINDIR}/xdrf2x xdrf2x.o ${LIBS}
-
-clean:
- rm -f *.o
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BINDIR=../../bin
-
-CC = cc
-CFLAGS = -O
-
-FC=ifort
-#FC=f77
-OPT = -O3 -ip -w -pc64 -tpp6
-#OPT= -O3 -w
-FFLAGS = -c ${OPT} -I../
-
-LIBS = -static-libcxa ../xdrf/libxdrf.a
-#LIBS = xdrf_local/libxdrf.a
-
-CPPFLAGS = -DLINUX -DUNRES -DMP -DMPI -DSPLITELE -DPROCOR
-
-.SUFFIXES: .F
-.F.o:
- ${FC} ${FFLAGS} ${CPPFLAGS} $*.F
-
-all:xdrf2pdb xdrf2x
-
-xdrf2pdb: xdrf2pdb.o geomout.o misc.o rescode.o nazwy.o
- ${FC} -o ${BINDIR}/xdrf2pdb xdrf2pdb.o geomout.o rescode.o misc.o nazwy.o ${LIBS}
-
-xdrf2x: xdrf2x.o
- ${FC} -o ${BINDIR}/xdrf2x xdrf2x.o ${LIBS}
-
-clean:
- rm -f *.o
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BINDIR=../../bin
-
-CC = cc
-CFLAGS = -O
-
-#FC=ifort
-FC=f77
-#OPT = -O3 -ip -w -pc64 -tpp6
-OPT= -O3 -w
-FFLAGS = -c ${OPT} -I../
-
-#LIBS = -static-libcxa ../xdrf/libxdrf.a
-LIBS = xdrf_local/libxdrf.a
-
-CPPFLAGS = -DLINUX -DUNRES -DMP -DMPI -DSPLITELE -DPROCOR
-
-.SUFFIXES: .F
-.F.o:
- ${FC} ${FFLAGS} ${CPPFLAGS} $*.F
-
-all:xdrf2pdb xdrf2x
-
-xdrf2pdb: xdrf2pdb.o geomout.o misc.o rescode.o nazwy.o
- ${FC} -o ${BINDIR}/xdrf2pdb xdrf2pdb.o geomout.o rescode.o misc.o nazwy.o ${LIBS}
-
-xdrf2x: xdrf2x.o
- ${FC} -o ${BINDIR}/xdrf2x xdrf2x.o ${LIBS}
-
-clean:
- rm -f *.o
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BINDIR=../../bin
-
-CC = cc
-CFLAGS = -O
-
-#FC=ifort
-FC=f77
-#OPT = -O3 -ip -w -pc64 -tpp6
-OPT= -O3 -w
-FFLAGS = -c ${OPT} -I../
-
-#LIBS = -static-libcxa ../xdrf/libxdrf.a
-LIBS = ../xdrf/libxdrf.a
-
-CPPFLAGS = -DLINUX -DUNRES -DMP -DMPI -DSPLITELE -DPROCOR
-
-.SUFFIXES: .F
-.F.o:
- ${FC} ${FFLAGS} ${CPPFLAGS} $*.F
-
-all:xdrf2pdb xdrf2x
-
-xdrf2pdb: xdrf2pdb.o geomout.o misc.o rescode.o nazwy.o
- ${FC} -o ${BINDIR}/xdrf2pdb xdrf2pdb.o geomout.o rescode.o misc.o nazwy.o ${LIBS}
-
-xdrf2x: xdrf2x.o
- ${FC} -o ${BINDIR}/xdrf2x xdrf2x.o ${LIBS}
-
-clean:
- rm -f *.o
-
+++ /dev/null
- subroutine pdbout(etot,tytul,iunit)
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.CHAIN'
- include 'COMMON.INTERACT'
- include 'COMMON.NAMES'
- include 'COMMON.IOUNITS'
- include 'COMMON.HEADER'
- include 'COMMON.SBRIDGE'
- include 'COMMON.DISTFIT'
- include 'COMMON.MD'
- character*50 tytul
- dimension ica(maxres)
- write (iunit,'(3a,1pe15.5)') 'REMARK ',tytul,' ENERGY ',etot
-cmodel write (iunit,'(a5,i6)') 'MODEL',1
- if (nhfrag.gt.0) then
- do j=1,nhfrag
- iti=itype(hfrag(1,j))
- itj=itype(hfrag(2,j))
- if (j.lt.10) then
- write (iunit,'(a5,i5,1x,a1,i1,2x,a3,i7,2x,a3,i7,i3,t76,i5)')
- & 'HELIX',j,'H',j,
- & restyp(iti),hfrag(1,j)-1,
- & restyp(itj),hfrag(2,j)-1,1,hfrag(2,j)-hfrag(1,j)
- else
- write (iunit,'(a5,i5,1x,a1,i2,1x,a3,i7,2x,a3,i7,i3)')
- & 'HELIX',j,'H',j,
- & restyp(iti),hfrag(1,j)-1,
- & restyp(itj),hfrag(2,j)-1,1,hfrag(2,j)-hfrag(1,j)
- endif
- enddo
- endif
-
- if (nbfrag.gt.0) then
-
- do j=1,nbfrag
-
- iti=itype(bfrag(1,j))
- itj=itype(bfrag(2,j)-1)
-
- write (iunit,'(a5,i5,1x,a1,i1,i3,1x,a3,i6,2x,a3,i6,i3)')
- & 'SHEET',1,'B',j,2,
- & restyp(iti),bfrag(1,j)-1,
- & restyp(itj),bfrag(2,j)-2,0
-
- if (bfrag(3,j).gt.bfrag(4,j)) then
-
- itk=itype(bfrag(3,j))
- itl=itype(bfrag(4,j)+1)
-
- write (iunit,'(a5,i5,1x,a1,i1,i3,1x,a3,i6,2x,a3,i6,i3,
- & 2x,a1,2x,a3,i6,3x,a1,2x,a3,i6)')
- & 'SHEET',2,'B',j,2,
- & restyp(itl),bfrag(4,j),
- & restyp(itk),bfrag(3,j)-1,-1,
- & "N",restyp(itk),bfrag(3,j)-1,
- & "O",restyp(iti),bfrag(1,j)-1
-
- else
-
- itk=itype(bfrag(3,j))
- itl=itype(bfrag(4,j)-1)
-
-
- write (iunit,'(a5,i5,1x,a1,i1,i3,1x,a3,i6,2x,a3,i6,i3,
- & 2x,a1,2x,a3,i6,3x,a1,2x,a3,i6)')
- & 'SHEET',2,'B',j,2,
- & restyp(itk),bfrag(3,j)-1,
- & restyp(itl),bfrag(4,j)-2,1,
- & "N",restyp(itk),bfrag(3,j)-1,
- & "O",restyp(iti),bfrag(1,j)-1
-
-
-
- endif
-
- enddo
- endif
-
- if (nss.gt.0) then
- do i=1,nss
- write(iunit,'(a6,i4,1x,a3,i7,4x,a3,i7)')
- & 'SSBOND',i,'CYS',ihpb(i)-1-nres,
- & 'CYS',jhpb(i)-1-nres
- enddo
- endif
-
- iatom=0
- do i=nnt,nct
- ires=i-nnt+1
- iatom=iatom+1
- ica(i)=iatom
- iti=itype(i)
- write (iunit,10) iatom,restyp(iti),ires,(c(j,i),j=1,3),vtot(i)
- if (iti.ne.10) then
- iatom=iatom+1
- write (iunit,20) iatom,restyp(iti),ires,(c(j,nres+i),j=1,3),
- & vtot(i+nres)
- endif
- enddo
- write (iunit,'(a)') 'TER'
- do i=nnt,nct-1
- if (itype(i).eq.10) then
- write (iunit,30) ica(i),ica(i+1)
- else
- write (iunit,30) ica(i),ica(i+1),ica(i)+1
- endif
- enddo
- if (itype(nct).ne.10) then
- write (iunit,30) ica(nct),ica(nct)+1
- endif
- do i=1,nss
- write (iunit,30) ica(ihpb(i)-nres)+1,ica(jhpb(i)-nres)+1
- enddo
- write (iunit,'(a6)') 'ENDMDL'
- 10 FORMAT ('ATOM',I7,' CA ',A3,I6,4X,3F8.3,f15.3)
- 20 FORMAT ('ATOM',I7,' CB ',A3,I6,4X,3F8.3,f15.3)
- 30 FORMAT ('CONECT',8I5)
- return
- end
-c------------------------------------------------------------------------------
- subroutine MOL2out(etot,tytul)
-C Prints the Cartesian coordinates of the alpha-carbons in the Tripos mol2
-C format.
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.CHAIN'
- include 'COMMON.INTERACT'
- include 'COMMON.NAMES'
- include 'COMMON.IOUNITS'
- include 'COMMON.HEADER'
- include 'COMMON.SBRIDGE'
- character*32 tytul,fd
- character*3 liczba
- character*6 res_num,pom,ucase
-#ifdef AIX
- call fdate_(fd)
-#elif (defined CRAY)
- call date(fd)
-#else
- call fdate(fd)
-#endif
- write (imol2,'(a)') '#'
- write (imol2,'(a)')
- & '# Creating user name: unres'
- write (imol2,'(2a)') '# Creation time: ',
- & fd
- write (imol2,'(/a)') '\@<TRIPOS>MOLECULE'
- write (imol2,'(a)') tytul
- write (imol2,'(5i5)') nct-nnt+1,nct-nnt+nss+1,nct-nnt+nss+1,0,0
- write (imol2,'(a)') 'SMALL'
- write (imol2,'(a)') 'USER_CHARGES'
- write (imol2,'(a)') '\@<TRIPOS>ATOM'
- do i=nnt,nct
- write (liczba,'(i3)') i
- pom=ucase(restyp(itype(i)))
- res_num = pom(:3)//liczba(2:)
- write (imol2,10) i,(c(j,i),j=1,3),i,res_num,0.0
- enddo
- write (imol2,'(a)') '\@<TRIPOS>BOND'
- do i=nnt,nct-1
- write (imol2,'(i5,2i6,i2)') i-nnt+1,i-nnt+1,i-nnt+2,1
- enddo
- do i=1,nss
- write (imol2,'(i5,2i6,i2)') nct-nnt+i,ihpb(i),jhpb(i),1
- enddo
- write (imol2,'(a)') '\@<TRIPOS>SUBSTRUCTURE'
- do i=nnt,nct
- write (liczba,'(i3)') i
- pom = ucase(restyp(itype(i)))
- res_num = pom(:3)//liczba(2:)
- write (imol2,30) i-nnt+1,res_num,i-nnt+1,0
- enddo
- 10 FORMAT (I7,' CA ',3F10.4,' C.3',I8,1X,A,F11.4,' ****')
- 30 FORMAT (I7,1x,A,I14,' RESIDUE',I13,' **** ****')
- return
- end
-c------------------------------------------------------------------------
- subroutine intout
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.IOUNITS'
- include 'COMMON.CHAIN'
- include 'COMMON.VAR'
- include 'COMMON.LOCAL'
- include 'COMMON.INTERACT'
- include 'COMMON.NAMES'
- include 'COMMON.GEO'
- write (iout,'(/a)') 'Geometry of the virtual chain.'
- write (iout,'(7a)') ' Res ',' d',' Theta',
- & ' Phi',' Dsc',' Alpha',' Omega'
- do i=1,nres
- iti=itype(i)
- write (iout,'(a3,i4,6f10.3)') restyp(iti),i,vbld(i),
- & rad2deg*theta(i),rad2deg*phi(i),vbld(nres+i),rad2deg*alph(i),
- & rad2deg*omeg(i)
- enddo
- return
- end
-c---------------------------------------------------------------------------
- subroutine briefout(it,ener)
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.IOUNITS'
- include 'COMMON.CHAIN'
- include 'COMMON.VAR'
- include 'COMMON.LOCAL'
- include 'COMMON.INTERACT'
- include 'COMMON.NAMES'
- include 'COMMON.GEO'
- include 'COMMON.SBRIDGE'
-c print '(a,i5)',intname,igeom
-#if defined(AIX) || defined(PGI)
- open (igeom,file=intname,position='append')
-#else
- open (igeom,file=intname,access='append')
-#endif
- IF (NSS.LE.9) THEN
- WRITE (igeom,180) IT,ENER,NSS,(IHPB(I),JHPB(I),I=1,NSS)
- ELSE
- WRITE (igeom,180) IT,ENER,NSS,(IHPB(I),JHPB(I),I=1,9)
- WRITE (igeom,190) (IHPB(I),JHPB(I),I=10,NSS)
- ENDIF
-c IF (nvar.gt.nphi) WRITE (igeom,200) (RAD2DEG*THETA(I),I=3,NRES)
- WRITE (igeom,200) (RAD2DEG*THETA(I),I=3,NRES)
- WRITE (igeom,200) (RAD2DEG*PHI(I),I=4,NRES)
-c if (nvar.gt.nphi+ntheta) then
- write (igeom,200) (rad2deg*alph(i),i=2,nres-1)
- write (igeom,200) (rad2deg*omeg(i),i=2,nres-1)
-c endif
- close(igeom)
- 180 format (I5,F12.3,I2,9(1X,2I3))
- 190 format (3X,11(1X,2I3))
- 200 format (8F10.4)
- return
- end
-#ifdef WINIFL
- subroutine fdate(fd)
- character*32 fd
- write(fd,'(32x)')
- return
- end
-#endif
-c----------------------------------------------------------------
- subroutine cartoutx(time)
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.CHAIN'
- include 'COMMON.INTERACT'
- include 'COMMON.NAMES'
- include 'COMMON.IOUNITS'
- include 'COMMON.HEADER'
- include 'COMMON.SBRIDGE'
- include 'COMMON.DISTFIT'
- include 'COMMON.MD'
- double precision time
-#if defined(AIX) || defined(PGI)
- open(icart,file=cartname,position="append")
-#else
- open(icart,file=cartname,access="append")
-#endif
- write (icart,'(e15.8,2e15.5,f12.5,$)') time,potE,uconst,t_bath
- write (icart,'(i4,$)')
- & nss,(ihpb(j),jhpb(j),j=1,nss)
- write (icart,'(i4,20f7.4)') nfrag+npair,
- & (qfrag(i),i=1,nfrag),(qpair(i),i=1,npair)
- write (icart,'(8f10.5)')
- & ((c(k,j),k=1,3),j=1,nres),
- & ((c(k,j+nres),k=1,3),j=nnt,nct)
- close(icart)
- return
- end
-c-----------------------------------------------------------------
- subroutine cartout(time)
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.CHAIN'
- include 'COMMON.INTERACT'
- include 'COMMON.NAMES'
- include 'COMMON.IOUNITS'
- include 'COMMON.HEADER'
- include 'COMMON.SBRIDGE'
- include 'COMMON.DISTFIT'
- include 'COMMON.MD'
- double precision time
- integer iret,itmp
- real xcoord(3,maxres2+2),prec
-
- call xdrfopen_(ixdrf,cartname, "w", iret)
- call xdrffloat_(ixdrf, real(time), iret)
- call xdrffloat_(ixdrf, real(potE), iret)
- call xdrffloat_(ixdrf, real(uconst), iret)
- call xdrffloat_(ixdrf, real(t_bath), iret)
- call xdrfint_(ixdrf, nss, iret)
- do j=1,nss
- call xdrfint_(ixdrf, ihpb(j), iret)
- call xdrfint_(ixdrf, jhpb(j), iret)
- enddo
- call xdrfint_(ixdrf, nfrag+npair, iret)
- do i=1,nfrag
- call xdrffloat_(ixdrf, real(qfrag(i)), iret)
- enddo
- do i=1,npair
- call xdrffloat_(ixdrf, real(qpair(i)), iret)
- enddo
- prec=10000.0
- do i=1,nres
- do j=1,3
- xcoord(j,i)=c(j,i)
- enddo
- enddo
- do i=nnt,nct
- do j=1,3
- xcoord(j,nres+i-nnt+1)=c(j,i+nres)
- enddo
- enddo
-
- itmp=nres+nct-nnt+1
- call xdrf3dfcoord_(ixdrf, xcoord, itmp, prec, iret)
- call xdrfclose_(ixdrf, iret)
- return
- end
+++ /dev/null
-C $Date: 1994/10/12 17:24:21 $
-C $Revision: 2.5 $
-C
-C
-C
- logical function find_arg(ipos,line,errflag)
- parameter (maxlen=80)
- character*80 line
- character*1 empty /' '/,equal /'='/
- logical errflag
-* This function returns .TRUE., if an argument follows keyword keywd; if so
-* IPOS will point to the first non-blank character of the argument. Returns
-* .FALSE., if no argument follows the keyword; in this case IPOS points
-* to the first non-blank character of the next keyword.
- do while (line(ipos:ipos) .eq. empty .and. ipos.le.maxlen)
- ipos=ipos+1
- enddo
- errflag=.false.
- if (line(ipos:ipos).eq.equal) then
- find_arg=.true.
- ipos=ipos+1
- do while (line(ipos:ipos) .eq. empty .and. ipos.le.maxlen)
- ipos=ipos+1
- enddo
- if (ipos.gt.maxlen) errflag=.true.
- else
- find_arg=.false.
- endif
- return
- end
- logical function find_group(iunit,jout,key1)
- character*(*) key1
- character*80 karta,ucase
- integer ilen
- external ilen
- logical lcom
- rewind (iunit)
- karta=' '
- ll=ilen(key1)
- do while (index(ucase(karta),key1(1:ll)).eq.0.or.lcom(1,karta))
- read (iunit,'(a)',end=10) karta
- enddo
- write (jout,'(2a)') '> ',karta(1:78)
- find_group=.true.
- return
- 10 find_group=.false.
- return
- end
- logical function iblnk(charc)
- character*1 charc
- integer n
- n = ichar(charc)
- iblnk = (n.eq.9) .or. (n.eq.10) .or. (charc.eq. ' ')
- return
- end
- integer function ilen(string)
- character*(*) string
- logical iblnk
-
- ilen = len(string)
-1 if ( ilen .gt. 0 ) then
- if ( iblnk( string(ilen:ilen) ) ) then
- ilen = ilen - 1
- goto 1
- endif
- endif
- return
- end
- integer function in_keywd_set(nkey,ikey,narg,keywd,keywdset)
- character*16 keywd,keywdset(1:nkey,0:nkey)
- character*16 ucase
- do i=1,narg
- if (ucase(keywd).eq.keywdset(i,ikey)) then
-* Match found
- in_keywd_set=i
- return
- endif
- enddo
-* No match to the allowed set of keywords if this point is reached.
- in_keywd_set=0
- return
- end
- character*(*) function lcase(string)
- integer i, k, idiff
- character*(*) string
- character*1 c
- character*40 chtmp
-c
- i = len(lcase)
- k = len(string)
- if (i .lt. k) then
- k = i
- if (string(k+1:) .ne. ' ') then
- chtmp = string
- endif
- endif
- idiff = ichar('a') - ichar('A')
- lcase = string
- do 99 i = 1, k
- c = string(i:i)
- if (lge(c,'A') .and. lle(c,'Z')) then
- lcase(i:i) = char(ichar(c) + idiff)
- endif
- 99 continue
- return
- end
- logical function lcom(ipos,karta)
- character*80 karta
- character koment(2) /'!','#'/
- lcom=.false.
- do i=1,2
- if (karta(ipos:ipos).eq.koment(i)) lcom=.true.
- enddo
- return
- end
- logical function lower_case(ch)
- character*(*) ch
- lower_case=(ch.ge.'a' .and. ch.le.'z')
- return
- end
- subroutine mykey(line,keywd,ipos,blankline,errflag)
-* This subroutine seeks a non-empty substring keywd in the string LINE.
-* The substring begins with the first character different from blank and
-* "=" encountered right to the pointer IPOS (inclusively) and terminates
-* at the character left to the first blank or "=". When the subroutine is
-* exited, the pointer IPOS is moved to the position of the terminator in LINE.
-* The logical variable BLANKLINE is set at .TRUE., if LINE(IPOS:) contains
-* only separators or the maximum length of the data line (80) has been reached.
-* The logical variable ERRFLAG is set at .TRUE. if the string
-* consists only from a "=".
- parameter (maxlen=80)
- character*1 empty /' '/,equal /'='/,comma /','/
- character*(*) keywd
- character*80 line
- logical blankline,errflag,lcom
- errflag=.false.
- do while (line(ipos:ipos).eq.empty .and. (ipos.le.maxlen))
- ipos=ipos+1
- enddo
- if (ipos.gt.maxlen .or. lcom(ipos,line) ) then
-* At this point the rest of the input line turned out to contain only blanks
-* or to be commented out.
- blankline=.true.
- return
- endif
- blankline=.false.
- istart=ipos
-* Checks whether the current char is a separator.
- do while (line(ipos:ipos).ne.empty .and. line(ipos:ipos).ne.equal
- & .and. line(ipos:ipos).ne.comma .and. ipos.le.maxlen)
- ipos=ipos+1
- enddo
- iend=ipos-1
-* Error flag set to .true., if the length of the keyword was found less than 1.
- if (iend.lt.istart) then
- errflag=.true.
- return
- endif
- keywd=line(istart:iend)
- return
- end
- subroutine numstr(inum,numm)
- character*10 huj /'0123456789'/
- character*(*) numm
- inumm=inum
- inum1=inumm/10
- inum2=inumm-10*inum1
- inumm=inum1
- numm(3:3)=huj(inum2+1:inum2+1)
- inum1=inumm/10
- inum2=inumm-10*inum1
- inumm=inum1
- numm(2:2)=huj(inum2+1:inum2+1)
- inum1=inumm/10
- inum2=inumm-10*inum1
- inumm=inum1
- numm(1:1)=huj(inum2+1:inum2+1)
- return
- end
- character*(*) function ucase(string)
- integer i, k, idiff
- character*(*) string
- character*1 c
- character*40 chtmp
-c
- i = len(ucase)
- k = len(string)
- if (i .lt. k) then
- k = i
- if (string(k+1:) .ne. ' ') then
- chtmp = string
- endif
- endif
- idiff = ichar('a') - ichar('A')
- ucase = string
- do 99 i = 1, k
- c = string(i:i)
- if (lge(c,'a') .and. lle(c,'z')) then
- ucase(i:i) = char(ichar(c) - idiff)
- endif
- 99 continue
- return
- end
+++ /dev/null
- block data nazwy
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.NAMES'
- include 'COMMON.FFIELD'
- data restyp /
- &'CYS','MET','PHE','ILE','LEU','VAL','TRP','TYR','ALA','GLY','THR',
- &'SER','GLN','ASN','GLU','ASP','HIS','ARG','LYS','PRO','D'/
- data onelet /
- &'C','M','F','I','L','V','W','Y','A','G','T',
- &'S','Q','N','E','D','H','R','K','P','X'/
- data potname /'LJ','LJK','BP','GB','GBV'/
- data ename /
- & "EVDW SC-SC","EVDW2 SC-p","EES p-p","ECORR4 ","ECORR5 ",
- & "ECORR6 ","EELLO ","ETURN3 ","ETURN4 ","ETURN6 ",
- & "EBE bend","ESC SCloc","ETORS ","ETORSD ","EHPB ","EVDWPP ",
- & "ESTR ","EVDW2_14 ","UCONST ", 2*""/
- data wname /
- & "WSC","WSCP","WELEC","WCORR","WCORR5","WCORR6","WEL_LOC",
- & "WTURN3","WTURN4","WTURN6","WANG","WSCLOC","WTOR","WTORD",
- & "WSTRAIN","WVDWPP","WBOND","SCAL14",2*""/
- data nprint_ene /20/
- data print_order/1,2,3,11,12,13,14,4,5,6,7,8,9,10,18,15,17,16,20,
- & 2*0/
- end
-
+++ /dev/null
- integer function rescode(iseq,nam,ione)
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.NAMES'
- include 'COMMON.IOUNITS'
- character*3 nam,ucase
-
- if (ione.eq.0) then
-
- do i=1,ntyp1
- if (ucase(nam).eq.restyp(i)) then
- rescode=i
- return
- endif
- enddo
- else
-
- do i=1,ntyp1
- if (nam(1:1).eq.onelet(i)) then
- rescode=i
- return
- endif
- enddo
-
- endif
-
- write (iout,10) iseq,nam
- stop
- 10 format ('**** Error - residue',i4,' has an unresolved name ',a3)
- end
-
+++ /dev/null
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.CHAIN'
- include 'COMMON.INTERACT'
- include 'COMMON.SBRIDGE'
- real*4 coord(3,1000)
- real*4 prec,time,potE,uconst,t_bath,qfrag(100)
- real*8 etot
- character*80 arg,seqfile,pdbfile
- character*3 sequenc(maxres)
- character*50 tytul
- character*8 onethree,cfreq
- character*8 ucase
- external ucase
- logical oneletter,iblnk
- integer rescode
- external rescode
-
- do i=1,maxres
- sequenc(i)=''
- enddo
-
- ifreq=1
- if (iargc().lt.3) then
- print '(a)',
- & "Usage: xdrf2pdb one/three seqfile cxfile [freq] [pdbfile]"
- stop
- endif
- call getarg(1,onethree)
- onethree = ucase(onethree)
- if (onethree.eq.'ONE') then
- oneletter = .true.
- else if (onethree.eq.'THREE') then
- oneletter = .false.
- else
- print *,"ONE or THREE must be specified"
- endif
- call getarg(2,seqfile)
- open (1,file=seqfile,status='old')
- if (oneletter) then
- read(1,'(80a1)',end=10,err=10) (sequenc(i)(1:1),i=1,maxres)
- 10 continue
- nres=i
- i=0
- do while (.not.iblnk(sequenc(i+1)(1:1)))
- i=i+1
- enddo
- nres=i
- do i=1,nres
- itype(i)=rescode(i,sequenc(i),1)
- enddo
- else
- read(1,'(20(a3,1x))',end=11,err=11) (sequenc(i),i=1,maxres)
- 11 continue
- nres=i
- i=0
- do while (.not.iblnk(sequenc(i+1)(1:1)))
- print *,sequenc(i+1)
- i=i+1
- enddo
- nres=i
- do i=1,nres
- itype(i)=rescode(i,sequenc(i),0)
- enddo
- endif
- call getarg(3,arg)
- iext = index(arg,'.cx') - 1
- if (iext.lt.0) then
- print *,"Error - not a cx file"
- stop
- endif
- if (iargc().gt.3) then
- call getarg(4,cfreq)
- read (cfreq,*) ifreq
- endif
- if (iargc().gt.4) then
- call getarg(5,pdbfile)
- else
- pdbfile=arg(:iext)//'.pdb'
- endif
- open(9,file=pdbfile)
- nnt = 1
- if (itype(1).eq.21) nnt = 2
- nct=nres
- if (itype(nres).eq.21) nct = nres-1
-
- call xdrfopen_(ixdrf,arg, "r", iret)
-
- kk = 0
- do while(.true.)
- call xdrffloat_(ixdrf, time, iret)
- if(iret.eq.0) exit
- kk = kk + 1
- call xdrffloat_(ixdrf, potE, iret)
- call xdrffloat_(ixdrf, uconst, iret)
- call xdrffloat_(ixdrf, t_bath, iret)
- call xdrfint_(ixdrf, nss, iret)
- do j=1,nss
- call xdrfint_(ixdrf, ihpb(j), iret)
- call xdrfint_(ixdrf, jhpb(j), iret)
- enddo
- call xdrfint_(ixdrf, nfrag, iret)
- do i=1,nfrag
- call xdrffloat_(ixdrf, qfrag(i), iret)
- enddo
- prec=10000.0
-
- isize=0
- call xdrf3dfcoord_(ixdrf, coord, isize, prec, iret)
-
-
-c write (*,'(e15.8,2e15.5,f12.5,$)') time,potE,uconst,t_bath
-c write (*,'(i4,$)') nss,(ihpb(j),jhpb(j),j=1,nss)
-c write (*,'(i4,20f7.4)') nfrag,(qfrag(i),i=1,nfrag)
-c write (*,'(8f10.5)') ((coord(k,j),k=1,3),j=1,isize)
- if (mod(kk,ifreq).eq.0) then
- if (isize .ne. nres + nct - nnt + 1) then
- print *,"Error: inconsistent sizes",isize,nres+nct-nnt+1
- endif
- do i=1,nres
- do j=1,3
- c(j,i)=coord(j,i)
- enddo
- enddo
- ii = 0
- do i=nnt,nct
- ii = ii + 1
- do j=1,3
- c(j,i+nres)=coord(j,ii+nres)
- enddo
- enddo
- etot=potE
- write (tytul,'(a,i6)') "Structure",kk
- call pdbout(etot,tytul,9)
- endif
- enddo
-
- end
+++ /dev/null
- real coord(3,1000)
- real prec,time,potE,uconst,t_bath,qfrag(100)
- integer ihpb(100),jhpb(100),iset
- character*80 arg
-
- print *,"start"
- call getarg(1,arg)
-
- print *,arg
-#ifdef BLUEGENE
- call xdrfopen_(ixdrf,arg, "r", iret)
-
- do while(.true.)
- call xdrffloat_(ixdrf, time, iret)
- if(iret.eq.0) goto 10
- call xdrffloat_(ixdrf, potE, iret)
- call xdrffloat_(ixdrf, uconst, iret)
- call xdrffloat_(ixdrf, t_bath, iret)
- call xdrfint_(ixdrf, nss, iret)
- do j=1,nss
- call xdrfint_(ixdrf, ihpb(j), iret)
- call xdrfint_(ixdrf, jhpb(j), iret)
- enddo
- call xdrfint_(ixdrf, nfrag, iret)
- call xdrfint_(ixdrf, iset, iret)
- do i=1,nfrag
- call xdrffloat_(ixdrf, qfrag(i), iret)
- enddo
- prec=10000.0
-
- isize=0
- call xdrf3dfcoord_(ixdrf, coord, isize, prec, iret)
-#else
- call xdrfopen(ixdrf,arg, "r", iret)
-
- do while(.true.)
- call xdrffloat(ixdrf, time, iret)
- if(iret.eq.0) goto 10
- call xdrffloat(ixdrf, potE, iret)
- call xdrffloat(ixdrf, uconst, iret)
- call xdrffloat(ixdrf, t_bath, iret)
- call xdrfint(ixdrf, nss, iret)
- do j=1,nss
- call xdrfint(ixdrf, ihpb(j), iret)
- call xdrfint(ixdrf, jhpb(j), iret)
- enddo
- call xdrfint(ixdrf, nfrag, iret)
- call xdrfint(ixdrf, iset, iret)
- do i=1,nfrag
- call xdrffloat(ixdrf, qfrag(i), iret)
- enddo
- prec=10000.0
-
- isize=0
- call xdrf3dfcoord(ixdrf, coord, isize, prec, iret)
-#endif
-
- write (*,'(e15.8,2e15.5,f12.5,$)') time,potE,uconst,t_bath
- write (*,'(i4,$)') nss,(ihpb(j),jhpb(j),j=1,nss)
- write (*,'(i4)') iset
- write (*,'(i4,20f7.4)') nfrag,(qfrag(i),i=1,nfrag)
- write (*,'(8f10.5)') ((coord(k,j),k=1,3),j=1,isize)
- enddo
- 10 continue
- end
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-
-
-int ftocstr(ds, dl, ss, sl)
- char *ds, *ss; /* dst, src ptrs */
- int dl; /* dst max len */
- int sl; /* src len */
-{
- char *p;
-
- for (p = ss + sl; --p >= ss && *p == ' '; ) ;
- sl = p - ss + 1;
- dl--;
- ds[0] = 0;
- if (sl > dl)
- return 1;
- while (sl--)
- (*ds++ = *ss++);
- *ds = '\0';
- return 0;
-}
-
-
-int ctofstr(ds, dl, ss)
- char *ds; /* dest space */
- int dl; /* max dest length */
- char *ss; /* src string (0-term) */
-{
- while (dl && *ss) {
- *ds++ = *ss++;
- dl--;
- }
- while (dl--)
- *ds++ = ' ';
- return 0;
-}
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# append an underscore to FORTRAN function names
-#
-define(`FUNCTION',`$1_')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
+++ /dev/null
-/*_________________________________________________________________
- |
- | xdrf.h - include file for C routines that want to use the
- | functions below.
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type);
-int xdrclose(XDR *xdrs) ;
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
-
-CC = /usr/bin/blrts_xlc
-CPPC = /usr/bin/blrts_xlc
-
-CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
-
-M4 = m4
-M4FILE = RS6K.m4
-
-libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f *.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
-# rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
-
-CC = /usr/bin/blrts_xlc
-CPPC = /usr/bin/blrts_xlc
-
-CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
-
-M4 = m4
-M4FILE = RS6K.m4
-
-libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
-# rm -f libxdrf.c
-
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# do nothing special to FORTRAN function names
-#
-define(`FUNCTION',`$1')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
-
+++ /dev/null
-
-
-int ftocstr(ds, dl, ss, sl)
- char *ds, *ss; /* dst, src ptrs */
- int dl; /* dst max len */
- int sl; /* src len */
-{
- char *p;
-
- for (p = ss + sl; --p >= ss && *p == ' '; ) ;
- sl = p - ss + 1;
- dl--;
- ds[0] = 0;
- if (sl > dl)
- return 1;
- while (sl--)
- (*ds++ = *ss++);
- *ds = '\0';
- return 0;
-}
-
-
-int ctofstr(ds, dl, ss)
- char *ds; /* dest space */
- int dl; /* max dest length */
- char *ss; /* src string (0-term) */
-{
- while (dl && *ss) {
- *ds++ = *ss++;
- dl--;
- }
- while (dl--)
- *ds++ = ' ';
- return 0;
-}
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-/* #include <rpc/rpc.h>
-#include <rpc/xdr.h> */
-#include "xdr.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "w+";
- lmode = XDR_ENCODE;
- } else if (*type == 'a' || *type == 'A') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-#include <rpc/rpc.h>
-#include <rpc/xdr.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-/* fixincludes should not add extern "C" to this file */
-/*
- * Rpc additions to <sys/types.h>
- */
-#ifndef _RPC_TYPES_H
-#define _RPC_TYPES_H 1
-
-typedef int bool_t;
-typedef int enum_t;
-/* This needs to be changed to uint32_t in the future */
-typedef unsigned long rpcprog_t;
-typedef unsigned long rpcvers_t;
-typedef unsigned long rpcproc_t;
-typedef unsigned long rpcprot_t;
-typedef unsigned long rpcport_t;
-
-#define __dontcare__ -1
-
-#ifndef FALSE
-# define FALSE (0)
-#endif
-
-#ifndef TRUE
-# define TRUE (1)
-#endif
-
-#ifndef NULL
-# define NULL 0
-#endif
-
-#include <stdlib.h> /* For malloc decl. */
-#define mem_alloc(bsize) malloc(bsize)
-/*
- * XXX: This must not use the second argument, or code in xdr_array.c needs
- * to be modified.
- */
-#define mem_free(ptr, bsize) free(ptr)
-
-#ifndef makedev /* ie, we haven't already included it */
-#include <sys/types.h>
-#endif
-
-#ifndef __u_char_defined
-typedef __u_char u_char;
-typedef __u_short u_short;
-typedef __u_int u_int;
-typedef __u_long u_long;
-typedef __quad_t quad_t;
-typedef __u_quad_t u_quad_t;
-typedef __fsid_t fsid_t;
-# define __u_char_defined
-#endif
-#ifndef __daddr_t_defined
-typedef __daddr_t daddr_t;
-typedef __caddr_t caddr_t;
-# define __daddr_t_defined
-#endif
-
-#include <sys/time.h>
-#include <sys/param.h>
-
-#include <netinet/in.h>
-
-#ifndef INADDR_LOOPBACK
-#define INADDR_LOOPBACK (u_long)0x7F000001
-#endif
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 64
-#endif
-
-#endif /* rpc/types.h */
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# append an underscore to FORTRAN function names
-#
-define(`FUNCTION',`$1_')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
-#endif
-
-/*
- * xdr.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1986, Sun Microsystems, Inc.
- *
- * These are the "generic" xdr routines used to serialize and de-serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <libintl.h>
-
-#include "types.h"
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-/*
- * constants specific to the xdr "protocol"
- */
-#define XDR_FALSE ((long) 0)
-#define XDR_TRUE ((long) 1)
-#define LASTUNSIGNED ((u_int) 0-1)
-
-/*
- * for unit alignment
- */
-static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
-
-/*
- * Free a data structure using XDR
- * Not a filter, but a convenient utility nonetheless
- */
-void
-xdr_free (xdrproc_t proc, char *objp)
-{
- XDR x;
-
- x.x_op = XDR_FREE;
- (*proc) (&x, objp);
-}
-
-/*
- * XDR nothing
- */
-bool_t
-xdr_void (void)
-{
- return TRUE;
-}
-INTDEF(xdr_void)
-
-/*
- * XDR integers
- */
-bool_t
-xdr_int (XDR *xdrs, int *ip)
-{
-
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *ip;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ip = (int) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif INT_MAX == LONG_MAX
- return INTUSE(xdr_long) (xdrs, (long *) ip);
-#elif INT_MAX == SHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) ip);
-#else
-#error unexpected integer sizes in_xdr_int()
-#endif
-}
-INTDEF(xdr_int)
-
-/*
- * XDR unsigned integers
- */
-bool_t
-xdr_u_int (XDR *xdrs, u_int *up)
-{
-#if UINT_MAX < ULONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * up;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *up = (u_int) (u_long) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif UINT_MAX == ULONG_MAX
- return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
-#elif UINT_MAX == USHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) up);
-#else
-#error unexpected integer sizes in_xdr_u_int()
-#endif
-}
-INTDEF(xdr_u_int)
-
-/*
- * XDR long integers
- * The definition of xdr_long() is kept for backward
- * compatibility. Instead xdr_int() should be used.
- */
-bool_t
-xdr_long (XDR *xdrs, long *lp)
-{
-
- if (xdrs->x_op == XDR_ENCODE
- && (sizeof (int32_t) == sizeof (long)
- || (int32_t) *lp == *lp))
- return XDR_PUTLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_DECODE)
- return XDR_GETLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_long)
-
-/*
- * XDR unsigned long integers
- * The definition of xdr_u_long() is kept for backward
- * compatibility. Instead xdr_u_int() should be used.
- */
-bool_t
-xdr_u_long (XDR *xdrs, u_long *ulp)
-{
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- {
- long int tmp;
-
- if (XDR_GETLONG (xdrs, &tmp) == FALSE)
- return FALSE;
-
- *ulp = (uint32_t) tmp;
- return TRUE;
- }
-
- case XDR_ENCODE:
- if (sizeof (uint32_t) != sizeof (u_long)
- && (uint32_t) *ulp != *ulp)
- return FALSE;
-
- return XDR_PUTLONG (xdrs, (long *) ulp);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_long)
-
-/*
- * XDR hyper integers
- * same as xdr_u_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_hyper (XDR *xdrs, quad_t *llp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (long) ((*llp) >> 32);
- t2 = (long) (*llp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *llp = ((quad_t) t1) << 32;
- *llp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_hyper)
-
-
-/*
- * XDR hyper integers
- * same as xdr_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (unsigned long) ((*ullp) >> 32);
- t2 = (unsigned long) (*ullp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *ullp = ((u_quad_t) t1) << 32;
- *ullp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_u_hyper)
-
-bool_t
-xdr_longlong_t (XDR *xdrs, quad_t *llp)
-{
- return INTUSE(xdr_hyper) (xdrs, llp);
-}
-
-bool_t
-xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
-{
- return INTUSE(xdr_u_hyper) (xdrs, ullp);
-}
-
-/*
- * XDR short integers
- */
-bool_t
-xdr_short (XDR *xdrs, short *sp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *sp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *sp = (short) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_short)
-
-/*
- * XDR unsigned short integers
- */
-bool_t
-xdr_u_short (XDR *xdrs, u_short *usp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * usp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *usp = (u_short) (u_long) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_short)
-
-
-/*
- * XDR a char
- */
-bool_t
-xdr_char (XDR *xdrs, char *cp)
-{
- int i;
-
- i = (*cp);
- if (!INTUSE(xdr_int) (xdrs, &i))
- {
- return FALSE;
- }
- *cp = i;
- return TRUE;
-}
-
-/*
- * XDR an unsigned char
- */
-bool_t
-xdr_u_char (XDR *xdrs, u_char *cp)
-{
- u_int u;
-
- u = (*cp);
- if (!INTUSE(xdr_u_int) (xdrs, &u))
- {
- return FALSE;
- }
- *cp = u;
- return TRUE;
-}
-
-/*
- * XDR booleans
- */
-bool_t
-xdr_bool (XDR *xdrs, bool_t *bp)
-{
- long lb;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- lb = *bp ? XDR_TRUE : XDR_FALSE;
- return XDR_PUTLONG (xdrs, &lb);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &lb))
- {
- return FALSE;
- }
- *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bool)
-
-/*
- * XDR enumerations
- */
-bool_t
-xdr_enum (XDR *xdrs, enum_t *ep)
-{
- enum sizecheck
- {
- SIZEVAL
- }; /* used to find the size of an enum */
-
- /*
- * enums are treated as ints
- */
- if (sizeof (enum sizecheck) == 4)
- {
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = *ep;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ep = l;
- case XDR_FREE:
- return TRUE;
-
- }
- return FALSE;
-#else
- return INTUSE(xdr_long) (xdrs, (long *) ep);
-#endif
- }
- else if (sizeof (enum sizecheck) == sizeof (short))
- {
- return INTUSE(xdr_short) (xdrs, (short *) ep);
- }
- else
- {
- return FALSE;
- }
-}
-INTDEF(xdr_enum)
-
-/*
- * XDR opaque data
- * Allows the specification of a fixed size sequence of opaque bytes.
- * cp points to the opaque object and cnt gives the byte length.
- */
-bool_t
-xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
-{
- u_int rndup;
- static char crud[BYTES_PER_XDR_UNIT];
-
- /*
- * if no data we are done
- */
- if (cnt == 0)
- return TRUE;
-
- /*
- * round byte count to full xdr units
- */
- rndup = cnt % BYTES_PER_XDR_UNIT;
- if (rndup > 0)
- rndup = BYTES_PER_XDR_UNIT - rndup;
-
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (!XDR_GETBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
-
- case XDR_ENCODE:
- if (!XDR_PUTBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_opaque)
-
-/*
- * XDR counted bytes
- * *cpp is a pointer to the bytes, *sizep is the count.
- * If *cpp is NULL maxsize bytes are allocated
- */
-bool_t
-xdr_bytes (xdrs, cpp, sizep, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int *sizep;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int nodesize;
-
- /*
- * first deal with the length since xdr bytes are counted
- */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- nodesize = *sizep;
- if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (nodesize == 0)
- {
- return TRUE;
- }
- if (sp == NULL)
- {
- *cpp = sp = (char *) mem_alloc (nodesize);
- }
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
- return FALSE;
- }
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
-
- case XDR_FREE:
- if (sp != NULL)
- {
- mem_free (sp, nodesize);
- *cpp = NULL;
- }
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bytes)
-
-/*
- * Implemented here due to commonality of the object.
- */
-bool_t
-xdr_netobj (xdrs, np)
- XDR *xdrs;
- struct netobj *np;
-{
-
- return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
-}
-INTDEF(xdr_netobj)
-
-/*
- * XDR a discriminated union
- * Support routine for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * an entry with a null procedure pointer. The routine gets
- * the discriminant value and then searches the array of xdrdiscrims
- * looking for that value. It calls the procedure given in the xdrdiscrim
- * to handle the discriminant. If there is no specific routine a default
- * routine may be called.
- * If there is no specific or default routine an error is returned.
- */
-bool_t
-xdr_union (xdrs, dscmp, unp, choices, dfault)
- XDR *xdrs;
- enum_t *dscmp; /* enum to decide which arm to work on */
- char *unp; /* the union itself */
- const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
- xdrproc_t dfault; /* default xdr routine */
-{
- enum_t dscm;
-
- /*
- * we deal with the discriminator; it's an enum
- */
- if (!INTUSE(xdr_enum) (xdrs, dscmp))
- {
- return FALSE;
- }
- dscm = *dscmp;
-
- /*
- * search choices for a value that matches the discriminator.
- * if we find one, execute the xdr routine for that value.
- */
- for (; choices->proc != NULL_xdrproc_t; choices++)
- {
- if (choices->value == dscm)
- return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
- }
-
- /*
- * no match - execute the default xdr routine if there is one
- */
- return ((dfault == NULL_xdrproc_t) ? FALSE :
- (*dfault) (xdrs, unp, LASTUNSIGNED));
-}
-INTDEF(xdr_union)
-
-
-/*
- * Non-portable xdr primitives.
- * Care should be taken when moving these routines to new architectures.
- */
-
-
-/*
- * XDR null terminated ASCII strings
- * xdr_string deals with "C strings" - arrays of bytes that are
- * terminated by a NULL character. The parameter cpp references a
- * pointer to storage; If the pointer is null, then the necessary
- * storage is allocated. The last parameter is the max allowed length
- * of the string as specified by a protocol.
- */
-bool_t
-xdr_string (xdrs, cpp, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int size;
- u_int nodesize;
-
- /*
- * first deal with the length since xdr strings are counted-strings
- */
- switch (xdrs->x_op)
- {
- case XDR_FREE:
- if (sp == NULL)
- {
- return TRUE; /* already free */
- }
- /* fall through... */
- case XDR_ENCODE:
- if (sp == NULL)
- return FALSE;
- size = strlen (sp);
- break;
- case XDR_DECODE:
- break;
- }
- if (!INTUSE(xdr_u_int) (xdrs, &size))
- {
- return FALSE;
- }
- if (size > maxsize)
- {
- return FALSE;
- }
- nodesize = size + 1;
- if (nodesize == 0)
- {
- /* This means an overflow. It a bug in the caller which
- provided a too large maxsize but nevertheless catch it
- here. */
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (sp == NULL)
- *cpp = sp = (char *) mem_alloc (nodesize);
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_string: out of memory\n");
- return FALSE;
- }
- sp[size] = 0;
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, size);
-
- case XDR_FREE:
- mem_free (sp, nodesize);
- *cpp = NULL;
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_string)
-
-/*
- * Wrapper for xdr_string that can be called directly from
- * routines like clnt_call
- */
-bool_t
-xdr_wrapstring (xdrs, cpp)
- XDR *xdrs;
- char **cpp;
-{
- if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
- {
- return TRUE;
- }
- return FALSE;
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr.h, External Data Representation Serialization Routines.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- */
-
-#ifndef _RPC_XDR_H
-#define _RPC_XDR_H 1
-
-#include <features.h>
-#include <sys/types.h>
-#include "types.h"
-
-/* We need FILE. */
-#include <stdio.h>
-
-__BEGIN_DECLS
-
-/*
- * XDR provides a conventional way for converting between C data
- * types and an external bit-string representation. Library supplied
- * routines provide for the conversion on built-in C data types. These
- * routines and utility routines defined here are used to help implement
- * a type encode/decode routine for each user-defined type.
- *
- * Each data type provides a single procedure which takes two arguments:
- *
- * bool_t
- * xdrproc(xdrs, argresp)
- * XDR *xdrs;
- * <type> *argresp;
- *
- * xdrs is an instance of a XDR handle, to which or from which the data
- * type is to be converted. argresp is a pointer to the structure to be
- * converted. The XDR handle contains an operation field which indicates
- * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
- *
- * XDR_DECODE may allocate space if the pointer argresp is null. This
- * data can be freed with the XDR_FREE operation.
- *
- * We write only one procedure per data type to make it easy
- * to keep the encode and decode procedures for a data type consistent.
- * In many cases the same code performs all operations on a user defined type,
- * because all the hard work is done in the component type routines.
- * decode as a series of calls on the nested data types.
- */
-
-/*
- * Xdr operations. XDR_ENCODE causes the type to be encoded into the
- * stream. XDR_DECODE causes the type to be extracted from the stream.
- * XDR_FREE can be used to release the space allocated by an XDR_DECODE
- * request.
- */
-enum xdr_op {
- XDR_ENCODE = 0,
- XDR_DECODE = 1,
- XDR_FREE = 2
-};
-
-/*
- * This is the number of bytes per unit of external data.
- */
-#define BYTES_PER_XDR_UNIT (4)
-/*
- * This only works if the above is a power of 2. But it's defined to be
- * 4 by the appropriate RFCs. So it will work. And it's normally quicker
- * than the old routine.
- */
-#if 1
-#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
-#else /* this is the old routine */
-#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
- * BYTES_PER_XDR_UNIT)
-#endif
-
-/*
- * The XDR handle.
- * Contains operation which is being applied to the stream,
- * an operations vector for the particular implementation (e.g. see xdr_mem.c),
- * and two private fields for the use of the particular implementation.
- */
-typedef struct XDR XDR;
-struct XDR
- {
- enum xdr_op x_op; /* operation; fast additional param */
- struct xdr_ops
- {
- bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
- /* get a long from underlying stream */
- bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
- /* put a long to " */
- bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
- /* get some bytes from " */
- bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
- /* put some bytes to " */
- u_int (*x_getpostn) (__const XDR *__xdrs);
- /* returns bytes off from beginning */
- bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
- /* lets you reposition the stream */
- int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
- /* buf quick ptr to buffered data */
- void (*x_destroy) (XDR *__xdrs);
- /* free privates of this xdr_stream */
- bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
- /* get a int from underlying stream */
- bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
- /* put a int to " */
- }
- *x_ops;
- caddr_t x_public; /* users' data */
- caddr_t x_private; /* pointer to private data */
- caddr_t x_base; /* private used for position info */
- u_int x_handy; /* extra private word */
- };
-
-/*
- * A xdrproc_t exists for each data type which is to be encoded or decoded.
- *
- * The second argument to the xdrproc_t is a pointer to an opaque pointer.
- * The opaque pointer generally points to a structure of the data type
- * to be decoded. If this pointer is 0, then the type routines should
- * allocate dynamic storage of the appropriate size and return it.
- * bool_t (*xdrproc_t)(XDR *, caddr_t *);
- */
-typedef bool_t (*xdrproc_t) (XDR *, void *,...);
-
-
-/*
- * Operations defined on a XDR handle
- *
- * XDR *xdrs;
- * int32_t *int32p;
- * long *longp;
- * caddr_t addr;
- * u_int len;
- * u_int pos;
- */
-#define XDR_GETINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-#define xdr_getint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-
-#define XDR_PUTINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-#define xdr_putint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-
-#define XDR_GETLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-#define xdr_getlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-
-#define XDR_PUTLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-#define xdr_putlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-
-#define XDR_GETBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-#define xdr_getbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-
-#define XDR_PUTBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-#define xdr_putbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-
-#define XDR_GETPOS(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-#define xdr_getpos(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-
-#define XDR_SETPOS(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-#define xdr_setpos(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-
-#define XDR_INLINE(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-#define xdr_inline(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-
-#define XDR_DESTROY(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-#define xdr_destroy(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-
-/*
- * Support struct for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * a entry with a null procedure pointer. The xdr_union routine gets
- * the discriminant value and then searches the array of structures
- * for a matching value. If a match is found the associated xdr routine
- * is called to handle that part of the union. If there is
- * no match, then a default routine may be called.
- * If there is no match and no default routine it is an error.
- */
-#define NULL_xdrproc_t ((xdrproc_t)0)
-struct xdr_discrim
-{
- int value;
- xdrproc_t proc;
-};
-
-/*
- * Inline routines for fast encode/decode of primitive data types.
- * Caveat emptor: these use single memory cycles to get the
- * data from the underlying buffer, and will fail to operate
- * properly if the data is not aligned. The standard way to use these
- * is to say:
- * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
- * return (FALSE);
- * <<< macro calls >>>
- * where ``count'' is the number of bytes of data occupied
- * by the primitive data types.
- *
- * N.B. and frozen for all time: each data type here uses 4 bytes
- * of external representation.
- */
-
-#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
-#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
-#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
-#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
-
-/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
- * and shouldn't be used any longer. Code which use this defines or longs
- * in the RPC code will not work on 64bit Solaris platforms !
- */
-#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
-#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
-#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
-#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-
-#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
-#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
-#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
-#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
-
-#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-/*
- * These are the "generic" xdr routines.
- * None of these can have const applied because it's not possible to
- * know whether the call is a read or a write to the passed parameter
- * also, the XDR structure is always updated by some of these calls.
- */
-extern bool_t xdr_void (void) __THROW;
-extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
-extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
-extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
-extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
-extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
-extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
-extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
-extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
-extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
-extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
-extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
-extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
-extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
-extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
-extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
-extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
-extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
-extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
-extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
- u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
- __THROW;
-extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
- u_int __maxsize) __THROW;
-extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
-extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
-extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
- __const struct xdr_discrim *__choices,
- xdrproc_t dfault) __THROW;
-extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
-extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
-extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
- u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
-extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
-extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
-extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
- xdrproc_t __proc) __THROW;
-extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
- u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
-extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
-extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
-
-/*
- * Common opaque bytes objects used by many rpc protocols;
- * declared here due to commonality.
- */
-#define MAX_NETOBJ_SZ 1024
-struct netobj
-{
- u_int n_len;
- char *n_bytes;
-};
-typedef struct netobj netobj;
-extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
-
-/*
- * These are the public routines for the various implementations of
- * xdr streams.
- */
-
-/* XDR using memory buffers */
-extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
- u_int __size, enum xdr_op __xop) __THROW;
-
-/* XDR using stdio library */
-extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
- __THROW;
-
-/* XDR pseudo records for tcp */
-extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
- u_int __recvsize, caddr_t __tcp_handle,
- int (*__readit) (char *, char *, int),
- int (*__writeit) (char *, char *, int)) __THROW;
-
-/* make end of xdr record */
-extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
-
-/* move to beginning of next record */
-extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
-
-/* true if no more input */
-extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
-
-/* free memory buffers for xdr */
-extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
-
-__END_DECLS
-
-#endif /* rpc/xdr.h */
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_array.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "non-trivial" xdr primitives used to serialize and de-serialize
- * arrays. See xdr.h for more info on the interface to xdr.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include "types.h"
-#include "xdr.h"
-#include <libintl.h>
-#include <limits.h>
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-#define LASTUNSIGNED ((u_int)0-1)
-
-
-/*
- * XDR an array of arbitrary elements
- * *addrp is a pointer to the array, *sizep is the number of elements.
- * If addrp is NULL (*sizep * elsize) bytes are allocated.
- * elsize is the size (in bytes) of each element, and elproc is the
- * xdr procedure to call to handle each element of the array.
- */
-bool_t
-xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
- XDR *xdrs;
- caddr_t *addrp; /* array pointer */
- u_int *sizep; /* number of elements */
- u_int maxsize; /* max numberof elements */
- u_int elsize; /* size in bytes of each element */
- xdrproc_t elproc; /* xdr routine to handle each element */
-{
- u_int i;
- caddr_t target = *addrp;
- u_int c; /* the actual element count */
- bool_t stat = TRUE;
- u_int nodesize;
-
- /* like strings, arrays are really counted arrays */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- c = *sizep;
- /*
- * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
- * doesn't actually use its second argument anyway.
- */
- if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
- nodesize = c * elsize;
-
- /*
- * if we are deserializing, we may need to allocate an array.
- * We also save time by checking for a null array if we are freeing.
- */
- if (target == NULL)
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (c == 0)
- return TRUE;
- *addrp = target = mem_alloc (nodesize);
- if (target == NULL)
- {
- fprintf (stderr, "%s", "xdr_array: out of memory\n");
- return FALSE;
- }
- __bzero (target, nodesize);
- break;
-
- case XDR_FREE:
- return TRUE;
- default:
- break;
- }
-
- /*
- * now we xdr each element of array
- */
- for (i = 0; (i < c) && stat; i++)
- {
- stat = (*elproc) (xdrs, target, LASTUNSIGNED);
- target += elsize;
- }
-
- /*
- * the array may need freeing
- */
- if (xdrs->x_op == XDR_FREE)
- {
- mem_free (*addrp, nodesize);
- *addrp = NULL;
- }
- return stat;
-}
-INTDEF(xdr_array)
-
-/*
- * xdr_vector():
- *
- * XDR a fixed length array. Unlike variable-length arrays,
- * the storage of fixed length arrays is static and unfreeable.
- * > basep: base of the array
- * > size: size of the array
- * > elemsize: size of each element
- * > xdr_elem: routine to XDR each element
- */
-bool_t
-xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
- XDR *xdrs;
- char *basep;
- u_int nelem;
- u_int elemsize;
- xdrproc_t xdr_elem;
-{
- u_int i;
- char *elptr;
-
- elptr = basep;
- for (i = 0; i < nelem; i++)
- {
- if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
- {
- return FALSE;
- }
- elptr += elemsize;
- }
- return TRUE;
-}
+++ /dev/null
-/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_float.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "floating point" xdr routines used to (de)serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <endian.h>
-
-#include "types.h"
-#include "xdr.h"
-
-/*
- * NB: Not portable.
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
-
-#ifdef vax
-
-/* What IEEE single precision floating point looks like on a Vax */
-struct ieee_single {
- unsigned int mantissa: 23;
- unsigned int exp : 8;
- unsigned int sign : 1;
-};
-
-/* Vax single precision floating point */
-struct vax_single {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
-};
-
-#define VAX_SNG_BIAS 0x81
-#define IEEE_SNG_BIAS 0x7f
-
-static struct sgl_limits {
- struct vax_single s;
- struct ieee_single ieee;
-} sgl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
- { 0x0, 0xff, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
- { 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-#endif /* vax */
-
-bool_t
-xdr_float(xdrs, fp)
- XDR *xdrs;
- float *fp;
-{
-#ifdef vax
- struct ieee_single is;
- struct vax_single vs, *vsp;
- struct sgl_limits *lim;
- int i;
-#endif
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vs = *((struct vax_single *)fp);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((vs.mantissa2 == lim->s.mantissa2) &&
- (vs.exp == lim->s.exp) &&
- (vs.mantissa1 == lim->s.mantissa1)) {
- is = lim->ieee;
- goto shipit;
- }
- }
- is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
- is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
- shipit:
- is.sign = vs.sign;
- return (XDR_PUTLONG(xdrs, (long *)&is));
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_PUTLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp = *(int *)fp;
- return (XDR_PUTLONG(xdrs, &tmp));
- }
- break;
-#endif
-
- case XDR_DECODE:
-#ifdef vax
- vsp = (struct vax_single *)fp;
- if (!XDR_GETLONG(xdrs, (long *)&is))
- return (FALSE);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((is.exp == lim->ieee.exp) &&
- (is.mantissa == lim->ieee.mantissa)) {
- *vsp = lim->s;
- goto doneit;
- }
- }
- vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
- vsp->mantissa2 = is.mantissa;
- vsp->mantissa1 = (is.mantissa >> 16);
- doneit:
- vsp->sign = is.sign;
- return (TRUE);
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_GETLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp;
- if (XDR_GETLONG(xdrs, &tmp)) {
- *(int *)fp = tmp;
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
-
-/*
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#ifdef vax
-/* What IEEE double precision floating point looks like on a Vax */
-struct ieee_double {
- unsigned int mantissa1 : 20;
- unsigned int exp : 11;
- unsigned int sign : 1;
- unsigned int mantissa2 : 32;
-};
-
-/* Vax double precision floating point */
-struct vax_double {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
- unsigned int mantissa3 : 16;
- unsigned int mantissa4 : 16;
-};
-
-#define VAX_DBL_BIAS 0x81
-#define IEEE_DBL_BIAS 0x3ff
-#define MASK(nbits) ((1 << nbits) - 1)
-
-static struct dbl_limits {
- struct vax_double d;
- struct ieee_double ieee;
-} dbl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
- { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
- { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-
-#endif /* vax */
-
-
-bool_t
-xdr_double(xdrs, dp)
- XDR *xdrs;
- double *dp;
-{
-#ifdef vax
- struct ieee_double id;
- struct vax_double vd;
- register struct dbl_limits *lim;
- int i;
-#endif
-
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vd = *((struct vax_double *)dp);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((vd.mantissa4 == lim->d.mantissa4) &&
- (vd.mantissa3 == lim->d.mantissa3) &&
- (vd.mantissa2 == lim->d.mantissa2) &&
- (vd.mantissa1 == lim->d.mantissa1) &&
- (vd.exp == lim->d.exp)) {
- id = lim->ieee;
- goto shipit;
- }
- }
- id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
- id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
- id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
- (vd.mantissa3 << 13) |
- ((vd.mantissa4 >> 3) & MASK(13));
- shipit:
- id.sign = vd.sign;
- dp = (double *)&id;
-#endif
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_PUTLONG(xdrs, lp+!LSW) &&
- XDR_PUTLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- tmp[0] = ip[!LSW];
- tmp[1] = ip[LSW];
- return (XDR_PUTLONG(xdrs, tmp) &&
- XDR_PUTLONG(xdrs, tmp+1));
- }
- break;
-
- case XDR_DECODE:
-#ifdef vax
- lp = (long *)&id;
- if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
- return (FALSE);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((id.mantissa2 == lim->ieee.mantissa2) &&
- (id.mantissa1 == lim->ieee.mantissa1) &&
- (id.exp == lim->ieee.exp)) {
- vd = lim->d;
- goto doneit;
- }
- }
- vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
- vd.mantissa1 = (id.mantissa1 >> 13);
- vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
- (id.mantissa2 >> 29);
- vd.mantissa3 = (id.mantissa2 >> 13);
- vd.mantissa4 = (id.mantissa2 << 3);
- doneit:
- vd.sign = id.sign;
- *dp = *((double *)&vd);
- return (TRUE);
-#else
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_GETLONG(xdrs, lp+!LSW) &&
- XDR_GETLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- if (XDR_GETLONG(xdrs, tmp+!LSW) &&
- XDR_GETLONG(xdrs, tmp+LSW)) {
- ip[0] = tmp[0];
- ip[1] = tmp[1];
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr_stdio.c, XDR implementation on standard i/o file.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * This set of routines implements a XDR on a stdio stream.
- * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
- * from the stream.
- */
-
-#include "types.h"
-#include <stdio.h>
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <libio/iolibio.h>
-# define fflush(s) INTUSE(_IO_fflush) (s)
-# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
-# define ftell(s) INTUSE(_IO_ftell) (s)
-# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
-#endif
-
-static bool_t xdrstdio_getlong (XDR *, long *);
-static bool_t xdrstdio_putlong (XDR *, const long *);
-static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
-static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
-static u_int xdrstdio_getpos (const XDR *);
-static bool_t xdrstdio_setpos (XDR *, u_int);
-static int32_t *xdrstdio_inline (XDR *, u_int);
-static void xdrstdio_destroy (XDR *);
-static bool_t xdrstdio_getint32 (XDR *, int32_t *);
-static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
-
-/*
- * Ops vector for stdio type XDR
- */
-static const struct xdr_ops xdrstdio_ops =
-{
- xdrstdio_getlong, /* deserialize a long int */
- xdrstdio_putlong, /* serialize a long int */
- xdrstdio_getbytes, /* deserialize counted bytes */
- xdrstdio_putbytes, /* serialize counted bytes */
- xdrstdio_getpos, /* get offset in the stream */
- xdrstdio_setpos, /* set offset in the stream */
- xdrstdio_inline, /* prime stream for inline macros */
- xdrstdio_destroy, /* destroy stream */
- xdrstdio_getint32, /* deserialize a int */
- xdrstdio_putint32 /* serialize a int */
-};
-
-/*
- * Initialize a stdio xdr stream.
- * Sets the xdr stream handle xdrs for use on the stream file.
- * Operation flag is set to op.
- */
-void
-xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
-{
- xdrs->x_op = op;
- /* We have to add the const since the `struct xdr_ops' in `struct XDR'
- is not `const'. */
- xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
- xdrs->x_private = (caddr_t) file;
- xdrs->x_handy = 0;
- xdrs->x_base = 0;
-}
-
-/*
- * Destroy a stdio xdr stream.
- * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
- */
-static void
-xdrstdio_destroy (XDR *xdrs)
-{
- (void) fflush ((FILE *) xdrs->x_private);
- /* xx should we close the file ?? */
-};
-
-static bool_t
-xdrstdio_getlong (XDR *xdrs, long *lp)
-{
- u_int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *lp = (long) ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putlong (XDR *xdrs, const long *lp)
-{
- int32_t mycopy = htonl ((u_int32_t) *lp);
-
- if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
-{
- if ((len != 0) && (fread (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
-{
- if ((len != 0) && (fwrite (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static u_int
-xdrstdio_getpos (const XDR *xdrs)
-{
- return (u_int) ftell ((FILE *) xdrs->x_private);
-}
-
-static bool_t
-xdrstdio_setpos (XDR *xdrs, u_int pos)
-{
- return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
-}
-
-static int32_t *
-xdrstdio_inline (XDR *xdrs, u_int len)
-{
- /*
- * Must do some work to implement this: must insure
- * enough data in the underlying stdio buffer,
- * that the buffer is aligned so that we can indirect through a
- * long *, and stuff this pointer in xdrs->x_buf. Doing
- * a fread or fwrite to a scratch buffer would defeat
- * most of the gains to be had here and require storage
- * management on this buffer, so we don't do this.
- */
- return NULL;
-}
-
-static bool_t
-xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
-{
- int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *ip = ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
-{
- int32_t mycopy = htonl (*ip);
-
- ip = &mycopy;
- if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-/* libc_hidden_def (xdrstdio_create) */
+++ /dev/null
-/*_________________________________________________________________
- |
- | xdrf.h - include file for C routines that want to use the
- | functions below.
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type);
-int xdrclose(XDR *xdrs) ;
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
-
enable_language (Fortran)
-#================================
-# build the xdrf library
-#================================
-add_subdirectory(xdrf)
#================================
# Set source file lists
#================================
-set(UNRES_MD_SRC0 unres.F arcos.f cartprint.f chainbuild.F convert.f
- initialize_p.F matmult.f readrtns.F parmread.F gen_rand_conf.F
- printmat.f map.f pinorm.f randgens.f rescode.f intcor.f timing.F
- misc.f intlocal.f cartder.F checkder_p.F econstr_local.F
- minimize_p.F sumsld.f cored.f rmdd.f geomout.F readpdb.F regularize.F
- thread.F fitsq.f mcm.F mc.F bond_move.f refsys.f check_sc_distr.f
- check_bond.f contact.f djacob.f eigen.f blas.f add.f entmcm.F
- minim_mcmf.F MP.F compare_s1.F prng_32.F banach.f rmsd.F elecont.f dihed_cons.F sc_move.F
- local_move.f intcartderiv.F lagrangian_lesyng.F stochfric.F kinetic_lesyng.f MD_A-MTS.F moments.f
- int_to_cart.f surfatom.f sort.f muca_md.f MREMD.F rattle.F gauss.f energy_split-sep.F
- q_measure.F gnmr1.f test.F )
-
-set(UNRES_MD_SRC3 energy_p_new_barrier.F energy_p_new-sep_barrier.F gradient_p.F )
-
-set(UNRES_MD_PP_SRC cartder.F chainbuild.F checkder_p.F compare_s1.F dihed_cons.F econstr_local.F
- energy_p_new_barrier.F energy_p_new-sep_barrier.F energy_split-sep.F entmcm.F gen_rand_conf.F
- geomout.F gradient_p.F initialize_p.F intcartderiv.F lagrangian_lesyng.F mc.F mcm.F MD_A-MTS.F
- minimize_p.F minim_mcmf.F MP.F MREMD.F parmread.F prng_32.F q_measure1.F q_measure3.F q_measure.F
- rattle.F readpdb.F readrtns.F regularize.F rmsd.F sc_move.F stochfric.F test.F thread.F timing.F
- unres.F proc_proc.c )
+set(UNRES_MD_SRC0
+ add.f
+ arcos.f
+ banach.f
+ blas.f
+ bond_move.f
+ cartder.F
+ cartprint.f
+ check_sc_distr.f
+ check_bond.f
+ chainbuild.F
+ checkder_p.F
+ compare_s1.F
+ contact.f
+ convert.f
+ cored.f
+ dihed_cons.F
+ djacob.f
+ econstr_local.F
+ eigen.f
+ elecont.f
+ energy_split-sep.F
+ entmcm.F
+ fitsq.f
+ gauss.f
+ gen_rand_conf.F
+ geomout.F
+ gnmr1.f
+ intcartderiv.F
+ initialize_p.F
+ int_to_cart.f
+ intcor.f
+ intlocal.f
+ kinetic_lesyng.f
+ lagrangian_lesyng.F
+ local_move.f
+ map.f
+ matmult.f
+ mc.F
+ mcm.F
+ MD_A-MTS.F
+ minimize_p.F
+ minim_mcmf.F
+ misc.f
+ moments.f
+ MP.F
+ MREMD.F
+ muca_md.f
+ parmread.F
+ pinorm.f
+ printmat.f
+ prng_32.F
+ q_measure.F
+ randgens.f
+ rattle.F
+ readpdb.F
+ readrtns.F
+ refsys.f
+ regularize.F
+ rescode.f
+ rmdd.f
+ rmsd.F
+ sc_move.F
+ sort.f
+ stochfric.F
+ sumsld.f
+ surfatom.f
+ test.F
+ timing.F
+ thread.F
+ unres.F
+)
+
+set(UNRES_MD_SRC3
+ energy_p_new_barrier.F
+ energy_p_new-sep_barrier.F
+ gradient_p.F )
+
+set(UNRES_MD_PP_SRC
+ cartder.F
+ chainbuild.F
+ checkder_p.F
+ compare_s1.F
+ dihed_cons.F
+ econstr_local.F
+ energy_p_new_barrier.F
+ energy_p_new-sep_barrier.F
+ energy_split-sep.F
+ entmcm.F
+ gen_rand_conf.F
+ geomout.F
+ gradient_p.F
+ initialize_p.F
+ intcartderiv.F
+ lagrangian_lesyng.F
+ mc.F
+ mcm.F
+ MD_A-MTS.F
+ minimize_p.F
+ minim_mcmf.F
+ MP.F
+ MREMD.F
+ parmread.F
+ prng_32.F
+ q_measure1.F
+ q_measure3.F
+ q_measure.F
+ rattle.F
+ readpdb.F
+ readrtns.F
+ regularize.F
+ rmsd.F
+ sc_move.F
+ stochfric.F
+ test.F
+ thread.F
+ timing.F
+ unres.F
+ proc_proc.c
+)
#================================================
+++ /dev/null
-#
-# CMake project file for UNRESPACK
-#
-
-# m4 macro processor
-add_custom_command(
- OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c
- COMMAND m4
- ARGS ${CMAKE_CURRENT_SOURCE_DIR}/underscore.m4 ${CMAKE_CURRENT_SOURCE_DIR}/libxdrf.m4 > ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c
- VERBATIM
-)
-
-# add headers from current dir
-include_directories(${CMAKE_CURRENT_SOURCE_DIR})
-# compile the libxdrf library
-add_library(xdrf STATIC ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.c ftocstr.c)
-set(UNRES_XDRFLIB ${CMAKE_CURRENT_BINARY_DIR}/libxdrf.a PARENT_SCOPE)
-
-#add_dependencies( ${UNRES_BIN} xdrf )
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = gcc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-BGLSYS = /bgl/BlueLight/ppcfloor/bglsys
-
-CC = /usr/bin/blrts_xlc
-CPPC = /usr/bin/blrts_xlc
-
-CFLAGS= -O2 -I$(BGLSYS)/include -L$(BGLSYS)/lib -qarch=440d -qtune=440
-
-M4 = m4
-M4FILE = RS6K.m4
-
-libxdrf.a: libxdrf.o ftocstr.o xdr_array.o xdr.o xdr_float.o xdr_stdio.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f *.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
-# rm -f libxdrf.c
-
+++ /dev/null
-# This make file is part of the xdrf package.
-#
-# (C) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-#
-# 2006 modified by Cezary Czaplewski
-
-# Set C compiler and flags for ARCH
-CC = cc
-CFLAGS = -O
-
-M4 = m4
-M4FILE = underscore.m4
-
-libxdrf.a: libxdrf.o ftocstr.o
- ar cr libxdrf.a $?
-
-clean:
- rm -f libxdrf.o ftocstr.o libxdrf.a
-
-ftocstr.o: ftocstr.c
- $(CC) $(CFLAGS) -c ftocstr.c
-
-libxdrf.o: libxdrf.m4 $(M4FILE)
- $(M4) $(M4FILE) libxdrf.m4 > libxdrf.c
- $(CC) $(CFLAGS) -c libxdrf.c
- rm -f libxdrf.c
-
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# do nothing special to FORTRAN function names
-#
-define(`FUNCTION',`$1')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
-
+++ /dev/null
-
-
-int ftocstr(ds, dl, ss, sl)
- char *ds, *ss; /* dst, src ptrs */
- int dl; /* dst max len */
- int sl; /* src len */
-{
- char *p;
-
- for (p = ss + sl; --p >= ss && *p == ' '; ) ;
- sl = p - ss + 1;
- dl--;
- ds[0] = 0;
- if (sl > dl)
- return 1;
- while (sl--)
- (*ds++ = *ss++);
- *ds = '\0';
- return 0;
-}
-
-
-int ctofstr(ds, dl, ss)
- char *ds; /* dest space */
- int dl; /* max dest length */
- char *ss; /* src string (0-term) */
-{
- while (dl && *ss) {
- *ds++ = *ss++;
- dl--;
- }
- while (dl--)
- *ds++ = ' ';
- return 0;
-}
+++ /dev/null
-/*____________________________________________________________________________
- |
- | libxdrf - portable fortran interface to xdr. some xdr routines
- | are C routines for compressed coordinates
- |
- | version 1.1
- |
- | This collection of routines is intended to write and read
- | data in a portable way to a file, so data written on one type
- | of machine can be read back on a different type.
- |
- | all fortran routines use an integer 'xdrid', which is an id to the
- | current xdr file, and is set by xdrfopen.
- | most routines have in integer 'ret' which is the return value.
- | The value of 'ret' is zero on failure, and most of the time one
- | on succes.
- |
- | There are three routines useful for C users:
- | xdropen(), xdrclose(), xdr3dfcoord().
- | The first two replace xdrstdio_create and xdr_destroy, and *must* be
- | used when you plan to use xdr3dfcoord(). (they are also a bit
- | easier to interface). For writing data other than compressed coordinates
- | you should use the standard C xdr routines (see xdr man page)
- |
- | xdrfopen(xdrid, filename, mode, ret)
- | character *(*) filename
- | character *(*) mode
- |
- | this will open the file with the given filename (string)
- | and the given mode, it returns an id in xdrid, which is
- | to be used in all other calls to xdrf routines.
- | mode is 'w' to create, or update an file, for all other
- | values of mode the file is opened for reading
- |
- | you need to call xdrfclose to flush the output and close
- | the file.
- | Note that you should not use xdrstdio_create, which comes with the
- | standard xdr library
- |
- | xdrfclose(xdrid, ret)
- | flush the data to the file, and closes the file;
- | You should not use xdr_destroy (which comes standard with
- | the xdr libraries.
- |
- | xdrfbool(xdrid, bp, ret)
- | integer pb
- |
- | This filter produces values of either 1 or 0
- |
- | xdrfchar(xdrid, cp, ret)
- | character cp
- |
- | filter that translate between characters and their xdr representation
- | Note that the characters in not compressed and occupies 4 bytes.
- |
- | xdrfdouble(xdrid, dp, ret)
- | double dp
- |
- | read/write a double.
- |
- | xdrffloat(xdrid, fp, ret)
- | float fp
- |
- | read/write a float.
- |
- | xdrfint(xdrid, ip, ret)
- | integer ip
- |
- | read/write integer.
- |
- | xdrflong(xdrid, lp, ret)
- | integer lp
- |
- | this routine has a possible portablility problem due to 64 bits longs.
- |
- | xdrfshort(xdrid, sp, ret)
- | integer *2 sp
- |
- | xdrfstring(xdrid, sp, maxsize, ret)
- | character *(*)
- | integer maxsize
- |
- | read/write a string, with maximum length given by maxsize
- |
- | xdrfwrapstring(xdris, sp, ret)
- | character *(*)
- |
- | read/write a string (it is the same as xdrfstring accept that it finds
- | the stringlength itself.
- |
- | xdrfvector(xdrid, cp, size, xdrfproc, ret)
- | character *(*)
- | integer size
- | external xdrfproc
- |
- | read/write an array pointed to by cp, with number of elements
- | defined by 'size'. the routine 'xdrfproc' is the name
- | of one of the above routines to read/write data (like xdrfdouble)
- | In contrast with the c-version you don't need to specify the
- | byte size of an element.
- | xdrfstring is not allowed here (it is in the c version)
- |
- | xdrf3dfcoord(xdrid, fp, size, precision, ret)
- | real (*) fp
- | real precision
- | integer size
- |
- | this is *NOT* a standard xdr routine. I named it this way, because
- | it invites people to use the other xdr routines.
- | It is introduced to store specifically 3d coordinates of molecules
- | (as found in molecular dynamics) and it writes it in a compressed way.
- | It starts by multiplying all numbers by precision and
- | rounding the result to integer. effectively converting
- | all floating point numbers to fixed point.
- | it uses an algorithm for compression that is optimized for
- | molecular data, but could be used for other 3d coordinates
- | as well. There is subtantial overhead involved, so call this
- | routine only if you have a large number of coordinates to read/write
- |
- | ________________________________________________________________________
- |
- | Below are the routines to be used by C programmers. Use the 'normal'
- | xdr routines to write integers, floats, etc (see man xdr)
- |
- | int xdropen(XDR *xdrs, const char *filename, const char *type)
- | This will open the file with the given filename and the
- | given mode. You should pass it an allocated XDR struct
- | in xdrs, to be used in all other calls to xdr routines.
- | Mode is 'w' to create, or update an file, and for all
- | other values of mode the file is opened for reading.
- | You need to call xdrclose to flush the output and close
- | the file.
- |
- | Note that you should not use xdrstdio_create, which
- | comes with the standard xdr library.
- |
- | int xdrclose(XDR *xdrs)
- | Flush the data to the file, and close the file;
- | You should not use xdr_destroy (which comes standard
- | with the xdr libraries).
- |
- | int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision)
- | This is \fInot\fR a standard xdr routine. I named it this
- | way, because it invites people to use the other xdr
- | routines.
- |
- | (c) 1995 Frans van Hoesel, hoesel@chem.rug.nl
-*/
-
-
-#include <limits.h>
-#include <malloc.h>
-#include <math.h>
-/* #include <rpc/rpc.h>
-#include <rpc/xdr.h> */
-#include "xdr.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include "xdrf.h"
-
-int ftocstr(char *, int, char *, int);
-int ctofstr(char *, int, char *);
-
-#define MAXID 20
-static FILE *xdrfiles[MAXID];
-static XDR *xdridptr[MAXID];
-static char xdrmodes[MAXID];
-static unsigned int cnt;
-
-typedef void (* FUNCTION(xdrfproc)) (int *, void *, int *);
-
-void
-FUNCTION(xdrfbool) ARGS(`xdrid, pb, ret')
-int *xdrid, *ret;
-int *pb;
-{
- *ret = xdr_bool(xdridptr[*xdrid], (bool_t *) pb);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrfchar) ARGS(`xdrid, cp, ret')
-int *xdrid, *ret;
-char *cp;
-{
- *ret = xdr_char(xdridptr[*xdrid], cp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfdouble) ARGS(`xdrid, dp, ret')
-int *xdrid, *ret;
-double *dp;
-{
- *ret = xdr_double(xdridptr[*xdrid], dp);
- cnt += sizeof(double);
-}
-
-void
-FUNCTION(xdrffloat) ARGS(`xdrid, fp, ret')
-int *xdrid, *ret;
-float *fp;
-{
- *ret = xdr_float(xdridptr[*xdrid], fp);
- cnt += sizeof(float);
-}
-
-void
-FUNCTION(xdrfint) ARGS(`xdrid, ip, ret')
-int *xdrid, *ret;
-int *ip;
-{
- *ret = xdr_int(xdridptr[*xdrid], ip);
- cnt += sizeof(int);
-}
-
-void
-FUNCTION(xdrflong) ARGS(`xdrid, lp, ret')
-int *xdrid, *ret;
-long *lp;
-{
- *ret = xdr_long(xdridptr[*xdrid], lp);
- cnt += sizeof(long);
-}
-
-void
-FUNCTION(xdrfshort) ARGS(`xdrid, sp, ret')
-int *xdrid, *ret;
-short *sp;
-{
- *ret = xdr_short(xdridptr[*xdrid], sp);
- cnt += sizeof(sp);
-}
-
-void
-FUNCTION(xdrfuchar) ARGS(`xdrid, ucp, ret')
-int *xdrid, *ret;
-char *ucp;
-{
- *ret = xdr_u_char(xdridptr[*xdrid], ucp);
- cnt += sizeof(char);
-}
-
-void
-FUNCTION(xdrfulong) ARGS(`xdrid, ulp, ret')
-int *xdrid, *ret;
-unsigned long *ulp;
-{
- *ret = xdr_u_long(xdridptr[*xdrid], ulp);
- cnt += sizeof(unsigned long);
-}
-
-void
-FUNCTION(xdrfushort) ARGS(`xdrid, usp, ret')
-int *xdrid, *ret;
-unsigned short *usp;
-{
- *ret = xdr_u_short(xdridptr[*xdrid], usp);
- cnt += sizeof(unsigned short);
-}
-
-void
-FUNCTION(xdrf3dfcoord) ARGS(`xdrid, fp, size, precision, ret')
-int *xdrid, *ret;
-float *fp;
-int *size;
-float *precision;
-{
- *ret = xdr3dfcoord(xdridptr[*xdrid], fp, size, precision);
-}
-
-void
-FUNCTION(xdrfstring) ARGS(`xdrid, STRING_ARG(sp), maxsize, ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-int *maxsize;
-{
- char *tsp;
-
- tsp = (char*) malloc(((STRING_LEN(sp)) + 1) * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, *maxsize+1, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int) *maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += *maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfwrapstring) ARGS(`xdrid, STRING_ARG(sp), ret')
-int *xdrid, *ret;
-STRING_ARG_DECL(sp);
-{
- char *tsp;
- int maxsize;
- maxsize = (STRING_LEN(sp)) + 1;
- tsp = (char*) malloc(maxsize * sizeof(char));
- if (tsp == NULL) {
- *ret = -1;
- return;
- }
- if (ftocstr(tsp, maxsize, STRING_PTR(sp), STRING_LEN(sp))) {
- *ret = -1;
- free(tsp);
- return;
- }
- *ret = xdr_string(xdridptr[*xdrid], (char **) &tsp, (u_int)maxsize);
- ctofstr( STRING_PTR(sp), STRING_LEN(sp), tsp);
- cnt += maxsize;
- free(tsp);
-}
-
-void
-FUNCTION(xdrfopaque) ARGS(`xdrid, cp, ccnt, ret')
-int *xdrid, *ret;
-caddr_t *cp;
-int *ccnt;
-{
- *ret = xdr_opaque(xdridptr[*xdrid], (caddr_t)*cp, (u_int)*ccnt);
- cnt += *ccnt;
-}
-
-void
-FUNCTION(xdrfsetpos) ARGS(`xdrid, pos, ret')
-int *xdrid, *ret;
-int *pos;
-{
- *ret = xdr_setpos(xdridptr[*xdrid], (u_int) *pos);
-}
-
-void
-FUNCTION(xdrf) ARGS(`xdrid, pos')
-int *xdrid, *pos;
-{
- *pos = xdr_getpos(xdridptr[*xdrid]);
-}
-
-void
-FUNCTION(xdrfvector) ARGS(`xdrid, cp, size, elproc, ret')
-int *xdrid, *ret;
-char *cp;
-int *size;
-FUNCTION(xdrfproc) elproc;
-{
- int lcnt;
- cnt = 0;
- for (lcnt = 0; lcnt < *size; lcnt++) {
- elproc(xdrid, (cp+cnt) , ret);
- }
-}
-
-
-void
-FUNCTION(xdrfclose) ARGS(`xdrid, ret')
-int *xdrid;
-int *ret;
-{
- *ret = xdrclose(xdridptr[*xdrid]);
- cnt = 0;
-}
-
-void
-FUNCTION(xdrfopen) ARGS(`xdrid, STRING_ARG(fp), STRING_ARG(mode), ret')
-int *xdrid;
-STRING_ARG_DECL(fp);
-STRING_ARG_DECL(mode);
-int *ret;
-{
- char fname[512];
- char fmode[3];
-
- if (ftocstr(fname, sizeof(fname), STRING_PTR(fp), STRING_LEN(fp))) {
- *ret = 0;
- }
- if (ftocstr(fmode, sizeof(fmode), STRING_PTR(mode),
- STRING_LEN(mode))) {
- *ret = 0;
- }
-
- *xdrid = xdropen(NULL, fname, fmode);
- if (*xdrid == 0)
- *ret = 0;
- else
- *ret = 1;
-}
-
-/*___________________________________________________________________________
- |
- | what follows are the C routines for opening, closing xdr streams
- | and the routine to read/write compressed coordinates together
- | with some routines to assist in this task (those are marked
- | static and cannot be called from user programs)
-*/
-#define MAXABS INT_MAX-2
-
-#ifndef MIN
-#define MIN(x,y) ((x) < (y) ? (x):(y))
-#endif
-#ifndef MAX
-#define MAX(x,y) ((x) > (y) ? (x):(y))
-#endif
-#ifndef SQR
-#define SQR(x) ((x)*(x))
-#endif
-static 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))
-
-
-/*__________________________________________________________________________
- |
- | xdropen - open xdr file
- |
- | This versions differs from xdrstdio_create, because I need to know
- | the state of the file (read or write) so I can use xdr3dfcoord
- | in eigther read or write mode, and the file descriptor
- | so I can close the file (something xdr_destroy doesn't do).
- |
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type) {
- static int init_done = 0;
- enum xdr_op lmode;
- const char *type1;
- int xdrid;
-
- if (init_done == 0) {
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- xdridptr[xdrid] = NULL;
- }
- init_done = 1;
- }
- xdrid = 1;
- while (xdrid < MAXID && xdridptr[xdrid] != NULL) {
- xdrid++;
- }
- if (xdrid == MAXID) {
- return 0;
- }
- if (*type == 'w' || *type == 'W') {
- type = "w+";
- type1 = "w+";
- lmode = XDR_ENCODE;
- } else if (*type == 'a' || *type == 'A') {
- type = "w+";
- type1 = "a+";
- lmode = XDR_ENCODE;
- } else {
- type = "r";
- type1 = "r";
- lmode = XDR_DECODE;
- }
- xdrfiles[xdrid] = fopen(filename, type1);
- if (xdrfiles[xdrid] == NULL) {
- xdrs = NULL;
- return 0;
- }
- xdrmodes[xdrid] = *type;
- /* next test isn't usefull in the case of C language
- * but is used for the Fortran interface
- * (C users are expected to pass the address of an already allocated
- * XDR staructure)
- */
- if (xdrs == NULL) {
- xdridptr[xdrid] = (XDR *) malloc(sizeof(XDR));
- xdrstdio_create(xdridptr[xdrid], xdrfiles[xdrid], lmode);
- } else {
- xdridptr[xdrid] = xdrs;
- xdrstdio_create(xdrs, xdrfiles[xdrid], lmode);
- }
- return xdrid;
-}
-
-/*_________________________________________________________________________
- |
- | xdrclose - close a xdr file
- |
- | This will flush the xdr buffers, and destroy the xdr stream.
- | It also closes the associated file descriptor (this is *not*
- | done by xdr_destroy).
- |
-*/
-
-int xdrclose(XDR *xdrs) {
- int xdrid;
-
- if (xdrs == NULL) {
- fprintf(stderr, "xdrclose: passed a NULL pointer\n");
- exit(1);
- }
- for (xdrid = 1; xdrid < MAXID; xdrid++) {
- if (xdridptr[xdrid] == xdrs) {
-
- xdr_destroy(xdrs);
- fclose(xdrfiles[xdrid]);
- xdridptr[xdrid] = NULL;
- return 1;
- }
- }
- fprintf(stderr, "xdrclose: no such open xdr file\n");
- exit(1);
-
-}
-
-/*____________________________________________________________________________
- |
- | sendbits - 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
- | better make sure that this number of bits is enough to hold the value
- | Also num must be positive.
- |
-*/
-
-static void sendbits(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);
- }
-}
-
-/*_________________________________________________________________________
- |
- | sizeofint - calculate bitsize of an integer
- |
- | return the number of bits needed to store an integer with given max size
- |
-*/
-
-static int sizeofint(const 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 the number of small unsigned integers and the maximum value
- | return the number of bits needed to read or write them with the
- | routines receiveints and sendints. You need this parameter when
- | calling these routines. Note that for many calls I can use
- | the variable 'smallidx' which is exactly the number of bits, and
- | So I don't need to call 'sizeofints for those calls.
-*/
-
-static int sizeofints( const 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;
-
-}
-
-/*____________________________________________________________________________
- |
- | sendints - send a small set of small integers in compressed format
- |
- | this routine is used internally by xdr3dfcoord, to send a set of
- | small integers to the buffer.
- | 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 causes bad results.
- |
- */
-
-static void sendints(int buf[], const int num_of_ints, const 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 sendints num %d doesn't "
- "match size %d\n", nums[i], sizes[i]);
- exit(1);
- }
- /* 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++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits - num_of_bytes * 8, 0);
- } else {
- for (i = 0; i < num_of_bytes-1; i++) {
- sendbits(buf, 8, bytes[i]);
- }
- sendbits(buf, num_of_bits- (num_of_bytes -1) * 8, bytes[i]);
- }
-}
-
-
-/*___________________________________________________________________________
- |
- | receivebits - 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 receivebits(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;
-}
-
-/*____________________________________________________________________________
- |
- | receiveints - decode 'small' integers from the buf array
- |
- | this routine is the inverse from sendints() 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 receiveints(int buf[], const 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++] = receivebits(buf, 8);
- num_of_bits -= 8;
- }
- if (num_of_bits > 0) {
- bytes[num_of_bytes++] = receivebits(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);
-}
-
-/*____________________________________________________________________________
- |
- | xdr3dfcoord - read or write compressed 3d coordinates to xdr file.
- |
- | this routine reads or writes (depending on how you opened the file with
- | xdropen() ) a large number of 3d coordinates (stored in *fp).
- | The number of coordinates triplets to write is given by *size. On
- | read this number may be zero, in which case it reads as many as were written
- | or it may specify the number if triplets to read (which should match the
- | number written).
- | Compression is achieved by first converting all floating numbers to integer
- | using multiplication by *precision and rounding to the nearest integer.
- | Then the minimum and maximum value are calculated to determine the range.
- | The limited range of integers so found, is used to compress the coordinates.
- | In addition the differences between succesive coordinates is calculated.
- | If the difference happens to be 'small' then only the difference is saved,
- | compressing the data even more. The notion of 'small' is changed dynamically
- | and is enlarged or reduced whenever needed or possible.
- | Extra compression is achieved in the case of GROMOS and coordinates of
- | water molecules. GROMOS first writes out the Oxygen position, followed by
- | the two hydrogens. In order to make the differences smaller (and thereby
- | compression the data better) the order is changed into first one hydrogen
- | then the oxygen, followed by the other hydrogen. This is rather special, but
- | it shouldn't harm in the general case.
- |
- */
-
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) {
-
-
- static int *ip = NULL;
- static int oldsize;
- static int *buf;
-
- 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 flag, k;
- int small, smaller, larger, i, is_small, is_smaller, run, prevrun;
- float *lfp, lf;
- int tmp, *thiscoord, prevcoord[3];
- unsigned int tmpcoord[30];
-
- int bufsize, xdrid, lsize;
- unsigned int bitsize;
- float inv_precision;
- int errval = 1;
-
- /* find out if xdrs is opened for reading or for writing */
- xdrid = 0;
- while (xdridptr[xdrid] != xdrs) {
- xdrid++;
- if (xdrid >= MAXID) {
- fprintf(stderr, "xdr error. no open xdr stream\n");
- exit (1);
- }
- }
- if (xdrmodes[xdrid] == 'w') {
-
- /* xdrs is open for writing */
-
- if (xdr_int(xdrs, size) == 0)
- return 0;
- size3 = *size * 3;
- /* when the number of coordinates is small, don't try to compress; just
- * write them as floats using xdr_vector
- */
- if (*size <= 9 ) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
-
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- /* buf[0-2] are special and do not contain actual data */
- buf[0] = buf[1] = buf[2] = 0;
- minint[0] = minint[1] = minint[2] = INT_MAX;
- maxint[0] = maxint[1] = maxint[2] = INT_MIN;
- prevrun = -1;
- lfp = fp;
- lip = ip;
- mindiff = INT_MAX;
- oldlint1 = oldlint2 = oldlint3 = 0;
- while(lfp < fp + size3 ) {
- /* find nearest integer */
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint1 = lf;
- if (lint1 < minint[0]) minint[0] = lint1;
- if (lint1 > maxint[0]) maxint[0] = lint1;
- *lip++ = lint1;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint2 = lf;
- if (lint2 < minint[1]) minint[1] = lint2;
- if (lint2 > maxint[1]) maxint[1] = lint2;
- *lip++ = lint2;
- lfp++;
- if (*lfp >= 0.0)
- lf = *lfp * *precision + 0.5;
- else
- lf = *lfp * *precision - 0.5;
- if (fabs(lf) > MAXABS) {
- /* scaling would cause overflow */
- errval = 0;
- }
- lint3 = lf;
- if (lint3 < minint[2]) minint[2] = lint3;
- if (lint3 > maxint[2]) maxint[2] = lint3;
- *lip++ = lint3;
- lfp++;
- diff = abs(oldlint1-lint1)+abs(oldlint2-lint2)+abs(oldlint3-lint3);
- if (diff < mindiff && lfp > fp + 3)
- mindiff = diff;
- oldlint1 = lint1;
- oldlint2 = lint2;
- oldlint3 = lint3;
- }
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- if ((float)maxint[0] - (float)minint[0] >= MAXABS ||
- (float)maxint[1] - (float)minint[1] >= MAXABS ||
- (float)maxint[2] - (float)minint[2] >= MAXABS) {
- /* turning value in unsigned by subtracting minint
- * would cause overflow
- */
- errval = 0;
- }
- 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);
- }
- lip = ip;
- luip = (unsigned int *) ip;
- smallidx = FIRSTIDX;
- while (smallidx < LASTIDX && magicints[smallidx] < mindiff) {
- smallidx++;
- }
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- larger = magicints[maxidx] / 2;
- i = 0;
- while (i < *size) {
- is_small = 0;
- thiscoord = (int *)(luip) + i * 3;
- if (smallidx < maxidx && i >= 1 &&
- abs(thiscoord[0] - prevcoord[0]) < larger &&
- abs(thiscoord[1] - prevcoord[1]) < larger &&
- abs(thiscoord[2] - prevcoord[2]) < larger) {
- is_smaller = 1;
- } else if (smallidx > minidx) {
- is_smaller = -1;
- } else {
- is_smaller = 0;
- }
- if (i + 1 < *size) {
- if (abs(thiscoord[0] - thiscoord[3]) < small &&
- abs(thiscoord[1] - thiscoord[4]) < small &&
- abs(thiscoord[2] - thiscoord[5]) < small) {
- /* interchange first with second atom for better
- * compression of water molecules
- */
- tmp = thiscoord[0]; thiscoord[0] = thiscoord[3];
- thiscoord[3] = tmp;
- tmp = thiscoord[1]; thiscoord[1] = thiscoord[4];
- thiscoord[4] = tmp;
- tmp = thiscoord[2]; thiscoord[2] = thiscoord[5];
- thiscoord[5] = tmp;
- is_small = 1;
- }
-
- }
- tmpcoord[0] = thiscoord[0] - minint[0];
- tmpcoord[1] = thiscoord[1] - minint[1];
- tmpcoord[2] = thiscoord[2] - minint[2];
- if (bitsize == 0) {
- sendbits(buf, bitsizeint[0], tmpcoord[0]);
- sendbits(buf, bitsizeint[1], tmpcoord[1]);
- sendbits(buf, bitsizeint[2], tmpcoord[2]);
- } else {
- sendints(buf, 3, bitsize, sizeint, tmpcoord);
- }
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
- thiscoord = thiscoord + 3;
- i++;
-
- run = 0;
- if (is_small == 0 && is_smaller == -1)
- is_smaller = 0;
- while (is_small && run < 8*3) {
- if (is_smaller == -1 && (
- SQR(thiscoord[0] - prevcoord[0]) +
- SQR(thiscoord[1] - prevcoord[1]) +
- SQR(thiscoord[2] - prevcoord[2]) >= smaller * smaller)) {
- is_smaller = 0;
- }
-
- tmpcoord[run++] = thiscoord[0] - prevcoord[0] + small;
- tmpcoord[run++] = thiscoord[1] - prevcoord[1] + small;
- tmpcoord[run++] = thiscoord[2] - prevcoord[2] + small;
-
- prevcoord[0] = thiscoord[0];
- prevcoord[1] = thiscoord[1];
- prevcoord[2] = thiscoord[2];
-
- i++;
- thiscoord = thiscoord + 3;
- is_small = 0;
- if (i < *size &&
- abs(thiscoord[0] - prevcoord[0]) < small &&
- abs(thiscoord[1] - prevcoord[1]) < small &&
- abs(thiscoord[2] - prevcoord[2]) < small) {
- is_small = 1;
- }
- }
- if (run != prevrun || is_smaller != 0) {
- prevrun = run;
- sendbits(buf, 1, 1); /* flag the change in run-length */
- sendbits(buf, 5, run+is_smaller+1);
- } else {
- sendbits(buf, 1, 0); /* flag the fact that runlength did not change */
- }
- for (k=0; k < run; k+=3) {
- sendints(buf, 3, smallidx, sizesmall, &tmpcoord[k]);
- }
- if (is_smaller != 0) {
- smallidx += is_smaller;
- if (is_smaller < 0) {
- small = smaller;
- smaller = magicints[smallidx-1] / 2;
- } else {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx];
- }
- }
- if (buf[1] != 0) buf[0]++;;
- xdr_int(xdrs, &(buf[0])); /* buf[0] holds the length in bytes */
- return errval * (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]));
- } else {
-
- /* xdrs is open for reading */
-
- if (xdr_int(xdrs, &lsize) == 0)
- return 0;
- if (*size != 0 && lsize != *size) {
- fprintf(stderr, "wrong number of coordinates in xdr3dfcoor; "
- "%d arg vs %d in file", *size, lsize);
- }
- *size = lsize;
- size3 = *size * 3;
- if (*size <= 9) {
- return (xdr_vector(xdrs, (char *) fp, size3, sizeof(*fp),
- (xdrproc_t)xdr_float));
- }
- xdr_float(xdrs, precision);
- if (ip == NULL) {
- ip = (int *)malloc(size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)malloc(bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- } else if (*size > oldsize) {
- ip = (int *)realloc(ip, size3 * sizeof(*ip));
- if (ip == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- bufsize = size3 * 1.2;
- buf = (int *)realloc(buf, bufsize * sizeof(*buf));
- if (buf == NULL) {
- fprintf(stderr,"malloc failed\n");
- exit(1);
- }
- oldsize = *size;
- }
- buf[0] = buf[1] = buf[2] = 0;
-
- xdr_int(xdrs, &(minint[0]));
- xdr_int(xdrs, &(minint[1]));
- xdr_int(xdrs, &(minint[2]));
-
- xdr_int(xdrs, &(maxint[0]));
- xdr_int(xdrs, &(maxint[1]));
- xdr_int(xdrs, &(maxint[2]));
-
- 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);
- }
-
- xdr_int(xdrs, &smallidx);
- maxidx = MIN(LASTIDX, smallidx + 8) ;
- minidx = maxidx - 8; /* often this equal smallidx */
- smaller = magicints[MAX(FIRSTIDX, smallidx-1)] / 2;
- small = magicints[smallidx] / 2;
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- larger = magicints[maxidx];
-
- /* buf[0] holds the length in bytes */
-
- if (xdr_int(xdrs, &(buf[0])) == 0)
- return 0;
- if (xdr_opaque(xdrs, (caddr_t)&(buf[3]), (u_int)buf[0]) == 0)
- return 0;
- buf[0] = buf[1] = buf[2] = 0;
-
- lfp = fp;
- inv_precision = 1.0 / * precision;
- run = 0;
- i = 0;
- lip = ip;
- while ( i < lsize ) {
- thiscoord = (int *)(lip) + i * 3;
-
- if (bitsize == 0) {
- thiscoord[0] = receivebits(buf, bitsizeint[0]);
- thiscoord[1] = receivebits(buf, bitsizeint[1]);
- thiscoord[2] = receivebits(buf, bitsizeint[2]);
- } else {
- receiveints(buf, 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 = receivebits(buf, 1);
- is_smaller = 0;
- if (flag == 1) {
- run = receivebits(buf, 5);
- is_smaller = run % 3;
- run -= is_smaller;
- is_smaller--;
- }
- if (run > 0) {
- thiscoord += 3;
- for (k = 0; k < run; k+=3) {
- receiveints(buf, 3, smallidx, sizesmall, thiscoord);
- i++;
- thiscoord[0] += prevcoord[0] - small;
- thiscoord[1] += prevcoord[1] - small;
- thiscoord[2] += prevcoord[2] - small;
- 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) {
- small = smaller;
- if (smallidx > FIRSTIDX) {
- smaller = magicints[smallidx - 1] /2;
- } else {
- smaller = 0;
- }
- } else if (is_smaller > 0) {
- smaller = small;
- small = magicints[smallidx] / 2;
- }
- sizesmall[0] = sizesmall[1] = sizesmall[2] = magicints[smallidx] ;
- }
- }
- return 1;
-}
-
-
-
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-/* fixincludes should not add extern "C" to this file */
-/*
- * Rpc additions to <sys/types.h>
- */
-#ifndef _RPC_TYPES_H
-#define _RPC_TYPES_H 1
-
-typedef int bool_t;
-typedef int enum_t;
-/* This needs to be changed to uint32_t in the future */
-typedef unsigned long rpcprog_t;
-typedef unsigned long rpcvers_t;
-typedef unsigned long rpcproc_t;
-typedef unsigned long rpcprot_t;
-typedef unsigned long rpcport_t;
-
-#define __dontcare__ -1
-
-#ifndef FALSE
-# define FALSE (0)
-#endif
-
-#ifndef TRUE
-# define TRUE (1)
-#endif
-
-#ifndef NULL
-# define NULL 0
-#endif
-
-#include <stdlib.h> /* For malloc decl. */
-#define mem_alloc(bsize) malloc(bsize)
-/*
- * XXX: This must not use the second argument, or code in xdr_array.c needs
- * to be modified.
- */
-#define mem_free(ptr, bsize) free(ptr)
-
-#ifndef makedev /* ie, we haven't already included it */
-#include <sys/types.h>
-#endif
-
-#ifndef __u_char_defined
-typedef __u_char u_char;
-typedef __u_short u_short;
-typedef __u_int u_int;
-typedef __u_long u_long;
-typedef __quad_t quad_t;
-typedef __u_quad_t u_quad_t;
-typedef __fsid_t fsid_t;
-# define __u_char_defined
-#endif
-#ifndef __daddr_t_defined
-typedef __daddr_t daddr_t;
-typedef __caddr_t caddr_t;
-# define __daddr_t_defined
-#endif
-
-#include <sys/time.h>
-#include <sys/param.h>
-
-#include <netinet/in.h>
-
-#ifndef INADDR_LOOPBACK
-#define INADDR_LOOPBACK (u_long)0x7F000001
-#endif
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 64
-#endif
-
-#endif /* rpc/types.h */
+++ /dev/null
-divert(-1)
-undefine(`len')
-#
-# append an underscore to FORTRAN function names
-#
-define(`FUNCTION',`$1_')
-#
-# FORTRAN character strings are passed as follows:
-# a pointer to the base of the string is passed in the normal
-# argument list, and the length is passed by value as an extra
-# argument, after all of the other arguments.
-#
-define(`ARGS',`($1`'undivert(1))')
-define(`SAVE',`divert(1)$1`'divert(0)')
-define(`STRING_ARG',`$1_ptr`'SAVE(`, $1_len')')
-define(`STRING_ARG_DECL',`char * $1_ptr; int $1_len')
-define(`STRING_LEN',`$1_len')
-define(`STRING_PTR',`$1_ptr')
-divert(0)
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
-#endif
-
-/*
- * xdr.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1986, Sun Microsystems, Inc.
- *
- * These are the "generic" xdr routines used to serialize and de-serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <libintl.h>
-
-#include "types.h"
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-/*
- * constants specific to the xdr "protocol"
- */
-#define XDR_FALSE ((long) 0)
-#define XDR_TRUE ((long) 1)
-#define LASTUNSIGNED ((u_int) 0-1)
-
-/*
- * for unit alignment
- */
-static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
-
-/*
- * Free a data structure using XDR
- * Not a filter, but a convenient utility nonetheless
- */
-void
-xdr_free (xdrproc_t proc, char *objp)
-{
- XDR x;
-
- x.x_op = XDR_FREE;
- (*proc) (&x, objp);
-}
-
-/*
- * XDR nothing
- */
-bool_t
-xdr_void (void)
-{
- return TRUE;
-}
-INTDEF(xdr_void)
-
-/*
- * XDR integers
- */
-bool_t
-xdr_int (XDR *xdrs, int *ip)
-{
-
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *ip;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ip = (int) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif INT_MAX == LONG_MAX
- return INTUSE(xdr_long) (xdrs, (long *) ip);
-#elif INT_MAX == SHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) ip);
-#else
-#error unexpected integer sizes in_xdr_int()
-#endif
-}
-INTDEF(xdr_int)
-
-/*
- * XDR unsigned integers
- */
-bool_t
-xdr_u_int (XDR *xdrs, u_int *up)
-{
-#if UINT_MAX < ULONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * up;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *up = (u_int) (u_long) l;
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-#elif UINT_MAX == ULONG_MAX
- return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
-#elif UINT_MAX == USHRT_MAX
- return INTUSE(xdr_short) (xdrs, (short *) up);
-#else
-#error unexpected integer sizes in_xdr_u_int()
-#endif
-}
-INTDEF(xdr_u_int)
-
-/*
- * XDR long integers
- * The definition of xdr_long() is kept for backward
- * compatibility. Instead xdr_int() should be used.
- */
-bool_t
-xdr_long (XDR *xdrs, long *lp)
-{
-
- if (xdrs->x_op == XDR_ENCODE
- && (sizeof (int32_t) == sizeof (long)
- || (int32_t) *lp == *lp))
- return XDR_PUTLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_DECODE)
- return XDR_GETLONG (xdrs, lp);
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_long)
-
-/*
- * XDR unsigned long integers
- * The definition of xdr_u_long() is kept for backward
- * compatibility. Instead xdr_u_int() should be used.
- */
-bool_t
-xdr_u_long (XDR *xdrs, u_long *ulp)
-{
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- {
- long int tmp;
-
- if (XDR_GETLONG (xdrs, &tmp) == FALSE)
- return FALSE;
-
- *ulp = (uint32_t) tmp;
- return TRUE;
- }
-
- case XDR_ENCODE:
- if (sizeof (uint32_t) != sizeof (u_long)
- && (uint32_t) *ulp != *ulp)
- return FALSE;
-
- return XDR_PUTLONG (xdrs, (long *) ulp);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_long)
-
-/*
- * XDR hyper integers
- * same as xdr_u_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_hyper (XDR *xdrs, quad_t *llp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (long) ((*llp) >> 32);
- t2 = (long) (*llp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *llp = ((quad_t) t1) << 32;
- *llp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_hyper)
-
-
-/*
- * XDR hyper integers
- * same as xdr_hyper - open coded to save a proc call!
- */
-bool_t
-xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
-{
- long int t1, t2;
-
- if (xdrs->x_op == XDR_ENCODE)
- {
- t1 = (unsigned long) ((*ullp) >> 32);
- t2 = (unsigned long) (*ullp);
- return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
- }
-
- if (xdrs->x_op == XDR_DECODE)
- {
- if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
- return FALSE;
- *ullp = ((u_quad_t) t1) << 32;
- *ullp |= (uint32_t) t2;
- return TRUE;
- }
-
- if (xdrs->x_op == XDR_FREE)
- return TRUE;
-
- return FALSE;
-}
-INTDEF(xdr_u_hyper)
-
-bool_t
-xdr_longlong_t (XDR *xdrs, quad_t *llp)
-{
- return INTUSE(xdr_hyper) (xdrs, llp);
-}
-
-bool_t
-xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
-{
- return INTUSE(xdr_u_hyper) (xdrs, ullp);
-}
-
-/*
- * XDR short integers
- */
-bool_t
-xdr_short (XDR *xdrs, short *sp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (long) *sp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *sp = (short) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_short)
-
-/*
- * XDR unsigned short integers
- */
-bool_t
-xdr_u_short (XDR *xdrs, u_short *usp)
-{
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = (u_long) * usp;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *usp = (u_short) (u_long) l;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_u_short)
-
-
-/*
- * XDR a char
- */
-bool_t
-xdr_char (XDR *xdrs, char *cp)
-{
- int i;
-
- i = (*cp);
- if (!INTUSE(xdr_int) (xdrs, &i))
- {
- return FALSE;
- }
- *cp = i;
- return TRUE;
-}
-
-/*
- * XDR an unsigned char
- */
-bool_t
-xdr_u_char (XDR *xdrs, u_char *cp)
-{
- u_int u;
-
- u = (*cp);
- if (!INTUSE(xdr_u_int) (xdrs, &u))
- {
- return FALSE;
- }
- *cp = u;
- return TRUE;
-}
-
-/*
- * XDR booleans
- */
-bool_t
-xdr_bool (XDR *xdrs, bool_t *bp)
-{
- long lb;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- lb = *bp ? XDR_TRUE : XDR_FALSE;
- return XDR_PUTLONG (xdrs, &lb);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &lb))
- {
- return FALSE;
- }
- *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
- return TRUE;
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bool)
-
-/*
- * XDR enumerations
- */
-bool_t
-xdr_enum (XDR *xdrs, enum_t *ep)
-{
- enum sizecheck
- {
- SIZEVAL
- }; /* used to find the size of an enum */
-
- /*
- * enums are treated as ints
- */
- if (sizeof (enum sizecheck) == 4)
- {
-#if INT_MAX < LONG_MAX
- long l;
-
- switch (xdrs->x_op)
- {
- case XDR_ENCODE:
- l = *ep;
- return XDR_PUTLONG (xdrs, &l);
-
- case XDR_DECODE:
- if (!XDR_GETLONG (xdrs, &l))
- {
- return FALSE;
- }
- *ep = l;
- case XDR_FREE:
- return TRUE;
-
- }
- return FALSE;
-#else
- return INTUSE(xdr_long) (xdrs, (long *) ep);
-#endif
- }
- else if (sizeof (enum sizecheck) == sizeof (short))
- {
- return INTUSE(xdr_short) (xdrs, (short *) ep);
- }
- else
- {
- return FALSE;
- }
-}
-INTDEF(xdr_enum)
-
-/*
- * XDR opaque data
- * Allows the specification of a fixed size sequence of opaque bytes.
- * cp points to the opaque object and cnt gives the byte length.
- */
-bool_t
-xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
-{
- u_int rndup;
- static char crud[BYTES_PER_XDR_UNIT];
-
- /*
- * if no data we are done
- */
- if (cnt == 0)
- return TRUE;
-
- /*
- * round byte count to full xdr units
- */
- rndup = cnt % BYTES_PER_XDR_UNIT;
- if (rndup > 0)
- rndup = BYTES_PER_XDR_UNIT - rndup;
-
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (!XDR_GETBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
-
- case XDR_ENCODE:
- if (!XDR_PUTBYTES (xdrs, cp, cnt))
- {
- return FALSE;
- }
- if (rndup == 0)
- return TRUE;
- return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
-
- case XDR_FREE:
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_opaque)
-
-/*
- * XDR counted bytes
- * *cpp is a pointer to the bytes, *sizep is the count.
- * If *cpp is NULL maxsize bytes are allocated
- */
-bool_t
-xdr_bytes (xdrs, cpp, sizep, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int *sizep;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int nodesize;
-
- /*
- * first deal with the length since xdr bytes are counted
- */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- nodesize = *sizep;
- if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (nodesize == 0)
- {
- return TRUE;
- }
- if (sp == NULL)
- {
- *cpp = sp = (char *) mem_alloc (nodesize);
- }
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
- return FALSE;
- }
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
-
- case XDR_FREE:
- if (sp != NULL)
- {
- mem_free (sp, nodesize);
- *cpp = NULL;
- }
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_bytes)
-
-/*
- * Implemented here due to commonality of the object.
- */
-bool_t
-xdr_netobj (xdrs, np)
- XDR *xdrs;
- struct netobj *np;
-{
-
- return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
-}
-INTDEF(xdr_netobj)
-
-/*
- * XDR a discriminated union
- * Support routine for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * an entry with a null procedure pointer. The routine gets
- * the discriminant value and then searches the array of xdrdiscrims
- * looking for that value. It calls the procedure given in the xdrdiscrim
- * to handle the discriminant. If there is no specific routine a default
- * routine may be called.
- * If there is no specific or default routine an error is returned.
- */
-bool_t
-xdr_union (xdrs, dscmp, unp, choices, dfault)
- XDR *xdrs;
- enum_t *dscmp; /* enum to decide which arm to work on */
- char *unp; /* the union itself */
- const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
- xdrproc_t dfault; /* default xdr routine */
-{
- enum_t dscm;
-
- /*
- * we deal with the discriminator; it's an enum
- */
- if (!INTUSE(xdr_enum) (xdrs, dscmp))
- {
- return FALSE;
- }
- dscm = *dscmp;
-
- /*
- * search choices for a value that matches the discriminator.
- * if we find one, execute the xdr routine for that value.
- */
- for (; choices->proc != NULL_xdrproc_t; choices++)
- {
- if (choices->value == dscm)
- return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
- }
-
- /*
- * no match - execute the default xdr routine if there is one
- */
- return ((dfault == NULL_xdrproc_t) ? FALSE :
- (*dfault) (xdrs, unp, LASTUNSIGNED));
-}
-INTDEF(xdr_union)
-
-
-/*
- * Non-portable xdr primitives.
- * Care should be taken when moving these routines to new architectures.
- */
-
-
-/*
- * XDR null terminated ASCII strings
- * xdr_string deals with "C strings" - arrays of bytes that are
- * terminated by a NULL character. The parameter cpp references a
- * pointer to storage; If the pointer is null, then the necessary
- * storage is allocated. The last parameter is the max allowed length
- * of the string as specified by a protocol.
- */
-bool_t
-xdr_string (xdrs, cpp, maxsize)
- XDR *xdrs;
- char **cpp;
- u_int maxsize;
-{
- char *sp = *cpp; /* sp is the actual string pointer */
- u_int size;
- u_int nodesize;
-
- /*
- * first deal with the length since xdr strings are counted-strings
- */
- switch (xdrs->x_op)
- {
- case XDR_FREE:
- if (sp == NULL)
- {
- return TRUE; /* already free */
- }
- /* fall through... */
- case XDR_ENCODE:
- if (sp == NULL)
- return FALSE;
- size = strlen (sp);
- break;
- case XDR_DECODE:
- break;
- }
- if (!INTUSE(xdr_u_int) (xdrs, &size))
- {
- return FALSE;
- }
- if (size > maxsize)
- {
- return FALSE;
- }
- nodesize = size + 1;
- if (nodesize == 0)
- {
- /* This means an overflow. It a bug in the caller which
- provided a too large maxsize but nevertheless catch it
- here. */
- return FALSE;
- }
-
- /*
- * now deal with the actual bytes
- */
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (sp == NULL)
- *cpp = sp = (char *) mem_alloc (nodesize);
- if (sp == NULL)
- {
- fprintf (NULL, "%s", "xdr_string: out of memory\n");
- return FALSE;
- }
- sp[size] = 0;
- /* fall into ... */
-
- case XDR_ENCODE:
- return INTUSE(xdr_opaque) (xdrs, sp, size);
-
- case XDR_FREE:
- mem_free (sp, nodesize);
- *cpp = NULL;
- return TRUE;
- }
- return FALSE;
-}
-INTDEF(xdr_string)
-
-/*
- * Wrapper for xdr_string that can be called directly from
- * routines like clnt_call
- */
-bool_t
-xdr_wrapstring (xdrs, cpp)
- XDR *xdrs;
- char **cpp;
-{
- if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
- {
- return TRUE;
- }
- return FALSE;
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr.h, External Data Representation Serialization Routines.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- */
-
-#ifndef _RPC_XDR_H
-#define _RPC_XDR_H 1
-
-#include <features.h>
-#include <sys/types.h>
-#include "types.h"
-
-/* We need FILE. */
-#include <stdio.h>
-
-__BEGIN_DECLS
-
-/*
- * XDR provides a conventional way for converting between C data
- * types and an external bit-string representation. Library supplied
- * routines provide for the conversion on built-in C data types. These
- * routines and utility routines defined here are used to help implement
- * a type encode/decode routine for each user-defined type.
- *
- * Each data type provides a single procedure which takes two arguments:
- *
- * bool_t
- * xdrproc(xdrs, argresp)
- * XDR *xdrs;
- * <type> *argresp;
- *
- * xdrs is an instance of a XDR handle, to which or from which the data
- * type is to be converted. argresp is a pointer to the structure to be
- * converted. The XDR handle contains an operation field which indicates
- * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
- *
- * XDR_DECODE may allocate space if the pointer argresp is null. This
- * data can be freed with the XDR_FREE operation.
- *
- * We write only one procedure per data type to make it easy
- * to keep the encode and decode procedures for a data type consistent.
- * In many cases the same code performs all operations on a user defined type,
- * because all the hard work is done in the component type routines.
- * decode as a series of calls on the nested data types.
- */
-
-/*
- * Xdr operations. XDR_ENCODE causes the type to be encoded into the
- * stream. XDR_DECODE causes the type to be extracted from the stream.
- * XDR_FREE can be used to release the space allocated by an XDR_DECODE
- * request.
- */
-enum xdr_op {
- XDR_ENCODE = 0,
- XDR_DECODE = 1,
- XDR_FREE = 2
-};
-
-/*
- * This is the number of bytes per unit of external data.
- */
-#define BYTES_PER_XDR_UNIT (4)
-/*
- * This only works if the above is a power of 2. But it's defined to be
- * 4 by the appropriate RFCs. So it will work. And it's normally quicker
- * than the old routine.
- */
-#if 1
-#define RNDUP(x) (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
-#else /* this is the old routine */
-#define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
- * BYTES_PER_XDR_UNIT)
-#endif
-
-/*
- * The XDR handle.
- * Contains operation which is being applied to the stream,
- * an operations vector for the particular implementation (e.g. see xdr_mem.c),
- * and two private fields for the use of the particular implementation.
- */
-typedef struct XDR XDR;
-struct XDR
- {
- enum xdr_op x_op; /* operation; fast additional param */
- struct xdr_ops
- {
- bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
- /* get a long from underlying stream */
- bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
- /* put a long to " */
- bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
- /* get some bytes from " */
- bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
- /* put some bytes to " */
- u_int (*x_getpostn) (__const XDR *__xdrs);
- /* returns bytes off from beginning */
- bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
- /* lets you reposition the stream */
- int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
- /* buf quick ptr to buffered data */
- void (*x_destroy) (XDR *__xdrs);
- /* free privates of this xdr_stream */
- bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
- /* get a int from underlying stream */
- bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
- /* put a int to " */
- }
- *x_ops;
- caddr_t x_public; /* users' data */
- caddr_t x_private; /* pointer to private data */
- caddr_t x_base; /* private used for position info */
- u_int x_handy; /* extra private word */
- };
-
-/*
- * A xdrproc_t exists for each data type which is to be encoded or decoded.
- *
- * The second argument to the xdrproc_t is a pointer to an opaque pointer.
- * The opaque pointer generally points to a structure of the data type
- * to be decoded. If this pointer is 0, then the type routines should
- * allocate dynamic storage of the appropriate size and return it.
- * bool_t (*xdrproc_t)(XDR *, caddr_t *);
- */
-typedef bool_t (*xdrproc_t) (XDR *, void *,...);
-
-
-/*
- * Operations defined on a XDR handle
- *
- * XDR *xdrs;
- * int32_t *int32p;
- * long *longp;
- * caddr_t addr;
- * u_int len;
- * u_int pos;
- */
-#define XDR_GETINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-#define xdr_getint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
-
-#define XDR_PUTINT32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-#define xdr_putint32(xdrs, int32p) \
- (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
-
-#define XDR_GETLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-#define xdr_getlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
-
-#define XDR_PUTLONG(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-#define xdr_putlong(xdrs, longp) \
- (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
-
-#define XDR_GETBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-#define xdr_getbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
-
-#define XDR_PUTBYTES(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-#define xdr_putbytes(xdrs, addr, len) \
- (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
-
-#define XDR_GETPOS(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-#define xdr_getpos(xdrs) \
- (*(xdrs)->x_ops->x_getpostn)(xdrs)
-
-#define XDR_SETPOS(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-#define xdr_setpos(xdrs, pos) \
- (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
-
-#define XDR_INLINE(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-#define xdr_inline(xdrs, len) \
- (*(xdrs)->x_ops->x_inline)(xdrs, len)
-
-#define XDR_DESTROY(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-#define xdr_destroy(xdrs) \
- do { \
- if ((xdrs)->x_ops->x_destroy) \
- (*(xdrs)->x_ops->x_destroy)(xdrs); \
- } while (0)
-
-/*
- * Support struct for discriminated unions.
- * You create an array of xdrdiscrim structures, terminated with
- * a entry with a null procedure pointer. The xdr_union routine gets
- * the discriminant value and then searches the array of structures
- * for a matching value. If a match is found the associated xdr routine
- * is called to handle that part of the union. If there is
- * no match, then a default routine may be called.
- * If there is no match and no default routine it is an error.
- */
-#define NULL_xdrproc_t ((xdrproc_t)0)
-struct xdr_discrim
-{
- int value;
- xdrproc_t proc;
-};
-
-/*
- * Inline routines for fast encode/decode of primitive data types.
- * Caveat emptor: these use single memory cycles to get the
- * data from the underlying buffer, and will fail to operate
- * properly if the data is not aligned. The standard way to use these
- * is to say:
- * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
- * return (FALSE);
- * <<< macro calls >>>
- * where ``count'' is the number of bytes of data occupied
- * by the primitive data types.
- *
- * N.B. and frozen for all time: each data type here uses 4 bytes
- * of external representation.
- */
-
-#define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
-#define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
-#define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
-#define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32(buf, (int32_t)(v))
-
-/* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
- * and shouldn't be used any longer. Code which use this defines or longs
- * in the RPC code will not work on 64bit Solaris platforms !
- */
-#define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
-#define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
-#define IXDR_GET_U_LONG(buf) ((u_long)IXDR_GET_LONG(buf))
-#define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-
-#define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
-#define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
-#define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
-#define IXDR_GET_U_SHORT(buf) ((u_short)IXDR_GET_LONG(buf))
-
-#define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-#define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG(buf, (long)(v))
-
-/*
- * These are the "generic" xdr routines.
- * None of these can have const applied because it's not possible to
- * know whether the call is a read or a write to the passed parameter
- * also, the XDR structure is always updated by some of these calls.
- */
-extern bool_t xdr_void (void) __THROW;
-extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
-extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
-extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
-extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
-extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
-extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
-extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
-extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
-extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
-extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
-extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
-extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
-extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
-extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
-extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
-extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
-extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
-extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
-extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
-extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
-extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
- u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
- __THROW;
-extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
- u_int __maxsize) __THROW;
-extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
-extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
-extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
- __const struct xdr_discrim *__choices,
- xdrproc_t dfault) __THROW;
-extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
-extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
-extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
- u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
-extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
-extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
-extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
- xdrproc_t __proc) __THROW;
-extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
- u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
-extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
-extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
-
-/*
- * Common opaque bytes objects used by many rpc protocols;
- * declared here due to commonality.
- */
-#define MAX_NETOBJ_SZ 1024
-struct netobj
-{
- u_int n_len;
- char *n_bytes;
-};
-typedef struct netobj netobj;
-extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
-
-/*
- * These are the public routines for the various implementations of
- * xdr streams.
- */
-
-/* XDR using memory buffers */
-extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
- u_int __size, enum xdr_op __xop) __THROW;
-
-/* XDR using stdio library */
-extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
- __THROW;
-
-/* XDR pseudo records for tcp */
-extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
- u_int __recvsize, caddr_t __tcp_handle,
- int (*__readit) (char *, char *, int),
- int (*__writeit) (char *, char *, int)) __THROW;
-
-/* make end of xdr record */
-extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
-
-/* move to beginning of next record */
-extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
-
-/* true if no more input */
-extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
-
-/* free memory buffers for xdr */
-extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
-
-__END_DECLS
-
-#endif /* rpc/xdr.h */
+++ /dev/null
-# define INTUSE(name) name
-# define INTDEF(name)
-/* @(#)xdr_array.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_array.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "non-trivial" xdr primitives used to serialize and de-serialize
- * arrays. See xdr.h for more info on the interface to xdr.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include "types.h"
-#include "xdr.h"
-#include <libintl.h>
-#include <limits.h>
-
-#ifdef USE_IN_LIBIO
-# include <wchar.h>
-#endif
-
-#define LASTUNSIGNED ((u_int)0-1)
-
-
-/*
- * XDR an array of arbitrary elements
- * *addrp is a pointer to the array, *sizep is the number of elements.
- * If addrp is NULL (*sizep * elsize) bytes are allocated.
- * elsize is the size (in bytes) of each element, and elproc is the
- * xdr procedure to call to handle each element of the array.
- */
-bool_t
-xdr_array (xdrs, addrp, sizep, maxsize, elsize, elproc)
- XDR *xdrs;
- caddr_t *addrp; /* array pointer */
- u_int *sizep; /* number of elements */
- u_int maxsize; /* max numberof elements */
- u_int elsize; /* size in bytes of each element */
- xdrproc_t elproc; /* xdr routine to handle each element */
-{
- u_int i;
- caddr_t target = *addrp;
- u_int c; /* the actual element count */
- bool_t stat = TRUE;
- u_int nodesize;
-
- /* like strings, arrays are really counted arrays */
- if (!INTUSE(xdr_u_int) (xdrs, sizep))
- {
- return FALSE;
- }
- c = *sizep;
- /*
- * XXX: Let the overflow possibly happen with XDR_FREE because mem_free()
- * doesn't actually use its second argument anyway.
- */
- if ((c > maxsize || c > UINT_MAX / elsize) && (xdrs->x_op != XDR_FREE))
- {
- return FALSE;
- }
- nodesize = c * elsize;
-
- /*
- * if we are deserializing, we may need to allocate an array.
- * We also save time by checking for a null array if we are freeing.
- */
- if (target == NULL)
- switch (xdrs->x_op)
- {
- case XDR_DECODE:
- if (c == 0)
- return TRUE;
- *addrp = target = mem_alloc (nodesize);
- if (target == NULL)
- {
- fprintf (stderr, "%s", "xdr_array: out of memory\n");
- return FALSE;
- }
- __bzero (target, nodesize);
- break;
-
- case XDR_FREE:
- return TRUE;
- default:
- break;
- }
-
- /*
- * now we xdr each element of array
- */
- for (i = 0; (i < c) && stat; i++)
- {
- stat = (*elproc) (xdrs, target, LASTUNSIGNED);
- target += elsize;
- }
-
- /*
- * the array may need freeing
- */
- if (xdrs->x_op == XDR_FREE)
- {
- mem_free (*addrp, nodesize);
- *addrp = NULL;
- }
- return stat;
-}
-INTDEF(xdr_array)
-
-/*
- * xdr_vector():
- *
- * XDR a fixed length array. Unlike variable-length arrays,
- * the storage of fixed length arrays is static and unfreeable.
- * > basep: base of the array
- * > size: size of the array
- * > elemsize: size of each element
- * > xdr_elem: routine to XDR each element
- */
-bool_t
-xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
- XDR *xdrs;
- char *basep;
- u_int nelem;
- u_int elemsize;
- xdrproc_t xdr_elem;
-{
- u_int i;
- char *elptr;
-
- elptr = basep;
- for (i = 0; i < nelem; i++)
- {
- if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
- {
- return FALSE;
- }
- elptr += elemsize;
- }
- return TRUE;
-}
+++ /dev/null
-/* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-#if !defined(lint) && defined(SCCSIDS)
-static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
-#endif
-
-/*
- * xdr_float.c, Generic XDR routines implementation.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * These are the "floating point" xdr routines used to (de)serialize
- * most common data items. See xdr.h for more info on the interface to
- * xdr.
- */
-
-#include <stdio.h>
-#include <endian.h>
-
-#include "types.h"
-#include "xdr.h"
-
-/*
- * NB: Not portable.
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
-
-#ifdef vax
-
-/* What IEEE single precision floating point looks like on a Vax */
-struct ieee_single {
- unsigned int mantissa: 23;
- unsigned int exp : 8;
- unsigned int sign : 1;
-};
-
-/* Vax single precision floating point */
-struct vax_single {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
-};
-
-#define VAX_SNG_BIAS 0x81
-#define IEEE_SNG_BIAS 0x7f
-
-static struct sgl_limits {
- struct vax_single s;
- struct ieee_single ieee;
-} sgl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
- { 0x0, 0xff, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
- { 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-#endif /* vax */
-
-bool_t
-xdr_float(xdrs, fp)
- XDR *xdrs;
- float *fp;
-{
-#ifdef vax
- struct ieee_single is;
- struct vax_single vs, *vsp;
- struct sgl_limits *lim;
- int i;
-#endif
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vs = *((struct vax_single *)fp);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((vs.mantissa2 == lim->s.mantissa2) &&
- (vs.exp == lim->s.exp) &&
- (vs.mantissa1 == lim->s.mantissa1)) {
- is = lim->ieee;
- goto shipit;
- }
- }
- is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
- is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
- shipit:
- is.sign = vs.sign;
- return (XDR_PUTLONG(xdrs, (long *)&is));
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_PUTLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp = *(int *)fp;
- return (XDR_PUTLONG(xdrs, &tmp));
- }
- break;
-#endif
-
- case XDR_DECODE:
-#ifdef vax
- vsp = (struct vax_single *)fp;
- if (!XDR_GETLONG(xdrs, (long *)&is))
- return (FALSE);
- for (i = 0, lim = sgl_limits;
- i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
- i++, lim++) {
- if ((is.exp == lim->ieee.exp) &&
- (is.mantissa == lim->ieee.mantissa)) {
- *vsp = lim->s;
- goto doneit;
- }
- }
- vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
- vsp->mantissa2 = is.mantissa;
- vsp->mantissa1 = (is.mantissa >> 16);
- doneit:
- vsp->sign = is.sign;
- return (TRUE);
-#else
- if (sizeof(float) == sizeof(long))
- return (XDR_GETLONG(xdrs, (long *)fp));
- else if (sizeof(float) == sizeof(int)) {
- long tmp;
- if (XDR_GETLONG(xdrs, &tmp)) {
- *(int *)fp = tmp;
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
-
-/*
- * This routine works on Suns (Sky / 68000's) and Vaxen.
- */
-
-#ifdef vax
-/* What IEEE double precision floating point looks like on a Vax */
-struct ieee_double {
- unsigned int mantissa1 : 20;
- unsigned int exp : 11;
- unsigned int sign : 1;
- unsigned int mantissa2 : 32;
-};
-
-/* Vax double precision floating point */
-struct vax_double {
- unsigned int mantissa1 : 7;
- unsigned int exp : 8;
- unsigned int sign : 1;
- unsigned int mantissa2 : 16;
- unsigned int mantissa3 : 16;
- unsigned int mantissa4 : 16;
-};
-
-#define VAX_DBL_BIAS 0x81
-#define IEEE_DBL_BIAS 0x3ff
-#define MASK(nbits) ((1 << nbits) - 1)
-
-static struct dbl_limits {
- struct vax_double d;
- struct ieee_double ieee;
-} dbl_limits[2] = {
- {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
- { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
- {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
- { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
-};
-
-#endif /* vax */
-
-
-bool_t
-xdr_double(xdrs, dp)
- XDR *xdrs;
- double *dp;
-{
-#ifdef vax
- struct ieee_double id;
- struct vax_double vd;
- register struct dbl_limits *lim;
- int i;
-#endif
-
- switch (xdrs->x_op) {
-
- case XDR_ENCODE:
-#ifdef vax
- vd = *((struct vax_double *)dp);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((vd.mantissa4 == lim->d.mantissa4) &&
- (vd.mantissa3 == lim->d.mantissa3) &&
- (vd.mantissa2 == lim->d.mantissa2) &&
- (vd.mantissa1 == lim->d.mantissa1) &&
- (vd.exp == lim->d.exp)) {
- id = lim->ieee;
- goto shipit;
- }
- }
- id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
- id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
- id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
- (vd.mantissa3 << 13) |
- ((vd.mantissa4 >> 3) & MASK(13));
- shipit:
- id.sign = vd.sign;
- dp = (double *)&id;
-#endif
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_PUTLONG(xdrs, lp+!LSW) &&
- XDR_PUTLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- tmp[0] = ip[!LSW];
- tmp[1] = ip[LSW];
- return (XDR_PUTLONG(xdrs, tmp) &&
- XDR_PUTLONG(xdrs, tmp+1));
- }
- break;
-
- case XDR_DECODE:
-#ifdef vax
- lp = (long *)&id;
- if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
- return (FALSE);
- for (i = 0, lim = dbl_limits;
- i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
- i++, lim++) {
- if ((id.mantissa2 == lim->ieee.mantissa2) &&
- (id.mantissa1 == lim->ieee.mantissa1) &&
- (id.exp == lim->ieee.exp)) {
- vd = lim->d;
- goto doneit;
- }
- }
- vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
- vd.mantissa1 = (id.mantissa1 >> 13);
- vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
- (id.mantissa2 >> 29);
- vd.mantissa3 = (id.mantissa2 >> 13);
- vd.mantissa4 = (id.mantissa2 << 3);
- doneit:
- vd.sign = id.sign;
- *dp = *((double *)&vd);
- return (TRUE);
-#else
- if (2*sizeof(long) == sizeof(double)) {
- long *lp = (long *)dp;
- return (XDR_GETLONG(xdrs, lp+!LSW) &&
- XDR_GETLONG(xdrs, lp+LSW));
- } else if (2*sizeof(int) == sizeof(double)) {
- int *ip = (int *)dp;
- long tmp[2];
- if (XDR_GETLONG(xdrs, tmp+!LSW) &&
- XDR_GETLONG(xdrs, tmp+LSW)) {
- ip[0] = tmp[0];
- ip[1] = tmp[1];
- return (TRUE);
- }
- }
- break;
-#endif
-
- case XDR_FREE:
- return (TRUE);
- }
- return (FALSE);
-}
+++ /dev/null
-/*
- * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
- * unrestricted use provided that this legend is included on all tape
- * media and as a part of the software program in whole or part. Users
- * may copy or modify Sun RPC without charge, but are not authorized
- * to license or distribute it to anyone else except as part of a product or
- * program developed by the user.
- *
- * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
- * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
- *
- * Sun RPC is provided with no support and without any obligation on the
- * part of Sun Microsystems, Inc. to assist in its use, correction,
- * modification or enhancement.
- *
- * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
- * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
- * OR ANY PART THEREOF.
- *
- * In no event will Sun Microsystems, Inc. be liable for any lost revenue
- * or profits or other special, indirect and consequential damages, even if
- * Sun has been advised of the possibility of such damages.
- *
- * Sun Microsystems, Inc.
- * 2550 Garcia Avenue
- * Mountain View, California 94043
- */
-
-/*
- * xdr_stdio.c, XDR implementation on standard i/o file.
- *
- * Copyright (C) 1984, Sun Microsystems, Inc.
- *
- * This set of routines implements a XDR on a stdio stream.
- * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
- * from the stream.
- */
-
-#include "types.h"
-#include <stdio.h>
-#include "xdr.h"
-
-#ifdef USE_IN_LIBIO
-# include <libio/iolibio.h>
-# define fflush(s) INTUSE(_IO_fflush) (s)
-# define fread(p, m, n, s) INTUSE(_IO_fread) (p, m, n, s)
-# define ftell(s) INTUSE(_IO_ftell) (s)
-# define fwrite(p, m, n, s) INTUSE(_IO_fwrite) (p, m, n, s)
-#endif
-
-static bool_t xdrstdio_getlong (XDR *, long *);
-static bool_t xdrstdio_putlong (XDR *, const long *);
-static bool_t xdrstdio_getbytes (XDR *, caddr_t, u_int);
-static bool_t xdrstdio_putbytes (XDR *, const char *, u_int);
-static u_int xdrstdio_getpos (const XDR *);
-static bool_t xdrstdio_setpos (XDR *, u_int);
-static int32_t *xdrstdio_inline (XDR *, u_int);
-static void xdrstdio_destroy (XDR *);
-static bool_t xdrstdio_getint32 (XDR *, int32_t *);
-static bool_t xdrstdio_putint32 (XDR *, const int32_t *);
-
-/*
- * Ops vector for stdio type XDR
- */
-static const struct xdr_ops xdrstdio_ops =
-{
- xdrstdio_getlong, /* deserialize a long int */
- xdrstdio_putlong, /* serialize a long int */
- xdrstdio_getbytes, /* deserialize counted bytes */
- xdrstdio_putbytes, /* serialize counted bytes */
- xdrstdio_getpos, /* get offset in the stream */
- xdrstdio_setpos, /* set offset in the stream */
- xdrstdio_inline, /* prime stream for inline macros */
- xdrstdio_destroy, /* destroy stream */
- xdrstdio_getint32, /* deserialize a int */
- xdrstdio_putint32 /* serialize a int */
-};
-
-/*
- * Initialize a stdio xdr stream.
- * Sets the xdr stream handle xdrs for use on the stream file.
- * Operation flag is set to op.
- */
-void
-xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
-{
- xdrs->x_op = op;
- /* We have to add the const since the `struct xdr_ops' in `struct XDR'
- is not `const'. */
- xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
- xdrs->x_private = (caddr_t) file;
- xdrs->x_handy = 0;
- xdrs->x_base = 0;
-}
-
-/*
- * Destroy a stdio xdr stream.
- * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
- */
-static void
-xdrstdio_destroy (XDR *xdrs)
-{
- (void) fflush ((FILE *) xdrs->x_private);
- /* xx should we close the file ?? */
-};
-
-static bool_t
-xdrstdio_getlong (XDR *xdrs, long *lp)
-{
- u_int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *lp = (long) ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putlong (XDR *xdrs, const long *lp)
-{
- int32_t mycopy = htonl ((u_int32_t) *lp);
-
- if (fwrite ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_getbytes (XDR *xdrs, const caddr_t addr, u_int len)
-{
- if ((len != 0) && (fread (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putbytes (XDR *xdrs, const char *addr, u_int len)
-{
- if ((len != 0) && (fwrite (addr, (int) len, 1,
- (FILE *) xdrs->x_private) != 1))
- return FALSE;
- return TRUE;
-}
-
-static u_int
-xdrstdio_getpos (const XDR *xdrs)
-{
- return (u_int) ftell ((FILE *) xdrs->x_private);
-}
-
-static bool_t
-xdrstdio_setpos (XDR *xdrs, u_int pos)
-{
- return fseek ((FILE *) xdrs->x_private, (long) pos, 0) < 0 ? FALSE : TRUE;
-}
-
-static int32_t *
-xdrstdio_inline (XDR *xdrs, u_int len)
-{
- /*
- * Must do some work to implement this: must insure
- * enough data in the underlying stdio buffer,
- * that the buffer is aligned so that we can indirect through a
- * long *, and stuff this pointer in xdrs->x_buf. Doing
- * a fread or fwrite to a scratch buffer would defeat
- * most of the gains to be had here and require storage
- * management on this buffer, so we don't do this.
- */
- return NULL;
-}
-
-static bool_t
-xdrstdio_getint32 (XDR *xdrs, int32_t *ip)
-{
- int32_t mycopy;
-
- if (fread ((caddr_t) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- *ip = ntohl (mycopy);
- return TRUE;
-}
-
-static bool_t
-xdrstdio_putint32 (XDR *xdrs, const int32_t *ip)
-{
- int32_t mycopy = htonl (*ip);
-
- ip = &mycopy;
- if (fwrite ((caddr_t) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
- return FALSE;
- return TRUE;
-}
-
-/* libc_hidden_def (xdrstdio_create) */
+++ /dev/null
-/*_________________________________________________________________
- |
- | xdrf.h - include file for C routines that want to use the
- | functions below.
-*/
-
-int xdropen(XDR *xdrs, const char *filename, const char *type);
-int xdrclose(XDR *xdrs) ;
-int xdr3dfcoord(XDR *xdrs, float *fp, int *size, float *precision) ;
-
add_executable(UNRES_BIN-MIN ${UNRES_MIN_SRCS} )
set_target_properties(UNRES_BIN-MIN PROPERTIES OUTPUT_NAME ${UNRES_BIN})
-#if (Fortran_COMPILER_NAME STREQUAL "ifort")
-target_link_libraries (UNRES_BIN-MIN ${CMAKE_THREAD_LIBS_INIT})
-#endif (Fortran_COMPILER_NAME STREQUAL "ifort")
+if (Fortran_COMPILER_NAME STREQUAL "ifort")
+ target_link_libraries (UNRES_BIN-MIN ${CMAKE_THREAD_LIBS_INIT})
+endif (Fortran_COMPILER_NAME STREQUAL "ifort")
#set_property(TARGET ${UNRES_BIN} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/unres/MD )
--- /dev/null
+# Set of programs to convert UNRES xdrf format (compressed Cartesian coordinates) to PDF
+# or raw-Cartesian format (*.x) or to extract backbone angular coordinates (*.ang)
+# The pdb files can be constructed from canonical or MREMD trajectories.
+#
+# The xdrf library is required
+#
+# Programs
+#
+# xdrf2pdb : converts a single cx trajectory file to PDB format
+# xdrf2x : converts a single cx trajectory file to raw-coordinate (x) format
+# xdrf2ang : extracts backbone angles from a cx trajectory file
+# xdrf2pdb-m : converts a selected trajectory of a MREMD run dumpend into a cx file to PDB format
+#
+# xdrf2pdb1 : converts conformation(s) selected from a wham post-processing run into PDB format
+# xdrf2x1 : converts conformation(s) selected from a wham post-processing run into raw (x) format.
+#
+# 9/23/2010 A. Liwo
+#
+# CMake file by D. Jagieła
+#
+
+set(UNRES_XDRF_XDRF2PDB_SRC-M
+ xdrf2pdb.F
+ geomout.F
+ misc.f
+ rescode.f
+ nazwy.f
+)
+
+set(UNRES_XDRF_XDRF2PDB-M_SRC-M
+ xdrf2pdb-m.F
+ geomout.F
+ misc.f
+ rescode.f
+ nazwy.f
+)
+
+
+set(UNRES_XDRF_PP_SRC-M
+ geomout.F
+ xdrf2pdb.F
+ xdrf2pdb-m.F
+ xdrf2x.F
+)
+
+set(CPPFLAGS "PROCOR -DUNRES -DMP -DMPI -DSPLITELE -DNEWUNRES" )
+
+#=========================================
+# System specific flags
+#=========================================
+if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+ set(CPPFLAGS "${CPPFLAGS} -DLINUX")
+endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+
+# Apply preprocesor flags to *.F files
+set_property(SOURCE ${UNRES_XDRF_PP_SRC-M} PROPERTY COMPILE_DEFINITIONS ${CPPFLAGS} )
+
+
+#=========================================
+# Build the binaries
+#=========================================
+add_executable(UNRES_XDRF2PDB_BIN-M ${UNRES_XDRF_XDRF2PDB_SRC-M} )
+target_link_libraries(UNRES_XDRF2PDB_BIN-M xdrf )
+set_target_properties(UNRES_XDRF2PDB_BIN-M PROPERTIES OUTPUT_NAME xdrf2pdb )
+
+add_executable(UNRES_XDRF2PDBM_BIN-M ${UNRES_XDRF_XDRF2PDB-M_SRC-M} )
+target_link_libraries( UNRES_XDRF2PDBM_BIN-M xdrf )
+set_target_properties(UNRES_XDRF2PDBM_BIN-M PROPERTIES OUTPUT_NAME xdrf2pdb-m )
+
-../xdrf
\ No newline at end of file
+../../lib/xdrf
\ No newline at end of file
+++ /dev/null
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.CHAIN'
- include 'COMMON.INTERACT'
- include 'COMMON.SBRIDGE'
- real*4 coord(3,1000)
- real*4 prec,potE,efree,rmsdev
- real*8 etot
- character*80 arg,seqfile,pdbfile
- character*3 sequenc(maxres)
- character*50 tytul
- character*8 onethree,cfreq
- character*8 ucase
- external ucase
- logical oneletter
- integer rescode
- external rescode
-
- ifreq=1
- if (iargc().lt.4) then
- print '(a)',
- & "Usage: xdrf2pdb one/three seqfile cxfile conf [pdbfile]"
- stop
- endif
- call getarg(1,onethree)
- onethree = ucase(onethree)
- if (onethree.eq.'ONE') then
- oneletter = .true.
- else if (onethree.eq.'THREE') then
- oneletter = .false.
- else
- print *,"ONE or THREE must be specified"
- endif
- call getarg(2,seqfile)
- open (1,file=seqfile,status='old')
- if (oneletter) then
- read(1,'(80a1)',end=10,err=10) (sequenc(i)(1:1),i=1,maxres)
- 10 continue
- nres=i
- i=0
- do while (.not.iblnk(sequenc(i+1)(1:1)))
- i=i+1
- enddo
- nres=i
- do i=1,nres
- itype(i)=rescode(i,sequenc(i),1)
- enddo
- else
- read(1,'(20(a3,1x))',end=11,err=11) (sequenc(i),i=1,maxres)
- 11 continue
- nres=i
- print *,"nres",nres
- do i=1,nres
- print *,i," ",sequenc(i)
- enddo
- print *
- i=0
- do while (.not.iblnk(sequenc(i+1)(1:1)))
- print *,i+1," ",sequenc(i+1)," ",sequenc(i+1)(1:1)
- i=i+1
- enddo
- nres=i
- print *,"nres",nres
- do i=1,nres
- itype(i)=rescode(i,sequenc(i),0)
- enddo
- print *,(itype(i),i=1,nres)
- endif
- call getarg(3,arg)
- iext = index(arg,'.cx') - 1
- if (iext.lt.0) then
- print *,"Error - not a cx file"
- stop
- endif
- print *,"arg ",arg
- call getarg(4,cfreq)
- read (cfreq,*) iconf
- print *,"iconf",iconf
- if (iargc().gt.4) then
- call getarg(5,pdbfile)
- else
- pdbfile=arg(:iext)//'.pdb'
- endif
- open(9,file=pdbfile)
- nnt = 1
- if (itype(1).eq.21) nnt = 2
- nct=nres
- if (itype(nres).eq.21) nct = nres-1
-c if (nct.eq.nres-1) nres=nres-1
-c if (nnt.eq.2) nres=nres-1
-
- print *,"nres",nres," nnt",nnt," nct",nct
-
- call xdrfopen(ixdrf,arg, "r", iret)
- print *,"iret",iret
- kk = 0
- do while(.true.)
- prec=10000.0
- isize=0
- call xdrf3dfcoord(ixdrf, coord, isize, prec, iret)
- call xdrfint(ixdrf, nss, iret)
- do j=1,nss
- call xdrfint(ixdrf, ihpb(j), iret)
- call xdrfint(ixdrf, jhpb(j), iret)
- enddo
- call xdrffloat(ixdrf, potE, iret)
- if(iret.eq.0) exit
- kk = kk + 1
- call xdrffloat(ixdrf, efree, iret)
- call xdrffloat(ixdrf, rmsdev, iret)
- call xdrfint(ixdrf, iscor, iret)
- if (kk.eq.iconf) then
- print *,"pote",pote," efree",efree," rmsdev",rmsdev
-
- print *,"isize",isize
-
- if (isize .ne. nres+nct-nnt+1) then
- print *,"Error: inconsistent sizes",isize,nres+nct-nnt+1
- endif
- do i=1,nres
- do j=1,3
- c(j,i)=coord(j,i)
- enddo
- enddo
- ii = 0
- do i=nnt,nct
- ii = ii + 1
- do j=1,3
- c(j,i+nres)=coord(j,ii+nres)
- enddo
- enddo
- etot=potE
- write (tytul,'(a,i6)') "Structure",kk
- call pdbout(etot,tytul,9)
- stop
- endif
- enddo
-
- end
--- /dev/null
+# Set of programs to convert UNRES xdrf format (compressed Cartesian coordinates) to PDF
+# or raw-Cartesian format (*.x) or to extract backbone angular coordinates (*.ang)
+# The pdb files can be constructed from canonical or MREMD trajectories.
+#
+# The xdrf library is required
+#
+# Programs
+#
+# xdrf2pdb : converts a single cx trajectory file to PDB format
+# xdrf2x : converts a single cx trajectory file to raw-coordinate (x) format
+# xdrf2ang : extracts backbone angles from a cx trajectory file
+# xdrf2pdb-m : converts a selected trajectory of a MREMD run dumpend into a cx file to PDB format
+#
+# xdrf2pdb1 : converts conformation(s) selected from a wham post-processing run into PDB format
+# xdrf2x1 : converts conformation(s) selected from a wham post-processing run into raw (x) format.
+#
+# 9/23/2010 A. Liwo
+#
+# CMake file by D. Jagieła
+#
+
+set(UNRES_XDRF_XDRF2PDB_SRC
+ xdrf2pdb.F
+ geomout.F
+ misc.f
+ rescode.f
+ nazwy.f
+)
+
+set(UNRES_XDRF_XDRF2PDB-M_SRC
+ xdrf2pdb-m.F
+ geomout.F
+ misc.f
+ rescode.f
+ nazwy.f
+)
+
+
+set(UNRES_XDRF_XDRF2X_SRC
+ xdrf2x.F
+)
+
+
+set(UNRES_XDRF_XDRF2XANG_SRC
+ xdrf2ang.f
+ misc.f
+ rescode.f
+ nazwy.f
+ intcor.f
+ arcos.f
+)
+
+set(UNRES_XDRF_PP_SRC
+ geomout.F
+ xdrf2pdb.F
+ xdrf2pdb-m.F
+ xdrf2x.F
+)
+
+set(CPPFLAGS "PROCOR -DUNRES -DMP -DMPI -DSPLITELE -DNEWUNRES" )
+
+#=========================================
+# System specific flags
+#=========================================
+if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+ set(CPPFLAGS "${CPPFLAGS} -DLINUX")
+endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
+
+# Apply preprocesor flags to *.F files
+set_property(SOURCE ${UNRES_XDRF_PP_SRC} PROPERTY COMPILE_DEFINITIONS ${CPPFLAGS} )
+
+
+#=========================================
+# Build the binaries
+#=========================================
+add_executable(xdrf2pdb ${UNRES_XDRF_XDRF2PDB_SRC} )
+target_link_libraries( xdrf2pdb xdrf )
+
+add_executable(xdrf2pdb-m ${UNRES_XDRF_XDRF2PDB-M_SRC} )
+target_link_libraries( xdrf2pdb-m xdrf )
+
+add_executable(xdrf2x ${UNRES_XDRF_XDRF2X_SRC} )
+target_link_libraries( xdrf2x xdrf )
+
+add_executable(xdrf2ang ${UNRES_XDRF_XDRF2XANG_SRC} )
+target_link_libraries( xdrf2ang xdrf )
+
+
+
-../xdrf
\ No newline at end of file
+../../lib/xdrf
\ No newline at end of file
+++ /dev/null
- implicit real*8 (a-h,o-z)
- include 'DIMENSIONS'
- include 'COMMON.CHAIN'
- include 'COMMON.INTERACT'
- include 'COMMON.SBRIDGE'
- real*4 coord(3,1000)
- real*4 prec,potE,efree,rmsdev
- real*8 etot
- character*80 arg,seqfile,pdbfile
- character*3 sequenc(maxres)
- character*50 tytul
- character*8 onethree,cfreq
- character*8 ucase
- external ucase
- logical oneletter
- integer rescode
- external rescode
-
- ifreq=1
- if (iargc().lt.4) then
- print '(a)',
- & "Usage: xdrf2pdb one/three seqfile cxfile conf [pdbfile]"
- stop
- endif
- call getarg(1,onethree)
- onethree = ucase(onethree)
- if (onethree.eq.'ONE') then
- oneletter = .true.
- else if (onethree.eq.'THREE') then
- oneletter = .false.
- else
- print *,"ONE or THREE must be specified"
- endif
- call getarg(2,seqfile)
- open (1,file=seqfile,status='old')
- if (oneletter) then
- read(1,'(80a1)',end=10,err=10) (sequenc(i)(1:1),i=1,maxres)
- 10 continue
- nres=i
- i=0
- do while (.not.iblnk(sequenc(i+1)(1:1)))
- i=i+1
- enddo
- nres=i
- do i=1,nres
- itype(i)=rescode(i,sequenc(i),1)
- enddo
- else
- read(1,'(20(a3,1x))',end=11,err=11) (sequenc(i),i=1,maxres)
- 11 continue
- nres=i
- print *,"nres",nres
- do i=1,nres
- print *,i," ",sequenc(i)
- enddo
- print *
- i=0
- do while (.not.iblnk(sequenc(i+1)(1:1)))
- print *,i+1," ",sequenc(i+1)," ",sequenc(i+1)(1:1)
- i=i+1
- enddo
- nres=i
- print *,"nres",nres
- do i=1,nres
- itype(i)=rescode(i,sequenc(i),0)
- enddo
- print *,(itype(i),i=1,nres)
- endif
- call getarg(3,arg)
- iext = index(arg,'.cx') - 1
- if (iext.lt.0) then
- print *,"Error - not a cx file"
- stop
- endif
- print *,"arg ",arg
- call getarg(4,cfreq)
- read (cfreq,*) iconf
- print *,"iconf",iconf
- if (iargc().gt.4) then
- call getarg(5,pdbfile)
- else
- pdbfile=arg(:iext)//'.pdb'
- endif
- open(9,file=pdbfile)
- nnt = 1
- if (itype(1).eq.21) nnt = 2
- nct=nres
- if (itype(nres).eq.21) nct = nres-1
-c if (nct.eq.nres-1) nres=nres-1
-c if (nnt.eq.2) nres=nres-1
-
- print *,"nres",nres," nnt",nnt," nct",nct
-
- call xdrfopen(ixdrf,arg, "r", iret)
- print *,"iret",iret
- kk = 0
- do while(.true.)
- prec=10000.0
- isize=0
- call xdrf3dfcoord(ixdrf, coord, isize, prec, iret)
- call xdrfint(ixdrf, nss, iret)
- do j=1,nss
- call xdrfint(ixdrf, ihpb(j), iret)
- call xdrfint(ixdrf, jhpb(j), iret)
- enddo
- call xdrffloat(ixdrf, potE, iret)
- if(iret.eq.0) exit
- kk = kk + 1
- call xdrffloat(ixdrf, efree, iret)
- call xdrffloat(ixdrf, rmsdev, iret)
- call xdrfint(ixdrf, iscor, iret)
- if (kk.eq.iconf) then
- print *,"pote",pote," efree",efree," rmsdev",rmsdev
-
- print *,"isize",isize
-
- if (isize .ne. nres+nct-nnt+1) then
- print *,"Error: inconsistent sizes",isize,nres+nct-nnt+1
- endif
- do i=1,nres
- do j=1,3
- c(j,i)=coord(j,i)
- enddo
- enddo
- ii = 0
- do i=nnt,nct
- ii = ii + 1
- do j=1,3
- c(j,i+nres)=coord(j,ii+nres)
- enddo
- enddo
- etot=potE
- write (tytul,'(a,i6)') "Structure",kk
- call pdbout(etot,tytul,9)
- stop
- endif
- enddo
-
- end
+++ /dev/null
- implicit real*8 (a-h,o-z)
- integer ihpb(100),jhpb(100)
- real*4 coord(3,1000)
- real*4 prec,potE,efree,rmsdev,qfrag(100)
- real*8 etot
- character*80 arg,xfile
- character*8 ucase,cfreq
- external ucase
- logical oneletter
- integer rescode
- external rescode
-
- ifreq=1
- if (iargc().lt.2) then
- print '(a)',
- & "Usage: xdrf2x1 cxfile conf [pdbfile]"
- stop
- endif
- call getarg(1,arg)
- iext = index(arg,'.cx') - 1
- if (iext.lt.0) then
- print *,"Error - not a cx file"
- stop
- endif
- print *,"arg ",arg
- call getarg(2,cfreq)
- read (cfreq,*) iconf
- print *,"iconf",iconf
- if (iargc().gt.2) then
- call getarg(3,pdbfile)
- else
- xfile=arg(:iext)//'.x'
- endif
- open(9,file=xfile)
-
- call xdrfopen(ixdrf,arg, "r", iret)
- print *,"iret",iret
- kk = 0
- do while(.true.)
- prec=10000.0
- isize=0
- call xdrf3dfcoord(ixdrf, coord, isize, prec, iret)
- call xdrfint(ixdrf, nss, iret)
- do j=1,nss
- call xdrfint(ixdrf, ihpb(j), iret)
- call xdrfint(ixdrf, jhpb(j), iret)
- enddo
- call xdrffloat(ixdrf, potE, iret)
- if(iret.eq.0) exit
- kk = kk + 1
- call xdrffloat(ixdrf, efree, iret)
- call xdrffloat(ixdrf, rmsdev, iret)
- call xdrfint(ixdrf, iscor, iret)
- if (kk.eq.iconf) then
- print *,"pote",pote," efree",efree," rmsdev",rmsdev
-
- print *,"isize",isize
-
- write (9,'(e15.8,2e15.5,f12.5,$)') time,potE,uconst,t_bath
- write (9,'(i4,$)') nss,(ihpb(j),jhpb(j),j=1,nss)
- write (9,'(i4,20f7.4)') nfrag,(qfrag(i),i=1,nfrag)
- write (9,'(8f10.5)') ((coord(k,j),k=1,3),j=1,isize)
- stop
- endif
- enddo
-
- end