1 /* Compressed section support (intended for debug sections).
2 Copyright 2008, 2010, 2011, 2012
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. */
31 decompress_contents (bfd_byte
*compressed_buffer
,
32 bfd_size_type compressed_size
,
33 bfd_byte
*uncompressed_buffer
,
34 bfd_size_type uncompressed_size
)
39 /* It is possible the section consists of several compressed
40 buffers concatenated together, so we uncompress in a loop. */
44 strm
.avail_in
= compressed_size
- 12;
45 strm
.next_in
= (Bytef
*) compressed_buffer
+ 12;
46 strm
.avail_out
= uncompressed_size
;
48 rc
= inflateInit (&strm
);
49 while (strm
.avail_in
> 0)
53 strm
.next_out
= ((Bytef
*) uncompressed_buffer
54 + (uncompressed_size
- strm
.avail_out
));
55 rc
= inflate (&strm
, Z_FINISH
);
56 if (rc
!= Z_STREAM_END
)
58 rc
= inflateReset (&strm
);
60 rc
= inflateEnd (&strm
);
61 return rc
== Z_OK
&& strm
.avail_out
== 0;
67 bfd_compress_section_contents
70 bfd_boolean bfd_compress_section_contents
71 (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer,
72 bfd_size_type uncompressed_size);
76 Compress data of the size specified in @var{uncompressed_size}
77 and pointed to by @var{uncompressed_buffer} using zlib and store
78 as the contents field. This function assumes the contents
79 field was allocated using bfd_malloc() or equivalent. If zlib
80 is not installed on this machine, the input is unmodified.
82 Return @code{TRUE} if the full section contents is compressed
87 bfd_compress_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
88 sec_ptr sec ATTRIBUTE_UNUSED
,
89 bfd_byte
*uncompressed_buffer ATTRIBUTE_UNUSED
,
90 bfd_size_type uncompressed_size ATTRIBUTE_UNUSED
)
93 bfd_set_error (bfd_error_invalid_operation
);
96 uLong compressed_size
;
97 bfd_byte
*compressed_buffer
;
99 compressed_size
= compressBound (uncompressed_size
) + 12;
100 compressed_buffer
= (bfd_byte
*) bfd_malloc (compressed_size
);
102 if (compressed_buffer
== NULL
)
105 if (compress ((Bytef
*) compressed_buffer
+ 12,
107 (const Bytef
*) uncompressed_buffer
,
108 uncompressed_size
) != Z_OK
)
110 free (compressed_buffer
);
111 bfd_set_error (bfd_error_bad_value
);
115 /* Write the zlib header. In this case, it should be "ZLIB" followed
116 by the uncompressed section size, 8 bytes in big-endian order. */
117 memcpy (compressed_buffer
, "ZLIB", 4);
118 compressed_buffer
[11] = uncompressed_size
; uncompressed_size
>>= 8;
119 compressed_buffer
[10] = uncompressed_size
; uncompressed_size
>>= 8;
120 compressed_buffer
[9] = uncompressed_size
; uncompressed_size
>>= 8;
121 compressed_buffer
[8] = uncompressed_size
; uncompressed_size
>>= 8;
122 compressed_buffer
[7] = uncompressed_size
; uncompressed_size
>>= 8;
123 compressed_buffer
[6] = uncompressed_size
; uncompressed_size
>>= 8;
124 compressed_buffer
[5] = uncompressed_size
; uncompressed_size
>>= 8;
125 compressed_buffer
[4] = uncompressed_size
;
126 compressed_size
+= 12;
128 /* Free the uncompressed contents if we compress in place. */
129 if (uncompressed_buffer
== sec
->contents
)
130 free (uncompressed_buffer
);
132 sec
->contents
= compressed_buffer
;
133 sec
->size
= compressed_size
;
134 sec
->compress_status
= COMPRESS_SECTION_DONE
;
137 #endif /* HAVE_ZLIB_H */
142 bfd_get_full_section_contents
145 bfd_boolean bfd_get_full_section_contents
146 (bfd *abfd, asection *section, bfd_byte **ptr);
149 Read all data from @var{section} in BFD @var{abfd}, decompress
150 if needed, and store in @var{*ptr}. If @var{*ptr} is NULL,
151 return @var{*ptr} with memory malloc'd by this function.
153 Return @code{TRUE} if the full section contents is retrieved
158 bfd_get_full_section_contents (bfd
*abfd
, sec_ptr sec
, bfd_byte
**ptr
)
164 bfd_size_type compressed_size
;
165 bfd_size_type uncompressed_size
;
166 bfd_size_type rawsize
;
167 bfd_byte
*compressed_buffer
;
168 bfd_byte
*uncompressed_buffer
;
171 if (abfd
->direction
!= write_direction
&& sec
->rawsize
!= 0)
178 switch (sec
->compress_status
)
180 case COMPRESS_SECTION_NONE
:
183 p
= (bfd_byte
*) bfd_malloc (sz
);
187 if (!bfd_get_section_contents (abfd
, sec
, p
, 0, sz
))
196 case DECOMPRESS_SECTION_SIZED
:
198 bfd_set_error (bfd_error_invalid_operation
);
201 /* Read in the full compressed section contents. */
202 uncompressed_size
= sec
->size
;
203 compressed_size
= sec
->compressed_size
;
204 compressed_buffer
= (bfd_byte
*) bfd_malloc (compressed_size
);
205 if (compressed_buffer
== NULL
)
207 rawsize
= sec
->rawsize
;
208 /* Clear rawsize, set size to compressed size and set compress_status
209 to COMPRESS_SECTION_NONE. If the compressed size is bigger than
210 the uncompressed size, bfd_get_section_contents will fail. */
212 sec
->size
= compressed_size
;
213 sec
->compress_status
= COMPRESS_SECTION_NONE
;
214 ret
= bfd_get_section_contents (abfd
, sec
, compressed_buffer
,
216 /* Restore rawsize and size. */
217 sec
->rawsize
= rawsize
;
218 sec
->size
= uncompressed_size
;
219 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
221 goto fail_compressed
;
223 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
224 if (uncompressed_buffer
== NULL
)
225 goto fail_compressed
;
227 if (!decompress_contents (compressed_buffer
, compressed_size
,
228 uncompressed_buffer
, uncompressed_size
))
230 bfd_set_error (bfd_error_bad_value
);
231 free (uncompressed_buffer
);
233 free (compressed_buffer
);
237 free (compressed_buffer
);
238 sec
->contents
= uncompressed_buffer
;
239 sec
->compress_status
= COMPRESS_SECTION_DONE
;
243 case COMPRESS_SECTION_DONE
:
246 p
= (bfd_byte
*) bfd_malloc (sz
);
251 memcpy (p
, sec
->contents
, sz
);
261 bfd_is_section_compressed
264 bfd_boolean bfd_is_section_compressed
265 (bfd *abfd, asection *section);
268 Return @code{TRUE} if @var{section} is compressed.
272 bfd_is_section_compressed (bfd
*abfd
, sec_ptr sec
)
274 bfd_byte compressed_buffer
[12];
276 /* Read the zlib header. In this case, it should be "ZLIB" followed
277 by the uncompressed section size, 8 bytes in big-endian order. */
278 return (bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12)
279 && CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"));
284 bfd_init_section_decompress_status
287 bfd_boolean bfd_init_section_decompress_status
288 (bfd *abfd, asection *section);
291 Record compressed section size, update section size with
292 decompressed size and set compress_status to
293 DECOMPRESS_SECTION_SIZED.
295 Return @code{FALSE} if the section is not a valid compressed
296 section or zlib is not installed on this machine. Otherwise,
301 bfd_init_section_decompress_status (bfd
*abfd ATTRIBUTE_UNUSED
,
302 sec_ptr sec ATTRIBUTE_UNUSED
)
305 bfd_set_error (bfd_error_invalid_operation
);
308 bfd_byte compressed_buffer
[12];
309 bfd_size_type uncompressed_size
;
311 if (sec
->rawsize
!= 0
312 || sec
->contents
!= NULL
313 || sec
->compress_status
!= COMPRESS_SECTION_NONE
314 || !bfd_get_section_contents (abfd
, sec
, compressed_buffer
, 0, 12))
316 bfd_set_error (bfd_error_invalid_operation
);
320 /* Read the zlib header. In this case, it should be "ZLIB" followed
321 by the uncompressed section size, 8 bytes in big-endian order. */
322 if (! CONST_STRNEQ ((char*) compressed_buffer
, "ZLIB"))
324 bfd_set_error (bfd_error_wrong_format
);
328 uncompressed_size
= compressed_buffer
[4]; uncompressed_size
<<= 8;
329 uncompressed_size
+= compressed_buffer
[5]; uncompressed_size
<<= 8;
330 uncompressed_size
+= compressed_buffer
[6]; uncompressed_size
<<= 8;
331 uncompressed_size
+= compressed_buffer
[7]; uncompressed_size
<<= 8;
332 uncompressed_size
+= compressed_buffer
[8]; uncompressed_size
<<= 8;
333 uncompressed_size
+= compressed_buffer
[9]; uncompressed_size
<<= 8;
334 uncompressed_size
+= compressed_buffer
[10]; uncompressed_size
<<= 8;
335 uncompressed_size
+= compressed_buffer
[11];
337 sec
->compressed_size
= sec
->size
;
338 sec
->size
= uncompressed_size
;
339 sec
->compress_status
= DECOMPRESS_SECTION_SIZED
;
347 bfd_init_section_compress_status
350 bfd_boolean bfd_init_section_compress_status
351 (bfd *abfd, asection *section);
354 If open for read, compress section, update section size with
355 compressed size and set compress_status to COMPRESS_SECTION_DONE.
357 Return @code{FALSE} if the section is not a valid compressed
358 section or zlib is not installed on this machine. Otherwise,
363 bfd_init_section_compress_status (bfd
*abfd ATTRIBUTE_UNUSED
,
364 sec_ptr sec ATTRIBUTE_UNUSED
)
367 bfd_set_error (bfd_error_invalid_operation
);
370 bfd_size_type uncompressed_size
;
371 bfd_byte
*uncompressed_buffer
;
374 /* Error if not opened for read. */
375 if (abfd
->direction
!= read_direction
378 || sec
->contents
!= NULL
379 || sec
->compress_status
!= COMPRESS_SECTION_NONE
)
381 bfd_set_error (bfd_error_invalid_operation
);
385 /* Read in the full section contents and compress it. */
386 uncompressed_size
= sec
->size
;
387 uncompressed_buffer
= (bfd_byte
*) bfd_malloc (uncompressed_size
);
388 if (!bfd_get_section_contents (abfd
, sec
, uncompressed_buffer
,
389 0, uncompressed_size
))
392 ret
= bfd_compress_section_contents (abfd
, sec
,
396 free (uncompressed_buffer
);