]> git.proxmox.com Git - libgit2.git/blame - src/pkt.c
Lay the foundations for pkt-line parsing
[libgit2.git] / src / pkt.c
CommitLineData
f7fc68df
CMN
1/*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26#include "git2/pkt.h"
27#include "git2/types.h"
28#include "git2/errors.h"
29
30#include "common.h"
31#include "util.h"
32
33/*
34 * As per the documentation, the syntax is:
35 *
36 * pkt-line = data-pkt / flush-pkt
37 * data-pkt = pkt-len pkt-payload
38 * pkt-len = 4*(HEXDIG)
39 * pkt-payload = (pkt-len -4)*(OCTET)
40 * flush-pkt = "0000"
41 *
42 * Which means that the first four bytes are the length of the line,
43 * in ASCII hexadecimal (including itself)
44 */
45
46int git_pkt_parse_line(git_pkt **head, const char *line, const char **out)
47{
48 int error = GIT_SUCCESS;
49 long int len;
50 const char *num_end;
51 git_pkt *pkt;
52
53 error = git__strtol32(&len, line, &num_end, 16);
54 if (error < GIT_SUCCESS)
55 return error;
56
57 /*
58 * TODO: How do we deal with empty lines? Try again? with the next
59 * line?
60 */
61 if (len == 4) {
62 *out = num_end;
63 return GIT_SUCCESS;
64 }
65
66 if (len == 0) { /* Flush, should go into its own fn */
67 pkt = git__malloc(sizeof(git_pkt));
68 if (pkt == NULL)
69 return GIT_ENOMEM;
70
71 pkt->type = GIT_PKT_FLUSH;
72 *head = pkt;
73 *out = num_end;
74 return GIT_SUCCESS;
75 }
76
77 /* TODO: Write the rest of this thing */
78
79 return GIT_SUCCESS;
80}