diff options
author | Karel Zak | 2014-04-15 13:58:34 +0200 |
---|---|---|
committer | Karel Zak | 2014-04-15 13:58:34 +0200 |
commit | c426f70fd4ad97b0d73263893f0ae98174dd1458 (patch) | |
tree | 1ab13f15dccfc14068bfcea47522a3b3f2729f22 /lib | |
parent | libblkid: fix return codes from PART_ENTRY_* probing (diff) | |
download | kernel-qcow2-util-linux-c426f70fd4ad97b0d73263893f0ae98174dd1458.tar.gz kernel-qcow2-util-linux-c426f70fd4ad97b0d73263893f0ae98174dd1458.tar.xz kernel-qcow2-util-linux-c426f70fd4ad97b0d73263893f0ae98174dd1458.zip |
lib/mbsalign: split mbs_safe_encode()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mbsalign.c | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/lib/mbsalign.c b/lib/mbsalign.c index 5933f1863..ef59f41ed 100644 --- a/lib/mbsalign.c +++ b/lib/mbsalign.c @@ -85,27 +85,25 @@ size_t mbs_safe_width(const char *s) } /* - * Returns allocated string where all control and non-printable chars are - * replaced with \x?? hex sequence. + * Copy @s to @buf and replace control and non-printable chars with + * \x?? hex sequence. The @width returns number of cells. + * + * The @buf has to be big enough to store mbs_safe_encode_size(strlen(s))) + * bytes. */ -char *mbs_safe_encode(const char *s, size_t *width) +char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf) { mbstate_t st; const char *p = s; - char *res, *r; + char *r; size_t sz = s ? strlen(s) : 0; - - if (!sz) + if (!sz || !buf) return NULL; memset(&st, 0, sizeof(st)); - res = malloc((sz * 4) + 1); - if (!res) - return NULL; - - r = res; + r = buf; *width = 0; while (p && *p) { @@ -166,7 +164,30 @@ char *mbs_safe_encode(const char *s, size_t *width) *r = '\0'; - return res; + return buf; +} + +size_t mbs_safe_encode_size(size_t bytes) +{ + return (bytes * 4) + 1; +} + +/* + * Returns allocated string where all control and non-printable chars are + * replaced with \x?? hex sequence. + */ +char *mbs_safe_encode(const char *s, size_t *width) +{ + size_t sz = s ? strlen(s) : 0; + char *buf; + + if (!sz) + return NULL; + buf = malloc(mbs_safe_encode_size(sz)); + if (!buf) + return NULL; + + return mbs_safe_encode_to_buffer(s, width, buf); } static bool |