Merge branch 'rs/bswap-ubsan-fix' into maint
[git] / pkt-line.c
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "run-command.h"
4
5 char packet_buffer[LARGE_PACKET_MAX];
6 static const char *packet_trace_prefix = "git";
7 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
8 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
9
10 void packet_trace_identity(const char *prog)
11 {
12         packet_trace_prefix = xstrdup(prog);
13 }
14
15 static const char *get_trace_prefix(void)
16 {
17         return in_async() ? "sideband" : packet_trace_prefix;
18 }
19
20 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
21 {
22         if (!sideband) {
23                 trace_verbatim(&trace_pack, buf, len);
24                 return 1;
25         } else if (len && *buf == '\1') {
26                 trace_verbatim(&trace_pack, buf + 1, len - 1);
27                 return 1;
28         } else {
29                 /* it's another non-pack sideband */
30                 return 0;
31         }
32 }
33
34 static void packet_trace(const char *buf, unsigned int len, int write)
35 {
36         int i;
37         struct strbuf out;
38         static int in_pack, sideband;
39
40         if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
41                 return;
42
43         if (in_pack) {
44                 if (packet_trace_pack(buf, len, sideband))
45                         return;
46         } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
47                 in_pack = 1;
48                 sideband = *buf == '\1';
49                 packet_trace_pack(buf, len, sideband);
50
51                 /*
52                  * Make a note in the human-readable trace that the pack data
53                  * started.
54                  */
55                 buf = "PACK ...";
56                 len = strlen(buf);
57         }
58
59         if (!trace_want(&trace_packet))
60                 return;
61
62         /* +32 is just a guess for header + quoting */
63         strbuf_init(&out, len+32);
64
65         strbuf_addf(&out, "packet: %12s%c ",
66                     get_trace_prefix(), write ? '>' : '<');
67
68         /* XXX we should really handle printable utf8 */
69         for (i = 0; i < len; i++) {
70                 /* suppress newlines */
71                 if (buf[i] == '\n')
72                         continue;
73                 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
74                         strbuf_addch(&out, buf[i]);
75                 else
76                         strbuf_addf(&out, "\\%o", buf[i]);
77         }
78
79         strbuf_addch(&out, '\n');
80         trace_strbuf(&trace_packet, &out);
81         strbuf_release(&out);
82 }
83
84 /*
85  * If we buffered things up above (we don't, but we should),
86  * we'd flush it here
87  */
88 void packet_flush(int fd)
89 {
90         packet_trace("0000", 4, 1);
91         write_or_die(fd, "0000", 4);
92 }
93
94 int packet_flush_gently(int fd)
95 {
96         packet_trace("0000", 4, 1);
97         if (write_in_full(fd, "0000", 4) == 4)
98                 return 0;
99         return error("flush packet write failed");
100 }
101
102 void packet_buf_flush(struct strbuf *buf)
103 {
104         packet_trace("0000", 4, 1);
105         strbuf_add(buf, "0000", 4);
106 }
107
108 static void set_packet_header(char *buf, const int size)
109 {
110         static char hexchar[] = "0123456789abcdef";
111
112         #define hex(a) (hexchar[(a) & 15])
113         buf[0] = hex(size >> 12);
114         buf[1] = hex(size >> 8);
115         buf[2] = hex(size >> 4);
116         buf[3] = hex(size);
117         #undef hex
118 }
119
120 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
121 {
122         size_t orig_len, n;
123
124         orig_len = out->len;
125         strbuf_addstr(out, "0000");
126         strbuf_vaddf(out, fmt, args);
127         n = out->len - orig_len;
128
129         if (n > LARGE_PACKET_MAX)
130                 die("protocol error: impossibly long line");
131
132         set_packet_header(&out->buf[orig_len], n);
133         packet_trace(out->buf + orig_len + 4, n - 4, 1);
134 }
135
136 static int packet_write_fmt_1(int fd, int gently,
137                               const char *fmt, va_list args)
138 {
139         struct strbuf buf = STRBUF_INIT;
140         ssize_t count;
141
142         format_packet(&buf, fmt, args);
143         count = write_in_full(fd, buf.buf, buf.len);
144         if (count == buf.len)
145                 return 0;
146
147         if (!gently) {
148                 check_pipe(errno);
149                 die_errno("packet write with format failed");
150         }
151         return error("packet write with format failed");
152 }
153
154 void packet_write_fmt(int fd, const char *fmt, ...)
155 {
156         va_list args;
157
158         va_start(args, fmt);
159         packet_write_fmt_1(fd, 0, fmt, args);
160         va_end(args);
161 }
162
163 int packet_write_fmt_gently(int fd, const char *fmt, ...)
164 {
165         int status;
166         va_list args;
167
168         va_start(args, fmt);
169         status = packet_write_fmt_1(fd, 1, fmt, args);
170         va_end(args);
171         return status;
172 }
173
174 int packet_writel(int fd, const char *line, ...)
175 {
176         va_list args;
177         int err;
178         va_start(args, line);
179         for (;;) {
180                 if (!line)
181                         break;
182                 if (strlen(line) > LARGE_PACKET_DATA_MAX)
183                         return -1;
184                 err = packet_write_fmt_gently(fd, "%s\n", line);
185                 if (err)
186                         return err;
187                 line = va_arg(args, const char*);
188         }
189         va_end(args);
190         return packet_flush_gently(fd);
191 }
192
193 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
194 {
195         static char packet_write_buffer[LARGE_PACKET_MAX];
196         size_t packet_size;
197
198         if (size > sizeof(packet_write_buffer) - 4)
199                 return error("packet write failed - data exceeds max packet size");
200
201         packet_trace(buf, size, 1);
202         packet_size = size + 4;
203         set_packet_header(packet_write_buffer, packet_size);
204         memcpy(packet_write_buffer + 4, buf, size);
205         if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)
206                 return 0;
207         return error("packet write failed");
208 }
209
210 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
211 {
212         va_list args;
213
214         va_start(args, fmt);
215         format_packet(buf, fmt, args);
216         va_end(args);
217 }
218
219 int write_packetized_from_fd(int fd_in, int fd_out)
220 {
221         static char buf[LARGE_PACKET_DATA_MAX];
222         int err = 0;
223         ssize_t bytes_to_write;
224
225         while (!err) {
226                 bytes_to_write = xread(fd_in, buf, sizeof(buf));
227                 if (bytes_to_write < 0)
228                         return COPY_READ_ERROR;
229                 if (bytes_to_write == 0)
230                         break;
231                 err = packet_write_gently(fd_out, buf, bytes_to_write);
232         }
233         if (!err)
234                 err = packet_flush_gently(fd_out);
235         return err;
236 }
237
238 int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
239 {
240         int err = 0;
241         size_t bytes_written = 0;
242         size_t bytes_to_write;
243
244         while (!err) {
245                 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
246                         bytes_to_write = LARGE_PACKET_DATA_MAX;
247                 else
248                         bytes_to_write = len - bytes_written;
249                 if (bytes_to_write == 0)
250                         break;
251                 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
252                 bytes_written += bytes_to_write;
253         }
254         if (!err)
255                 err = packet_flush_gently(fd_out);
256         return err;
257 }
258
259 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
260                            void *dst, unsigned size, int options)
261 {
262         ssize_t ret;
263
264         if (fd >= 0 && src_buf && *src_buf)
265                 die("BUG: multiple sources given to packet_read");
266
267         /* Read up to "size" bytes from our source, whatever it is. */
268         if (src_buf && *src_buf) {
269                 ret = size < *src_size ? size : *src_size;
270                 memcpy(dst, *src_buf, ret);
271                 *src_buf += ret;
272                 *src_size -= ret;
273         } else {
274                 ret = read_in_full(fd, dst, size);
275                 if (ret < 0)
276                         die_errno("read error");
277         }
278
279         /* And complain if we didn't get enough bytes to satisfy the read. */
280         if (ret < size) {
281                 if (options & PACKET_READ_GENTLE_ON_EOF)
282                         return -1;
283
284                 die("The remote end hung up unexpectedly");
285         }
286
287         return ret;
288 }
289
290 static int packet_length(const char *linelen)
291 {
292         int val = hex2chr(linelen);
293         return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
294 }
295
296 int packet_read(int fd, char **src_buf, size_t *src_len,
297                 char *buffer, unsigned size, int options)
298 {
299         int len, ret;
300         char linelen[4];
301
302         ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
303         if (ret < 0)
304                 return ret;
305         len = packet_length(linelen);
306         if (len < 0)
307                 die("protocol error: bad line length character: %.4s", linelen);
308         if (!len) {
309                 packet_trace("0000", 4, 0);
310                 return 0;
311         }
312         len -= 4;
313         if (len >= size)
314                 die("protocol error: bad line length %d", len);
315         ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
316         if (ret < 0)
317                 return ret;
318
319         if ((options & PACKET_READ_CHOMP_NEWLINE) &&
320             len && buffer[len-1] == '\n')
321                 len--;
322
323         buffer[len] = 0;
324         packet_trace(buffer, len, 0);
325         return len;
326 }
327
328 static char *packet_read_line_generic(int fd,
329                                       char **src, size_t *src_len,
330                                       int *dst_len)
331 {
332         int len = packet_read(fd, src, src_len,
333                               packet_buffer, sizeof(packet_buffer),
334                               PACKET_READ_CHOMP_NEWLINE);
335         if (dst_len)
336                 *dst_len = len;
337         return (len > 0) ? packet_buffer : NULL;
338 }
339
340 char *packet_read_line(int fd, int *len_p)
341 {
342         return packet_read_line_generic(fd, NULL, NULL, len_p);
343 }
344
345 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
346 {
347         int len = packet_read(fd, NULL, NULL,
348                               packet_buffer, sizeof(packet_buffer),
349                               PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
350         if (dst_len)
351                 *dst_len = len;
352         if (dst_line)
353                 *dst_line = (len > 0) ? packet_buffer : NULL;
354         return len;
355 }
356
357 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
358 {
359         return packet_read_line_generic(-1, src, src_len, dst_len);
360 }
361
362 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
363 {
364         int packet_len;
365
366         size_t orig_len = sb_out->len;
367         size_t orig_alloc = sb_out->alloc;
368
369         for (;;) {
370                 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
371                 packet_len = packet_read(fd_in, NULL, NULL,
372                         /* strbuf_grow() above always allocates one extra byte to
373                          * store a '\0' at the end of the string. packet_read()
374                          * writes a '\0' extra byte at the end, too. Let it know
375                          * that there is already room for the extra byte.
376                          */
377                         sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
378                         PACKET_READ_GENTLE_ON_EOF);
379                 if (packet_len <= 0)
380                         break;
381                 sb_out->len += packet_len;
382         }
383
384         if (packet_len < 0) {
385                 if (orig_alloc == 0)
386                         strbuf_release(sb_out);
387                 else
388                         strbuf_setlen(sb_out, orig_len);
389                 return packet_len;
390         }
391         return sb_out->len - orig_len;
392 }