/* * SwapBytes? */ static inline int wkb_swap_bytes(uint8_t variant) { /* If requested variant matches machine arch, we don't have to swap! */ if ( ((variant & WKB_NDR) && (getMachineEndian() == NDR)) || ((! (variant & WKB_NDR)) && (getMachineEndian() == XDR)) ) { return LW_FALSE; } return LW_TRUE; }
/** * GEOMETRY * Generic handling for WKB geometries. The front of every WKB geometry * (including those embedded in collections) is an endian byte, a type * number and an optional srid number. We handle all those here, then pass * to the appropriate handler for the specific type. */ LWGEOM* lwgeom_from_wkb_state(wkb_parse_state *s) { char wkb_little_endian; uint32_t wkb_type; LWDEBUG(4,"Entered function"); /* Fail when handed incorrect starting byte */ wkb_little_endian = byte_from_wkb_state(s); if( wkb_little_endian != 1 && wkb_little_endian != 0 ) { LWDEBUG(4,"Leaving due to bad first byte!"); lwerror("Invalid endian flag value encountered."); return NULL; } /* Check the endianness of our input */ s->swap_bytes = LW_FALSE; if( getMachineEndian() == NDR ) /* Machine arch is little */ { if ( ! wkb_little_endian ) /* Data is big! */ s->swap_bytes = LW_TRUE; } else /* Machine arch is big */ { if ( wkb_little_endian ) /* Data is little! */ s->swap_bytes = LW_TRUE; } /* Read the type number */ wkb_type = integer_from_wkb_state(s); LWDEBUGF(4,"Got WKB type number: 0x%X", wkb_type); lwtype_from_wkb_state(s, wkb_type); /* Read the SRID, if necessary */ if( s->has_srid ) { s->srid = clamp_srid(integer_from_wkb_state(s)); /* TODO: warn on explicit UNKNOWN srid ? */ LWDEBUGF(4,"Got SRID: %u", s->srid); } /* Do the right thing */ switch( s->lwtype ) { case POINTTYPE: return (LWGEOM*)lwpoint_from_wkb_state(s); break; case LINETYPE: return (LWGEOM*)lwline_from_wkb_state(s); break; case CIRCSTRINGTYPE: return (LWGEOM*)lwcircstring_from_wkb_state(s); break; case POLYGONTYPE: return (LWGEOM*)lwpoly_from_wkb_state(s); break; case TRIANGLETYPE: return (LWGEOM*)lwtriangle_from_wkb_state(s); break; case CURVEPOLYTYPE: return (LWGEOM*)lwcurvepoly_from_wkb_state(s); break; case MULTIPOINTTYPE: case MULTILINETYPE: case MULTIPOLYGONTYPE: case COMPOUNDTYPE: case MULTICURVETYPE: case MULTISURFACETYPE: case POLYHEDRALSURFACETYPE: case TINTYPE: case COLLECTIONTYPE: return (LWGEOM*)lwcollection_from_wkb_state(s); break; /* Unknown type! */ default: lwerror("Unsupported geometry type: %s [%d]", lwtype_name(s->lwtype), s->lwtype); } /* Return value to keep compiler happy. */ return NULL; }