Skip to content

Commit

Permalink
fix pedantic warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tfussell committed Sep 25, 2014
1 parent bb3f47d commit 48928d9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 74 deletions.
29 changes: 16 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
CC = clang
CXX = clang++

INCLUDE = -I../
CFLAGS = -ggdb $(INCLUDE) -std=c11 -Werror -Wall -Wextra -pedantic
CPPFLAGS = -ggdb $(INCLUDE) -std=c++11 -Werror -Wall -Wextra -pedantic
WARN = -Werror -Wall -Wextra -pedantic -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded
DEBUG = -g
CFLAGS = $(DEBUG) -std=c11
CPPFLAGS = $(DEBUG) -std=c++1y $(WARN)

.PHONY: all clean

all: test pipe read write

test: test.o zip_file.o miniz.o
g++ $(CPPFLAGS) -o test test.o zip_file.o miniz.o
$(CXX) $(CPPFLAGS) -o test test.o zip_file.o miniz.o

pipe: pipe.o zip_file.o miniz.o
g++ $(CPPFLAGS) -o pipe pipe.o zip_file.o miniz.o
$(CXX) $(CPPFLAGS) -o pipe pipe.o zip_file.o miniz.o

read: read.o zip_file.o miniz.o
g++ $(CPPFLAGS) -o read read.o zip_file.o miniz.o
$(CXX) $(CPPFLAGS) -o read read.o zip_file.o miniz.o

write: write.o zip_file.o miniz.o
g++ $(CPPFLAGS) -o write write.o zip_file.o miniz.o
$(CXX) $(CPPFLAGS) -o write write.o zip_file.o miniz.o

test.o: test.cpp
g++ $(CPPFLAGS) -o test.o -c test.cpp
$(CXX) $(CPPFLAGS) -o test.o -c test.cpp

pipe.o: pipe.cpp
g++ $(CPPFLAGS) -o pipe.o -c pipe.cpp
$(CXX) $(CPPFLAGS) -o pipe.o -c pipe.cpp

read.o: read.cpp
g++ $(CPPFLAGS) -o read.o -c read.cpp
$(CXX) $(CPPFLAGS) -o read.o -c read.cpp

write.o: write.cpp
g++ $(CPPFLAGS) -o write.o -c write.cpp
$(CXX) $(CPPFLAGS) -o write.o -c write.cpp

zip_file.o: zip_file.cpp
g++ $(CPPFLAGS) -o zip_file.o -c zip_file.cpp
$(CXX) $(CPPFLAGS) -o zip_file.o -c zip_file.cpp

miniz.o: miniz.c
gcc $(CFLAGS) -o miniz.o -c miniz.c
$(CC) $(CFLAGS) -o miniz.o -c miniz.c

clean:
@rm -f *.o *~ test pipe read write
4 changes: 4 additions & 0 deletions pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "zip_file.hpp"

namespace {

// Returns true if program is being run directly
// If this is false, we assume that input will be given through stdin
bool is_tty()
Expand All @@ -21,6 +23,8 @@ bool is_tty()
#endif
}

} // namespace

// Either 1. Print directory structure of zip file given as filename or piped through stdin
// or 2. Print contents of file contained in aforementioned zip file
int main(int argc, char *argv[])
Expand Down
2 changes: 1 addition & 1 deletion read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ int main(int argc, char *argv[])
file.printdir();

return 0;
}
}
45 changes: 20 additions & 25 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#define ZIP_TEST

#include <array>
#include <cassert>
#include <fstream>

#include "zip_file.hpp"

