1*cf7f2e2dSJohn Marino /* Compressed section support (intended for debug sections). 2*cf7f2e2dSJohn Marino Copyright 2008, 2010 35796c8dcSSimon Schubert Free Software Foundation, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of BFD, the Binary File Descriptor library. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 105796c8dcSSimon Schubert (at your option) any later version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155796c8dcSSimon Schubert GNU General Public License for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 185796c8dcSSimon Schubert along with this program; if not, write to the Free Software 195796c8dcSSimon Schubert Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 205796c8dcSSimon Schubert MA 02110-1301, USA. */ 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "config.h" 235796c8dcSSimon Schubert #include "sysdep.h" 245796c8dcSSimon Schubert #include "bfd.h" 255796c8dcSSimon Schubert #include "libbfd.h" 265796c8dcSSimon Schubert #ifdef HAVE_ZLIB_H 275796c8dcSSimon Schubert #include <zlib.h> 285796c8dcSSimon Schubert #endif 295796c8dcSSimon Schubert 305796c8dcSSimon Schubert /* 315796c8dcSSimon Schubert FUNCTION 325796c8dcSSimon Schubert bfd_uncompress_section_contents 335796c8dcSSimon Schubert 345796c8dcSSimon Schubert SYNOPSIS 355796c8dcSSimon Schubert bfd_boolean bfd_uncompress_section_contents 365796c8dcSSimon Schubert (bfd_byte **buffer, bfd_size_type *size); 375796c8dcSSimon Schubert 385796c8dcSSimon Schubert DESCRIPTION 395796c8dcSSimon Schubert 405796c8dcSSimon Schubert Uncompresses a section that was compressed using zlib, in place. At 415796c8dcSSimon Schubert the call to this function, *@var{buffer} and *@var{size} should point 425796c8dcSSimon Schubert to the section contents to be uncompressed. At the end of the 435796c8dcSSimon Schubert function, *@var{buffer} and *@var{size} will point to the uncompressed 445796c8dcSSimon Schubert contents. This function assumes *BUFFER was allocated using 455796c8dcSSimon Schubert bfd_malloc() or equivalent. If the section is not a valid compressed 465796c8dcSSimon Schubert section, or zlib is not installed on this machine, the input is 475796c8dcSSimon Schubert unmodified. 485796c8dcSSimon Schubert 495796c8dcSSimon Schubert Returns @code{FALSE} if unable to uncompress successfully; in that case 505796c8dcSSimon Schubert the input is unmodified. Otherwise, returns @code{TRUE}. 515796c8dcSSimon Schubert */ 525796c8dcSSimon Schubert 535796c8dcSSimon Schubert bfd_boolean 54*cf7f2e2dSJohn Marino bfd_uncompress_section_contents (bfd_byte **buffer ATTRIBUTE_UNUSED, 55*cf7f2e2dSJohn Marino bfd_size_type *size ATTRIBUTE_UNUSED) 565796c8dcSSimon Schubert { 575796c8dcSSimon Schubert #ifndef HAVE_ZLIB_H 585796c8dcSSimon Schubert return FALSE; 595796c8dcSSimon Schubert #else 605796c8dcSSimon Schubert bfd_size_type compressed_size = *size; 615796c8dcSSimon Schubert bfd_byte *compressed_buffer = *buffer; 625796c8dcSSimon Schubert bfd_size_type uncompressed_size; 635796c8dcSSimon Schubert bfd_byte *uncompressed_buffer; 645796c8dcSSimon Schubert z_stream strm; 655796c8dcSSimon Schubert int rc; 665796c8dcSSimon Schubert bfd_size_type header_size = 12; 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert /* Read the zlib header. In this case, it should be "ZLIB" followed 695796c8dcSSimon Schubert by the uncompressed section size, 8 bytes in big-endian order. */ 705796c8dcSSimon Schubert if (compressed_size < header_size 715796c8dcSSimon Schubert || ! CONST_STRNEQ ((char*) compressed_buffer, "ZLIB")) 725796c8dcSSimon Schubert return FALSE; 735796c8dcSSimon Schubert uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8; 745796c8dcSSimon Schubert uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8; 755796c8dcSSimon Schubert uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8; 765796c8dcSSimon Schubert uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8; 775796c8dcSSimon Schubert uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8; 785796c8dcSSimon Schubert uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8; 795796c8dcSSimon Schubert uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8; 805796c8dcSSimon Schubert uncompressed_size += compressed_buffer[11]; 815796c8dcSSimon Schubert 825796c8dcSSimon Schubert /* It is possible the section consists of several compressed 835796c8dcSSimon Schubert buffers concatenated together, so we uncompress in a loop. */ 845796c8dcSSimon Schubert strm.zalloc = NULL; 855796c8dcSSimon Schubert strm.zfree = NULL; 865796c8dcSSimon Schubert strm.opaque = NULL; 875796c8dcSSimon Schubert strm.avail_in = compressed_size - header_size; 885796c8dcSSimon Schubert strm.next_in = (Bytef*) compressed_buffer + header_size; 895796c8dcSSimon Schubert strm.avail_out = uncompressed_size; 905796c8dcSSimon Schubert uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size); 915796c8dcSSimon Schubert if (! uncompressed_buffer) 925796c8dcSSimon Schubert return FALSE; 935796c8dcSSimon Schubert 945796c8dcSSimon Schubert rc = inflateInit (&strm); 955796c8dcSSimon Schubert while (strm.avail_in > 0) 965796c8dcSSimon Schubert { 975796c8dcSSimon Schubert if (rc != Z_OK) 985796c8dcSSimon Schubert goto fail; 995796c8dcSSimon Schubert strm.next_out = ((Bytef*) uncompressed_buffer 1005796c8dcSSimon Schubert + (uncompressed_size - strm.avail_out)); 1015796c8dcSSimon Schubert rc = inflate (&strm, Z_FINISH); 1025796c8dcSSimon Schubert if (rc != Z_STREAM_END) 1035796c8dcSSimon Schubert goto fail; 1045796c8dcSSimon Schubert rc = inflateReset (&strm); 1055796c8dcSSimon Schubert } 1065796c8dcSSimon Schubert rc = inflateEnd (&strm); 1075796c8dcSSimon Schubert if (rc != Z_OK 1085796c8dcSSimon Schubert || strm.avail_out != 0) 1095796c8dcSSimon Schubert goto fail; 1105796c8dcSSimon Schubert 1115796c8dcSSimon Schubert free (compressed_buffer); 1125796c8dcSSimon Schubert *buffer = uncompressed_buffer; 1135796c8dcSSimon Schubert *size = uncompressed_size; 1145796c8dcSSimon Schubert return TRUE; 1155796c8dcSSimon Schubert 1165796c8dcSSimon Schubert fail: 1175796c8dcSSimon Schubert free (uncompressed_buffer); 1185796c8dcSSimon Schubert return FALSE; 1195796c8dcSSimon Schubert #endif /* HAVE_ZLIB_H */ 1205796c8dcSSimon Schubert } 121