mirror of
https://github.com/corda/corda.git
synced 2025-01-07 13:38:47 +00:00
allow streaming to more than just files in binaryToObject
This commit is contained in:
parent
9c308f751c
commit
175db9ec54
@ -166,7 +166,7 @@ public:
|
|||||||
ElfObjectWriter(PlatformInfo::Architecture arch):
|
ElfObjectWriter(PlatformInfo::Architecture arch):
|
||||||
arch(arch) {}
|
arch(arch) {}
|
||||||
|
|
||||||
void writeObject(const uint8_t* data, unsigned size, FILE* out,
|
void writeObject(const uint8_t* data, unsigned size, OutputStream* out,
|
||||||
const char* startName, const char* endName,
|
const char* startName, const char* endName,
|
||||||
const char* sectionName, unsigned sectionFlags,
|
const char* sectionName, unsigned sectionFlags,
|
||||||
unsigned alignment, int machine, int encoding)
|
unsigned alignment, int machine, int encoding)
|
||||||
@ -311,31 +311,30 @@ public:
|
|||||||
endSymbol.st_other = V1(STV_DEFAULT);
|
endSymbol.st_other = V1(STV_DEFAULT);
|
||||||
endSymbol.st_shndx = V2(bodySectionNumber);
|
endSymbol.st_shndx = V2(bodySectionNumber);
|
||||||
|
|
||||||
fwrite(&fileHeader, 1, sizeof(fileHeader), out);
|
out->writeChunk(&fileHeader, sizeof(fileHeader));
|
||||||
fwrite(&nullSection, 1, sizeof(nullSection), out);
|
out->writeChunk(&nullSection, sizeof(nullSection));
|
||||||
fwrite(&bodySection, 1, sizeof(bodySection), out);
|
out->writeChunk(&bodySection, sizeof(bodySection));
|
||||||
fwrite(§ionStringTableSection, 1, sizeof(sectionStringTableSection),
|
out->writeChunk(§ionStringTableSection, sizeof(sectionStringTableSection));
|
||||||
out);
|
out->writeChunk(&stringTableSection, sizeof(stringTableSection));
|
||||||
fwrite(&stringTableSection, 1, sizeof(stringTableSection), out);
|
out->writeChunk(&symbolTableSection, sizeof(symbolTableSection));
|
||||||
fwrite(&symbolTableSection, 1, sizeof(symbolTableSection), out);
|
|
||||||
|
|
||||||
fwrite(data, 1, size, out);
|
out->writeChunk(data, size);
|
||||||
|
|
||||||
fputc(0, out);
|
out->write(0);
|
||||||
fwrite(sectionStringTableName, 1, sectionStringTableNameLength, out);
|
out->writeChunk(sectionStringTableName, sectionStringTableNameLength);
|
||||||
fwrite(stringTableName, 1, stringTableNameLength, out);
|
out->writeChunk(stringTableName, stringTableNameLength);
|
||||||
fwrite(symbolTableName, 1, symbolTableNameLength, out);
|
out->writeChunk(symbolTableName, symbolTableNameLength);
|
||||||
fwrite(sectionName, 1, sectionNameLength, out);
|
out->writeChunk(sectionName, sectionNameLength);
|
||||||
|
|
||||||
fputc(0, out);
|
out->write(0);
|
||||||
fwrite(startName, 1, startNameLength, out);
|
out->writeChunk(startName, startNameLength);
|
||||||
fwrite(endName, 1, endNameLength, out);
|
out->writeChunk(endName, endNameLength);
|
||||||
|
|
||||||
fwrite(&startSymbol, 1, sizeof(startSymbol), out);
|
out->writeChunk(&startSymbol, sizeof(startSymbol));
|
||||||
fwrite(&endSymbol, 1, sizeof(endSymbol), out);
|
out->writeChunk(&endSymbol, sizeof(endSymbol));
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool write(uint8_t* data, size_t size, FILE* out,
|
virtual bool write(uint8_t* data, size_t size, OutputStream* out,
|
||||||
const char* startName, const char* endName,
|
const char* startName, const char* endName,
|
||||||
unsigned alignment, unsigned accessFlags)
|
unsigned alignment, unsigned accessFlags)
|
||||||
{
|
{
|
||||||
|
@ -148,7 +148,7 @@ public:
|
|||||||
MachOObjectWriter(PlatformInfo::Architecture arch):
|
MachOObjectWriter(PlatformInfo::Architecture arch):
|
||||||
arch(arch) {}
|
arch(arch) {}
|
||||||
|
|
||||||
void writeObject(const uint8_t* data, unsigned size, FILE* out,
|
void writeObject(const uint8_t* data, unsigned size, OutputStream* out,
|
||||||
const char* startName, const char* endName,
|
const char* startName, const char* endName,
|
||||||
const char* segmentName, const char* sectionName,
|
const char* segmentName, const char* sectionName,
|
||||||
unsigned alignment, cpu_type_t cpuType, cpu_subtype_t cpuSubType)
|
unsigned alignment, cpu_type_t cpuType, cpu_subtype_t cpuSubType)
|
||||||
@ -242,22 +242,23 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fwrite(&header, 1, sizeof(header), out);
|
out->writeChunk(&header, sizeof(header));
|
||||||
fwrite(&segment, 1, sizeof(segment), out);
|
out->writeChunk(&segment, sizeof(segment));
|
||||||
fwrite(§, 1, sizeof(sect), out);
|
out->writeChunk(§, sizeof(sect));
|
||||||
fwrite(&symbolTable, 1, sizeof(symbolTable), out);
|
out->writeChunk(&symbolTable, sizeof(symbolTable));
|
||||||
|
|
||||||
fwrite(data, 1, size, out);
|
out->writeChunk(data, size);
|
||||||
for (unsigned i = 0; i < pad(size) - size; ++i) fputc(0, out);
|
out->writeRepeat(0, pad(size) - size);
|
||||||
|
|
||||||
fwrite(&symbolList, 1, sizeof(symbolList), out);
|
out->writeChunk(&symbolList, sizeof(symbolList));
|
||||||
|
|
||||||
fputc(0, out);
|
out->write(0);
|
||||||
fwrite(startName, 1, startNameLength, out);
|
|
||||||
fwrite(endName, 1, endNameLength, out);
|
out->writeChunk(startName, startNameLength);
|
||||||
|
out->writeChunk(endName, endNameLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool write(uint8_t* data, size_t size, FILE* out,
|
virtual bool write(uint8_t* data, size_t size, OutputStream* out,
|
||||||
const char* startName, const char* endName,
|
const char* startName, const char* endName,
|
||||||
unsigned alignment, unsigned accessFlags)
|
unsigned alignment, unsigned accessFlags)
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ namespace {
|
|||||||
using namespace avian::tools;
|
using namespace avian::tools;
|
||||||
|
|
||||||
bool
|
bool
|
||||||
writeObject(uint8_t* data, unsigned size, FILE* out, const char* startName,
|
writeObject(uint8_t* data, unsigned size, OutputStream* out, const char* startName,
|
||||||
const char* endName, const char* os,
|
const char* endName, const char* os,
|
||||||
const char* architecture, unsigned alignment, bool writable,
|
const char* architecture, unsigned alignment, bool writable,
|
||||||
bool executable)
|
bool executable)
|
||||||
@ -142,13 +142,11 @@ main(int argc, const char** argv)
|
|||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
FILE* out = fopen(argv[2], "wb");
|
FileOutputStream out(argv[2]);
|
||||||
if (out) {
|
if (out.isValid()) {
|
||||||
success = writeObject
|
success = writeObject
|
||||||
(data, size, out, argv[3], argv[4], argv[5], argv[6], alignment,
|
(data, size, &out, argv[3], argv[4], argv[5], argv[6], alignment,
|
||||||
writable, executable);
|
writable, executable);
|
||||||
|
|
||||||
fclose(out);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "unable to open %s\n", argv[2]);
|
fprintf(stderr, "unable to open %s\n", argv[2]);
|
||||||
}
|
}
|
||||||
|
@ -79,8 +79,10 @@ pad(unsigned n)
|
|||||||
return (n + (4 - 1)) & ~(4 - 1);
|
return (n + (4 - 1)) & ~(4 - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using namespace avian::tools;
|
||||||
|
|
||||||
void
|
void
|
||||||
writeObject(const uint8_t* data, unsigned size, FILE* out,
|
writeObject(const uint8_t* data, unsigned size, OutputStream* out,
|
||||||
const char* startName, const char* endName,
|
const char* startName, const char* endName,
|
||||||
const char* sectionName, int machine, int machineMask,
|
const char* sectionName, int machine, int machineMask,
|
||||||
int sectionMask)
|
int sectionMask)
|
||||||
@ -147,24 +149,22 @@ writeObject(const uint8_t* data, unsigned size, FILE* out,
|
|||||||
};
|
};
|
||||||
endSymbol.N.Name.Long = endNameOffset;
|
endSymbol.N.Name.Long = endNameOffset;
|
||||||
|
|
||||||
fwrite(&fileHeader, 1, sizeof(fileHeader), out);
|
out->writeChunk(&fileHeader, sizeof(fileHeader));
|
||||||
fwrite(§ionHeader, 1, sizeof(sectionHeader), out);
|
out->writeChunk(§ionHeader, sizeof(sectionHeader));
|
||||||
|
|
||||||
fwrite(data, 1, size, out);
|
out->writeChunk(data, size);
|
||||||
for (unsigned i = 0; i < pad(size) - size; ++i) fputc(0, out);
|
out->writeRepeat(0, pad(size) - size);
|
||||||
|
|
||||||
fwrite(&startSymbol, 1, sizeof(startSymbol), out);
|
out->writeChunk(&startSymbol, sizeof(startSymbol));
|
||||||
fwrite(&endSymbol, 1, sizeof(endSymbol), out);
|
out->writeChunk(&endSymbol, sizeof(endSymbol));
|
||||||
|
|
||||||
uint32_t symbolTableSize = endNameOffset + endNameLength;
|
uint32_t symbolTableSize = endNameOffset + endNameLength;
|
||||||
fwrite(&symbolTableSize, 1, 4, out);
|
out->writeChunk(&symbolTableSize, 4);
|
||||||
|
|
||||||
fwrite(startName, 1, startNameLength, out);
|
out->writeChunk(startName, startNameLength);
|
||||||
fwrite(endName, 1, endNameLength, out);
|
out->writeChunk(endName, endNameLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
using namespace avian::tools;
|
|
||||||
|
|
||||||
template<unsigned BytesPerWord>
|
template<unsigned BytesPerWord>
|
||||||
class WindowsPlatform : public Platform {
|
class WindowsPlatform : public Platform {
|
||||||
public:
|
public:
|
||||||
@ -172,7 +172,7 @@ public:
|
|||||||
class PEObjectWriter : public ObjectWriter {
|
class PEObjectWriter : public ObjectWriter {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual bool write(uint8_t* data, size_t size, FILE* out,
|
virtual bool write(uint8_t* data, size_t size, OutputStream* out,
|
||||||
const char* startName, const char* endName,
|
const char* startName, const char* endName,
|
||||||
unsigned alignment, unsigned accessFlags)
|
unsigned alignment, unsigned accessFlags)
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,37 @@ namespace avian {
|
|||||||
|
|
||||||
namespace tools {
|
namespace tools {
|
||||||
|
|
||||||
|
void OutputStream::write(uint8_t byte) {
|
||||||
|
writeChunk(&byte, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OutputStream::writeRepeat(uint8_t byte, size_t size) {
|
||||||
|
for(size_t i = 0; i < size; i++) {
|
||||||
|
write(byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FileOutputStream::FileOutputStream(const char* name):
|
||||||
|
file(fopen(name, "wb")) {}
|
||||||
|
|
||||||
|
FileOutputStream::~FileOutputStream() {
|
||||||
|
if(file) {
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileOutputStream::isValid() {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileOutputStream::writeChunk(const void* data, size_t size) {
|
||||||
|
fwrite(data, size, 1, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileOutputStream::write(uint8_t byte) {
|
||||||
|
fputc(byte, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Platform* Platform::first = 0;
|
Platform* Platform::first = 0;
|
||||||
|
|
||||||
|
@ -15,6 +15,26 @@ namespace avian {
|
|||||||
|
|
||||||
namespace tools {
|
namespace tools {
|
||||||
|
|
||||||
|
class OutputStream {
|
||||||
|
public:
|
||||||
|
virtual void writeChunk(const void* data, size_t size) = 0;
|
||||||
|
virtual void write(uint8_t byte);
|
||||||
|
virtual void writeRepeat(uint8_t byte, size_t size);
|
||||||
|
};
|
||||||
|
|
||||||
|
class FileOutputStream : public OutputStream {
|
||||||
|
private:
|
||||||
|
FILE* file;
|
||||||
|
public:
|
||||||
|
FileOutputStream(const char* name);
|
||||||
|
~FileOutputStream();
|
||||||
|
|
||||||
|
bool isValid();
|
||||||
|
|
||||||
|
virtual void writeChunk(const void* data, size_t size);
|
||||||
|
virtual void write(uint8_t byte);
|
||||||
|
};
|
||||||
|
|
||||||
class ObjectWriter {
|
class ObjectWriter {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -24,7 +44,7 @@ public:
|
|||||||
Executable = 1 << 2
|
Executable = 1 << 2
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual bool write(uint8_t* data, size_t size, FILE* out,
|
virtual bool write(uint8_t* data, size_t size, OutputStream* out,
|
||||||
const char* startName, const char* endName,
|
const char* startName, const char* endName,
|
||||||
unsigned alignment, unsigned accessFlags) = 0;
|
unsigned alignment, unsigned accessFlags) = 0;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user