const unsigned char test_xlsx[] = {
namespace {

static const unsigned char test_xlsx[] = {
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x08, 0x00, 0x79, 0x9a,
0xff, 0x44, 0x9d, 0x9e, 0xa3, 0xe3, 0xe4, 0x00, 0x00, 0x00, 0x4b, 0x02,
0x00, 0x00, 0x0b, 0x00, 0x14, 0x00, 0x5f, 0x72, 0x65, 0x6c, 0x73, 0x2f,
Expand Down Expand Up @@ -41008,25 +41009,17 @@ const unsigned char test_xlsx[] = {
0x06, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x0d, 0x03, 0x00,
0x00, 0xc4, 0x7e, 0x07, 0x00, 0x00, 0x00
};
unsigned int test_xlsx_len = 492007;
static const unsigned int test_xlsx_len = 492007;

const std::string existing_file = "test.xlsx";
const std::string temp_file = "temp.zip";
const std::string expected_content_types_string = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Types xmlns=\"https://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"xml\" ContentType=\"application/xml\"/><Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"jpeg\" ContentType=\"image/jpg\"/><Default Extension=\"png\" ContentType=\"image/png\"/><Default Extension=\"bmp\" ContentType=\"image/bmp\"/><Default Extension=\"gif\" ContentType=\"image/gif\"/><Default Extension=\"tif\" ContentType=\"image/tif\"/><Default Extension=\"pdf\" ContentType=\"application/pdf\"/><Default Extension=\"mov\" ContentType=\"application/movie\"/><Default Extension=\"vml\" ContentType=\"application/vnd.openxmlformats-officedocument.vmlDrawing\"/><Default Extension=\"xlsx\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\"/><Override PartName=\"/docProps/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/><Override PartName=\"/docProps/app.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.extended-properties+xml\"/><Override PartName=\"/xl/workbook.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml\"/><Override PartName=\"/xl/sharedStrings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml\"/><Override PartName=\"/xl/styles.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml\"/><Override PartName=\"/xl/theme/theme1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.theme+xml\"/><Override PartName=\"/xl/worksheets/sheet1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/></Types>\n";
const std::string expected_atxt_string = "aaa\r\nbbb\r\nccc\n\n\n";
const std::string expected_printdir_string = " Length Date Time Name\n--------- ---------- ----- ----\n 587 07/31/2014 19:19 _rels/.rels\n 299 07/31/2014 19:19 docProps/core.xml\n 231 07/31/2014 19:19 docProps/app.xml\n 415 07/31/2014 19:19 xl/workbook.xml\n 697 07/31/2014 19:19 xl/_rels/workbook.xml.rels\n 26038 07/31/2014 19:19 xl/theme/theme1.xml\n 291 07/31/2014 19:19 xl/theme/_rels/theme1.xml.rels\n 6415 07/31/2014 19:19 xl/worksheets/sheet1.xml\n 223 07/31/2014 19:19 xl/sharedStrings.xml\n 3188 07/31/2014 19:19 xl/styles.xml\n 489200 07/31/2014 19:19 xl/media/image1.png\n 1736 07/31/2014 19:19 [Content_Types].xml\n--------- -------\n 529320 12 files\n";
static const char *existing_file = "test.xlsx";
static const char *temp_file = "temp.zip";
static const char *expected_content_types_string = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Types xmlns=\"https://schemas.openxmlformats.org/package/2006/content-types\"><Default Extension=\"xml\" ContentType=\"application/xml\"/><Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/><Default Extension=\"jpeg\" ContentType=\"image/jpg\"/><Default Extension=\"png\" ContentType=\"image/png\"/><Default Extension=\"bmp\" ContentType=\"image/bmp\"/><Default Extension=\"gif\" ContentType=\"image/gif\"/><Default Extension=\"tif\" ContentType=\"image/tif\"/><Default Extension=\"pdf\" ContentType=\"application/pdf\"/><Default Extension=\"mov\" ContentType=\"application/movie\"/><Default Extension=\"vml\" ContentType=\"application/vnd.openxmlformats-officedocument.vmlDrawing\"/><Default Extension=\"xlsx\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\"/><Override PartName=\"/docProps/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/><Override PartName=\"/docProps/app.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.extended-properties+xml\"/><Override PartName=\"/xl/workbook.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml\"/><Override PartName=\"/xl/sharedStrings.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml\"/><Override PartName=\"/xl/styles.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml\"/><Override PartName=\"/xl/theme/theme1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.theme+xml\"/><Override PartName=\"/xl/worksheets/sheet1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml\"/></Types>\n";
static const char *expected_atxt_string = "aaa\r\nbbb\r\nccc\n\n\n";
static const char *expected_printdir_string = " Length Date Time Name\n--------- ---------- ----- ----\n 587 07/31/2014 19:19 _rels/.rels\n 299 07/31/2014 19:19 docProps/core.xml\n 231 07/31/2014 19:19 docProps/app.xml\n 415 07/31/2014 19:19 xl/workbook.xml\n 697 07/31/2014 19:19 xl/_rels/workbook.xml.rels\n 26038 07/31/2014 19:19 xl/theme/theme1.xml\n 291 07/31/2014 19:19 xl/theme/_rels/theme1.xml.rels\n 6415 07/31/2014 19:19 xl/worksheets/sheet1.xml\n 223 07/31/2014 19:19 xl/sharedStrings.xml\n 3188 07/31/2014 19:19 xl/styles.xml\n 489200 07/31/2014 19:19 xl/media/image1.png\n 1736 07/31/2014 19:19 [Content_Types].xml\n--------- -------\n 529320 12 files\n";

void remove_temp_file()
{
std::remove(temp_file.c_str());
}

void make_temp_directory()
{
}

void remove_temp_directory()
{
std::remove(temp_file);
}

bool files_equal(const std::string &a, const std::string &b)
Expand Down Expand Up @@ -41080,7 +41073,7 @@ void test_load_bytes()
std::ifstream in_stream(existing_file, std::ios::binary);
while(in_stream)
{
source_bytes.push_back((unsigned char)in_stream.get());
source_bytes.push_back(static_cast<unsigned char>(in_stream.get()));
}
f.load(source_bytes);
f.save(temp_file);
Expand Down Expand Up @@ -41127,7 +41120,7 @@ void test_reset()
void test_getinfo()
{
zip_file f(existing_file);
auto info = f.getinfo("[Content_Types].xml");
zip_info info = f.getinfo("[Content_Types].xml");
assert(info.filename == "[Content_Types].xml");
}

Expand Down Expand Up @@ -41196,7 +41189,7 @@ void test_printdir()
zip_file f(existing_file);
std::stringstream ss;
f.printdir(ss);
auto printed = ss.str();
std::string printed = ss.str();
assert(printed == expected_printdir_string);
}

Expand Down Expand Up @@ -41264,14 +41257,16 @@ void test_comment()
void write_existing()
{
std::ofstream stream(existing_file, std::ios::binary);
stream.write((const char *)test_xlsx, sizeof(test_xlsx));
std::array<char, test_xlsx_len> test_xlsx_chars = {{0}};
std::copy(test_xlsx, test_xlsx + test_xlsx_len, test_xlsx_chars.begin());
stream.write(test_xlsx_chars.data(), test_xlsx_chars.size());
std::ofstream stream2("a.txt", std::ios::binary);
stream2 << expected_atxt_string;
}

void remove_existing()
{
std::remove(existing_file.c_str());
std::remove(existing_file);
std::remove("a.txt");
}

Expand Down Expand Up @@ -41302,11 +41297,11 @@ void test_zip()
remove_existing();
}

#ifdef ZIP_TEST
} // namespace

