1 /* ldbuildid.c - Build Id support routines
2 Copyright (C) 2013-2015 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. */
23 #include "safe-ctype.h"
26 #include "ldbuildid.h"
28 #define streq(a,b) strcmp ((a), (b)) == 0
29 #define strneq(a,b,n) strncmp ((a), (b), (n)) == 0
32 validate_build_id_style (const char *style
)
34 if ((streq (style
, "md5")) || (streq (style
, "sha1"))
36 || (streq (style
, "uuid"))
38 || (strneq (style
, "0x", 2)))
45 compute_build_id_size (const char *style
)
47 if (streq (style
, "md5") || streq (style
, "uuid"))
50 if (streq (style
, "sha1"))
53 if (strneq (style
, "0x", 2))
55 bfd_size_type size
= 0;
56 /* ID is in string form (hex). Count the bytes. */
57 const char *id
= style
+ 2;
61 if (ISXDIGIT (id
[0]) && ISXDIGIT (id
[1]))
66 else if (*id
== '-' || *id
== ':')
73 } while (*id
!= '\0');
81 read_hex (const char xdigit
)
87 return xdigit
- 'A' + 0xa;
90 return xdigit
- 'a' + 0xa;
97 generate_build_id (bfd
*abfd
,
99 checksum_fn checksum_contents
,
100 unsigned char *id_bits
,
101 int size ATTRIBUTE_UNUSED
)
103 if (streq (style
, "md5"))
108 if (!(*checksum_contents
) (abfd
, (sum_fn
) &md5_process_bytes
, &ctx
))
110 md5_finish_ctx (&ctx
, id_bits
);
112 else if (streq (style
, "sha1"))
116 sha1_init_ctx (&ctx
);
117 if (!(*checksum_contents
) (abfd
, (sum_fn
) &sha1_process_bytes
, &ctx
))
119 sha1_finish_ctx (&ctx
, id_bits
);
122 else if (streq (style
, "uuid"))
125 int fd
= open ("/dev/urandom", O_RDONLY
);
129 n
= read (fd
, id_bits
, size
);
135 else if (strneq (style
, "0x", 2))
137 /* ID is in string form (hex). Convert to bits. */
138 const char *id
= style
+ 2;
143 if (ISXDIGIT (id
[0]) && ISXDIGIT (id
[1]))
145 id_bits
[n
] = read_hex (*id
++) << 4;
146 id_bits
[n
++] |= read_hex (*id
++);
148 else if (*id
== '-' || *id
== ':')
151 abort (); /* Should have been validated earlier. */
152 } while (*id
!= '\0');
155 abort (); /* Should have been validated earlier. */