pkt-line: provide a generic reading function with options
[git/git.git] / pkt-line.c
CommitLineData
f3a3214e
LT
1#include "cache.h"
2#include "pkt-line.h"
3
1e4cd68c 4static const char *packet_trace_prefix = "git";
bbc30f99
JK
5static const char trace_key[] = "GIT_TRACE_PACKET";
6
7void packet_trace_identity(const char *prog)
8{
9 packet_trace_prefix = xstrdup(prog);
10}
11
12static void packet_trace(const char *buf, unsigned int len, int write)
13{
14 int i;
15 struct strbuf out;
16
17 if (!trace_want(trace_key))
18 return;
19
20 /* +32 is just a guess for header + quoting */
21 strbuf_init(&out, len+32);
22
23 strbuf_addf(&out, "packet: %12s%c ",
24 packet_trace_prefix, write ? '>' : '<');
25
26 if ((len >= 4 && !prefixcmp(buf, "PACK")) ||
27 (len >= 5 && !prefixcmp(buf+1, "PACK"))) {
28 strbuf_addstr(&out, "PACK ...");
29 unsetenv(trace_key);
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');
45 trace_strbuf(trace_key, &out);
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])
f5615d24
SP
66static char buffer[1000];
67static unsigned format_packet(const char *fmt, va_list args)
f3a3214e 68{
f3a3214e 69 static char hexchar[] = "0123456789abcdef";
f3a3214e
LT
70 unsigned n;
71
f3a3214e 72 n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
f3a3214e
LT
73 if (n >= sizeof(buffer)-4)
74 die("protocol error: impossibly long line");
75 n += 4;
76 buffer[0] = hex(n >> 12);
77 buffer[1] = hex(n >> 8);
78 buffer[2] = hex(n >> 4);
79 buffer[3] = hex(n);
bbc30f99 80 packet_trace(buffer+4, n-4, 1);
f5615d24
SP
81 return n;
82}
83
84void packet_write(int fd, const char *fmt, ...)
85{
86 va_list args;
87 unsigned n;
88
89 va_start(args, fmt);
90 n = format_packet(fmt, args);
91 va_end(args);
cdf4fb8e 92 write_or_die(fd, buffer, n);
f3a3214e
LT
93}
94
f5615d24
SP
95void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
96{
97 va_list args;
98 unsigned n;
99
100 va_start(args, fmt);
101 n = format_packet(fmt, args);
102 va_end(args);
103 strbuf_add(buf, buffer, n);
104}
105
03809429 106static int safe_read(int fd, void *buffer, unsigned size, int options)
f3a3214e 107{
c697ad14
HO
108 ssize_t ret = read_in_full(fd, buffer, size);
109 if (ret < 0)
d824cbba 110 die_errno("read error");
46284dd1 111 else if (ret < size) {
03809429 112 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
113 return -1;
114
c697ad14 115 die("The remote end hung up unexpectedly");
46284dd1
HV
116 }
117
118 return ret;
f3a3214e
LT
119}
120
f5615d24 121static int packet_length(const char *linelen)
f3a3214e
LT
122{
123 int n;
f5615d24 124 int len = 0;
f3a3214e 125
f3a3214e
LT
126 for (n = 0; n < 4; n++) {
127 unsigned char c = linelen[n];
128 len <<= 4;
129 if (c >= '0' && c <= '9') {
130 len += c - '0';
131 continue;
132 }
133 if (c >= 'a' && c <= 'f') {
134 len += c - 'a' + 10;
135 continue;
136 }
137 if (c >= 'A' && c <= 'F') {
138 len += c - 'A' + 10;
139 continue;
140 }
f5615d24 141 return -1;
f3a3214e 142 }
f5615d24
SP
143 return len;
144}
145
03809429 146int packet_read(int fd, char *buffer, unsigned size, int options)
f5615d24 147{
46284dd1 148 int len, ret;
f5615d24
SP
149 char linelen[4];
150
03809429
JK
151 ret = safe_read(fd, linelen, 4, options);
152 if (ret < 0)
46284dd1 153 return ret;
f5615d24
SP
154 len = packet_length(linelen);
155 if (len < 0)
743c4b7b 156 die("protocol error: bad line length character: %.4s", linelen);
bbc30f99
JK
157 if (!len) {
158 packet_trace("0000", 4, 0);
f3a3214e 159 return 0;
bbc30f99 160 }
f3a3214e
LT
161 len -= 4;
162 if (len >= size)
163 die("protocol error: bad line length %d", len);
03809429
JK
164 ret = safe_read(fd, buffer, len, options);
165 if (ret < 0)
46284dd1 166 return ret;
f3a3214e 167 buffer[len] = 0;
bbc30f99 168 packet_trace(buffer, len, 0);
f3a3214e
LT
169 return len;
170}
f5615d24 171
46284dd1
HV
172int packet_read_line(int fd, char *buffer, unsigned size)
173{
03809429 174 return packet_read(fd, buffer, size, 0);
46284dd1
HV
175}
176
f5615d24
SP
177int packet_get_line(struct strbuf *out,
178 char **src_buf, size_t *src_len)
179{
180 int len;
181
182 if (*src_len < 4)
183 return -1;
184 len = packet_length(*src_buf);
185 if (len < 0)
186 return -1;
187 if (!len) {
188 *src_buf += 4;
189 *src_len -= 4;
bbc30f99 190 packet_trace("0000", 4, 0);
f5615d24
SP
191 return 0;
192 }
193 if (*src_len < len)
194 return -2;
195
196 *src_buf += 4;
197 *src_len -= 4;
198 len -= 4;
199
200 strbuf_add(out, *src_buf, len);
201 *src_buf += len;
202 *src_len -= len;
bbc30f99 203 packet_trace(out->buf, out->len, 0);
f5615d24
SP
204 return len;
205}