Check-in Number:
|
4241 | |
Date: |
2001-Jul-24 15:52:41 (local)
2001-Jul-24 13:52:41 (UTC) |
User: | simons |
Branch: | |
Comment: |
If there are any ladies reading this: Please pardon my language in the
following paragraph!
Fuck snprintf(). Since apparently the Unix vendors are too bloody
stupid to provide a working snprintf() implementation that conforms to
an 8 year old standard, we don't use snprintf() at all but convert the
numbers ourselves. I re-implemented all XML callbacks to do maths on
their own. |
Tickets: |
|
Inspections: |
|
Files: |
ossp-pkg/xds/xml-decode-double.c
|
1.2
->
1.3
|
 
0 inserted, 2 deleted
|
ossp-pkg/xds/xml-decode-int32.c
|
1.1
->
1.2
|
 
41 inserted, 7 deleted
|
ossp-pkg/xds/xml-decode-int64.c
|
1.2
->
1.3
|
 
41 inserted, 7 deleted
|
ossp-pkg/xds/xml-decode-uint32.c
|
1.1
->
1.2
|
 
31 inserted, 7 deleted
|
ossp-pkg/xds/xml-decode-uint64.c
|
1.2
->
1.3
|
 
31 inserted, 7 deleted
|
ossp-pkg/xds/xml-encode-double.c
|
1.2
->
1.3
|
 
0 inserted, 2 deleted
|
ossp-pkg/xds/xml-encode-int32.c
|
1.2
->
1.3
|
 
40 inserted, 8 deleted
|
ossp-pkg/xds/xml-encode-int64.c
|
1.2
->
1.3
|
 
40 inserted, 8 deleted
|
ossp-pkg/xds/xml-encode-uint32.c
|
1.1
->
1.2
|
 
30 inserted, 8 deleted
|
ossp-pkg/xds/xml-encode-uint64.c
|
1.2
->
1.3
|
 
