]> git.cworth.org Git - apitrace/blob - trace_snappyfile.cpp
Fix writing/reading compressed length of the chunks.
[apitrace] / trace_snappyfile.cpp
1 #include "trace_snappyfile.hpp"
2
3 #include <snappy.h>
4
5 #include <assert.h>
6 #include <string.h>
7
8 using namespace Trace;
9
10 SnappyFile::SnappyFile(const std::string &filename,
11                               File::Mode mode)
12     : File(),
13       m_cache(0),
14       m_cachePtr(0),
15       m_cacheSize(0)
16 {
17     m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
18 }
19
20 SnappyFile::~SnappyFile()
21 {
22     delete [] m_compressedCache;
23 }
24
25 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
26 {
27     std::ios_base::openmode fmode = std::fstream::binary;
28     if (mode == File::Write) {
29         fmode |= (std::fstream::out | std::fstream::trunc);
30         createCache(SNAPPY_CHUNK_SIZE);
31     } else if (mode == File::Read) {
32         fmode |= std::fstream::in;
33     }
34
35     m_stream.open(filename.c_str(), fmode);
36
37     //read in the initial buffer if we're reading
38     if (m_stream.is_open() && mode == File::Read) {
39         // read the snappy file identifier
40         unsigned char byte1, byte2;
41         m_stream >> byte1;
42         m_stream >> byte2;
43         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
44
45         flushCache();
46     } else if (m_stream.is_open() && mode == File::Write) {
47         // write the snappy file identifier
48         m_stream << SNAPPY_BYTE1;
49         m_stream << SNAPPY_BYTE2;
50     }
51     return m_stream.is_open();
52 }
53
54 bool SnappyFile::rawWrite(const void *buffer, int length)
55 {
56     if (freeCacheSize() > length) {
57         memcpy(m_cachePtr, buffer, length);
58         m_cachePtr += length;
59     } else if (freeCacheSize() == length) {
60         memcpy(m_cachePtr, buffer, length);
61         m_cachePtr += length;
62         flushCache();
63     } else {
64         int sizeToWrite = length;
65
66         while (sizeToWrite >= freeCacheSize()) {
67             int endSize = freeCacheSize();
68             int offset = length - sizeToWrite;
69             memcpy(m_cachePtr, (char*)buffer + offset, endSize);
70             sizeToWrite -= endSize;
71             m_cachePtr += endSize;
72             flushCache();
73         }
74         if (sizeToWrite) {
75             int offset = length - sizeToWrite;
76             memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
77             m_cachePtr += sizeToWrite;
78         }
79     }
80
81     return true;
82 }
83
84 bool SnappyFile::rawRead(void *buffer, int length)
85 {
86     if (m_stream.eof()) {
87         return false;
88     }
89     if (freeCacheSize() > length) {
90         memcpy(buffer, m_cachePtr, length);
91         m_cachePtr += length;
92     } else if (freeCacheSize() == length) {
93         memcpy(buffer, m_cachePtr, length);
94         m_cachePtr += length;
95         flushCache();
96     } else {
97         int sizeToRead = length;
98         int offset = 0;
99         while (sizeToRead) {
100             int chunkSize = std::min(freeCacheSize(), sizeToRead);
101             offset = length - sizeToRead;
102             memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
103             m_cachePtr += chunkSize;
104             sizeToRead -= chunkSize;
105             if (sizeToRead > 0)
106                 flushCache();
107             if (!m_cacheSize)
108                 break;
109         }
110     }
111
112     return true;
113 }
114
115 int SnappyFile::rawGetc()
116 {
117     int c = 0;
118     if (!rawRead(&c, 1))
119         return -1;
120     return c;
121 }
122
123 void SnappyFile::rawClose()
124 {
125     flushCache();
126     m_stream.close();
127     delete [] m_cache;
128     m_cache = NULL;
129     m_cachePtr = NULL;
130 }
131
132 void SnappyFile::rawFlush(FlushType type)
133 {
134     if (type == FlushDeep) {
135         flushCache();
136     }
137     m_stream.flush();
138 }
139
140 void SnappyFile::flushCache()
141 {
142     if (m_mode == File::Write) {
143         size_t compressedLength;
144
145         ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
146                               m_compressedCache, &compressedLength);
147
148         writeCompressedLength(compressedLength);
149         m_stream.write(m_compressedCache, compressedLength);
150         m_cachePtr = m_cache;
151     } else if (m_mode == File::Read) {
152         if (m_stream.eof())
153             return;
154         //assert(m_cachePtr == m_cache + m_cacheSize);
155         size_t compressedLength;
156         compressedLength = readCompressedLength();
157         m_stream.read((char*)m_compressedCache, compressedLength);
158         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
159                                         &m_cacheSize);
160         if (m_cache)
161             delete [] m_cache;
162         createCache(m_cacheSize);
163         ::snappy::RawUncompress(m_compressedCache, compressedLength,
164                                 m_cache);
165     }
166 }
167
168 void SnappyFile::createCache(size_t size)
169 {
170     m_cache = new char[size];
171     m_cachePtr = m_cache;
172     m_cacheSize = size;
173 }
174
175 void SnappyFile::writeCompressedLength(size_t value)
176 {
177     m_stream.write((char*)&value, sizeof value);
178 }
179
180 size_t SnappyFile::readCompressedLength()
181 {
182     size_t len;
183     m_stream.read((char*)&len, sizeof len);
184     return len;
185 }