Add translations for various sub-directories
[binutils-gdb.git] / ld / ldbuildid.c
bloba4a6dc3c3e9403e72bcd52eaa6dfda39fd912586
1 /* ldbuildid.c - Build Id support routines
2 Copyright (C) 2013-2025 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 #ifdef WITH_XXHASH
27 #define XXH_INLINE_ALL
28 #include <xxhash.h>
29 #endif
30 #include "ldbuildid.h"
31 #ifdef __MINGW32__
32 #include <windows.h>
33 #include <rpcdce.h>
34 #endif
36 #define streq(a,b) strcmp ((a), (b)) == 0
38 bool
39 validate_build_id_style (const char *style)
41 if ((streq (style, "md5")) || (streq (style, "sha1"))
42 #ifdef WITH_XXHASH
43 || (streq (style, "xx"))
44 #endif
45 || (streq (style, "uuid")) || (startswith (style, "0x")))
46 return true;
48 return false;
51 bfd_size_type
52 compute_build_id_size (const char *style)
54 if (streq (style, "md5") || streq (style, "uuid"))
55 return 128 / 8;
57 #ifdef WITH_XXHASH
58 if (streq (style, "xx"))
59 return 128 / 8;
60 #endif
62 if (streq (style, "sha1"))
63 return 160 / 8;
65 if (startswith (style, "0x"))
67 bfd_size_type size = 0;
68 /* ID is in string form (hex). Count the bytes. */
69 const char *id = style + 2;
73 if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
75 ++size;
76 id += 2;
78 else if (*id == '-' || *id == ':')
79 ++id;
80 else
82 size = 0;
83 break;
85 } while (*id != '\0');
86 return size;
89 return 0;
92 static unsigned char
93 read_hex (const char xdigit)
95 if (ISDIGIT (xdigit))
96 return xdigit - '0';
98 if (ISUPPER (xdigit))
99 return xdigit - 'A' + 0xa;
101 if (ISLOWER (xdigit))
102 return xdigit - 'a' + 0xa;
104 abort ();
105 return 0;
109 #ifdef WITH_XXHASH
110 static void
111 xx_process_bytes(const void* buffer, size_t size, void* state)
113 XXH3_128bits_update ((XXH3_state_t*) state, buffer, size);
115 #endif
118 bool
119 generate_build_id (bfd *abfd,
120 const char *style,
121 checksum_fn checksum_contents,
122 unsigned char *id_bits,
123 int size ATTRIBUTE_UNUSED)
125 #ifdef WITH_XXHASH
126 if (streq (style, "xx"))
128 XXH3_state_t* state = XXH3_createState ();
129 if (!state)
131 return false;
133 XXH3_128bits_reset (state);
134 if (!(*checksum_contents) (abfd, &xx_process_bytes, state))
136 XXH3_freeState (state);
137 return false;
139 XXH128_hash_t result = XXH3_128bits_digest (state);
140 XXH3_freeState (state);
141 /* Use canonical-endianness output. */
142 XXH128_canonical_t result_canon;
143 XXH128_canonicalFromHash (&result_canon, result);
144 memcpy (id_bits, &result_canon,
145 (size_t) size < sizeof (result) ? (size_t) size : sizeof (result));
147 else
148 #endif
149 if (streq (style, "md5"))
151 struct md5_ctx ctx;
153 md5_init_ctx (&ctx);
154 if (!(*checksum_contents) (abfd, (sum_fn) &md5_process_bytes, &ctx))
155 return false;
156 md5_finish_ctx (&ctx, id_bits);
158 else if (streq (style, "sha1"))
160 struct sha1_ctx ctx;
162 sha1_init_ctx (&ctx);
163 if (!(*checksum_contents) (abfd, (sum_fn) sha1_choose_process_bytes (),
164 &ctx))
165 return false;
166 sha1_finish_ctx (&ctx, id_bits);
168 else if (streq (style, "uuid"))
170 #ifndef __MINGW32__
171 int n;
172 int fd = open ("/dev/urandom", O_RDONLY);
174 if (fd < 0)
175 return false;
176 n = read (fd, id_bits, size);
177 close (fd);
178 if (n < size)
179 return false;
180 #else /* __MINGW32__ */
181 typedef RPC_STATUS (RPC_ENTRY * UuidCreateFn) (UUID *);
182 UUID uuid;
183 UuidCreateFn uuid_create = 0;
184 HMODULE rpc_library = LoadLibrary ("rpcrt4.dll");
186 if (!rpc_library)
187 return false;
188 uuid_create = (UuidCreateFn) (void (WINAPI *)(void)) GetProcAddress (rpc_library, "UuidCreate");
189 if (!uuid_create)
191 FreeLibrary (rpc_library);
192 return false;
195 if (uuid_create (&uuid) != RPC_S_OK)
197 FreeLibrary (rpc_library);
198 return false;
200 FreeLibrary (rpc_library);
201 memcpy (id_bits, &uuid,
202 (size_t) size < sizeof (UUID) ? (size_t) size : sizeof (UUID));
203 #endif /* __MINGW32__ */
205 else if (startswith (style, "0x"))
207 /* ID is in string form (hex). Convert to bits. */
208 const char *id = style + 2;
209 size_t n = 0;
213 if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
215 id_bits[n] = read_hex (*id++) << 4;
216 id_bits[n++] |= read_hex (*id++);
218 else if (*id == '-' || *id == ':')
219 ++id;
220 else
221 abort (); /* Should have been validated earlier. */
223 while (*id != '\0');
225 else
226 abort (); /* Should have been validated earlier. */
228 return true;