4 char packet_buffer
[LARGE_PACKET_MAX
];
5 static const char *packet_trace_prefix
= "git";
6 static struct trace_key trace_packet
= TRACE_KEY_INIT(PACKET
);
8 void packet_trace_identity(const char *prog
)
10 packet_trace_prefix
= xstrdup(prog
);
13 static void packet_trace(const char *buf
, unsigned int len
, int write
)
18 if (!trace_want(&trace_packet
))
21 /* +32 is just a guess for header + quoting */
22 strbuf_init(&out
, len
+32);
24 strbuf_addf(&out
, "packet: %12s%c ",
25 packet_trace_prefix
, write
? '>' : '<');
27 if ((len
>= 4 && starts_with(buf
, "PACK")) ||
28 (len
>= 5 && starts_with(buf
+1, "PACK"))) {
29 strbuf_addstr(&out
, "PACK ...");
30 trace_disable(&trace_packet
);
33 /* XXX we should really handle printable utf8 */
34 for (i
= 0; i
< len
; i
++) {
35 /* suppress newlines */
38 if (buf
[i
] >= 0x20 && buf
[i
] <= 0x7e)
39 strbuf_addch(&out
, buf
[i
]);
41 strbuf_addf(&out
, "\\%o", buf
[i
]);
45 strbuf_addch(&out
, '\n');
46 trace_strbuf(&trace_packet
, &out
);
51 * If we buffered things up above (we don't, but we should),
54 void packet_flush(int fd
)
56 packet_trace("0000", 4, 1);
57 write_or_die(fd
, "0000", 4);
60 void packet_buf_flush(struct strbuf
*buf
)
62 packet_trace("0000", 4, 1);
63 strbuf_add(buf
, "0000", 4);
66 #define hex(a) (hexchar[(a) & 15])
67 static void format_packet(struct strbuf
*out
, const char *fmt
, va_list args
)
69 static char hexchar
[] = "0123456789abcdef";
73 strbuf_addstr(out
, "0000");
74 strbuf_vaddf(out
, fmt
, args
);
75 n
= out
->len
- orig_len
;
77 if (n
> LARGE_PACKET_MAX
)
78 die("protocol error: impossibly long line");
80 out
->buf
[orig_len
+ 0] = hex(n
>> 12);
81 out
->buf
[orig_len
+ 1] = hex(n
>> 8);
82 out
->buf
[orig_len
+ 2] = hex(n
>> 4);
83 out
->buf
[orig_len
+ 3] = hex(n
);
84 packet_trace(out
->buf
+ orig_len
+ 4, n
- 4, 1);
87 void packet_write(int fd
, const char *fmt
, ...)
89 static struct strbuf buf
= STRBUF_INIT
;
94 format_packet(&buf
, fmt
, args
);
96 write_or_die(fd
, buf
.buf
, buf
.len
);
99 void packet_buf_write(struct strbuf
*buf
, const char *fmt
, ...)
104 format_packet(buf
, fmt
, args
);
108 static int get_packet_data(int fd
, char **src_buf
, size_t *src_size
,
109 void *dst
, unsigned size
, int options
)
113 if (fd
>= 0 && src_buf
&& *src_buf
)
114 die("BUG: multiple sources given to packet_read");
116 /* Read up to "size" bytes from our source, whatever it is. */
117 if (src_buf
&& *src_buf
) {
118 ret
= size
< *src_size
? size
: *src_size
;
119 memcpy(dst
, *src_buf
, ret
);
123 ret
= read_in_full(fd
, dst
, size
);
125 die_errno("read error");
128 /* And complain if we didn't get enough bytes to satisfy the read. */
130 if (options
& PACKET_READ_GENTLE_ON_EOF
)
133 die("The remote end hung up unexpectedly");
139 static int packet_length(const char *linelen
)
144 for (n
= 0; n
< 4; n
++) {
145 unsigned char c
= linelen
[n
];
147 if (c
>= '0' && c
<= '9') {
151 if (c
>= 'a' && c
<= 'f') {
155 if (c
>= 'A' && c
<= 'F') {
164 int packet_read(int fd
, char **src_buf
, size_t *src_len
,
165 char *buffer
, unsigned size
, int options
)
170 ret
= get_packet_data(fd
, src_buf
, src_len
, linelen
, 4, options
);
173 len
= packet_length(linelen
);
175 die("protocol error: bad line length character: %.4s", linelen
);
177 packet_trace("0000", 4, 0);
182 die("protocol error: bad line length %d", len
);
183 ret
= get_packet_data(fd
, src_buf
, src_len
, buffer
, len
, options
);
187 if ((options
& PACKET_READ_CHOMP_NEWLINE
) &&
188 len
&& buffer
[len
-1] == '\n')
192 packet_trace(buffer
, len
, 0);
196 static char *packet_read_line_generic(int fd
,
197 char **src
, size_t *src_len
,
200 int len
= packet_read(fd
, src
, src_len
,
201 packet_buffer
, sizeof(packet_buffer
),
202 PACKET_READ_CHOMP_NEWLINE
);
205 return len
? packet_buffer
: NULL
;
208 char *packet_read_line(int fd
, int *len_p
)
210 return packet_read_line_generic(fd
, NULL
, NULL
, len_p
);
213 char *packet_read_line_buf(char **src
, size_t *src_len
, int *dst_len
)
215 return packet_read_line_generic(-1, src
, src_len
, dst_len
);