xref: /netbsd-src/external/gpl3/binutils.old/dist/libctf/ctf-sha1.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1*c42dbd0eSchristos /* SHA-1 thunks.
2*c42dbd0eSchristos    Copyright (C) 2019-2022 Free Software Foundation, Inc.
3*c42dbd0eSchristos 
4*c42dbd0eSchristos    This file is part of libctf.
5*c42dbd0eSchristos 
6*c42dbd0eSchristos    libctf is free software; you can redistribute it and/or modify it under
7*c42dbd0eSchristos    the terms of the GNU General Public License as published by the Free
8*c42dbd0eSchristos    Software Foundation; either version 3, or (at your option) any later
9*c42dbd0eSchristos    version.
10*c42dbd0eSchristos 
11*c42dbd0eSchristos    This program is distributed in the hope that it will be useful, but
12*c42dbd0eSchristos    WITHOUT ANY WARRANTY; without even the implied warranty of
13*c42dbd0eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14*c42dbd0eSchristos    See the GNU General Public License for more details.
15*c42dbd0eSchristos 
16*c42dbd0eSchristos    You should have received a copy of the GNU General Public License
17*c42dbd0eSchristos    along with this program; see the file COPYING.  If not see
18*c42dbd0eSchristos    <http://www.gnu.org/licenses/>.  */
19*c42dbd0eSchristos 
20*c42dbd0eSchristos #include <ctf-impl.h>
21*c42dbd0eSchristos #include <ctf-sha1.h>
22*c42dbd0eSchristos 
23*c42dbd0eSchristos static const char hex[] = "0123456789abcdef";
24*c42dbd0eSchristos 
25*c42dbd0eSchristos char *
ctf_sha1_fini(ctf_sha1_t * sha1,char * buf)26*c42dbd0eSchristos ctf_sha1_fini (ctf_sha1_t *sha1, char *buf)
27*c42dbd0eSchristos {
28*c42dbd0eSchristos   size_t i;
29*c42dbd0eSchristos 
30*c42dbd0eSchristos   /* Alignment suitable for a uint32_t. */
31*c42dbd0eSchristos   union
32*c42dbd0eSchristos   {
33*c42dbd0eSchristos     uint32_t align;
34*c42dbd0eSchristos     unsigned char digest[((CTF_SHA1_SIZE - 1) / 2) + 1];
35*c42dbd0eSchristos   } align;
36*c42dbd0eSchristos 
37*c42dbd0eSchristos   sha1_finish_ctx (sha1, align.digest);
38*c42dbd0eSchristos 
39*c42dbd0eSchristos   if (!buf)
40*c42dbd0eSchristos     return NULL;
41*c42dbd0eSchristos 
42*c42dbd0eSchristos   buf[CTF_SHA1_SIZE - 1] = '\0';
43*c42dbd0eSchristos 
44*c42dbd0eSchristos   for (i = 0; i < (CTF_SHA1_SIZE - 1) / 2; i++)
45*c42dbd0eSchristos     {
46*c42dbd0eSchristos       buf[2 * i] = hex[align.digest[i] >> 4];
47*c42dbd0eSchristos       buf[2 * i + 1] = hex[align.digest[i] & 0xf];
48*c42dbd0eSchristos     }
49*c42dbd0eSchristos   return buf;
50*c42dbd0eSchristos }
51