diff --git a/libc3/buf.c b/libc3/buf.c
index 7215974..ff2ef7f 100644
--- a/libc3/buf.c
+++ b/libc3/buf.c
@@ -20,9 +20,6 @@
#include "str.h"
#include "sym.h"
-const sw buf_u8_to_hex_size = 2;
-const sw buf_inspect_str_byte_size = 4;
-
void buf_clean (s_buf *buf)
{
assert(buf);
@@ -748,7 +745,7 @@ sw buf_str_to_hex (s_buf *buf, const s_str *src)
b = src->ptr.pu8;
i = 0;
while (i++ < src->size)
- buf_u8_to_hex(buf, *(b++));
+ buf_u8_to_hex(buf, b++);
return size;
}
@@ -761,24 +758,41 @@ s_str * buf_to_str (const s_buf *buf, s_str *str)
return str_init(str, free, buf->size, buf->ptr.p);
}
-sw buf_u8_to_hex (s_buf *buf, u8 x)
+sw buf_u8_to_hex (s_buf *buf, const u8 *x)
{
u8 digit;
- if (buf->wpos + buf_u8_to_hex_size > buf->size) {
- assert(! "buffer overflow");
- return -1;
- }
- digit = x >> 4;
+ sw r;
+ sw result = 0;
+ s_buf_save save;
+ buf_save_init(buf, &save);
+ digit = *x >> 4;
if (digit < 10)
- buf_write_u8(buf, digit + '0');
+ r = buf_write_u8(buf, digit + '0');
else
- buf_write_u8(buf, digit - 10 + 'A');
- digit = x & 0xF;
+ r = buf_write_u8(buf, digit - 10 + 'A');
+ if (r <= 0)
+ goto clean;
+ result += r;
+ digit = *x & 0xF;
if (digit < 10)
- buf_write_u8(buf, digit + '0');
+ r = buf_write_u8(buf, digit + '0');
else
- buf_write_u8(buf, digit - 10 + 'A');
- return buf_u8_to_hex_size;
+ r = buf_write_u8(buf, digit - 10 + 'A');
+ if (r <= 0)
+ goto restore;
+ result += r;
+ r = result;
+ restore:
+ buf_save_restore_wpos(buf, &save);
+ clean:
+ buf_save_clean(buf, &save);
+ return r;
+}
+
+sw buf_u8_to_hex_size(const u8 *u)
+{
+ (void) u;
+ return 2;
}
sw buf_vf (s_buf *buf, const char *fmt, va_list ap)
diff --git a/libc3/buf.h b/libc3/buf.h
index 1258819..f145112 100644
--- a/libc3/buf.h
+++ b/libc3/buf.h
@@ -27,8 +27,6 @@
#define BUF_SIZE 10240
-extern const sw buf_u8_to_hex_size;
-
/* Stack-allocation compatible functions, call buf_clean after use. */
void buf_clean (s_buf *buf);
s_buf * buf_init (s_buf *buf, bool free, uw size, s8 *p);
@@ -84,7 +82,8 @@ s_buf * buf_restore (s_buf *buf, const s_buf *save);
sw buf_seek (s_buf *buf, sw offset, u8 whence);
sw buf_str_to_hex (s_buf *buf, const s_str *src);
sw buf_str_to_hex_size (const s_str *src);
-sw buf_u8_to_hex (s_buf *buf, u8 x);
+sw buf_u8_to_hex (s_buf *buf, const u8 *x);
+sw buf_u8_to_hex_size (const u8 *x);
sw buf_vf (s_buf *buf, const char *fmt, va_list ap);
sw buf_write_1 (s_buf *buf, const s8 *p);
sw buf_write_character_utf8 (s_buf *buf, character c);
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index c1224ee..9bc60d0 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -1026,16 +1026,15 @@ sw buf_inspect_str_byte (s_buf *buf, const u8 *byte)
sw result = 0;
s_buf_save save;
buf_save_init(buf, &save);
- if ((r = buf_write_u8(buf, '\\')) != 1) {
- if (r >= 0)
- r = -1;
+ if ((r = buf_write_1(buf, "\\")) <= 0) {
+ r = -1;
goto clean;
}
result += r;
- if ((r = buf_write_u8(buf, 'x')) != 1)
+ if ((r = buf_write_1(buf, "x")) <= 0)
goto restore;
result += r;
- if ((r = buf_u8_to_hex(buf, *byte)) != 2)
+ if ((r = buf_u8_to_hex(buf, byte)) != 2)
goto restore;
result += r;
r = result;
@@ -1049,6 +1048,20 @@ sw buf_inspect_str_byte (s_buf *buf, const u8 *byte)
return r;
}
+sw buf_inspect_str_byte_size (const u8 *byte)
+{
+ sw r;
+ sw result = 0;
+ (void) byte;
+ r = strlen("\\");
+ result += r;
+ r = strlen("x");
+ result += r;
+ r = 2;
+ result += r;
+ return result;
+}
+
sw buf_inspect_str_character (s_buf *buf, const character *c)
{
s8 b[4];
@@ -1083,14 +1096,14 @@ sw buf_inspect_str_character (s_buf *buf, const character *c)
if ((r = buf_write_u8(buf, 'x')) != 1)
goto restore;
result1 += r;
- if ((r = buf_u8_to_hex(buf, char_buf.ptr.pu8[j++])) != 2)
+ if ((r = buf_u8_to_hex(buf, &char_buf.ptr.pu8[j++])) != 2)
goto restore;
result1 += r;
while (i--) {
if ((r = buf_write_1(buf, "\\x")) != 2)
goto restore;
result1 += r;
- if ((r = buf_u8_to_hex(buf, char_buf.ptr.pu8[j++])) != 2)
+ if ((r = buf_u8_to_hex(buf, &char_buf.ptr.pu8[j++])) != 2)
goto restore;
result1 += r;
}
@@ -1204,7 +1217,7 @@ sw buf_inspect_str_reserved_size (const s_str *str)
else if ((r = str_read_u8(&s, &byte)) < 0)
goto restore;
else if (r) {
- r = buf_inspect_str_byte_size;
+ r = buf_inspect_str_byte_size(&byte);
result += r;
}
}