#include <unistd.h>
#include "blob.h"
namespace Security {
char *BlobCore::stringAt(Offset offset)
{
char *s = at<char>(offset);
if (offset < this->length() && memchr(s, 0, this->length() - offset))
return s;
else
return NULL;
}
const char *BlobCore::stringAt(Offset offset) const
{
const char *s = at<const char>(offset);
if (offset < this->length() && memchr(s, 0, this->length() - offset))
return s;
else
return NULL;
}
BlobCore *BlobCore::readBlob(int fd, size_t offset, uint32_t magic, size_t minSize, size_t maxSize)
{
BlobCore header;
if (::pread(fd, &header, sizeof(header), offset) == sizeof(header))
if (header.validateBlob(magic, minSize, maxSize))
if (BlobCore *blob = (BlobCore *)malloc(header.length())) {
memcpy(blob, &header, sizeof(header));
size_t remainder = header.length() - sizeof(header);
if (::pread(fd, blob+1, remainder, offset + sizeof(header)) == ssize_t(remainder))
return blob;
free(blob);
errno = EINVAL;
}
return NULL;
}
BlobCore *BlobCore::readBlob(int fd, uint32_t magic, size_t minSize, size_t maxSize)
{
BlobCore header;
if (::read(fd, &header, sizeof(header)) == sizeof(header))
if (header.validateBlob(magic, minSize, maxSize))
if (BlobCore *blob = (BlobCore *)malloc(header.length())) {
memcpy(blob, &header, sizeof(header));
size_t remainder = header.length() - sizeof(header);
if (::read(fd, blob+1, remainder) == ssize_t(remainder))
return blob;
free(blob);
errno = EINVAL;
}
return NULL;
}
BlobCore *BlobCore::readBlob(std::FILE *file, uint32_t magic, size_t minSize, size_t maxSize)
{
BlobCore header;
if (::fread(&header, sizeof(header), 1, file) == 1)
if (header.validateBlob(magic, minSize, maxSize))
if (BlobCore *blob = (BlobCore *)malloc(header.length())) {
memcpy(blob, &header, sizeof(header));
if (::fread(blob+1, header.length() - sizeof(header), 1, file) == 1)
return blob;
free(blob);
errno = EINVAL;
}
return NULL;
}
BlobWrapper *BlobWrapper::alloc(size_t length, Magic magic )
{
size_t wrapLength = length + sizeof(BlobCore);
BlobWrapper *w = (BlobWrapper *)malloc(wrapLength);
w->BlobCore::initialize(magic, wrapLength);
return w;
}
BlobWrapper *BlobWrapper::alloc(const void *data, size_t length, Magic magic )
{
BlobWrapper *w = alloc(length, magic);
memcpy(w->data(), data, w->length());
return w;
}
}