2020-06-20 23:39:08 +02:00
|
|
|
#include "framework.h"
|
|
|
|
#include "ChunkId.h"
|
|
|
|
using std::string;
|
2020-06-20 21:02:45 -03:00
|
|
|
ChunkId::ChunkId(char* bytes, int length) {
|
2020-06-20 23:39:08 +02:00
|
|
|
if (length == 0) {
|
|
|
|
m_chunkBytes = NULL;
|
|
|
|
m_length = 0;
|
|
|
|
} else {
|
|
|
|
m_chunkBytes = (byte*)malloc(length);
|
|
|
|
memcpy(m_chunkBytes, bytes, length);
|
|
|
|
m_length = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 21:02:45 -03:00
|
|
|
ChunkId::~ChunkId() {
|
2020-06-20 23:39:08 +02:00
|
|
|
if (m_chunkBytes != NULL)
|
|
|
|
delete m_chunkBytes;
|
|
|
|
}
|
|
|
|
|
2020-08-11 19:18:36 +02:00
|
|
|
std::unique_ptr<ChunkId> ChunkId::FromString(const char* str) {
|
|
|
|
return std::make_unique<ChunkId>((char*)str, strlen(str));
|
2020-06-20 23:39:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 19:18:36 +02:00
|
|
|
std::unique_ptr<ChunkId> ChunkId::FromString(string* str) {
|
|
|
|
return std::make_unique<ChunkId>( (char*)str->c_str(), str->length());
|
2020-06-20 23:39:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 19:18:36 +02:00
|
|
|
std::unique_ptr<ChunkId> ChunkId::FromStream(BaseStream* stream) {
|
2020-06-20 23:39:08 +02:00
|
|
|
int idLength = LEB128::ReadInt32(stream);
|
|
|
|
char* buffer = (char*)malloc(idLength);
|
|
|
|
stream->Read(buffer, idLength);
|
2020-08-11 19:18:36 +02:00
|
|
|
std::unique_ptr<ChunkId> chunk = std::make_unique<ChunkId>(buffer, idLength);
|
2020-06-20 23:39:08 +02:00
|
|
|
free(buffer);
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
2020-06-20 21:02:45 -03:00
|
|
|
void ChunkId::ToStream(BaseStream* stream) {
|
2020-06-20 23:39:08 +02:00
|
|
|
LEB128::Write(stream, m_length);
|
|
|
|
stream->WriteBytes(m_chunkBytes, m_length);
|
|
|
|
}
|
|
|
|
|
2020-06-20 21:02:45 -03:00
|
|
|
byte* ChunkId::GetBytes() {
|
2020-06-20 23:39:08 +02:00
|
|
|
return m_chunkBytes;
|
|
|
|
}
|
|
|
|
|
2020-06-20 21:02:45 -03:00
|
|
|
int ChunkId::GetLength() {
|
2020-06-20 23:39:08 +02:00
|
|
|
return m_length;
|
|
|
|
}
|
|
|
|
|
2020-06-20 21:02:45 -03:00
|
|
|
bool ChunkId::EqualsTo(ChunkId* other) {
|
2020-06-20 23:39:08 +02:00
|
|
|
if (m_length != other->GetLength())
|
|
|
|
return false;
|
|
|
|
return (strncmp((const char*)m_chunkBytes, (const char*)other->GetBytes(), m_length) == 0);
|
|
|
|
}
|