signature.c 7.5 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * 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 17
		return;

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
static const char *skip_leading_spaces(const char *buffer, const char *buffer_end)
{
	while (*buffer == ' ' && buffer < buffer_end)
		buffer++;

	return buffer;
}

static const char *skip_trailing_spaces(const char *buffer_start, const char *buffer_end)
{
	while (*buffer_end == ' ' && buffer_end > buffer_start)
		buffer_end--;

	return buffer_end;
}

41 42 43 44 45 46
static int signature_error(const char *msg)
{
	giterr_set(GITERR_INVALID, "Failed to parse signature - %s", msg);
	return -1;
}

47 48 49
static int process_trimming(const char *input, char **storage, const char *input_end, int fail_when_empty)
{
	const char *left, *right;
50
	size_t trimmed_input_length;
51

52 53
	assert(storage);

54 55 56
	left = skip_leading_spaces(input, input_end);
	right = skip_trailing_spaces(input, input_end - 1);

schu committed
57
	if (right < left) {
58
		if (fail_when_empty)
59 60 61
			return signature_error("input is either empty of contains only spaces");

		right = left - 1;
Vicent Marti committed
62
	}
63 64 65 66

	trimmed_input_length = right - left + 1;

	*storage = git__malloc(trimmed_input_length + 1);
67
	GITERR_CHECK_ALLOC(*storage);
68 69 70 71

	memcpy(*storage, left, trimmed_input_length);
	(*storage)[trimmed_input_length] = 0;

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 81
	assert(name && email);

82 83
	*sig_out = NULL;

84 85
	p = git__calloc(1, sizeof(git_signature));
	GITERR_CHECK_ALLOC(p);
86

87 88 89 90 91
	if ((error = process_trimming(name, &p->name, name + strlen(name), 1)) < 0 ||
		(error = process_trimming(email, &p->email, email + strlen(email), 1)) < 0)
	{
		git_signature_free(p);
		return error;
92 93 94 95
	}

	p->when.time = time;
	p->when.offset = offset;
96

97 98
	*sig_out = p;

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 107
		return NULL;
	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 118 119
#ifndef GIT_WIN32
	struct tm _utc, _local;
#endif
120

121 122
	*sig_out = NULL;

123
	time(&now);
124

125 126 127 128 129 130 131 132 133 134 135 136 137
	/**
	 * On Win32, `gmtime_r` doesn't exist but
	 * `gmtime` is threadsafe, so we can use that
	 */
#ifdef GIT_WIN32
	utc_tm = gmtime(&now);
	local_tm = localtime(&now);
#else
	utc_tm = gmtime_r(&now, &_utc);
	local_tm = localtime_r(&now, &_local);
#endif

	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 143
		offset += 60;

144 145
	if (git_signature_new(&sig, name, email, now, (int)offset) < 0)
		return -1;
146 147 148

	*sig_out = sig;

149 150 151 152 153 154 155
	return 0;
}

static int timezone_error(const char *msg)
{
	giterr_set(GITERR_INVALID, "Failed to parse TZ offset - %s", msg);
	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 164
	const char *offset_start;
	const char *offset_end;
165

166
	offset_start = buffer;
167

168
	if (*offset_start == '\n') {
169 170 171 172
		*offset_out = 0;
		return GIT_SUCCESS;
	}

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 189 190
	hours = dec_offset / 100;
	mins = dec_offset % 100;

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 198 199 200

	offset = (hours * 60) + mins;

	if (offset_start[0] == '-')
		offset *= -1;
201

202 203
	*offset_out = offset;

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 228
		return NULL;

229
	start = skip_trailing_spaces(left_boundary, buffer);
230 231 232 233 234 235 236 237

	/* Search for previous occurence of space */
	while (start[-1] != ' ' && start > left_boundary)
		start--;

	return start;
}

238
static int parse_time(git_time_t *time_out, const char *buffer)
239
{
240
	int time;
241 242
	int error;

243 244 245 246
	if (*buffer == '+' || *buffer == '-') {
		giterr_set(GITERR_INVALID, "Failed while parsing time. '%s' actually looks like a timezone offset.", buffer);
		return -1;
	}
247 248 249

	error = git__strtol32(&time, buffer, &buffer, 10);

250 251
	if (!error)
		*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 = 0;
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 269
	if (header) {
		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 275
		buffer += header_len;
	}
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 300
	if (tz_start == NULL)
		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 305 306 307
		/* The tz_start might point at the time */
		parse_time(&sig->when.time, tz_start);
		goto clean_exit;
	}
308

309
	if (parse_timezone_offset(tz_start, &sig->when.offset) < 0) {
310 311
		sig->when.time = 0; /* Bogus timezone, we reset the time */
	}
312

313
clean_exit:
314
	*buffer_out = line_end + 1;
315
	return 0;
316 317
}

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig)
{
	int offset, hours, mins;
	char sign;

	offset = sig->when.offset;
	sign = (sig->when.offset < 0) ? '-' : '+';

	if (offset < 0)
		offset = -offset;

	hours = offset / 60;
	mins = offset % 60;

	git_buf_printf(buf, "%s%s <%s> %u %c%02d%02d\n",
			header ? header : "", sig->name, sig->email,
			(unsigned)sig->when.time, sign, hours, mins);
}
336