1 /* Compressed section support (intended for debug sections).
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
32 decompress_contents (bfd_byte *compressed_buffer,
33 bfd_size_type compressed_size,
34 bfd_byte *uncompressed_buffer,
35 bfd_size_type uncompressed_size)
40 /* It is possible the section consists of several compressed
41 buffers concatenated together, so we uncompress in a loop. */
45 strm.avail_in = compressed_size - 12;
46 strm.next_in = (Bytef*) compressed_buffer + 12;
47 strm.avail_out = uncompressed_size;
49 rc = inflateInit (&strm);
50 while (strm.avail_in > 0)
54 strm.next_out = ((Bytef*) uncompressed_buffer
55 + (uncompressed_size - strm.avail_out));
56 rc = inflate (&strm, Z_FINISH);
57 if (rc != Z_STREAM_END)
59 rc = inflateReset (&strm);
61 rc = inflateEnd (&strm);
62 return rc != Z_OK || strm.avail_out != 0 ? FALSE: TRUE;
68 bfd_compress_section_contents
71 bfd_boolean bfd_compress_section_contents
72 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer,
73 bfd_size_type uncompressed_size);
77 Compress data of the size specified in @var{uncompressed_size}
78 and pointed to by @var{uncompressed_buffer} using zlib and store
79 as the contents field. This function assumes the contents
80 field was allocated using bfd_malloc() or equivalent. If zlib
81 is not installed on this machine, the input is unmodified.
83 Return @code{TRUE} if the full section contents is compressed
88 bfd_compress_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
89 sec_ptr sec ATTRIBUTE_UNUSED,
90 bfd_byte *uncompressed_buffer ATTRIBUTE_UNUSED,
91 bfd_size_type uncompressed_size ATTRIBUTE_UNUSED)
94 bfd_set_error (bfd_error_invalid_operation);
97 bfd_size_type compressed_size;
98 bfd_byte *compressed_buffer;
100 compressed_size = compressBound (uncompressed_size) + 12;
101 compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
103 if (compress ((Bytef*) compressed_buffer + 12,
105 (const Bytef*) uncompressed_buffer,
106 uncompressed_size) != Z_OK)
108 free (compressed_buffer);
109 bfd_set_error (bfd_error_bad_value);
113 /* Write the zlib header. In this case, it should be "ZLIB" followed
114 by the uncompressed section size, 8 bytes in big-endian order. */
115 memcpy (compressed_buffer, "ZLIB", 4);
116 compressed_buffer[11] = uncompressed_size; uncompressed_size >>= 8;
117 compressed_buffer[10] = uncompressed_size; uncompressed_size >>= 8;
118 compressed_buffer[9] = uncompressed_size; uncompressed_size >>= 8;
119 compressed_buffer[8] = uncompressed_size; uncompressed_size >>= 8;
120 compressed_buffer[7] = uncompressed_size; uncompressed_size >>= 8;
121 compressed_buffer[6] = uncompressed_size; uncompressed_size >>= 8;
122 compressed_buffer[5] = uncompressed_size; uncompressed_size >>= 8;
123 compressed_buffer[4] = uncompressed_size;
124 compressed_size += 12;
126 /* Free the uncompressed contents if we compress in place. */
127 if (uncompressed_buffer == sec->contents)
128 free (uncompressed_buffer);
130 sec->contents = compressed_buffer;
131 sec->size = compressed_size;
132 sec->compress_status = COMPRESS_SECTION_DONE;
135 #endif /* HAVE_ZLIB_H */
140 bfd_get_full_section_contents
143 bfd_boolean bfd_get_full_section_contents
144 (bfd *abfd, asection *section, bfd_byte **ptr);
147 Read all data from @var{section} in BFD @var{abfd}, decompress
148 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
149 return @var{*ptr} with memory malloc'd by this function.
151 Return @code{TRUE} if the full section contents is retrieved
156 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
158 bfd_size_type sz = sec->rawsize ? sec->rawsize : sec->size;
160 bfd_boolean need_free, ret;
162 bfd_size_type compressed_size;
163 bfd_size_type uncompressed_size;
164 bfd_size_type rawsize;
165 bfd_byte *compressed_buffer;
166 bfd_byte *uncompressed_buffer;
172 switch (sec->compress_status)
174 case COMPRESS_SECTION_NONE:
177 p = (bfd_byte *) bfd_malloc (sz);
185 ret = bfd_get_section_contents (abfd, sec, p, 0, sz);
186 if (!ret && need_free)
190 case COMPRESS_SECTION_DONE:
192 memcpy (p, sec->contents, sz);
194 *ptr = sec->contents;
197 case DECOMPRESS_SECTION_SIZED:
205 bfd_set_error (bfd_error_invalid_operation);
208 /* Read in the full compressed section contents. */
209 uncompressed_size = sec->size;
210 compressed_size = sec->compressed_size;
211 compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
212 rawsize = sec->rawsize;
213 /* Clear rawsize, set size to compressed size and set compress_status
214 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
215 the uncompressed size, bfd_get_section_contents will fail. */
217 sec->size = compressed_size;
218 sec->compress_status = COMPRESS_SECTION_NONE;
219 ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
221 /* Restore rawsize and size. */
222 sec->rawsize = rawsize;
223 sec->size = uncompressed_size;
227 sec->compress_status = DECOMPRESS_SECTION_SIZED;
228 free (compressed_buffer);
232 /* Decompress to caller buffer directly if it is provided. */
234 uncompressed_buffer = p;
237 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
238 if (uncompressed_buffer == NULL)
239 goto fail_compressed;
242 if (!decompress_contents (compressed_buffer, compressed_size,
243 uncompressed_buffer, uncompressed_size))
245 sec->compress_status = DECOMPRESS_SECTION_SIZED;
246 free (compressed_buffer);
248 free (uncompressed_buffer);
249 bfd_set_error (bfd_error_bad_value);
253 free (compressed_buffer);
255 *ptr = uncompressed_buffer;
257 sec->contents = uncompressed_buffer;
258 sec->compress_status = COMPRESS_SECTION_DONE;
266 bfd_is_section_compressed
269 bfd_boolean bfd_is_section_compressed
270 (bfd *abfd, asection *section);
273 Return @code{TRUE} if @var{section} is compressed.
277 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
279 bfd_byte compressed_buffer [12];
281 /* Read the zlib header. In this case, it should be "ZLIB" followed
282 by the uncompressed section size, 8 bytes in big-endian order. */
283 return (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
284 && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
289 bfd_init_section_decompress_status
292 bfd_boolean bfd_init_section_decompress_status
293 (bfd *abfd, asection *section);
296 Record compressed section size, update section size with
297 decompressed size and set compress_status to
298 DECOMPRESS_SECTION_SIZED.
300 Return @code{FALSE} if the section is not a valid compressed
301 section or zlib is not installed on this machine. Otherwise,
306 bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED,
307 sec_ptr sec ATTRIBUTE_UNUSED)
310 bfd_set_error (bfd_error_invalid_operation);
313 bfd_byte compressed_buffer [12];
314 bfd_size_type uncompressed_size;
316 if (sec->rawsize != 0
317 || sec->contents != NULL
318 || sec->compress_status != COMPRESS_SECTION_NONE
319 || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
321 bfd_set_error (bfd_error_invalid_operation);
325 /* Read the zlib header. In this case, it should be "ZLIB" followed
326 by the uncompressed section size, 8 bytes in big-endian order. */
327 if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
329 bfd_set_error (bfd_error_wrong_format);
333 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
334 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
335 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
336 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
337 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
338 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
339 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
340 uncompressed_size += compressed_buffer[11];
342 sec->compressed_size = sec->size;
343 sec->size = uncompressed_size;
344 sec->compress_status = DECOMPRESS_SECTION_SIZED;
352 bfd_init_section_compress_status
355 bfd_boolean bfd_init_section_compress_status
356 (bfd *abfd, asection *section);
359 If open for read, compress section, update section size with
360 compressed size and set compress_status to COMPRESS_SECTION_DONE.
362 Return @code{FALSE} if the section is not a valid compressed
363 section or zlib is not installed on this machine. Otherwise,
368 bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
369 sec_ptr sec ATTRIBUTE_UNUSED)
372 bfd_set_error (bfd_error_invalid_operation);
375 bfd_size_type uncompressed_size;
376 bfd_byte *uncompressed_buffer;
379 /* Error if not opened for read. */
380 if (abfd->direction != read_direction
383 || sec->contents != NULL
384 || sec->compress_status != COMPRESS_SECTION_NONE)
386 bfd_set_error (bfd_error_invalid_operation);
390 /* Read in the full section contents and compress it. */
391 uncompressed_size = sec->size;
392 uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
393 if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
394 0, uncompressed_size))
397 ret = bfd_compress_section_contents (abfd, sec,
401 free (uncompressed_buffer);