Blender  V2.93
Stream.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <Stream.h>
22 
23 #include <cstdio> /* printf */
24 #include <cstring> /* memcpy */
25 
26 static const char *msg_error_seek = "DDS: trying to seek beyond end of stream (corrupt file?)";
27 static const char *msg_error_read = "DDS: trying to read beyond end of stream (corrupt file?)";
28 
29 inline bool is_read_within_bounds(const Stream &mem, unsigned int cnt)
30 {
31  if (mem.pos >= mem.size) {
32  /* No more data remained in the memory buffer. */
33  return false;
34  }
35 
36  if (cnt > mem.size - mem.pos) {
37  /* Reading past the memory bounds. */
38  return false;
39  }
40 
41  return true;
42 }
43 
44 unsigned int Stream::seek(unsigned int p)
45 {
46  if (p > size) {
48  }
49  else {
50  pos = p;
51  }
52 
53  return pos;
54 }
55 
56 unsigned int mem_read(Stream &mem, unsigned long long &i)
57 {
58  if (!is_read_within_bounds(mem, 8)) {
60  return 0;
61  }
62  memcpy(&i, mem.mem + mem.pos, 8); /* @@ todo: make sure little endian */
63  mem.pos += 8;
64  return 8;
65 }
66 
67 unsigned int mem_read(Stream &mem, unsigned int &i)
68 {
69  if (!is_read_within_bounds(mem, 4)) {
71  return 0;
72  }
73  memcpy(&i, mem.mem + mem.pos, 4); /* @@ todo: make sure little endian */
74  mem.pos += 4;
75  return 4;
76 }
77 
78 unsigned int mem_read(Stream &mem, unsigned short &i)
79 {
80  if (!is_read_within_bounds(mem, 2)) {
82  return 0;
83  }
84  memcpy(&i, mem.mem + mem.pos, 2); /* @@ todo: make sure little endian */
85  mem.pos += 2;
86  return 2;
87 }
88 
89 unsigned int mem_read(Stream &mem, unsigned char &i)
90 {
91  if (!is_read_within_bounds(mem, 1)) {
93  return 0;
94  }
95  i = (mem.mem + mem.pos)[0];
96  mem.pos += 1;
97  return 1;
98 }
99 
100 unsigned int mem_read(Stream &mem, unsigned char *i, unsigned int cnt)
101 {
102  if (!is_read_within_bounds(mem, cnt)) {
104  return 0;
105  }
106  memcpy(i, mem.mem + mem.pos, cnt);
107  mem.pos += cnt;
108  return cnt;
109 }
110 
111 void Stream::set_failed(const char *msg)
112 {
113  if (!failed) {
114  puts(msg);
115  failed = true;
116  }
117 }
static const char * msg_error_read
Definition: Stream.cpp:27
bool is_read_within_bounds(const Stream &mem, unsigned int cnt)
Definition: Stream.cpp:29
unsigned int mem_read(Stream &mem, unsigned long long &i)
Definition: Stream.cpp:56
static const char * msg_error_seek
Definition: Stream.cpp:26
Definition: Stream.h:25
bool failed
Definition: Stream.h:29
void set_failed(const char *msg)
Definition: Stream.cpp:111
unsigned int seek(unsigned int p)
Definition: Stream.cpp:44
unsigned int size
Definition: Stream.h:27
unsigned int pos
Definition: Stream.h:28
unsigned char * mem
Definition: Stream.h:26