1 //========================================================================
2 //
3 // CachedFile.h
4 //
5 // Caching files support.
6 //
7 // This file is licensed under the GPLv2 or later
8 //
9 // Copyright 2009 Stefan Thomas <thomas@eload24.com>
10 // Copyright 2010 Hib Eris <hib@hiberis.nl>
11 // Copyright 2010, 2018-2020 Albert Astals Cid <aacid@kde.org>
12 //
13 //========================================================================
14 
15 #ifndef CACHEDFILE_H
16 #define CACHEDFILE_H
17 
18 #include "poppler-config.h"
19 #include "poppler_private_export.h"
20 
21 #include "Object.h"
22 #include "Stream.h"
23 
24 #include <vector>
25 
26 //------------------------------------------------------------------------
27 
28 #define CachedFileChunkSize 8192 // This should be a multiple of cachedStreamBufSize
29 
30 class GooString;
31 class CachedFileLoader;
32 
33 //------------------------------------------------------------------------
34 // CachedFile
35 //
36 // CachedFile gives FILE-like access to a document at a specified URI.
37 // In the constructor, you specify a CachedFileLoader that handles loading
38 // the data from the document. The CachedFile requests no more data then it
39 // needs from the CachedFileLoader.
40 //------------------------------------------------------------------------
41 
42 class POPPLER_PRIVATE_EXPORT CachedFile
43 {
44 
45     friend class CachedFileWriter;
46 
47 public:
48     CachedFile(CachedFileLoader *cacheLoader, GooString *uri);
49 
50     CachedFile(const CachedFile &) = delete;
51     CachedFile &operator=(const CachedFile &) = delete;
52 
getLength()53     unsigned int getLength() const { return length; }
54     long int tell();
55     int seek(long int offset, int origin);
56     size_t read(void *ptr, size_t unitsize, size_t count);
57     size_t write(const char *ptr, size_t size, size_t fromByte);
58     int cache(const std::vector<ByteRange> &ranges);
59 
60     // Reference counting.
61     void incRefCnt();
62     void decRefCnt();
63 
64 private:
65     ~CachedFile();
66 
67     enum ChunkState
68     {
69         chunkStateNew = 0,
70         chunkStateLoaded
71     };
72 
73     typedef struct
74     {
75         ChunkState state;
76         char data[CachedFileChunkSize];
77     } Chunk;
78 
79     int cache(size_t offset, size_t length);
80 
81     CachedFileLoader *loader;
82     GooString *uri;
83 
84     size_t length;
85     size_t streamPos;
86 
87     std::vector<Chunk> *chunks;
88 
89     int refCnt; // reference count
90 };
91 
92 //------------------------------------------------------------------------
93 // CachedFileWriter
94 //
95 // CachedFileWriter handles sequential writes to a CachedFile.
96 // On construction, you specify the CachedFile and the chunks of it to which data
97 // should be written.
98 //------------------------------------------------------------------------
99 
100 class POPPLER_PRIVATE_EXPORT CachedFileWriter
101 {
102 
103 public:
104     // Construct a CachedFile Writer.
105     // The caller is responsible for deleting the cachedFile and chunksA.
106     CachedFileWriter(CachedFile *cachedFile, std::vector<int> *chunksA);
107 
108     ~CachedFileWriter();
109 
110     // Writes size bytes from ptr to cachedFile, returns number of bytes written.
111     size_t write(const char *ptr, size_t size);
112 
113 private:
114     CachedFile *cachedFile;
115     std::vector<int> *chunks;
116     std::vector<int>::iterator it;
117     size_t offset;
118 };
119 
120 //------------------------------------------------------------------------
121 // CachedFileLoader
122 //
123 // CachedFileLoader is an abstact class that specifies the interface for
124 // loadng data from an URI into a CachedFile.
125 //------------------------------------------------------------------------
126 
127 class POPPLER_PRIVATE_EXPORT CachedFileLoader
128 {
129 
130 public:
131     CachedFileLoader() = default;
132     virtual ~CachedFileLoader();
133 
134     CachedFileLoader(const CachedFileLoader &) = delete;
135     CachedFileLoader &operator=(const CachedFileLoader &) = delete;
136 
137     // Initializes the file load.
138     // Returns the length of the file.
139     // The caller is responsible for deleting uri and cachedFile.
140     virtual size_t init(GooString *uri, CachedFile *cachedFile) = 0;
141 
142     // Loads specified byte ranges and passes it to the writer to store them.
143     // Returns 0 on success, Anything but 0 on failure.
144     // The caller is responsible for deleting the writer.
145     virtual int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
146 };
147 
148 //------------------------------------------------------------------------
149 
150 #endif
151