Add stream helper library
[git/jrn.git] / pkt-line.c
blob295ba2b16c8675d8c5c5d0a5ee6e61e044ce99dd
1 #include "cache.h"
2 #include "pkt-line.h"
4 /*
5 * Write a packetized stream, where each line is preceded by
6 * its length (including the header) as a 4-byte hex number.
7 * A length of 'zero' means end of stream (and a length of 1-3
8 * would be an error).
10 * This is all pretty stupid, but we use this packetized line
11 * format to make a streaming format possible without ever
12 * over-running the read buffers. That way we'll never read
13 * into what might be the pack data (which should go to another
14 * process entirely).
16 * The writing side could use stdio, but since the reading
17 * side can't, we stay with pure read/write interfaces.
19 ssize_t safe_write(int fd, const void *buf, ssize_t n)
21 ssize_t nn = n;
22 while (n) {
23 int ret = xwrite(fd, buf, n);
24 if (ret > 0) {
25 buf = (char *) buf + ret;
26 n -= ret;
27 continue;
29 if (!ret)
30 die("write error (disk full?)");
31 die_errno("write error");
33 return nn;
37 * If we buffered things up above (we don't, but we should),
38 * we'd flush it here
40 void packet_flush(int fd)
42 safe_write(fd, "0000", 4);
45 void packet_buf_flush(struct strbuf *buf)
47 strbuf_add(buf, "0000", 4);
50 #define hex(a) (hexchar[(a) & 15])
51 static char buffer[1000];
52 static unsigned format_packet(const char *fmt, va_list args)
54 static char hexchar[] = "0123456789abcdef";
55 unsigned n;
57 n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
58 if (n >= sizeof(buffer)-4)
59 die("protocol error: impossibly long line");
60 n += 4;
61 buffer[0] = hex(n >> 12);
62 buffer[1] = hex(n >> 8);
63 buffer[2] = hex(n >> 4);
64 buffer[3] = hex(n);
65 return n;
68 void packet_write(int fd, const char *fmt, ...)
70 va_list args;
71 unsigned n;
73 va_start(args, fmt);
74 n = format_packet(fmt, args);
75 va_end(args);
76 safe_write(fd, buffer, n);
79 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
81 va_list args;
82 unsigned n;
84 va_start(args, fmt);
85 n = format_packet(fmt, args);
86 va_end(args);
87 strbuf_add(buf, buffer, n);
90 static void safe_read(int fd, void *buffer, unsigned size)
92 ssize_t ret = read_in_full(fd, buffer, size);
93 if (ret < 0)
94 die_errno("read error");
95 else if (ret < size)
96 die("The remote end hung up unexpectedly");
99 static int packet_length(const char *linelen)
101 int n;
102 int len = 0;
104 for (n = 0; n < 4; n++) {
105 unsigned char c = linelen[n];
106 len <<= 4;
107 if (c >= '0' && c <= '9') {
108 len += c - '0';
109 continue;
111 if (c >= 'a' && c <= 'f') {
112 len += c - 'a' + 10;
113 continue;
115 if (c >= 'A' && c <= 'F') {
116 len += c - 'A' + 10;
117 continue;
119 return -1;
121 return len;
124 int packet_read_line(int fd, char *buffer, unsigned size)
126 int len;
127 char linelen[4];
129 safe_read(fd, linelen, 4);
130 len = packet_length(linelen);
131 if (len < 0)
132 die("protocol error: bad line length character: %.4s", linelen);
133 if (!len)
134 return 0;
135 len -= 4;
136 if (len >= size)
137 die("protocol error: bad line length %d", len);
138 safe_read(fd, buffer, len);
139 buffer[len] = 0;
140 return len;
143 int packet_get_line(struct strbuf *out,
144 char **src_buf, size_t *src_len)
146 int len;
148 if (*src_len < 4)
149 return -1;
150 len = packet_length(*src_buf);
151 if (len < 0)
152 return -1;
153 if (!len) {
154 *src_buf += 4;
155 *src_len -= 4;
156 return 0;
158 if (*src_len < len)
159 return -2;
161 *src_buf += 4;
162 *src_len -= 4;
163 len -= 4;
165 strbuf_add(out, *src_buf, len);
166 *src_buf += len;
167 *src_len -= len;
168 return len;