Exiv2
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  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  explicit IoCloser(BasicIo& bio) : bio_(bio) {
252  }
254  virtual ~IoCloser() {
255  close();
256  }
258 
260 
261  void close() {
263  if (bio_.isopen())
264  bio_.close();
265  }
267 
268  // DATA
271 
272  // Not implemented
274  IoCloser(const IoCloser&) = delete;
276  IoCloser& operator=(const IoCloser&) = delete;
277 }; // class IoCloser
278 
283 class EXIV2API FileIo : public BasicIo {
284  public:
286 
287 
293  explicit FileIo(const std::string& path);
294 
296  ~FileIo() override;
298 
300 
301 
314  int open(const std::string& mode);
322  int open() override;
329  int close() override;
339  size_t write(const byte* data, size_t wcount) override;
349  size_t write(BasicIo& src) override;
357  int putb(byte data) override;
368  DataBuf read(size_t rcount) override;
381  size_t read(byte* buf, size_t rcount) override;
388  int getb() override;
407  void transfer(BasicIo& src) override;
408 
409  int seek(int64_t offset, Position pos) override;
410 
422  byte* mmap(bool isWriteable = false) override;
430  int munmap() override;
434  virtual void setPath(const std::string& path);
435 
437 
439 
443  [[nodiscard]] size_t tell() const override;
450  [[nodiscard]] size_t size() const override;
452  [[nodiscard]] bool isopen() const override;
454  [[nodiscard]] int error() const override;
456  [[nodiscard]] bool eof() const override;
458  [[nodiscard]] const std::string& path() const noexcept override;
459 
467  void populateFakeData() override;
469 
470  // NOT IMPLEMENTED
472  FileIo(const FileIo&) = delete;
474  FileIo& operator=(const FileIo&) = delete;
475 
476  private:
477  // Pimpl idiom
478  class Impl;
479  std::unique_ptr<Impl> p_;
480 
481 }; // class FileIo
482 
495 class EXIV2API MemIo : public BasicIo {
496  public:
498 
499  MemIo();
508  MemIo(const byte* data, size_t size);
510  ~MemIo() override;
512 
514 
515 
521  int open() override;
526  int close() override;
537  size_t write(const byte* data, size_t wcount) override;
548  size_t write(BasicIo& src) override;
556  int putb(byte data) override;
567  DataBuf read(size_t rcount) override;
580  size_t read(byte* buf, size_t rcount) override;
587  int getb() override;
603  void transfer(BasicIo& src) override;
604 
605  int seek(int64_t offset, Position pos) override;
606 
615  byte* mmap(bool /*isWriteable*/ = false) override;
616  int munmap() override;
618 
620 
621 
625  [[nodiscard]] size_t tell() const override;
631  [[nodiscard]] size_t size() const override;
633  [[nodiscard]] bool isopen() const override;
635  [[nodiscard]] int error() const override;
637  [[nodiscard]] bool eof() const override;
639  [[nodiscard]] const std::string& path() const noexcept override;
640 
648  void populateFakeData() override;
649 
651 
652  // NOT IMPLEMENTED
654  MemIo(const MemIo&) = delete;
656  MemIo& operator=(const MemIo&) = delete;
657 
658  private:
659  // Pimpl idiom
660  class Impl;
661  std::unique_ptr<Impl> p_;
662 
663 }; // class MemIo
664 
668 #if EXV_XPATH_MEMIO
669 class EXIV2API XPathIo : public MemIo {
670  public:
672 
673  XPathIo(const std::string& path);
676  private:
681  void ReadStdin();
687  void ReadDataUri(const std::string& path);
688 }; // class XPathIo
689 #else
690 class EXIV2API XPathIo : public FileIo {
691  public:
696  static constexpr auto TEMP_FILE_EXT = ".exiv2_temp";
701  static constexpr auto GEN_FILE_EXT = ".exiv2";
702 
704 
705  explicit XPathIo(const std::string& orgPath);
707 
709  ~XPathIo() override;
711 
712  XPathIo(const XPathIo&) = delete;
713  XPathIo& operator=(const XPathIo&) = delete;
714 
716 
717 
721  void transfer(BasicIo& src) override;
722 
724 
726 
727 
733  static std::string writeDataToFile(const std::string& orgPath);
735 
736  private:
737  // True if the file is a temporary file and it should be deleted in destructor.
738  bool isTemp_{true};
739  std::string tempFilePath_;
740 }; // class XPathIo
741 #endif
742 
748 class EXIV2API RemoteIo : public BasicIo {
749  public:
751  RemoteIo();
752  ~RemoteIo() override;
754 
755  RemoteIo(const RemoteIo&) = delete;
756  RemoteIo& operator=(const RemoteIo&) = delete;
757 
759 
760 
769  int open() override;
770 
776  int close() override;
781  size_t write(const byte* data, size_t wcount) override;
796  size_t write(BasicIo& src) override;
797 
802  int putb(byte data) override;
815  DataBuf read(size_t rcount) override;
830  size_t read(byte* buf, size_t rcount) override;
839  int getb() override;
854  void transfer(BasicIo& src) override;
855 
856  int seek(int64_t offset, Position pos) override;
857 
862  byte* mmap(bool /*isWriteable*/ = false) override;
867  int munmap() override;
869 
871 
875  [[nodiscard]] size_t tell() const override;
881  [[nodiscard]] size_t size() const override;
883  [[nodiscard]] bool isopen() const override;
885  [[nodiscard]] int error() const override;
887  [[nodiscard]] bool eof() const override;
889  [[nodiscard]] const std::string& path() const noexcept override;
890 
898  void populateFakeData() override;
899 
901 
902  protected:
903  // Pimpl idiom
904  class Impl;
906  std::unique_ptr<Impl> p_;
907 }; // class RemoteIo
908 
912 class EXIV2API HttpIo : public RemoteIo {
913  public:
915 
916 
925  explicit HttpIo(const std::string& url, size_t blockSize = 1024);
926 
927  private:
928  // Pimpl idiom
929  class HttpImpl;
930 };
931 
932 #ifdef EXV_USE_CURL
933 
937 class EXIV2API CurlIo : public RemoteIo {
938  public:
940 
941 
950  explicit CurlIo(const std::string& url, size_t blockSize = 0);
951 
957  size_t write(const byte* data, size_t wcount) override;
963  size_t write(BasicIo& src) override;
964 
965  protected:
966  // Pimpl idiom
967  class CurlImpl;
968 };
969 #endif
970 
971 // *****************************************************************************
972 // template, inline and free functions
973 
979 EXIV2API DataBuf readFile(const std::string& path);
985 EXIV2API size_t writeFile(const DataBuf& buf, const std::string& path);
986 #ifdef EXV_USE_CURL
987 
990 EXIV2API size_t curlWriter(char* data, size_t size, size_t nmemb, std::string* writerData);
991 #endif
992 } // namespace Exiv2
993 #endif // #ifndef BASICIO_HPP_
An interface for simple binary IO.
Definition: basicio.hpp:35
Provides remote binary file IO by implementing the BasicIo interface. This is an abstract class...
Definition: basicio.hpp:748
Provides binary file IO by implementing the BasicIo interface.
Definition: basicio.hpp:283
Internal Pimpl abstract structure of class RemoteIo.
Definition: basicio.cpp:1003
uint8_t byte
1 byte unsigned integer type.
Definition: types.hpp:26
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:124
Provides the http read/write access for the RemoteIo.
Definition: basicio.hpp:912
EXIV2API DataBuf readFile(const std::string &path)
Read file path into a DataBuf, which is returned.
Definition: basicio.cpp:1730
Position
Seek starting positions.
Definition: basicio.hpp:41
Internal Pimpl structure of class FileIo.
Definition: basicio.cpp:79
Internal Pimpl structure of class MemIo.
Definition: basicio.cpp:575
Provides binary IO on blocks of memory by implementing the BasicIo interface. A copy-on-write impleme...
Definition: basicio.hpp:495
EXIV2API size_t writeFile(const DataBuf &buf, const std::string &path)
Write DataBuf buf to file path.
Definition: basicio.cpp:1746
std::unique_ptr< BasicIo > UniquePtr
BasicIo auto_ptr type.
Definition: basicio.hpp:38
BasicIo & bio_
The BasicIo reference.
Definition: basicio.hpp:270
Error class for exceptions, log message class.
Class CrwImage to access Canon CRW images. References: The Canon RAW (CRW) File Format by Phil Harv...
Definition: asfvideo.hpp:15
Utility class that closes a BasicIo instance upon destruction. Meant to be used as a stack variable i...
Definition: basicio.hpp:246
virtual ~IoCloser()
Destructor, closes the BasicIo reference.
Definition: basicio.hpp:254
std::unique_ptr< Impl > p_
Pointer to implementation.
Definition: basicio.hpp:904
ErrorCode
Complete list of all Exiv2 error codes.
Definition: error.hpp:162
Internal Pimpl structure of class HttpIo.
Definition: basicio.cpp:1389
Provides binary IO for the data from stdin and data uri path.
Definition: basicio.hpp:690