Skip to content

Commit

Permalink
Rename hton/ntoh methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jclehner committed Jun 28, 2023
1 parent e94e757 commit 2ea96e8
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 120 deletions.
2 changes: 1 addition & 1 deletion bcm2boltenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ int main(int argc, char* argv[])
throw user_error("unknown variable type 0x" + to_hex(var.type));
}

size_t size = (var.type == boltenv_var::TYPE_VAR) ? (var.var.size - 1): ntoh(var.block_size_be);
size_t size = (var.type == boltenv_var::TYPE_VAR) ? (var.var.size - 1): be_to_h(var.block_size_be);

string raw(size, '\0');
if (in.readsome(&raw[0], size) != size) {
Expand Down
8 changes: 4 additions & 4 deletions bcm2dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ bool run_script_command(vector<string> args, sp<rwx> rwx)
uint32_t result;

if (width == 4) {
result = ntoh(extract<uint32_t>(buf));
result = be_to_h(extract<uint32_t>(buf));
} else if (width == 2) {
result = ntoh(extract<uint16_t>(buf));
result = be_to_h(extract<uint16_t>(buf));
} else {
result = buf[0];
}
Expand All @@ -257,9 +257,9 @@ bool run_script_command(vector<string> args, sp<rwx> rwx)
string buf;

if (width == 4) {
buf = to_buf(hton(value));
buf = to_buf(h_to_be(value));
} else if (width == 2) {
buf = to_buf(hton(uint16_t(value & 0xffff)));
buf = to_buf(h_to_be(uint16_t(value & 0xffff)));
} else {
buf += char(value & 0xff);
}
Expand Down
8 changes: 4 additions & 4 deletions gwsettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ string gws_decrypt(string buf, string& checksum, string& key, const csp<profile>
logger::d() << "decrypting with profile " << p->name() << endl;

if (flags & BCM2_CFG_FMT_GWS_LEN_PREFIX) {
auto len = ntoh(extract<uint32_t>(checksum));
auto len = be_to_h(extract<uint32_t>(checksum));
if (len == (buf.size() + 12)) {
checksum.erase(0, 4);
checksum.append(buf.substr(0, 4));
Expand Down Expand Up @@ -246,7 +246,7 @@ string gws_encrypt(string buf, const string& key, const csp<profile>& p, bool pa
}

if (flags & BCM2_CFG_FMT_GWS_LEN_PREFIX) {
buf.insert(0, to_buf(htonl(buf.size())));
buf.insert(0, to_buf(h_to_be(buf.size())));
} else if (flags & BCM2_CFG_FMT_GWS_CLEN_PREFIX) {
buf.insert(0, "Content-Length: " + to_string(buf.size()) + "\r\n\r\n");
}
Expand Down Expand Up @@ -576,14 +576,14 @@ class permdyn : public encryptable_settings
uint32_t sum = buf.size() + 8;

while (remaining >= 4) {
sum += ntoh(extract<uint32_t>(buf.substr(buf.size() - remaining, 4)));
sum += be_to_h(extract<uint32_t>(buf.substr(buf.size() - remaining, 4)));
remaining -= 4;
}

uint16_t half = 0;

if (remaining >= 2) {
half = ntoh(extract<uint16_t>(buf.substr(buf.size() - remaining, 2)));
half = be_to_h(extract<uint16_t>(buf.substr(buf.size() - remaining, 2)));
remaining -= 2;
}

Expand Down
2 changes: 1 addition & 1 deletion interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void bfc::do_elevate_privileges()
try {
wait_ready();
ram->space().check_offset(ct_instance, "bfc:conthread_instance");
uint32_t addr = ntoh(extract<uint32_t>(ram->read(ct_instance, 4)));
uint32_t addr = be_to_h(extract<uint32_t>(ram->read(ct_instance, 4)));
addr += ct_priv_off;
ram->space().check_offset(addr, "console_priv_flag");
ram->write(addr, "\x01"s);
Expand Down
4 changes: 2 additions & 2 deletions io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ int connect_nonblock(int fd, sockaddr* addr, socklen_t len)
void set_port(sockaddr* sa, uint16_t port)
{
if (sa->sa_family == AF_INET) {
reinterpret_cast<sockaddr_in*>(sa)->sin_port = htons(port);
reinterpret_cast<sockaddr_in*>(sa)->sin_port = h_to_be(port);
} else if (sa->sa_family == AF_INET6) {
reinterpret_cast<sockaddr_in6*>(sa)->sin6_port = htons(port);
reinterpret_cast<sockaddr_in6*>(sa)->sin6_port = h_to_be(port);
}
}

Expand Down
15 changes: 4 additions & 11 deletions nonvol2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,9 @@ std::string desc(const nv_val::named& var)
return var.name + " (" + var.val->type() + ")";
}

template<typename T> T read_num(istream& is)
{
T num;
is.read(reinterpret_cast<char*>(&num), sizeof(T));
return ntoh(num);
}

template<typename T> void write_num(ostream& os, T num)
{
num = hton(num);
num = h_to_be(num);
os.write(reinterpret_cast<const char*>(&num), sizeof(T));
}

Expand Down Expand Up @@ -623,9 +616,9 @@ istream& nv_string::read(istream& is)

if (!size) {
if (m_flags & flag_prefix_u8) {
size = read_num<uint8_t>(is);
size = nv_u8::read_num(is);
} else if (m_flags & flag_prefix_u16) {
size = read_num<uint16_t>(is);
size = nv_u16::read_num(is);
} else {
getline(is, val, '\0');
zstring = true;
Expand Down Expand Up @@ -724,7 +717,7 @@ nv_magic::nv_magic(const std::string& magic)
nv_magic::nv_magic(uint32_t magic)
: nv_magic()
{
magic = hton(magic);
magic = h_to_be(magic);
parse_checked(string(reinterpret_cast<const char*>(&magic), 4));
}

Expand Down
41 changes: 34 additions & 7 deletions nonvol2.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ template<class T, class I, bool L> class nv_array_generic : public nv_array_base
if (!m_count && !is.read(reinterpret_cast<char*>(&m_count), sizeof(I))) {
return is;
}
m_count = bcm2dump::ntoh(m_count);
m_count = bcm2dump::be_to_h(m_count);
}

// FIXME ugly workaround for parsing an array of elements with non-constant width
Expand All @@ -290,7 +290,7 @@ template<class T, class I, bool L> class nv_array_generic : public nv_array_base
virtual std::ostream& write(std::ostream& os) const override
{
if (L) {
I count = bcm2dump::hton(m_count);
I count = bcm2dump::h_to_be(m_count);
if (!os.write(reinterpret_cast<const char*>(&count), sizeof(I))) {
return os;
}
Expand Down Expand Up @@ -575,7 +575,9 @@ class nv_num : public nv_val
virtual std::istream& read(std::istream& is) override
{
if (is.read(reinterpret_cast<char*>(&m_val), sizeof(T))) {
m_val = bcm2dump::ntoh(m_val);
if (sizeof(T) > 1) {
m_val = bcm2dump::be_to_h(m_val);
}
m_set = true;
}

Expand All @@ -599,18 +601,43 @@ class nv_num : public nv_val

static std::ostream& write(std::ostream& os, const T& num)
{
T swapped = bcm2dump::hton(num);
T swapped = bcm2dump::h_to_be(num);
return os.write(reinterpret_cast<const char*>(&swapped), sizeof(T));
}

static const T read(std::istream& in, T& num)
static T read(std::istream& in, T& num)
{
return read(in, num, nullptr);
}

static T read_num(std::istream& in)
{
in.read(reinterpret_cast<char*>(&num), sizeof(T));
num = bcm2dump::ntoh(num);
T num;
bool success = false;
read(in, num, &success);
if (!success) {
throw std::runtime_error("failed to read number");
}
return num;
}

protected:
static T read(std::istream& in, T& num, bool* p_success)
{
bool success = !!in.read(reinterpret_cast<char*>(&num), sizeof(T));
if (success) {
if (sizeof(T) > 1) {
num = bcm2dump::be_to_h(num);
}
}

if (p_success) {
*p_success = success;
}

return num;
}

T m_val;
bool m_hex = false;
};
Expand Down
2 changes: 1 addition & 1 deletion ps.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ps_header& ps_header::parse(const string& buf)
memcpy(&m_raw, buf.data(), sizeof(m_raw));
uint16_t hcs = crc16_ccitt(&m_raw, sizeof(m_raw) - 8) ^ 0xffff;

m_valid = (hcs == ntoh(m_raw.hcs));
m_valid = (hcs == be_to_h(m_raw.hcs));
return *this;
}

Expand Down
6 changes: 3 additions & 3 deletions ps.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ class ps_header
std::string filename() const;

uint16_t signature() const
{ return ntoh(m_raw.signature); }
{ return be_to_h(m_raw.signature); }

uint32_t length() const
{ return ntoh(m_raw.length); }
{ return be_to_h(m_raw.length); }

uint16_t control() const
{ return ntoh(m_raw.control); }
{ return be_to_h(m_raw.control); }

uint16_t compression() const
{ return control() & 0x7; }
Expand Down
14 changes: 7 additions & 7 deletions psextract.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ class mono_header
}

bool valid() const
{ return ntoh(m_raw.magic) == 0x4d4f4e4f; }
{ return be_to_h(m_raw.magic) == 0x4d4f4e4f; }

uint16_t signature() const
{ return ntoh(m_raw.signature); }
{ return be_to_h(m_raw.signature); }

uint32_t length() const
{ return ntoh(m_raw.length); }
{ return be_to_h(m_raw.length); }

uint16_t unk1() const
{ return ntoh(m_raw.unk1); }
{ return be_to_h(m_raw.unk1); }

uint16_t unk2() const
{ return ntoh(m_raw.unk2); }
{ return be_to_h(m_raw.unk2); }

uint16_t unk3() const
{ return ntoh(m_raw.unk3); }
{ return be_to_h(m_raw.unk3); }

private:
raw m_raw;
Expand Down Expand Up @@ -157,7 +157,7 @@ void extract_image(istream& in)
// add 7, because sizeof(type + len) is 4, and
// sizeof(end-of-data) is 2. add 1 for next data.

auto len = ntoh(extract<uint16_t>(hbuf, 2)) + 7;
auto len = be_to_h(extract<uint16_t>(hbuf, 2)) + 7;
logger::i() << "asn.1 data, " << len << " b " << endl;

in.seekg(beg + len);
Expand Down

0 comments on commit 2ea96e8

Please sign in to comment.