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, 2022 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
30class GooString;
31class 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
42class POPPLER_PRIVATE_EXPORT CachedFile
43{
44
45 friend class CachedFileWriter;
46
47public:
48 explicit CachedFile(CachedFileLoader *cacheLoader);
49
50 CachedFile(const CachedFile &) = delete;
51 CachedFile &operator=(const CachedFile &) = delete;
52
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
64private:
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
83 size_t length;
84 size_t streamPos;
85
86 std::vector<Chunk> *chunks;
87
88 int refCnt; // reference count
89};
90
91//------------------------------------------------------------------------
92// CachedFileWriter
93//
94// CachedFileWriter handles sequential writes to a CachedFile.
95// On construction, you specify the CachedFile and the chunks of it to which data
96// should be written.
97//------------------------------------------------------------------------
98
99class POPPLER_PRIVATE_EXPORT CachedFileWriter
100{
101
102public:
103 // Construct a CachedFile Writer.
104 // The caller is responsible for deleting the cachedFile and chunksA.
105 CachedFileWriter(CachedFile *cachedFile, std::vector<int> *chunksA);
106
107 ~CachedFileWriter();
108
109 // Writes size bytes from ptr to cachedFile, returns number of bytes written.
110 size_t write(const char *ptr, size_t size);
111
112private:
113 CachedFile *cachedFile;
114 std::vector<int> *chunks;
115 std::vector<int>::iterator it;
116 size_t offset;
117};
118
119//------------------------------------------------------------------------
120// CachedFileLoader
121//
122// CachedFileLoader is an abstact class that specifies the interface for
123// loadng data from an URI into a CachedFile.
124//------------------------------------------------------------------------
125
126class POPPLER_PRIVATE_EXPORT CachedFileLoader
127{
128
129public:
130 CachedFileLoader() = default;
131 virtual ~CachedFileLoader();
132
133 CachedFileLoader(const CachedFileLoader &) = delete;
134 CachedFileLoader &operator=(const CachedFileLoader &) = delete;
135
136 // Initializes the file load.
137 // Returns the length of the file.
138 // The caller is responsible for deleting cachedFile.
139 virtual size_t init(CachedFile *cachedFile) = 0;
140
141 // Loads specified byte ranges and passes it to the writer to store them.
142 // Returns 0 on success, Anything but 0 on failure.
143 // The caller is responsible for deleting the writer.
144 virtual int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
145};
146
147//------------------------------------------------------------------------
148
149#endif
150

source code of poppler/poppler/CachedFile.h