int main()
{
test_zip();
std::cout << "all tests passed" << std::endl;
return 0;
}
#endif
2 changes: 1 addition & 1 deletion write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ int main(int argc, char *argv[])
file.save(argv[1]);

return 0;
}
}
69 changes: 35 additions & 34 deletions zip_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ uint32_t crc32buf(const char *buf, std::size_t len)
};

#define UPDC32(octet,crc) (crc_32_tab[((crc)\
^ ((uint8_t)octet)) & 0xff] ^ ((crc) >> 8))
^ static_cast<uint8_t>(octet)) & 0xff] ^ ((crc) >> 8))

for ( ; len; --len, ++buf)
{
Expand All @@ -158,6 +158,24 @@ tm safe_localtime(const time_t &t)
#endif
}

std::size_t write_callback(void *opaque, mz_uint64 file_ofs, const void *pBuf, std::size_t n)
{
auto buffer = static_cast<std::vector<char> *>(opaque);

if(file_ofs + n > buffer->size())
{
auto new_size = static_cast<std::vector<char>::size_type>(file_ofs + n);
buffer->resize(new_size);
}

for(std::size_t i = 0; i < n; i++)
{
(*buffer)[static_cast<std::size_t>(file_ofs + i)] = (static_cast<const char *>(pBuf))[i];
}

return n;
}

} // namespace

zip_info::zip_info()
Expand Down Expand Up @@ -254,7 +272,7 @@ void zip_file::save(std::ostream &stream)
}

append_comment();
stream.write(buffer_.data(), buffer_.size());
stream.write(buffer_.data(), static_cast<long>(buffer_.size()));
}

void zip_file::save(std::vector<unsigned char> &bytes)
Expand Down Expand Up @@ -282,9 +300,9 @@ void zip_file::append_comment()
{
if(!comment.empty())
{
auto comment_length = std::min((uint16_t)comment.length(), std::numeric_limits<uint16_t>::max());
buffer_[buffer_.size() - 2] = comment_length & 0xFF;
buffer_[buffer_.size() - 1] = comment_length >> 8 & 0xFF;
auto comment_length = std::min(static_cast<uint16_t>(comment.length()), std::numeric_limits<uint16_t>::max());
buffer_[buffer_.size() - 2] = static_cast<char>(comment_length);
buffer_[buffer_.size() - 1] = static_cast<char>(comment_length >> 8);
std::copy(comment.begin(), comment.end(), std::back_inserter(buffer_));
}
}
Expand Down Expand Up @@ -312,8 +330,8 @@ void zip_file::remove_comment()
throw std::runtime_error("didn't find end of central directory signature");
}

