pkt-line.con commit mailinfo: handle_commit_msg() shouldn't be called after finding patchbreak (4933910)
   1#include "cache.h"
   2#include "pkt-line.h"
   3
   4char packet_buffer[LARGE_PACKET_MAX];
   5static const char *packet_trace_prefix = "git";
   6static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
   7static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
   8
   9void packet_trace_identity(const char *prog)
  10{
  11        packet_trace_prefix = xstrdup(prog);
  12}
  13
  14static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
  15{
  16        if (!sideband) {
  17                trace_verbatim(&trace_pack, buf, len);
  18                return 1;
  19        } else if (len && *buf == '\1') {
  20                trace_verbatim(&trace_pack, buf + 1, len - 1);
  21                return 1;
  22        } else {
  23                /* it's another non-pack sideband */
  24                return 0;
  25        }
  26}
  27
  28static void packet_trace(const char *buf, unsigned int len, int write)
  29{
  30        int i;
  31        struct strbuf out;
  32        static int in_pack, sideband;
  33
  34        if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
  35                return;
  36
  37        if (in_pack) {
  38                if (packet_trace_pack(buf, len, sideband))
  39                        return;
  40        } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
  41                in_pack = 1;
  42                sideband = *buf == '\1';
  43                packet_trace_pack(buf, len, sideband);
  44
  45                /*
  46                 * Make a note in the human-readable trace that the pack data
  47                 * started.
  48                 */
  49                buf = "PACK ...";
  50                len = strlen(buf);
  51        }
  52
  53        if (!trace_want(&trace_packet))
  54                return;
  55
  56        /* +32 is just a guess for header + quoting */
  57        strbuf_init(&out, len+32);
  58
  59        strbuf_addf(&out, "packet: %12s%c ",
  60                    packet_trace_prefix, write ? '>' : '<');
  61
  62        /* XXX we should really handle printable utf8 */
  63        for (i = 0; i < len; i++) {
  64                /* suppress newlines */
  65                if (buf[i] == '\n')
  66                        continue;
  67                if (buf[i] >= 0x20 && buf[i] <= 0x7e)
  68                        strbuf_addch(&out, buf[i]);
  69                else
  70                        strbuf_addf(&out, "\\%o", buf[i]);
  71        }
  72
  73        strbuf_addch(&out, '\n');
  74        trace_strbuf(&trace_packet, &out);
  75        strbuf_release(&out);
  76}
  77
  78/*
  79 * If we buffered things up above (we don't, but we should),
  80 * we'd flush it here
  81 */
  82void packet_flush(int fd)
  83{
  84        packet_trace("0000", 4, 1);
  85        write_or_die(fd, "0000", 4);
  86}
  87
  88void packet_buf_flush(struct strbuf *buf)
  89{
  90        packet_trace("0000", 4, 1);
  91        strbuf_add(buf, "0000", 4);
  92}
  93
  94#define hex(a) (hexchar[(a) & 15])
  95static void format_packet(struct strbuf *out, const char *fmt, va_list args)
  96{
  97        static char hexchar[] = "0123456789abcdef";
  98        size_t orig_len, n;
  99
 100        orig_len = out->len;
 101        strbuf_addstr(out, "0000");
 102        strbuf_vaddf(out, fmt, args);
 103        n = out->len - orig_len;
 104
 105        if (n > LARGE_PACKET_MAX)
 106                die("protocol error: impossibly long line");
 107
 108        out->buf[orig_len + 0] = hex(n >> 12);
 109        out->buf[orig_len + 1] = hex(n >> 8);
 110        out->buf[orig_len + 2] = hex(n >> 4);
 111        out->buf[orig_len + 3] = hex(n);
 112        packet_trace(out->buf + orig_len + 4, n - 4, 1);
 113}
 114
 115void packet_write(int fd, const char *fmt, ...)
 116{
 117        static struct strbuf buf = STRBUF_INIT;
 118        va_list args;
 119
 120        strbuf_reset(&buf);
 121        va_start(args, fmt);
 122        format_packet(&buf, fmt, args);
 123        va_end(args);
 124        write_or_die(fd, buf.buf, buf.len);
 125}
 126
 127void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
 128{
 129        va_list args;
 130
 131        va_start(args, fmt);
 132        format_packet(buf, fmt, args);
 133        va_end(args);
 134}
 135
 136static int get_packet_data(int fd, char **src_buf, size_t *src_size,
 137                           void *dst, unsigned size, int options)
 138{
 139        ssize_t ret;
 140
 141        if (fd >= 0 && src_buf && *src_buf)
 142                die("BUG: multiple sources given to packet_read");
 143
 144        /* Read up to "size" bytes from our source, whatever it is. */
 145        if (src_buf && *src_buf) {
 146                ret = size < *src_size ? size : *src_size;
 147                memcpy(dst, *src_buf, ret);
 148                *src_buf += ret;
 149                *src_size -= ret;
 150        } else {
 151                ret = read_in_full(fd, dst, size);
 152                if (ret < 0)
 153                        die_errno("read error");
 154        }
 155
 156        /* And complain if we didn't get enough bytes to satisfy the read. */
 157        if (ret < size) {
 158                if (options & PACKET_READ_GENTLE_ON_EOF)
 159                        return -1;
 160
 161                die("The remote end hung up unexpectedly");
 162        }
 163
 164        return ret;
 165}
 166
 167static int packet_length(const char *linelen)
 168{
 169        int n;
 170        int len = 0;
 171
 172        for (n = 0; n < 4; n++) {
 173                unsigned char c = linelen[n];
 174                len <<= 4;
 175                if (c >= '0' && c <= '9') {
 176                        len += c - '0';
 177                        continue;
 178                }
 179                if (c >= 'a' && c <= 'f') {
 180                        len += c - 'a' + 10;
 181                        continue;
 182                }
 183                if (c >= 'A' && c <= 'F') {
 184                        len += c - 'A' + 10;
 185                        continue;
 186                }
 187                return -1;
 188        }
 189        return len;
 190}
 191
 192int packet_read(int fd, char **src_buf, size_t *src_len,
 193                char *buffer, unsigned size, int options)
 194{
 195        int len, ret;
 196        char linelen[4];
 197
 198        ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
 199        if (ret < 0)
 200                return ret;
 201        len = packet_length(linelen);
 202        if (len < 0)
 203                die("protocol error: bad line length character: %.4s", linelen);
 204        if (!len) {
 205                packet_trace("0000", 4, 0);
 206                return 0;
 207        }
 208        len -= 4;
 209        if (len >= size)
 210                die("protocol error: bad line length %d", len);
 211        ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
 212        if (ret < 0)
 213                return ret;
 214
 215        if ((options & PACKET_READ_CHOMP_NEWLINE) &&
 216            len && buffer[len-1] == '\n')
 217                len--;
 218
 219        buffer[len] = 0;
 220        packet_trace(buffer, len, 0);
 221        return len;
 222}
 223
 224static char *packet_read_line_generic(int fd,
 225                                      char **src, size_t *src_len,
 226                                      int *dst_len)
 227{
 228        int len = packet_read(fd, src, src_len,
 229                              packet_buffer, sizeof(packet_buffer),
 230                              PACKET_READ_CHOMP_NEWLINE);
 231        if (dst_len)
 232                *dst_len = len;
 233        return len ? packet_buffer : NULL;
 234}
 235
 236char *packet_read_line(int fd, int *len_p)
 237{
 238        return packet_read_line_generic(fd, NULL, NULL, len_p);
 239}
 240
 241char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
 242{
 243        return packet_read_line_generic(-1, src, src_len, dst_len);
 244}