]> granicus.if.org Git - postgis/commitdiff
Fixed memory alignment bug in base geometry type serializers, added integrity check...
authorSandro Santilli <strk@keybit.net>
Fri, 11 Nov 2005 17:45:53 +0000 (17:45 +0000)
committerSandro Santilli <strk@keybit.net>
Fri, 11 Nov 2005 17:45:53 +0000 (17:45 +0000)
git-svn-id: http://svn.osgeo.org/postgis/branches/pgis_1_0@2031 b70326c6-7e19-0410-871a-916f4a2858ee

lwgeom/lwline.c
lwgeom/lwpoint.c
lwgeom/lwpoly.c

index d0d6b1c8971cc792373b70b0eba6c7ce38d574be..543c9ab4b34fa68fb6d20dc3dc20f52dd9844a44 100644 (file)
@@ -121,8 +121,8 @@ lwline_serialize_buf(LWLINE *line, uchar *buf, size_t *retsize)
 {
        char hasSRID;
        uchar *loc;
-       int ptsize = pointArray_ptsize(line->points);
-       unsigned int u;
+       int ptsize;
+       size_t size;
 
 #ifdef PGIS_DEBUG_CALLS
        lwnotice("lwline_serialize_buf(%p, %p, %p) called",
@@ -132,6 +132,11 @@ lwline_serialize_buf(LWLINE *line, uchar *buf, size_t *retsize)
        if (line == NULL)
                lwerror("lwline_serialize:: given null line");
 
+       if ( TYPE_GETZM(line->type) != TYPE_GETZM(line->points->dims) )
+               lwerror("Dimensions mismatch in lwline");
+
+       ptsize = pointArray_ptsize(line->points);
+
        hasSRID = (line->SRID != -1);
 
        buf[0] = (uchar) lwgeom_makeType_full(
@@ -170,41 +175,9 @@ lwline_serialize_buf(LWLINE *line, uchar *buf, size_t *retsize)
 #endif
 
        //copy in points
-       if (TYPE_NDIMS(line->type) == 3)
-       {
-               if ( TYPE_HASZ(line->type) )
-               {
-                       for (u=0; u<line->points->npoints; u++)
-                       {
-                               getPoint3dz_p(line->points, u, (POINT3DZ *)loc);
-                               loc += ptsize;
-                       }
-               }
-               else
-               {
-                       for (u=0; u<line->points->npoints; u++)
-                       {
-                               getPoint3dm_p(line->points, u, (POINT3DM *)loc);
-                               loc += ptsize;
-                       }
-               }
-       }
-       else if (TYPE_NDIMS(line->type) == 2)
-       {
-                       for (u=0; u<line->points->npoints; u++)
-                       {
-                               getPoint2d_p(line->points, u, (POINT2D *)loc);
-                               loc+= 16;
-                       }
-       }
-       else if (TYPE_NDIMS(line->type) == 4)
-       {
-                       for (u=0; u<line->points->npoints; u++)
-                       {
-                               getPoint4d_p(line->points, u, (POINT4D *)loc);
-                               loc+= 32;
-                       }
-       }
+       size = line->points->npoints*ptsize;
+       memcpy(loc, getPoint_internal(line->points, 0), size);
+       loc += size;
 
 #ifdef PGIS_DEBUG
        lwnotice("lwline_serialize_buf copied serialized_pointlist (%d bytes)",
index 94beba3349e550365aa0e6ac81e188850b6c4d6e..3abd81e860355aa18e19cea685f5b314c7881bd3 100644 (file)
@@ -35,6 +35,10 @@ lwpoint_serialize_buf(LWPOINT *point, uchar *buf, size_t *retsize)
        int size=1;
        char hasSRID;
        uchar *loc;
+       int ptsize = pointArray_ptsize(point->point);
+
+       if ( TYPE_GETZM(point->type) != TYPE_GETZM(point->point->dims) )
+               lwerror("Dimensions mismatch in lwpoint");
 
        //printLWPOINT(point);
 #ifdef PGIS_DEBUG_CALLS
@@ -66,18 +70,7 @@ lwpoint_serialize_buf(LWPOINT *point, uchar *buf, size_t *retsize)
        }
 
        //copy in points
-
-       if (TYPE_NDIMS(point->type) == 3)
-       {
-               if (TYPE_HASZ(point->type))
-                       getPoint3dz_p(point->point, 0, (POINT3DZ *)loc);
-               else
-                       getPoint3dm_p(point->point, 0, (POINT3DM *)loc);
-       }
-       else if (TYPE_NDIMS(point->type) == 2)
-               getPoint2d_p(point->point, 0, (POINT2D *)loc);
-       else if (TYPE_NDIMS(point->type) == 4)
-               getPoint4d_p(point->point, 0, (POINT4D *)loc);
+       memcpy(loc, getPoint_internal(point->point, 0), ptsize);
 
        if (retsize) *retsize = size;
 }
index a10f0a724db75624c577da6981c64b76ba4a5dd6..8df7924f498db6af45a8a1ad1b47e3388d5793fc 100644 (file)
@@ -153,27 +153,23 @@ lwpoly_serialize(LWPOLY *poly)
 void
 lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
 {
-       int size=1;  // type byte
+       size_t size=1;  // type byte
        char hasSRID;
-       int t,u;
-       int total_points = 0;
-       int npoints;
+       int t;
        uchar *loc;
+       int ptsize;
 
 #ifdef PGIS_DEBUG_CALLS
        lwnotice("lwpoly_serialize_buf called");
 #endif
 
+       ptsize = sizeof(double)*TYPE_NDIMS(poly->type);
+
        hasSRID = (poly->SRID != -1);
 
        size += 4; // nrings
        size += 4*poly->nrings; //npoints/ring
 
-       for (t=0;t<poly->nrings;t++) {
-               total_points  += poly->rings[t]->npoints;
-       }
-       size += sizeof(double)*TYPE_NDIMS(poly->type)*total_points;
-
        buf[0] = (uchar) lwgeom_makeType_full(
                TYPE_HASZ(poly->type), TYPE_HASM(poly->type),
                hasSRID, POLYGONTYPE, poly->bbox ? 1 : 0);
@@ -199,44 +195,24 @@ lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
        for (t=0;t<poly->nrings;t++)
        {
                POINTARRAY *pa = poly->rings[t];
-               npoints = poly->rings[t]->npoints;
-               memcpy(loc, &npoints, sizeof(int32)); //npoints this ring
+               size_t pasize;
+               uint32 npoints;
+
+               if ( TYPE_GETZM(poly->type) != TYPE_GETZM(pa->dims) )
+                       lwerror("Dimensions mismatch in lwpoly");
+
+               npoints = pa->npoints;
+
+               memcpy(loc, &npoints, sizeof(uint32)); //npoints this ring
                loc+=4;
-               if (TYPE_NDIMS(poly->type) == 3)
-               {
-                       if ( TYPE_HASZ(poly->type) )
-                       {
-                               for (u=0;u<npoints;u++)
-                               {
-                                       getPoint3dz_p(pa, u, (POINT3DZ *)loc);
-                                       loc+= 24;
-                               }
-                       }
-                       else
-                       {
-                               for (u=0;u<npoints;u++)
-                               {
-                                       getPoint3dm_p(pa, u, (POINT3DM *)loc);
-                                       loc+= 24;
-                               }
-                       }
-               }
-               else if (TYPE_NDIMS(poly->type) == 2)
-               {
-                       for (u=0;u<npoints;u++)
-                       {
-                               getPoint2d_p(pa, u, (POINT2D *)loc);
-                               loc+= 16;
-                       }
-               }
-               else if (TYPE_NDIMS(poly->type) == 4)
-               {
-                       for (u=0;u<npoints;u++)
-                       {
-                               getPoint4d_p(pa, u, (POINT4D *)loc);
-                               loc+= 32;
-                       }
-               }
+
+               pasize = npoints*ptsize;
+               size += pasize;
+
+               // copy points
+               memcpy(loc, getPoint_internal(pa, 0), pasize);
+               loc += pasize;
+
        }
 
        if (retsize) *retsize = size;