uint16_t length = buffer_[position + 1];
length = (length << 8) + buffer_[position];
uint16_t length = static_cast<uint16_t>(buffer_[position + 1]);
length = static_cast<uint16_t>(length << 8) + static_cast<uint16_t>(buffer_[position]);
position += 2;

if(length != 0)
Expand Down Expand Up @@ -381,15 +399,15 @@ zip_info zip_file::getinfo(int index)
}

mz_zip_archive_file_stat stat;
mz_zip_reader_file_stat(archive_.get(), index, &stat);
mz_zip_reader_file_stat(archive_.get(), static_cast<mz_uint>(index), &stat);

zip_info result;

result.filename = std::string(stat.m_filename, stat.m_filename + std::strlen(stat.m_filename));
result.comment = std::string(stat.m_comment, stat.m_comment + stat.m_comment_size);
result.compress_size = (std::size_t)stat.m_comp_size;
result.file_size = (std::size_t)stat.m_uncomp_size;
result.header_offset = (std::size_t)stat.m_local_header_ofs;
result.compress_size = static_cast<std::size_t>(stat.m_comp_size);
result.file_size = static_cast<std::size_t>(stat.m_uncomp_size);
result.header_offset = static_cast<std::size_t>(stat.m_local_header_ofs);
result.crc = stat.m_crc32;
auto time = safe_localtime(stat.m_time);
result.date_time.year = 1900 + time.tm_year;
Expand Down Expand Up @@ -429,24 +447,6 @@ void zip_file::start_read()
}
}

std::size_t write_callback(void *opaque, mz_uint64 file_ofs, const void *pBuf, std::size_t n)
{
auto buffer = (std::vector<char> *)opaque;

if(file_ofs + n > buffer->size())
{
auto new_size = (std::vector<char>::size_type)(file_ofs + n);
buffer->resize(new_size);
}

for(std::size_t i = 0; i < n; i++)
{
(*buffer)[(std::size_t)file_ofs + i] = ((const char *)pBuf)[i];
}

return n;
}

void zip_file::start_write()
{
if(archive_->m_zip_mode == MZ_ZIP_MODE_WRITING) return;
Expand Down Expand Up @@ -475,7 +475,7 @@ void zip_file::start_write()
throw std::runtime_error("bad zip");
}

for(int i = 0; i < (int)archive_copy.m_total_files; i++)
for(unsigned int i = 0; i < static_cast<unsigned int>(archive_copy.m_total_files); i++)
{
if(!mz_zip_writer_add_from_zip_reader(archive_.get(), &archive_copy, i))
{
Expand All @@ -489,7 +489,8 @@ void zip_file::start_write()
case MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED:
mz_zip_writer_end(archive_.get());
break;
default:
case MZ_ZIP_MODE_INVALID:
case MZ_ZIP_MODE_WRITING:
break;
}

Expand Down Expand Up @@ -550,7 +551,7 @@ void zip_file::writestr(const zip_info &info, const std::string &bytes)

auto crc = crc32buf(bytes.c_str(), bytes.size());

if(!mz_zip_writer_add_mem_ex(archive_.get(), info.filename.c_str(), bytes.data(), bytes.size(), info.comment.c_str(), (mz_uint16)info.comment.size(), MZ_BEST_COMPRESSION, 0, crc))
if(!mz_zip_writer_add_mem_ex(archive_.get(), info.filename.c_str(), bytes.data(), bytes.size(), info.comment.c_str(), static_cast<mz_uint16>(info.comment.size()), MZ_BEST_COMPRESSION, 0, crc))
{
throw std::runtime_error("write error");
}
Expand All @@ -559,7 +560,7 @@ void zip_file::writestr(const zip_info &info, const std::string &bytes)
std::string zip_file::read(const zip_info &info)
{
std::size_t size;
char *data = (char *)mz_zip_reader_extract_file_to_heap(archive_.get(), info.filename.c_str(), &size, 0);
char *data = static_cast<char *>(mz_zip_reader_extract_file_to_heap(archive_.get(), info.filename.c_str(), &size, 0));
if(data == nullptr)
{
throw std::runtime_error("file couldn't be read");
Expand Down Expand Up @@ -602,7 +603,7 @@ std::vector<zip_info> zip_file::infolist()

for(std::size_t i = 0; i < mz_zip_reader_get_num_files(archive_.get()); i++)
{
info.push_back(getinfo((int)i));
info.push_back(getinfo(static_cast<int>(i)));
}

return info;
Expand Down

0 comments on commit 48928d9

Please sign in to comment.