1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* This file is Copyright 2010 by the GPSD project
* SPDX-License-Identifier: BSD-2-clause
*/
#include "../include/gpsd_config.h" // must be before all includes
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "../include/gpsd.h"
/*
* gpsd_packetdump()
*
* if binbuf is printable, just return a copy of it
* otherwise) convert a binary string to a hex string and return that
*
* *scbuf -- the buffer to fill with hex
* scbuflen -- sizeof(scbuf)
* *binbuf -- the binary to convert to hex and place in scbuf
* binbuflen -- sizeof(binbuf)
*
* scbuflen needs to be 2x binbuflen to hold the hex conversion
*/
const char *gpsd_packetdump(char *scbuf, size_t scbuflen,
const unsigned char *binbuf,
size_t binbuflen)
{
const unsigned char *cp;
bool printable = true;
if (NULL == binbuf) {
return "";
}
for (cp = binbuf; cp < binbuf + binbuflen; cp++)
if (!isprint(*cp) && !isspace(*cp)) {
printable = false;
break; // no need to keep iterating
}
if (printable) {
return (const char *)binbuf;
}
// else
return gps_hexdump(scbuf, scbuflen, binbuf, binbuflen);
}
/* gps_hexdump()
* convert binary in binbuf, of length binbuflen
* into hex string at scbuf of length scbuflen
*
* Return scbuf
*/
const char *gps_hexdump(char *scbuf, size_t scbuflen,
const unsigned char *binbuf, size_t binbuflen)
{
size_t i, j = 0;
size_t len =
(size_t)((binbuflen >
MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen);
const char *ibuf = (const char *)binbuf;
const char *hexchar = "0123456789abcdef";
if (NULL == binbuf ||
0 == binbuflen) {
scbuf[0] = '\0';
return scbuf;
}
for (i = 0; i < len && j < (scbuflen - 3); i++) {
scbuf[j++] = hexchar[(ibuf[i] & 0xf0) >> 4];
scbuf[j++] = hexchar[ibuf[i] & 0x0f];
}
scbuf[j] = '\0';
return scbuf;
}
static int hex2bin(const char *s)
{
int a, b;
a = s[0] & 0xff;
b = s[1] & 0xff;
if ((a >= 'a') &&
(a <= 'f')) {
a = a + 10 - 'a';
} else if ((a >= 'A') &&
(a <= 'F')) {
a = a + 10 - 'A';
} else if ((a >= '0') &&
(a <= '9')) {
a -= '0';
} else {
return -1;
}
if ((b >= 'a') &&
(b <= 'f')) {
b = b + 10 - 'a';
} else if ((b >= 'A') &&
(b <= 'F')) {
b = b + 10 - 'A';
} else if ((b >= '0') &&
(b <= '9')) {
b -= '0';
} else {
return -1;
}
return ((a << 4) + b);
}
// hex2bin source string to destination - destination can be same as source
ssize_t gps_hexpack(const char *src, unsigned char *dst, size_t len)
{
ssize_t i, j;
j = strnlen(src, BUFSIZ) / 2;
if ((1 > j) ||
((size_t)j > len)) {
return -2;
}
for (i = 0; i < j; i++) {
int k;
if (-1 == (k = hex2bin(src + i * 2))) {
return -1;
}
dst[i] = (char)(k & 0xff);
}
(void)memset(dst + i, '\0', (size_t)(len - i));
return j;
}
// interpret C-style hex escapes
ssize_t hex_escapes(char *cooked, const char *raw)
{
char c, *cookend;
for (cookend = cooked; *raw != '\0'; raw++) {
if ('\\' != *raw) {
// not an escape, just copy thje character
*cookend++ = *raw;
continue;
}
switch (*++raw) {
case 'b':
*cookend++ = '\b';
break;
case 'e':
*cookend++ = '\x1b';
break;
case 'f':
*cookend++ = '\f';
break;
case 'n':
*cookend++ = '\n';
break;
case 'r':
*cookend++ = '\r';
break;
case 't':
*cookend++ = '\r';
break;
case 'v':
*cookend++ = '\v';
break;
case 'x':
switch (*++raw) {
case '0':
c = (char)0x00;
break;
case '1':
c = (char)0x10;
break;
case '2':
c = (char)0x20;
break;
case '3':
c = (char)0x30;
break;
case '4':
c = (char)0x40;
break;
case '5':
c = (char)0x50;
break;
case '6':
c = (char)0x60;
break;
case '7':
c = (char)0x70;
break;
case '8':
c = (char)0x80;
break;
case '9':
c = (char)0x90;
break;
case 'A':
FALLTHROUGH
case 'a':
c = (char)0xa0;
break;
case 'B':
FALLTHROUGH
case 'b':
c = (char)0xb0;
break;
case 'C':
FALLTHROUGH
case 'c':
c = (char)0xc0;
break;
case 'D':
FALLTHROUGH
case 'd':
c = (char)0xd0;
break;
case 'E':
FALLTHROUGH
case 'e':
c = (char)0xe0;
break;
case 'F':
FALLTHROUGH
case 'f':
c = (char)0xf0;
break;
default:
return -1;
}
switch (*++raw) {
case '0':
c += 0x00;
break;
case '1':
c += 0x01;
break;
case '2':
c += 0x02;
break;
case '3':
c += 0x03;
break;
case '4':
c += 0x04;
break;
case '5':
c += 0x05;
break;
case '6':
c += 0x06;
break;
case '7':
c += 0x07;
break;
case '8':
c += 0x08;
break;
case '9':
c += 0x09;
break;
case 'A':
FALLTHROUGH
case 'a':
c += 0x0a;
break;
case 'B':
FALLTHROUGH
case 'b':
c += 0x0b;
break;
case 'C':
FALLTHROUGH
case 'c':
c += 0x0c;
break;
case 'D':
FALLTHROUGH
case 'd':
c += 0x0d;
break;
case 'E':
FALLTHROUGH
case 'e':
c += 0x0e;
break;
case 'F':
FALLTHROUGH
case 'f':
c += 0x0f;
break;
default:
return -2;
}
*cookend++ = c;
break;
case '\\':
*cookend++ = '\\';
break;
default:
return -3;
}
}
return (ssize_t)(cookend - cooked);
}
// copy inbuf string to outbuf, replacingg non-printing characters
// slow, but only used for debug output
char *gps_visibilize(char *outbuf, size_t outlen,
const char *inbuf, size_t inlen)
{
const char *sp;
size_t next = 0;
outbuf[0] = '\0';
// FIXME!! snprintf() when strlcat() will do!
for (sp = inbuf; sp < inbuf + inlen && (next + 6) < outlen; sp++) {
int ret;
if (isprint((unsigned char)*sp)) {
ret = snprintf(&outbuf[next], 2, "%c", *sp);
} else {
ret = snprintf(&outbuf[next], 6, "\\x%02x",
0x00ff & (unsigned)*sp);
}
if (1 > ret) {
// error, or ran out of space
break;
}
next += ret;
}
return outbuf;
}
// vim: set expandtab shiftwidth=4
|