OSSP CVS Repository

ossp - Difference in ossp-pkg/xds/xml-decode-int32.c versions 1.1 and 1.2
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/xds/xml-decode-int32.c 1.1 -> 1.2

--- xml-decode-int32.c   2001/07/23 15:57:47     1.1
+++ xml-decode-int32.c   2001/07/24 13:52:41     1.2
@@ -25,15 +25,16 @@
    SUCH DAMAGE.
 */
 
-#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
 #include <assert.h>
 #include "internal.h"
 
 int xml_decode_int32(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
     {
-    int rc;
     xds_int32_t* value;
-    int len;
+    char* p;
+    int negative;
 
     /* Consistency checks. */
 
@@ -44,12 +45,45 @@
     if (xds == NULL || buffer == NULL || buffer_size == 0 || args == NULL)
         return XDS_ERR_INVALID_ARG;
 
-    /* Format value into buffer. */
+    /* The buffer must contain at least opening tag, closing tag and
+       one digit. Check whether it is large enough. */
+
+    if (buffer_size < 7 + 8 + 1)
+        return XDS_ERR_UNDERFLOW;
+
+    /* Does the opening XML tag match? */
+
+    if (strncmp(buffer, "<int32>", 7) != 0)
+        return XDS_ERR_TYPE_MISMATCH;
+
+    /* Decode the representation of the value. */
 
     value = va_arg(*args, xds_int32_t*);
-    rc = sscanf(buffer, "<int32>%d</int32>%n", value, &len);
-    if (rc <= 0)
+    *value = 0;
+    p = (char*)buffer + 7;
+    if (*p == '-')
+        {
+        negative = XDS_TRUE;
+        ++p;
+        }
+    else
+        negative = XDS_FALSE;
+    while(isdigit(*p))
+        {
+        if (p >= (char*)buffer + buffer_size)
+            return XDS_ERR_UNDERFLOW;
+        *value *= 10;
+        *value += *p++ - '0';
+        }
+    if (negative)
+        *value = 0 - *value;
+
+    /* Does the closing XML tag match? */
+
+    if (p+8 > (char*)buffer + buffer_size)
         return XDS_ERR_UNDERFLOW;
+    else if (strncmp(p, "</int32>", 8) != 0)
+        return XDS_ERR_TYPE_MISMATCH;
 
-    return len;
+    return p + 8 - (char*)buffer;
     }

CVSTrac 2.0.1