Tue, 06 Jan 2015 21:39:09 +0100
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 2005 and onwards Google Inc. |
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 | // A light-weight compression algorithm. It is designed for speed of |
michael@0 | 30 | // compression and decompression, rather than for the utmost in space |
michael@0 | 31 | // savings. |
michael@0 | 32 | // |
michael@0 | 33 | // For getting better compression ratios when you are compressing data |
michael@0 | 34 | // with long repeated sequences or compressing data that is similar to |
michael@0 | 35 | // other data, while still compressing fast, you might look at first |
michael@0 | 36 | // using BMDiff and then compressing the output of BMDiff with |
michael@0 | 37 | // Snappy. |
michael@0 | 38 | |
michael@0 | 39 | #ifndef UTIL_SNAPPY_SNAPPY_H__ |
michael@0 | 40 | #define UTIL_SNAPPY_SNAPPY_H__ |
michael@0 | 41 | |
michael@0 | 42 | #include <stddef.h> |
michael@0 | 43 | #include <string> |
michael@0 | 44 | |
michael@0 | 45 | #include "snappy-stubs-public.h" |
michael@0 | 46 | |
michael@0 | 47 | namespace snappy { |
michael@0 | 48 | class Source; |
michael@0 | 49 | class Sink; |
michael@0 | 50 | |
michael@0 | 51 | // ------------------------------------------------------------------------ |
michael@0 | 52 | // Generic compression/decompression routines. |
michael@0 | 53 | // ------------------------------------------------------------------------ |
michael@0 | 54 | |
michael@0 | 55 | // Compress the bytes read from "*source" and append to "*sink". Return the |
michael@0 | 56 | // number of bytes written. |
michael@0 | 57 | size_t Compress(Source* source, Sink* sink); |
michael@0 | 58 | |
michael@0 | 59 | bool GetUncompressedLength(Source* source, uint32* result); |
michael@0 | 60 | |
michael@0 | 61 | // ------------------------------------------------------------------------ |
michael@0 | 62 | // Higher-level string based routines (should be sufficient for most users) |
michael@0 | 63 | // ------------------------------------------------------------------------ |
michael@0 | 64 | |
michael@0 | 65 | // Sets "*output" to the compressed version of "input[0,input_length-1]". |
michael@0 | 66 | // Original contents of *output are lost. |
michael@0 | 67 | // |
michael@0 | 68 | // REQUIRES: "input[]" is not an alias of "*output". |
michael@0 | 69 | size_t Compress(const char* input, size_t input_length, string* output); |
michael@0 | 70 | |
michael@0 | 71 | // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed". |
michael@0 | 72 | // Original contents of "*uncompressed" are lost. |
michael@0 | 73 | // |
michael@0 | 74 | // REQUIRES: "compressed[]" is not an alias of "*uncompressed". |
michael@0 | 75 | // |
michael@0 | 76 | // returns false if the message is corrupted and could not be decompressed |
michael@0 | 77 | bool Uncompress(const char* compressed, size_t compressed_length, |
michael@0 | 78 | string* uncompressed); |
michael@0 | 79 | |
michael@0 | 80 | |
michael@0 | 81 | // ------------------------------------------------------------------------ |
michael@0 | 82 | // Lower-level character array based routines. May be useful for |
michael@0 | 83 | // efficiency reasons in certain circumstances. |
michael@0 | 84 | // ------------------------------------------------------------------------ |
michael@0 | 85 | |
michael@0 | 86 | // REQUIRES: "compressed" must point to an area of memory that is at |
michael@0 | 87 | // least "MaxCompressedLength(input_length)" bytes in length. |
michael@0 | 88 | // |
michael@0 | 89 | // Takes the data stored in "input[0..input_length]" and stores |
michael@0 | 90 | // it in the array pointed to by "compressed". |
michael@0 | 91 | // |
michael@0 | 92 | // "*compressed_length" is set to the length of the compressed output. |
michael@0 | 93 | // |
michael@0 | 94 | // Example: |
michael@0 | 95 | // char* output = new char[snappy::MaxCompressedLength(input_length)]; |
michael@0 | 96 | // size_t output_length; |
michael@0 | 97 | // RawCompress(input, input_length, output, &output_length); |
michael@0 | 98 | // ... Process(output, output_length) ... |
michael@0 | 99 | // delete [] output; |
michael@0 | 100 | void RawCompress(const char* input, |
michael@0 | 101 | size_t input_length, |
michael@0 | 102 | char* compressed, |
michael@0 | 103 | size_t* compressed_length); |
michael@0 | 104 | |
michael@0 | 105 | // Given data in "compressed[0..compressed_length-1]" generated by |
michael@0 | 106 | // calling the Snappy::Compress routine, this routine |
michael@0 | 107 | // stores the uncompressed data to |
michael@0 | 108 | // uncompressed[0..GetUncompressedLength(compressed)-1] |
michael@0 | 109 | // returns false if the message is corrupted and could not be decrypted |
michael@0 | 110 | bool RawUncompress(const char* compressed, size_t compressed_length, |
michael@0 | 111 | char* uncompressed); |
michael@0 | 112 | |
michael@0 | 113 | // Given data from the byte source 'compressed' generated by calling |
michael@0 | 114 | // the Snappy::Compress routine, this routine stores the uncompressed |
michael@0 | 115 | // data to |
michael@0 | 116 | // uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1] |
michael@0 | 117 | // returns false if the message is corrupted and could not be decrypted |
michael@0 | 118 | bool RawUncompress(Source* compressed, char* uncompressed); |
michael@0 | 119 | |
michael@0 | 120 | // Returns the maximal size of the compressed representation of |
michael@0 | 121 | // input data that is "source_bytes" bytes in length; |
michael@0 | 122 | size_t MaxCompressedLength(size_t source_bytes); |
michael@0 | 123 | |
michael@0 | 124 | // REQUIRES: "compressed[]" was produced by RawCompress() or Compress() |
michael@0 | 125 | // Returns true and stores the length of the uncompressed data in |
michael@0 | 126 | // *result normally. Returns false on parsing error. |
michael@0 | 127 | // This operation takes O(1) time. |
michael@0 | 128 | bool GetUncompressedLength(const char* compressed, size_t compressed_length, |
michael@0 | 129 | size_t* result); |
michael@0 | 130 | |
michael@0 | 131 | // Returns true iff the contents of "compressed[]" can be uncompressed |
michael@0 | 132 | // successfully. Does not return the uncompressed data. Takes |
michael@0 | 133 | // time proportional to compressed_length, but is usually at least |
michael@0 | 134 | // a factor of four faster than actual decompression. |
michael@0 | 135 | bool IsValidCompressedBuffer(const char* compressed, |
michael@0 | 136 | size_t compressed_length); |
michael@0 | 137 | |
michael@0 | 138 | // *** DO NOT CHANGE THE VALUE OF kBlockSize *** |
michael@0 | 139 | // |
michael@0 | 140 | // New Compression code chops up the input into blocks of at most |
michael@0 | 141 | // the following size. This ensures that back-references in the |
michael@0 | 142 | // output never cross kBlockSize block boundaries. This can be |
michael@0 | 143 | // helpful in implementing blocked decompression. However the |
michael@0 | 144 | // decompression code should not rely on this guarantee since older |
michael@0 | 145 | // compression code may not obey it. |
michael@0 | 146 | static const int kBlockLog = 15; |
michael@0 | 147 | static const size_t kBlockSize = 1 << kBlockLog; |
michael@0 | 148 | |
michael@0 | 149 | static const int kMaxHashTableBits = 14; |
michael@0 | 150 | static const size_t kMaxHashTableSize = 1 << kMaxHashTableBits; |
michael@0 | 151 | |
michael@0 | 152 | } // end namespace snappy |
michael@0 | 153 | |
michael@0 | 154 | |
michael@0 | 155 | #endif // UTIL_SNAPPY_SNAPPY_H__ |