summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/raptor_internal.h1
-rw-r--r--src/raptor_iostream.c18
-rw-r--r--src/raptor_xml.c13
-rw-r--r--src/snprintf.c50
4 files changed, 62 insertions, 20 deletions
diff --git a/src/raptor_internal.h b/src/raptor_internal.h
index 79bcc3c0..71a773eb 100644
--- a/src/raptor_internal.h
+++ b/src/raptor_internal.h
@@ -1325,6 +1325,7 @@ int raptor_rdfxmla_serialize_set_write_typed_nodes(raptor_serializer* serializer
/* snprintf.c */
char* raptor_format_float(char *buffer, size_t *currlen, size_t maxlen, double fvalue, unsigned int min, unsigned int max, int flags);
int raptor_format_integer(char* buffer, size_t bufsize, int integer);
+int raptor_format_hexadecimal(char* buffer, size_t bufsize, unsigned int integer, int width);
/* raptor_world structure */
#define RAPTOR1_WORLD_MAGIC_1 0
diff --git a/src/raptor_iostream.c b/src/raptor_iostream.c
index 4b65fac5..532c513d 100644
--- a/src/raptor_iostream.c
+++ b/src/raptor_iostream.c
@@ -917,26 +917,18 @@ int
raptor_iostream_hexadecimal_write(unsigned int integer, int width,
raptor_iostream* iostr)
{
- unsigned char *buf;
- unsigned char *p;
+ char *buf;
int rc;
- if(width <1)
+ if(width < 1)
return 1;
- buf = RAPTOR_MALLOC(unsigned char*, width);
+ buf = RAPTOR_MALLOC(char*, width + 1);
if(!buf)
return 1;
- p = buf+width-1;
- do {
- unsigned int digit = (integer & 15);
- *p-- =(digit < 10) ? '0'+digit : 'A'+(digit-10);
- integer >>= 4;
- } while(integer);
- while(p >= buf)
- *p-- = '0';
-
+ (void)raptor_format_hexadecimal(buf, width + 1, integer, width);
+
rc = raptor_iostream_write_bytes(buf, 1, width, iostr);
RAPTOR_FREE(char*, buf);
return rc;
diff --git a/src/raptor_xml.c b/src/raptor_xml.c
index e424a519..101764b3 100644
--- a/src/raptor_xml.c
+++ b/src/raptor_xml.c
@@ -711,12 +711,13 @@ raptor_xml_escape_string_any(raptor_world *world,
unichar);
} else {
/* &#xX; */
- *q++='&';
- *q++='#';
- *q++='x';
- sprintf((char*)q, "%X", (unsigned int)unichar);
- q+= (unichar < 0x10) ? 1 : 2;
- *q++=';';
+ *q++ = '&';
+ *q++ = '#';
+ *q++ = 'x';
+ q += raptor_format_hexadecimal((char*)q, 3,
+ RAPTOR_GOOD_CAST(unsigned int, unichar),
+ (unichar < 0x10) ? 2 : 3);
+ *q++ = ';';
}
} else {
memcpy(q, p, unichar_len);
diff --git a/src/snprintf.c b/src/snprintf.c
index 0d7c858a..41b656fb 100644
--- a/src/snprintf.c
+++ b/src/snprintf.c
@@ -392,7 +392,7 @@ raptor_vasprintf(char **ret, const char *format, va_list arguments)
}
-static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* raptor_format_integer:
@@ -444,3 +444,51 @@ raptor_format_integer(char* buffer, size_t bufsize, int integer)
return len;
}
+
+
+
+/**
+ * raptor_format_hexadecimal:
+ * @buffer: buffer (or NULL)
+ * @bufsize: size of above (or 0)
+ * @integer: unsigned integer value to format
+ * @width: width of output
+ *
+ * INTERNAL - Format an integer as uppercase hexadecimal into a
+ * buffer or calculate the size needed.
+ *
+ * Works Like the C99 snprintf() but just for hexadecimal integers and
+ * always writes @width hex digits.
+ *
+ * If @buffer is NULL or the @bufsize is too small, the number of
+ * bytes needed (excluding NUL) is returned and no formatting is done.
+ *
+ * Return value: number of bytes needed or written (excluding NUL)
+ */
+int
+raptor_format_hexadecimal(char* buffer, size_t bufsize,
+ unsigned int integer, int width)
+{
+ char *p;
+
+ if(width < 1)
+ return 1;
+
+ if(!buffer || bufsize < RAPTOR_GOOD_CAST(size_t, (width + 1))) /* for NUL */
+ return width;
+
+ p = &buffer[width];
+ *p-- = '\0';
+ do {
+ *p-- = digits[integer & 15];
+ integer >>= 4;
+ } while(integer);
+
+ while(p >= buffer)
+ *p-- = '0';
+
+ return width;
+}
+
+
+