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