pkt-line: share buffer/descriptor reading implementation
[git/git.git] / pkt-line.c
1 #include "cache.h"
2 #include "pkt-line.h"
3
4 char packet_buffer[LARGE_PACKET_MAX];
5 static const char *packet_trace_prefix = "git";
6 static const char trace_key[] = "GIT_TRACE_PACKET";
7
8 void packet_trace_identity(const char *prog)
9 {
10 packet_trace_prefix = xstrdup(prog);
11 }
12
13 static void packet_trace(const char *buf, unsigned int len, int write)
14 {
15 int i;
16 struct strbuf out;
17
18 if (!trace_want(trace_key))
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
27 if ((len >= 4 && !prefixcmp(buf, "PACK")) ||
28 (len >= 5 && !prefixcmp(buf+1, "PACK"))) {
29 strbuf_addstr(&out, "PACK ...");
30 unsetenv(trace_key);
31 }
32 else {
33 /* XXX we should really handle printable utf8 */
34 for (i = 0; i < len; i++) {
35 /* suppress newlines */
36 if (buf[i] == '\n')
37 continue;
38 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
39 strbuf_addch(&out, buf[i]);
40 else
41 strbuf_addf(&out, "\\%o", buf[i]);
42 }
43 }
44
45 strbuf_addch(&out, '\n');
46 trace_strbuf(trace_key, &out);
47 strbuf_release(&out);
48 }
49
50 /*
51 * If we buffered things up above (we don't, but we should),
52 * we'd flush it here
53 */
54 void packet_flush(int fd)
55 {
56 packet_trace("0000", 4, 1);
57 write_or_die(fd, "0000", 4);
58 }
59
60 void packet_buf_flush(struct strbuf *buf)
61 {
62 packet_trace("0000", 4, 1);
63 strbuf_add(buf, "0000", 4);
64 }
65
66 #define hex(a) (hexchar[(a) & 15])
67 static char buffer[1000];
68 static unsigned format_packet(const char *fmt, va_list args)
69 {
70 static char hexchar[] = "0123456789abcdef";
71 unsigned n;
72
73 n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
74 if (n >= sizeof(buffer)-4)
75 die("protocol error: impossibly long line");
76 n += 4;
77 buffer[0] = hex(n >> 12);
78 buffer[1] = hex(n >> 8);
79 buffer[2] = hex(n >> 4);
80 buffer[3] = hex(n);
81 packet_trace(buffer+4, n-4, 1);
82 return n;
83 }
84
85 void packet_write(int fd, const char *fmt, ...)
86 {
87 va_list args;
88 unsigned n;
89
90 va_start(args, fmt);
91 n = format_packet(fmt, args);
92 va_end(args);
93 write_or_die(fd, buffer, n);
94 }
95
96 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
97 {
98 va_list args;
99 unsigned n;
100
101 va_start(args, fmt);
102 n = format_packet(fmt, args);
103 va_end(args);
104 strbuf_add(buf, buffer, n);
105 }
106
107 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
108 void *dst, unsigned size, int options)
109 {
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) {
129 if (options & PACKET_READ_GENTLE_ON_EOF)
130 return -1;
131
132 die("The remote end hung up unexpectedly");
133 }
134
135 return ret;
136 }
137
138 static int packet_length(const char *linelen)
139 {
140 int n;
141 int len = 0;
142
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 }
158 return -1;
159 }
160 return len;
161 }
162
163 int packet_read(int fd, char **src_buf, size_t *src_len,
164 char *buffer, unsigned size, int options)
165 {
166 int len, ret;
167 char linelen[4];
168
169 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
170 if (ret < 0)
171 return ret;
172 len = packet_length(linelen);
173 if (len < 0)
174 die("protocol error: bad line length character: %.4s", linelen);
175 if (!len) {
176 packet_trace("0000", 4, 0);
177 return 0;
178 }
179 len -= 4;
180 if (len >= size)
181 die("protocol error: bad line length %d", len);
182 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
183 if (ret < 0)
184 return ret;
185
186 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
187 len && buffer[len-1] == '\n')
188 len--;
189
190 buffer[len] = 0;
191 packet_trace(buffer, len, 0);
192 return len;
193 }
194
195 static char *packet_read_line_generic(int fd,
196 char **src, size_t *src_len,
197 int *dst_len)
198 {
199 int len = packet_read(fd, src, src_len,
200 packet_buffer, sizeof(packet_buffer),
201 PACKET_READ_CHOMP_NEWLINE);
202 if (dst_len)
203 *dst_len = len;
204 return len ? packet_buffer : NULL;
205 }
206
207 char *packet_read_line(int fd, int *len_p)
208 {
209 return packet_read_line_generic(fd, NULL, NULL, len_p);
210 }
211
212 char *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);
215 }