bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Functions for \oct encoding used in mtab/fstab/swaps/etc. |
| 3 | * |
| 4 | * Based on code from mount(8). |
| 5 | * |
| 6 | * Copyright (C) 2010 Karel Zak <kzak@redhat.com> |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <ctype.h> |
| 12 | |
| 13 | #include "mangle.h" |
| 14 | #include "c.h" |
| 15 | |
| 16 | #define isoctal(a) (((a) & ~7) == '0') |
| 17 | |
| 18 | #define from_hex(c) (isdigit(c) ? c - '0' : tolower(c) - 'a' + 10) |
| 19 | |
| 20 | #define is_unwanted_char(x) (strchr(" \t\n\\", (unsigned int) x) != NULL) |
| 21 | |
| 22 | |
| 23 | char *mangle(const char *s) |
| 24 | { |
| 25 | char *ss, *sp; |
| 26 | |
| 27 | if (!s) |
| 28 | return NULL; |
| 29 | |
| 30 | ss = sp = malloc(4 * strlen(s) + 1); |
| 31 | if (!sp) |
| 32 | return NULL; |
| 33 | while(1) { |
| 34 | if (!*s) { |
| 35 | *sp = '\0'; |
| 36 | break; |
| 37 | } |
| 38 | if (is_unwanted_char(*s)) { |
| 39 | *sp++ = '\\'; |
| 40 | *sp++ = '0' + ((*s & 0300) >> 6); |
| 41 | *sp++ = '0' + ((*s & 070) >> 3); |
| 42 | *sp++ = '0' + (*s & 07); |
| 43 | } else |
| 44 | *sp++ = *s; |
| 45 | s++; |
| 46 | } |
| 47 | return ss; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void unmangle_to_buffer(const char *s, char *buf, size_t len) |
| 52 | { |
| 53 | size_t sz = 0; |
| 54 | |
| 55 | if (!s) |
| 56 | return; |
| 57 | |
| 58 | while(*s && sz < len - 1) { |
| 59 | if (*s == '\\' && sz + 3 < len - 1 && isoctal(s[1]) && |
| 60 | isoctal(s[2]) && isoctal(s[3])) { |
| 61 | |
| 62 | *buf++ = 64*(s[1] & 7) + 8*(s[2] & 7) + (s[3] & 7); |
| 63 | s += 4; |
| 64 | sz += 4; |
| 65 | } else { |
| 66 | *buf++ = *s++; |
| 67 | sz++; |
| 68 | } |
| 69 | } |
| 70 | *buf = '\0'; |
| 71 | } |
| 72 | |
| 73 | void unhexmangle_to_buffer(const char *s, char *buf, size_t len) |
| 74 | { |
| 75 | size_t sz = 0; |
| 76 | |
| 77 | if (!s) |
| 78 | return; |
| 79 | |
| 80 | while(*s && sz < len - 1) { |
| 81 | if (*s == '\\' && sz + 3 < len - 1 && s[1] == 'x' && |
| 82 | isxdigit(s[2]) && isxdigit(s[3])) { |
| 83 | |
| 84 | *buf++ = from_hex(s[2]) << 4 | from_hex(s[3]); |
| 85 | s += 4; |
| 86 | sz += 4; |
| 87 | } else { |
| 88 | *buf++ = *s++; |
| 89 | sz++; |
| 90 | } |
| 91 | } |
| 92 | *buf = '\0'; |
| 93 | } |
| 94 | |
| 95 | static inline char *skip_nonspaces(const char *s) |
| 96 | { |
| 97 | while (*s && !(*s == ' ' || *s == '\t')) |
| 98 | s++; |
| 99 | return (char *) s; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Returns mallocated buffer or NULL in case of error. |
| 104 | */ |
| 105 | char *unmangle(const char *s, char **end) |
| 106 | { |
| 107 | char *buf; |
| 108 | char *e; |
| 109 | size_t sz; |
| 110 | |
| 111 | if (!s) |
| 112 | return NULL; |
| 113 | |
| 114 | e = skip_nonspaces(s); |
| 115 | sz = e - s + 1; |
| 116 | |
| 117 | if (end) |
| 118 | *end = e; |
| 119 | if (e == s) |
| 120 | return NULL; /* empty string */ |
| 121 | |
| 122 | buf = malloc(sz); |
| 123 | if (!buf) |
| 124 | return NULL; |
| 125 | |
| 126 | unmangle_to_buffer(s, buf, sz); |
| 127 | return buf; |
| 128 | } |
| 129 | |
| 130 | #ifdef TEST_PROGRAM |
| 131 | #include <errno.h> |
| 132 | int main(int argc, char *argv[]) |
| 133 | { |
| 134 | char *p = NULL; |
| 135 | if (argc < 3) { |
| 136 | fprintf(stderr, "usage: %s --mangle|unmangle <string>\n", |
| 137 | program_invocation_short_name); |
| 138 | return EXIT_FAILURE; |
| 139 | } |
| 140 | |
| 141 | if (!strcmp(argv[1], "--mangle")) { |
| 142 | p = mangle(argv[2]); |
| 143 | printf("mangled: '%s'\n", p); |
| 144 | free(p); |
| 145 | } |
| 146 | |
| 147 | else if (!strcmp(argv[1], "--unmangle")) { |
| 148 | char *x = unmangle(argv[2], NULL); |
| 149 | |
| 150 | if (x) { |
| 151 | printf("unmangled: '%s'\n", x); |
| 152 | free(x); |
| 153 | } |
| 154 | |
| 155 | x = strdup(argv[2]); |
| 156 | unmangle_to_buffer(x, x, strlen(x) + 1); |
| 157 | |
| 158 | if (x) { |
| 159 | printf("self-unmangled: '%s'\n", x); |
| 160 | free(x); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return EXIT_SUCCESS; |
| 165 | } |
| 166 | #endif /* TEST_PROGRAM */ |