other-licenses/snappy/src/snappy-c.cc

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // Copyright 2011 Martin Gieseking <martin.gieseking@uos.de>.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions are
michael@0 5 // met:
michael@0 6 //
michael@0 7 // * Redistributions of source code must retain the above copyright
michael@0 8 // notice, this list of conditions and the following disclaimer.
michael@0 9 // * Redistributions in binary form must reproduce the above
michael@0 10 // copyright notice, this list of conditions and the following disclaimer
michael@0 11 // in the documentation and/or other materials provided with the
michael@0 12 // distribution.
michael@0 13 // * Neither the name of Google Inc. nor the names of its
michael@0 14 // contributors may be used to endorse or promote products derived from
michael@0 15 // this software without specific prior written permission.
michael@0 16 //
michael@0 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28
michael@0 29 #include "snappy.h"
michael@0 30 #include "snappy-c.h"
michael@0 31
michael@0 32 extern "C" {
michael@0 33
michael@0 34 snappy_status snappy_compress(const char* input,
michael@0 35 size_t input_length,
michael@0 36 char* compressed,
michael@0 37 size_t *compressed_length) {
michael@0 38 if (*compressed_length < snappy_max_compressed_length(input_length)) {
michael@0 39 return SNAPPY_BUFFER_TOO_SMALL;
michael@0 40 }
michael@0 41 snappy::RawCompress(input, input_length, compressed, compressed_length);
michael@0 42 return SNAPPY_OK;
michael@0 43 }
michael@0 44
michael@0 45 snappy_status snappy_uncompress(const char* compressed,
michael@0 46 size_t compressed_length,
michael@0 47 char* uncompressed,
michael@0 48 size_t* uncompressed_length) {
michael@0 49 size_t real_uncompressed_length;
michael@0 50 if (!snappy::GetUncompressedLength(compressed,
michael@0 51 compressed_length,
michael@0 52 &real_uncompressed_length)) {
michael@0 53 return SNAPPY_INVALID_INPUT;
michael@0 54 }
michael@0 55 if (*uncompressed_length < real_uncompressed_length) {
michael@0 56 return SNAPPY_BUFFER_TOO_SMALL;
michael@0 57 }
michael@0 58 if (!snappy::RawUncompress(compressed, compressed_length, uncompressed)) {
michael@0 59 return SNAPPY_INVALID_INPUT;
michael@0 60 }
michael@0 61 *uncompressed_length = real_uncompressed_length;
michael@0 62 return SNAPPY_OK;
michael@0 63 }
michael@0 64
michael@0 65 size_t snappy_max_compressed_length(size_t source_length) {
michael@0 66 return snappy::MaxCompressedLength(source_length);
michael@0 67 }
michael@0 68
michael@0 69 snappy_status snappy_uncompressed_length(const char *compressed,
michael@0 70 size_t compressed_length,
michael@0 71 size_t *result) {
michael@0 72 if (snappy::GetUncompressedLength(compressed,
michael@0 73 compressed_length,
michael@0 74 result)) {
michael@0 75 return SNAPPY_OK;
michael@0 76 } else {
michael@0 77 return SNAPPY_INVALID_INPUT;
michael@0 78 }
michael@0 79 }
michael@0 80
michael@0 81 snappy_status snappy_validate_compressed_buffer(const char *compressed,
michael@0 82 size_t compressed_length) {
michael@0 83 if (snappy::IsValidCompressedBuffer(compressed, compressed_length)) {
michael@0 84 return SNAPPY_OK;
michael@0 85 } else {
michael@0 86 return SNAPPY_INVALID_INPUT;
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 } // extern "C"

mercurial