1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _MEMORY_BYTE_GRABBER_HXX_
24 #define _MEMORY_BYTE_GRABBER_HXX_
25 
26 #include <com/sun/star/io/XInputStream.hpp>
27 #include <com/sun/star/io/XSeekable.hpp>
28 #include <string.h>
29 
30 class MemoryByteGrabber
31 {
32 protected:
33 	const com::sun::star::uno::Sequence < sal_Int8 > maBuffer;
34 	const sal_Int8 *mpBuffer;
35 	sal_Int32 mnCurrent, mnEnd;
36 public:
MemoryByteGrabber(const com::sun::star::uno::Sequence<sal_Int8> & rBuffer)37 	MemoryByteGrabber ( const com::sun::star::uno::Sequence < sal_Int8 > & rBuffer )
38 	: maBuffer ( rBuffer )
39 	, mpBuffer ( rBuffer.getConstArray() )
40 	, mnCurrent ( 0 )
41 	, mnEnd ( rBuffer.getLength() )
42 	{
43 	}
MemoryByteGrabber()44 	MemoryByteGrabber()
45 	{
46 	}
getCurrentPos()47 	const sal_Int8 * getCurrentPos () { return mpBuffer + mnCurrent; }
48 
49 	// XInputStream chained
readBytes(com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)50 	sal_Int32 SAL_CALL readBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
51 											sal_Int32 nBytesToRead )
52 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
53 	{
54 		if ( nBytesToRead < 0)
55 			throw com::sun::star::io::BufferSizeExceededException();
56 
57 		if (nBytesToRead + mnCurrent > mnEnd)
58 			nBytesToRead = mnEnd - mnCurrent;
59 
60 		aData.realloc ( nBytesToRead );
61 		rtl_copyMemory( aData.getArray(), mpBuffer + mnCurrent, nBytesToRead );
62 		mnCurrent += nBytesToRead;
63 		return nBytesToRead;
64 	}
65 
readSomeBytes(com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)66 	sal_Int32 SAL_CALL readSomeBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
67 													sal_Int32 nMaxBytesToRead )
68 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
69 	{
70 		return readBytes( aData, nMaxBytesToRead );
71 	}
skipBytes(sal_Int32 nBytesToSkip)72 	void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
73 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::BufferSizeExceededException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
74 	{
75 		mnCurrent += nBytesToSkip;
76 	}
available()77 	sal_Int32 SAL_CALL available(  )
78 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
79 	{
80 		return mnEnd - mnCurrent;
81 	}
closeInput()82 	void SAL_CALL closeInput(  )
83 		throw(com::sun::star::io::NotConnectedException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
84 	{
85 	}
86 
87 	// XSeekable chained...
seek(sal_Int64 location)88 	sal_Int64 SAL_CALL seek( sal_Int64 location )
89 		throw(com::sun::star::lang::IllegalArgumentException, com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
90 	{
91 		if ( location < 0 || location > mnEnd )
92 			throw com::sun::star::lang::IllegalArgumentException ();
93 		mnCurrent = static_cast < sal_Int32 > ( location );
94 		return mnCurrent;
95 	}
getPosition()96 	sal_Int64 SAL_CALL getPosition(  )
97 			throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
98 	{
99 		return mnCurrent;
100 	}
getLength()101 	sal_Int64 SAL_CALL getLength(  )
102 			throw(com::sun::star::io::IOException, com::sun::star::uno::RuntimeException)
103 	{
104 		return mnEnd;
105 	}
operator >>(sal_Int8 & rInt8)106 	MemoryByteGrabber& operator >> (sal_Int8& rInt8)
107 	{
108 		if (mnCurrent + 1 > mnEnd )
109 			rInt8 = 0;
110 		else
111 			rInt8 = mpBuffer [mnCurrent++] & 0xFF;
112 		return *this;
113 	}
operator >>(sal_Int16 & rInt16)114 	MemoryByteGrabber& operator >> (sal_Int16& rInt16)
115 	{
116 		if (mnCurrent + 2 > mnEnd )
117 			rInt16 = 0;
118 		else
119 		{
120 			rInt16  =   mpBuffer[mnCurrent++] & 0xFF;
121 			rInt16 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
122 		}
123 		return *this;
124 	}
operator >>(sal_Int32 & rInt32)125 	MemoryByteGrabber& operator >> (sal_Int32& rInt32)
126 	{
127 		if (mnCurrent + 4 > mnEnd )
128 			rInt32 = 0;
129 		else
130 		{
131 			rInt32  =   mpBuffer[mnCurrent++] & 0xFF;
132 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 8;
133 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 16;
134 			rInt32 |= ( mpBuffer[mnCurrent++] & 0xFF ) << 24;
135 		}
136 		return *this;
137 	}
138 
operator >>(sal_uInt8 & rInt8)139 	MemoryByteGrabber& operator >> (sal_uInt8& rInt8)
140 	{
141 		if (mnCurrent + 1 > mnEnd )
142 			rInt8 = 0;
143 		else
144 			rInt8 = mpBuffer [mnCurrent++] & 0xFF;
145 		return *this;
146 	}
operator >>(sal_uInt16 & rInt16)147 	MemoryByteGrabber& operator >> (sal_uInt16& rInt16)
148 	{
149 		if (mnCurrent + 2 > mnEnd )
150 			rInt16 = 0;
151 		else
152 		{
153 			rInt16  =   mpBuffer [mnCurrent++] & 0xFF;
154 			rInt16 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
155 		}
156 		return *this;
157 	}
operator >>(sal_uInt32 & rInt32)158 	MemoryByteGrabber& operator >> (sal_uInt32& rInt32)
159 	{
160 		if (mnCurrent + 4 > mnEnd )
161 			rInt32 = 0;
162 		else
163 		{
164 			rInt32  =   mpBuffer [mnCurrent++] & 0xFF;
165 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 8;
166 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 16;
167 			rInt32 |= ( mpBuffer [mnCurrent++] & 0xFF ) << 24;
168 		}
169 		return *this;
170 	}
171 };
172 
173 #endif
174