]> git.proxmox.com Git - libgit2.git/blame - src/signature.c
tree: Add traversal in post-order
[libgit2.git] / src / signature.c
CommitLineData
58519018 1/*
bb742ede 2 * Copyright (C) 2009-2011 the libgit2 contributors
58519018 3 *
bb742ede
VM
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.
58519018
VM
6 */
7
8#include "common.h"
638c2ca4 9#include "signature.h"
58519018 10#include "repository.h"
44908fe7 11#include "git2/common.h"
58519018 12
638c2ca4 13void git_signature_free(git_signature *sig)
58519018 14{
638c2ca4 15 if (sig == NULL)
58519018
VM
16 return;
17
9f86ec52
VM
18 free(sig->name);
19 free(sig->email);
638c2ca4 20 free(sig);
58519018
VM
21}
22
a01acc47 23static const char *skip_leading_spaces(const char *buffer, const char *buffer_end)
24{
25 while (*buffer == ' ' && buffer < buffer_end)
26 buffer++;
27
28 return buffer;
29}
30
31static const char *skip_trailing_spaces(const char *buffer_start, const char *buffer_end)
32{
33 while (*buffer_end == ' ' && buffer_end > buffer_start)
34 buffer_end--;
35
36 return buffer_end;
37}
38
39static int process_trimming(const char *input, char **storage, const char *input_end, int fail_when_empty)
40{
41 const char *left, *right;
42 int trimmed_input_length;
43
44 left = skip_leading_spaces(input, input_end);
45 right = skip_trailing_spaces(input, input_end - 1);
46
5274c31a 47 if (right < left) {
a01acc47 48 if (fail_when_empty)
49 return git__throw(GIT_EINVALIDARGS, "Failed to trim. Input is either empty or only contains spaces");
50 else
51 right = left - 1;
9f86ec52 52 }
a01acc47 53
54 trimmed_input_length = right - left + 1;
55
56 *storage = git__malloc(trimmed_input_length + 1);
57 if (*storage == NULL)
58 return GIT_ENOMEM;
59
60 memcpy(*storage, left, trimmed_input_length);
61 (*storage)[trimmed_input_length] = 0;
62
63 return GIT_SUCCESS;
64}
65
63396a39 66int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset)
58519018 67{
a01acc47 68 int error;
638c2ca4 69 git_signature *p = NULL;
58519018 70
a01acc47 71 assert(name && email);
72
63396a39
MS
73 *sig_out = NULL;
74
75 if ((p = git__malloc(sizeof(git_signature))) == NULL) {
76 error = GIT_ENOMEM;
58519018 77 goto cleanup;
63396a39 78 }
58519018 79
a01acc47 80 memset(p, 0x0, sizeof(git_signature));
58519018 81
a01acc47 82 error = process_trimming(name, &p->name, name + strlen(name), 1);
83 if (error < GIT_SUCCESS) {
84 git__rethrow(GIT_EINVALIDARGS, "Failed to create signature. 'name' argument is invalid");
58519018 85 goto cleanup;
a01acc47 86 }
87
88 error = process_trimming(email, &p->email, email + strlen(email), 1);
89 if (error < GIT_SUCCESS) {
90 git__rethrow(GIT_EINVALIDARGS, "Failed to create signature. 'email' argument is invalid");
91 goto cleanup;
92 }
93
94 p->when.time = time;
95 p->when.offset = offset;
58519018 96
63396a39
MS
97 *sig_out = p;
98
99 return error;
58519018
VM
100
101cleanup:
638c2ca4 102 git_signature_free(p);
63396a39 103 return error;
58519018
VM
104}
105
638c2ca4 106git_signature *git_signature_dup(const git_signature *sig)
58519018 107{
63396a39
MS
108 git_signature *new;
109 if (git_signature_new(&new, sig->name, sig->email, sig->when.time, sig->when.offset) < GIT_SUCCESS)
110 return NULL;
111 return new;
58519018
VM
112}
113
63396a39 114int git_signature_now(git_signature **sig_out, const char *name, const char *email)
9e9e6ae1 115{
63396a39 116 int error;
9e9e6ae1 117 time_t now;
53b7560b 118 time_t offset;
14eb94ee 119 struct tm *utc_tm, *local_tm;
63396a39 120 git_signature *sig;
9e9e6ae1 121
14eb94ee
VM
122#ifndef GIT_WIN32
123 struct tm _utc, _local;
124#endif
9e9e6ae1 125
63396a39
MS
126 *sig_out = NULL;
127
14eb94ee 128 time(&now);
9e9e6ae1 129
14eb94ee
VM
130 /**
131 * On Win32, `gmtime_r` doesn't exist but
132 * `gmtime` is threadsafe, so we can use that
133 */
134#ifdef GIT_WIN32
135 utc_tm = gmtime(&now);
136 local_tm = localtime(&now);
137#else
138 utc_tm = gmtime_r(&now, &_utc);
139 local_tm = localtime_r(&now, &_local);
140#endif
141
142 offset = mktime(local_tm) - mktime(utc_tm);
9e9e6ae1 143 offset /= 60;
14eb94ee 144
9e9e6ae1 145 /* mktime takes care of setting tm_isdst correctly */
14eb94ee 146 if (local_tm->tm_isdst)
9e9e6ae1
CMN
147 offset += 60;
148
63396a39
MS
149 if ((error = git_signature_new(&sig, name, email, now, (int)offset)) < GIT_SUCCESS)
150 return error;
151
152 *sig_out = sig;
153
154 return error;
9e9e6ae1 155}
58519018 156
42a1b5e1 157static int parse_timezone_offset(const char *buffer, int *offset_out)
13710f1e 158{
ad196c6a 159 int dec_offset;
42a1b5e1 160 int mins, hours, offset;
13710f1e 161
c6e65aca
VM
162 const char *offset_start;
163 const char *offset_end;
13710f1e 164
42a1b5e1 165 offset_start = buffer;
13710f1e 166
638c2ca4 167 if (*offset_start == '\n') {
fee065a0 168 *offset_out = 0;
169 return GIT_SUCCESS;
170 }
171
13710f1e 172 if (offset_start[0] != '-' && offset_start[0] != '+')
5de24ec7 173 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. It doesn't start with '+' or '-'");
13710f1e 174
fbfc7580 175 if (offset_start[1] < '0' || offset_start[1] > '9')
fea400f8 176 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset.");
fbfc7580 177
c6e65aca 178 if (git__strtol32(&dec_offset, offset_start + 1, &offset_end, 10) < GIT_SUCCESS)
5de24ec7 179 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. It isn't a number");
13710f1e 180
181 if (offset_end - offset_start != 5)
5de24ec7 182 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Invalid length");
13710f1e 183
fbfc7580
DG
184 if (dec_offset > 1400)
185 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Value too large");
186
13710f1e 187 hours = dec_offset / 100;
188 mins = dec_offset % 100;
189
932d1baf 190 if (hours > 14) // see http://www.worldtimezone.com/faq.html
fbfc7580 191 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Hour value too large");
13710f1e 192
638c2ca4 193 if (mins > 59)
5de24ec7 194 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Minute value too large");
13710f1e 195
196 offset = (hours * 60) + mins;
197
198 if (offset_start[0] == '-')
13710f1e 199 offset *= -1;
932d1baf 200
13710f1e 201 *offset_out = offset;
202
203 return GIT_SUCCESS;
204}
205
d568d585 206static int process_next_token(const char **buffer_out, char **storage,
a01acc47 207 const char *token_end, const char *right_boundary)
42a1b5e1 208{
a01acc47 209 int error = process_trimming(*buffer_out, storage, token_end, 0);
210 if (error < GIT_SUCCESS)
211 return error;
42a1b5e1 212
a01acc47 213 *buffer_out = token_end + 1;
42a1b5e1 214
a01acc47 215 if (*buffer_out > right_boundary)
42a1b5e1 216 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Signature too short");
217
42a1b5e1 218 return GIT_SUCCESS;
219}
220
d568d585 221static const char *scan_for_previous_token(const char *buffer, const char *left_boundary)
42a1b5e1 222{
a01acc47 223 const char *start;
42a1b5e1 224
a01acc47 225 if (buffer <= left_boundary)
42a1b5e1 226 return NULL;
227
a01acc47 228 start = skip_trailing_spaces(left_boundary, buffer);
42a1b5e1 229
230 /* Search for previous occurence of space */
231 while (start[-1] != ' ' && start > left_boundary)
232 start--;
233
234 return start;
235}
236
d568d585 237static int parse_time(git_time_t *time_out, const char *buffer)
42a1b5e1 238{
ad196c6a 239 int time;
42a1b5e1 240 int error;
241
242 if (*buffer == '+' || *buffer == '-')
243 return git__throw(GIT_ERROR, "Failed while parsing time. '%s' rather look like a timezone offset.", buffer);
244
245 error = git__strtol32(&time, buffer, &buffer, 10);
246
247 if (error < GIT_SUCCESS)
248 return error;
249
250 *time_out = (git_time_t)time;
251
252 return GIT_SUCCESS;
253}
13710f1e 254
720d5472 255int git_signature__parse(git_signature *sig, const char **buffer_out,
7757be33 256 const char *buffer_end, const char *header, char ender)
58519018 257{
720d5472 258 const char *buffer = *buffer_out;
42a1b5e1 259 const char *line_end, *name_end, *email_end, *tz_start, *time_start;
260 int error = GIT_SUCCESS;
58519018 261
638c2ca4 262 memset(sig, 0x0, sizeof(git_signature));
58519018 263
7757be33 264 if ((line_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
8b2c913a 265 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. No newline given");
58519018 266
8b2c913a
MS
267 if (header) {
268 const size_t header_len = strlen(header);
58519018 269
8b2c913a
MS
270 if (memcmp(buffer, header, header_len) != 0)
271 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Expected prefix '%s' doesn't match actual", header);
58519018 272
8b2c913a
MS
273 buffer += header_len;
274 }
58519018 275
8b2c913a
MS
276 if (buffer > line_end)
277 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Signature too short");
58519018 278
8b2c913a
MS
279 if ((name_end = strchr(buffer, '<')) == NULL)
280 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Cannot find '<' in signature");
fbfc7580 281
6f2856f3 282 if ((email_end = strchr(name_end, '>')) == NULL)
8b2c913a 283 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Cannot find '>' in signature");
58519018 284
42a1b5e1 285 if (email_end < name_end)
286 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Malformed e-mail");
076141a1 287
42a1b5e1 288 error = process_next_token(&buffer, &sig->name, name_end, line_end);
289 if (error < GIT_SUCCESS)
290 return error;
58519018 291
42a1b5e1 292 error = process_next_token(&buffer, &sig->email, email_end, line_end);
293 if (error < GIT_SUCCESS)
294 return error;
58519018 295
42a1b5e1 296 tz_start = scan_for_previous_token(line_end - 1, buffer);
58519018 297
42a1b5e1 298 if (tz_start == NULL)
299 goto clean_exit; /* No timezone nor date */
c6e65aca 300
42a1b5e1 301 time_start = scan_for_previous_token(tz_start - 1, buffer);
302 if (time_start == NULL || parse_time(&sig->when.time, time_start) < GIT_SUCCESS) {
303 /* The tz_start might point at the time */
304 parse_time(&sig->when.time, tz_start);
305 goto clean_exit;
306 }
932d1baf 307
42a1b5e1 308 if (parse_timezone_offset(tz_start, &sig->when.offset) < GIT_SUCCESS) {
309 sig->when.time = 0; /* Bogus timezone, we reset the time */
310 }
13710f1e 311
42a1b5e1 312clean_exit:
8b2c913a 313 *buffer_out = line_end + 1;
6f02c3ba 314 return GIT_SUCCESS;
58519018
VM
315}
316
afeecf4f
VM
317void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig)
318{
319 int offset, hours, mins;
320 char sign;
321
322 offset = sig->when.offset;
323 sign = (sig->when.offset < 0) ? '-' : '+';
324
325 if (offset < 0)
326 offset = -offset;
327
328 hours = offset / 60;
329 mins = offset % 60;
330
331 git_buf_printf(buf, "%s%s <%s> %u %c%02d%02d\n",
332 header ? header : "", sig->name, sig->email,
333 (unsigned)sig->when.time, sign, hours, mins);
334}
58519018 335