* compress.c (bfd_get_full_section_contents): Don't cache
[external/binutils.git] / bfd / compress.c
1 /* Compressed section support (intended for debug sections).
2    Copyright 2008, 2010, 2011, 2012
3    Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #ifdef HAVE_ZLIB_H
26 #include <zlib.h>
27 #endif
28
29 #ifdef HAVE_ZLIB_H
30 static bfd_boolean
31 decompress_contents (bfd_byte *compressed_buffer,
32                      bfd_size_type compressed_size,
33                      bfd_byte *uncompressed_buffer,
34                      bfd_size_type uncompressed_size)
35 {
36   z_stream strm;
37   int rc;
38
39   /* It is possible the section consists of several compressed
40      buffers concatenated together, so we uncompress in a loop.  */
41   strm.zalloc = NULL;
42   strm.zfree = NULL;
43   strm.opaque = NULL;
44   strm.avail_in = compressed_size - 12;
45   strm.next_in = (Bytef*) compressed_buffer + 12;
46   strm.avail_out = uncompressed_size;
47
48   rc = inflateInit (&strm);
49   while (strm.avail_in > 0 && strm.avail_out > 0)
50     {
51       if (rc != Z_OK)
52         return FALSE;
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)
57         return FALSE;
58       rc = inflateReset (&strm);
59     }
60   rc = inflateEnd (&strm);
61   return rc == Z_OK && strm.avail_out == 0;
62 }
63 #endif
64
65 /*
66 FUNCTION
67         bfd_compress_section_contents
68
69 SYNOPSIS
70         bfd_boolean bfd_compress_section_contents
71           (bfd *abfd, asection *section, bfd_byte *uncompressed_buffer,
72            bfd_size_type uncompressed_size);
73
74 DESCRIPTION
75
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.
81
82         Return @code{TRUE} if the full section contents is compressed 
83         successfully.
84 */
85
86 bfd_boolean
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)
91 {
92 #ifndef HAVE_ZLIB_H
93   bfd_set_error (bfd_error_invalid_operation);
94   return FALSE;
95 #else
96   uLong compressed_size;
97   bfd_byte *compressed_buffer;
98
99   compressed_size = compressBound (uncompressed_size) + 12;
100   compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
101
102   if (compressed_buffer == NULL)
103     return FALSE;
104
105   if (compress ((Bytef*) compressed_buffer + 12,
106                 &compressed_size,
107                 (const Bytef*) uncompressed_buffer,
108                 uncompressed_size) != Z_OK)
109     {
110       free (compressed_buffer);
111       bfd_set_error (bfd_error_bad_value);
112       return FALSE;
113     }
114
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;
127
128   /* Free the uncompressed contents if we compress in place.  */
129   if (uncompressed_buffer == sec->contents)
130     free (uncompressed_buffer);
131
132   sec->contents = compressed_buffer;
133   sec->size = compressed_size;
134   sec->compress_status = COMPRESS_SECTION_DONE;
135
136   return TRUE;
137 #endif  /* HAVE_ZLIB_H */
138 }
139
140 /*
141 FUNCTION
142         bfd_get_full_section_contents
143
144 SYNOPSIS
145         bfd_boolean bfd_get_full_section_contents
146           (bfd *abfd, asection *section, bfd_byte **ptr);
147
148 DESCRIPTION
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.  
152
153         Return @code{TRUE} if the full section contents is retrieved
154         successfully.
155 */
156
157 bfd_boolean
158 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
159 {
160   bfd_size_type sz;
161   bfd_byte *p = *ptr;
162 #ifdef HAVE_ZLIB_H
163   bfd_boolean ret;
164   bfd_size_type compressed_size;
165   bfd_size_type uncompressed_size;
166   bfd_size_type rawsize;
167   bfd_byte *compressed_buffer;
168 #endif
169
170   if (abfd->direction != write_direction && sec->rawsize != 0)
171     sz = sec->rawsize;
172   else
173     sz = sec->size;
174   if (sz == 0)
175     return TRUE;
176
177   switch (sec->compress_status)
178     {
179     case COMPRESS_SECTION_NONE:
180       if (p == NULL)
181         {
182           p = (bfd_byte *) bfd_malloc (sz);
183           if (p == NULL)
184             return FALSE;
185         }
186       if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
187         {
188           if (*ptr != p)
189             free (p);
190           return FALSE;
191         }
192       *ptr = p;
193       return TRUE;
194
195     case DECOMPRESS_SECTION_SIZED:
196 #ifndef HAVE_ZLIB_H
197       bfd_set_error (bfd_error_invalid_operation);
198       return FALSE;
199 #else
200       /* Read in the full compressed section contents.  */
201       uncompressed_size = sec->size;
202       compressed_size = sec->compressed_size;
203       compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
204       if (compressed_buffer == NULL)
205         return FALSE;
206       rawsize = sec->rawsize;
207       /* Clear rawsize, set size to compressed size and set compress_status
208          to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
209          the uncompressed size, bfd_get_section_contents will fail.  */
210       sec->rawsize = 0;
211       sec->size = compressed_size;
212       sec->compress_status = COMPRESS_SECTION_NONE;
213       ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
214                                       0, compressed_size);
215       /* Restore rawsize and size.  */
216       sec->rawsize = rawsize;
217       sec->size = uncompressed_size;
218       sec->compress_status = DECOMPRESS_SECTION_SIZED;
219       if (!ret)
220         goto fail_compressed;
221
222       if (p == NULL)
223         p = (bfd_byte *) bfd_malloc (uncompressed_size);
224       if (p == NULL)
225         goto fail_compressed;
226
227       if (!decompress_contents (compressed_buffer, compressed_size,
228                                 p, uncompressed_size))
229         {
230           bfd_set_error (bfd_error_bad_value);
231           free (p);
232         fail_compressed:
233           free (compressed_buffer);
234           return FALSE;
235         }
236
237       free (compressed_buffer);
238       *ptr = p;
239       return TRUE;
240 #endif
241
242     case COMPRESS_SECTION_DONE:
243       if (p == NULL)
244         {
245           p = (bfd_byte *) bfd_malloc (sz);
246           if (p == NULL)
247             return FALSE;
248           *ptr = p;
249         }
250       memcpy (p, sec->contents, sz);
251       return TRUE;
252
253     default:
254       abort ();
255     }
256 }
257
258 /*
259 FUNCTION
260         bfd_is_section_compressed
261
262 SYNOPSIS
263         bfd_boolean bfd_is_section_compressed
264           (bfd *abfd, asection *section);
265
266 DESCRIPTION
267         Return @code{TRUE} if @var{section} is compressed.
268 */
269
270 bfd_boolean
271 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
272 {
273   bfd_byte compressed_buffer [12];
274
275   /* Read the zlib header.  In this case, it should be "ZLIB" followed
276      by the uncompressed section size, 8 bytes in big-endian order.  */
277   return (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
278           && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
279 }
280
281 /*
282 FUNCTION
283         bfd_init_section_decompress_status
284
285 SYNOPSIS
286         bfd_boolean bfd_init_section_decompress_status
287           (bfd *abfd, asection *section);
288
289 DESCRIPTION
290         Record compressed section size, update section size with
291         decompressed size and set compress_status to
292         DECOMPRESS_SECTION_SIZED.
293
294         Return @code{FALSE} if the section is not a valid compressed
295         section or zlib is not installed on this machine.  Otherwise,
296         return @code{TRUE}.
297 */
298
299 bfd_boolean
300 bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED,
301                                     sec_ptr sec ATTRIBUTE_UNUSED)
302 {
303 #ifndef HAVE_ZLIB_H
304   bfd_set_error (bfd_error_invalid_operation);
305   return FALSE;
306 #else
307   bfd_byte compressed_buffer [12];
308   bfd_size_type uncompressed_size;
309
310   if (sec->rawsize != 0
311       || sec->contents != NULL
312       || sec->compress_status != COMPRESS_SECTION_NONE
313       || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
314     {
315       bfd_set_error (bfd_error_invalid_operation);
316       return FALSE;
317     }
318
319   /* Read the zlib header.  In this case, it should be "ZLIB" followed
320      by the uncompressed section size, 8 bytes in big-endian order.  */
321   if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
322     {
323       bfd_set_error (bfd_error_wrong_format);
324       return FALSE;
325     }
326
327   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
328   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
329   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
330   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
331   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
332   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
333   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
334   uncompressed_size += compressed_buffer[11];
335
336   sec->compressed_size = sec->size;
337   sec->size = uncompressed_size;
338   sec->compress_status = DECOMPRESS_SECTION_SIZED;
339
340   return TRUE;
341 #endif
342 }
343
344 /*
345 FUNCTION
346         bfd_init_section_compress_status
347
348 SYNOPSIS
349         bfd_boolean bfd_init_section_compress_status
350           (bfd *abfd, asection *section);
351
352 DESCRIPTION
353         If open for read, compress section, update section size with
354         compressed size and set compress_status to COMPRESS_SECTION_DONE.
355
356         Return @code{FALSE} if the section is not a valid compressed
357         section or zlib is not installed on this machine.  Otherwise,
358         return @code{TRUE}.
359 */
360
361 bfd_boolean
362 bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
363                                   sec_ptr sec ATTRIBUTE_UNUSED)
364 {
365 #ifndef HAVE_ZLIB_H
366   bfd_set_error (bfd_error_invalid_operation);
367   return FALSE;
368 #else
369   bfd_size_type uncompressed_size;
370   bfd_byte *uncompressed_buffer;
371   bfd_boolean ret;
372
373   /* Error if not opened for read.  */
374   if (abfd->direction != read_direction
375       || sec->size == 0
376       || sec->rawsize != 0
377       || sec->contents != NULL
378       || sec->compress_status != COMPRESS_SECTION_NONE)
379     {
380       bfd_set_error (bfd_error_invalid_operation);
381       return FALSE;
382     }
383
384   /* Read in the full section contents and compress it.  */
385   uncompressed_size = sec->size;
386   uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
387   if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
388                                  0, uncompressed_size))
389     ret = FALSE;
390   else
391     ret = bfd_compress_section_contents (abfd, sec,
392                                          uncompressed_buffer,
393                                          uncompressed_size);
394
395   free (uncompressed_buffer);
396   return ret;
397 #endif
398 }