gdb/copyright.py: Adapt after move of gnulib from gdb to toplevel
[binutils-gdb.git] / ld / ldbuildid.c
blobc4ad19992b51161bb42049a20fd92766ce79d426
1 /* ldbuildid.c - Build Id support routines
2 Copyright (C) 2013-2019 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "safe-ctype.h"
24 #include "md5.h"
25 #include "sha1.h"
26 #include "ldbuildid.h"
27 #ifdef __MINGW32__
28 #include <windows.h>
29 #include <rpcdce.h>
30 #endif
32 #define streq(a,b) strcmp ((a), (b)) == 0
33 #define strneq(a,b,n) strncmp ((a), (b), (n)) == 0
35 bfd_boolean
36 validate_build_id_style (const char *style)
38 if ((streq (style, "md5")) || (streq (style, "sha1"))
39 || (streq (style, "uuid")) || (strneq (style, "0x", 2)))
40 return TRUE;
42 return FALSE;
45 bfd_size_type
46 compute_build_id_size (const char *style)
48 if (streq (style, "md5") || streq (style, "uuid"))
49 return 128 / 8;
51 if (streq (style, "sha1"))
52 return 160 / 8;
54 if (strneq (style, "0x", 2))
56 bfd_size_type size = 0;
57 /* ID is in string form (hex). Count the bytes. */
58 const char *id = style + 2;
62 if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
64 ++size;
65 id += 2;
67 else if (*id == '-' || *id == ':')
68 ++id;
69 else
71 size = 0;
72 break;
74 } while (*id != '\0');
75 return size;
78 return 0;
81 static unsigned char
82 read_hex (const char xdigit)
84 if (ISDIGIT (xdigit))
85 return xdigit - '0';
87 if (ISUPPER (xdigit))
88 return xdigit - 'A' + 0xa;
90 if (ISLOWER (xdigit))
91 return xdigit - 'a' + 0xa;
93 abort ();
94 return 0;
97 bfd_boolean
98 generate_build_id (bfd *abfd,
99 const char *style,
100 checksum_fn checksum_contents,
101 unsigned char *id_bits,
102 int size ATTRIBUTE_UNUSED)
104 if (streq (style, "md5"))
106 struct md5_ctx ctx;
108 md5_init_ctx (&ctx);
109 if (!(*checksum_contents) (abfd, (sum_fn) &md5_process_bytes, &ctx))
110 return FALSE;
111 md5_finish_ctx (&ctx, id_bits);
113 else if (streq (style, "sha1"))
115 struct sha1_ctx ctx;
117 sha1_init_ctx (&ctx);
118 if (!(*checksum_contents) (abfd, (sum_fn) &sha1_process_bytes, &ctx))
119 return FALSE;
120 sha1_finish_ctx (&ctx, id_bits);
122 else if (streq (style, "uuid"))
124 #ifndef __MINGW32__
125 int n;
126 int fd = open ("/dev/urandom", O_RDONLY);
128 if (fd < 0)
129 return FALSE;
130 n = read (fd, id_bits, size);
131 close (fd);
132 if (n < size)
133 return FALSE;
134 #else /* __MINGW32__ */
135 typedef RPC_STATUS (RPC_ENTRY * UuidCreateFn) (UUID *);
136 UUID uuid;
137 UuidCreateFn uuid_create = 0;
138 HMODULE rpc_library = LoadLibrary ("rpcrt4.dll");
140 if (!rpc_library)
141 return FALSE;
142 uuid_create = (UuidCreateFn) (void (WINAPI *)(void)) GetProcAddress (rpc_library, "UuidCreate");
143 if (!uuid_create)
145 FreeLibrary (rpc_library);
146 return FALSE;
149 if (uuid_create (&uuid) != RPC_S_OK)
151 FreeLibrary (rpc_library);
152 return FALSE;
154 FreeLibrary (rpc_library);
155 memcpy (id_bits, &uuid,
156 (size_t) size < sizeof (UUID) ? (size_t) size : sizeof (UUID));
157 #endif /* __MINGW32__ */
159 else if (strneq (style, "0x", 2))
161 /* ID is in string form (hex). Convert to bits. */
162 const char *id = style + 2;
163 size_t n = 0;
167 if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
169 id_bits[n] = read_hex (*id++) << 4;
170 id_bits[n++] |= read_hex (*id++);
172 else if (*id == '-' || *id == ':')
173 ++id;
174 else
175 abort (); /* Should have been validated earlier. */
177 while (*id != '\0');
179 else
180 abort (); /* Should have been validated earlier. */
182 return TRUE;