pkt-line.con commit Add branch --set-upstream (4fc5006)
   1#include "cache.h"
   2#include "pkt-line.h"
   3
   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).
   9 *
  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).
  15 *
  16 * The writing side could use stdio, but since the reading
  17 * side can't, we stay with pure read/write interfaces.
  18 */
  19ssize_t safe_write(int fd, const void *buf, ssize_t n)
  20{
  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;
  28                }
  29                if (!ret)
  30                        die("write error (disk full?)");
  31                die_errno("write error");
  32        }
  33        return nn;
  34}
  35
  36/*
  37 * If we buffered things up above (we don't, but we should),
  38 * we'd flush it here
  39 */
  40void packet_flush(int fd)
  41{
  42        safe_write(fd, "0000", 4);
  43}
  44
  45void packet_buf_flush(struct strbuf *buf)
  46{
  47        strbuf_add(buf, "0000", 4);
  48}
  49
  50#define hex(a) (hexchar[(a) & 15])
  51static char buffer[1000];
  52static unsigned format_packet(const char *fmt, va_list args)
  53{
  54        static char hexchar[] = "0123456789abcdef";
  55        unsigned n;
  56
  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;
  66}
  67
  68void packet_write(int fd, const char *fmt, ...)
  69{
  70        va_list args;
  71        unsigned n;
  72
  73        va_start(args, fmt);
  74        n = format_packet(fmt, args);
  75        va_end(args);
  76        safe_write(fd, buffer, n);
  77}
  78
  79void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
  80{
  81        va_list args;
  82        unsigned n;
  83
  84        va_start(args, fmt);
  85        n = format_packet(fmt, args);
  86        va_end(args);
  87        strbuf_add(buf, buffer, n);
  88}
  89
  90static void safe_read(int fd, void *buffer, unsigned size)
  91{
  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");
  97}
  98
  99static int packet_length(const char *linelen)
 100{
 101        int n;
 102        int len = 0;
 103
 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;
 110                }
 111                if (c >= 'a' && c <= 'f') {
 112                        len += c - 'a' + 10;
 113                        continue;
 114                }
 115                if (c >= 'A' && c <= 'F') {
 116                        len += c - 'A' + 10;
 117                        continue;
 118                }
 119                return -1;
 120        }
 121        return len;
 122}
 123
 124int packet_read_line(int fd, char *buffer, unsigned size)
 125{
 126        int len;
 127        char linelen[4];
 128
 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;
 141}
 142
 143int packet_get_line(struct strbuf *out,
 144        char **src_buf, size_t *src_len)
 145{
 146        int len;
 147
 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;
 157        }
 158        if (*src_len < len)
 159                return -2;
 160
 161        *src_buf += 4;
 162        *src_len -= 4;
 163        len -= 4;
 164
 165        strbuf_add(out, *src_buf, len);
 166        *src_buf += len;
 167        *src_len -= len;
 168        return len;
 169}