crl_set: Support big endian

This commit is contained in:
klzgrad 2019-11-30 22:19:02 +08:00
parent 7c605f71f9
commit 07da15711f

View File

@ -46,8 +46,8 @@ base::DictionaryValue* ReadHeader(base::StringPiece* data) {
uint16_t header_len; uint16_t header_len;
if (data->size() < sizeof(header_len)) if (data->size() < sizeof(header_len))
return nullptr; return nullptr;
// Assumes little-endian. header_len = static_cast<uint8_t>(data->data()[0]);
memcpy(&header_len, data->data(), sizeof(header_len)); header_len |= static_cast<uint8_t>(data->data()[1]) << 8;
data->remove_prefix(sizeof(header_len)); data->remove_prefix(sizeof(header_len));
if (data->size() < header_len) if (data->size() < header_len)
@ -81,8 +81,10 @@ bool ReadCRL(base::StringPiece* data,
uint32_t num_serials; uint32_t num_serials;
if (data->size() < sizeof(num_serials)) if (data->size() < sizeof(num_serials))
return false; return false;
// Assumes little endian. num_serials = static_cast<uint8_t>(data->data()[0]);
memcpy(&num_serials, data->data(), sizeof(num_serials)); num_serials |= static_cast<uint8_t>(data->data()[1]) << 8;
num_serials |= static_cast<uint8_t>(data->data()[2]) << 16;
num_serials |= static_cast<uint8_t>(data->data()[3]) << 24;
data->remove_prefix(sizeof(num_serials)); data->remove_prefix(sizeof(num_serials));
if (num_serials > 32 * 1024 * 1024) // Sanity check. if (num_serials > 32 * 1024 * 1024) // Sanity check.
@ -194,16 +196,6 @@ CRLSet::~CRLSet() = default;
// static // static
bool CRLSet::Parse(base::StringPiece data, scoped_refptr<CRLSet>* out_crl_set) { bool CRLSet::Parse(base::StringPiece data, scoped_refptr<CRLSet>* out_crl_set) {
TRACE_EVENT0(NetTracingCategory(), "CRLSet::Parse"); TRACE_EVENT0(NetTracingCategory(), "CRLSet::Parse");
// Other parts of Chrome assume that we're little endian, so we don't lose
// anything by doing this.
#if defined(__BYTE_ORDER)
// Linux check
static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, "assumes little endian");
#elif defined(__BIG_ENDIAN__)
// Mac check
#error assumes little endian
#endif
std::unique_ptr<base::DictionaryValue> header_dict(ReadHeader(&data)); std::unique_ptr<base::DictionaryValue> header_dict(ReadHeader(&data));
if (!header_dict.get()) if (!header_dict.get())
return false; return false;