pkt-line.con commit pkt-line: provide a LARGE_PACKET_MAX static buffer (74543a0)
   1#include "cache.h"
   2#include "pkt-line.h"
   3
   4char packet_buffer[LARGE_PACKET_MAX];
   5static const char *packet_trace_prefix = "git";
   6static const char trace_key[] = "GIT_TRACE_PACKET";
   7
   8void packet_trace_identity(const char *prog)
   9{
  10        packet_trace_prefix = xstrdup(prog);
  11}
  12
  13static 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 */
  54void packet_flush(int fd)
  55{
  56        packet_trace("0000", 4, 1);
  57        write_or_die(fd, "0000", 4);
  58}
  59
  60void 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])
  67static char buffer[1000];
  68static 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
  85void 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
  96void 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
 107static int safe_read(int fd, void *buffer, unsigned size, int options)
 108{
 109        ssize_t ret = read_in_full(fd, buffer, size);
 110        if (ret < 0)
 111                die_errno("read error");
 112        else if (ret < size) {
 113                if (options & PACKET_READ_GENTLE_ON_EOF)
 114                        return -1;
 115
 116                die("The remote end hung up unexpectedly");
 117        }
 118
 119        return ret;
 120}
 121
 122static int packet_length(const char *linelen)
 123{
 124        int n;
 125        int len = 0;
 126
 127        for (n = 0; n < 4; n++) {
 128                unsigned char c = linelen[n];
 129                len <<= 4;
 130                if (c >= '0' && c <= '9') {
 131                        len += c - '0';
 132                        continue;
 133                }
 134                if (c >= 'a' && c <= 'f') {
 135                        len += c - 'a' + 10;
 136                        continue;
 137                }
 138                if (c >= 'A' && c <= 'F') {
 139                        len += c - 'A' + 10;
 140                        continue;
 141                }
 142                return -1;
 143        }
 144        return len;
 145}
 146
 147int packet_read(int fd, char *buffer, unsigned size, int options)
 148{
 149        int len, ret;
 150        char linelen[4];
 151
 152        ret = safe_read(fd, linelen, 4, options);
 153        if (ret < 0)
 154                return ret;
 155        len = packet_length(linelen);
 156        if (len < 0)
 157                die("protocol error: bad line length character: %.4s", linelen);
 158        if (!len) {
 159                packet_trace("0000", 4, 0);
 160                return 0;
 161        }
 162        len -= 4;
 163        if (len >= size)
 164                die("protocol error: bad line length %d", len);
 165        ret = safe_read(fd, buffer, len, options);
 166        if (ret < 0)
 167                return ret;
 168
 169        if ((options & PACKET_READ_CHOMP_NEWLINE) &&
 170            len && buffer[len-1] == '\n')
 171                len--;
 172
 173        buffer[len] = 0;
 174        packet_trace(buffer, len, 0);
 175        return len;
 176}
 177
 178char *packet_read_line(int fd, int *len_p)
 179{
 180        int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
 181                              PACKET_READ_CHOMP_NEWLINE);
 182        if (len_p)
 183                *len_p = len;
 184        return len ? packet_buffer : NULL;
 185}
 186
 187int packet_get_line(struct strbuf *out,
 188        char **src_buf, size_t *src_len)
 189{
 190        int len;
 191
 192        if (*src_len < 4)
 193                return -1;
 194        len = packet_length(*src_buf);
 195        if (len < 0)
 196                return -1;
 197        if (!len) {
 198                *src_buf += 4;
 199                *src_len -= 4;
 200                packet_trace("0000", 4, 0);
 201                return 0;
 202        }
 203        if (*src_len < len)
 204                return -2;
 205
 206        *src_buf += 4;
 207        *src_len -= 4;
 208        len -= 4;
 209
 210        strbuf_add(out, *src_buf, len);
 211        *src_buf += len;
 212        *src_len -= len;
 213        packet_trace(out->buf, out->len, 0);
 214        return len;
 215}