Skip to content

Commit

Permalink
Add #include <cstddef>, use std::size_t (not just size_t)
Browse files Browse the repository at this point in the history
We use `std::size_t` (and not the global `size_t`) because it is the
only variant that `<cstddef>` is guaranteed to provide, see
<https://developers.redhat.com/blog/2016/02/29/why-cstdlib-is-more-complicated-than-you-might-think>:

> You must not assume that `<cxxx>` adds any names to the global
> namespace, and you must not assume that `<xxx.h>` adds any names to
> namespace `std`.

So technically we could do it the other way around (`#include
<stddef.h>` and use `size_t` everywhere), but the `<xxx.h>` headers are
deprecated in C++ in favour of `<cxxx>`. Plus I think it's somewhat
beneficial to use the `std::` prefix, because it reminds us that it's a
type from the standard library, so it needs an `#include`.
  • Loading branch information
generalmimon committed Apr 30, 2024
1 parent d641d91 commit 7795aef
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions kaitai/kaitaistream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <algorithm> // std::reverse
#include <cerrno> // errno, ERANGE
#include <cstddef> // std::size_t
#include <cstdlib> // std::strtoll
#include <ios> // std::streamsize
#include <istream> // std::istream
Expand Down Expand Up @@ -415,7 +416,7 @@ std::string kaitai::kstream::read_bytes_full() {
std::istream::pos_type p1 = m_io->tellg();
m_io->seekg(0, std::istream::end);
std::istream::pos_type p2 = m_io->tellg();
size_t len = p2 - p1;
std::size_t len = p2 - p1;

// Note: this requires a std::string to be backed with a
// contiguous buffer. Officially, it's a only requirement since
Expand Down Expand Up @@ -486,22 +487,22 @@ std::string kaitai::kstream::bytes_terminate(std::string src, char term, bool in
// ========================================================================

std::string kaitai::kstream::process_xor_one(std::string data, uint8_t key) {
size_t len = data.length();
std::size_t len = data.length();
std::string result(len, ' ');

for (size_t i = 0; i < len; i++)
for (std::size_t i = 0; i < len; i++)
result[i] = data[i] ^ key;

return result;
}

std::string kaitai::kstream::process_xor_many(std::string data, std::string key) {
size_t len = data.length();
size_t kl = key.length();
std::size_t len = data.length();
std::size_t kl = key.length();
std::string result(len, ' ');

size_t ki = 0;
for (size_t i = 0; i < len; i++) {
std::size_t ki = 0;
for (std::size_t i = 0; i < len; i++) {
result[i] = data[i] ^ key[ki];
ki++;
if (ki >= kl)
Expand All @@ -512,10 +513,10 @@ std::string kaitai::kstream::process_xor_many(std::string data, std::string key)
}

std::string kaitai::kstream::process_rotate_left(std::string data, int amount) {
size_t len = data.length();
std::size_t len = data.length();
std::string result(len, ' ');

for (size_t i = 0; i < len; i++) {
for (std::size_t i = 0; i < len; i++) {
uint8_t bits = data[i];
result[i] = (bits << amount) | (bits >> (8 - amount));
}
Expand Down Expand Up @@ -670,27 +671,27 @@ std::string kaitai::kstream::bytes_to_str(const std::string src, const char *src
}
}

size_t src_len = src.length();
size_t src_left = src_len;
std::size_t src_len = src.length();
std::size_t src_left = src_len;

// Start with a buffer length of double the source length.
size_t dst_len = src_len * 2;
std::size_t dst_len = src_len * 2;
std::string dst(dst_len, ' ');
size_t dst_left = dst_len;
std::size_t dst_left = dst_len;

// NB: this should be const char *, but for some reason iconv() requires non-const in its 2nd argument,
// so we force it with a cast.
char *src_ptr = const_cast<char*>(src.data());
char *dst_ptr = &dst[0];

while (true) {
size_t res = iconv(cd, &src_ptr, &src_left, &dst_ptr, &dst_left);
std::size_t res = iconv(cd, &src_ptr, &src_left, &dst_ptr, &dst_left);

if (res == (size_t)-1) {
if (res == (std::size_t)-1) {
if (errno == E2BIG) {
// dst buffer is not enough to accomodate whole string
// enlarge the buffer and try again
size_t dst_used = dst_len - dst_left;
std::size_t dst_used = dst_len - dst_left;
dst_left += dst_len;
dst_len += dst_len;
dst.resize(dst_len);
Expand Down

0 comments on commit 7795aef

Please sign in to comment.