Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 716295 part 3 - Add serialization into a buffer instead of an ost…
Browse files Browse the repository at this point in the history
…ream. r=nfroyd
  • Loading branch information
glandium committed Aug 9, 2012
1 parent fb62e31 commit 3b9992d
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions build/unix/elfhack/elfxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ class serializable: public T::Type32 {
throw std::runtime_error("Unsupported ELF data encoding");
}

template <typename R>
void serialize(const char *buf, size_t len, char ei_data)
{
assert(len >= sizeof(R));
if (ei_data == ELFDATA2LSB) {
T::template swap<little_endian>(*this, *(R *)buf);
return;
} else if (ei_data == ELFDATA2MSB) {
T::template swap<big_endian>(*this, *(R *)buf);
return;
}
throw std::runtime_error("Unsupported ELF data encoding");
}

public:
serializable(const char *buf, size_t len, char ei_class, char ei_data)
{
Expand Down Expand Up @@ -209,30 +223,30 @@ class serializable: public T::Type32 {
{
if (ei_class == ELFCLASS32) {
typename T::Type32 e;
if (ei_data == ELFDATA2LSB) {
T::template swap<little_endian>(*this, e);
file.write((char *)&e, sizeof(e));
return;
} else if (ei_data == ELFDATA2MSB) {
T::template swap<big_endian>(*this, e);
file.write((char *)&e, sizeof(e));
return;
}
serialize<typename T::Type32>((char *)&e, sizeof(e), ei_data);
file.write((char *)&e, sizeof(e));
return;
} else if (ei_class == ELFCLASS64) {
typename T::Type64 e;
if (ei_data == ELFDATA2LSB) {
T::template swap<little_endian>(*this, e);
file.write((char *)&e, sizeof(e));
return;
} else if (ei_data == ELFDATA2MSB) {
T::template swap<big_endian>(*this, e);
file.write((char *)&e, sizeof(e));
return;
}
serialize<typename T::Type64>((char *)&e, sizeof(e), ei_data);
file.write((char *)&e, sizeof(e));
return;
}
throw std::runtime_error("Unsupported ELF class or data encoding");
}

void serialize(char *buf, size_t len, char ei_class, char ei_data)
{
if (ei_class == ELFCLASS32) {
serialize<typename T::Type32>(buf, len, ei_data);
return;
} else if (ei_class == ELFCLASS64) {
serialize<typename T::Type64>(buf, len, ei_data);
return;
}
throw std::runtime_error("Unsupported ELF class");
}

static inline unsigned int size(char ei_class)
{
if (ei_class == ELFCLASS32)
Expand Down

0 comments on commit 3b9992d

Please sign in to comment.