From 21169393e3aec30f0c67037b9a0afb292b7f99ae Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 10 Mar 2010 23:54:11 +0100 Subject: [PATCH] lib: add mangle.c for mtab (fstab or swaps) encoding Signed-off-by: Karel Zak --- include/Makefile.am | 1 + include/mangle.h | 14 ++++++ lib/Makefile.am | 3 +- lib/mangle.c | 116 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 include/mangle.h create mode 100644 lib/mangle.c diff --git a/include/Makefile.am b/include/Makefile.am index 525e3d2e..1da31fe1 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -20,4 +20,5 @@ dist_noinst_HEADERS = \ wholedisk.h \ widechar.h \ crc32.h \ + mangle.h \ xstrncpy.h diff --git a/include/mangle.h b/include/mangle.h new file mode 100644 index 00000000..8ce38752 --- /dev/null +++ b/include/mangle.h @@ -0,0 +1,14 @@ +#ifndef UTIL_LINUX_MANGLE_H +#define UTIL_LINUX_MANGLE_H + +/* + * Functions for \oct encoding used in mtab/fstab/swaps/etc. + */ + +extern char *mangle(const char *s); + +extern void unmangle_to_buffer(char *s, char *buf, size_t len); +extern char *unmangle(char *s); + +#endif /* UTIL_LINUX_MANGLE_H */ + diff --git a/lib/Makefile.am b/lib/Makefile.am index 5600d18c..0fddfb5e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -2,11 +2,12 @@ include $(top_srcdir)/config/include-Makefile.am AM_CPPFLAGS += -DTEST_PROGRAM -noinst_PROGRAMS = test_blkdev test_ismounted test_wholedisk +noinst_PROGRAMS = test_blkdev test_ismounted test_wholedisk test_mangle test_blkdev_SOURCES = blkdev.c test_ismounted_SOURCES = ismounted.c test_wholedisk_SOURCES = wholedisk.c +test_mangle_SOURCES = mangle.c if LINUX test_blkdev_SOURCES += linux_version.c diff --git a/lib/mangle.c b/lib/mangle.c new file mode 100644 index 00000000..766d479d --- /dev/null +++ b/lib/mangle.c @@ -0,0 +1,116 @@ +/* + * Functions for \oct encoding used in mtab/fstab/swaps/etc. + * + * Based on code from mount(8). + * + * Copyright (C) 2010 Karel Zak + */ +#include +#include +#include +#include + +#include "mangle.h" + +#define isoctal(a) (((a) & ~7) == '0') + +static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' }; + +char *mangle(const char *s) +{ + char *ss, *sp; + int n; + + n = strlen(s); + ss = sp = malloc(4*n+1); + if (!sp) + return NULL; + while(1) { + for (n = 0; n < sizeof(need_escaping); n++) { + if (*s == need_escaping[n]) { + *sp++ = '\\'; + *sp++ = '0' + ((*s & 0300) >> 6); + *sp++ = '0' + ((*s & 070) >> 3); + *sp++ = '0' + (*s & 07); + goto next; + } + } + *sp++ = *s; + if (*s == 0) + break; + next: + s++; + } + return ss; +} + +void unmangle_to_buffer(char *s, char *buf, size_t len) +{ + size_t sz = 0; + + while(*s && sz < len - 1) { + if (*s == '\\' && sz + 4 < len - 1 && isoctal(s[1]) && + isoctal(s[2]) && isoctal(s[3])) { + + *buf++ = 64*(s[1] & 7) + 8*(s[2] & 7) + (s[3] & 7); + s += 4; + sz += 4; + } else { + *buf++ = *s++; + sz++; + } + } + *buf = '\0'; +} + +static inline char *skip_nonspaces(char *s) +{ + while (*s && !(*s == ' ' || *s == '\t')) + s++; + return s; +} + +/* + * Returns mallocated buffer or NULL in case of error. + */ +char *unmangle(char *s) +{ + char *buf, *end; + size_t sz; + + end = skip_nonspaces(s); + sz = end - s + 1; + + buf = malloc(sz); + if (!buf) + return NULL; + + unmangle_to_buffer(s, buf, sz); + return buf; +} + +#ifdef TEST_PROGRAM +#include +#include +int main(int argc, char *argv[]) +{ + if (argc < 3) { + fprintf(stderr, "usage: %s --mangle | --unmangle \n", + program_invocation_short_name); + return EXIT_FAILURE; + } + + if (!strcmp(argv[1], "--mangle")) + printf("mangled: '%s'\n", mangle(argv[2])); + + else if (!strcmp(argv[1], "--unmangle")) { + char *x = unmangle(argv[2]); + if (x) + printf("unmangled: '%s'\n", x); + else + err(EXIT_FAILURE, "unmangle failed"); + } + + return EXIT_SUCCESS; +} +#endif /* TEST_PROGRAM */ -- 2.39.5