]> git.proxmox.com Git - libgit2.git/blob - src/signature.c
Merge pull request #467 from oleganza/oa-config-parse-fix
[libgit2.git] / src / signature.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
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 #include "signature.h"
10 #include "repository.h"
11 #include "git2/common.h"
12
13 void git_signature_free(git_signature *sig)
14 {
15 if (sig == NULL)
16 return;
17
18 free(sig->name);
19 free(sig->email);
20 free(sig);
21 }
22
23 static 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
31 static 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
39 static 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
47 if (right < left) {
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;
52 }
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
66 int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset)
67 {
68 int error;
69 git_signature *p = NULL;
70
71 assert(name && email);
72
73 *sig_out = NULL;
74
75 if ((p = git__malloc(sizeof(git_signature))) == NULL) {
76 error = GIT_ENOMEM;
77 goto cleanup;
78 }
79
80 memset(p, 0x0, sizeof(git_signature));
81
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");
85 goto cleanup;
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;
96
97 *sig_out = p;
98
99 return error;
100
101 cleanup:
102 git_signature_free(p);
103 return error;
104 }
105
106 git_signature *git_signature_dup(const git_signature *sig)
107 {
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;
112 }
113
114 int git_signature_now(git_signature **sig_out, const char *name, const char *email)
115 {
116 int error;
117 time_t now;
118 time_t offset;
119 struct tm *utc_tm, *local_tm;
120 git_signature *sig;
121
122 #ifndef GIT_WIN32
123 struct tm _utc, _local;
124 #endif
125
126 *sig_out = NULL;
127
128 time(&now);
129
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);
143 offset /= 60;
144
145 /* mktime takes care of setting tm_isdst correctly */
146 if (local_tm->tm_isdst)
147 offset += 60;
148
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;
155 }
156
157 static int parse_timezone_offset(const char *buffer, int *offset_out)
158 {
159 int dec_offset;
160 int mins, hours, offset;
161
162 const char *offset_start;
163 const char *offset_end;
164
165 offset_start = buffer;
166
167 if (*offset_start == '\n') {
168 *offset_out = 0;
169 return GIT_SUCCESS;
170 }
171
172 if (offset_start[0] != '-' && offset_start[0] != '+')
173 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. It doesn't start with '+' or '-'");
174
175 if (offset_start[1] < '0' || offset_start[1] > '9')
176 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset.");
177
178 if (git__strtol32(&dec_offset, offset_start + 1, &offset_end, 10) < GIT_SUCCESS)
179 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. It isn't a number");
180
181 if (offset_end - offset_start != 5)
182 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Invalid length");
183
184 if (dec_offset > 1400)
185 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Value too large");
186
187 hours = dec_offset / 100;
188 mins = dec_offset % 100;
189
190 if (hours > 14) // see http://www.worldtimezone.com/faq.html
191 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Hour value too large");
192
193 if (mins > 59)
194 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse TZ offset. Minute value too large");
195
196 offset = (hours * 60) + mins;
197
198 if (offset_start[0] == '-')
199 offset *= -1;
200
201 *offset_out = offset;
202
203 return GIT_SUCCESS;
204 }
205
206 static int process_next_token(const char **buffer_out, char **storage,
207 const char *token_end, const char *right_boundary)
208 {
209 int error = process_trimming(*buffer_out, storage, token_end, 0);
210 if (error < GIT_SUCCESS)
211 return error;
212
213 *buffer_out = token_end + 1;
214
215 if (*buffer_out > right_boundary)
216 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Signature too short");
217
218 return GIT_SUCCESS;
219 }
220
221 static const char *scan_for_previous_token(const char *buffer, const char *left_boundary)
222 {
223 const char *start;
224
225 if (buffer <= left_boundary)
226 return NULL;
227
228 start = skip_trailing_spaces(left_boundary, buffer);
229
230 /* Search for previous occurence of space */
231 while (start[-1] != ' ' && start > left_boundary)
232 start--;
233
234 return start;
235 }
236
237 static int parse_time(git_time_t *time_out, const char *buffer)
238 {
239 int time;
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 }
254
255 int git_signature__parse(git_signature *sig, const char **buffer_out,
256 const char *buffer_end, const char *header, char ender)
257 {
258 const char *buffer = *buffer_out;
259 const char *line_end, *name_end, *email_end, *tz_start, *time_start;
260 int error = GIT_SUCCESS;
261
262 memset(sig, 0x0, sizeof(git_signature));
263
264 if ((line_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
265 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. No newline given");
266
267 if (header) {
268 const size_t header_len = strlen(header);
269
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);
272
273 buffer += header_len;
274 }
275
276 if (buffer > line_end)
277 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Signature too short");
278
279 if ((name_end = strchr(buffer, '<')) == NULL)
280 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Cannot find '<' in signature");
281
282 if ((email_end = strchr(name_end, '>')) == NULL)
283 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Cannot find '>' in signature");
284
285 if (email_end < name_end)
286 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse signature. Malformed e-mail");
287
288 error = process_next_token(&buffer, &sig->name, name_end, line_end);
289 if (error < GIT_SUCCESS)
290 return error;
291
292 error = process_next_token(&buffer, &sig->email, email_end, line_end);
293 if (error < GIT_SUCCESS)
294 return error;
295
296 tz_start = scan_for_previous_token(line_end - 1, buffer);
297
298 if (tz_start == NULL)
299 goto clean_exit; /* No timezone nor date */
300
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 }
307
308 if (parse_timezone_offset(tz_start, &sig->when.offset) < GIT_SUCCESS) {
309 sig->when.time = 0; /* Bogus timezone, we reset the time */
310 }
311
312 clean_exit:
313 *buffer_out = line_end + 1;
314 return GIT_SUCCESS;
315 }
316
317 void 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 }
335