Skip to content

Commit

Permalink
Merge pull request #19 from mapbox/fix-casts
Browse files Browse the repository at this point in the history
Fix C++ style casts
  • Loading branch information
springmeyer authored Oct 16, 2017
2 parents 848af42 + 912a7f8 commit 76ec7b5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
9 changes: 5 additions & 4 deletions include/gzip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* gzip implements a standard namespace with a few available functions.
*/

#include "gzip/compress.hpp"
#include "gzip/decompress.hpp"
#include "gzip/utils.hpp"
#include "gzip/version.hpp"
#include <gzip/compress.hpp>
#include <gzip/config.hpp>
#include <gzip/decompress.hpp>
#include <gzip/utils.hpp>
#include <gzip/version.hpp>
8 changes: 5 additions & 3 deletions include/gzip/compress.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <iostream>
#include <gzip/config.hpp>

// zlib
#include <zlib.h>
// std
#include <limits>
#include <stdexcept>
#include <string>

namespace gzip {

Expand Down Expand Up @@ -31,7 +33,7 @@ std::string compress(const char* data,
{
throw std::runtime_error("deflate init failed");
}
deflate_s.next_in = (Bytef*)data;
deflate_s.next_in = reinterpret_cast<z_const Bytef*>(data);

#ifdef DEBUG
// Verify if size input will fit into unsigned int, type used for zlib's avail_in
Expand All @@ -57,7 +59,7 @@ std::string compress(const char* data,
// There is no way we see that "increase" would not fit in an unsigned int,
// hence we use static cast here to avoid -Wshorten-64-to-32 error
deflate_s.avail_out = static_cast<unsigned int>(increase);
deflate_s.next_out = (Bytef*)(output.data() + length);
deflate_s.next_out = reinterpret_cast<Bytef*>((&output[0] + length));
// From http://www.zlib.net/zlib_how.html
// "deflate() has a return value that can indicate errors, yet we do not check it here.
// Why not? Well, it turns out that deflate() can do no wrong here."
Expand Down
5 changes: 5 additions & 0 deletions include/gzip/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#ifndef ZLIB_CONST
#define ZLIB_CONST
#endif
7 changes: 5 additions & 2 deletions include/gzip/decompress.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <gzip/config.hpp>

// zlib
#include <zlib.h>
// std
#include <limits>
#include <stdexcept>
#include <string>

namespace gzip {

Expand All @@ -25,7 +28,7 @@ std::string decompress(const char* data, std::size_t size)
{
throw std::runtime_error("inflate init failed");
}
inflate_s.next_in = (Bytef*)data;
inflate_s.next_in = reinterpret_cast<z_const Bytef*>(data);

#ifdef DEBUG
// Verify if size (long type) input will fit into unsigned int, type used for zlib's avail_in
Expand All @@ -47,7 +50,7 @@ std::string decompress(const char* data, std::size_t size)
{
output.resize(length + 2 * size);
inflate_s.avail_out = static_cast<unsigned int>(2 * size);
inflate_s.next_out = (Bytef*)(output.data() + length);
inflate_s.next_out = reinterpret_cast<Bytef*>(&output[0] + length);
int ret = inflate(&inflate_s, Z_FINISH);
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR)
{
Expand Down
3 changes: 3 additions & 0 deletions include/gzip/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <cstdlib>

namespace gzip {

// These live in gzip.hpp because it doesnt need to use deps.
// Otherwise, they would need to live in impl files if these methods used
// zlib structures or functions like inflate/deflate)
Expand Down

0 comments on commit 76ec7b5

Please sign in to comment.