pkt-line: tighten sideband PACK check when tracing
[git/git.git] / pkt-line.c
CommitLineData
f3a3214e
LT
1#include "cache.h"
2#include "pkt-line.h"
3
74543a04 4char packet_buffer[LARGE_PACKET_MAX];
1e4cd68c 5static const char *packet_trace_prefix = "git";
6aa30857 6static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
bbc30f99
JK
7
8void packet_trace_identity(const char *prog)
9{
10 packet_trace_prefix = xstrdup(prog);
11}
12
13static void packet_trace(const char *buf, unsigned int len, int write)
14{
15 int i;
16 struct strbuf out;
17
6aa30857 18 if (!trace_want(&trace_packet))
bbc30f99
JK
19 return;
20
21 /* +32 is just a guess for header + quoting */
22 strbuf_init(&out, len+32);
23
24 strbuf_addf(&out, "packet: %12s%c ",
25 packet_trace_prefix, write ? '>' : '<');
26
d6d1a75e 27 if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
bbc30f99 28 strbuf_addstr(&out, "PACK ...");
6aa30857 29 trace_disable(&trace_packet);
bbc30f99
JK
30 }
31 else {
32 /* XXX we should really handle printable utf8 */
33 for (i = 0; i < len; i++) {
34 /* suppress newlines */
35 if (buf[i] == '\n')
36 continue;
37 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
38 strbuf_addch(&out, buf[i]);
39 else
40 strbuf_addf(&out, "\\%o", buf[i]);
41 }
42 }
43
44 strbuf_addch(&out, '\n');
6aa30857 45 trace_strbuf(&trace_packet, &out);
bbc30f99
JK
46 strbuf_release(&out);
47}
48
f3a3214e
LT
49/*
50 * If we buffered things up above (we don't, but we should),
51 * we'd flush it here
52 */
53void packet_flush(int fd)
54{
bbc30f99 55 packet_trace("0000", 4, 1);
cdf4fb8e 56 write_or_die(fd, "0000", 4);
f3a3214e
LT
57}
58
f5615d24
SP
59void packet_buf_flush(struct strbuf *buf)
60{
bbc30f99 61 packet_trace("0000", 4, 1);
f5615d24
SP
62 strbuf_add(buf, "0000", 4);
63}
64
f3a3214e 65#define hex(a) (hexchar[(a) & 15])
8e9faf27 66static void format_packet(struct strbuf *out, const char *fmt, va_list args)
f3a3214e 67{
f3a3214e 68 static char hexchar[] = "0123456789abcdef";
8e9faf27 69 size_t orig_len, n;
f3a3214e 70
8e9faf27
JK
71 orig_len = out->len;
72 strbuf_addstr(out, "0000");
73 strbuf_vaddf(out, fmt, args);
74 n = out->len - orig_len;
75
76 if (n > LARGE_PACKET_MAX)
f3a3214e 77 die("protocol error: impossibly long line");
8e9faf27
JK
78
79 out->buf[orig_len + 0] = hex(n >> 12);
80 out->buf[orig_len + 1] = hex(n >> 8);
81 out->buf[orig_len + 2] = hex(n >> 4);
82 out->buf[orig_len + 3] = hex(n);
83 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d24
SP
84}
85
86void packet_write(int fd, const char *fmt, ...)
87{
8e9faf27 88 static struct strbuf buf = STRBUF_INIT;
f5615d24 89 va_list args;
f5615d24 90
8e9faf27 91 strbuf_reset(&buf);
f5615d24 92 va_start(args, fmt);
8e9faf27 93 format_packet(&buf, fmt, args);
f5615d24 94 va_end(args);
8e9faf27 95 write_or_die(fd, buf.buf, buf.len);
f3a3214e
LT
96}
97
f5615d24
SP
98void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
99{
100 va_list args;
f5615d24
SP
101
102 va_start(args, fmt);
8e9faf27 103 format_packet(buf, fmt, args);
f5615d24 104 va_end(args);
f5615d24
SP
105}
106
4981fe75
JK
107static int get_packet_data(int fd, char **src_buf, size_t *src_size,
108 void *dst, unsigned size, int options)
f3a3214e 109{
4981fe75
JK
110 ssize_t ret;
111
112 if (fd >= 0 && src_buf && *src_buf)
113 die("BUG: multiple sources given to packet_read");
114
115 /* Read up to "size" bytes from our source, whatever it is. */
116 if (src_buf && *src_buf) {
117 ret = size < *src_size ? size : *src_size;
118 memcpy(dst, *src_buf, ret);
119 *src_buf += ret;
120 *src_size -= ret;
121 } else {
122 ret = read_in_full(fd, dst, size);
123 if (ret < 0)
124 die_errno("read error");
125 }
126
127 /* And complain if we didn't get enough bytes to satisfy the read. */
128 if (ret < size) {
03809429 129 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
130 return -1;
131
c697ad14 132 die("The remote end hung up unexpectedly");
46284dd1
HV
133 }
134
135 return ret;
f3a3214e
LT
136}
137
f5615d24 138static int packet_length(const char *linelen)
f3a3214e
LT
139{
140 int n;
f5615d24 141 int len = 0;
f3a3214e 142
f3a3214e
LT
143 for (n = 0; n < 4; n++) {
144 unsigned char c = linelen[n];
145 len <<= 4;
146 if (c >= '0' && c <= '9') {
147 len += c - '0';
148 continue;
149 }
150 if (c >= 'a' && c <= 'f') {
151 len += c - 'a' + 10;
152 continue;
153 }
154 if (c >= 'A' && c <= 'F') {
155 len += c - 'A' + 10;
156 continue;
157 }
f5615d24 158 return -1;
f3a3214e 159 }
f5615d24
SP
160 return len;
161}
162
4981fe75
JK
163int packet_read(int fd, char **src_buf, size_t *src_len,
164 char *buffer, unsigned size, int options)
f5615d24 165{
46284dd1 166 int len, ret;
f5615d24
SP
167 char linelen[4];
168
4981fe75 169 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
03809429 170 if (ret < 0)
46284dd1 171 return ret;
f5615d24
SP
172 len = packet_length(linelen);
173 if (len < 0)
743c4b7b 174 die("protocol error: bad line length character: %.4s", linelen);
bbc30f99
JK
175 if (!len) {
176 packet_trace("0000", 4, 0);
f3a3214e 177 return 0;
bbc30f99 178 }
f3a3214e
LT
179 len -= 4;
180 if (len >= size)
181 die("protocol error: bad line length %d", len);
4981fe75 182 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
03809429 183 if (ret < 0)
46284dd1 184 return ret;
819b929d
JK
185
186 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
187 len && buffer[len-1] == '\n')
188 len--;
189
f3a3214e 190 buffer[len] = 0;
bbc30f99 191 packet_trace(buffer, len, 0);
f3a3214e
LT
192 return len;
193}
f5615d24 194
4981fe75
JK
195static char *packet_read_line_generic(int fd,
196 char **src, size_t *src_len,
197 int *dst_len)
46284dd1 198{
4981fe75
JK
199 int len = packet_read(fd, src, src_len,
200 packet_buffer, sizeof(packet_buffer),
74543a04 201 PACKET_READ_CHOMP_NEWLINE);
4981fe75
JK
202 if (dst_len)
203 *dst_len = len;
74543a04 204 return len ? packet_buffer : NULL;
46284dd1
HV
205}
206
4981fe75 207char *packet_read_line(int fd, int *len_p)
f5615d24 208{
4981fe75
JK
209 return packet_read_line_generic(fd, NULL, NULL, len_p);
210}
f5615d24 211
4981fe75
JK
212char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
213{
214 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d24 215}