Some code reorganization needed for an internal change.

R=fikes


git-svn-id: https://snappy.googlecode.com/svn/trunk@75 03e5f5b5-db94-4691-08a0-1a8bf15f6143
This commit is contained in:
snappy.mirrorbot@gmail.com
2013-06-12 19:51:15 +00:00
parent a3e928d62b
commit cd92eb0852

View File

@@ -839,27 +839,18 @@ bool SnappyDecompressor::RefillTag() {
} }
template <typename Writer> template <typename Writer>
static bool InternalUncompress(Source* r, static bool InternalUncompress(Source* r, Writer* writer) {
Writer* writer,
uint32 max_len) {
// Read the uncompressed length from the front of the compressed input // Read the uncompressed length from the front of the compressed input
SnappyDecompressor decompressor(r); SnappyDecompressor decompressor(r);
uint32 uncompressed_len = 0; uint32 uncompressed_len = 0;
if (!decompressor.ReadUncompressedLength(&uncompressed_len)) return false; if (!decompressor.ReadUncompressedLength(&uncompressed_len)) return false;
return InternalUncompressAllTags( return InternalUncompressAllTags(&decompressor, writer, uncompressed_len);
&decompressor, writer, uncompressed_len, max_len);
} }
template <typename Writer> template <typename Writer>
static bool InternalUncompressAllTags(SnappyDecompressor* decompressor, static bool InternalUncompressAllTags(SnappyDecompressor* decompressor,
Writer* writer, Writer* writer,
uint32 uncompressed_len, uint32 uncompressed_len) {
uint32 max_len) {
// Protect against possible DoS attack
if (static_cast<uint64>(uncompressed_len) > max_len) {
return false;
}
writer->SetExpectedLength(uncompressed_len); writer->SetExpectedLength(uncompressed_len);
// Process the entire input // Process the entire input
@@ -1039,7 +1030,7 @@ bool RawUncompress(const char* compressed, size_t n, char* uncompressed) {
bool RawUncompress(Source* compressed, char* uncompressed) { bool RawUncompress(Source* compressed, char* uncompressed) {
SnappyArrayWriter output(uncompressed); SnappyArrayWriter output(uncompressed);
return InternalUncompress(compressed, &output, kuint32max); return InternalUncompress(compressed, &output);
} }
bool Uncompress(const char* compressed, size_t n, string* uncompressed) { bool Uncompress(const char* compressed, size_t n, string* uncompressed) {
@@ -1047,9 +1038,9 @@ bool Uncompress(const char* compressed, size_t n, string* uncompressed) {
if (!GetUncompressedLength(compressed, n, &ulength)) { if (!GetUncompressedLength(compressed, n, &ulength)) {
return false; return false;
} }
// Protect against possible DoS attack // On 32-bit builds: max_size() < kuint32max. Check for that instead
if ((static_cast<uint64>(ulength) + uncompressed->size()) > // of crashing (e.g., consider externally specified compressed data).
uncompressed->max_size()) { if (ulength > uncompressed->max_size()) {
return false; return false;
} }
STLStringResizeUninitialized(uncompressed, ulength); STLStringResizeUninitialized(uncompressed, ulength);
@@ -1088,7 +1079,7 @@ class SnappyDecompressionValidator {
bool IsValidCompressedBuffer(const char* compressed, size_t n) { bool IsValidCompressedBuffer(const char* compressed, size_t n) {
ByteArraySource reader(compressed, n); ByteArraySource reader(compressed, n);
SnappyDecompressionValidator writer; SnappyDecompressionValidator writer;
return InternalUncompress(&reader, &writer, kuint32max); return InternalUncompress(&reader, &writer);
} }
void RawCompress(const char* input, void RawCompress(const char* input,