2 * Copyright (C) 2013-2014 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
33 #include <sys/types.h>
38 #include "sha256sum.h"
40 static const char *arg[2] = { "/usr/bin/sha256sum", NULL };
43 enum { RUNNING, FAILED, SUCCESSED } state;
50 struct sha256sum *sha256sum_create()
56 struct sha256sum *result;
58 result = malloc(sizeof * result);
93 execve( arg[0], (char**)arg, environ);
99 result->state = RUNNING;
101 result->tofd = fds1[1];
102 result->fromfd = fds2[0];
106 void sha256sum_destroy(struct sha256sum *s)
110 if (s->state == RUNNING) {
113 waitpid(s->pid, &sts, 0);
118 int sha256sum_add_data(struct sha256sum *s, const void *data, size_t length)
123 if (s->state != RUNNING)
128 sl = write(s->tofd, data, length);
129 } while (sl == -1 && (errno == EINTR || errno == EAGAIN));
134 waitpid(s->pid, &sts, 0);
137 length -= (size_t)sl;
138 data = (const void*)((const char*)data + (size_t)sl);
143 int sha256sum_add_file(struct sha256sum *s, const char *filename)
149 fd = open(filename, O_RDONLY);
155 rd = read(fd, buffer, sizeof buffer);
156 } while (rd == -1 && (errno == EINTR || errno == EAGAIN));
165 if (sha256sum_add_data(s, buffer, (size_t)rd)) {
172 int sha256sum_get(struct sha256sum *s, char result[32])
180 if (s->state == RUNNING) {
183 waitpid(s->pid, &sts, 0);
184 if (WIFEXITED(sts) && WEXITSTATUS(sts) == 0) {
185 sts = read(s->fromfd, buffer, 65);
186 if (sts == 65 && buffer[64] == ' ') {
187 s->state = SUCCESSED;
188 for (j = 0 ; j < 32 ; j++) {
190 if ('0' <= c1 && c1 <= '9')
192 else if ('a' <= c1 && c1 <= 'f')
193 c1 = c1 - 'a' + '\012';
194 else if ('A' <= c1 && c1 <= 'F')
195 c1 = c1 - 'A' + '\012';
201 if ('0' <= c2 && c2 <= '9')
203 else if ('a' <= c2 && c2 <= 'f')
204 c2 = c2 - 'a' + '\012';
205 else if ('A' <= c2 && c2 <= 'F')
206 c2 = c2 - 'A' + '\012';
211 s->result[j] = (c1 << 4) | c2;
218 if (s->state == FAILED)
221 memcpy(result, s->result, 32);
229 int main(int argc, char **argv)
236 s = sha256sum_create();
238 r = sha256sum_add_file(s, *argv);
240 r = sha256sum_get(s, sum);
242 sha256sum_destroy(s);
243 for (j=0 ; j < 32 ; j++)
244 printf("%02x", (int)(unsigned char)sum[j]);
245 printf(" %s\n", *argv);