1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42/** @file MemoryIOWrapper.h
43 * Handy IOStream/IOSystem implemetation to read directly from a memory buffer */
44#ifndef AI_MEMORYIOSTREAM_H_INC
45#define AI_MEMORYIOSTREAM_H_INC
46
47#include <assimp/IOStream.hpp>
48#include <assimp/IOSystem.hpp>
49#include <assimp/ai_assert.h>
50#include <stdint.h>
51
52namespace Assimp {
53#define AI_MEMORYIO_MAGIC_FILENAME "$$$___magic___$$$"
54#define AI_MEMORYIO_MAGIC_FILENAME_LENGTH 17
55
56// ----------------------------------------------------------------------------------
57/** Implementation of IOStream to read directly from a memory buffer */
58// ----------------------------------------------------------------------------------
59class MemoryIOStream : public IOStream
60{
61 //friend class MemoryIOSystem;
62public:
63 MemoryIOStream (const uint8_t* buff, size_t len, bool own = false)
64 : buffer (buff)
65 , length(len)
66 , pos((size_t)0)
67 , own(own)
68 {
69 }
70
71public:
72
73 ~MemoryIOStream () {
74 if(own) {
75 delete[] buffer;
76 }
77 }
78
79 // -------------------------------------------------------------------
80 // Read from stream
81 size_t Read(void* pvBuffer, size_t pSize, size_t pCount) {
82 const size_t cnt = std::min(pCount,(length-pos)/pSize),ofs = pSize*cnt;
83
84 memcpy(pvBuffer,buffer+pos,ofs);
85 pos += ofs;
86
87 return cnt;
88 }
89
90 // -------------------------------------------------------------------
91 // Write to stream
92 size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/,size_t /*pCount*/) {
93 ai_assert(false); // won't be needed
94 return 0;
95 }
96
97 // -------------------------------------------------------------------
98 // Seek specific position
99 aiReturn Seek(size_t pOffset, aiOrigin pOrigin) {
100 if (aiOrigin_SET == pOrigin) {
101 if (pOffset >= length) {
102 return AI_FAILURE;
103 }
104 pos = pOffset;
105 }
106 else if (aiOrigin_END == pOrigin) {
107 if (pOffset >= length) {
108 return AI_FAILURE;
109 }
110 pos = length-pOffset;
111 }
112 else {
113 if (pOffset+pos >= length) {
114 return AI_FAILURE;
115 }
116 pos += pOffset;
117 }
118 return AI_SUCCESS;
119 }
120
121 // -------------------------------------------------------------------
122 // Get current seek position
123 size_t Tell() const {
124 return pos;
125 }
126
127 // -------------------------------------------------------------------
128 // Get size of file
129 size_t FileSize() const {
130 return length;
131 }
132
133 // -------------------------------------------------------------------
134 // Flush file contents
135 void Flush() {
136 ai_assert(false); // won't be needed
137 }
138
139private:
140 const uint8_t* buffer;
141 size_t length,pos;
142 bool own;
143};
144
145// ---------------------------------------------------------------------------
146/** Dummy IO system to read from a memory buffer */
147class MemoryIOSystem : public IOSystem
148{
149public:
150 /** Constructor. */
151 MemoryIOSystem (const uint8_t* buff, size_t len)
152 : buffer (buff), length(len) {
153 }
154
155 /** Destructor. */
156 ~MemoryIOSystem() {
157 }
158
159 // -------------------------------------------------------------------
160 /** Tests for the existence of a file at the given path. */
161 bool Exists( const char* pFile) const {
162 return !strncmp(pFile,AI_MEMORYIO_MAGIC_FILENAME,AI_MEMORYIO_MAGIC_FILENAME_LENGTH);
163 }
164
165 // -------------------------------------------------------------------
166 /** Returns the directory separator. */
167 char getOsSeparator() const {
168 return '/'; // why not? it doesn't care
169 }
170
171 // -------------------------------------------------------------------
172 /** Open a new file with a given path. */
173 IOStream* Open( const char* pFile, const char* /*pMode*/ = "rb") {
174 if (strncmp(pFile,AI_MEMORYIO_MAGIC_FILENAME,AI_MEMORYIO_MAGIC_FILENAME_LENGTH)) {
175 return NULL;
176 }
177 return new MemoryIOStream(buffer,length);
178 }
179
180 // -------------------------------------------------------------------
181 /** Closes the given file and releases all resources associated with it. */
182 void Close( IOStream* pFile) {
183 delete pFile;
184 }
185
186 // -------------------------------------------------------------------
187 /** Compare two paths */
188 bool ComparePaths (const char* /*one*/, const char* /*second*/) const {
189 return false;
190 }
191
192private:
193 const uint8_t* buffer;
194 size_t length;
195};
196} // end namespace Assimp
197
198#endif
199