/src/librevenge/src/lib/RVNGMemoryStream.cpp
Line | Count | Source |
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 (wrlach@gmail.com) |
11 | | * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch) |
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 | 5.68M | RVNGInputStream(), |
28 | 5.68M | m_offset(0), |
29 | 5.68M | m_size(size), |
30 | 5.68M | m_data(data) |
31 | 5.68M | { |
32 | 5.68M | } |
33 | | |
34 | | RVNGMemoryInputStream::~RVNGMemoryInputStream() |
35 | 5.68M | { |
36 | 5.68M | } |
37 | | |
38 | | const unsigned char *RVNGMemoryInputStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
39 | 183M | { |
40 | 183M | numBytesRead = 0; |
41 | | |
42 | 183M | if (numBytes == 0) |
43 | 1.74k | return nullptr; |
44 | | |
45 | 183M | long numBytesToRead; |
46 | | |
47 | 183M | if (m_offset+long(numBytes) < long(m_size)) |
48 | 182M | numBytesToRead = long(numBytes); |
49 | 1.38M | else |
50 | 1.38M | numBytesToRead = long(m_size) - long(m_offset); |
51 | | |
52 | 183M | numBytesRead = (unsigned long) numBytesToRead; // about as paranoid as we can be.. |
53 | | |
54 | 183M | if (numBytesToRead == 0) |
55 | 6.01k | return nullptr; |
56 | | |
57 | 183M | long oldOffset = m_offset; |
58 | 183M | m_offset += numBytesToRead; |
59 | | |
60 | 183M | return &m_data[oldOffset]; |
61 | 183M | } |
62 | | |
63 | | int RVNGMemoryInputStream::seek(long offset, RVNG_SEEK_TYPE seekType) |
64 | 60.7M | { |
65 | 60.7M | if (seekType == RVNG_SEEK_CUR) |
66 | 2.22M | m_offset += offset; |
67 | 58.5M | else if (seekType == RVNG_SEEK_SET) |
68 | 52.9M | m_offset = offset; |
69 | 5.62M | else if (seekType == RVNG_SEEK_END) |
70 | 5.62M | m_offset = (long)m_size+offset; |
71 | | |
72 | 60.7M | if (m_offset < 0) |
73 | 469 | { |
74 | 469 | m_offset = 0; |
75 | 469 | return -1; |
76 | 469 | } |
77 | 60.7M | if ((long)m_offset > (long)m_size) |
78 | 321k | { |
79 | 321k | m_offset = (long) m_size; |
80 | 321k | return -1; |
81 | 321k | } |
82 | | |
83 | 60.4M | return 0; |
84 | 60.7M | } |
85 | | |
86 | | long RVNGMemoryInputStream::tell() |
87 | 193M | { |
88 | 193M | return m_offset; |
89 | 193M | } |
90 | | |
91 | | bool RVNGMemoryInputStream::isEnd() |
92 | 191M | { |
93 | 191M | if ((long)m_offset == (long)m_size) |
94 | 103k | return true; |
95 | | |
96 | 191M | return false; |
97 | 191M | } |
98 | | |
99 | | } |
100 | | |
101 | | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |