summaryrefslogtreecommitdiff
path: root/hex.c
blob: f09f6159434bd2aa0bcd1020ad19e27030857be2 (plain)
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
/* $Id$ */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "gpsd_config.h"
#include "gpsd.h"

int gpsd_hexdump_level = -1;
/*
 * A wrapper around gpsd_hexdump to prevent wasting cpu time by hexdumping
 * buffers and copying strings that will never be printed. only messages at
 * level "N" and lower will be printed. By way of example, without any -D
 * options, gpsd probably won't ever call the real gpsd_hexdump. At -D2,
 * LOG_PROG (and higher) won't get to call the real gpsd_hexdump. For high
 * speed, chatty protocols, this can save a lot of CPU.
 */
char *gpsd_hexdump_wrapper(const void *binbuf, size_t binbuflen,
    int msg_debug_level)
{
#ifndef SQUELCH_ENABLE
    if (msg_debug_level <= gpsd_hexdump_level)
	return gpsd_hexdump(binbuf, binbuflen);
#endif /* SQUELCH_ENABLE */
    return "";
}

char /*@ observer @*/ *gpsd_hexdump(const void *binbuf, size_t binbuflen)
{
    static char hexbuf[MAX_PACKET_LENGTH*2+1];
#ifndef SQUELCH_ENABLE
    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) 
	return "";

    /*@ -shiftimplementation @*/
    for (i = 0; i < len; i++) {
	hexbuf[j++] = hexchar[ (ibuf[i]&0xf0)>>4 ];
	hexbuf[j++] = hexchar[ ibuf[i]&0x0f ];
    }
    /*@ +shiftimplementation @*/
    hexbuf[j] ='\0';
#else /* SQUELCH defined */
    hexbuf[0] = '\0';
#endif /* SQUELCH_ENABLE */
    return hexbuf;
}

int gpsd_hexpack(char *src, char *dst, size_t len){
/* hex2bin source string to destination - destination can be same as source */ 
    int i, k, l;

    l = (int)(strlen(src) / 2);
    if ((l < 1) || ((size_t)l > len))
	return -2;

    bzero(dst, (int)len);
    for (i = 0; i < l; i++)
	if ((k = hex2bin(src+i*2)) != -1)
	    dst[i] = (char)(k & 0xff);
	else
	    return -1;
    return l;
}

/*@ +charint -shiftimplementation @*/
int hex2bin(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);
}
/*@ -charint +shiftimplementation @*/

ssize_t hex_escapes(/*@out@*/char *cooked, const char *raw)
/* interpret C-style hex escapes */
{
    char c, *cookend;

    /*@ +charint -mustdefine -compdef @*/
    for (cookend = cooked; *raw != '\0'; raw++)
	if (*raw != '\\')
	    *cookend++ = *raw;
	else {
	    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 = 0x00; break;
		case '1': c = 0x10; break;
		case '2': c = 0x20; break;
		case '3': c = 0x30; break;
		case '4': c = 0x40; break;
		case '5': c = 0x50; break;
		case '6': c = 0x60; break;
		case '7': c = 0x70; break;
		case '8': c = 0x80; break;
		case '9': c = 0x90; break;
		case 'A': case 'a': c = 0xa0; break;
		case 'B': case 'b': c = 0xb0; break;
		case 'C': case 'c': c = 0xc0; break;
		case 'D': case 'd': c = 0xd0; break;
		case 'E': case 'e': c = 0xe0; break;
		case 'F': case 'f': c = 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': case 'a': c += 0x0a; break;
		case 'B': case 'b': c += 0x0b; break;
		case 'C': case 'c': c += 0x0c; break;
		case 'D': case 'd': c += 0x0d; break;
		case 'E': case 'e': c += 0x0e; break;
		case 'F': case 'f': c += 0x0f; break;
		default:
		    return -2;
		}
		*cookend++ = c;
		break;
	    case '\\': *cookend++ = '\\'; break;
	    default:
		return -3;
	    }
	}
    return (ssize_t)(cookend - cooked);
    /*@ +charint +mustdefine +compdef @*/
}