30 inserted, 8 deleted
|
|
ossp-pkg/xds/xml-decode-double.c 1.2 -> 1.3
--- xml-decode-double.c 2001/07/24 13:04:43 1.2
+++ xml-decode-double.c 2001/07/24 13:52:41 1.3
@@ -25,8 +25,6 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
-#include <assert.h>
#include "internal.h"
int xml_decode_double(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
|
|
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;
}
|
|
ossp-pkg/xds/xml-decode-int64.c 1.2 -> 1.3
--- xml-decode-int64.c 2001/07/23 16:40:38 1.2
+++ xml-decode-int64.c 2001/07/24 13:52:41 1.3
@@ -25,15 +25,16 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
#include <assert.h>
#include "internal.h"
int xml_decode_int64(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_int64_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, "<int64>", 7) != 0)
+ return XDS_ERR_TYPE_MISMATCH;
+
+ /* Decode the representation of the value. */
value = va_arg(*args, xds_int64_t*);
- rc = sscanf(buffer, "<int64>%lld</int64>%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, "</int64>", 8) != 0)
+ return XDS_ERR_TYPE_MISMATCH;
- return len;
+ return p + 8 - (char*)buffer;
}
|
|
ossp-pkg/xds/xml-decode-uint32.c 1.1 -> 1.2
--- xml-decode-uint32.c 2001/07/23 16:17:43 1.1
+++ xml-decode-uint32.c 2001/07/24 13:52:41 1.2
@@ -25,15 +25,15 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
#include <assert.h>
#include "internal.h"
int xml_decode_uint32(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_uint32_t* value;
- int len;
+ char* p;
/* Consistency checks. */
@@ -44,12 +44,36 @@
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 < 8 + 9 + 1)
+ return XDS_ERR_UNDERFLOW;
+
+ /* Does the opening XML tag match? */
+
+ if (strncmp(buffer, "<uint32>", 8) != 0)
+ return XDS_ERR_TYPE_MISMATCH;
+
+ /* Decode the representation of the value. */
value = va_arg(*args, xds_uint32_t*);
- rc = sscanf(buffer, "<uint32>%u</uint32>%n", value, &len);
- if (rc <= 0)
+ *value = 0;
+ p = (char*)buffer + 8;
+ while(isdigit(*p))
+ {
+ if (p >= (char*)buffer + buffer_size)
+ return XDS_ERR_UNDERFLOW;
+ *value *= 10;
+ *value += *p++ - '0';
+ }
+
+ /* Does the closing XML tag match? */
+
+ if (p+9 > (char*)buffer + buffer_size)
return XDS_ERR_UNDERFLOW;
+ else if (strncmp(p, "</uint32>", 9) != 0)
+ return XDS_ERR_TYPE_MISMATCH;
- return len;
+ return p + 9 - (char*)buffer;
}
|
|
ossp-pkg/xds/xml-decode-uint64.c 1.2 -> 1.3
--- xml-decode-uint64.c 2001/07/23 16:40:38 1.2
+++ xml-decode-uint64.c 2001/07/24 13:52:41 1.3
@@ -25,15 +25,15 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
#include <assert.h>
#include "internal.h"
int xml_decode_uint64(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_uint64_t* value;
- int len;
+ char* p;
/* Consistency checks. */
@@ -44,12 +44,36 @@
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 < 8 + 9 + 1)
+ return XDS_ERR_UNDERFLOW;
+
+ /* Does the opening XML tag match? */
+
+ if (strncmp(buffer, "<uint64>", 8) != 0)
+ return XDS_ERR_TYPE_MISMATCH;
+
+ /* Decode the representation of the value. */
value = va_arg(*args, xds_uint64_t*);
- rc = sscanf(buffer, "<uint64>%llu</uint64>%n", value, &len);
- if (rc <= 0)
+ *value = 0;
+ p = (char*)buffer + 8;
+ while(isdigit(*p))
+ {
+ if (p >= (char*)buffer + buffer_size)
+ return XDS_ERR_UNDERFLOW;
+ *value *= 10;
+ *value += *p++ - '0';
+ }
+
+ /* Does the closing XML tag match? */
+
+ if (p+9 > (char*)buffer + buffer_size)
return XDS_ERR_UNDERFLOW;
+ else if (strncmp(p, "</uint64>", 9) != 0)
+ return XDS_ERR_TYPE_MISMATCH;
- return len;
+ return p + 9 - (char*)buffer;
}
|
|
ossp-pkg/xds/xml-encode-double.c 1.2 -> 1.3
--- xml-encode-double.c 2001/07/24 13:04:43 1.2
+++ xml-encode-double.c 2001/07/24 13:52:41 1.3
@@ -25,8 +25,6 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
-#include <assert.h>
#include "internal.h"
int xml_encode_double(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
|
|
ossp-pkg/xds/xml-encode-int32.c 1.2 -> 1.3
--- xml-encode-int32.c 2001/07/23 15:57:47 1.2
+++ xml-encode-int32.c 2001/07/24 13:52:41 1.3
@@ -25,14 +25,17 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
#include <assert.h>
#include "internal.h"
int xml_encode_int32(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_int32_t value;
+ char buf[32];
+ size_t i, j;
+ char* p;
+ int negative;
/* Consistency checks. */
@@ -43,13 +46,42 @@
if (xds == NULL || buffer == NULL || buffer_size == 0 || args == NULL)
return XDS_ERR_INVALID_ARG;
- /* Format value into buffer. */
+ /* Format value into our buffer. */
value = va_arg(*args, xds_int32_t);
- rc = snprintf(buffer, buffer_size, "<int32>%d</int32>", value);
- if (rc < 0)
- return buffer_size*2;
- assert(rc >= 15);
+ if (value < 0)
+ {
+ negative = XDS_TRUE;
+ value = 0 - value;
+ }
+ else
+ negative = XDS_FALSE;
+ i = 0;
+ do
+ {
+ unsigned char remainder = value % 10;
+ value = value / 10;
+ buf[i++] = '0' + remainder;
+ }
+ while (value != 0);
+ if (negative)
+ buf[i++] = '-';
+
+ /* Check the buffer size. */
+
+ if (buffer_size < 7 + 8 + i)
+ return 7 + 8 + i;
+
+ /* Write result into the buffer. */
+
+ p = buffer;
+ strcpy(p, "<int32>");
+ p += 7;
+ for (j = i; j > 0; )
+ {
+ *p++ = buf[--j];
+ }
+ strcpy(p, "</int32>");
- return rc;
+ return 7 + 8 + i;
}
|
|
ossp-pkg/xds/xml-encode-int64.c 1.2 -> 1.3
--- xml-encode-int64.c 2001/07/23 16:40:38 1.2
+++ xml-encode-int64.c 2001/07/24 13:52:41 1.3
@@ -25,14 +25,17 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
#include <assert.h>
#include "internal.h"
int xml_encode_int64(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_int64_t value;
+ char buf[64];
+ size_t i, j;
+ char* p;
+ int negative;
/* Consistency checks. */
@@ -43,13 +46,42 @@
if (xds == NULL || buffer == NULL || buffer_size == 0 || args == NULL)
return XDS_ERR_INVALID_ARG;
- /* Format value into buffer. */
+ /* Format value into our buffer. */
value = va_arg(*args, xds_int64_t);
- rc = snprintf(buffer, buffer_size, "<int64>%lld</int64>", value);
- if (rc < 0)
- return buffer_size*2;
- assert(rc >= 15);
+ if (value < 0)
+ {
+ negative = XDS_TRUE;
+ value = 0 - value;
+ }
+ else
+ negative = XDS_FALSE;
+ i = 0;
+ do
+ {
+ unsigned char remainder = value % 10;
+ value = value / 10;
+ buf[i++] = '0' + remainder;
+ }
+ while (value != 0);
+ if (negative)
+ buf[i++] = '-';
+
+ /* Check the buffer size. */
+
+ if (buffer_size < 7 + 8 + i)
+ return 7 + 8 + i;
+
+ /* Write result into the buffer. */
+
+ p = buffer;
+ strcpy(p, "<int64>");
+ p += 7;
+ for (j = i; j > 0; )
+ {
+ *p++ = buf[--j];
+ }
+ strcpy(p, "</int64>");
- return rc;
+ return 7 + 8 + i;
}
|
|
ossp-pkg/xds/xml-encode-uint32.c 1.1 -> 1.2
--- xml-encode-uint32.c 2001/07/23 16:17:43 1.1
+++ xml-encode-uint32.c 2001/07/24 13:52:41 1.2
@@ -25,14 +25,16 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
#include <assert.h>
#include "internal.h"
int xml_encode_uint32(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_uint32_t value;
+ char buf[32];
+ size_t i, j;
+ char* p;
/* Consistency checks. */
@@ -43,13 +45,33 @@
if (xds == NULL || buffer == NULL || buffer_size == 0 || args == NULL)
return XDS_ERR_INVALID_ARG;
- /* Format value into buffer. */
+ /* Format value into our buffer. */
value = va_arg(*args, xds_uint32_t);
- rc = snprintf(buffer, buffer_size, "<uint32>%u</uint32>", value);
- if (rc < 0)
- return buffer_size*2;
- assert(rc >= 15);
+ i = 0;
+ do
+ {
+ unsigned char remainder = value % 10;
+ value = value / 10;
+ buf[i++] = '0' + remainder;
+ }
+ while (value != 0);
+
+ /* Check the buffer size. */
+
+ if (buffer_size < 8 + 9 + i)
+ return 8 + 9 + i;
+
+ /* Write result into the buffer. */
+
+ p = buffer;
+ strcpy(p, "<uint32>");
+ p += 8;
+ for (j = i; j > 0; )
+ {
+ *p++ = buf[--j];
+ }
+ strcpy(p, "</uint32>");
- return rc;
+ return 8 + 9 + i;
}
|
|
ossp-pkg/xds/xml-encode-uint64.c 1.2 -> 1.3
--- xml-encode-uint64.c 2001/07/23 16:40:38 1.2
+++ xml-encode-uint64.c 2001/07/24 13:52:41 1.3
@@ -25,14 +25,16 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
#include <assert.h>
#include "internal.h"
int xml_encode_uint64(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_uint64_t value;
+ char buf[64];
+ size_t i, j;
+ char* p;
/* Consistency checks. */
@@ -43,13 +45,33 @@
if (xds == NULL || buffer == NULL || buffer_size == 0 || args == NULL)
return XDS_ERR_INVALID_ARG;
- /* Format value into buffer. */
+ /* Format value into our buffer. */
value = va_arg(*args, xds_uint64_t);
- rc = snprintf(buffer, buffer_size, "<uint64>%llu</uint64>", value);
- if (rc < 0)
- return buffer_size*2;
- assert(rc >= 15);
+ i = 0;
+ do
+ {
+ unsigned char remainder = value % 10;
+ value = value / 10;
+ buf[i++] = '0' + remainder;
+ }
+ while (value != 0);
+
+ /* Check the buffer size. */
+
+ if (buffer_size < 8 + 9 + i)
+ return 8 + 9 + i;
+
+ /* Write result into the buffer. */
+
+ p = buffer;
+ strcpy(p, "<uint64>");
+ p += 8;
+ for (j = i; j > 0; )
+ {
+ *p++ = buf[--j];
+ }
+ strcpy(p, "</uint64>");
- return rc;
+ return 8 + 9 + i;
}
|
|