2 * This file is Copyright (c) 2010 by the GPSD project
3 * BSD terms apply: see the file COPYING in the distribution root for details.
7 #endif /* S_SPLINT_S */
15 int gpsd_hexdump_level = -1;
17 * A wrapper around gpsd_hexdump to prevent wasting cpu time by hexdumping
18 * buffers and copying strings that will never be printed. only messages at
19 * level "N" and lower will be printed. By way of example, without any -D
20 * options, gpsd probably won't ever call the real gpsd_hexdump. At -D2,
21 * LOG_PROG (and higher) won't get to call the real gpsd_hexdump. For high
22 * speed, chatty protocols, this can save a lot of CPU.
24 char *gpsd_hexdump_wrapper(const void *binbuf, size_t binbuflen,
27 #ifndef SQUELCH_ENABLE
28 if (msg_debug_level <= gpsd_hexdump_level)
29 return gpsd_hexdump(binbuf, binbuflen);
30 #endif /* SQUELCH_ENABLE */
34 char /*@ observer @*/ *gpsd_hexdump(const void *binbuf, size_t binbuflen)
36 static char hexbuf[MAX_PACKET_LENGTH * 2 + 1];
37 #ifndef SQUELCH_ENABLE
40 (size_t) ((binbuflen >
41 MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen);
42 const char *ibuf = (const char *)binbuf;
43 const char *hexchar = "0123456789abcdef";
45 if (NULL == binbuf || 0 == binbuflen)
48 /*@ -shiftimplementation @*/
49 for (i = 0; i < len; i++) {
50 hexbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4];
51 hexbuf[j++] = hexchar[ibuf[i] & 0x0f];
53 /*@ +shiftimplementation @*/
55 #else /* SQUELCH defined */
57 #endif /* SQUELCH_ENABLE */
61 int gpsd_hexpack( /*@in@*/ const char *src, /*@out@ */ char *dst, size_t len)
63 /* hex2bin source string to destination - destination can be same as source */
67 l = (int)(strlen(src) / 2);
68 if ((l < 1) || ((size_t) l > len))
71 for (i = 0; i < l; i++)
72 if ((k = hex2bin(src + i * 2)) != -1)
73 dst[i] = (char)(k & 0xff);
76 (void)memset(dst + i, '\0', (size_t) (len - i));
81 /*@ +charint -shiftimplementation @*/
82 int hex2bin(const char *s)
89 if ((a >= 'a') && (a <= 'f'))
91 else if ((a >= 'A') && (a <= 'F'))
93 else if ((a >= '0') && (a <= '9'))
98 if ((b >= 'a') && (b <= 'f'))
100 else if ((b >= 'A') && (b <= 'F'))
102 else if ((b >= '0') && (b <= '9'))
107 return ((a << 4) + b);
110 /*@ -charint +shiftimplementation @*/
112 ssize_t hex_escapes( /*@out@*/ char *cooked, const char *raw)
113 /* interpret C-style hex escapes */
117 /*@ +charint -mustdefine -compdef @*/
118 for (cookend = cooked; *raw != '\0'; raw++)
270 return (ssize_t) (cookend - cooked);
271 /*@ +charint +mustdefine +compdef @*/