Blame src/transports/smart_pkt.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/types.h"
Packit Service 20376f
#include "git2/errors.h"
Packit Service 20376f
#include "git2/refs.h"
Packit Service 20376f
#include "git2/revwalk.h"
Packit Service 20376f
Packit Service 20376f
#include "smart.h"
Packit Service 20376f
#include "util.h"
Packit Service 20376f
#include "netops.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
Packit Service 20376f
#include <ctype.h>
Packit Service 20376f
Packit Service 20376f
#define PKT_LEN_SIZE 4
Packit Service 20376f
static const char pkt_done_str[] = "0009done\n";
Packit Service 20376f
static const char pkt_flush_str[] = "0000";
Packit Service 20376f
static const char pkt_have_prefix[] = "0032have ";
Packit Service 20376f
static const char pkt_want_prefix[] = "0032want ";
Packit Service 20376f
Packit Service 20376f
static int flush_pkt(git_pkt **out)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt *pkt;
Packit Service 20376f
Packit Service 20376f
	pkt = git__malloc(sizeof(git_pkt));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
Packit Service 20376f
	pkt->type = GIT_PKT_FLUSH;
Packit Service 20376f
	*out = pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* the rest of the line will be useful for multi_ack and multi_ack_detailed */
Packit Service 20376f
static int ack_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_ack *pkt;
Packit Service 20376f
Packit Service 20376f
	pkt = git__calloc(1, sizeof(git_pkt_ack));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
	pkt->type = GIT_PKT_ACK;
Packit Service 20376f
Packit Service 20376f
	if (git__prefixncmp(line, len, "ACK "))
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	line += 4;
Packit Service 20376f
	len -= 4;
Packit Service 20376f
Packit Service 20376f
	if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->oid, line) < 0)
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	line += GIT_OID_HEXSZ;
Packit Service 20376f
	len -= GIT_OID_HEXSZ;
Packit Service 20376f
Packit Service 20376f
	if (len && line[0] == ' ') {
Packit Service 20376f
		line++;
Packit Service 20376f
		len--;
Packit Service 20376f
Packit Service 20376f
		if (!git__prefixncmp(line, len, "continue"))
Packit Service 20376f
			pkt->status = GIT_ACK_CONTINUE;
Packit Service 20376f
		else if (!git__prefixncmp(line, len, "common"))
Packit Service 20376f
			pkt->status = GIT_ACK_COMMON;
Packit Service 20376f
		else if (!git__prefixncmp(line, len, "ready"))
Packit Service 20376f
			pkt->status = GIT_ACK_READY;
Packit Service 20376f
		else
Packit Service 20376f
			goto out_err;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *) pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
out_err:
Packit Service 20376f
	giterr_set(GITERR_NET, "error parsing ACK pkt-line");
Packit Service 20376f
	git__free(pkt);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int nak_pkt(git_pkt **out)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt *pkt;
Packit Service 20376f
Packit Service 20376f
	pkt = git__malloc(sizeof(git_pkt));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
Packit Service 20376f
	pkt->type = GIT_PKT_NAK;
Packit Service 20376f
	*out = pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int comment_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_comment *pkt;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_comment), len);
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
Packit Service 20376f
	pkt = git__malloc(alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
Packit Service 20376f
	pkt->type = GIT_PKT_COMMENT;
Packit Service 20376f
	memcpy(pkt->comment, line, len);
Packit Service 20376f
	pkt->comment[len] = '\0';
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *) pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int err_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_err *pkt = NULL;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	/* Remove "ERR " from the line */
Packit Service 20376f
	if (git__prefixncmp(line, len, "ERR "))
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	line += 4;
Packit Service 20376f
	len -= 4;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
Packit Service 20376f
	pkt = git__malloc(alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
	pkt->type = GIT_PKT_ERR;
Packit Service 20376f
	pkt->len = len;
Packit Service 20376f
Packit Service 20376f
	memcpy(pkt->error, line, len);
Packit Service 20376f
	pkt->error[len] = '\0';
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *) pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
out_err:
Packit Service 20376f
	giterr_set(GITERR_NET, "error parsing ERR pkt-line");
Packit Service 20376f
	git__free(pkt);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int data_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_data *pkt;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	line++;
Packit Service 20376f
	len--;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
Packit Service 20376f
	pkt = git__malloc(alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
Packit Service 20376f
	pkt->type = GIT_PKT_DATA;
Packit Service 20376f
	pkt->len = len;
Packit Service 20376f
	memcpy(pkt->data, line, len);
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *) pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int sideband_progress_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_progress *pkt;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	line++;
Packit Service 20376f
	len--;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
