Fix macOS build, where le32toh is not available

This commit is contained in:
Eelco Dolstra 2022-08-22 14:09:52 +02:00
parent a115c4f4b2
commit 91aea1572e
3 changed files with 15 additions and 12 deletions

View file

@ -1,7 +1,7 @@
#include "input-accessor.hh" #include "input-accessor.hh"
#include <zip.h> #include <zip.h>
#include <endian.h> #include <arpa/inet.h>
namespace nix { namespace nix {
@ -61,7 +61,7 @@ struct ZipInputAccessor : InputAccessor
zip_uint16_t id, len; zip_uint16_t id, len;
auto extra = zip_file_extra_field_get(zipFile, i, 0, &id, &len, ZIP_FL_CENTRAL); auto extra = zip_file_extra_field_get(zipFile, i, 0, &id, &len, ZIP_FL_CENTRAL);
if (id == 0x5455 && len >= 5) if (id == 0x5455 && len >= 5)
lastModified = std::max(lastModified, (time_t) le32toh(*((uint32_t *) (extra + 1)))); lastModified = std::max(lastModified, readLittleEndian<time_t>((unsigned char *) extra + 1));
} }
auto slash = strchr(sb.name, '/'); auto slash = strchr(sb.name, '/');

View file

@ -331,17 +331,9 @@ T readNum(Source & source)
unsigned char buf[8]; unsigned char buf[8];
source((char *) buf, sizeof(buf)); source((char *) buf, sizeof(buf));
uint64_t n = auto n = readLittleEndian<uint64_t>(buf);
((uint64_t) buf[0]) |
((uint64_t) buf[1] << 8) |
((uint64_t) buf[2] << 16) |
((uint64_t) buf[3] << 24) |
((uint64_t) buf[4] << 32) |
((uint64_t) buf[5] << 40) |
((uint64_t) buf[6] << 48) |
((uint64_t) buf[7] << 56);
if (n > (uint64_t)std::numeric_limits<T>::max()) if (n > (uint64_t) std::numeric_limits<T>::max())
throw SerialisationError("serialised integer %d is too large for type '%s'", n, typeid(T).name()); throw SerialisationError("serialised integer %d is too large for type '%s'", n, typeid(T).name());
return (T) n; return (T) n;

View file

@ -506,6 +506,17 @@ std::optional<N> string2Float(const std::string_view s)
} }
/* Convert a little-endian integer to host order. */
template<typename T>
T readLittleEndian(unsigned char * p)
{
T x = 0;
for (size_t i = 0; i < sizeof(x); ++i)
x |= *p++ << (i * 8);
return x;
}
/* Return true iff `s' starts with `prefix'. */ /* Return true iff `s' starts with `prefix'. */
bool hasPrefix(std::string_view s, std::string_view prefix); bool hasPrefix(std::string_view s, std::string_view prefix);