Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | #include "EbmlWriter.h" |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | #include <wchar.h> |
michael@0 | 13 | #include <string.h> |
michael@0 | 14 | #include <limits.h> |
michael@0 | 15 | #include "EbmlBufferWriter.h" |
michael@0 | 16 | #if defined(_MSC_VER) |
michael@0 | 17 | #define LITERALU64(n) n |
michael@0 | 18 | #else |
michael@0 | 19 | #define LITERALU64(n) n##LLU |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | void Ebml_WriteLen(EbmlGlobal *glob, int64_t val) { |
michael@0 | 23 | /* TODO check and make sure we are not > than 0x0100000000000000LLU */ |
michael@0 | 24 | unsigned char size = 8; /* size in bytes to output */ |
michael@0 | 25 | |
michael@0 | 26 | /* mask to compare for byte size */ |
michael@0 | 27 | int64_t minVal = 0xff; |
michael@0 | 28 | |
michael@0 | 29 | for (size = 1; size < 8; size ++) { |
michael@0 | 30 | if (val < minVal) |
michael@0 | 31 | break; |
michael@0 | 32 | |
michael@0 | 33 | minVal = (minVal << 7); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | val |= (((uint64_t)0x80) << ((size - 1) * 7)); |
michael@0 | 37 | |
michael@0 | 38 | Ebml_Serialize(glob, (void *) &val, sizeof(val), size); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | void Ebml_WriteString(EbmlGlobal *glob, const char *str) { |
michael@0 | 42 | const size_t size_ = strlen(str); |
michael@0 | 43 | const uint64_t size = size_; |
michael@0 | 44 | Ebml_WriteLen(glob, size); |
michael@0 | 45 | /* TODO: it's not clear from the spec whether the nul terminator |
michael@0 | 46 | * should be serialized too. For now we omit the null terminator. |
michael@0 | 47 | */ |
michael@0 | 48 | Ebml_Write(glob, str, (unsigned long)size); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | void Ebml_WriteUTF8(EbmlGlobal *glob, const wchar_t *wstr) { |
michael@0 | 52 | const size_t strlen = wcslen(wstr); |
michael@0 | 53 | |
michael@0 | 54 | /* TODO: it's not clear from the spec whether the nul terminator |
michael@0 | 55 | * should be serialized too. For now we include it. |
michael@0 | 56 | */ |
michael@0 | 57 | const uint64_t size = strlen; |
michael@0 | 58 | |
michael@0 | 59 | Ebml_WriteLen(glob, size); |
michael@0 | 60 | Ebml_Write(glob, wstr, (unsigned long)size); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | void Ebml_WriteID(EbmlGlobal *glob, unsigned long class_id) { |
michael@0 | 64 | int len; |
michael@0 | 65 | |
michael@0 | 66 | if (class_id >= 0x01000000) |
michael@0 | 67 | len = 4; |
michael@0 | 68 | else if (class_id >= 0x00010000) |
michael@0 | 69 | len = 3; |
michael@0 | 70 | else if (class_id >= 0x00000100) |
michael@0 | 71 | len = 2; |
michael@0 | 72 | else |
michael@0 | 73 | len = 1; |
michael@0 | 74 | |
michael@0 | 75 | Ebml_Serialize(glob, (void *)&class_id, sizeof(class_id), len); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | void Ebml_SerializeUnsigned32(EbmlGlobal *glob, unsigned long class_id, uint32_t ui) { |
michael@0 | 79 | unsigned char sizeSerialized = 8 | 0x80; |
michael@0 | 80 | Ebml_WriteID(glob, class_id); |
michael@0 | 81 | Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1); |
michael@0 | 82 | Ebml_Serialize(glob, &ui, sizeof(ui), 4); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | void Ebml_SerializeUnsigned64(EbmlGlobal *glob, unsigned long class_id, uint64_t ui) { |
michael@0 | 86 | unsigned char sizeSerialized = 8 | 0x80; |
michael@0 | 87 | Ebml_WriteID(glob, class_id); |
michael@0 | 88 | Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1); |
michael@0 | 89 | Ebml_Serialize(glob, &ui, sizeof(ui), 8); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void Ebml_SerializeUnsigned(EbmlGlobal *glob, unsigned long class_id, unsigned long ui) { |
michael@0 | 93 | unsigned char size = 8; /* size in bytes to output */ |
michael@0 | 94 | unsigned char sizeSerialized = 0; |
michael@0 | 95 | unsigned long minVal; |
michael@0 | 96 | |
michael@0 | 97 | Ebml_WriteID(glob, class_id); |
michael@0 | 98 | minVal = 0x7fLU; /* mask to compare for byte size */ |
michael@0 | 99 | |
michael@0 | 100 | for (size = 1; size < 4; size ++) { |
michael@0 | 101 | if (ui < minVal) { |
michael@0 | 102 | break; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | minVal <<= 7; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | sizeSerialized = 0x80 | size; |
michael@0 | 109 | Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1); |
michael@0 | 110 | Ebml_Serialize(glob, &ui, sizeof(ui), size); |
michael@0 | 111 | } |
michael@0 | 112 | /* TODO: perhaps this is a poor name for this id serializer helper function */ |
michael@0 | 113 | void Ebml_SerializeBinary(EbmlGlobal *glob, unsigned long class_id, unsigned long bin) { |
michael@0 | 114 | int size; |
michael@0 | 115 | for (size = 4; size > 1; size--) { |
michael@0 | 116 | if (bin & (unsigned int)0x000000ff << ((size - 1) * 8)) |
michael@0 | 117 | break; |
michael@0 | 118 | } |
michael@0 | 119 | Ebml_WriteID(glob, class_id); |
michael@0 | 120 | Ebml_WriteLen(glob, size); |
michael@0 | 121 | Ebml_WriteID(glob, bin); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | void Ebml_SerializeFloat(EbmlGlobal *glob, unsigned long class_id, double d) { |
michael@0 | 125 | unsigned char len = 0x88; |
michael@0 | 126 | |
michael@0 | 127 | Ebml_WriteID(glob, class_id); |
michael@0 | 128 | Ebml_Serialize(glob, &len, sizeof(len), 1); |
michael@0 | 129 | Ebml_Serialize(glob, &d, sizeof(d), 8); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | void Ebml_WriteSigned16(EbmlGlobal *glob, short val) { |
michael@0 | 133 | signed long out = ((val & 0x003FFFFF) | 0x00200000) << 8; |
michael@0 | 134 | Ebml_Serialize(glob, &out, sizeof(out), 3); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | void Ebml_SerializeString(EbmlGlobal *glob, unsigned long class_id, const char *s) { |
michael@0 | 138 | Ebml_WriteID(glob, class_id); |
michael@0 | 139 | Ebml_WriteString(glob, s); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | void Ebml_SerializeUTF8(EbmlGlobal *glob, unsigned long class_id, wchar_t *s) { |
michael@0 | 143 | Ebml_WriteID(glob, class_id); |
michael@0 | 144 | Ebml_WriteUTF8(glob, s); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | void Ebml_SerializeData(EbmlGlobal *glob, unsigned long class_id, unsigned char *data, unsigned long data_length) { |
michael@0 | 148 | Ebml_WriteID(glob, class_id); |
michael@0 | 149 | Ebml_WriteLen(glob, data_length); |
michael@0 | 150 | Ebml_Write(glob, data, data_length); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | void Ebml_WriteVoid(EbmlGlobal *glob, unsigned long vSize) { |
michael@0 | 154 | unsigned char tmp = 0; |
michael@0 | 155 | unsigned long i = 0; |
michael@0 | 156 | |
michael@0 | 157 | Ebml_WriteID(glob, 0xEC); |
michael@0 | 158 | Ebml_WriteLen(glob, vSize); |
michael@0 | 159 | |
michael@0 | 160 | for (i = 0; i < vSize; i++) { |
michael@0 | 161 | Ebml_Write(glob, &tmp, 1); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | /* TODO Serialize Date */ |