/src/elfutils/libelf/gnuhash_xlate.h
Line | Count | Source |
1 | | /* Conversion functions for versioning information. |
2 | | Copyright (C) 2006, 2007 Red Hat, Inc. |
3 | | Copyright (C) 2023, Mark J. Wielaard <mark@klomp.org> |
4 | | This file is part of elfutils. |
5 | | Written by Ulrich Drepper <drepper@redhat.com>, 2006. |
6 | | |
7 | | This file is free software; you can redistribute it and/or modify |
8 | | it under the terms of either |
9 | | |
10 | | * the GNU Lesser General Public License as published by the Free |
11 | | Software Foundation; either version 3 of the License, or (at |
12 | | your option) any later version |
13 | | |
14 | | or |
15 | | |
16 | | * the GNU General Public License as published by the Free |
17 | | Software Foundation; either version 2 of the License, or (at |
18 | | your option) any later version |
19 | | |
20 | | or both in parallel, as here. |
21 | | |
22 | | elfutils is distributed in the hope that it will be useful, but |
23 | | WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
25 | | General Public License for more details. |
26 | | |
27 | | You should have received copies of the GNU General Public License and |
28 | | the GNU Lesser General Public License along with this program. If |
29 | | not, see <http://www.gnu.org/licenses/>. */ |
30 | | |
31 | | #include <gelf.h> |
32 | | |
33 | | #include "libelfP.h" |
34 | | |
35 | | |
36 | | static void |
37 | | elf_cvt_gnuhash (void *dest, const void *src, size_t len, int encode) |
38 | 65 | { |
39 | 65 | size_t size = len; |
40 | | /* The GNU hash table format on 64 bit machines mixes 32 bit and 64 bit |
41 | | words. We must detangle them here. */ |
42 | 65 | Elf32_Word *dest32 = dest; |
43 | 65 | const Elf32_Word *src32 = src; |
44 | | |
45 | | /* First four control words, 32 bits. */ |
46 | 315 | for (unsigned int cnt = 0; cnt < 4; ++cnt) |
47 | 254 | { |
48 | 254 | if (len < 4) |
49 | 4 | goto done; |
50 | 250 | dest32[cnt] = bswap_32 (src32[cnt]); |
51 | 250 | len -= 4; |
52 | 250 | } |
53 | | |
54 | 61 | Elf32_Word bitmask_words = encode ? src32[2] : dest32[2]; |
55 | | |
56 | | /* Now the 64 bit words. */ |
57 | 61 | Elf64_Xword *dest64 = (Elf64_Xword *) &dest32[4]; |
58 | 61 | const Elf64_Xword *src64 = (const Elf64_Xword *) &src32[4]; |
59 | 1.07M | for (unsigned int cnt = 0; cnt < bitmask_words; ++cnt) |
60 | 1.07M | { |
61 | 1.07M | if (len < 8) |
62 | 47 | goto done; |
63 | 1.07M | dest64[cnt] = bswap_64 (src64[cnt]); |
64 | 1.07M | len -= 8; |
65 | 1.07M | } |
66 | | |
67 | | /* The rest are 32 bit words again. */ |
68 | 14 | src32 = (const Elf32_Word *) &src64[bitmask_words]; |
69 | 14 | dest32 = (Elf32_Word *) &dest64[bitmask_words]; |
70 | 396k | while (len >= 4) |
71 | 396k | { |
72 | 396k | *dest32++ = bswap_32 (*src32++); |
73 | 396k | len -= 4; |
74 | 396k | } |
75 | | |
76 | 65 | done: |
77 | | /* If there are any bytes left, we weren't able to convert the |
78 | | partial structures, just copy them over. */ |
79 | 65 | if (len > 0) |
80 | 51 | memmove (dest + size - len, src + size - len, len); |
81 | 65 | } |