Packit Service 20376f
	pkt = git__malloc(alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
Packit Service 20376f
	pkt->type = GIT_PKT_PROGRESS;
Packit Service 20376f
	pkt->len = len;
Packit Service 20376f
	memcpy(pkt->data, line, len);
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *) pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int sideband_error_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_err *pkt;
Packit Service 20376f
	size_t alloc_len;
Packit Service 20376f
Packit Service 20376f
	line++;
Packit Service 20376f
	len--;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(git_pkt_err), len);
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
Packit Service 20376f
	pkt = git__malloc(alloc_len);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
Packit Service 20376f
	pkt->type = GIT_PKT_ERR;
Packit Service 20376f
	pkt->len = (int)len;
Packit Service 20376f
	memcpy(pkt->error, line, len);
Packit Service 20376f
	pkt->error[len] = '\0';
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *)pkt;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Parse an other-ref line.
Packit Service 20376f
 */
Packit Service 20376f
static int ref_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_ref *pkt;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	pkt = git__calloc(1, sizeof(git_pkt_ref));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
	pkt->type = GIT_PKT_REF;
Packit Service 20376f
Packit Service 20376f
	if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->head.oid, line) < 0)
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	line += GIT_OID_HEXSZ;
Packit Service 20376f
	len -= GIT_OID_HEXSZ;
Packit Service 20376f
Packit Service 20376f
	if (git__prefixncmp(line, len, " "))
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	line++;
Packit Service 20376f
	len--;
Packit Service 20376f
Packit Service 20376f
	if (!len)
Packit Service 20376f
		goto out_err;
Packit Service 20376f
Packit Service 20376f
	if (line[len - 1] == '\n')
Packit Service 20376f
		--len;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, len, 1);
Packit Service 20376f
	pkt->head.name = git__malloc(alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt->head.name);
Packit Service 20376f
Packit Service 20376f
	memcpy(pkt->head.name, line, len);
Packit Service 20376f
	pkt->head.name[len] = '\0';
Packit Service 20376f
Packit Service 20376f
	if (strlen(pkt->head.name) < len)
Packit Service 20376f
		pkt->capabilities = strchr(pkt->head.name, '\0') + 1;
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *)pkt;
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
out_err:
Packit Service 20376f
	giterr_set(GITERR_NET, "error parsing REF pkt-line");
Packit Service 20376f
	if (pkt)
Packit Service 20376f
		git__free(pkt->head.name);
Packit Service 20376f
	git__free(pkt);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ok_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_ok *pkt;
Packit Service 20376f
	size_t alloc_len;
Packit Service 20376f
Packit Service 20376f
	pkt = git__malloc(sizeof(*pkt));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
	pkt->type = GIT_PKT_OK;
Packit Service 20376f
Packit Service 20376f
	if (git__prefixncmp(line, len, "ok "))
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	line += 3;
Packit Service 20376f
	len -= 3;
Packit Service 20376f
Packit Service 20376f
	if (line[len - 1] == '\n')
Packit Service 20376f
		--len;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
Packit Service 20376f
	pkt->ref = git__malloc(alloc_len);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt->ref);
Packit Service 20376f
Packit Service 20376f
	memcpy(pkt->ref, line, len);
Packit Service 20376f
	pkt->ref[len] = '\0';
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *)pkt;
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
out_err:
Packit Service 20376f
	giterr_set(GITERR_NET, "error parsing OK pkt-line");
Packit Service 20376f
	git__free(pkt);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int ng_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_ng *pkt;
Packit Service 20376f
	const char *ptr, *eol;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	pkt = git__malloc(sizeof(*pkt));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
Packit Service 20376f
	pkt->ref = NULL;
Packit Service 20376f
	pkt->type = GIT_PKT_NG;
Packit Service 20376f
Packit Service 20376f
	eol = line + len;
Packit Service 20376f
Packit Service 20376f
	if (git__prefixncmp(line, len, "ng "))
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	line += 3;
Packit Service 20376f
Packit Service 20376f
	if (!(ptr = memchr(line, ' ', eol - line)))
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	len = ptr - line;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, len, 1);
Packit Service 20376f
	pkt->ref = git__malloc(alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt->ref);
Packit Service 20376f
Packit Service 20376f
	memcpy(pkt->ref, line, len);
