Merge branch 'vendor/OPENSSH'
[dragonfly.git] / contrib / binutils-2.21 / 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 (compress ((Bytef*) compressed_buffer + 12,
104                 &compressed_size,
105                 (const Bytef*) uncompressed_buffer,
106                 uncompressed_size) != Z_OK)
107     {
108       free (compressed_buffer);
109       bfd_set_error (bfd_error_bad_value);
110       return FALSE;
111     }
112
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;
125
126   /* Free the uncompressed contents if we compress in place.  */
127   if (uncompressed_buffer == sec->contents)
128     free (uncompressed_buffer);
129
130   sec->contents = compressed_buffer;
131   sec->size = compressed_size;
132   sec->compress_status = COMPRESS_SECTION_DONE;
133
134   return TRUE;
135 #endif  /* HAVE_ZLIB_H */
136 }
137
138 /*
139 FUNCTION
140         bfd_get_full_section_contents
141
142 SYNOPSIS
143         bfd_boolean bfd_get_full_section_contents
144           (bfd *abfd, asection *section, bfd_byte **ptr);
145
146 DESCRIPTION
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.  
150
151         Return @code{TRUE} if the full section contents is retrieved
152         successfully.
153 */
154
155 bfd_boolean
156 bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
157 {
158   bfd_size_type sz = sec->rawsize ? sec->rawsize : sec->size;
159   bfd_byte *p = *ptr;
160 #ifdef HAVE_ZLIB_H
161   bfd_boolean 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;
167 #endif
168
169   if (sz == 0)
170     return TRUE;
171
172   switch (sec->compress_status)
173     {
174     case COMPRESS_SECTION_NONE:
175       if (p == NULL)
176         {
177           p = (bfd_byte *) bfd_malloc (sz);
178           if (p == NULL)
179             return FALSE;
180         }
181       if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
182         {
183           if (*ptr != p)
184             free (p);
185           return FALSE;
186         }
187       *ptr = p;
188       return TRUE;
189
190     case DECOMPRESS_SECTION_SIZED:
191 #ifndef HAVE_ZLIB_H
192       bfd_set_error (bfd_error_invalid_operation);
193       return FALSE;
194 #else
195       /* Read in the full compressed section contents.  */
196       uncompressed_size = sec->size;
197       compressed_size = sec->compressed_size;
198       compressed_buffer = (bfd_byte *) bfd_malloc (compressed_size);
199       if (compressed_buffer == NULL)
200         return FALSE;
201       rawsize = sec->rawsize;
202       /* Clear rawsize, set size to compressed size and set compress_status
203          to COMPRESS_SECTION_NONE.  If the compressed size is bigger than
204          the uncompressed size, bfd_get_section_contents will fail.  */
205       sec->rawsize = 0;
206       sec->size = compressed_size;
207       sec->compress_status = COMPRESS_SECTION_NONE;
208       ret = bfd_get_section_contents (abfd, sec, compressed_buffer,
209                                       0, compressed_size);
210       /* Restore rawsize and size.  */
211       sec->rawsize = rawsize;
212       sec->size = uncompressed_size;
213       sec->compress_status = DECOMPRESS_SECTION_SIZED;
214       if (!ret)
215         goto fail_compressed;
216
217       uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
218       if (uncompressed_buffer == NULL)
219         goto fail_compressed;
220
221       if (!decompress_contents (compressed_buffer, compressed_size,
222                                 uncompressed_buffer, uncompressed_size))
223         {
224           bfd_set_error (bfd_error_bad_value);
225           free (uncompressed_buffer);
226         fail_compressed:
227           free (compressed_buffer);
228           return FALSE;
229         }
230
231       free (compressed_buffer);
232       sec->contents = uncompressed_buffer;
233       sec->compress_status = COMPRESS_SECTION_DONE;
234       /* Fall thru */
235 #endif
236
237     case COMPRESS_SECTION_DONE:
238       if (p == NULL)
239         {
240           p = (bfd_byte *) bfd_malloc (sz);
241           if (p == NULL)
242             return FALSE;
243           *ptr = p;
244         }
245       memcpy (p, sec->contents, sz);
246       return TRUE;
247
248     default:
249       abort ();
250     }
251 }
252
253 /*
254 FUNCTION
255         bfd_is_section_compressed
256
257 SYNOPSIS
258         bfd_boolean bfd_is_section_compressed
259           (bfd *abfd, asection *section);
260
261 DESCRIPTION
262         Return @code{TRUE} if @var{section} is compressed.
263 */
264
265 bfd_boolean
266 bfd_is_section_compressed (bfd *abfd, sec_ptr sec)
267 {
268   bfd_byte compressed_buffer [12];
269
270   /* Read the zlib header.  In this case, it should be "ZLIB" followed
271      by the uncompressed section size, 8 bytes in big-endian order.  */
272   return (bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12)
273           && CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"));
274 }
275
276 /*
277 FUNCTION
278         bfd_init_section_decompress_status
279
280 SYNOPSIS
281         bfd_boolean bfd_init_section_decompress_status
282           (bfd *abfd, asection *section);
283
284 DESCRIPTION
285         Record compressed section size, update section size with
286         decompressed size and set compress_status to
287         DECOMPRESS_SECTION_SIZED.
288
289         Return @code{FALSE} if the section is not a valid compressed
290         section or zlib is not installed on this machine.  Otherwise,
291         return @code{TRUE}.
292 */
293
294 bfd_boolean
295 bfd_init_section_decompress_status (bfd *abfd ATTRIBUTE_UNUSED,
296                                     sec_ptr sec ATTRIBUTE_UNUSED)
297 {
298 #ifndef HAVE_ZLIB_H
299   bfd_set_error (bfd_error_invalid_operation);
300   return FALSE;
301 #else
302   bfd_byte compressed_buffer [12];
303   bfd_size_type uncompressed_size;
304
305   if (sec->rawsize != 0
306       || sec->contents != NULL
307       || sec->compress_status != COMPRESS_SECTION_NONE
308       || !bfd_get_section_contents (abfd, sec, compressed_buffer, 0, 12))
309     {
310       bfd_set_error (bfd_error_invalid_operation);
311       return FALSE;
312     }
313
314   /* Read the zlib header.  In this case, it should be "ZLIB" followed
315      by the uncompressed section size, 8 bytes in big-endian order.  */
316   if (! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB"))
317     {
318       bfd_set_error (bfd_error_wrong_format);
319       return FALSE;
320     }
321
322   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
323   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
324   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
325   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
326   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
327   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
328   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
329   uncompressed_size += compressed_buffer[11];
330
331   sec->compressed_size = sec->size;
332   sec->size = uncompressed_size;
333   sec->compress_status = DECOMPRESS_SECTION_SIZED;
334
335   return TRUE;
336 #endif
337 }
338
339 /*
340 FUNCTION
341         bfd_init_section_compress_status
342
343 SYNOPSIS
344         bfd_boolean bfd_init_section_compress_status
345           (bfd *abfd, asection *section);
346
347 DESCRIPTION
348         If open for read, compress section, update section size with
349         compressed size and set compress_status to COMPRESS_SECTION_DONE.
350
351         Return @code{FALSE} if the section is not a valid compressed
352         section or zlib is not installed on this machine.  Otherwise,
353         return @code{TRUE}.
354 */
355
356 bfd_boolean
357 bfd_init_section_compress_status (bfd *abfd ATTRIBUTE_UNUSED,
358                                   sec_ptr sec ATTRIBUTE_UNUSED)
359 {
360 #ifndef HAVE_ZLIB_H
361   bfd_set_error (bfd_error_invalid_operation);
362   return FALSE;
363 #else
364   bfd_size_type uncompressed_size;
365   bfd_byte *uncompressed_buffer;
366   bfd_boolean ret;
367
368   /* Error if not opened for read.  */
369   if (abfd->direction != read_direction
370       || sec->size == 0
371       || sec->rawsize != 0
372       || sec->contents != NULL
373       || sec->compress_status != COMPRESS_SECTION_NONE)
374     {
375       bfd_set_error (bfd_error_invalid_operation);
376       return FALSE;
377     }
378
379   /* Read in the full section contents and compress it.  */
380   uncompressed_size = sec->size;
381   uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
382   if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
383                                  0, uncompressed_size))
384     ret = FALSE;
385   else
386     ret = bfd_compress_section_contents (abfd, sec,
387                                          uncompressed_buffer,
388                                          uncompressed_size);
389
390   free (uncompressed_buffer);
391   return ret;
392 #endif
393 }