From db7e4ac7974679c6ad4e175a2649c4f6daa57e7b Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 17 Dec 2004 11:08:53 +0000 Subject: [PATCH] Moved getMachineEndian from parser to liblwgeom.{h,c}. Added XDR and NDR defines. Fixed all usage of them. git-svn-id: http://svn.osgeo.org/postgis/trunk@1162 b70326c6-7e19-0410-871a-916f4a2858ee --- lwgeom/liblwgeom.c | 10 ++++++++++ lwgeom/liblwgeom.h | 7 +++++++ lwgeom/lwgeom_inout.c | 10 +++++----- lwgeom/lwgparse.c | 30 +++++++++++------------------- lwgeom/wktparse.h | 3 +-- lwgeom/wktunparse.c | 42 ++++++++++++++++-------------------------- 6 files changed, 50 insertions(+), 52 deletions(-) diff --git a/lwgeom/liblwgeom.c b/lwgeom/liblwgeom.c index e9a4f3611..13310472a 100644 --- a/lwgeom/liblwgeom.c +++ b/lwgeom/liblwgeom.c @@ -169,3 +169,13 @@ trim_trailing_zeros(char *str) //lwnotice("output: %s", str); } + +char getMachineEndian() +{ + static int endian_check_int = 1; // dont modify this!!! + + return *((char *) &endian_check_int); // 0 = big endian | xdr, + // 1 = little endian | ndr +} + + diff --git a/lwgeom/liblwgeom.h b/lwgeom/liblwgeom.h index f43ce9c31..5ba027c4a 100644 --- a/lwgeom/liblwgeom.h +++ b/lwgeom/liblwgeom.h @@ -350,6 +350,8 @@ extern int pointArray_ptsize(const POINTARRAY *pa); #define WKBZOFFSET 0x80000000 #define WKBMOFFSET 0x40000000 +#define WKBSRIDFLAG 0x20000000 +#define WKBBBOXFLAG 0x10000000 #define TYPE_SETTYPE(c,t) ((c)=(((c)&0xF0)|(t))) #define TYPE_SETZM(t,z,m) ((t)=(((t)&0xCF)|((z)<<5)|((m)<<4))) @@ -1030,4 +1032,9 @@ extern void lwfree(void *mem); /* Utilities */ extern void trim_trailing_zeros(char *num); +/* Machine endianness */ +#define XDR 0 +#define NDR 1 +extern char getMachineEndian(void); + #endif // !defined _LIBLWGEOM_H diff --git a/lwgeom/lwgeom_inout.c b/lwgeom/lwgeom_inout.c index 1835a9792..afd6db24b 100644 --- a/lwgeom/lwgeom_inout.c +++ b/lwgeom/lwgeom_inout.c @@ -23,7 +23,7 @@ #include "stringBuffer.h" -#define DEBUG 1 +//#define DEBUG 1 #include "lwgeom_pg.h" #include "wktparse.h" @@ -251,7 +251,7 @@ Datum LWGEOMFromWKB(PG_FUNCTION_ARGS) size_header = sprintf(sridText,"SRID=%i;",SRID); //SRID text size + wkb size (+1 = NULL term) - size_result = size_header + 2*(wkb_input->size -4) + 1; + size_result = size_header + 2*(wkb_input->size-4) + 1; wkb_srid_hexized = palloc(size_result); wkb_srid_hexized[0] = 0; // empty @@ -312,11 +312,11 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS) if ( ! strncmp(VARDATA(type), "xdr", 3) || ! strncmp(VARDATA(type), "XDR", 3) ) { - byteorder = BIG_ENDIAN; + byteorder = XDR; } else { - byteorder = LITTLE_ENDIAN; + byteorder = NDR; } } @@ -494,7 +494,7 @@ Datum LWGEOM_recv(PG_FUNCTION_ARGS) #endif #ifdef DEBUG - elog(NOTICE, "LWGEOMFromWKB returned %s", unparse_WKB(SERIALIZED_FORM(result),pg_alloc,pg_free,-1)); + elog(NOTICE, "LWGEOM_from_bytea returned %s", unparse_WKB(SERIALIZED_FORM(result),pg_alloc,pg_free,-1)); #endif diff --git a/lwgeom/lwgparse.c b/lwgeom/lwgparse.c index 1768a86ff..6947ef5fa 100644 --- a/lwgeom/lwgparse.c +++ b/lwgeom/lwgparse.c @@ -21,16 +21,6 @@ void set_zm(char z, char m); void close_parser(void); -#undef LITTLE_ENDIAN -#define LITTLE_ENDIAN 1 -char getMachineEndian() -{ - static int endian_check_int = 1; // dont modify this!!! - - return *((char *) &endian_check_int); // 0 = big endian, 1 = little endian -} - - typedef unsigned long int4; typedef struct tag_tuple tuple; @@ -295,7 +285,7 @@ void WRITE_INT4(output_state * out,int4 val) { if ( val <= 0x7f ){ - if ( getMachineEndian() == LITTLE_ENDIAN ){ + if ( getMachineEndian() == NDR ){ val = (val<<1) | 1; } else{ @@ -306,7 +296,7 @@ WRITE_INT4(output_state * out,int4 val) the_geom.alloc_size-=3; } else{ - if ( getMachineEndian() == LITTLE_ENDIAN ){ + if ( getMachineEndian() == NDR ){ val <<=1; } WRITE_INT4_REAL(out,val); @@ -749,13 +739,9 @@ parse_wkb(const char** b) byte xdr = read_wkb_byte(b); swap_order=0; - if ( xdr == 0x01 ){ // wkb is in little endian - if ( getMachineEndian() != LITTLE_ENDIAN ) - swap_order=1; - } - else if ( xdr == 0x00 ){ // wkb is in big endian - if ( getMachineEndian() == LITTLE_ENDIAN ) - swap_order=1; + if ( xdr != getMachineEndian() ) + { + swap_order=1; } type = read_wkb_int(b); @@ -777,6 +763,12 @@ parse_wkb(const char** b) } else the_geom.hasM = 0; + if (type & WKBSRIDFLAG ) + { + the_geom.hasZ = 1; + the_geom.ndims++; + } + type &=0x0f; if ( the_geom.lwgi ){ diff --git a/lwgeom/wktparse.h b/lwgeom/wktparse.h index 2b13051fa..44743422b 100644 --- a/lwgeom/wktparse.h +++ b/lwgeom/wktparse.h @@ -68,7 +68,6 @@ void alloc_wkb(const char* parser); byte* parse_lwg(const char* wkt,allocator allocfunc,report_error errfunc); byte* parse_lwgi(const char* wkt,allocator allocfunc,report_error errfunc); char* unparse_WKT(byte* serialized, allocator alloc,freeor free); -char* unparse_WKB(byte* serialized, allocator alloc,freeor free, unsigned int endian); +char* unparse_WKB(byte* serialized, allocator alloc,freeor free, char endian); -char getMachineEndian(); diff --git a/lwgeom/wktunparse.c b/lwgeom/wktunparse.c index a76f624a5..6e6a3384a 100644 --- a/lwgeom/wktunparse.c +++ b/lwgeom/wktunparse.c @@ -21,22 +21,6 @@ #include "liblwgeom.h" -static int endian_check_int = 1; // dont modify this!!! - -#define LITTLE_ENDIAN_CHECK 1 -static unsigned int getMachineEndian() -{ - // 0 = big endian, 1 = little endian - if ( *((char *) &endian_check_int) ) - { - return LITTLE_ENDIAN; - } - else - { - return BIG_ENDIAN; - } -} - //-- Typedefs ---------------------------------------------- typedef unsigned long int4; @@ -133,7 +117,7 @@ read_int(byte** geom) { int4 ret; #ifdef SHRINK_INTS - if ( getMachineEndian() == LITTLE_ENDIAN_CHECK ){ + if ( getMachineEndian() == NDR ){ if( (**geom)& 0x01){ ret = **geom >>1; (*geom)++; @@ -151,7 +135,7 @@ read_int(byte** geom) memcpy(&ret,*geom,4); #ifdef SHRINK_INTS - if ( getMachineEndian() == LITTLE_ENDIAN_CHECK ){ + if ( getMachineEndian() == NDR ){ ret >>= 1; } #endif @@ -524,7 +508,7 @@ output_wkb(byte* geom) } char * -unparse_WKB(byte* serialized, allocator alloc, freeor free, unsigned int endian) +unparse_WKB(byte* serialized, allocator alloc, freeor free, char endian) { #ifdef DEBUG @@ -540,13 +524,14 @@ unparse_WKB(byte* serialized, allocator alloc, freeor free, unsigned int endian) out_start = out_pos = alloc(len); lwgi=0; - if ( endian == -1 ) endian = getMachineEndian(); - - if ( endian == LITTLE_ENDIAN) endianbyte=1; - else endianbyte=0; - - if ( endian != getMachineEndian() ) flipbytes = 1; - else flipbytes = 0; + if ( endian == -1 ) { + endianbyte = getMachineEndian(); + flipbytes = 0; + } else { + endianbyte = endian; + if ( endianbyte != getMachineEndian() ) flipbytes = 1; + else flipbytes = 0; + } output_wkb(serialized); ensure(1); @@ -558,6 +543,11 @@ unparse_WKB(byte* serialized, allocator alloc, freeor free, unsigned int endian) /****************************************************************** * $Log$ + * Revision 1.14 2004/12/17 11:08:53 strk + * Moved getMachineEndian from parser to liblwgeom.{h,c}. + * Added XDR and NDR defines. + * Fixed all usage of them. + * * Revision 1.13 2004/10/25 12:27:33 strk * Removed useless network type includes, * Added param.h include for BYTE_ORDER defines under win32. -- 2.40.0