Packit Service 20376f
	pkt->ref[len] = '\0';
Packit Service 20376f
Packit Service 20376f
	line = ptr + 1;
Packit Service 20376f
	if (line >= eol)
Packit Service 20376f
		goto out_err;
Packit Service 20376f
Packit Service 20376f
	if (!(ptr = memchr(line, '\n', eol - line)))
Packit Service 20376f
		goto out_err;
Packit Service 20376f
	len = ptr - line;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloclen, len, 1);
Packit Service 20376f
	pkt->msg = git__malloc(alloclen);
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt->msg);
Packit Service 20376f
Packit Service 20376f
	memcpy(pkt->msg, line, len);
Packit Service 20376f
	pkt->msg[len] = '\0';
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *)pkt;
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
out_err:
Packit Service 20376f
	giterr_set(GITERR_NET, "invalid packet line");
Packit Service 20376f
	git__free(pkt->ref);
Packit Service 20376f
	git__free(pkt);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int unpack_pkt(git_pkt **out, const char *line, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	git_pkt_unpack *pkt;
Packit Service 20376f
Packit Service 20376f
	pkt = git__malloc(sizeof(*pkt));
Packit Service 20376f
	GITERR_CHECK_ALLOC(pkt);
Packit Service 20376f
	pkt->type = GIT_PKT_UNPACK;
Packit Service 20376f
Packit Service 20376f
	if (!git__prefixncmp(line, len, "unpack ok"))
Packit Service 20376f
		pkt->unpack_ok = 1;
Packit Service 20376f
	else
Packit Service 20376f
		pkt->unpack_ok = 0;
Packit Service 20376f
Packit Service 20376f
	*out = (git_pkt *)pkt;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_len(size_t *out, const char *line, size_t linelen)
Packit Service 20376f
{
Packit Service 20376f
	char num[PKT_LEN_SIZE + 1];
Packit Service 20376f
	int i, k, error;
Packit Service 20376f
	int32_t len;
Packit Service 20376f
	const char *num_end;
Packit Service 20376f
Packit Service 20376f
	/* Not even enough for the length */
Packit Service 20376f
	if (linelen < PKT_LEN_SIZE)
Packit Service 20376f
		return GIT_EBUFS;
Packit Service 20376f
Packit Service 20376f
	memcpy(num, line, PKT_LEN_SIZE);
Packit Service 20376f
	num[PKT_LEN_SIZE] = '\0';
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < PKT_LEN_SIZE; ++i) {
Packit Service 20376f
		if (!isxdigit(num[i])) {
Packit Service 20376f
			/* Make sure there are no special characters before passing to error message */
Packit Service 20376f
			for (k = 0; k < PKT_LEN_SIZE; ++k) {
Packit Service 20376f
				if(!isprint(num[k])) {
Packit Service 20376f
					num[k] = '.';
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			giterr_set(GITERR_NET, "invalid hex digit in length: '%s'", num);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git__strntol32(&len, num, PKT_LEN_SIZE, &num_end, 16)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (len < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	*out = (size_t) len;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * As per the documentation, the syntax is:
Packit Service 20376f
 *
Packit Service 20376f
 * pkt-line	= data-pkt / flush-pkt
Packit Service 20376f
 * data-pkt	= pkt-len pkt-payload
Packit Service 20376f
 * pkt-len		= 4*(HEXDIG)
Packit Service 20376f
 * pkt-payload = (pkt-len -4)*(OCTET)
Packit Service 20376f
 * flush-pkt	= "0000"
Packit Service 20376f
 *
Packit Service 20376f
 * Which means that the first four bytes are the length of the line,
Packit Service 20376f
 * in ASCII hexadecimal (including itself)
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
int git_pkt_parse_line(
Packit Service 20376f
	git_pkt **pkt, const char **endptr, const char *line, size_t linelen)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	size_t len;
Packit Service 20376f
Packit Service 20376f
	if ((error = parse_len(&len, line, linelen)) < 0) {
Packit Service 20376f
		/*
Packit Service 20376f
		 * If we fail to parse the length, it might be
Packit Service 20376f
		 * because the server is trying to send us the
Packit Service 20376f
		 * packfile already or because we do not yet have
Packit Service 20376f
		 * enough data.
Packit Service 20376f
		 */
Packit Service 20376f
		if (error == GIT_EBUFS)
Packit Service 20376f
			;
Packit Service 20376f
		else if (!git__prefixncmp(line, linelen, "PACK"))
Packit Service 20376f
			giterr_set(GITERR_NET, "unexpected pack file");
Packit Service 20376f
		else
Packit Service 20376f
			giterr_set(GITERR_NET, "bad packet length");
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Make sure there is enough in the buffer to satisfy
Packit Service 20376f
	 * this line.
Packit Service 20376f
	 */
Packit Service 20376f
	if (linelen < len)
Packit Service 20376f
		return GIT_EBUFS;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * The length has to be exactly 0 in case of a flush
Packit Service 20376f
	 * packet or greater than PKT_LEN_SIZE, as the decoded
Packit Service 20376f
	 * length includes its own encoded length of four bytes.
Packit Service 20376f
	 */
Packit Service 20376f
	if (len != 0 && len < PKT_LEN_SIZE)
Packit Service 20376f
		return GIT_ERROR;
Packit Service 20376f
Packit Service 20376f
	line += PKT_LEN_SIZE;
Packit Service 20376f
	/*
Packit Service 20376f
	 * The Git protocol does not specify empty lines as part
Packit Service 20376f
	 * of the protocol. Not knowing what to do with an empty
Packit Service 20376f
	 * line, we should return an error upon hitting one.
Packit Service 20376f
	 */
Packit Service 20376f
	if (len == PKT_LEN_SIZE) {
Packit Service 20376f
		giterr_set_str(GITERR_NET, "Invalid empty packet");
Packit Service 20376f
		return GIT_ERROR;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (len == 0) { /* Flush pkt */
Packit Service 20376f
		*endptr = line;
Packit Service 20376f
		return flush_pkt(pkt);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	len -= PKT_LEN_SIZE; /* the encoded length includes its own size */
Packit Service 20376f
Packit Service 20376f
	if (*line == GIT_SIDE_BAND_DATA)
Packit Service 20376f
		error = data_pkt(pkt, line, len);
Packit Service 20376f
	else if (*line == GIT_SIDE_BAND_PROGRESS)
Packit Service 20376f
		error = sideband_progress_pkt(pkt, line, len);
Packit Service 20376f
	else if (*line == GIT_SIDE_BAND_ERROR)
Packit Service 20376f
		error = sideband_error_pkt(pkt, line, len);
Packit Service 20376f
	else if (!git__prefixncmp(line, len, "ACK"))
Packit Service 20376f
		error = ack_pkt(pkt, line, len);
Packit Service 20376f
	else if (!git__prefixncmp(line, len, "NAK"))
Packit Service 20376f
		error = nak_pkt(pkt);
Packit Service 20376f
	else if (!git__prefixncmp(line, len, "ERR"))
Packit Service 20376f
		error = err_pkt(pkt, line, len);
Packit Service 20376f
	else if (*line == '#')
Packit Service 20376f
		error = comment_pkt(pkt, line, len);
Packit Service 20376f
	else if (!git__prefixncmp(line, len, "ok"))
Packit Service 20376f
		error = ok_pkt(pkt, line, len);
Packit Service 20376f
	else if (!git__prefixncmp(line, len, "ng"))
Packit Service 20376f
		error = ng_pkt(pkt, line, len);
Packit Service 20376f
	else if (!git__prefixncmp(line, len, "unpack"))
Packit Service 20376f
		error = unpack_pkt(pkt, line, len);
Packit Service 20376f
	else
Packit Service 20376f
		error = ref_pkt(pkt, line, len);
Packit Service 20376f
Packit Service 20376f
	*endptr = line + len;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_pkt_free(git_pkt *pkt)
Packit Service 20376f
{
Packit Service 20376f
	if (pkt->type == GIT_PKT_REF) {
Packit Service 20376f
		git_pkt_ref *p = (git_pkt_ref *) pkt;
Packit Service 20376f
		git__free(p->head.name);
Packit Service 20376f
		git__free(p->head.symref_target);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (pkt->type == GIT_PKT_OK) {
Packit Service 20376f
		git_pkt_ok *p = (git_pkt_ok *) pkt;
Packit Service 20376f
		git__free(p->ref);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (pkt->type == GIT_PKT_NG) {
Packit Service 20376f
		git_pkt_ng *p = (git_pkt_ng *) pkt;
Packit Service 20376f
		git__free(p->ref);
Packit Service 20376f
		git__free(p->msg);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(pkt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pkt_buffer_flush(git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	return git_buf_put(buf, pkt_flush_str, strlen(pkt_flush_str));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int buffer_want_with_caps(const git_remote_head *head, transport_smart_caps *caps, git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	git_buf str = GIT_BUF_INIT;
Packit Service 20376f
	char oid[GIT_OID_HEXSZ +1] = {0};
Packit Service 20376f
	size_t len;
Packit Service 20376f
Packit Service 20376f
	/* Prefer multi_ack_detailed */
Packit Service 20376f
	if (caps->multi_ack_detailed)
Packit Service 20376f
		git_buf_puts(&str, GIT_CAP_MULTI_ACK_DETAILED " ");
Packit Service 20376f
	else if (caps->multi_ack)
Packit Service 20376f
		git_buf_puts(&str, GIT_CAP_MULTI_ACK " ");
Packit Service 20376f
Packit Service 20376f
	/* Prefer side-band-64k if the server supports both */
Packit Service 20376f
	if (caps->side_band_64k)
Packit Service 20376f
		git_buf_printf(&str, "%s ", GIT_CAP_SIDE_BAND_64K);
Packit Service 20376f
	else if (caps->side_band)
Packit Service 20376f
		git_buf_printf(&str, "%s ", GIT_CAP_SIDE_BAND);
Packit Service 20376f
Packit Service 20376f
	if (caps->include_tag)
Packit Service 20376f
		git_buf_puts(&str, GIT_CAP_INCLUDE_TAG " ");
Packit Service 20376f
Packit Service 20376f
	if (caps->thin_pack)
Packit Service 20376f
		git_buf_puts(&str, GIT_CAP_THIN_PACK " ");
Packit Service 20376f
Packit Service 20376f
	if (caps->ofs_delta)
Packit Service 20376f
		git_buf_puts(&str, GIT_CAP_OFS_DELTA " ");
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(&str))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	len = strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ +
Packit Service 20376f
		 git_buf_len(&str) + 1 /* LF */;
Packit Service 20376f
Packit Service 20376f
	if (len > 0xffff) {
Packit Service 20376f
		giterr_set(GITERR_NET,
Packit Service 20376f
			"tried to produce packet with invalid length %" PRIuZ, len);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_grow_by(buf, len);
Packit Service 20376f
	git_oid_fmt(oid, &head->oid);
Packit Service 20376f
	git_buf_printf(buf,
Packit Service 20376f
		"%04xwant %s %s\n", (unsigned int)len, oid, git_buf_cstr(&str);;
Packit Service 20376f
	git_buf_free(&str);
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_BUF(buf);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * All "want" packets have the same length and format, so what we do
Packit Service 20376f
 * is overwrite the OID each time.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
int git_pkt_buffer_wants(
Packit Service 20376f
	const git_remote_head * const *refs,
Packit Service 20376f
	size_t count,
Packit Service 20376f
	transport_smart_caps *caps,
Packit Service 20376f
	git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	size_t i = 0;
Packit Service 20376f
	const git_remote_head *head;
Packit Service 20376f
Packit Service 20376f
	if (caps->common) {
Packit Service 20376f
		for (; i < count; ++i) {
Packit Service 20376f
			head = refs[i];
Packit Service 20376f
			if (!head->local)
Packit Service 20376f
				break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (buffer_want_with_caps(refs[i], caps, buf) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		i++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (; i < count; ++i) {
Packit Service 20376f
		char oid[GIT_OID_HEXSZ];
Packit Service 20376f
Packit Service 20376f
		head = refs[i];
Packit Service 20376f
		if (head->local)
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		git_oid_fmt(oid, &head->oid);
Packit Service 20376f
		git_buf_put(buf, pkt_want_prefix, strlen(pkt_want_prefix));
Packit Service 20376f
		git_buf_put(buf, oid, GIT_OID_HEXSZ);
Packit Service 20376f
		git_buf_putc(buf, '\n');
Packit Service 20376f
		if (git_buf_oom(buf))
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return git_pkt_buffer_flush(buf);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pkt_buffer_have(git_oid *oid, git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	char oidhex[GIT_OID_HEXSZ + 1];
Packit Service 20376f
Packit Service 20376f
	memset(oidhex, 0x0, sizeof(oidhex));
Packit Service 20376f
	git_oid_fmt(oidhex, oid);
Packit Service 20376f
	return git_buf_printf(buf, "%s%s\n", pkt_have_prefix, oidhex);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_pkt_buffer_done(git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	return git_buf_puts(buf, pkt_done_str);
Packit Service 20376f
}