1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/*++
Module Name:
GenericFile_Blob.cpp
Abstract:
Generic IO class for SNAP that can read from an in-memory blob.
Authors:
Bill Bolosky, March, 2014
Environment:
User mode service.
Revision History:
--*/
#include "stdafx.h"
#include "GenericFile_Blob.h"
GenericFile_Blob::GenericFile_Blob(void *i_blob, size_t i_blobSize) : blob((char *)i_blob), readPointer((char *)i_blob), blobSize(i_blobSize), blobEnd((char *)i_blob + i_blobSize)
{
}
GenericFile_Blob *
GenericFile_Blob::open(void *i_blob, size_t i_blobSize)
{
GenericFile_Blob *file = new GenericFile_Blob(i_blob, i_blobSize);
return file;
}
size_t
GenericFile_Blob::read(void *ptr, size_t count)
{
size_t bytesReturned;
void *base = mapAndAdvance(count, &bytesReturned);
memcpy(ptr, base, bytesReturned);
return bytesReturned;
}
int
GenericFile_Blob::advance(long long offset)
{
if (offset < 0) {
if (readPointer - blob > -1 * offset) {
return 1;
}
readPointer += offset;
return 0;
}
long long amountRemaining = blobEnd - readPointer;
if (amountRemaining < offset) {
return EOF;
}
readPointer += offset;
return 0;
}
void
GenericFile_Blob::close()
{
blob = readPointer = blobEnd = NULL;
blobSize = 0;
}
GenericFile_Blob::~GenericFile_Blob()
{
close();
}
int
GenericFile_Blob::getchar()
{
if (readPointer >= blobEnd) {
return EOF;
}
unsigned char c = *(unsigned char *)readPointer;
readPointer++;
return (int)c;
}
char *GenericFile_Blob::gets(char *buf, size_t count)
{
return _gets_impl(buf, count);
}
//
// This gives a pointer into the blob rather than copying it.
// It's the caller's responsibility to assure that the blob doesn't
// go away while this pointer's still in use.
//
void *
GenericFile_Blob::mapAndAdvance(size_t count, size_t *bytesReturned)
{
size_t amountRemaining = blobEnd - readPointer;
if (count > amountRemaining) {
*bytesReturned = amountRemaining;
} else {
*bytesReturned = count;
}
void *retVal = readPointer;
readPointer += *bytesReturned;
return retVal;
}
size_t
GenericFile_Blob::getAmountUsed()
{
return readPointer - blob;
}
|