libopenraw
memstream.cpp
1 /*
2  * libopenraw - memstream.cpp
3  *
4  * Copyright (C) 2007-2016 Hubert Figuière
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #include <string.h>
23 #include <stdio.h>
24 
25 #include <libopenraw/consts.h>
26 #include <libopenraw/debug.h>
27 
28 #include "memstream.hpp"
29 #include "trace.hpp"
30 
31 using namespace Debug;
32 
33 namespace OpenRaw {
34 namespace IO {
35 
36 MemStream::MemStream(void *ptr, size_t s)
37  : Stream(""),
38  m_ptr(ptr),
39  m_size(s),
40  m_current(NULL)
41 {
42 }
43 
44 or_error MemStream::open()
45 {
46  m_current = (unsigned char *)m_ptr;
47  return OR_ERROR_NONE;
48 }
49 
50 
51 int MemStream::close()
52 {
53  m_current = NULL;
54  return 0;
55 }
56 
57 int MemStream::seek(off_t offset, int whence)
58 {
59  int newpos = 0;
60 // Trace(DEBUG1) << "MemStream::seek " << offset
61 // << " bytes - whence = "
62 // << whence << "\n";
63  // TODO check bounds
64  if (m_current == NULL) {
65  // TODO set error code
66  return -1;
67  }
68  switch(whence)
69  {
70  case SEEK_SET:
71  m_current = (unsigned char*)m_ptr + offset;
72  newpos = offset;
73  break;
74  case SEEK_END:
75  m_current = (unsigned char*)m_ptr + m_size + offset;
76  newpos = m_size + offset;
77  break;
78  case SEEK_CUR:
79  m_current += offset;
80  newpos = (m_current - (unsigned char*)m_ptr);
81  break;
82  default:
83  return -1;
84  break;
85  }
86  return newpos;
87 }
88 
89 
90 int MemStream::read(void *buf, size_t count)
91 {
92  if((m_current == NULL) || (m_ptr == NULL)) {
93  Trace(DEBUG1) << "MemStream::failed\n";
94  return -1;
95  }
96 
97  unsigned char * end = (unsigned char*)m_ptr + m_size;
98  if((off_t)count > (end - m_current)) {
99  count = end - m_current;
100  // TODO set EOF
101  }
102  memcpy(buf, m_current, count);
103  m_current += count;
104  return count;
105 }
106 
107 
108 off_t MemStream::filesize()
109 {
110  return m_size;
111 }
112 
113 }
114 }
115 /*
116  Local Variables:
117  mode:c++
118  c-file-style:"stroustrup"
119  c-file-offsets:((innamespace . 0))
120  tab-width:2
121  c-basic-offset:2
122  indent-tabs-mode:nil
123  fill-column:80
124  End:
125 */
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:30
Definition: trace.cpp:30