The "safe" encoding replaces each unsafe byte with \x<hex>, the new
function mbs_safe_decoded_size() calculates the original size of the
string.
Signed-off-by: Karel Zak <kzak@redhat.com>
extern char *mbs_safe_encode(const char *s, size_t *width);
extern char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf, const char *safechars);
extern size_t mbs_safe_encode_size(size_t bytes);
+extern size_t mbs_safe_decode_size(const char *s);
extern char *mbs_invalid_encode(const char *s, size_t *width);
extern char *mbs_invalid_encode_to_buffer(const char *s, size_t *width, char *buf);
return buf;
}
+/*
+ * Guess size
+ */
size_t mbs_safe_encode_size(size_t bytes)
{
return (bytes * 4) + 1;
}
+/*
+ * Count size of the original string in bytes (count \x?? as one byte)
+ */
+size_t mbs_safe_decode_size(const char *p)
+{
+ size_t bytes = 0;
+
+ while (p && *p) {
+ if (*p == '\\' && *(p + 1) == 'x' &&
+ isxdigit(*(p + 2)) && isxdigit(*(p + 3)))
+ p += 4;
+ else
+ p++;
+ bytes++;
+ }
+ return bytes;
+}
+
/*
* Returns allocated string where all control and non-printable chars are
* replaced with \x?? hex sequence.