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