sl@0: // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "readers.h" sl@0: sl@0: // sl@0: // Creates a new CReader object. sl@0: // sl@0: CReader::CReader(const TDesC8& aBuffer, TReaderType aType) sl@0: : iBuffer(aBuffer), sl@0: iType(aType) sl@0: { sl@0: } sl@0: sl@0: // sl@0: // Returns ETrue is aPattern is in the buffer, EFalse otherwise. sl@0: // sl@0: TBool CReader::Match(const TDesC8& aPattern) sl@0: { sl@0: return (iBuffer.Match(aPattern) != KErrNotFound); sl@0: } sl@0: sl@0: sl@0: // sl@0: // Reads aBuf.Length() bytes into a descriptor. sl@0: // sl@0: void CReader::ReadBytesL(TDes8& aBuf) sl@0: { sl@0: TInt bufLen = aBuf.Length(); sl@0: sl@0: if (!CheckEnoughData(bufLen)) sl@0: { sl@0: User::Leave(KErrEof); sl@0: } sl@0: sl@0: for (TInt i = 0; i < bufLen; i++) sl@0: { sl@0: aBuf[i] = iBuffer[iBufPos++]; sl@0: } sl@0: } sl@0: sl@0: sl@0: // sl@0: // Reads a byte from the buffer. sl@0: // sl@0: void CReader::ReadByteL(TUint8& aData) sl@0: { sl@0: if (!CheckEnoughData(sizeof(TUint8))) sl@0: { sl@0: User::Leave(KErrEof); sl@0: } sl@0: sl@0: aData = iBuffer[iBufPos++]; sl@0: } sl@0: sl@0: // sl@0: // Reads 16 bits of data at the current reader position. sl@0: // Some formats (mainly those developed by Microsoft) still use little-endian sl@0: // encoding of fields. The file format specifications will specify which sl@0: // byte-ordering scheme is used. sl@0: // sl@0: void CReader::Read16L(TUint16& aData, TBool aLittleEndian) sl@0: { sl@0: if (!CheckEnoughData(sizeof(TUint16))) sl@0: { sl@0: User::Leave(KErrEof); sl@0: } sl@0: sl@0: TUint8 a = iBuffer[iBufPos++]; sl@0: TUint8 b = iBuffer[iBufPos++]; sl@0: sl@0: if (aLittleEndian) sl@0: { sl@0: aData = (b << 8) | a; sl@0: } sl@0: else sl@0: { sl@0: aData = (a << 8) | b; sl@0: } sl@0: } sl@0: sl@0: sl@0: // sl@0: // Reads 32 bits of data at the current reader position. sl@0: // Some formats (mainly those developed by Microsoft) still use little-endian sl@0: // encoding of fields. The file format specifications will specify which sl@0: // byte-ordering scheme is used. sl@0: // sl@0: void CReader::Read32L(TUint32& aData, TBool aLittleEndian) sl@0: { sl@0: if (!CheckEnoughData(sizeof(TUint32))) sl@0: { sl@0: User::Leave(KErrEof); sl@0: } sl@0: sl@0: TUint8 a = iBuffer[iBufPos++]; sl@0: TUint8 b = iBuffer[iBufPos++]; sl@0: TUint8 c = iBuffer[iBufPos++]; sl@0: TUint8 d = iBuffer[iBufPos++]; sl@0: sl@0: if (aLittleEndian) sl@0: { sl@0: aData = MAKE_INT32(d, c, b, a); sl@0: } sl@0: else sl@0: { sl@0: aData = MAKE_INT32(a, b, c, d); sl@0: } sl@0: } sl@0: sl@0: sl@0: // sl@0: // Reads 64 bits of data at the current reader position. sl@0: // Some formats (mainly those developed by Microsoft) still use little-endian sl@0: // encoding of fields. The file format specifications will specify which sl@0: // byte-ordering scheme is used. sl@0: // sl@0: void CReader::Read64L(TInt64& aData, TBool aLittleEndian) sl@0: { sl@0: TUint32 high; sl@0: TUint32 low; sl@0: sl@0: Read32L(high, aLittleEndian); sl@0: Read32L(low, aLittleEndian); sl@0: sl@0: if (aLittleEndian) sl@0: { sl@0: aData = MAKE_TINT64(low, high); sl@0: } sl@0: else sl@0: { sl@0: aData = MAKE_TINT64(high, low); sl@0: } sl@0: } sl@0: sl@0: sl@0: // sl@0: // sl@0: // sl@0: TBool CReader::CheckEnoughData(TInt aNeeded) sl@0: { sl@0: return ((iBufPos + aNeeded) <= iBuffer.Length()); sl@0: } sl@0: sl@0: sl@0: // sl@0: // Skips forwards or backwards aOffset number of bytes. sl@0: // sl@0: TInt CReader::Seek(TInt aOffset) sl@0: { sl@0: TInt newBufPos = iBufPos + aOffset; sl@0: sl@0: if ((newBufPos < 0) || (newBufPos >= iBuffer.Length())) sl@0: { sl@0: // Trying to seek past the bounds of the buffer. sl@0: return KErrUnderflow; sl@0: } sl@0: sl@0: iBufPos += aOffset; sl@0: return KErrNone; sl@0: } sl@0: sl@0: // sl@0: // Skips forwards or backwards aOffset number of bytes. sl@0: // sl@0: TInt CReader::Seek(TInt64 aOffset) sl@0: { sl@0: TInt64 newBufPos = iBufPos + aOffset; sl@0: sl@0: if ((newBufPos < 0) || (newBufPos >= iBuffer.Length())) sl@0: { sl@0: // Trying to seek past the bounds of the buffer. sl@0: return KErrUnderflow; sl@0: } sl@0: sl@0: iBufPos += aOffset; sl@0: return KErrNone; sl@0: } sl@0: