Blame


1 bd3d9e54 2021-09-05 stsp /*
2 bd3d9e54 2021-09-05 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 bd3d9e54 2021-09-05 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 bd3d9e54 2021-09-05 stsp *
5 bd3d9e54 2021-09-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 bd3d9e54 2021-09-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 bd3d9e54 2021-09-05 stsp * copyright notice and this permission notice appear in all copies.
8 bd3d9e54 2021-09-05 stsp *
9 bd3d9e54 2021-09-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 bd3d9e54 2021-09-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 bd3d9e54 2021-09-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 bd3d9e54 2021-09-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 bd3d9e54 2021-09-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 bd3d9e54 2021-09-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 bd3d9e54 2021-09-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 bd3d9e54 2021-09-05 stsp */
17 bd3d9e54 2021-09-05 stsp
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 8a8621c2 2021-09-06 naddy #include <sys/types.h>
20 bd3d9e54 2021-09-05 stsp
21 bd3d9e54 2021-09-05 stsp #include <ctype.h>
22 bd3d9e54 2021-09-05 stsp #include <stdio.h>
23 bd3d9e54 2021-09-05 stsp #include <stdlib.h>
24 bd3d9e54 2021-09-05 stsp #include <string.h>
25 bd3d9e54 2021-09-05 stsp
26 bd3d9e54 2021-09-05 stsp #include "got_error.h"
27 bd3d9e54 2021-09-05 stsp #include "got_path.h"
28 bd3d9e54 2021-09-05 stsp
29 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
30 bd3d9e54 2021-09-05 stsp
31 bd3d9e54 2021-09-05 stsp #ifndef nitems
32 bd3d9e54 2021-09-05 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
33 bd3d9e54 2021-09-05 stsp #endif
34 bd3d9e54 2021-09-05 stsp
35 3efd8e31 2022-10-23 thomas static void
36 3efd8e31 2022-10-23 thomas free_tokens(char **tokens, size_t ntokens)
37 3efd8e31 2022-10-23 thomas {
38 3efd8e31 2022-10-23 thomas int i;
39 3efd8e31 2022-10-23 thomas
40 3efd8e31 2022-10-23 thomas for (i = 0; i < ntokens; i++) {
41 3efd8e31 2022-10-23 thomas free(tokens[i]);
42 3efd8e31 2022-10-23 thomas tokens[i] = NULL;
43 3efd8e31 2022-10-23 thomas }
44 3efd8e31 2022-10-23 thomas }
45 3efd8e31 2022-10-23 thomas
46 bd3d9e54 2021-09-05 stsp static const struct got_error *
47 3efd8e31 2022-10-23 thomas tokenize_line(char **tokens, char *line, int len, int mintokens, int maxtokens)
48 bd3d9e54 2021-09-05 stsp {
49 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
50 bd3d9e54 2021-09-05 stsp char *p;
51 bd3d9e54 2021-09-05 stsp size_t i, n = 0;
52 bd3d9e54 2021-09-05 stsp
53 bd3d9e54 2021-09-05 stsp for (i = 0; i < maxtokens; i++)
54 bd3d9e54 2021-09-05 stsp tokens[i] = NULL;
55 bd3d9e54 2021-09-05 stsp
56 bd3d9e54 2021-09-05 stsp for (i = 0; n < len && i < maxtokens; i++) {
57 693bff59 2023-01-19 thomas while (n < len && isspace((unsigned char)*line)) {
58 bd3d9e54 2021-09-05 stsp line++;
59 bd3d9e54 2021-09-05 stsp n++;
60 bd3d9e54 2021-09-05 stsp }
61 bd3d9e54 2021-09-05 stsp p = line;
62 bd3d9e54 2021-09-05 stsp while (*line != '\0' && n < len &&
63 6771d425 2022-11-17 thomas (!isspace((unsigned char)*line) || i == maxtokens - 1)) {
64 bd3d9e54 2021-09-05 stsp line++;
65 bd3d9e54 2021-09-05 stsp n++;
66 bd3d9e54 2021-09-05 stsp }
67 bd3d9e54 2021-09-05 stsp tokens[i] = strndup(p, line - p);
68 bd3d9e54 2021-09-05 stsp if (tokens[i] == NULL) {
69 bd3d9e54 2021-09-05 stsp err = got_error_from_errno("strndup");
70 bd3d9e54 2021-09-05 stsp goto done;
71 bd3d9e54 2021-09-05 stsp }
72 bd3d9e54 2021-09-05 stsp /* Skip \0 field-delimiter at end of token. */
73 bd3d9e54 2021-09-05 stsp while (line[0] == '\0' && n < len) {
74 bd3d9e54 2021-09-05 stsp line++;
75 bd3d9e54 2021-09-05 stsp n++;
76 bd3d9e54 2021-09-05 stsp }
77 bd3d9e54 2021-09-05 stsp }
78 3efd8e31 2022-10-23 thomas if (i < mintokens)
79 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
80 3efd8e31 2022-10-23 thomas "pkt-line contains too few tokens");
81 bd3d9e54 2021-09-05 stsp done:
82 3efd8e31 2022-10-23 thomas if (err)
83 3efd8e31 2022-10-23 thomas free_tokens(tokens, i);
84 bd3d9e54 2021-09-05 stsp return err;
85 bd3d9e54 2021-09-05 stsp }
86 bd3d9e54 2021-09-05 stsp
87 bd3d9e54 2021-09-05 stsp const struct got_error *
88 bd3d9e54 2021-09-05 stsp got_gitproto_parse_refline(char **id_str, char **refname,
89 bd3d9e54 2021-09-05 stsp char **server_capabilities, char *line, int len)
90 bd3d9e54 2021-09-05 stsp {
91 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
92 bd3d9e54 2021-09-05 stsp char *tokens[3];
93 bd3d9e54 2021-09-05 stsp
94 35add24a 2021-10-08 thomas *id_str = NULL;
95 35add24a 2021-10-08 thomas *refname = NULL;
96 35add24a 2021-10-08 thomas /* don't reset *server_capabilities */
97 35add24a 2021-10-08 thomas
98 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 2, nitems(tokens));
99 bd3d9e54 2021-09-05 stsp if (err)
100 bd3d9e54 2021-09-05 stsp return err;
101 bd3d9e54 2021-09-05 stsp
102 bd3d9e54 2021-09-05 stsp if (tokens[0])
103 bd3d9e54 2021-09-05 stsp *id_str = tokens[0];
104 bd3d9e54 2021-09-05 stsp if (tokens[1])
105 bd3d9e54 2021-09-05 stsp *refname = tokens[1];
106 bd3d9e54 2021-09-05 stsp if (tokens[2]) {
107 35add24a 2021-10-08 thomas if (*server_capabilities == NULL) {
108 35add24a 2021-10-08 thomas char *p;
109 35add24a 2021-10-08 thomas *server_capabilities = tokens[2];
110 35add24a 2021-10-08 thomas p = strrchr(*server_capabilities, '\n');
111 35add24a 2021-10-08 thomas if (p)
112 35add24a 2021-10-08 thomas *p = '\0';
113 35add24a 2021-10-08 thomas } else
114 35add24a 2021-10-08 thomas free(tokens[2]);
115 bd3d9e54 2021-09-05 stsp }
116 bd3d9e54 2021-09-05 stsp
117 bd3d9e54 2021-09-05 stsp return NULL;
118 bd3d9e54 2021-09-05 stsp }
119 bd3d9e54 2021-09-05 stsp
120 3efd8e31 2022-10-23 thomas const struct got_error *
121 3efd8e31 2022-10-23 thomas got_gitproto_parse_want_line(char **id_str,
122 3efd8e31 2022-10-23 thomas char **capabilities, char *line, int len)
123 3efd8e31 2022-10-23 thomas {
124 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
125 3efd8e31 2022-10-23 thomas char *tokens[3];
126 3efd8e31 2022-10-23 thomas
127 3efd8e31 2022-10-23 thomas *id_str = NULL;
128 3efd8e31 2022-10-23 thomas /* don't reset *capabilities */
129 3efd8e31 2022-10-23 thomas
130 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 2, nitems(tokens));
131 3efd8e31 2022-10-23 thomas if (err)
132 3efd8e31 2022-10-23 thomas return err;
133 3efd8e31 2022-10-23 thomas
134 3efd8e31 2022-10-23 thomas if (tokens[0] == NULL) {
135 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
136 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "empty want-line");
137 3efd8e31 2022-10-23 thomas }
138 3efd8e31 2022-10-23 thomas
139 3efd8e31 2022-10-23 thomas if (strcmp(tokens[0], "want") != 0) {
140 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
141 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "bad want-line");
142 3efd8e31 2022-10-23 thomas }
143 3efd8e31 2022-10-23 thomas
144 3efd8e31 2022-10-23 thomas free(tokens[0]);
145 3efd8e31 2022-10-23 thomas if (tokens[1])
146 3efd8e31 2022-10-23 thomas *id_str = tokens[1];
147 3efd8e31 2022-10-23 thomas if (tokens[2]) {
148 3efd8e31 2022-10-23 thomas if (*capabilities == NULL) {
149 3efd8e31 2022-10-23 thomas char *p;
150 3efd8e31 2022-10-23 thomas *capabilities = tokens[2];
151 3efd8e31 2022-10-23 thomas p = strrchr(*capabilities, '\n');
152 3efd8e31 2022-10-23 thomas if (p)
153 3efd8e31 2022-10-23 thomas *p = '\0';
154 3efd8e31 2022-10-23 thomas } else
155 3efd8e31 2022-10-23 thomas free(tokens[2]);
156 3efd8e31 2022-10-23 thomas }
157 3efd8e31 2022-10-23 thomas
158 3efd8e31 2022-10-23 thomas return NULL;
159 3efd8e31 2022-10-23 thomas }
160 3efd8e31 2022-10-23 thomas
161 3efd8e31 2022-10-23 thomas const struct got_error *
162 3efd8e31 2022-10-23 thomas got_gitproto_parse_have_line(char **id_str, char *line, int len)
163 3efd8e31 2022-10-23 thomas {
164 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
165 3efd8e31 2022-10-23 thomas char *tokens[2];
166 3efd8e31 2022-10-23 thomas
167 3efd8e31 2022-10-23 thomas *id_str = NULL;
168 3efd8e31 2022-10-23 thomas
169 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 2, nitems(tokens));
170 3efd8e31 2022-10-23 thomas if (err)
171 3efd8e31 2022-10-23 thomas return err;
172 3efd8e31 2022-10-23 thomas
173 3efd8e31 2022-10-23 thomas if (tokens[0] == NULL) {
174 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
175 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "empty have-line");
176 3efd8e31 2022-10-23 thomas }
177 3efd8e31 2022-10-23 thomas
178 3efd8e31 2022-10-23 thomas if (strcmp(tokens[0], "have") != 0) {
179 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
180 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "bad have-line");
181 3efd8e31 2022-10-23 thomas }
182 3efd8e31 2022-10-23 thomas
183 3efd8e31 2022-10-23 thomas free(tokens[0]);
184 3efd8e31 2022-10-23 thomas if (tokens[1])
185 3efd8e31 2022-10-23 thomas *id_str = tokens[1];
186 3efd8e31 2022-10-23 thomas
187 3efd8e31 2022-10-23 thomas return NULL;
188 3efd8e31 2022-10-23 thomas }
189 3efd8e31 2022-10-23 thomas
190 3efd8e31 2022-10-23 thomas const struct got_error *
191 3efd8e31 2022-10-23 thomas got_gitproto_parse_ref_update_line(char **old_id_str, char **new_id_str,
192 3efd8e31 2022-10-23 thomas char **refname, char **capabilities, char *line, size_t len)
193 3efd8e31 2022-10-23 thomas {
194 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
195 3efd8e31 2022-10-23 thomas char *tokens[4];
196 3efd8e31 2022-10-23 thomas
197 3efd8e31 2022-10-23 thomas *old_id_str = NULL;
198 3efd8e31 2022-10-23 thomas *new_id_str = NULL;
199 3efd8e31 2022-10-23 thomas *refname = NULL;
200 3efd8e31 2022-10-23 thomas
201 3efd8e31 2022-10-23 thomas /* don't reset *capabilities */
202 3efd8e31 2022-10-23 thomas
203 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 3, nitems(tokens));
204 3efd8e31 2022-10-23 thomas if (err)
205 3efd8e31 2022-10-23 thomas return err;
206 3efd8e31 2022-10-23 thomas
207 3efd8e31 2022-10-23 thomas if (tokens[0] == NULL || tokens[1] == NULL || tokens[2] == NULL) {
208 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
209 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "empty ref-update");
210 3efd8e31 2022-10-23 thomas }
211 3efd8e31 2022-10-23 thomas
212 3efd8e31 2022-10-23 thomas *old_id_str = tokens[0];
213 3efd8e31 2022-10-23 thomas *new_id_str = tokens[1];
214 3efd8e31 2022-10-23 thomas *refname = tokens[2];
215 3efd8e31 2022-10-23 thomas if (tokens[3]) {
216 3efd8e31 2022-10-23 thomas if (*capabilities == NULL) {
217 3efd8e31 2022-10-23 thomas char *p;
218 3efd8e31 2022-10-23 thomas *capabilities = tokens[3];
219 3efd8e31 2022-10-23 thomas p = strrchr(*capabilities, '\n');
220 3efd8e31 2022-10-23 thomas if (p)
221 3efd8e31 2022-10-23 thomas *p = '\0';
222 3efd8e31 2022-10-23 thomas } else
223 3efd8e31 2022-10-23 thomas free(tokens[3]);
224 3efd8e31 2022-10-23 thomas }
225 3efd8e31 2022-10-23 thomas
226 3efd8e31 2022-10-23 thomas return NULL;
227 3efd8e31 2022-10-23 thomas }
228 3efd8e31 2022-10-23 thomas
229 bd3d9e54 2021-09-05 stsp static const struct got_error *
230 bd3d9e54 2021-09-05 stsp match_capability(char **my_capabilities, const char *capa,
231 bd3d9e54 2021-09-05 stsp const struct got_capability *mycapa)
232 bd3d9e54 2021-09-05 stsp {
233 bd3d9e54 2021-09-05 stsp char *equalsign;
234 bd3d9e54 2021-09-05 stsp char *s;
235 bd3d9e54 2021-09-05 stsp
236 bd3d9e54 2021-09-05 stsp equalsign = strchr(capa, '=');
237 bd3d9e54 2021-09-05 stsp if (equalsign) {
238 bd3d9e54 2021-09-05 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
239 bd3d9e54 2021-09-05 stsp return NULL;
240 bd3d9e54 2021-09-05 stsp } else {
241 bd3d9e54 2021-09-05 stsp if (strcmp(capa, mycapa->key) != 0)
242 bd3d9e54 2021-09-05 stsp return NULL;
243 bd3d9e54 2021-09-05 stsp }
244 bd3d9e54 2021-09-05 stsp
245 bd3d9e54 2021-09-05 stsp if (asprintf(&s, "%s %s%s%s",
246 bd3d9e54 2021-09-05 stsp *my_capabilities != NULL ? *my_capabilities : "",
247 bd3d9e54 2021-09-05 stsp mycapa->key,
248 bd3d9e54 2021-09-05 stsp mycapa->value != NULL ? "=" : "",
249 e33e440b 2021-09-05 stsp mycapa->value != NULL ? mycapa->value : "") == -1)
250 bd3d9e54 2021-09-05 stsp return got_error_from_errno("asprintf");
251 bd3d9e54 2021-09-05 stsp
252 bd3d9e54 2021-09-05 stsp free(*my_capabilities);
253 bd3d9e54 2021-09-05 stsp *my_capabilities = s;
254 bd3d9e54 2021-09-05 stsp return NULL;
255 bd3d9e54 2021-09-05 stsp }
256 bd3d9e54 2021-09-05 stsp
257 bd3d9e54 2021-09-05 stsp static const struct got_error *
258 bd3d9e54 2021-09-05 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
259 bd3d9e54 2021-09-05 stsp {
260 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
261 bd3d9e54 2021-09-05 stsp char *colon, *name = NULL, *target = NULL;
262 bd3d9e54 2021-09-05 stsp
263 bd3d9e54 2021-09-05 stsp /* Need at least "A:B" */
264 bd3d9e54 2021-09-05 stsp if (strlen(capa) < 3)
265 bd3d9e54 2021-09-05 stsp return NULL;
266 bd3d9e54 2021-09-05 stsp
267 bd3d9e54 2021-09-05 stsp colon = strchr(capa, ':');
268 bd3d9e54 2021-09-05 stsp if (colon == NULL)
269 bd3d9e54 2021-09-05 stsp return NULL;
270 bd3d9e54 2021-09-05 stsp
271 bd3d9e54 2021-09-05 stsp *colon = '\0';
272 bd3d9e54 2021-09-05 stsp name = strdup(capa);
273 bd3d9e54 2021-09-05 stsp if (name == NULL)
274 bd3d9e54 2021-09-05 stsp return got_error_from_errno("strdup");
275 bd3d9e54 2021-09-05 stsp
276 bd3d9e54 2021-09-05 stsp target = strdup(colon + 1);
277 bd3d9e54 2021-09-05 stsp if (target == NULL) {
278 bd3d9e54 2021-09-05 stsp err = got_error_from_errno("strdup");
279 bd3d9e54 2021-09-05 stsp goto done;
280 bd3d9e54 2021-09-05 stsp }
281 bd3d9e54 2021-09-05 stsp
282 bd3d9e54 2021-09-05 stsp /* We can't validate the ref itself here. The main process will. */
283 bd3d9e54 2021-09-05 stsp err = got_pathlist_append(symrefs, name, target);
284 bd3d9e54 2021-09-05 stsp done:
285 bd3d9e54 2021-09-05 stsp if (err) {
286 bd3d9e54 2021-09-05 stsp free(name);
287 bd3d9e54 2021-09-05 stsp free(target);
288 bd3d9e54 2021-09-05 stsp }
289 bd3d9e54 2021-09-05 stsp return err;
290 bd3d9e54 2021-09-05 stsp }
291 bd3d9e54 2021-09-05 stsp
292 bd3d9e54 2021-09-05 stsp const struct got_error *
293 bd3d9e54 2021-09-05 stsp got_gitproto_match_capabilities(char **common_capabilities,
294 3efd8e31 2022-10-23 thomas struct got_pathlist_head *symrefs, char *capabilities,
295 bd3d9e54 2021-09-05 stsp const struct got_capability my_capabilities[], size_t ncapa)
296 bd3d9e54 2021-09-05 stsp {
297 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
298 bd3d9e54 2021-09-05 stsp char *capa, *equalsign;
299 bd3d9e54 2021-09-05 stsp size_t i;
300 bd3d9e54 2021-09-05 stsp
301 bd3d9e54 2021-09-05 stsp *common_capabilities = NULL;
302 bd3d9e54 2021-09-05 stsp do {
303 3efd8e31 2022-10-23 thomas capa = strsep(&capabilities, " ");
304 bd3d9e54 2021-09-05 stsp if (capa == NULL)
305 bd3d9e54 2021-09-05 stsp return NULL;
306 bd3d9e54 2021-09-05 stsp
307 bd3d9e54 2021-09-05 stsp equalsign = strchr(capa, '=');
308 bd3d9e54 2021-09-05 stsp if (equalsign != NULL && symrefs != NULL &&
309 bd3d9e54 2021-09-05 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
310 bd3d9e54 2021-09-05 stsp err = add_symref(symrefs, equalsign + 1);
311 bd3d9e54 2021-09-05 stsp if (err)
312 bd3d9e54 2021-09-05 stsp break;
313 bd3d9e54 2021-09-05 stsp continue;
314 bd3d9e54 2021-09-05 stsp }
315 bd3d9e54 2021-09-05 stsp
316 bd3d9e54 2021-09-05 stsp for (i = 0; i < ncapa; i++) {
317 bd3d9e54 2021-09-05 stsp err = match_capability(common_capabilities,
318 bd3d9e54 2021-09-05 stsp capa, &my_capabilities[i]);
319 bd3d9e54 2021-09-05 stsp if (err)
320 bd3d9e54 2021-09-05 stsp break;
321 bd3d9e54 2021-09-05 stsp }
322 bd3d9e54 2021-09-05 stsp } while (capa);
323 bd3d9e54 2021-09-05 stsp
324 bd3d9e54 2021-09-05 stsp if (*common_capabilities == NULL) {
325 bd3d9e54 2021-09-05 stsp *common_capabilities = strdup("");
326 bd3d9e54 2021-09-05 stsp if (*common_capabilities == NULL)
327 bd3d9e54 2021-09-05 stsp err = got_error_from_errno("strdup");
328 bd3d9e54 2021-09-05 stsp }
329 bd3d9e54 2021-09-05 stsp return err;
330 bd3d9e54 2021-09-05 stsp }
331 3efd8e31 2022-10-23 thomas
332 3efd8e31 2022-10-23 thomas const struct got_error *
333 3efd8e31 2022-10-23 thomas got_gitproto_append_capabilities(size_t *capalen, char *buf, size_t offset,
334 3efd8e31 2022-10-23 thomas size_t bufsize, const struct got_capability my_capabilities[], size_t ncapa)
335 3efd8e31 2022-10-23 thomas {
336 3efd8e31 2022-10-23 thomas char *p = buf + offset;
337 3efd8e31 2022-10-23 thomas size_t i, len, remain = bufsize - offset;
338 3efd8e31 2022-10-23 thomas
339 3efd8e31 2022-10-23 thomas *capalen = 0;
340 3efd8e31 2022-10-23 thomas
341 3efd8e31 2022-10-23 thomas if (offset >= bufsize || remain < 1)
342 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
343 3efd8e31 2022-10-23 thomas
344 3efd8e31 2022-10-23 thomas /* Capabilities are hidden behind a NUL byte. */
345 3efd8e31 2022-10-23 thomas *p = '\0';
346 3efd8e31 2022-10-23 thomas p++;
347 3efd8e31 2022-10-23 thomas remain--;
348 3efd8e31 2022-10-23 thomas *capalen += 1;
349 3efd8e31 2022-10-23 thomas
350 3efd8e31 2022-10-23 thomas for (i = 0; i < ncapa; i++) {
351 3efd8e31 2022-10-23 thomas len = strlcat(p, " ", remain);
352 3efd8e31 2022-10-23 thomas if (len >= remain)
353 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
354 3efd8e31 2022-10-23 thomas remain -= len;
355 3efd8e31 2022-10-23 thomas *capalen += 1;
356 3efd8e31 2022-10-23 thomas
357 3efd8e31 2022-10-23 thomas len = strlcat(p, my_capabilities[i].key, remain);
358 3efd8e31 2022-10-23 thomas if (len >= remain)
359 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
360 3efd8e31 2022-10-23 thomas remain -= len;
361 3efd8e31 2022-10-23 thomas *capalen += strlen(my_capabilities[i].key);
362 3efd8e31 2022-10-23 thomas
363 3efd8e31 2022-10-23 thomas if (my_capabilities[i].value == NULL)
364 3efd8e31 2022-10-23 thomas continue;
365 3efd8e31 2022-10-23 thomas
366 3efd8e31 2022-10-23 thomas len = strlcat(p, "=", remain);
367 3efd8e31 2022-10-23 thomas if (len >= remain)
368 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
369 3efd8e31 2022-10-23 thomas remain -= len;
370 3efd8e31 2022-10-23 thomas *capalen += 1;
371 3efd8e31 2022-10-23 thomas
372 3efd8e31 2022-10-23 thomas len = strlcat(p, my_capabilities[i].value, remain);
373 3efd8e31 2022-10-23 thomas if (len >= remain)
374 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
375 3efd8e31 2022-10-23 thomas remain -= len;
376 3efd8e31 2022-10-23 thomas *capalen += strlen(my_capabilities[i].value);
377 3efd8e31 2022-10-23 thomas }
378 3efd8e31 2022-10-23 thomas
379 3efd8e31 2022-10-23 thomas return NULL;
380 3efd8e31 2022-10-23 thomas }
381 3efd8e31 2022-10-23 thomas
382 3efd8e31 2022-10-23 thomas const struct got_error *
383 3efd8e31 2022-10-23 thomas got_gitproto_split_capabilities_str(struct got_capability **capabilities,
384 3efd8e31 2022-10-23 thomas size_t *ncapabilities, char *capabilities_str)
385 3efd8e31 2022-10-23 thomas {
386 3efd8e31 2022-10-23 thomas char *capastr, *capa;
387 3efd8e31 2022-10-23 thomas size_t i;
388 3efd8e31 2022-10-23 thomas
389 3efd8e31 2022-10-23 thomas *capabilities = NULL;
390 3efd8e31 2022-10-23 thomas *ncapabilities = 0;
391 3efd8e31 2022-10-23 thomas
392 3efd8e31 2022-10-23 thomas /* Compute number of capabilities on a copy of the input string. */
393 3efd8e31 2022-10-23 thomas capastr = strdup(capabilities_str);
394 3efd8e31 2022-10-23 thomas if (capastr == NULL)
395 3efd8e31 2022-10-23 thomas return got_error_from_errno("strdup");
396 3efd8e31 2022-10-23 thomas do {
397 3efd8e31 2022-10-23 thomas capa = strsep(&capastr, " ");
398 3efd8e31 2022-10-23 thomas if (capa && *capa != '\0')
399 3efd8e31 2022-10-23 thomas (*ncapabilities)++;
400 3efd8e31 2022-10-23 thomas } while (capa);
401 3efd8e31 2022-10-23 thomas free(capastr);
402 3efd8e31 2022-10-23 thomas
403 3efd8e31 2022-10-23 thomas *capabilities = calloc(*ncapabilities, sizeof(**capabilities));
404 3efd8e31 2022-10-23 thomas if (*capabilities == NULL)
405 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
406 3efd8e31 2022-10-23 thomas
407 3efd8e31 2022-10-23 thomas /* Modify input string in place, splitting it into key/value tuples. */
408 3efd8e31 2022-10-23 thomas i = 0;
409 3efd8e31 2022-10-23 thomas for (;;) {
410 3efd8e31 2022-10-23 thomas char *key = NULL, *value = NULL, *equalsign;
411 3efd8e31 2022-10-23 thomas
412 3efd8e31 2022-10-23 thomas capa = strsep(&capabilities_str, " ");
413 3efd8e31 2022-10-23 thomas if (capa == NULL)
414 3efd8e31 2022-10-23 thomas break;
415 3efd8e31 2022-10-23 thomas if (*capa == '\0')
416 3efd8e31 2022-10-23 thomas continue;
417 3efd8e31 2022-10-23 thomas
418 3efd8e31 2022-10-23 thomas if (i >= *ncapabilities) { /* should not happen */
419 3efd8e31 2022-10-23 thomas free(*capabilities);
420 3efd8e31 2022-10-23 thomas *capabilities = NULL;
421 3efd8e31 2022-10-23 thomas *ncapabilities = 0;
422 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
423 3efd8e31 2022-10-23 thomas }
424 3efd8e31 2022-10-23 thomas
425 3efd8e31 2022-10-23 thomas key = capa;
426 3efd8e31 2022-10-23 thomas
427 3efd8e31 2022-10-23 thomas equalsign = strchr(capa, '=');
428 3efd8e31 2022-10-23 thomas if (equalsign != NULL) {
429 3efd8e31 2022-10-23 thomas *equalsign = '\0';
430 3efd8e31 2022-10-23 thomas value = equalsign + 1;
431 3efd8e31 2022-10-23 thomas }
432 3efd8e31 2022-10-23 thomas
433 3efd8e31 2022-10-23 thomas (*capabilities)[i].key = key;
434 3efd8e31 2022-10-23 thomas (*capabilities)[i].value = value;
435 3efd8e31 2022-10-23 thomas i++;
436 3efd8e31 2022-10-23 thomas }
437 3efd8e31 2022-10-23 thomas
438 3efd8e31 2022-10-23 thomas return NULL;
439 3efd8e31 2022-10-23 thomas }