Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sha2.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
27 #include "got_compat.h"
29 #include "got_object.h"
31 #include "got_lib_hash.h"
33 int
34 got_parse_xdigit(uint8_t *val, const char *hex)
35 {
36 char *ep;
37 long lval;
39 errno = 0;
40 lval = strtol(hex, &ep, 16);
41 if (hex[0] == '\0' || *ep != '\0')
42 return 0;
43 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
44 return 0;
46 *val = (uint8_t)lval;
47 return 1;
48 }
50 static int
51 parse_digest(uint8_t *digest, int len, const char *line)
52 {
53 uint8_t b = 0;
54 char hex[3] = {'\0', '\0', '\0'};
55 int i, j;
57 for (i = 0; i < len; i++) {
58 if (line[0] == '\0' || line[1] == '\0')
59 return 0;
60 for (j = 0; j < 2; j++) {
61 hex[j] = *line;
62 line++;
63 }
64 if (!got_parse_xdigit(&b, hex))
65 return 0;
66 digest[i] = b;
67 }
69 return 1;
70 }
72 static char *
73 digest_to_str(const uint8_t *digest, int len, char *buf)
74 {
75 char *p = buf;
76 char hex[3];
77 int i;
79 for (i = 0; i < len; i++) {
80 snprintf(hex, sizeof(hex), "%.2x", digest[i]);
81 p[0] = hex[0];
82 p[1] = hex[1];
83 p += 2;
84 }
85 p[0] = '\0';
87 return buf;
88 }
90 char *
91 got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
92 {
93 if (size < SHA1_DIGEST_STRING_LENGTH)
94 return NULL;
95 return digest_to_str(digest, SHA1_DIGEST_LENGTH, buf);
96 }
98 char *
99 got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
101 if (size < SHA256_DIGEST_STRING_LENGTH)
102 return NULL;
103 return digest_to_str(digest, SHA256_DIGEST_LENGTH, buf);
106 int
107 got_parse_hash_digest(uint8_t *digest, const char *line,
108 enum got_hash_algorithm algo)
110 switch (algo) {
111 case GOT_HASH_SHA1:
112 return parse_digest(digest, SHA1_DIGEST_LENGTH, line);
113 case GOT_HASH_SHA256:
114 return parse_digest(digest, SHA256_DIGEST_LENGTH, line);
115 default:
116 return 0;
120 int
121 got_parse_object_id(struct got_object_id *id, const char *line,
122 enum got_hash_algorithm algo)
124 memset(id, 0, sizeof(*id));
126 /* XXX: temporary until we grow got_object_id */
127 if (algo != GOT_HASH_SHA1)
128 return 0;
130 return got_parse_hash_digest(id->sha1, line, algo);