Skip to content

Commit

Permalink
remove some do while loops
Browse files Browse the repository at this point in the history
Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Dec 26, 2024
1 parent aa13844 commit 4a37b37
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/bmffimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ class BrotliDecoderWrapper {
void BmffImage::brotliUncompress(const byte* compressedBuf, size_t compressedBufSize, DataBuf& arr) {
BrotliDecoderWrapper decoder;
size_t uncompressedLen = compressedBufSize * 2; // just a starting point
BrotliDecoderResult result;
BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
int dos = 0;
size_t available_in = compressedBufSize;
const byte* next_in = compressedBuf;
size_t available_out;
byte* next_out;
size_t total_out = 0;

do {
while (result != BROTLI_DECODER_RESULT_SUCCESS) {
arr.alloc(uncompressedLen);
available_out = uncompressedLen - total_out;
next_out = arr.data() + total_out;
Expand All @@ -234,7 +234,7 @@ void BmffImage::brotliUncompress(const byte* compressedBuf, size_t compressedBuf
// something bad happened
throw Error(ErrorCode::kerErrorMessage, BrotliDecoderErrorString(BrotliDecoderGetErrorCode(decoder.get())));
}
} while (result != BROTLI_DECODER_RESULT_SUCCESS);
};

if (result != BROTLI_DECODER_RESULT_SUCCESS) {
throw Error(ErrorCode::kerFailedToReadImageData);
Expand Down
2 changes: 1 addition & 1 deletion src/image_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::ostream& operator<<(std::ostream& stream, const binaryToStringHelper<T>& bi
auto c = static_cast<int>(binToStr.buf_.at(i));
const bool bTrailingNull = c == 0 && i == binToStr.buf_.size() - 1;
if (!bTrailingNull) {
if (c < ' ' || c >= 127) {
if (std::iscntrl(c)) {
c = '.';
}
stream.put(static_cast<char>(c));
Expand Down
4 changes: 2 additions & 2 deletions src/minoltamn_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1674,15 +1674,15 @@ static std::vector<std::string> split(const std::string& str, const std::string&
std::vector<std::string> tokens;
size_t prev = 0;
size_t pos = 0;
do {
while (pos < str.length() && prev < str.length()) {
pos = str.find(delim, prev);
if (pos == std::string::npos)
pos = str.length();
std::string token = str.substr(prev, pos - prev);
if (!token.empty())
tokens.push_back(std::move(token));
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
};
return tokens;
}

Expand Down
12 changes: 6 additions & 6 deletions src/pngchunk_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ std::string PngChunk::makeMetadataChunk(const std::string& metadata, MetadataId

void PngChunk::zlibUncompress(const byte* compressedText, unsigned int compressedTextSize, DataBuf& arr) {
uLongf uncompressedLen = compressedTextSize * 2; // just a starting point
int zlibResult;
int zlibResult = Z_BUF_ERROR;
int dos = 0;

do {
while (zlibResult == Z_BUF_ERROR) {
arr.alloc(uncompressedLen);
zlibResult = uncompress(arr.data(), &uncompressedLen, compressedText, compressedTextSize);
if (zlibResult == Z_OK) {
Expand All @@ -355,7 +355,7 @@ void PngChunk::zlibUncompress(const byte* compressedText, unsigned int compresse
// something bad happened
throw Error(ErrorCode::kerFailedToReadImageData);
}
} while (zlibResult == Z_BUF_ERROR);
};

if (zlibResult != Z_OK) {
throw Error(ErrorCode::kerFailedToReadImageData);
Expand All @@ -364,10 +364,10 @@ void PngChunk::zlibUncompress(const byte* compressedText, unsigned int compresse

std::string PngChunk::zlibCompress(const std::string& text) {
auto compressedLen = static_cast<uLongf>(text.size() * 2); // just a starting point
int zlibResult;
int zlibResult = Z_BUF_ERROR;

DataBuf arr;
do {
while (zlibResult == Z_BUF_ERROR) {
arr.resize(compressedLen);
zlibResult = compress2(arr.data(), &compressedLen, reinterpret_cast<const Bytef*>(text.data()),
static_cast<uLong>(text.size()), Z_BEST_COMPRESSION);
Expand All @@ -390,7 +390,7 @@ std::string PngChunk::zlibCompress(const std::string& text) {
// Something bad happened
throw Error(ErrorCode::kerFailedToReadImageData);
}
} while (zlibResult == Z_BUF_ERROR);
};

return {arr.c_str(), arr.size()};

Expand Down
13 changes: 7 additions & 6 deletions src/pngimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static bool zlibToDataBuf(const byte* bytes, uLongf length, DataBuf& result) {
uLongf uncompressedLen = length; // just a starting point
int zlibResult = Z_BUF_ERROR;

do {
while (zlibResult == Z_BUF_ERROR) {
result.alloc(uncompressedLen);
zlibResult = uncompress(result.data(), &uncompressedLen, bytes, length);
// if result buffer is large than necessary, redo to fit perfectly.
Expand All @@ -95,7 +95,7 @@ static bool zlibToDataBuf(const byte* bytes, uLongf length, DataBuf& result) {
else
uncompressedLen *= 2;
}
} while (zlibResult == Z_BUF_ERROR);
};

return zlibResult == Z_OK;
}
Expand All @@ -104,7 +104,7 @@ static bool zlibToCompressed(const byte* bytes, uLongf length, DataBuf& result)
uLongf compressedLen = length; // just a starting point
int zlibResult = Z_BUF_ERROR;

do {
while (zlibResult == Z_BUF_ERROR) {
result.alloc(compressedLen);
zlibResult = compress(result.data(), &compressedLen, bytes, length);
if (zlibResult == Z_BUF_ERROR) {
Expand All @@ -116,7 +116,7 @@ static bool zlibToCompressed(const byte* bytes, uLongf length, DataBuf& result)
result.alloc(compressedLen);
zlibResult = compress(result.data(), &compressedLen, bytes, length);
}
} while (zlibResult == Z_BUF_ERROR);
};

return zlibResult == Z_OK;
}
Expand Down Expand Up @@ -443,9 +443,10 @@ void PngImage::readMetadata() {
} else if (chunkType == "iCCP") {
// The ICC profile name can vary from 1-79 characters.
uint32_t iccOffset = 0;
do {
while (chunkData.read_uint8(iccOffset) != 0x00) {
enforce(iccOffset < 80 && iccOffset < chunkLength, Exiv2::ErrorCode::kerCorruptedMetadata);
} while (chunkData.read_uint8(iccOffset++) != 0x00);
++iccOffset;
};

profileName_ = std::string(chunkData.c_str(), iccOffset - 1);
++iccOffset; // +1 = 'compressed' flag
Expand Down
5 changes: 3 additions & 2 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,12 @@ void hexdump(std::ostream& os, const byte* buf, size_t len, size_t offset) {
while (i < len) {
os << " " << std::setw(4) << std::setfill('0') << std::hex << i + offset << " ";
std::ostringstream ss;
do {
while (i < len && i % 16 != 0) {
byte c = buf[i];
os << std::setw(2) << std::setfill('0') << std::right << std::hex << static_cast<int>(c) << " ";
ss << (static_cast<int>(c) >= 31 && static_cast<int>(c) < 127 ? static_cast<char>(buf[i]) : '.');
} while (++i < len && i % 16 != 0);
++i;
};
std::string::size_type width = 9 + (((i - 1) % 16 + 1) * 3);
os << (width > pos ? "" : align.substr(width)) << ss.str() << "\n";
}
Expand Down

0 comments on commit 4a37b37

Please sign in to comment.