libopenraw
memstream.cpp
1/*
2 * libopenraw - memstream.cpp
3 *
4 * Copyright (C) 2007-2017 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
31using namespace Debug;
32
33namespace OpenRaw {
34namespace IO {
35
36MemStream::MemStream(void *ptr, size_t s)
37 : Stream(""),
38 m_ptr(ptr),
39 m_size(s),
40 m_current(nullptr)
41{
42}
43
44or_error MemStream::open()
45{
46 m_current = (unsigned char *)m_ptr;
47 return OR_ERROR_NONE;
48}
49
50
51int MemStream::close()
52{
53 m_current = NULL;
54 return 0;
55}
56
57int MemStream::seek(off_t offset, int whence)
58{
59 int newpos = 0;
60// LOGDBG1("MemStream::seek %ld bytes - whence = %d", offset, whence);
61 // TODO check bounds
62 if (m_current == nullptr) {
63 // TODO set error code
64 return -1;
65 }
66 switch(whence)
67 {
68 case SEEK_SET:
69 m_current = (unsigned char*)m_ptr + offset;
70 newpos = offset;
71 break;
72 case SEEK_END:
73 m_current = (unsigned char*)m_ptr + m_size + offset;
74 newpos = m_size + offset;
75 break;
76 case SEEK_CUR:
77 m_current += offset;
78 newpos = (m_current - (unsigned char*)m_ptr);
79 break;
80 default:
81 return -1;
82 break;
83 }
84 return newpos;
85}
86
87
88int MemStream::read(void *buf, size_t count)
89{
90 if((m_current == nullptr) || (m_ptr == nullptr)) {
91 LOGDBG1("MemStream::failed\n");
92 return -1;
93 }
94
95 unsigned char * end = (unsigned char*)m_ptr + m_size;
96 if((off_t)count > (end - m_current)) {
97 count = end - m_current;
98 // TODO set EOF
99 }
100 memcpy(buf, m_current, count);
101 m_current += count;
102 return count;
103}
104
105
106off_t MemStream::filesize()
107{
108 return m_size;
109}
110
111}
112}
113/*
114 Local Variables:
115 mode:c++
116 c-file-style:"stroustrup"
117 c-file-offsets:((innamespace . 0))
118 tab-width:2
119 c-basic-offset:2
120 indent-tabs-mode:nil
121 fill-column:80
122 End:
123*/
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard....
Definition: arwfile.cpp:30