home *** CD-ROM | disk | FTP | other *** search
- #include <string.h>
- #include "NDR.h"
- #include <libc.h>
-
- extern "C" {
- int ftoi(float *);
- int itof(float *);
- int dtoi(double *);
- int itod(double *);
- unsigned short ntohs(unsigned short);
- };
-
- #if defined(vax) || defined(mips)
- RJS_NDR::NDR_endian RJS_NDR::ndr_endian=RJS_NDR::NdrLittleEndian;
- RJS_NDR::NDR_character RJS_NDR::ndr_char=RJS_NDR::NdrASCII;
- #endif
-
- #if defined(vax)
- RJS_NDR::NDR_floating RJS_NDR::ndr_float=RJS_NDR::NdrVAX;
- #elif defined(mips)
- RJS_NDR::NDR_floating RJS_NDR::ndr_float=RJS_NDR::NdrIEEE;
- #endif
-
-
- RJS_Transport &operator>>(RJS_Transport &tr, RJS_NDR_receive &ndr)
- {
- unsigned short length;
- tr.read((char *)&length,sizeof(unsigned short));
- length=ntohs(length);
- // check for errors
- tr.read(ndr.raw_data(),length);
- ndr.init(ndr.raw_data(),length);
- return tr;
- }
-
- ostream &operator<<(ostream &os, RJS_NDR_receive &ndr)
- {
- cout << "local_endian() => " << ndr.local_endian() << endl;
- cout << "local_character() => " << ndr.local_character() << endl;
- cout << "local_floating() => " << ndr.local_floating() << endl;
- cout << "remote_endian() => " << ndr.remote_endian() << endl;
- cout << "remote_character() => " << ndr.remote_character() << endl;
- cout << "remote_floating() => " << ndr.remote_floating() << endl;
- cout << "raw_length() => " << ndr.raw_length() << endl;
- for (int i=0; i< ndr.raw_length(); i++) {
- cout << i << "\t" << int(*(ndr.raw_data()+i)) << endl;
- }
- return os;
- }
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr, RJS_XNDR &x) {
- x.to_internal(ndr);
- return ndr;
- }
-
- void RJS_NDR_receive::reset()
- {
- outptr=start_data+2;
- }
-
- void RJS_NDR_receive::init(char *buffer,int size)
- {
- int align=((unsigned int)(buffer)) % sizeof(double);
- if (align) buffer += (sizeof(double)-align);
- start_data = buffer;
- outptr = buffer;
- end_data = buffer+size;
- rem_endian = RJS_NDR::NDR_endian((*outptr >> 4) & 0x0f);
- rem_char = RJS_NDR::NDR_character(*outptr++ & 0x0f);
- rem_float = RJS_NDR::NDR_floating(*outptr++);
- }
-
- RJS_NDR_receive &RJS_NDR_receive::extract(char *d,int &count)
- {
- (*this) >> count;
- strncpy(d,outptr,count);
- outptr += count;
- return *this;
- }
-
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,char &ch)
- {
- //if (passed end_data) error
- ch = *ndr.outptr++;
- return ndr;
- }
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,unsigned char &ch)
- {
- //if (passed end_data) error
- ch = *ndr.outptr++;
- return ndr;
- }
-
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,short &i)
- {
- if (((unsigned int)(ndr.outptr)) % sizeof(short)) ndr.outptr++;
- if (ndr.local_endian()==ndr.remote_endian())
- i = * (short *) ndr.outptr;
- else {
- char *sp= (char *)&i;
- sp[0] = ndr.outptr[1];
- sp[1] = ndr.outptr[0];
- }
- ndr.outptr += sizeof(short);
- return ndr;
- }
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,unsigned short &i)
- {
- if (((unsigned int)(ndr.outptr)) % sizeof(unsigned short)) ndr.outptr++;
- if (ndr.local_endian()==ndr.remote_endian())
- i = * (unsigned short *) ndr.outptr;
- else {
- char *sp= (char *)&i;
- sp[0] = ndr.outptr[1];
- sp[1] = ndr.outptr[0];
- }
- ndr.outptr += sizeof(unsigned short);
- return ndr;
- }
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,int &i)
- {
- int align=((unsigned int)(ndr.outptr)) % sizeof(int);
- if (align) ndr.outptr += (sizeof(int)-align);
- if (ndr.local_endian()==ndr.remote_endian())
- i = * (int *) ndr.outptr;
- else {
- char *sp= (char *)&i;
- sp[0] = ndr.outptr[3];
- sp[1] = ndr.outptr[2];
- sp[2] = ndr.outptr[1];
- sp[3] = ndr.outptr[0];
- }
- ndr.outptr += sizeof(int);
- return ndr;
- }
-
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,unsigned int &i)
- {
- int align=((unsigned int)(ndr.outptr)) % sizeof(unsigned int);
- if (align) ndr.outptr += (sizeof(unsigned int)-align);
- if (ndr.local_endian()==ndr.remote_endian())
- i = * (unsigned int *) ndr.outptr;
- else {
- char *sp= (char *)&i;
- sp[0] = ndr.outptr[3];
- sp[1] = ndr.outptr[2];
- sp[2] = ndr.outptr[1];
- sp[3] = ndr.outptr[0];
- }
- ndr.outptr += sizeof(unsigned int);
- return ndr;
- }
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,float &f)
- {
- char buf[8];
- int align=((unsigned int)(ndr.outptr)) % sizeof(float);
- if (align) ndr.outptr += (sizeof(float)-align);
- if (ndr.local_endian()==ndr.remote_endian()) {
- buf[0] = ndr.outptr[0];
- buf[1] = ndr.outptr[1];
- buf[2] = ndr.outptr[2];
- buf[3] = ndr.outptr[3];
- } else {
- buf[0] = ndr.outptr[3];
- buf[1] = ndr.outptr[2];
- buf[2] = ndr.outptr[1];
- buf[3] = ndr.outptr[0];
- }
- if (ndr.local_floating()!=ndr.remote_floating()) {
- if (ndr.local_floating()==RJS_NDR::NdrVAX) {
- if (ndr.remote_floating()==RJS_NDR::NdrIEEE)
- itof((float *) &buf);
- } else if (ndr.local_floating()==RJS_NDR::NdrIEEE) {
- if (ndr.remote_floating()==RJS_NDR::NdrVAX)
- ftoi((float *) &buf);
- }
- }
- f = * (float *) buf;
- ndr.outptr += sizeof(float);
- return ndr;
- }
-
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,double &d)
- {
- char buf[8];
- int align=((unsigned int)(ndr.outptr)) % sizeof(double);
- if (align) ndr.outptr += (sizeof(double)-align);
- if (ndr.local_endian()==ndr.remote_endian()) {
- buf[0] = ndr.outptr[0];
- buf[1] = ndr.outptr[1];
- buf[2] = ndr.outptr[2];
- buf[3] = ndr.outptr[3];
- buf[4] = ndr.outptr[4];
- buf[5] = ndr.outptr[5];
- buf[6] = ndr.outptr[6];
- buf[7] = ndr.outptr[7];
- } else {
- buf[0] = ndr.outptr[7];
- buf[1] = ndr.outptr[6];
- buf[2] = ndr.outptr[5];
- buf[3] = ndr.outptr[4];
- buf[4] = ndr.outptr[3];
- buf[5] = ndr.outptr[2];
- buf[6] = ndr.outptr[1];
- buf[7] = ndr.outptr[0];
- }
- if (ndr.local_floating()!=ndr.remote_floating()) {
- if (ndr.local_floating()==RJS_NDR::NdrVAX) {
- if (ndr.remote_floating()==RJS_NDR::NdrIEEE)
- itod((double *) &buf);
- } else if (ndr.local_floating()==RJS_NDR::NdrIEEE) {
- if (ndr.remote_floating()==RJS_NDR::NdrVAX)
- dtoi((double *) &buf);
- }
- }
-
- d = * (double *) buf;
-
- ndr.outptr += sizeof(double);
- return ndr;
- }
-
- RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,char *str)
- {
- unsigned short len;
- ndr >> len;
- strncpy(str,ndr.outptr,len);
- ndr.outptr += len;
- return ndr;
- }
-
-
-