]> git.proxmox.com Git - libgit2.git/blob - src/signature.c
reflog: Fix reflog writer/reader
[libgit2.git] / src / signature.c
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "common.h"
27 #include "signature.h"
28 #include "repository.h"
29 #include "git2/common.h"
30
31 void git_signature_free(git_signature *sig)
32 {
33 if (sig == NULL)
34 return;
35
36 free(sig->name);
37 free(sig->email);
38 free(sig);
39 }
40
41 static const char *skip_leading_spaces(const char *buffer, const char *buffer_end)
42 {
43 while (*buffer == ' ' && buffer < buffer_end)
44 buffer++;
45
46 return buffer;
47 }
48
49 static const char *skip_trailing_spaces(const char *buffer_start, const char *buffer_end)
50 {
51 while (*buffer_end == ' ' && buffer_end > buffer_start)
52 buffer_end--;
53
54 return buffer_end;
55 }
56
57 static int process_trimming(const char *input, char **storage, const char *input_end, int fail_when_empty)
58 {
59 const char *left, *right;
60 int trimmed_input_length;
61
62 left = skip_leading_spaces(input, input_end);
63 right = skip_trailing_spaces(input, input_end - 1);
64
65 if (right <= left) {
66 if (fail_when_empty)
67 return git__throw(GIT_EINVALIDARGS, "Failed to trim. Input is either empty or only contains spaces");
68 else
69 right = left - 1;
70 }
71
72 trimmed_input_length = right - left + 1;
73
74 *storage = git__malloc(trimmed_input_length + 1);
75 if (*storage == NULL)
76 return GIT_ENOMEM;
77
78 memcpy(*storage, left, trimmed_input_length);
79 (*storage)[trimmed_input_length] = 0;
80
81 return GIT_SUCCESS;
82 }
83
84 git_signature *git_signature_new(const char *name, const char *email, git_time_t time, int offset)
85 {
86 int error;
87 git_signature *p = NULL;
88
89 assert(name && email);
90
91 if ((p = git__malloc(sizeof(git_signature))) == NULL)
92 goto cleanup;
93
94 memset(p, 0x0, sizeof(git_signature));
95
96 error = process_trimming(name, &p->name, name + strlen(name), 1);
97 if (error < GIT_SUCCESS) {
98 git__rethrow(GIT_EINVALIDARGS, "Failed to create signature. 'name' argument is invalid");
99 goto cleanup;
100 }
101
102 error = process_trimming(email, &p->email, email + strlen(email), 1);
103 if (error < GIT_SUCCESS) {
104 git__rethrow(GIT_EINVALIDARGS, "Failed to create signature. 'email' argument is invalid");
105 goto cleanup;
106 }
107
108 p->when.time = time;
109 p->when.offset = offset;
110
111 return p;
112
113 cleanup:
114 git_signature_free(p);
115 return NULL;
116 }
117
118 git_signature *git_signature_dup(const git_signature *sig)
119 {
120 return git_signature_new(sig->name, sig->email, sig->when.time, sig->when.offset);
121 }
122
123 git_signature *git_signature_now(const char *name, const char *email)
124 {
125 time_t now;
126 time_t offset;
127 struct tm *utc_tm, *local_tm;
128
129 #ifndef GIT_WIN32
130 struct tm _utc, _local;
131 #endif
132
133 time(&now);
134
135 /**
136 * On Win32, `gmtime_r` doesn't exist but
137 * `gmtime` is threadsafe, so we can use that
138 */
139 #ifdef GIT_WIN32
140 utc_tm = gmtime(&now);
141 local_tm = localtime(&now);
142 #else
143 utc_tm = gmtime_r(&now, &_utc);
144 local_tm = localtime_r(&now, &_local);
145 #endif
146
147 offset = mktime(local_tm) - mktime(utc_tm);
148 offset /= 60;
149
150 /* mktime takes care of setting tm_isdst correctly */
151 if (local_tm->tm_isdst)
152 offset += 60;
153
154 return git_signature_new(name, email, now, (int)offset);
155 }
156
157 static int parse_timezone_offset(const char *buffer, int *offset_out)
158 {
159 long 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 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 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 int parse_time(git_time_t *time_out, const char *buffer)
238 {
239 long 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(buffer, '>')) == 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