]> git.proxmox.com Git - libgit2.git/blame - src/signature.c
Do not declare variables in the middle of a func
[libgit2.git] / src / signature.c
CommitLineData
58519018
VM
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"
638c2ca4 27#include "signature.h"
58519018 28#include "repository.h"
44908fe7 29#include "git2/common.h"
58519018 30
638c2ca4 31void git_signature_free(git_signature *sig)
58519018 32{
638c2ca4 33 if (sig == NULL)
58519018
VM
34 return;
35
638c2ca4
VM
36 free(sig->name);
37 free(sig->email);
38 free(sig);
58519018
VM
39}
40
56d8ca26 41git_signature *git_signature_new(const char *name, const char *email, git_time_t time, int offset)
58519018 42{
638c2ca4 43 git_signature *p = NULL;
58519018 44
638c2ca4 45 if ((p = git__malloc(sizeof(git_signature))) == NULL)
58519018
VM
46 goto cleanup;
47
48 p->name = git__strdup(name);
49 p->email = git__strdup(email);
638c2ca4
VM
50 p->when.time = time;
51 p->when.offset = offset;
58519018
VM
52
53 if (p->name == NULL || p->email == NULL)
54 goto cleanup;
55
56 return p;
57
58cleanup:
638c2ca4 59 git_signature_free(p);
58519018
VM
60 return NULL;
61}
62
638c2ca4 63git_signature *git_signature_dup(const git_signature *sig)
58519018 64{
638c2ca4 65 return git_signature_new(sig->name, sig->email, sig->when.time, sig->when.offset);
58519018
VM
66}
67
58519018 68
638c2ca4 69static int parse_timezone_offset(const char *buffer, int *offset_out)
13710f1e 70{
71 int offset, dec_offset;
72 int mins, hours;
73
74 const char* offset_start;
75 char* offset_end;
76
77 offset_start = buffer + 1;
78
638c2ca4 79 if (*offset_start == '\n') {
fee065a0 80 *offset_out = 0;
81 return GIT_SUCCESS;
82 }
83
13710f1e 84 if (offset_start[0] != '-' && offset_start[0] != '+')
85 return GIT_EOBJCORRUPTED;
86
87 dec_offset = strtol(offset_start + 1, &offset_end, 10);
88
89 if (offset_end - offset_start != 5)
90 return GIT_EOBJCORRUPTED;
91
92 hours = dec_offset / 100;
93 mins = dec_offset % 100;
94
638c2ca4 95 if (hours > 14) // see http://www.worldtimezone.com/faq.html
13710f1e 96 return GIT_EOBJCORRUPTED;
97
638c2ca4 98 if (mins > 59)
13710f1e 99 return GIT_EOBJCORRUPTED;
100
101 offset = (hours * 60) + mins;
102
103 if (offset_start[0] == '-')
13710f1e 104 offset *= -1;
13710f1e 105
106 *offset_out = offset;
107
108 return GIT_SUCCESS;
109}
110
111
720d5472 112int git_signature__parse(git_signature *sig, const char **buffer_out,
58519018
VM
113 const char *buffer_end, const char *header)
114{
115 const size_t header_len = strlen(header);
116
117 int name_length, email_length;
720d5472
VM
118 const char *buffer = *buffer_out;
119 const char *line_end, *name_end, *email_end;
13710f1e 120 int offset = 0;
58519018 121
638c2ca4 122 memset(sig, 0x0, sizeof(git_signature));
58519018
VM
123
124 line_end = memchr(buffer, '\n', buffer_end - buffer);
125 if (!line_end)
126 return GIT_EOBJCORRUPTED;
127
128 if (buffer + (header_len + 1) > line_end)
129 return GIT_EOBJCORRUPTED;
130
131 if (memcmp(buffer, header, header_len) != 0)
132 return GIT_EOBJCORRUPTED;
133
134 buffer += header_len;
135
136 /* Parse name */
137 if ((name_end = memchr(buffer, '<', buffer_end - buffer)) == NULL)
138 return GIT_EOBJCORRUPTED;
139
140 name_length = name_end - buffer - 1;
638c2ca4
VM
141 sig->name = git__malloc(name_length + 1);
142 memcpy(sig->name, buffer, name_length);
143 sig->name[name_length] = 0;
58519018
VM
144 buffer = name_end + 1;
145
146 if (buffer >= line_end)
147 return GIT_EOBJCORRUPTED;
148
149 /* Parse email */
150 if ((email_end = memchr(buffer, '>', buffer_end - buffer)) == NULL)
151 return GIT_EOBJCORRUPTED;
152
153 email_length = email_end - buffer;
638c2ca4
VM
154 sig->email = git__malloc(email_length + 1);
155 memcpy(sig->email, buffer, email_length);
156 sig->email[email_length] = 0;
58519018
VM
157 buffer = email_end + 1;
158
159 if (buffer >= line_end)
160 return GIT_EOBJCORRUPTED;
161
720d5472 162 sig->when.time = strtol(buffer, (char **)&buffer, 10);
58519018 163
638c2ca4 164 if (sig->when.time == 0)
58519018
VM
165 return GIT_EOBJCORRUPTED;
166
638c2ca4 167 if (parse_timezone_offset(buffer, &offset) < GIT_SUCCESS)
13710f1e 168 return GIT_EOBJCORRUPTED;
169
638c2ca4 170 sig->when.offset = offset;
13710f1e 171
58519018 172 *buffer_out = (line_end + 1);
6f02c3ba 173 return GIT_SUCCESS;
58519018
VM
174}
175
72a3fe42 176int git_signature__write(char **signature, const char *header, const git_signature *sig)
58519018 177{
13710f1e 178 int offset, hours, mins;
72a3fe42
VM
179 char sig_buffer[2048];
180 int sig_buffer_len;
181 char sign;
13710f1e 182
638c2ca4
VM
183 offset = sig->when.offset;
184 sign = (sig->when.offset < 0) ? '-' : '+';
13710f1e 185
186 if (offset < 0)
187 offset = -offset;
188
189 hours = offset / 60;
190 mins = offset % 60;
191
72a3fe42
VM
192 sig_buffer_len = snprintf(sig_buffer, sizeof(sig_buffer),
193 "%s %s <%s> %u %c%02d%02d\n",
194 header, sig->name, sig->email,
195 (unsigned)sig->when.time, sign, hours, mins);
196
197 if (sig_buffer_len < 0 || (size_t)sig_buffer_len > sizeof(sig_buffer))
198 return GIT_ENOMEM;
199
200 *signature = git__strdup(sig_buffer);
201 return sig_buffer_len;
58519018
VM
202}
203
204