1 /* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <zlib/zlib.h>
24 std::string
shared::replace_extension(const std::string
& filename
, const std::string
& extension
)
27 const char* name
= filename
.c_str();
28 const char* ext
= strrchr(name
, '.');
31 // If an extension was found, replace it.
32 newname
.assign(name
, ext
-name
);
37 // If an extension was not found, append it.
44 std::string
shared::remove_extension(const std::string
& filename
)
47 const char* name
= filename
.c_str();
48 const char* ext
= strrchr(name
, '.');
51 newname
.assign(name
, ext
-name
);
60 std::string
shared::double_quote(const std::string
& str
)
63 for (int i
= 0, j
= str
.length(); i
< j
; i
++)
73 bool shared::inflate_to_file(const bytes
& buffer
, const char* filename
)
77 ofs
.open(filename
, std::ios::binary
);
84 z_stream d_stream
; // decompression stream
86 d_stream
.zalloc
= Z_NULL
;
87 d_stream
.zfree
= Z_NULL
;
88 d_stream
.opaque
= Z_NULL
;
90 d_stream
.next_in
= const_cast<bytes::value_type
*>(&buffer
[0]);
91 d_stream
.avail_in
= static_cast<uInt
>(buffer
.size());
93 int ret
= inflateInit(&d_stream
);
97 // Allocate buffer to hold the inflated data.
98 const size_t BUFSIZE
= 1048576;
99 Bytef
* infbuf
= new Bytef
[BUFSIZE
];
103 // Decompress untill the end of the input buffer.
108 d_stream
.next_out
= infbuf
;
109 d_stream
.avail_out
= BUFSIZE
;
111 ret
= inflate(&d_stream
, Z_NO_FLUSH
);
112 if (ret
== Z_STREAM_END
)
116 else if (ret
!= Z_OK
)
118 inflateEnd(&d_stream
);
123 // Write the inflated data to the output file.
124 if (!ofs
.write((const char*)infbuf
, d_stream
.total_out
-totalout
))
126 inflateEnd(&d_stream
);
130 totalout
= d_stream
.total_out
;
133 // Cleanup and return.
134 inflateEnd(&d_stream
);
140 bool shared::deflate_to_file(const bytes
& buffer
, const char* filename
)
144 ofs
.open(filename
, std::ios::binary
);
151 z_stream c_stream
; // compression stream.
153 c_stream
.zalloc
= Z_NULL
;
154 c_stream
.zfree
= Z_NULL
;
155 c_stream
.opaque
= Z_NULL
;
157 int ret
= deflateInit(&c_stream
, Z_BEST_COMPRESSION
);
161 // Allocate buffer to hold the deflated data.
162 const size_t BUFSIZE
= 1048576;
163 Bytef
* defbuf
= new Bytef
[BUFSIZE
];
167 c_stream
.avail_in
= static_cast<uInt
>(buffer
.size());
168 c_stream
.next_in
= const_cast<bytes::value_type
*>(&buffer
[0]);
170 // Compress until end of the buffer.
175 c_stream
.avail_out
= BUFSIZE
;
176 c_stream
.next_out
= defbuf
;
178 ret
= deflate(&c_stream
, Z_NO_FLUSH
); // no bad return value
179 if (ret
== Z_STREAM_END
)
183 else if (ret
== Z_BUF_ERROR
&& !c_stream
.avail_in
)
185 ret
= deflate(&c_stream
, Z_FINISH
); // no bad return value
188 else if (ret
!= Z_OK
)
190 deflateEnd(&c_stream
);
195 // Write the inflated data to the output file.
196 if (!ofs
.write((const char*)defbuf
, c_stream
.total_out
-totalout
))
198 deflateEnd(&c_stream
);
203 totalout
= c_stream
.total_out
;
206 // Clean up and return.
207 deflateEnd(&c_stream
);