unvis.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2013 by egnite GmbH
  3. * Copyright (c) 1989, 1993 The Regents of the University of California
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. #include <sys/types.h>
  36. #include <ctype.h>
  37. #include <vis.h>
  38. /*
  39. * Decode driven by state machine
  40. */
  41. #define S_GROUND 0 /*!< \brief haven't seen escape char */
  42. #define S_START 1 /*!< \brief start decoding special sequence */
  43. #define S_META 2 /*!< \brief metachar started (M) */
  44. #define S_META1 3 /*!< \brief metachar more, regular char (-) */
  45. #define S_CTRL 4 /*!< \brief control char started (^) */
  46. #define S_OCTAL2 5 /*!< \brief octal digit 2 */
  47. #define S_OCTAL3 6 /*!< \brief octal digit 3 */
  48. #define S_HEX1 7 /*!< \brief hex digit */
  49. #define S_HEX2 8 /*!< \brief hex digit 2 */
  50. #define isoctal(c) (((unsigned char)(c)) >= '0' && ((unsigned char)(c)) <= '7')
  51. #define xtod(c) (isdigit(c) ? ((c) - '0') : ((tolower(c) - 'a') + 10))
  52. /*!
  53. * \brief Decode a visual representation of characters.
  54. *
  55. * This function is the inverse function of vis(). It reverts a visual
  56. * representation of data back to its original form.
  57. */
  58. int unvis(char *cp, int c, int *astate, int flag)
  59. {
  60. if (flag & UNVIS_END) {
  61. if (*astate == S_OCTAL2 || *astate == S_OCTAL3 || *astate == S_HEX2) {
  62. *astate = S_GROUND;
  63. return UNVIS_VALID;
  64. }
  65. return *astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD;
  66. }
  67. switch (*astate) {
  68. case S_GROUND:
  69. *cp = 0;
  70. if (c == '\\') {
  71. *astate = S_START;
  72. return 0;
  73. }
  74. if ((flag & VIS_HTTPSTYLE) && c == '%') {
  75. *astate = S_HEX1;
  76. return 0;
  77. }
  78. *cp = c;
  79. return UNVIS_VALID;
  80. case S_START:
  81. switch (c) {
  82. case '\\':
  83. *cp = c;
  84. *astate = S_GROUND;
  85. return UNVIS_VALID;
  86. case '0':
  87. case '1':
  88. case '2':
  89. case '3':
  90. case '4':
  91. case '5':
  92. case '6':
  93. case '7':
  94. *cp = (c - '0');
  95. *astate = S_OCTAL2;
  96. return 0;
  97. case 'M':
  98. *cp = (char) 0200;
  99. *astate = S_META;
  100. return 0;
  101. case '^':
  102. *astate = S_CTRL;
  103. return 0;
  104. case 'n':
  105. *cp = '\n';
  106. *astate = S_GROUND;
  107. return UNVIS_VALID;
  108. case 'r':
  109. *cp = '\r';
  110. *astate = S_GROUND;
  111. return UNVIS_VALID;
  112. case 'b':
  113. *cp = '\b';
  114. *astate = S_GROUND;
  115. return UNVIS_VALID;
  116. case 'a':
  117. *cp = '\007';
  118. *astate = S_GROUND;
  119. return UNVIS_VALID;
  120. case 'v':
  121. *cp = '\v';
  122. *astate = S_GROUND;
  123. return UNVIS_VALID;
  124. case 't':
  125. *cp = '\t';
  126. *astate = S_GROUND;
  127. return UNVIS_VALID;
  128. case 'f':
  129. *cp = '\f';
  130. *astate = S_GROUND;
  131. return UNVIS_VALID;
  132. case 's':
  133. *cp = ' ';
  134. *astate = S_GROUND;
  135. return UNVIS_VALID;
  136. case 'E':
  137. *cp = '\033';
  138. *astate = S_GROUND;
  139. return UNVIS_VALID;
  140. case '\n':
  141. /*
  142. * hidden newline
  143. */
  144. *astate = S_GROUND;
  145. return UNVIS_NOCHAR;
  146. case '$':
  147. /*
  148. * hidden marker
  149. */
  150. *astate = S_GROUND;
  151. return UNVIS_NOCHAR;
  152. }
  153. *astate = S_GROUND;
  154. return UNVIS_SYNBAD;
  155. case S_META:
  156. if (c == '-')
  157. *astate = S_META1;
  158. else if (c == '^')
  159. *astate = S_CTRL;
  160. else {
  161. *astate = S_GROUND;
  162. return UNVIS_SYNBAD;
  163. }
  164. return 0;
  165. case S_META1:
  166. *astate = S_GROUND;
  167. *cp |= c;
  168. return UNVIS_VALID;
  169. case S_CTRL:
  170. if (c == '?')
  171. *cp |= 0177;
  172. else
  173. *cp |= c & 037;
  174. *astate = S_GROUND;
  175. return UNVIS_VALID;
  176. case S_OCTAL2: /* second possible octal digit */
  177. if (isoctal(c)) {
  178. /*
  179. * yes - and maybe a third
  180. */
  181. *cp = (*cp << 3) + (c - '0');
  182. *astate = S_OCTAL3;
  183. return 0;
  184. }
  185. /*
  186. * no - done with current sequence, push back passed char
  187. */
  188. *astate = S_GROUND;
  189. return UNVIS_VALIDPUSH;
  190. case S_OCTAL3: /* third possible octal digit */
  191. *astate = S_GROUND;
  192. if (isoctal(c)) {
  193. *cp = (*cp << 3) + (c - '0');
  194. return UNVIS_VALID;
  195. }
  196. /*
  197. * we were done, push back passed char
  198. */
  199. return UNVIS_VALIDPUSH;
  200. case S_HEX1:
  201. if (isxdigit(c)) {
  202. *cp = xtod(c);
  203. *astate = S_HEX2;
  204. return 0;
  205. }
  206. /*
  207. * no - done with current sequence, push back passed char
  208. */
  209. *astate = S_GROUND;
  210. return UNVIS_VALIDPUSH;
  211. case S_HEX2:
  212. *astate = S_GROUND;
  213. if (isxdigit(c)) {
  214. *cp = xtod(c) | (*cp << 4);
  215. return UNVIS_VALID;
  216. }
  217. return UNVIS_VALIDPUSH;
  218. default:
  219. /*
  220. * decoder in unknown state - (probably uninitialized)
  221. */
  222. *astate = S_GROUND;
  223. return UNVIS_SYNBAD;
  224. }
  225. }
  226. int strunvisx(char *dst, const char *src, int flag)
  227. {
  228. char c;
  229. char *start = dst;
  230. int state = 0;
  231. while ((c = *src++)) {
  232. again:
  233. switch (unvis(dst, c, &state, flag)) {
  234. case UNVIS_VALID:
  235. dst++;
  236. break;
  237. case UNVIS_VALIDPUSH:
  238. dst++;
  239. goto again;
  240. case 0:
  241. case UNVIS_NOCHAR:
  242. break;
  243. default:
  244. *dst = '\0';
  245. return -1;
  246. }
  247. }
  248. if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
  249. dst++;
  250. *dst = '\0';
  251. return dst - start;
  252. }
  253. /*!
  254. * \brief Decode a visually encoded string.
  255. *
  256. * This function is the inverse function of strvis().
  257. */
  258. int strunvis(char *dst, const char *src)
  259. {
  260. return strunvisx(dst, src, 0);
  261. }
  262. /*!
  263. * \brief Decode a visually encoded string up to a maximum number of characters.
  264. *
  265. */
  266. int strnunvis(char *dst, const char *src, size_t sz)
  267. {
  268. char c, p;
  269. char *start = dst, *end = dst + sz - 1;
  270. int state = 0;
  271. if (sz > 0)
  272. *end = '\0';
  273. while ((c = *src++)) {
  274. again:
  275. switch (unvis(&p, c, &state, 0)) {
  276. case UNVIS_VALID:
  277. if (dst < end)
  278. *dst = p;
  279. dst++;
  280. break;
  281. case UNVIS_VALIDPUSH:
  282. if (dst < end)
  283. *dst = p;
  284. dst++;
  285. goto again;
  286. case 0:
  287. case UNVIS_NOCHAR:
  288. break;
  289. default:
  290. if (dst <= end)
  291. *dst = '\0';
  292. return -1;
  293. }
  294. }
  295. if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) {
  296. if (dst < end)
  297. *dst = p;
  298. dst++;
  299. }
  300. if (dst <= end)
  301. *dst = '\0';
  302. return dst - start;
  303. }