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