| Exiv2

Loading...
Searching...
No Matches
basicio.hpp
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#ifndef BASICIO_HPP_
4#define BASICIO_HPP_
5
6// *****************************************************************************
7#include "exiv2lib_export.h"
8
9// included header files
10#include "error.hpp"
11#include "types.hpp"
12
13// + standard includes
14#include <memory>
15
16// The way to handle data from stdin or data uri path. If EXV_XPATH_MEMIO = 1,
17// it uses MemIo. Otherwises, it uses FileIo.
18#ifndef EXV_XPATH_MEMIO
19#define EXV_XPATH_MEMIO 0
20#endif
21
22// *****************************************************************************
23// namespace extensions
24 namespace Exiv2 {
25// *****************************************************************************
26// class definitions
27
35 class EXIV2API BasicIo {
36 public:
38 using UniquePtr = std::unique_ptr<BasicIo>;
39
41 enum Position { beg, cur, end };
42
44
45
46 virtual ~BasicIo() = default;
48
50
51
63 virtual int open() = 0;
64
72 virtual int close() = 0;
82 virtual size_t write(const byte* data, size_t wcount) = 0;
92 virtual size_t write(BasicIo& src) = 0;
100 virtual int putb(byte data) = 0;
111 virtual DataBuf read(size_t rcount) = 0;
124 virtual size_t read(byte* buf, size_t rcount) = 0;
135 void readOrThrow(byte* buf, size_t rcount, ErrorCode err = ErrorCode::kerCorruptedMetadata);
142 virtual int getb() = 0;
156 virtual void transfer(BasicIo& src) = 0;
165 virtual int seek(int64_t offset, Position pos) = 0;
166
175 void seekOrThrow(int64_t offset, Position pos, ErrorCode err);
176
186 virtual byte* mmap(bool isWriteable = false) = 0;
193 virtual int munmap() = 0;
194
196
198
199
203 [[nodiscard]] virtual size_t tell() const = 0;
209 [[nodiscard]] virtual size_t size() const = 0;
211 [[nodiscard]] virtual bool isopen() const = 0;
213 [[nodiscard]] virtual int error() const = 0;
215 [[nodiscard]] virtual bool eof() const = 0;
221 [[nodiscard]] virtual const std::string& path() const noexcept = 0;
222
230 virtual void populateFakeData() = 0;
231
235 byte* bigBlock_{};
236
238}; // class BasicIo
239
246 class EXIV2API IoCloser {
247 public:
249
250
251 explicit IoCloser(BasicIo& bio) : bio_(bio) {
252 }
253
254 virtual ~IoCloser() {
255 close();
256 }
257
258
260
261
262 void close() {
263 if (bio_.isopen())
264 bio_.close();
265 }
266
267
268 // DATA
270 BasicIo& bio_;
271
272 // Not implemented
274 IoCloser(const IoCloser&) = delete;
276 IoCloser& operator=(const IoCloser&) = delete;
277}; // class IoCloser
278
279#ifdef EXV_ENABLE_FILESYSTEM
284 class EXIV2API FileIo : public BasicIo {
285 public:
287
288
294 explicit FileIo(const std::string& path);
295
297 ~FileIo() override;
299
301
302
315 int open(const std::string& mode);
323 int open() override;
330 int close() override;
340 size_t write(const byte* data, size_t wcount) override;
350 size_t write(BasicIo& src) override;
358 int putb(byte data) override;
369 DataBuf read(size_t rcount) override;
382 size_t read(byte* buf, size_t rcount) override;
389 int getb() override;
408 void transfer(BasicIo& src) override;
409
410 int seek(int64_t offset, Position pos) override;
411
423 byte* mmap(bool isWriteable = false) override;
431 int munmap() override;
435 virtual void setPath(const std::string& path);
436
438
440
444 [[nodiscard]] size_t tell() const override;
451 [[nodiscard]] size_t size() const override;
453 [[nodiscard]] bool isopen() const override;
455 [[nodiscard]] int error() const override;
457 [[nodiscard]] bool eof() const override;
459 [[nodiscard]] const std::string& path() const noexcept override;
460
468 void populateFakeData() override;
470
471 // NOT IMPLEMENTED
473 FileIo(const FileIo&) = delete;
475 FileIo& operator=(const FileIo&) = delete;
476
477 private:
478 // Pimpl idiom
479 class Impl;
480 std::unique_ptr<Impl> p_;
481
482}; // class FileIo
483#endif
484
497 class EXIV2API MemIo : public BasicIo {
498 public:
500
501
502 MemIo();
510 MemIo(const byte* data, size_t size);
512 ~MemIo() override;
514
516
517
523 int open() override;
528 int close() override;
539 size_t write(const byte* data, size_t wcount) override;
550 size_t write(BasicIo& src) override;
558 int putb(byte data) override;
569 DataBuf read(size_t rcount) override;
582 size_t read(byte* buf, size_t rcount) override;
589 int getb() override;
605 void transfer(BasicIo& src) override;
606
607 int seek(int64_t offset, Position pos) override;
608
617 byte* mmap(bool /*isWriteable*/ = false) override;
618 int munmap() override;
620
622
623
627 [[nodiscard]] size_t tell() const override;
633 [[nodiscard]] size_t size() const override;
635 [[nodiscard]] bool isopen() const override;
637 [[nodiscard]] int error() const override;
639 [[nodiscard]] bool eof() const override;
641 [[nodiscard]] const std::string& path() const noexcept override;
642
650 void populateFakeData() override;
651
653
654 // NOT IMPLEMENTED
656 MemIo(const MemIo&) = delete;
658 MemIo& operator=(const MemIo&) = delete;
659
660 private:
661 // Pimpl idiom
662 class Impl;
663 std::unique_ptr<Impl> p_;
664
665}; // class MemIo
666
670#if EXV_XPATH_MEMIO
671 class EXIV2API XPathIo : public MemIo {
672 public:
674
675
676 XPathIo(const std::string& path);
678 private:
683 void ReadStdin();
689 void ReadDataUri(const std::string& path);
690}; // class XPathIo
691#elif defined(EXV_ENABLE_FILESYSTEM)
692 class EXIV2API XPathIo : public FileIo {
693 public:
698 static constexpr auto TEMP_FILE_EXT = ".exiv2_temp";
703 static constexpr auto GEN_FILE_EXT = ".exiv2";
704
706
707
708 explicit XPathIo(const std::string& orgPath);
709
711 ~XPathIo() override;
713
714 XPathIo(const XPathIo&) = delete;
715 XPathIo& operator=(const XPathIo&) = delete;
716
718
719
723 void transfer(BasicIo& src) override;
724
726
728
729
735 static std::string writeDataToFile(const std::string& orgPath);
737
738 private:
739 // True if the file is a temporary file and it should be deleted in destructor.
740 bool isTemp_{true};
741 std::string tempFilePath_;
742}; // class XPathIo
743#endif
744
750 class EXIV2API RemoteIo : public BasicIo {
751 public:
753 RemoteIo();
754 ~RemoteIo() override;
756
757 RemoteIo(const RemoteIo&) = delete;
758 RemoteIo& operator=(const RemoteIo&) = delete;
759
761
762
771 int open() override;
772
778 int close() override;
783 size_t write(const byte* data, size_t wcount) override;
798 size_t write(BasicIo& src) override;
799
804 int putb(byte data) override;
817 DataBuf read(size_t rcount) override;
832 size_t read(byte* buf, size_t rcount) override;
841 int getb() override;
856 void transfer(BasicIo& src) override;
857
858 int seek(int64_t offset, Position pos) override;
859
864 byte* mmap(bool /*isWriteable*/ = false) override;
869 int munmap() override;
871
873
877 [[nodiscard]] size_t tell() const override;
883 [[nodiscard]] size_t size() const override;
885 [[nodiscard]] bool isopen() const override;
887 [[nodiscard]] int error() const override;
889 [[nodiscard]] bool eof() const override;
891 [[nodiscard]] const std::string& path() const noexcept override;
892
900 void populateFakeData() override;
901
903
904 protected:
905 // Pimpl idiom
906 class Impl;
908 std::unique_ptr<Impl> p_;
909}; // class RemoteIo
910
914class EXIV2API HttpIo : public RemoteIo {
915 public:
917
918
927 explicit HttpIo(const std::string& url, size_t blockSize = 1024);
928
929 private:
930 // Pimpl idiom
931 class HttpImpl;
932};
933
934#ifdef EXV_USE_CURL
939 class EXIV2API CurlIo : public RemoteIo {
940 public:
942
943
952 explicit CurlIo(const std::string& url, size_t blockSize = 0);
953
959 size_t write(const byte* data, size_t wcount) override;
965 size_t write(BasicIo& src) override;
966
967 protected:
968 // Pimpl idiom
969 class CurlImpl;
970};
971#endif
972
973// *****************************************************************************
974// template, inline and free functions
975
981EXIV2API DataBuf readFile(const std::string& path);
987EXIV2API size_t writeFile(const DataBuf& buf, const std::string& path);
988#ifdef EXV_USE_CURL
992EXIV2API size_t curlWriter(char* data, size_t size, size_t nmemb, std::string* writerData);
993#endif
994} // namespace Exiv2
995#endif // #ifndef BASICIO_HPP_
Exiv2::BasicIo
An interface for simple binary IO.
Definition basicio.hpp:35
Exiv2::BasicIo::write
virtual size_t write(const byte *data, size_t wcount)=0
Write data to the IO source. Current IO position is advanced by the number of bytes written.
Exiv2::BasicIo::isopen
virtual bool isopen() const =0
Returns true if the IO source is open, otherwise false.
Exiv2::BasicIo::open
virtual int open()=0
Open the IO source using the default access mode. The default mode should allow for reading and writi...
Exiv2::BasicIo::Position
Position
Seek starting positions.
Definition basicio.hpp:41
Exiv2::BasicIo::eof
virtual bool eof() const =0
Returns true if the IO position has reached the end, otherwise false.
Exiv2::BasicIo::populateFakeData
virtual void populateFakeData()=0
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Exiv2::BasicIo::size
virtual size_t size() const =0
Get the current size of the IO source in bytes.
Exiv2::BasicIo::getb
virtual int getb()=0
Read one byte from the IO source. Current IO position is advanced by one byte.
Exiv2::BasicIo::bigBlock_
byte * bigBlock_
this is allocated and populated by mmap()
Definition basicio.hpp:235
Exiv2::BasicIo::close
virtual int close()=0
Close the IO source. After closing a BasicIo instance can not be read or written. Closing flushes any...
Exiv2::BasicIo::tell
virtual size_t tell() const =0
Get the current IO position.
Exiv2::BasicIo::~BasicIo
virtual ~BasicIo()=default
Destructor.
Exiv2::BasicIo::readOrThrow
void readOrThrow(byte *buf, size_t rcount, ErrorCode err=ErrorCode::kerCorruptedMetadata)
Safe version of read() that checks for errors and throws an exception if the read was unsuccessful.
Definition basicio.cpp:56
Exiv2::BasicIo::seek
virtual int seek(int64_t offset, Position pos)=0
Move the current IO position.
Exiv2::BasicIo::path
virtual const std::string & path() const noexcept=0
Return the path to the IO resource. Often used to form comprehensive error messages where only a Basi...
Exiv2::BasicIo::munmap
virtual int munmap()=0
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
Exiv2::BasicIo::write
virtual size_t write(BasicIo &src)=0
Write data that is read from another BasicIo instance to the IO source. Current IO position is advanc...
Exiv2::BasicIo::UniquePtr
std::unique_ptr< BasicIo > UniquePtr
BasicIo auto_ptr type.
Definition basicio.hpp:38
Exiv2::BasicIo::read
virtual DataBuf read(size_t rcount)=0
Read data from the IO source. Reading starts at the current IO position and the position is advanced ...
Exiv2::BasicIo::transfer
virtual void transfer(BasicIo &src)=0
Remove all data from this object's IO source and then transfer data from the src BasicIo object into ...
Exiv2::BasicIo::putb
virtual int putb(byte data)=0
Write one byte to the IO source. Current IO position is advanced by one byte.
Exiv2::BasicIo::error
virtual int error() const =0
Returns 0 if the IO source is in a valid state, otherwise nonzero.
Exiv2::BasicIo::mmap
virtual byte * mmap(bool isWriteable=false)=0
Direct access to the IO data. For files, this is done by mapping the file into the process's address ...
Exiv2::BasicIo::read
virtual size_t read(byte *buf, size_t rcount)=0
Read data from the IO source. Reading starts at the current IO position and the position is advanced ...
Exiv2::BasicIo::seekOrThrow
void seekOrThrow(int64_t offset, Position pos, ErrorCode err)
Safe version of seek() that checks for errors and throws an exception if the seek was unsuccessful.
Definition basicio.cpp:62
Exiv2::FileIo::Impl
Internal Pimpl structure of class FileIo.
Definition basicio.cpp:69
Exiv2::FileIo::setPath
virtual void setPath(const std::string &path)
close the file source and set a new path.
Definition basicio.cpp:297
Exiv2::FileIo::open
int open(const std::string &mode)
Open the file using the specified mode.
Definition basicio.cpp:495
Exiv2::FileIo::read
DataBuf read(size_t rcount) override
Read data from the file. Reading starts at the current file position and the position is advanced by ...
Definition basicio.cpp:521
Exiv2::FileIo::FileIo
FileIo(const FileIo &)=delete
Copy constructor.
Exiv2::FileIo::isopen
bool isopen() const override
Returns true if the file is open, otherwise false.
Definition basicio.cpp:505
Exiv2::FileIo::seek
int seek(int64_t offset, Position pos) override
Move the current IO position.
Definition basicio.cpp:441
Exiv2::FileIo::write
size_t write(const byte *data, size_t wcount) override
Write data to the file. The file position is advanced by the number of bytes written.
Definition basicio.cpp:302
Exiv2::FileIo::tell
size_t tell() const override
Get the current file position.
Definition basicio.cpp:464
Exiv2::FileIo::populateFakeData
void populateFakeData() override
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Definition basicio.cpp:558
Exiv2::FileIo::path
const std::string & path() const noexcept override
Returns the path of the file.
Definition basicio.cpp:554
Exiv2::FileIo::munmap
int munmap() override
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
Definition basicio.cpp:190
Exiv2::FileIo::error
int error() const override
Returns 0 if the file is in a valid state, otherwise nonzero.
Definition basicio.cpp:546
Exiv2::FileIo::eof
bool eof() const override
Returns true if the file position has reached the end, otherwise false.
Definition basicio.cpp:550
Exiv2::FileIo::close
int close() override
Flush and unwritten data and close the file . It is safe to call close on an already closed instance.
Definition basicio.cpp:509
Exiv2::FileIo::FileIo
FileIo(const std::string &path)
Constructor that accepts the file path on which IO will be performed. The constructor does not open t...
Definition basicio.cpp:183
Exiv2::FileIo::size
size_t size() const override
Flush any buffered writes and get the current file size in bytes.
Definition basicio.cpp:474
Exiv2::FileIo::operator=
FileIo & operator=(const FileIo &)=delete
Assignment operator.
Exiv2::FileIo::getb
int getb() override
Read one byte from the file. The file position is advanced by one byte.
Definition basicio.cpp:540
Exiv2::FileIo::mmap
byte * mmap(bool isWriteable=false) override
Map the file into the process's address space. The file must be open before mmap() is called....
Definition basicio.cpp:225
Exiv2::FileIo::putb
int putb(byte data) override
Write one byte to the file. The file position is advanced by one byte.
Definition basicio.cpp:435
Exiv2::FileIo::transfer
void transfer(BasicIo &src) override
Remove the contents of the file and then transfer data from the src BasicIo object into the empty fil...
Definition basicio.cpp:333
Exiv2::HttpIo::HttpIo
HttpIo(const std::string &url, size_t blockSize=1024)
Constructor that accepts the http URL on which IO will be performed. The constructor does not open th...
Exiv2::IoCloser::operator=
IoCloser & operator=(const IoCloser &)=delete
Assignment operator.
Exiv2::IoCloser::IoCloser
IoCloser(BasicIo &bio)
Constructor, takes a BasicIo reference.
Definition basicio.hpp:251
Exiv2::IoCloser::~IoCloser
virtual ~IoCloser()
Destructor, closes the BasicIo reference.
Definition basicio.hpp:254
Exiv2::IoCloser::close
void close()
Close the BasicIo if it is open.
Definition basicio.hpp:262
Exiv2::IoCloser::bio_
BasicIo & bio_
The BasicIo reference.
Definition basicio.hpp:270
Exiv2::IoCloser::IoCloser
IoCloser(const IoCloser &)=delete
Copy constructor.
Exiv2::MemIo::Impl
Internal Pimpl structure of class MemIo.
Definition basicio.cpp:563
Exiv2::MemIo
Provides binary IO on blocks of memory by implementing the BasicIo interface. A copy-on-write impleme...
Definition basicio.hpp:497
Exiv2::MemIo::read
DataBuf read(size_t rcount) override
Read data from the memory block. Reading starts at the current IO position and the position is advanc...
Definition basicio.cpp:819
Exiv2::MemIo::transfer
void transfer(BasicIo &src) override
Clear the memory block and then transfer data from the src BasicIo object into a new block of memory.
Definition basicio.cpp:710
Exiv2::MemIo::tell
size_t tell() const override
Get the current IO position.
Definition basicio.cpp:797
Exiv2::MemIo::write
size_t write(const byte *data, size_t wcount) override
Write data to the memory block. If needed, the size of the internal memory block is expanded....
Definition basicio.cpp:701
Exiv2::MemIo::seek
int seek(int64_t offset, Position pos) override
Move the current IO position.
Definition basicio.cpp:761
Exiv2::MemIo::isopen
bool isopen() const override
Always returns true.
Definition basicio.cpp:811
Exiv2::MemIo::operator=
MemIo & operator=(const MemIo &)=delete
Assignment operator.
Exiv2::MemIo::path
const std::string & path() const noexcept override
Returns a dummy path, indicating that memory access is used.
Definition basicio.cpp:855
Exiv2::MemIo::open
int open() override
Memory IO is always open for reading and writing. This method therefore only resets the IO position t...
Definition basicio.cpp:805
Exiv2::MemIo::eof
bool eof() const override
Returns true if the IO position has reached the end, otherwise false.
Definition basicio.cpp:851
Exiv2::MemIo::MemIo
MemIo()
Default constructor that results in an empty object.
Definition basicio.cpp:689
Exiv2::MemIo::getb
int getb() override
Read one byte from the memory block. The IO position is advanced by one byte.
Definition basicio.cpp:839
Exiv2::MemIo::mmap
byte * mmap(bool=false) override
Allow direct access to the underlying data buffer. The buffer is not protected against write access i...
Definition basicio.cpp:789
Exiv2::MemIo::populateFakeData
void populateFakeData() override
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Definition basicio.cpp:860
Exiv2::MemIo::MemIo
MemIo(const MemIo &)=delete
Copy constructor.
Exiv2::MemIo::putb
int putb(byte data) override
Write one byte to the memory block. The IO position is advanced by one byte.
Definition basicio.cpp:755
Exiv2::MemIo::size
size_t size() const override
Get the current memory buffer size in bytes.
Definition basicio.cpp:801
Exiv2::MemIo::munmap
int munmap() override
Remove a mapping established with mmap(). If the mapped area is writeable, this ensures that changes ...
Definition basicio.cpp:793
Exiv2::MemIo::error
int error() const override
Always returns 0.
Definition basicio.cpp:847
Exiv2::MemIo::close
int close() override
Does nothing on MemIo objects.
Definition basicio.cpp:815
Exiv2::RemoteIo::Impl
Internal Pimpl abstract structure of class RemoteIo.
Definition basicio.cpp:1001
Exiv2::RemoteIo
Provides remote binary file IO by implementing the BasicIo interface. This is an abstract class....
Definition basicio.hpp:750
Exiv2::RemoteIo::RemoteIo
RemoteIo()
Destructor. Releases all managed memory.
Exiv2::RemoteIo::close
int close() override
Reset the IO position to the start. It does not release the data.
Definition basicio.cpp:1143
Exiv2::RemoteIo::error
int error() const override
Always returns 0.
Definition basicio.cpp:1366
Exiv2::RemoteIo::p_
std::unique_ptr< Impl > p_
Pointer to implementation.
Definition basicio.hpp:908
Exiv2::RemoteIo::write
size_t write(const byte *data, size_t wcount) override
Not support this method.
Definition basicio.cpp:1158
Exiv2::RemoteIo::size
size_t size() const override
Get the current memory buffer size in bytes.
Definition basicio.cpp:1358
Exiv2::RemoteIo::open
int open() override
Connect to the remote server, get the size of the remote file and allocate the array of blocksMap.
Definition basicio.cpp:1108
Exiv2::RemoteIo::populateFakeData
void populateFakeData() override
Mark all the bNone blocks to bKnow. This avoids allocating memory for parts of the file that contain ...
Definition basicio.cpp:1378
Exiv2::RemoteIo::mmap
byte * mmap(bool=false) override
Not support.
Definition basicio.cpp:1328
Exiv2::RemoteIo::path
const std::string & path() const noexcept override
Returns the URL of the file.
Definition basicio.cpp:1374
Exiv2::RemoteIo::tell
size_t tell() const override
Get the current IO position.
Definition basicio.cpp:1354
Exiv2::RemoteIo::seek
int seek(int64_t offset, Position pos) override
Move the current IO position.
Definition basicio.cpp:1304
Exiv2::RemoteIo::eof
bool eof() const override
Returns true if the IO position has reached the end, otherwise false.
Definition basicio.cpp:1370
Exiv2::RemoteIo::munmap
int munmap() override
Not support.
Definition basicio.cpp:1350
Exiv2::RemoteIo::putb
int putb(byte data) override
Not support.
Definition basicio.cpp:1230
Exiv2::RemoteIo::isopen
bool isopen() const override
Returns true if the memory area is allocated.
Definition basicio.cpp:1362
Exiv2::RemoteIo::transfer
void transfer(BasicIo &src) override
Remove the contents of the file and then transfer data from the src BasicIo object into the empty fil...
Definition basicio.cpp:1296
Exiv2::RemoteIo::read
DataBuf read(size_t rcount) override
Read data from the memory blocks. Reading starts at the current IO position and the position is advan...
Definition basicio.cpp:1234
Exiv2::RemoteIo::getb
int getb() override
Read one byte from the memory blocks. The IO position is advanced by one byte. If the memory block is...
Definition basicio.cpp:1282
Exiv2::XPathIo
Provides binary IO for the data from stdin and data uri path.
Definition basicio.hpp:692
Exiv2::XPathIo::transfer
void transfer(BasicIo &src) override
Change the name of the temp file and make it untemporary before calling the method of superclass File...
Definition basicio.cpp:920
Exiv2::XPathIo::TEMP_FILE_EXT
static constexpr auto TEMP_FILE_EXT
The extension of the temporary file which is created when getting input data to read metadata....
Definition basicio.hpp:698
Exiv2::XPathIo::GEN_FILE_EXT
static constexpr auto GEN_FILE_EXT
The extension of the generated file which is created when getting input data to add or modify the met...
Definition basicio.hpp:703
Exiv2::XPathIo::XPathIo
XPathIo(const std::string &orgPath)
Default constructor that reads data from stdin/data uri path and writes them to the temp file.
Definition basicio.cpp:910
Exiv2::XPathIo::writeDataToFile
static std::string writeDataToFile(const std::string &orgPath)
Read the data from stdin/data uri path and write them to the file.
Definition basicio.cpp:945
error.hpp
Error class for exceptions, log message class.
Exiv2
Class CrwImage to access Canon CRW images. References: The Canon RAW (CRW) File Format by Phil Harv...
Definition asfvideo.hpp:15
Exiv2::ErrorCode
ErrorCode
Complete list of all Exiv2 error codes.
Definition error.hpp:162
Exiv2::DataBuf
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition types.hpp:124