xref: /netbsd-src/external/gpl3/binutils/dist/libctf/ctf-sha1.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
14f645668Schristos /* SHA-1 thunks.
2*cb63e24eSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
34f645668Schristos 
44f645668Schristos    This file is part of libctf.
54f645668Schristos 
64f645668Schristos    libctf is free software; you can redistribute it and/or modify it under
74f645668Schristos    the terms of the GNU General Public License as published by the Free
84f645668Schristos    Software Foundation; either version 3, or (at your option) any later
94f645668Schristos    version.
104f645668Schristos 
114f645668Schristos    This program is distributed in the hope that it will be useful, but
124f645668Schristos    WITHOUT ANY WARRANTY; without even the implied warranty of
134f645668Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
144f645668Schristos    See the GNU General Public License for more details.
154f645668Schristos 
164f645668Schristos    You should have received a copy of the GNU General Public License
174f645668Schristos    along with this program; see the file COPYING.  If not see
184f645668Schristos    <http://www.gnu.org/licenses/>.  */
194f645668Schristos 
204f645668Schristos #include <ctf-impl.h>
214f645668Schristos #include <ctf-sha1.h>
224f645668Schristos 
234f645668Schristos static const char hex[] = "0123456789abcdef";
244f645668Schristos 
254f645668Schristos char *
ctf_sha1_fini(ctf_sha1_t * sha1,char * buf)264f645668Schristos ctf_sha1_fini (ctf_sha1_t *sha1, char *buf)
274f645668Schristos {
284f645668Schristos   size_t i;
294f645668Schristos 
304f645668Schristos   /* Alignment suitable for a uint32_t. */
314f645668Schristos   union
324f645668Schristos   {
334f645668Schristos     uint32_t align;
344f645668Schristos     unsigned char digest[((CTF_SHA1_SIZE - 1) / 2) + 1];
354f645668Schristos   } align;
364f645668Schristos 
374f645668Schristos   sha1_finish_ctx (sha1, align.digest);
384f645668Schristos 
394f645668Schristos   if (!buf)
404f645668Schristos     return NULL;
414f645668Schristos 
424f645668Schristos   buf[CTF_SHA1_SIZE - 1] = '\0';
434f645668Schristos 
444f645668Schristos   for (i = 0; i < (CTF_SHA1_SIZE - 1) / 2; i++)
454f645668Schristos     {
464f645668Schristos       buf[2 * i] = hex[align.digest[i] >> 4];
474f645668Schristos       buf[2 * i + 1] = hex[align.digest[i] & 0xf];
484f645668Schristos     }
494f645668Schristos   return buf;
504f645668Schristos }
51