deps/libgit2/src/libgit2/transports/smart_pkt.c

filter: Statementstmt 0.0% 0 / 335 Branchbran 0.0% 0 / 222 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 557 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/transports/smart.c ↑ Summary deps/libgit2/src/libgit2/transports/smart_protocol.c »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7  
8 #include "common.h"
9  
10 #include "smart.h"
11 #include "util.h"
12 #include "netops.h"
13 #include "posix.h"
14 #include "str.h"
15  
16 #include "git2/types.h"
17 #include "git2/errors.h"
18 #include "git2/refs.h"
19 #include "git2/revwalk.h"
20  
21 #include <ctype.h>
22  
23 #define PKT_LEN_SIZE 4
24 static const char pkt_done_str[] = "0009done\n";
25 static const char pkt_flush_str[] = "0000";
26 static const char pkt_have_prefix[] = "0032have ";
27 static const char pkt_want_prefix[] = "0032want ";
28  
29 0 static int flush_pkt(git_pkt **out)
30 {
31 git_pkt *pkt;
32  
33 0 pkt = git__malloc(sizeof(git_pkt));
34 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
35  
36 0 pkt->type = GIT_PKT_FLUSH;
37 0 *out = pkt;
38  
39 0 return 0;
40 }
41  
42 /* the rest of the line will be useful for multi_ack and multi_ack_detailed */
43 0 static int ack_pkt(git_pkt **out, const char *line, size_t len)
44 {
45 git_pkt_ack *pkt;
46  
47 0 pkt = git__calloc(1, sizeof(git_pkt_ack));
48 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
49 0 pkt->type = GIT_PKT_ACK;
50  
51 0 ▶ if (git__prefixncmp(line, len, "ACK "))
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixncmp(line, len, "ACK ")) 0 0 0
52 0 goto out_err;
53 0 line += 4;
54 0 len -= 4;
55  
56 0 ▶ if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->oid, line) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->oid, line) < 0) 0 0 0
if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->oid, line) < 0) 0 0 0
57 0 goto out_err;
58 0 line += GIT_OID_HEXSZ;
59 0 len -= GIT_OID_HEXSZ;
60  
61 0 ▶ if (len && line[0] == ' ') {
Branch: 2 branches
BranchTrueFalseTotal
if (len && line[0] == ' ') { 0 0 0
if (len && line[0] == ' ') { 0 0 0
62 0 line++;
63 0 len--;
64  
65 0 ▶ if (!git__prefixncmp(line, len, "continue"))
Branch: 1 branch
BranchTrueFalseTotal
if (!git__prefixncmp(line, len, "continue")) 0 0 0
66 0 pkt->status = GIT_ACK_CONTINUE;
67 0 ▶ else if (!git__prefixncmp(line, len, "common"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "common")) 0 0 0
68 0 pkt->status = GIT_ACK_COMMON;
69 0 ▶ else if (!git__prefixncmp(line, len, "ready"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "ready")) 0 0 0
70 0 pkt->status = GIT_ACK_READY;
71 else
72 0 goto out_err;
73 }
74  
75 0 *out = (git_pkt *) pkt;
76  
77 0 return 0;
78  
79 0 out_err:
80 0 git_error_set(GIT_ERROR_NET, "error parsing ACK pkt-line");
81 0 git__free(pkt);
82 0 return -1;
83 }
84  
85 0 static int nak_pkt(git_pkt **out)
86 {
87 git_pkt *pkt;
88  
89 0 pkt = git__malloc(sizeof(git_pkt));
90 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
91  
92 0 pkt->type = GIT_PKT_NAK;
93 0 *out = pkt;
94  
95 0 return 0;
96 }
97  
98 0 static int comment_pkt(git_pkt **out, const char *line, size_t len)
99 {
100 git_pkt_comment *pkt;
101 size_t alloclen;
102  
103 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_comment), len);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_comment), len); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_comment), len); 0 0 0
104 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 0 0
105 0 pkt = git__malloc(alloclen);
106 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
107  
108 0 pkt->type = GIT_PKT_COMMENT;
109 0 memcpy(pkt->comment, line, len);
110 0 pkt->comment[len] = '\0';
111  
112 0 *out = (git_pkt *) pkt;
113  
114 0 return 0;
115 }
116  
117 0 static int err_pkt(git_pkt **out, const char *line, size_t len)
118 {
119 0 git_pkt_err *pkt = NULL;
120 size_t alloclen;
121  
122 /* Remove "ERR " from the line */
123 0 ▶ if (git__prefixncmp(line, len, "ERR "))
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixncmp(line, len, "ERR ")) 0 0 0
124 0 goto out_err;
125 0 line += 4;
126 0 len -= 4;
127  
128 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); 0 0 0
129 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); 0 0 0
130 0 pkt = git__malloc(alloclen);
131 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
132 0 pkt->type = GIT_PKT_ERR;
133 0 pkt->len = len;
134  
135 0 memcpy(pkt->error, line, len);
136 0 pkt->error[len] = '\0';
137  
138 0 *out = (git_pkt *) pkt;
139  
140 0 return 0;
141  
142 0 out_err:
143 0 git_error_set(GIT_ERROR_NET, "error parsing ERR pkt-line");
144 0 git__free(pkt);
145 0 return -1;
146 }
147  
148 0 static int data_pkt(git_pkt **out, const char *line, size_t len)
149 {
150 git_pkt_data *pkt;
151 size_t alloclen;
152  
153 0 line++;
154 0 len--;
155  
156 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); 0 0 0
157 0 pkt = git__malloc(alloclen);
158 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
159  
160 0 pkt->type = GIT_PKT_DATA;
161 0 pkt->len = len;
162 0 memcpy(pkt->data, line, len);
163  
164 0 *out = (git_pkt *) pkt;
165  
166 0 return 0;
167 }
168  
169 0 static int sideband_progress_pkt(git_pkt **out, const char *line, size_t len)
170 {
171 git_pkt_progress *pkt;
172 size_t alloclen;
173  
174 0 line++;
175 0 len--;
176  
177 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_pkt_progress), len); 0 0 0
178 0 pkt = git__malloc(alloclen);
179 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
180  
181 0 pkt->type = GIT_PKT_PROGRESS;
182 0 pkt->len = len;
183 0 memcpy(pkt->data, line, len);
184  
185 0 *out = (git_pkt *) pkt;
186  
187 0 return 0;
188 }
189  
190 0 static int sideband_error_pkt(git_pkt **out, const char *line, size_t len)
191 {
192 git_pkt_err *pkt;
193 size_t alloc_len;
194  
195 0 line++;
196 0 len--;
197  
198 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(git_pkt_err), len);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(git_pkt_err), len); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(git_pkt_err), len); 0 0 0
199 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1); 0 0 0
200 0 pkt = git__malloc(alloc_len);
201 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
202  
203 0 pkt->type = GIT_PKT_ERR;
204 0 pkt->len = (int)len;
205 0 memcpy(pkt->error, line, len);
206 0 pkt->error[len] = '\0';
207  
208 0 *out = (git_pkt *)pkt;
209  
210 0 return 0;
211 }
212  
213 /*
214 * Parse an other-ref line.
215 */
216 0 static int ref_pkt(git_pkt **out, const char *line, size_t len)
217 {
218 git_pkt_ref *pkt;
219 size_t alloclen;
220  
221 0 pkt = git__calloc(1, sizeof(git_pkt_ref));
222 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
223 0 pkt->type = GIT_PKT_REF;
224  
225 0 ▶ if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->head.oid, line) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->head.oid, line) < 0) 0 0 0
if (len < GIT_OID_HEXSZ || git_oid_fromstr(&pkt->head.oid, line) < 0) 0 0 0
226 0 goto out_err;
227 0 line += GIT_OID_HEXSZ;
228 0 len -= GIT_OID_HEXSZ;
229  
230 0 ▶ if (git__prefixncmp(line, len, " "))
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixncmp(line, len, " ")) 0 0 0
231 0 goto out_err;
232 0 line++;
233 0 len--;
234  
235 0 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 0 0 0
236 0 goto out_err;
237  
238 0 ▶ if (line[len - 1] == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (line[len - 1] == '\n') 0 0 0
239 0 --len;
240  
241 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); 0 0 0
242 0 pkt->head.name = git__malloc(alloclen);
243 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt->head.name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt->head.name); 0 0 0
244  
245 0 memcpy(pkt->head.name, line, len);
246 0 pkt->head.name[len] = '\0';
247  
248 0 ▶ if (strlen(pkt->head.name) < len)
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(pkt->head.name) < len) 0 0 0
249 0 pkt->capabilities = strchr(pkt->head.name, '\0') + 1;
250  
251 0 *out = (git_pkt *)pkt;
252 0 return 0;
253  
254 0 out_err:
255 0 git_error_set(GIT_ERROR_NET, "error parsing REF pkt-line");
256 0 ▶ if (pkt)
Branch: 1 branch
BranchTrueFalseTotal
if (pkt) 0 0 0
257 0 git__free(pkt->head.name);
258 0 git__free(pkt);
259 0 return -1;
260 }
261  
262 0 static int ok_pkt(git_pkt **out, const char *line, size_t len)
263 {
264 git_pkt_ok *pkt;
265 size_t alloc_len;
266  
267 0 pkt = git__malloc(sizeof(*pkt));
268 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
269 0 pkt->type = GIT_PKT_OK;
270  
271 0 ▶ if (git__prefixncmp(line, len, "ok "))
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixncmp(line, len, "ok ")) 0 0 0
272 0 goto out_err;
273 0 line += 3;
274 0 len -= 3;
275  
276 0 ▶ if (len && line[len - 1] == '\n')
Branch: 2 branches
BranchTrueFalseTotal
if (len && line[len - 1] == '\n') 0 0 0
if (len && line[len - 1] == '\n') 0 0 0
277 0 --len;
278  
279 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1); 0 0 0
280 0 pkt->ref = git__malloc(alloc_len);
281 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt->ref);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt->ref); 0 0 0
282  
283 0 memcpy(pkt->ref, line, len);
284 0 pkt->ref[len] = '\0';
285  
286 0 *out = (git_pkt *)pkt;
287 0 return 0;
288  
289 0 out_err:
290 0 git_error_set(GIT_ERROR_NET, "error parsing OK pkt-line");
291 0 git__free(pkt);
292 0 return -1;
293 }
294  
295 0 static int ng_pkt(git_pkt **out, const char *line, size_t len)
296 {
297 git_pkt_ng *pkt;
298 const char *ptr, *eol;
299 size_t alloclen;
300  
301 0 pkt = git__malloc(sizeof(*pkt));
302 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
303  
304 0 pkt->ref = NULL;
305 0 pkt->type = GIT_PKT_NG;
306  
307 0 eol = line + len;
308  
309 0 ▶ if (git__prefixncmp(line, len, "ng "))
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixncmp(line, len, "ng ")) 0 0 0
310 0 goto out_err;
311 0 line += 3;
312  
313 0 ▶ if (!(ptr = memchr(line, ' ', eol - line)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(ptr = memchr(line, ' ', eol - line))) 0 0 0
314 0 goto out_err;
315 0 len = ptr - line;
316  
317 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); 0 0 0
318 0 pkt->ref = git__malloc(alloclen);
319 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt->ref);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt->ref); 0 0 0
320  
321 0 memcpy(pkt->ref, line, len);
322 0 pkt->ref[len] = '\0';
323  
324 0 line = ptr + 1;
325 0 ▶ if (line >= eol)
Branch: 1 branch
BranchTrueFalseTotal
if (line >= eol) 0 0 0
326 0 goto out_err;
327  
328 0 ▶ if (!(ptr = memchr(line, '\n', eol - line)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(ptr = memchr(line, '\n', eol - line))) 0 0 0
329 0 goto out_err;
330 0 len = ptr - line;
331  
332 0 ▶ GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); 0 0 0
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); 0 0 0
333 0 pkt->msg = git__malloc(alloclen);
334 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt->msg);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt->msg); 0 0 0
335  
336 0 memcpy(pkt->msg, line, len);
337 0 pkt->msg[len] = '\0';
338  
339 0 *out = (git_pkt *)pkt;
340 0 return 0;
341  
342 0 out_err:
343 0 git_error_set(GIT_ERROR_NET, "invalid packet line");
344 0 git__free(pkt->ref);
345 0 git__free(pkt);
346 0 return -1;
347 }
348  
349 0 static int unpack_pkt(git_pkt **out, const char *line, size_t len)
350 {
351 git_pkt_unpack *pkt;
352  
353 0 pkt = git__malloc(sizeof(*pkt));
354 0 ▶ GIT_ERROR_CHECK_ALLOC(pkt);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(pkt); 0 0 0
355 0 pkt->type = GIT_PKT_UNPACK;
356  
357 0 ▶ if (!git__prefixncmp(line, len, "unpack ok"))
Branch: 1 branch
BranchTrueFalseTotal
if (!git__prefixncmp(line, len, "unpack ok")) 0 0 0
358 0 pkt->unpack_ok = 1;
359 else
360 0 pkt->unpack_ok = 0;
361  
362 0 *out = (git_pkt *)pkt;
363 0 return 0;
364 }
365  
366 0 static int parse_len(size_t *out, const char *line, size_t linelen)
367 {
368 char num[PKT_LEN_SIZE + 1];
369 int i, k, error;
370 int32_t len;
371 const char *num_end;
372  
373 /* Not even enough for the length */
374 0 ▶ if (linelen < PKT_LEN_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (linelen < PKT_LEN_SIZE) 0 0 0
375 0 return GIT_EBUFS;
376  
377 0 memcpy(num, line, PKT_LEN_SIZE);
378 0 num[PKT_LEN_SIZE] = '\0';
379  
380 0 ▶ for (i = 0; i < PKT_LEN_SIZE; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < PKT_LEN_SIZE; ++i) { 0 0 0
381 0 ▶ if (!isxdigit(num[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!isxdigit(num[i])) { 0 0 0
382 /* Make sure there are no special characters before passing to error message */
383 0 ▶ for (k = 0; k < PKT_LEN_SIZE; ++k) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < PKT_LEN_SIZE; ++k) { 0 0 0
384 0 ▶ if(!isprint(num[k])) {
Branch: 1 branch
BranchTrueFalseTotal
if(!isprint(num[k])) { 0 0 0
385 0 num[k] = '.';
386 }
387 }
388  
389 0 git_error_set(GIT_ERROR_NET, "invalid hex digit in length: '%s'", num);
390 0 return -1;
391 }
392 }
393  
394 0 ▶ if ((error = git__strntol32(&len, num, PKT_LEN_SIZE, &num_end, 16)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git__strntol32(&len, num, PKT_LEN_SIZE, &num_end, 16)) < 0) 0 0 0
395 0 return error;
396  
397 0 ▶ if (len < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) 0 0 0
398 0 return -1;
399  
400 0 *out = (size_t) len;
401 0 return 0;
402 }
403  
404 /*
405 * As per the documentation, the syntax is:
406 *
407 * pkt-line = data-pkt / flush-pkt
408 * data-pkt = pkt-len pkt-payload
409 * pkt-len = 4*(HEXDIG)
410 * pkt-payload = (pkt-len -4)*(OCTET)
411 * flush-pkt = "0000"
412 *
413 * Which means that the first four bytes are the length of the line,
414 * in ASCII hexadecimal (including itself)
415 */
416  
417 0 int git_pkt_parse_line(
418 git_pkt **pkt, const char **endptr, const char *line, size_t linelen)
419 {
420 int error;
421 size_t len;
422  
423 0 ▶ if ((error = parse_len(&len, line, linelen)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((error = parse_len(&len, line, linelen)) < 0) { 0 0 0
424 /*
425 * If we fail to parse the length, it might be
426 * because the server is trying to send us the
427 * packfile already or because we do not yet have
428 * enough data.
429 */
430 0 ▶ if (error == GIT_EBUFS)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_EBUFS) 0 0 0
431 ;
432 0 ▶ else if (!git__prefixncmp(line, linelen, "PACK"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, linelen, "PACK")) 0 0 0
433 0 git_error_set(GIT_ERROR_NET, "unexpected pack file");
434 else
435 0 git_error_set(GIT_ERROR_NET, "bad packet length");
436 0 return error;
437 }
438  
439 /*
440 * Make sure there is enough in the buffer to satisfy
441 * this line.
442 */
443 0 ▶ if (linelen < len)
Branch: 1 branch
BranchTrueFalseTotal
if (linelen < len) 0 0 0
444 0 return GIT_EBUFS;
445  
446 /*
447 * The length has to be exactly 0 in case of a flush
448 * packet or greater than PKT_LEN_SIZE, as the decoded
449 * length includes its own encoded length of four bytes.
450 */
451 0 ▶ if (len != 0 && len < PKT_LEN_SIZE)
Branch: 2 branches
BranchTrueFalseTotal
if (len != 0 && len < PKT_LEN_SIZE) 0 0 0
if (len != 0 && len < PKT_LEN_SIZE) 0 0 0
452 0 return GIT_ERROR;
453  
454 0 line += PKT_LEN_SIZE;
455 /*
456 * The Git protocol does not specify empty lines as part
457 * of the protocol. Not knowing what to do with an empty
458 * line, we should return an error upon hitting one.
459 */
460 0 ▶ if (len == PKT_LEN_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == PKT_LEN_SIZE) { 0 0 0
461 0 git_error_set_str(GIT_ERROR_NET, "Invalid empty packet");
462 0 return GIT_ERROR;
463 }
464  
465 0 ▶ if (len == 0) { /* Flush pkt */
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { /* Flush pkt */ 0 0 0
466 0 *endptr = line;
467 0 return flush_pkt(pkt);
468 }
469  
470 0 len -= PKT_LEN_SIZE; /* the encoded length includes its own size */
471  
472 0 ▶ if (*line == GIT_SIDE_BAND_DATA)
Branch: 1 branch
BranchTrueFalseTotal
if (*line == GIT_SIDE_BAND_DATA) 0 0 0
473 0 error = data_pkt(pkt, line, len);
474 0 ▶ else if (*line == GIT_SIDE_BAND_PROGRESS)
Branch: 1 branch
BranchTrueFalseTotal
else if (*line == GIT_SIDE_BAND_PROGRESS) 0 0 0
475 0 error = sideband_progress_pkt(pkt, line, len);
476 0 ▶ else if (*line == GIT_SIDE_BAND_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
else if (*line == GIT_SIDE_BAND_ERROR) 0 0 0
477 0 error = sideband_error_pkt(pkt, line, len);
478 0 ▶ else if (!git__prefixncmp(line, len, "ACK"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "ACK")) 0 0 0
479 0 error = ack_pkt(pkt, line, len);
480 0 ▶ else if (!git__prefixncmp(line, len, "NAK"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "NAK")) 0 0 0
481 0 error = nak_pkt(pkt);
482 0 ▶ else if (!git__prefixncmp(line, len, "ERR"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "ERR")) 0 0 0
483 0 error = err_pkt(pkt, line, len);
484 0 ▶ else if (*line == '#')
Branch: 1 branch
BranchTrueFalseTotal
else if (*line == '#') 0 0 0
485 0 error = comment_pkt(pkt, line, len);
486 0 ▶ else if (!git__prefixncmp(line, len, "ok"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "ok")) 0 0 0
487 0 error = ok_pkt(pkt, line, len);
488 0 ▶ else if (!git__prefixncmp(line, len, "ng"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "ng")) 0 0 0
489 0 error = ng_pkt(pkt, line, len);
490 0 ▶ else if (!git__prefixncmp(line, len, "unpack"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!git__prefixncmp(line, len, "unpack")) 0 0 0
491 0 error = unpack_pkt(pkt, line, len);
492 else
493 0 error = ref_pkt(pkt, line, len);
494  
495 0 *endptr = line + len;
496  
497 0 return error;
498 }
499  
500 0 void git_pkt_free(git_pkt *pkt)
501 {
502 0 ▶ if (pkt == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkt == NULL) { 0 0 0
503 0 return;
504 }
505 0 ▶ if (pkt->type == GIT_PKT_REF) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkt->type == GIT_PKT_REF) { 0 0 0
506 0 git_pkt_ref *p = (git_pkt_ref *) pkt;
507 0 git__free(p->head.name);
508 0 git__free(p->head.symref_target);
509 }
510  
511 0 ▶ if (pkt->type == GIT_PKT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkt->type == GIT_PKT_OK) { 0 0 0
512 0 git_pkt_ok *p = (git_pkt_ok *) pkt;
513 0 git__free(p->ref);
514 }
515  
516 0 ▶ if (pkt->type == GIT_PKT_NG) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkt->type == GIT_PKT_NG) { 0 0 0
517 0 git_pkt_ng *p = (git_pkt_ng *) pkt;
518 0 git__free(p->ref);
519 0 git__free(p->msg);
520 }
521  
522 0 git__free(pkt);
523 }
524  
525 0 int git_pkt_buffer_flush(git_str *buf)
526 {
527 0 return git_str_put(buf, pkt_flush_str, strlen(pkt_flush_str));
528 }
529  
530 0 static int buffer_want_with_caps(const git_remote_head *head, transport_smart_caps *caps, git_str *buf)
531 {
532 0 git_str str = GIT_STR_INIT;
533 0 char oid[GIT_OID_HEXSZ +1] = {0};
534 size_t len;
535  
536 /* Prefer multi_ack_detailed */
537 0 ▶ if (caps->multi_ack_detailed)
Branch: 1 branch
BranchTrueFalseTotal
if (caps->multi_ack_detailed) 0 0 0
538 0 git_str_puts(&str, GIT_CAP_MULTI_ACK_DETAILED " ");
539 0 ▶ else if (caps->multi_ack)
Branch: 1 branch
BranchTrueFalseTotal
else if (caps->multi_ack) 0 0 0
540 0 git_str_puts(&str, GIT_CAP_MULTI_ACK " ");
541  
542 /* Prefer side-band-64k if the server supports both */
543 0 ▶ if (caps->side_band_64k)
Branch: 1 branch
BranchTrueFalseTotal
if (caps->side_band_64k) 0 0 0
544 0 git_str_printf(&str, "%s ", GIT_CAP_SIDE_BAND_64K);
545 0 ▶ else if (caps->side_band)
Branch: 1 branch
BranchTrueFalseTotal
else if (caps->side_band) 0 0 0
546 0 git_str_printf(&str, "%s ", GIT_CAP_SIDE_BAND);
547  
548 0 ▶ if (caps->include_tag)
Branch: 1 branch
BranchTrueFalseTotal
if (caps->include_tag) 0 0 0
549 0 git_str_puts(&str, GIT_CAP_INCLUDE_TAG " ");
550  
551 0 ▶ if (caps->thin_pack)
Branch: 1 branch
BranchTrueFalseTotal
if (caps->thin_pack) 0 0 0
552 0 git_str_puts(&str, GIT_CAP_THIN_PACK " ");
553  
554 0 ▶ if (caps->ofs_delta)
Branch: 1 branch
BranchTrueFalseTotal
if (caps->ofs_delta) 0 0 0
555 0 git_str_puts(&str, GIT_CAP_OFS_DELTA " ");
556  
557 0 ▶ if (git_str_oom(&str))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(&str)) 0 0 0
558 0 return -1;
559  
560 0 len = strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ +
561 0 git_str_len(&str) + 1 /* LF */;
562  
563 0 ▶ if (len > 0xffff) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0xffff) { 0 0 0
564 0 git_error_set(GIT_ERROR_NET,
565 "tried to produce packet with invalid length %" PRIuZ, len);
566 0 return -1;
567 }
568  
569 0 git_str_grow_by(buf, len);
570 0 git_oid_fmt(oid, &head->oid);
571 0 git_str_printf(buf,
572 "%04xwant %s %s\n", (unsigned int)len, oid, git_str_cstr(&str));
573 0 git_str_dispose(&str);
574  
575 0 ▶ GIT_ERROR_CHECK_ALLOC_STR(buf);
Branch: 2 branches
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC_STR(buf); 0 0 0
GIT_ERROR_CHECK_ALLOC_STR(buf); 0 0 0
576  
577 0 return 0;
578 }
579  
580 /*
581 * All "want" packets have the same length and format, so what we do
582 * is overwrite the OID each time.
583 */
584  
585 0 int git_pkt_buffer_wants(
586 const git_remote_head * const *refs,
587 size_t count,
588 transport_smart_caps *caps,
589 git_str *buf)
590 {
591 0 size_t i = 0;
592 const git_remote_head *head;
593  
594 0 ▶ if (caps->common) {
Branch: 1 branch
BranchTrueFalseTotal
if (caps->common) { 0 0 0
595 0 ▶ for (; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < count; ++i) { 0 0 0
596 0 head = refs[i];
597 0 ▶ if (!head->local)
Branch: 1 branch
BranchTrueFalseTotal
if (!head->local) 0 0 0
598 0 break;
599 }
600  
601 0 ▶ if (buffer_want_with_caps(refs[i], caps, buf) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (buffer_want_with_caps(refs[i], caps, buf) < 0) 0 0 0
602 0 return -1;
603  
604 0 i++;
605 }
606  
607 0 ▶ for (; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < count; ++i) { 0 0 0
608 char oid[GIT_OID_HEXSZ];
609  
610 0 head = refs[i];
611 0 ▶ if (head->local)
Branch: 1 branch
BranchTrueFalseTotal
if (head->local) 0 0 0
612 0 continue;
613  
614 0 git_oid_fmt(oid, &head->oid);
615 0 git_str_put(buf, pkt_want_prefix, strlen(pkt_want_prefix));
616 0 git_str_put(buf, oid, GIT_OID_HEXSZ);
617 0 git_str_putc(buf, '\n');
618 0 ▶ if (git_str_oom(buf))
Branch: 1 branch
BranchTrueFalseTotal
if (git_str_oom(buf)) 0 0 0
619 0 return -1;
620 }
621  
622 0 return git_pkt_buffer_flush(buf);
623 }
624  
625 0 int git_pkt_buffer_have(git_oid *oid, git_str *buf)
626 {
627 char oidhex[GIT_OID_HEXSZ + 1];
628  
629 0 memset(oidhex, 0x0, sizeof(oidhex));
630 0 git_oid_fmt(oidhex, oid);
631 0 return git_str_printf(buf, "%s%s\n", pkt_have_prefix, oidhex);
632 }
633  
634 0 int git_pkt_buffer_done(git_str *buf)
635 {
636 0 return git_str_puts(buf, pkt_done_str);
637 }
« deps/libgit2/src/libgit2/transports/smart.c ↑ Summary deps/libgit2/src/libgit2/transports/smart_protocol.c »