pkt-line: provide a generic reading function with options
[git/jrn.git] / pkt-line.c
blob8700cf8add94507c67959f1939b9710a7d5cbf52
1 #include "cache.h"
2 #include "pkt-line.h"
4 static const char *packet_trace_prefix = "git";
5 static const char trace_key[] = "GIT_TRACE_PACKET";
7 void packet_trace_identity(const char *prog)
9 packet_trace_prefix = xstrdup(prog);
12 static void packet_trace(const char *buf, unsigned int len, int write)
14 int i;
15 struct strbuf out;
17 if (!trace_want(trace_key))
18 return;
20 /* +32 is just a guess for header + quoting */
21 strbuf_init(&out, len+32);
23 strbuf_addf(&out, "packet: %12s%c ",
24 packet_trace_prefix, write ? '>' : '<');
26 if ((len >= 4 && !prefixcmp(buf, "PACK")) ||
27 (len >= 5 && !prefixcmp(buf+1, "PACK"))) {
28 strbuf_addstr(&out, "PACK ...");
29 unsetenv(trace_key);
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]);
44 strbuf_addch(&out, '\n');
45 trace_strbuf(trace_key, &out);
46 strbuf_release(&out);
50 * If we buffered things up above (we don't, but we should),
51 * we'd flush it here
53 void packet_flush(int fd)
55 packet_trace("0000", 4, 1);
56 write_or_die(fd, "0000", 4);
59 void packet_buf_flush(struct strbuf *buf)
61 packet_trace("0000", 4, 1);
62 strbuf_add(buf, "0000", 4);
65 #define hex(a) (hexchar[(a) & 15])
66 static char buffer[1000];
67 static unsigned format_packet(const char *fmt, va_list args)
69 static char hexchar[] = "0123456789abcdef";
70 unsigned n;
72 n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
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);
80 packet_trace(buffer+4, n-4, 1);
81 return n;
84 void packet_write(int fd, const char *fmt, ...)
86 va_list args;
87 unsigned n;
89 va_start(args, fmt);
90 n = format_packet(fmt, args);
91 va_end(args);
92 write_or_die(fd, buffer, n);
95 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
97 va_list args;
98 unsigned n;
100 va_start(args, fmt);
101 n = format_packet(fmt, args);
102 va_end(args);
103 strbuf_add(buf, buffer, n);
106 static int safe_read(int fd, void *buffer, unsigned size, int options)
108 ssize_t ret = read_in_full(fd, buffer, size);
109 if (ret < 0)
110 die_errno("read error");
111 else if (ret < size) {
112 if (options & PACKET_READ_GENTLE_ON_EOF)
113 return -1;
115 die("The remote end hung up unexpectedly");
118 return ret;
121 static int packet_length(const char *linelen)
123 int n;
124 int len = 0;
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;
133 if (c >= 'a' && c <= 'f') {
134 len += c - 'a' + 10;
135 continue;
137 if (c >= 'A' && c <= 'F') {
138 len += c - 'A' + 10;
139 continue;
141 return -1;
143 return len;
146 int packet_read(int fd, char *buffer, unsigned size, int options)
148 int len, ret;
149 char linelen[4];
151 ret = safe_read(fd, linelen, 4, options);
152 if (ret < 0)
153 return ret;
154 len = packet_length(linelen);
155 if (len < 0)
156 die("protocol error: bad line length character: %.4s", linelen);
157 if (!len) {
158 packet_trace("0000", 4, 0);
159 return 0;
161 len -= 4;
162 if (len >= size)
163 die("protocol error: bad line length %d", len);
164 ret = safe_read(fd, buffer, len, options);
165 if (ret < 0)
166 return ret;
167 buffer[len] = 0;
168 packet_trace(buffer, len, 0);
169 return len;
172 int packet_read_line(int fd, char *buffer, unsigned size)
174 return packet_read(fd, buffer, size, 0);
177 int packet_get_line(struct strbuf *out,
178 char **src_buf, size_t *src_len)
180 int len;
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;
190 packet_trace("0000", 4, 0);
191 return 0;
193 if (*src_len < len)
194 return -2;
196 *src_buf += 4;
197 *src_len -= 4;
198 len -= 4;
200 strbuf_add(out, *src_buf, len);
201 *src_buf += len;
202 *src_len -= len;
203 packet_trace(out->buf, out->len, 0);
204 return len;