1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 // -*- c++ -*- 19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21 // O S C L _ B I N _ S T R E A M 22 23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25 /*! \addtogroup osclutil OSCL Util 26 * 27 * @{ 28 */ 29 30 31 /*! 32 * \file oscl_bin_stream.h 33 * \brief Defines a set of binary stream classes which handle portable input / output of binary data regardless of the native byte order. 34 * 35 */ 36 37 38 /* 39 ** File: oscl_bin_stream.h 40 ** 41 ** Description: 42 ** This module defines a set of binary stream classes to provide portability of code accross 43 ** different platforms and compilers. Output and input stream classes are defined both for 44 ** little endian and big endian byte orders. These classes should be used when sharing 45 ** information across platforms (e.g. sending messages) instead of C structures. The classes 46 ** will read/write in the correct byte order regardless of the platform you are running on. 47 ** The classes are: 48 ** OsclBinIStreamBigEndian - Input stream for big endian byte order. 49 ** OsclBinIStreamLittleEndian - Input stream for little endian byte order. 50 ** OsclBinOStreamBigEndian - Output stream for big endian byte order. 51 ** OsclBinIStreamLittleEndian - Input stream for little endian byte order. 52 ** 53 ** The preprocessor defined constants BYTE_ORDER_BIG_ENDIAN, BYTE_ORDER_LITTLE_ENDIAN are defined 54 ** in oscl_base.h and tell this module the byte order used by the native platform. This is used 55 ** to decide if we can do a straight copy or we need to manipulate the field one byte at a time. 56 ** The preprocessor defined constant INTEGERS_BYTE_ALIGNED tells us if the platform supports 57 ** accessing integers at any address. 58 */ 59 #ifndef OSCL_BIN_STREAM_H_INCLUDED 60 #define OSCL_BIN_STREAM_H_INCLUDED 61 62 /* 63 ** Includes 64 */ 65 #ifndef OSCL_BASE_H_INCLUDED 66 #include "oscl_base.h" 67 #endif 68 69 70 71 /* 72 ** Classes 73 */ 74 75 class OsclBinStream 76 { 77 public: OsclBinStream()78 OsclBinStream() 79 : state(GOOD_STATE), 80 pBasePosition(0), 81 pPosition(0) 82 { 83 } 84 85 //! This method determines if the stream is ok. 86 /*! 87 \return true if stream is ok. 88 */ 89 bool good(); 90 91 //! This method determines if end of stream has been reached 92 /*! 93 \return true if end of stream has been reached. 94 */ 95 bool eof(); 96 97 //! This method determines if an error has occured in the stream 98 /*! 99 ** \return true if an error occured in the stream. 100 */ 101 bool fail(); 102 103 104 //! This methods specifies the data buffer to attach to the stream. 105 /*! 106 \param buffer will provide the input 107 \param length of the buffer 108 */ 109 void Attach(void * buffer, uint32 l_length); 110 111 //! This method specifies the memory fragment array to use for input. 112 /*! 113 This array should remain static while the stream refers to it. 114 \param numFragments is the number of elements in the array 115 \param fragPtr is the pointer to the MemoryFragment array 116 */ 117 void Attach(const uint32 numFragments, const OsclMemoryFragment * fragPtr); 118 119 120 //! This method returns the current stream position 121 /*! 122 This method is to be used if the input stream is a pointer to the MemoryFragment array 123 ** \return Stream position. 124 */ 125 uint32 tellg(); 126 127 //! This method seeks to the specified stream position. 128 /*! 129 \return Stream position. 130 */ 131 void Seek(uint32 absPosition); 132 133 //! This method returns the current stream position. 134 /*! 135 \returns stream position. 136 */ 137 uint32 PositionInBlock(); 138 139 140 //! This method seeks to the specified offset from the current location 141 /*! 142 \param offset from current stream location 143 */ 144 void seekFromCurrentPosition(int32 offset); 145 146 147 148 protected: 149 150 /* 151 ** Description: 152 ** Reserves space for storing/reading. If no space is available it sets the stream state 153 ** to fail and returns false. If the end of the stream has been reached, it sets the state 154 ** to end of stream. 155 ** 156 ** Returns: true if space was successfully reserved. 157 ** Side effects: None. 158 */ 159 bool ReserveSpace(uint32 size); 160 161 bool HaveRoomInCurrentBlock(uint32 size); 162 163 typedef enum 164 { 165 GOOD_STATE, 166 EOF_STATE, 167 FAIL_STATE 168 } state_t; 169 170 state_t state; 171 172 /* Position of the beginning of the data buffer */ 173 uint8 * pBasePosition; 174 175 /* Current data buffer position */ 176 uint8 * pPosition; 177 178 /* length of data buffer */ 179 uint32 length; 180 181 const OsclMemoryFragment * nextFragPtr; 182 int fragsLeft; 183 184 const OsclMemoryFragment * firstFragPtr; 185 int numFrags; 186 OsclMemoryFragment specialFragBuffer; 187 188 }; 189 190 /* 191 ** Class OsclBinIStream 192 ** This class implements the basic stream functions for an input stream. 193 */ 194 class OsclBinIStream : public OsclBinStream 195 { 196 private: 197 public: 198 /* 199 ** Description: 200 ** Constructor. 201 ** 202 ** Returns: None. 203 ** Side effects: None. 204 */ OsclBinIStream()205 OsclBinIStream() 206 { 207 } 208 209 /* 210 ** Description: 211 ** Destructor. 212 ** 213 ** Returns: None. 214 ** Side effects: None. 215 */ ~OsclBinIStream()216 ~OsclBinIStream() 217 { 218 } 219 220 //! This method reads an unsigned short from the stream. 221 /*! 222 ** \returns Unsigned short read from the stream. 223 */ 224 uint8 Read_uint8(); 225 226 //! This method reads 'length' number of bytes from the stream and places them in 'data'. 227 /*! 228 \param data is a pointer to the place to store the bytes read 229 \param size is the number of bytes to read 230 */ 231 OsclBinIStream & get( 232 int8 * data, 233 int32 size 234 ); 235 236 protected: 237 238 239 }; 240 241 /* 242 ** Class OsclBinIStreamLittleEndian 243 ** This class implements a binary input stream using little endian byte ordering 244 */ 245 class OsclBinIStreamLittleEndian : public OsclBinIStream 246 { 247 public: OsclBinIStreamLittleEndian()248 OsclBinIStreamLittleEndian() 249 { 250 } 251 252 //! This method reads a int8 from the stream and stores it in 'data'. 253 OsclBinIStreamLittleEndian & operator>>(int8 & data); 254 255 //!This method reads a uint8 from the stream and stores it in 'data'. 256 OsclBinIStreamLittleEndian & operator>>(uint8 & data); 257 258 //! This method reads a int16 from the stream and stores it in 'data'. 259 OsclBinIStreamLittleEndian & operator>>(int16 & data); 260 261 //! This method reads a uint16 from the stream and stores it in 'data'. 262 OsclBinIStreamLittleEndian & operator>>(uint16 & data); 263 264 //! This method reads a int32 from the stream and stores it in 'data'. 265 OsclBinIStreamLittleEndian & operator>>(int32 & data); 266 267 //! This method reads a uint32 from the stream and stores it in 'data'. 268 OsclBinIStreamLittleEndian & operator>>(uint32 & data); 269 270 protected: 271 272 /* 273 ** Description: 274 ** Reads an unsigned short from the stream. 275 ** 276 ** Returns: Unsigned short read from the stream. 277 ** Side effects: None. 278 */ 279 uint16 Read_uint16(); 280 /* 281 ** Description: 282 ** Reads an unsigned long from the stream. 283 ** 284 ** Returns: Unsigned long read from the stream. 285 ** Side effects: None. 286 */ 287 uint32 Read_uint32(); 288 }; 289 290 291 /* 292 ** Class OsclBinIStreamBigEndian 293 ** This class implements a binary input stream using big endian byte ordering 294 */ 295 class OsclBinIStreamBigEndian : public OsclBinIStream 296 { 297 public: OsclBinIStreamBigEndian()298 OsclBinIStreamBigEndian() 299 { 300 } 301 302 void Read(int8 & data); 303 304 void Read(uint8 & data); 305 306 void Read(int16 & data); 307 308 void Read(uint16 & data); 309 310 void Read(int32 & data); 311 312 void Read(uint32 & data); 313 314 //! This method reads a int8 from the stream and stores it in 'data'. 315 OsclBinIStreamBigEndian & operator>>(int8 & data); 316 317 //! This method reads a uint8 from the stream and stores it in 'data'. 318 OsclBinIStream & operator>>(uint8 & data); 319 320 //! This method reads a int16 from the stream and stores it in 'data'. 321 OsclBinIStreamBigEndian & operator>>(int16 & data); 322 323 //! This method reads a uint16 from the stream and stores it in 'data'. 324 OsclBinIStreamBigEndian & operator>>(uint16 & data); 325 326 //! This method reads a int32 from the stream and stores it in 'data'. 327 OsclBinIStreamBigEndian & operator>>(int32 & data); 328 329 //! This method reads a uint32 from the stream and stores it in 'data'. 330 OsclBinIStreamBigEndian & operator>>(uint32 & data); 331 332 //! This method reads an unsigned short from the stream. 333 /*! 334 \return Unsigned short read from the stream. 335 */ 336 uint16 Read_uint16(); 337 338 //! This method reads an unsigned long from the stream. 339 /*! 340 \return unsigned long read from the stream. 341 */ 342 uint32 Read_uint32(); 343 protected: 344 345 }; 346 347 348 //! Class OsclBinOStream implements the basic stream functions for an output stream. 349 class OsclBinOStream : public OsclBinStream 350 { 351 public: OsclBinOStream()352 OsclBinOStream() 353 { 354 } 355 ~OsclBinOStream()356 virtual ~OsclBinOStream() 357 { 358 } 359 360 //! This method writes 'length' number of bytes stored in 'data' to the stream. 361 OsclBinOStream & write( 362 const int8 * data, /* data to store */ 363 int32 size /* length of data to store */ 364 ); 365 366 protected: 367 }; 368 369 //! Class OsclBinOStreamLittleEndian implements a binary output stream using little endian byte ordering 370 class OsclBinOStreamLittleEndian : public OsclBinOStream 371 { 372 public: OsclBinOStreamLittleEndian()373 OsclBinOStreamLittleEndian() 374 { 375 } 376 377 //! This method writes a int8 from 'data' to the stream. 378 OsclBinOStreamLittleEndian & operator<<(const int8 & data); 379 380 //!This method writes a uint8 from 'data' to the stream. 381 OsclBinOStreamLittleEndian & operator<<(const uint8 & data); 382 383 //! This method writes a int16 from 'data' to the stream. 384 OsclBinOStreamLittleEndian & operator<<(const int16 & data); 385 386 //! This method writes a uint16 from 'data' to the stream. 387 OsclBinOStreamLittleEndian & operator<<(const uint16 & data); 388 389 //! This method writes a int32 from 'data' to the stream. 390 OsclBinOStreamLittleEndian & operator<<(const int32 & data); 391 392 //! This method writes a uint32 from 'data' to the stream. 393 OsclBinOStreamLittleEndian & operator<<(const uint32 & data); 394 395 protected: 396 397 //! This method writes 'data' (unsigned short) to the stream. 398 void WriteUnsignedShort(const uint16 data); 399 400 //! This method writes 'data' (unsigned long) to the stream. 401 void WriteUnsignedLong(const uint32 data); 402 403 }; 404 405 406 //! Class OsclBinOStreamBigEndian implements a binary output stream using big endian byte ordering 407 class OsclBinOStreamBigEndian : public OsclBinOStream 408 { 409 public: OsclBinOStreamBigEndian()410 OsclBinOStreamBigEndian() 411 { 412 } 413 414 //! This method writes a int8 from 'data' to the stream. 415 OsclBinOStreamBigEndian & operator<<(const int8 & data); 416 417 //! This method writes a uint8 from 'data' to the stream. 418 OsclBinOStreamBigEndian & operator<<(const uint8 & data); 419 420 //! This method writes a int16 from 'data' to the stream. 421 OsclBinOStreamBigEndian & operator<<(const int16 & data); 422 423 //! This method writes a uint16 from 'data' to the stream. 424 OsclBinOStreamBigEndian & operator<<(const uint16 & data); 425 426 //! This method writes a int32 from 'data' to the stream. 427 OsclBinOStreamBigEndian & operator<<(const int32 & data); 428 429 //! This method writes a uint32 from 'data' to the stream. 430 OsclBinOStreamBigEndian & operator<<(const uint32 & data); 431 432 protected: 433 434 /* 435 ** Description: 436 ** Writes 'data' (unsigned short) to the stream. 437 ** 438 ** Returns: None. 439 ** Side effects: None. 440 */ 441 void WriteUnsignedShort(const uint16 data); 442 443 /* 444 ** Description: 445 ** Writes 'data' (unsigned long) to the stream. 446 ** 447 ** Returns: None. 448 ** Side effects: None. 449 */ 450 void WriteUnsignedLong(const uint32 data); 451 }; 452 453 #if (!OSCL_DISABLE_INLINES) 454 #include "oscl_bin_stream.inl" 455 #endif 456 457 #endif 458 459 /*! @} */ 460