| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2004-2005 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2006 Fridrich Strba ([email protected]) |
| 12 | * |
| 13 | * For minor contributions see the git repository. |
| 14 | * |
| 15 | * Alternatively, the contents of this file may be used under the terms |
| 16 | * of the GNU Lesser General Public License Version 2.1 or later |
| 17 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 18 | * applicable instead of those above. |
| 19 | */ |
| 20 | |
| 21 | #include "RVNGMemoryStream.h" |
| 22 | |
| 23 | namespace librevenge |
| 24 | { |
| 25 | |
| 26 | RVNGMemoryInputStream::RVNGMemoryInputStream(unsigned char *data, unsigned long size) : |
| 27 | RVNGInputStream(), |
| 28 | m_offset(0), |
| 29 | m_size(size), |
| 30 | m_data(data) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | RVNGMemoryInputStream::~RVNGMemoryInputStream() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | const unsigned char *RVNGMemoryInputStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
| 39 | { |
| 40 | numBytesRead = 0; |
| 41 | |
| 42 | if (numBytes == 0) |
| 43 | return nullptr; |
| 44 | |
| 45 | long numBytesToRead; |
| 46 | |
| 47 | if (m_offset+long(numBytes) < long(m_size)) |
| 48 | numBytesToRead = long(numBytes); |
| 49 | else |
| 50 | numBytesToRead = long(m_size) - long(m_offset); |
| 51 | |
| 52 | numBytesRead = (unsigned long) numBytesToRead; // about as paranoid as we can be.. |
| 53 | |
| 54 | if (numBytesToRead == 0) |
| 55 | return nullptr; |
| 56 | |
| 57 | long oldOffset = m_offset; |
| 58 | m_offset += numBytesToRead; |
| 59 | |
| 60 | return &m_data[oldOffset]; |
| 61 | } |
| 62 | |
| 63 | int RVNGMemoryInputStream::seek(long offset, RVNG_SEEK_TYPE seekType) |
| 64 | { |
| 65 | if (seekType == RVNG_SEEK_CUR) |
| 66 | m_offset += offset; |
| 67 | else if (seekType == RVNG_SEEK_SET) |
| 68 | m_offset = offset; |
| 69 | else if (seekType == RVNG_SEEK_END) |
| 70 | m_offset = (long)m_size+offset; |
| 71 | |
| 72 | if (m_offset < 0) |
| 73 | { |
| 74 | m_offset = 0; |
| 75 | return -1; |
| 76 | } |
| 77 | if ((long)m_offset > (long)m_size) |
| 78 | { |
| 79 | m_offset = (long) m_size; |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | long RVNGMemoryInputStream::tell() |
| 87 | { |
| 88 | return m_offset; |
| 89 | } |
| 90 | |
| 91 | bool RVNGMemoryInputStream::isEnd() |
| 92 | { |
| 93 | if ((long)m_offset == (long)m_size) |
| 94 | return true; |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |