2 53bf0b54 2023-02-23 op * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 53bf0b54 2023-02-23 op * Permission to use, copy, modify, and distribute this software for any
5 53bf0b54 2023-02-23 op * purpose with or without fee is hereby granted, provided that the above
6 53bf0b54 2023-02-23 op * copyright notice and this permission notice appear in all copies.
8 53bf0b54 2023-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 53bf0b54 2023-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 53bf0b54 2023-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 53bf0b54 2023-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 53bf0b54 2023-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 53bf0b54 2023-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 53bf0b54 2023-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 53bf0b54 2023-02-23 op #include <sys/types.h>
18 53bf0b54 2023-02-23 op #include <sha1.h>
19 53bf0b54 2023-02-23 op #include <sha2.h>
20 53bf0b54 2023-02-23 op #include <errno.h>
21 53bf0b54 2023-02-23 op #include <stdio.h>
22 53bf0b54 2023-02-23 op #include <stdlib.h>
23 53bf0b54 2023-02-23 op #include <limits.h>
25 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
28 53bf0b54 2023-02-23 op got_parse_xdigit(uint8_t *val, const char *hex)
34 53bf0b54 2023-02-23 op lval = strtol(hex, &ep, 16);
35 53bf0b54 2023-02-23 op if (hex[0] == '\0' || *ep != '\0')
37 53bf0b54 2023-02-23 op if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
40 53bf0b54 2023-02-23 op *val = (uint8_t)lval;
45 53bf0b54 2023-02-23 op got_parse_sha1_digest(uint8_t *digest, const char *line)
47 53bf0b54 2023-02-23 op uint8_t b = 0;
48 53bf0b54 2023-02-23 op char hex[3] = {'\0', '\0', '\0'};
51 53bf0b54 2023-02-23 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
52 53bf0b54 2023-02-23 op if (line[0] == '\0' || line[1] == '\0')
54 53bf0b54 2023-02-23 op for (j = 0; j < 2; j++) {
55 53bf0b54 2023-02-23 op hex[j] = *line;
58 53bf0b54 2023-02-23 op if (!got_parse_xdigit(&b, hex))
60 53bf0b54 2023-02-23 op digest[i] = b;
67 53bf0b54 2023-02-23 op got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
69 53bf0b54 2023-02-23 op char *p = buf;
73 53bf0b54 2023-02-23 op if (size < SHA1_DIGEST_STRING_LENGTH)
76 53bf0b54 2023-02-23 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
77 53bf0b54 2023-02-23 op snprintf(hex, sizeof(hex), "%.2x", digest[i]);
78 53bf0b54 2023-02-23 op p[0] = hex[0];
79 53bf0b54 2023-02-23 op p[1] = hex[1];