You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hex.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * This file is Copyright (c) 2010-2018 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #include "gpsd_config.h" /* must be before all includes */
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <assert.h>
  9. #include "gpsd.h"
  10. /*
  11. * gpsd_packetdump()
  12. *
  13. * if binbuf is printable, just return a copy of it
  14. * otherwise) convert a binary string to a hex string and return that
  15. *
  16. * *scbuf -- the buffer to fill with hex
  17. * scbuflen -- sizeof(scbuf)
  18. * *binbuf -- the binary to convert to hex and place in scbuf
  19. * binbuflen -- sizeof(binbuf)
  20. *
  21. * scbuflen needs to be 2x binbuflen to hold the hex conversion
  22. */
  23. const char *gpsd_packetdump(char *scbuf, size_t scbuflen,
  24. char *binbuf, size_t binbuflen)
  25. {
  26. char *cp;
  27. bool printable = true;
  28. assert(binbuf != NULL);
  29. for (cp = binbuf; cp < binbuf + binbuflen; cp++)
  30. if (!isprint((unsigned char) *cp) && !isspace((unsigned char) *cp)) {
  31. printable = false;
  32. break; /* no need to keep iterating */
  33. }
  34. if (printable)
  35. return binbuf;
  36. else
  37. return gpsd_hexdump(scbuf, scbuflen, binbuf, binbuflen);
  38. }
  39. const char *gpsd_hexdump(char *scbuf, size_t scbuflen,
  40. char *binbuf, size_t binbuflen)
  41. {
  42. #ifndef SQUELCH_ENABLE
  43. size_t i, j = 0;
  44. size_t len =
  45. (size_t) ((binbuflen >
  46. MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen);
  47. const char *ibuf = (const char *)binbuf;
  48. const char *hexchar = "0123456789abcdef";
  49. if (NULL == binbuf || 0 == binbuflen)
  50. return "";
  51. for (i = 0; i < len && j < (scbuflen - 3); i++) {
  52. scbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4];
  53. scbuf[j++] = hexchar[ibuf[i] & 0x0f];
  54. }
  55. scbuf[j] = '\0';
  56. #else /* SQUELCH defined */
  57. scbuf[0] = '\0';
  58. (void)scbuflen;
  59. (void)binbuf;
  60. (void)binbuflen;
  61. #endif /* SQUELCH_ENABLE */
  62. return scbuf;
  63. }
  64. static int hex2bin(const char *s)
  65. {
  66. int a, b;
  67. a = s[0] & 0xff;
  68. b = s[1] & 0xff;
  69. if ((a >= 'a') && (a <= 'f'))
  70. a = a + 10 - 'a';
  71. else if ((a >= 'A') && (a <= 'F'))
  72. a = a + 10 - 'A';
  73. else if ((a >= '0') && (a <= '9'))
  74. a -= '0';
  75. else
  76. return -1;
  77. if ((b >= 'a') && (b <= 'f'))
  78. b = b + 10 - 'a';
  79. else if ((b >= 'A') && (b <= 'F'))
  80. b = b + 10 - 'A';
  81. else if ((b >= '0') && (b <= '9'))
  82. b -= '0';
  83. else
  84. return -1;
  85. return ((a << 4) + b);
  86. }
  87. int gpsd_hexpack(const char *src, char *dst, size_t len)
  88. /* hex2bin source string to destination - destination can be same as source */
  89. {
  90. int i, j;
  91. j = (int)(strlen(src) / 2);
  92. if ((j < 1) || ((size_t) j > len))
  93. return -2;
  94. for (i = 0; i < j; i++) {
  95. int k;
  96. if ((k = hex2bin(src + i * 2)) != -1)
  97. dst[i] = (char)(k & 0xff);
  98. else
  99. return -1;
  100. }
  101. (void)memset(dst + i, '\0', (size_t) (len - i));
  102. return j;
  103. }
  104. ssize_t hex_escapes(char *cooked, const char *raw)
  105. /* interpret C-style hex escapes */
  106. {
  107. char c, *cookend;
  108. for (cookend = cooked; *raw != '\0'; raw++)
  109. if (*raw != '\\')
  110. *cookend++ = *raw;
  111. else {
  112. switch (*++raw) {
  113. case 'b':
  114. *cookend++ = '\b';
  115. break;
  116. case 'e':
  117. *cookend++ = '\x1b';
  118. break;
  119. case 'f':
  120. *cookend++ = '\f';
  121. break;
  122. case 'n':
  123. *cookend++ = '\n';
  124. break;
  125. case 'r':
  126. *cookend++ = '\r';
  127. break;
  128. case 't':
  129. *cookend++ = '\r';
  130. break;
  131. case 'v':
  132. *cookend++ = '\v';
  133. break;
  134. case 'x':
  135. switch (*++raw) {
  136. case '0':
  137. c = (char)0x00;
  138. break;
  139. case '1':
  140. c = (char)0x10;
  141. break;
  142. case '2':
  143. c = (char)0x20;
  144. break;
  145. case '3':
  146. c = (char)0x30;
  147. break;
  148. case '4':
  149. c = (char)0x40;
  150. break;
  151. case '5':
  152. c = (char)0x50;
  153. break;
  154. case '6':
  155. c = (char)0x60;
  156. break;
  157. case '7':
  158. c = (char)0x70;
  159. break;
  160. case '8':
  161. c = (char)0x80;
  162. break;
  163. case '9':
  164. c = (char)0x90;
  165. break;
  166. case 'A':
  167. case 'a':
  168. c = (char)0xa0;
  169. break;
  170. case 'B':
  171. case 'b':
  172. c = (char)0xb0;
  173. break;
  174. case 'C':
  175. case 'c':
  176. c = (char)0xc0;
  177. break;
  178. case 'D':
  179. case 'd':
  180. c = (char)0xd0;
  181. break;
  182. case 'E':
  183. case 'e':
  184. c = (char)0xe0;
  185. break;
  186. case 'F':
  187. case 'f':
  188. c = (char)0xf0;
  189. break;
  190. default:
  191. return -1;
  192. }
  193. switch (*++raw) {
  194. case '0':
  195. c += 0x00;
  196. break;
  197. case '1':
  198. c += 0x01;
  199. break;
  200. case '2':
  201. c += 0x02;
  202. break;
  203. case '3':
  204. c += 0x03;
  205. break;
  206. case '4':
  207. c += 0x04;
  208. break;
  209. case '5':
  210. c += 0x05;
  211. break;
  212. case '6':
  213. c += 0x06;
  214. break;
  215. case '7':
  216. c += 0x07;
  217. break;
  218. case '8':
  219. c += 0x08;
  220. break;
  221. case '9':
  222. c += 0x09;
  223. break;
  224. case 'A':
  225. case 'a':
  226. c += 0x0a;
  227. break;
  228. case 'B':
  229. case 'b':
  230. c += 0x0b;
  231. break;
  232. case 'C':
  233. case 'c':
  234. c += 0x0c;
  235. break;
  236. case 'D':
  237. case 'd':
  238. c += 0x0d;
  239. break;
  240. case 'E':
  241. case 'e':
  242. c += 0x0e;
  243. break;
  244. case 'F':
  245. case 'f':
  246. c += 0x0f;
  247. break;
  248. default:
  249. return -2;
  250. }
  251. *cookend++ = c;
  252. break;
  253. case '\\':
  254. *cookend++ = '\\';
  255. break;
  256. default:
  257. return -3;
  258. }
  259. }
  260. return (ssize_t) (cookend - cooked);
  261. }