RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
MMapReader.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2025 Roman Lebedev
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
8 License as published by the Free Software Foundation; either
9 version 2 of 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, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#if !defined(_WIN32)
22
23#include "io/MMapReader.h"
24#include "adt/Array1DRef.h"
25#include "adt/Casts.h"
26#include "io/Buffer.h"
27#include "io/FileIOException.h"
28#include <cstdint>
29#include <fcntl.h>
30#include <limits>
31#include <string>
32#include <sys/mman.h>
33#include <sys/stat.h>
34#include <unistd.h>
35
36namespace rawspeed {
37
38MMapReader::MMapReader(const std::string& fname)
39 : fd(open(fname.c_str(), O_RDONLY)) {
40
41 if (fd == -1)
42 ThrowFIE("Could not open file \"%s\".", fname.c_str());
43
44 struct stat sb;
45 if (fstat(fd, &sb) == -1)
46 ThrowFIE("Could not obtain the file size");
47
48 length = sb.st_size;
49
50 addr = mmap(nullptr, length, PROT_READ, MAP_PRIVATE, fd, 0);
51 if (addr == MAP_FAILED)
52 ThrowFIE("Could not mmap the file");
53}
54
56 if (static_cast<int64_t>(length) >
57 std::numeric_limits<Buffer::size_type>::max())
58 ThrowFIE("File is too big (%zu bytes).", length);
59 return Array1DRef(static_cast<const uint8_t*>(addr),
61}
62
64 munmap(addr, length);
65 close(fd);
66}
67
68} // namespace rawspeed
69
70#endif // !defined(_WIN32)
#define ThrowFIE(...)
Buffer getAsBuffer() const
MMapReader(const std::string &fname)
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
Array1DRef(T *data_, int numElts_) -> Array1DRef< T >