xref: /haiku/src/kits/package/hpkg/PackageDataReader.cpp (revision 47bedf16012092c1a0e3f640ae671939486576bc)
10d6b3b20SOliver Tappe /*
232832cbeSIngo Weinhold  * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@gmx.de.
30d6b3b20SOliver Tappe  * Distributed under the terms of the MIT License.
40d6b3b20SOliver Tappe  */
50d6b3b20SOliver Tappe 
60d6b3b20SOliver Tappe 
70d6b3b20SOliver Tappe #include <package/hpkg/PackageDataReader.h>
80d6b3b20SOliver Tappe 
90d6b3b20SOliver Tappe #include <string.h>
100d6b3b20SOliver Tappe 
110d6b3b20SOliver Tappe #include <algorithm>
120d6b3b20SOliver Tappe #include <new>
130d6b3b20SOliver Tappe 
1433bc4425SOliver Tappe #include <package/hpkg/HPKGDefsPrivate.h>
150d6b3b20SOliver Tappe #include <package/hpkg/PackageData.h>
160d6b3b20SOliver Tappe 
170d6b3b20SOliver Tappe 
180d6b3b20SOliver Tappe namespace BPackageKit {
190d6b3b20SOliver Tappe 
205fb1c6ffSOliver Tappe namespace BHPKG {
210d6b3b20SOliver Tappe 
225fb1c6ffSOliver Tappe 
235fb1c6ffSOliver Tappe using namespace BPrivate;
240d6b3b20SOliver Tappe 
250d6b3b20SOliver Tappe 
261f633814SIngo Weinhold // #pragma mark - PackageDataHeapReader
270d6b3b20SOliver Tappe 
280d6b3b20SOliver Tappe 
291f633814SIngo Weinhold class PackageDataHeapReader : public BAbstractBufferedDataReader {
302c32402dSIngo Weinhold public:
PackageDataHeapReader(BAbstractBufferedDataReader * dataReader,const BPackageData & data)311f633814SIngo Weinhold 	PackageDataHeapReader(BAbstractBufferedDataReader* dataReader,
321f633814SIngo Weinhold 		const BPackageData& data)
330d6b3b20SOliver Tappe 		:
341f633814SIngo Weinhold 		fDataReader(dataReader),
351f633814SIngo Weinhold 		fOffset(data.Offset()),
361f633814SIngo Weinhold 		fSize(data.Size())
370d6b3b20SOliver Tappe 	{
380d6b3b20SOliver Tappe 	}
390d6b3b20SOliver Tappe 
ReadData(off_t offset,void * buffer,size_t size)400d6b3b20SOliver Tappe 	virtual status_t ReadData(off_t offset, void* buffer, size_t size)
410d6b3b20SOliver Tappe 	{
420d6b3b20SOliver Tappe 		if (size == 0)
430d6b3b20SOliver Tappe 			return B_OK;
440d6b3b20SOliver Tappe 
450d6b3b20SOliver Tappe 		if (offset < 0)
460d6b3b20SOliver Tappe 			return B_BAD_VALUE;
470d6b3b20SOliver Tappe 
480d6b3b20SOliver Tappe 		if ((uint64)offset > fSize || size > fSize - offset)
490d6b3b20SOliver Tappe 			return B_BAD_VALUE;
500d6b3b20SOliver Tappe 
510d6b3b20SOliver Tappe 		return fDataReader->ReadData(fOffset + offset, buffer, size);
520d6b3b20SOliver Tappe 	}
530d6b3b20SOliver Tappe 
ReadDataToOutput(off_t offset,size_t size,BDataIO * output)540d6b3b20SOliver Tappe 	virtual status_t ReadDataToOutput(off_t offset, size_t size,
5532832cbeSIngo Weinhold 		BDataIO* output)
560d6b3b20SOliver Tappe 	{
570d6b3b20SOliver Tappe 		if (size == 0)
580d6b3b20SOliver Tappe 			return B_OK;
590d6b3b20SOliver Tappe 
600d6b3b20SOliver Tappe 		if (offset < 0)
610d6b3b20SOliver Tappe 			return B_BAD_VALUE;
620d6b3b20SOliver Tappe 
630d6b3b20SOliver Tappe 		if ((uint64)offset > fSize || size > fSize - offset)
640d6b3b20SOliver Tappe 			return B_BAD_VALUE;
650d6b3b20SOliver Tappe 
661f633814SIngo Weinhold 		return fDataReader->ReadDataToOutput(fOffset + offset, size, output);
670d6b3b20SOliver Tappe 	}
680d6b3b20SOliver Tappe 
690d6b3b20SOliver Tappe private:
701f633814SIngo Weinhold 	BAbstractBufferedDataReader*	fDataReader;
710d6b3b20SOliver Tappe 	uint64							fOffset;
720d6b3b20SOliver Tappe 	uint64							fSize;
730d6b3b20SOliver Tappe };
740d6b3b20SOliver Tappe 
750d6b3b20SOliver Tappe 
7632832cbeSIngo Weinhold // #pragma mark - PackageDataInlineReader
770d6b3b20SOliver Tappe 
780d6b3b20SOliver Tappe 
791f633814SIngo Weinhold class PackageDataInlineReader : public BBufferDataReader {
800d6b3b20SOliver Tappe public:
PackageDataInlineReader(const BPackageData & data)811f633814SIngo Weinhold 	PackageDataInlineReader(const BPackageData& data)
820d6b3b20SOliver Tappe 		:
83*47bedf16SAugustin Cavalier 		BBufferDataReader(data.InlineData(), data.Size()),
841f633814SIngo Weinhold 		fData(data)
850d6b3b20SOliver Tappe 	{
860d6b3b20SOliver Tappe 	}
870d6b3b20SOliver Tappe 
880d6b3b20SOliver Tappe private:
891f633814SIngo Weinhold 	BPackageData	fData;
900d6b3b20SOliver Tappe };
910d6b3b20SOliver Tappe 
920d6b3b20SOliver Tappe 
935fb1c6ffSOliver Tappe // #pragma mark - BPackageDataReaderFactory
940d6b3b20SOliver Tappe 
950d6b3b20SOliver Tappe 
BPackageDataReaderFactory()961f633814SIngo Weinhold BPackageDataReaderFactory::BPackageDataReaderFactory()
970d6b3b20SOliver Tappe {
980d6b3b20SOliver Tappe }
990d6b3b20SOliver Tappe 
1000d6b3b20SOliver Tappe 
1010d6b3b20SOliver Tappe status_t
CreatePackageDataReader(BAbstractBufferedDataReader * dataReader,const BPackageData & data,BAbstractBufferedDataReader * & _reader)1021f633814SIngo Weinhold BPackageDataReaderFactory::CreatePackageDataReader(
1031f633814SIngo Weinhold 	BAbstractBufferedDataReader* dataReader, const BPackageData& data,
1041f633814SIngo Weinhold 	BAbstractBufferedDataReader*& _reader)
1050d6b3b20SOliver Tappe {
1061f633814SIngo Weinhold 	BAbstractBufferedDataReader* reader;
1071f633814SIngo Weinhold 	if (data.IsEncodedInline())
1081f633814SIngo Weinhold 		reader = new(std::nothrow) PackageDataInlineReader(data);
1091f633814SIngo Weinhold 	else
1101f633814SIngo Weinhold 		reader = new(std::nothrow) PackageDataHeapReader(dataReader, data);
1110d6b3b20SOliver Tappe 
1120d6b3b20SOliver Tappe 	if (reader == NULL)
1130d6b3b20SOliver Tappe 		return B_NO_MEMORY;
1140d6b3b20SOliver Tappe 
1150d6b3b20SOliver Tappe 	_reader = reader;
1160d6b3b20SOliver Tappe 	return B_OK;
1170d6b3b20SOliver Tappe }
1180d6b3b20SOliver Tappe 
1190d6b3b20SOliver Tappe 
1205fb1c6ffSOliver Tappe }	// namespace BHPKG
1210d6b3b20SOliver Tappe 
1220d6b3b20SOliver Tappe }	// namespace BPackageKit
123