File: mrwcontainer.cpp

package info (click to toggle)
libopenraw 0.1.2-0.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,484 kB
  • sloc: cpp: 13,907; sh: 4,090; ansic: 1,922; xml: 769; makefile: 676; perl: 42
file content (233 lines) | stat: -rw-r--r-- 6,799 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * libopenraw - mrwcontainer.cpp
 *
 * Copyright (C) 2006-2017 Hubert Figuière
 * Copyright (C) 2008 Bradley Broom
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <fcntl.h>
#include <stddef.h>

#include <libopenraw/debug.h>

#include "trace.hpp"
#include "mrwcontainer.hpp"

using namespace Debug;

namespace OpenRaw {
namespace Internals {

namespace MRW {

DataBlock::DataBlock(off_t start, MRWContainer *_container)
    : m_start(start), m_container(_container), m_loaded(false)
{
    LOGDBG2("> DataBlock start == %ld\n", start);
    if (m_container->fetchData(m_name, m_start, 4) != 4) {
        // FIXME: Handle error
        LOGWARN("  Error reading block name %ld\n", start);
        return;
    }
    auto result = m_container->readInt32(m_container->file());
    if (result.empty()) {
        // FIXME: Handle error
        LOGWARN("  Error reading block length %ld\n", start);
        return;
    }
    m_length = result.unwrap();
    LOGDBG1("  DataBlock %s, length %d at %ld\n", name().c_str(), m_length, m_start);
    LOGDBG2("< DataBlock\n");
    m_loaded = true;
}

Option<int8_t>
DataBlock::int8_val(off_t off)
{
    MRWContainer *mc = m_container;
    mc->file()->seek(m_start + DataBlockHeaderLength + off, SEEK_SET);
    return mc->readInt8(mc->file());
}

Option<uint8_t>
DataBlock::uint8_val(off_t off)
{
    MRWContainer *mc = m_container;
    mc->file()->seek(m_start + DataBlockHeaderLength + off, SEEK_SET);
    return  mc->readUInt8(mc->file());
}

Option<uint16_t>
DataBlock::uint16_val(off_t off)
{
    MRWContainer *mc = m_container;
    mc->file()->seek(m_start + DataBlockHeaderLength + off, SEEK_SET);
    return mc->readUInt16(mc->file());
}

Option<std::string>
DataBlock::string_val(off_t off)
{
    char buf[9];
    size_t s;
    MRWContainer *mc = m_container;
    s = mc->fetchData(buf, m_start + DataBlockHeaderLength + off, 8);
    if (s != 8) {
        return Option<std::string>();
    }
    buf[8] = 0;
    return Option<std::string>(buf);
}

}

MRWContainer::MRWContainer(const IO::Stream::Ptr &_file, off_t _offset)
    : IfdFileContainer(_file, _offset)
{
}

MRWContainer::~MRWContainer()
{
}

IfdFileContainer::EndianType MRWContainer::isMagicHeader(const char *p, int len)
{
    if (len < 4) {
        // we need at least 4 bytes to check
        return ENDIAN_NULL;
    }

    if ((p[0] == 0x00) && (p[1] == 'M') && (p[2] == 'R') && (p[3] == 'M')) {

        LOGDBG1("Identified MRW file\n");

        return ENDIAN_BIG;
    }

    LOGDBG1("Unidentified MRW file\n");

    return ENDIAN_NULL;
}

bool MRWContainer::locateDirsPreHook()
{
    char version[9];
    off_t position;

    LOGDBG1("> MRWContainer::locateDirsPreHook()\n");
    m_endian = ENDIAN_BIG;

    /* MRW file always starts with an MRM datablock. */
    mrm = std::make_shared<MRW::DataBlock>(m_offset, this);
    if (mrm->name() != "MRM") {
        LOGWARN("MRW file begins not with MRM block, "
                "but with unrecognized DataBlock :: name == %s\n",
                mrm->name().c_str());
        return false;
    }

    /* Subblocks are contained within the MRM block. Scan them and create
     * appropriate block descriptors.
     */
    position = mrm->offset() + MRW::DataBlockHeaderLength;
    while (position < pixelDataOffset()) {
        auto ref = std::make_shared<MRW::DataBlock>(position, this);
        LOGDBG1("Loaded DataBlock :: name == %s\n", ref->name().c_str());
        if (!ref || !ref->loaded()) {
            break;
        }
        if (ref->name() == "PRD") {
            if (prd) {
                LOGWARN("File contains duplicate DataBlock :: name == %s\n",
                        ref->name().c_str());
            }
            prd = ref;
        } else if (ref->name() == "TTW") {
            if (ttw) {
                LOGWARN("File contains duplicate DataBlock :: name == %s\n",
                        ref->name().c_str());
            }
            ttw = ref;
        } else if (ref->name() == "WBG") {
            if (wbg) {
                LOGWARN("File contains duplicate DataBlock :: name == %s\n",
                        ref->name().c_str());
            }
            wbg = ref;
        } else if (ref->name() == "RIF") {
            if (rif) {
                LOGWARN("File contains duplicate DataBlock :: name == %s\n",
                        ref->name().c_str());
            }
            rif = ref;
        } else if (ref->name() != "PAD") {
            LOGWARN("File contains unrecognized DataBlock :: name == %s\n",
                    ref->name().c_str());
        }
        position = ref->offset() + MRW::DataBlockHeaderLength + ref->length();
    }

    /* Check that we found all the expected data blocks. */
    if (!prd) {
        LOGWARN("File does NOT contain expected DataBlock :: name == PRD\n");
        return false;
    }
    if (!ttw) {
        LOGWARN("File does NOT contain expected DataBlock :: name == TTW\n");
        return false;
    }
    if (!wbg) {
        LOGWARN("File does NOT contain expected DataBlock :: name == WBG\n");
        return false;
    }
    if (!rif) {
        LOGWARN("File does NOT contain expected DataBlock :: name == RIF\n");
        return false;
    }

    /* Extract the file version string. */
    if (fetchData(version,
                  prd->offset() + MRW::DataBlockHeaderLength + MRW::PRD_VERSION,
                  8) != 8) {
        // FIXME: Handle error
        LOGDBG1("  Error reading version string\n");
    }
    version[8] = '\0';
    m_version = std::string(version);
    LOGDBG1("  MRW file version == %s\n", m_version.c_str());

    /* For the benefit of our parent class, set the container offset to the
     * beginning of
     * the TIFF data (the contents of the TTW data block), and seek there.
     */
    m_offset = ttw->offset() + MRW::DataBlockHeaderLength;

    // TODO: Not sure exactly here the origin of this.
    // But it doesn't work.
    //  if((version[2] != '7') || (version[3] != '3')) {
    setExifOffsetCorrection(m_offset);
    LOGDBG1("setting correction to %ld\n", m_offset);
    //  }

    m_file->seek(m_offset, SEEK_SET);
    LOGDBG1("< MRWContainer\n");

    return true;
}

}
}