mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
93f846cd1f
- Bug 1275016 - Rename Endian.h to EndianUtils.h to avoid #include confusion with Android's endian.h stdlib header. r=froydnj (b54a25f572) - add crashreporter stuff (aa7ef15337) - Bug 1261168 - Add AlignedAutoTArray type in Web Audio; r=padenot (285d2cb88b) - Bug 1273390. Part 1 - move some functions to private. r=jya. (07a3037e59) - Bug 1273390. Part 2 - add assertions. r=jya. (2cae7c596a) - Bug 1273390. Part 3 - rename some functions to be consistent with other sub-classes of MediaDataDecoder. r=jya. (c48c7060ce) - Bug 1273390. Part 4 - remove use of FlushableTaskQueue::Flush(). r=jya. (00565a65f4) - Bug 1273390. Part 5 - remove use of FlushableTaskQueue. r=jya. (30600b204e) - Bug 1273774. Part 1 - remove unused members and thread assertions. r=jya (f5177ed641) - Bug 1273774. Part 2 - do decoding jobs synchronously without dispatching. r=jya. (62d840d27c) - Bug 1273774. Part 3 - remove members no longer used. r=jya. (e957ca512a) - Bug 1244410: [ffmpeg] Ensure the last drained frame has the proper duration set. r=gerald (d5521bfdd4) - Bug 1271508. Part 1 - refactor FFmpegAudioDecoder code to be similar to FFmpegVideoDecoder::Input() so it would be easier to extract common code to the parent class. r=jya. (613e6c624c) - Bug 1271508. Part 2 - rename functions so they are the same as those of FFmpegAudioDecoder so it would be easier to extract common code to the parent class. r=jya. (cb281cba26) - Bug 1270350 - per comment 0, use SyncRunnable to repalce the boilerplate code. r=jya. (b99460e571) - Bug 1271508. Part 3 - extract code to the parent class and remove use of mTaskQueue from sub-classes. r=jya. (2a7ff4dd1e) - Bug 1274216 - remove use of FlushableTaskQueue from PlatformDecoderModule. r=jya. (eb160c5fa2) - Bug 1271517. Part 1 - remove use of FlushableTaskQueue::Flush() from FFmpegDataDecoder::Flush(). r=jya. (fdf10da4ab) - Bug 1271517. Part 2 - remove use of FlushableTaskQueue. r=jya. (a7016d8506) - Bug 1273397. Part 1 - rename some functions to be consistent with other MediaDataDecoder sub-classes. r=jya. (7eecb164be) - Bug 1273397. Part 2 - constify some members. r=jya. (e4482f9a23) - Bug 1273397. Part 3 - remove use of FlushableTaskQueue::Flush(). r=jya. (0b7ee073fe) - Bug 1273397. Part 4 - remove use of FlushableTaskQueue. r=jya. (6a612161d5) - Bug 1273397. Part 5 - add assertions. r=jya. (ff3a62a6fb) - Bug 1274199 - remove use of FlushableTaskQueue. r=cpearce. (adc4c84ede) - Bug 1273405. Part 1 - rename some functions to be consistent with other MediaDataDecoder sub-classes. r=jya. (af123d6c21) - Bug 1273405. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. (2d144bfbcd) - Bug 1273405. Part 3 - remove use of FlushableTaskQueue. r=jya. (1e9ea3c2c7) - Bug 1273405. Part 4 - add assertions. r=jya. (b400647323) - Bug 1271491: [WMF] P1. Don't use main thread only preferences methods. r=cpearce (7177454dfb) - Bug 1262427. Don't try D3D11 harder. r=dvander (404147d6fa) - Use gfxConfig for D3D9 preferences. (bug 1270650, r=jrmuizel) (40d89c154c) - Bug 1271491: P2. Allow initialization of WMFPlatformDecoderModule from any threads. r=mattwoodrow (c8fe0bf009) - Bug 1271491: P3. Remove refcounting the number of time apple's linkers are called. r=cpearce (0324ffe876) - Bug 1271491: [ffmpeg] P4. Remove requirements to call Init on the main thread. r=cpearce (b511d7dfd5) - Bug 1271491: [GMP] P5. Allow GMPDecoderModule::Init() to be called off the main thread. r=cpearce (2131eb0b2e) - Bug 1266102 - Don't run VP9 benchmark on Android r=jya (57d7b573fe) - Bug 1271491: P6. Remove the need to call PDMFactory::Init(). r=cpearce (5726cfe49c) - Bug 1271491: P7. Remove unused members. r=alfredo (0f8a9dde73) - Bug 1268905 - Disable D3D11 with some Toshiba DLLs - r=cpearce (b5bf77442e) - Bug 1269204 - Disable D3D11 with idg10umd32 9.17.10.2857 - r=cpearce (7eb6a3d96b) - Bug 1273406 - Disable D3D11 with some iSonyVideoProcessor DLLs - r=cpearce (d9b6f0cefe) - Bug 1273406 - Ugly macros transform into beautiful constexpr goodness - r=cpearce (0671483695) - Bug 1273691 - Implement 'media.wmf.disable-d3d11-for-dlls' pref - r=cpearce (193ae53070) - Bug 1272225. Part 1 - add assertions to make thread constraints clear. r=jya. (83c620470e) - Bug 1272225. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. (9473e092d1) - Bug 1272553. Part 1 - move code around to be able to extract common code in P2. r=jya. (d727f97ee8) - Bug 1272553. Part 2 - extract common code to the parent class. r=jya. (2fb3cd4bd9) - Bug 1272553. Part 3 - make mTaskQueue private. r=jya. (93fea98cb6) - Bug 1272232. Part 1 - move code around so we can extract common code in P2. r=jya. (8cdaab9078) - Bug 1272232. Part 2 - extract common code to the parent class. r=jya. (27156668b3) - Bug 1272232. Part 3 - constify some members and make them private when possible. r=jya. (550b963d97) - Bug 1272232. Part 4 - remove use of FlushableTaskQueue::Flush(). r=jya. (bdbfdeb6bc) - Bug 1272232. Part 5 - remove use of FlushableTaskQueue. r=jya. (640f889a9d) - Bug 1274913 - Move PDM log definition to header. r=njn (823b07f42b) - Bug 1275538: P1. Abort early if a skip request is in progress. r=gerald,kamidphish (d67b8a2236) - Bug 1272422 - Part 1: Expose control of suspending background video. r=cpearce (ec7193773f) - Bug 1272422 - Part 2: Vidoe -> Video. r=cpearce (97390aee69) - Bug 1272422 - Part 3: Don't reset audio queue. r=jya (e183db1062) - Bug 1272964: P1. Only activate skip to next keyframe logic when next keyframe time is known. r=gerald (1be74df027) - Bug 1272964: P2. Don't activate skip to next keyframe until we passed the internal seek target. r=gerald (c55b6ae003) - Bug 1258922: [MSE] P1. Initialise variable. r=gerald (56a5acb345) - Bug 1258922: [MSE] P2. Do not go over gap when attempting to find the next key frame. r=gerald (db1319f080) - Bug 1258922: [MSE] P3. Check that the data we are attempting to skip to is buffered. r=gerald (621d71d5d6) - Bug 1258922: [MSE] P4. Set draining flag to true when skip to next keyframe failed. r=gerald (6c75613faf) - Bug 1272916: [MSE] P1. Don't rely only on dts gap to establish if we have a gap in our source buffer. r=gerald (8770113b83) - Bug 1272964: [MSE] P3. Do not skip over gaps when searching for the next keyframe. r=gerald (76916c5ac6) - Bug 1272964: P4. Only flush decoder if skip to next keyframe actually succeeds. r=cpearce (5394708eef) - Bug 1270323: P1. Don't reset flag indicating that new data was received. r=cpearce (d32f06ef34) - Bug 1270323: P2. Don't process new incoming data while a skip to next keyframe is pending. r=cpearce (bca7909de9) - Bug 1270323: [ffmpeg] P3. Use the dts of the last sample input, not the dts of the last decoded sample (0d768c33ef) - Bug 1270323: P4. Don't drain decoder if we're already waiting for new data. r=cpearce (679302cb6e) - Bug 1270323: P5. Prevent potential null deref. r=cpearce (cc63270e06) - Bug 1275538: P2. Drop decoded frames that we know are already too late. r=kamidphish (4e7af9398c) - Bug 1273018: P1. Rename some members. r=gerald (3a92fbd994) - Bug 1273018: P2. Don't reject audio waiting promise when performing a video only seek. r=gerald (34e4988db1) - Bug 1273018: P3. Adjust range of audio assertions. r=gerald (feb2afd0ae) - Bug 1249706 - Backout a085ea2d24bb for blowing telemetry server's mind. r=backout (d61fb51f52) - Bug 1249706 - Fix 8fe22dd4fc8a (backout of a085ea2d24bb). r=bustage (ba65251db7) - Bug 1272964: [MSE] P5. Default to skipping to the next keyframe if no keyframe was found past currentTime. (29086fcf56) - Bug 1272964: P6. Exclude frames dropped due to internal seeking from calculations. r=cpearce (bf6faa7612) - Bug 1068151 - keep decoding a corrupted video. r=jya (3b5462e5b6) - Bug 1273947 - Update ResetDecode() to ResetDecode(TargetQueue) r=jya (6c28d46974) - Bug 1277508: P1. Don't attempt to demux new samples while we're currently draining. r=kamidphish (64f200b921) - Bug 1274933: Reject data promise when EOS is encountered following waiting for data. r=gerald (5bba4a7853) - Bug 1277508: P2. Add HasPendingDrain convenience method. r=kamidphish (3d89a90a97)
745 lines
20 KiB
C++
745 lines
20 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
#include "Key.h"
|
|
|
|
#include <algorithm>
|
|
#include "IndexedDatabaseManager.h"
|
|
#include "js/Date.h"
|
|
#include "js/Value.h"
|
|
#include "jsfriendapi.h"
|
|
#include "mozilla/EndianUtils.h"
|
|
#include "mozilla/FloatingPoint.h"
|
|
#include "mozIStorageStatement.h"
|
|
#include "mozIStorageValueArray.h"
|
|
#include "nsAlgorithm.h"
|
|
#include "nsJSUtils.h"
|
|
#include "ReportInternalError.h"
|
|
#include "xpcpublic.h"
|
|
|
|
#ifdef ENABLE_INTL_API
|
|
#include "unicode/ucol.h"
|
|
#endif
|
|
|
|
namespace mozilla {
|
|
namespace dom {
|
|
namespace indexedDB {
|
|
|
|
/*
|
|
Here's how we encode keys:
|
|
|
|
Basic strategy is the following
|
|
|
|
Numbers: 0x10 n n n n n n n n ("n"s are encoded 64bit float)
|
|
Dates: 0x20 n n n n n n n n ("n"s are encoded 64bit float)
|
|
Strings: 0x30 s s s ... 0 ("s"s are encoded unicode bytes)
|
|
Arrays: 0x50 i i i ... 0 ("i"s are encoded array items)
|
|
|
|
|
|
When encoding floats, 64bit IEEE 754 are almost sortable, except that
|
|
positive sort lower than negative, and negative sort descending. So we use
|
|
the following encoding:
|
|
|
|
value < 0 ?
|
|
(-to64bitInt(value)) :
|
|
(to64bitInt(value) | 0x8000000000000000)
|
|
|
|
|
|
When encoding strings, we use variable-size encoding per the following table
|
|
|
|
Chars 0 - 7E are encoded as 0xxxxxxx with 1 added
|
|
Chars 7F - (3FFF+7F) are encoded as 10xxxxxx xxxxxxxx with 7F subtracted
|
|
Chars (3FFF+80) - FFFF are encoded as 11xxxxxx xxxxxxxx xx000000
|
|
|
|
This ensures that the first byte is never encoded as 0, which means that the
|
|
string terminator (per basic-stategy table) sorts before any character.
|
|
The reason that (3FFF+80) - FFFF is encoded "shifted up" 6 bits is to maximize
|
|
the chance that the last character is 0. See below for why.
|
|
|
|
|
|
When encoding Arrays, we use an additional trick. Rather than adding a byte
|
|
containing the value 0x50 to indicate type, we instead add 0x50 to the next byte.
|
|
This is usually the byte containing the type of the first item in the array.
|
|
So simple examples are
|
|
|
|
["foo"] 0x80 s s s 0 0 // 0x80 is 0x30 + 0x50
|
|
[1, 2] 0x60 n n n n n n n n 1 n n n n n n n n 0 // 0x60 is 0x10 + 0x50
|
|
|
|
Whe do this iteratively if the first item in the array is also an array
|
|
|
|
[["foo"]] 0xA0 s s s 0 0 0
|
|
|
|
However, to avoid overflow in the byte, we only do this 3 times. If the first
|
|
item in an array is an array, and that array also has an array as first item,
|
|
we simply write out the total value accumulated so far and then follow the
|
|
"normal" rules.
|
|
|
|
[[["foo"]]] 0xF0 0x30 s s s 0 0 0 0
|
|
|
|
There is another edge case that can happen though, which is that the array
|
|
doesn't have a first item to which we can add 0x50 to the type. Instead the
|
|
next byte would normally be the array terminator (per basic-strategy table)
|
|
so we simply add the 0x50 there.
|
|
|
|
[[]] 0xA0 0 // 0xA0 is 0x50 + 0x50 + 0
|
|
[] 0x50 // 0x50 is 0x50 + 0
|
|
[[], "foo"] 0xA0 0x30 s s s 0 0 // 0xA0 is 0x50 + 0x50 + 0
|
|
|
|
Note that the max-3-times rule kicks in before we get a chance to add to the
|
|
array terminator
|
|
|
|
[[[]]] 0xF0 0 0 0 // 0xF0 is 0x50 + 0x50 + 0x50
|
|
|
|
As a final optimization we do a post-encoding step which drops all 0s at the
|
|
end of the encoded buffer.
|
|
|
|
"foo" // 0x30 s s s
|
|
1 // 0x10 bf f0
|
|
["a", "b"] // 0x80 s 0 0x30 s
|
|
[1, 2] // 0x60 bf f0 0 0 0 0 0 0 0x10 c0
|
|
[[]] // 0x80
|
|
*/
|
|
#ifdef ENABLE_INTL_API
|
|
nsresult
|
|
Key::ToLocaleBasedKey(Key& aTarget, const nsCString& aLocale) const
|
|
{
|
|
if (IsUnset()) {
|
|
aTarget.Unset();
|
|
return NS_OK;
|
|
}
|
|
|
|
if (IsFloat() || IsDate()) {
|
|
aTarget.mBuffer = mBuffer;
|
|
return NS_OK;
|
|
}
|
|
|
|
aTarget.mBuffer.Truncate();
|
|
aTarget.mBuffer.SetCapacity(mBuffer.Length());
|
|
|
|
auto* it = reinterpret_cast<const unsigned char*>(mBuffer.BeginReading());
|
|
auto* end = reinterpret_cast<const unsigned char*>(mBuffer.EndReading());
|
|
|
|
// First we do a pass and see if there are any strings in this key. We only
|
|
// want to copy/decode when necessary.
|
|
bool canShareBuffers = true;
|
|
while (it < end) {
|
|
auto type = *it % eMaxType;
|
|
if (type == eTerminator || type == eArray) {
|
|
it++;
|
|
} else if (type == eFloat || type == eDate) {
|
|
it++;
|
|
it += std::min(sizeof(uint64_t), size_t(end - it));
|
|
} else {
|
|
// We have a string!
|
|
canShareBuffers = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (canShareBuffers) {
|
|
MOZ_ASSERT(it == end);
|
|
aTarget.mBuffer = mBuffer;
|
|
return NS_OK;
|
|
}
|
|
|
|
// A string was found, so we need to copy the data we've read so far
|
|
auto* start = reinterpret_cast<const unsigned char*>(mBuffer.BeginReading());
|
|
if (it > start) {
|
|
char* buffer;
|
|
if (!aTarget.mBuffer.GetMutableData(&buffer, it-start)) {
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
}
|
|
|
|
while (start < it) {
|
|
*(buffer++) = *(start++);
|
|
}
|
|
}
|
|
|
|
// Now continue decoding
|
|
while (it < end) {
|
|
char* buffer;
|
|
uint32_t oldLen = aTarget.mBuffer.Length();
|
|
auto type = *it % eMaxType;
|
|
|
|
if (type == eTerminator || type == eArray) {
|
|
// Copy array TypeID and terminator from raw key
|
|
if (!aTarget.mBuffer.GetMutableData(&buffer, oldLen + 1)) {
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
}
|
|
*(buffer + oldLen) = *(it++);
|
|
} else if (type == eFloat || type == eDate) {
|
|
// Copy number from raw key
|
|
if (!aTarget.mBuffer.GetMutableData(&buffer,
|
|
oldLen + 1 + sizeof(uint64_t))) {
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
}
|
|
buffer += oldLen;
|
|
*(buffer++) = *(it++);
|
|
|
|
const size_t byteCount = std::min(sizeof(uint64_t), size_t(end - it));
|
|
for (size_t count = 0; count < byteCount; count++) {
|
|
*(buffer++) = (*it++);
|
|
}
|
|
} else {
|
|
// Decode string and reencode
|
|
uint8_t typeOffset = *it - eString;
|
|
MOZ_ASSERT((typeOffset % eArray == 0) && (typeOffset / eArray <= 2));
|
|
|
|
nsDependentString str;
|
|
DecodeString(it, end, str);
|
|
aTarget.EncodeLocaleString(str, typeOffset, aLocale);
|
|
}
|
|
}
|
|
aTarget.TrimBuffer();
|
|
return NS_OK;
|
|
}
|
|
#endif
|
|
|
|
nsresult
|
|
Key::EncodeJSValInternal(JSContext* aCx, JS::Handle<JS::Value> aVal,
|
|
uint8_t aTypeOffset, uint16_t aRecursionDepth)
|
|
{
|
|
static_assert(eMaxType * kMaxArrayCollapse < 256,
|
|
"Unable to encode jsvals.");
|
|
|
|
if (NS_WARN_IF(aRecursionDepth == kMaxRecursionDepth)) {
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
}
|
|
|
|
if (aVal.isString()) {
|
|
nsAutoJSString str;
|
|
if (!str.init(aCx, aVal)) {
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
EncodeString(str, aTypeOffset);
|
|
return NS_OK;
|
|
}
|
|
|
|
if (aVal.isNumber()) {
|
|
double d = aVal.toNumber();
|
|
if (mozilla::IsNaN(d)) {
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
}
|
|
EncodeNumber(d, eFloat + aTypeOffset);
|
|
return NS_OK;
|
|
}
|
|
|
|
if (aVal.isObject()) {
|
|
JS::Rooted<JSObject*> obj(aCx, &aVal.toObject());
|
|
|
|
js::ESClassValue cls;
|
|
if (!js::GetBuiltinClass(aCx, obj, &cls)) {
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
if (cls == js::ESClass_Array) {
|
|
aTypeOffset += eMaxType;
|
|
|
|
if (aTypeOffset == eMaxType * kMaxArrayCollapse) {
|
|
mBuffer.Append(aTypeOffset);
|
|
aTypeOffset = 0;
|
|
}
|
|
NS_ASSERTION((aTypeOffset % eMaxType) == 0 &&
|
|
aTypeOffset < (eMaxType * kMaxArrayCollapse),
|
|
"Wrong typeoffset");
|
|
|
|
uint32_t length;
|
|
if (!JS_GetArrayLength(aCx, obj, &length)) {
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
|
|
for (uint32_t index = 0; index < length; index++) {
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
if (!JS_GetElement(aCx, obj, index, &val)) {
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
|
|
nsresult rv = EncodeJSValInternal(aCx, val, aTypeOffset,
|
|
aRecursionDepth + 1);
|
|
if (NS_FAILED(rv)) {
|
|
return rv;
|
|
}
|
|
|
|
aTypeOffset = 0;
|
|
}
|
|
|
|
mBuffer.Append(eTerminator + aTypeOffset);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
if (cls == js::ESClass_Date) {
|
|
bool valid;
|
|
if (!js::DateIsValid(aCx, obj, &valid)) {
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
if (!valid) {
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
}
|
|
double t;
|
|
if (!js::DateGetMsecSinceEpoch(aCx, obj, &t)) {
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
EncodeNumber(t, eDate + aTypeOffset);
|
|
return NS_OK;
|
|
}
|
|
}
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
}
|
|
|
|
// static
|
|
nsresult
|
|
Key::DecodeJSValInternal(const unsigned char*& aPos, const unsigned char* aEnd,
|
|
JSContext* aCx, uint8_t aTypeOffset, JS::MutableHandle<JS::Value> aVal,
|
|
uint16_t aRecursionDepth)
|
|
{
|
|
if (NS_WARN_IF(aRecursionDepth == kMaxRecursionDepth)) {
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
}
|
|
|
|
if (*aPos - aTypeOffset >= eArray) {
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, 0));
|
|
if (!array) {
|
|
NS_WARNING("Failed to make array!");
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
|
|
aTypeOffset += eMaxType;
|
|
|
|
if (aTypeOffset == eMaxType * kMaxArrayCollapse) {
|
|
++aPos;
|
|
aTypeOffset = 0;
|
|
}
|
|
|
|
uint32_t index = 0;
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
while (aPos < aEnd && *aPos - aTypeOffset != eTerminator) {
|
|
nsresult rv = DecodeJSValInternal(aPos, aEnd, aCx, aTypeOffset,
|
|
&val, aRecursionDepth + 1);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
aTypeOffset = 0;
|
|
|
|
if (!JS_DefineElement(aCx, array, index++, val, JSPROP_ENUMERATE)) {
|
|
NS_WARNING("Failed to set array element!");
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
}
|
|
|
|
NS_ASSERTION(aPos >= aEnd || (*aPos % eMaxType) == eTerminator,
|
|
"Should have found end-of-array marker");
|
|
++aPos;
|
|
|
|
aVal.setObject(*array);
|
|
}
|
|
else if (*aPos - aTypeOffset == eString) {
|
|
nsString key;
|
|
DecodeString(aPos, aEnd, key);
|
|
if (!xpc::StringToJsval(aCx, key, aVal)) {
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
}
|
|
else if (*aPos - aTypeOffset == eDate) {
|
|
double msec = static_cast<double>(DecodeNumber(aPos, aEnd));
|
|
JS::ClippedTime time = JS::TimeClip(msec);
|
|
MOZ_ASSERT(msec == time.toDouble(),
|
|
"encoding from a Date object not containing an invalid date "
|
|
"means we should always have clipped values");
|
|
JSObject* date = JS::NewDateObject(aCx, time);
|
|
if (!date) {
|
|
IDB_WARNING("Failed to make date!");
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
|
|
aVal.setObject(*date);
|
|
}
|
|
else if (*aPos - aTypeOffset == eFloat) {
|
|
aVal.setDouble(DecodeNumber(aPos, aEnd));
|
|
}
|
|
else {
|
|
NS_NOTREACHED("Unknown key type!");
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
#define ONE_BYTE_LIMIT 0x7E
|
|
#define TWO_BYTE_LIMIT (0x3FFF+0x7F)
|
|
|
|
#define ONE_BYTE_ADJUST 1
|
|
#define TWO_BYTE_ADJUST (-0x7F)
|
|
#define THREE_BYTE_SHIFT 6
|
|
|
|
nsresult
|
|
Key::EncodeJSVal(JSContext* aCx,
|
|
JS::Handle<JS::Value> aVal,
|
|
uint8_t aTypeOffset)
|
|
{
|
|
return EncodeJSValInternal(aCx, aVal, aTypeOffset, 0);
|
|
}
|
|
|
|
void
|
|
Key::EncodeString(const nsAString& aString, uint8_t aTypeOffset)
|
|
{
|
|
const char16_t* start = aString.BeginReading();
|
|
const char16_t* end = aString.EndReading();
|
|
EncodeString(start, end, aTypeOffset);
|
|
}
|
|
|
|
template <typename T>
|
|
void
|
|
Key::EncodeString(const T* aStart, const T* aEnd, uint8_t aTypeOffset)
|
|
{
|
|
// First measure how long the encoded string will be.
|
|
|
|
// The +2 is for initial 3 and trailing 0. We'll compensate for multi-byte
|
|
// chars below.
|
|
uint32_t size = (aEnd - aStart) + 2;
|
|
|
|
const T* start = aStart;
|
|
const T* end = aEnd;
|
|
for (const T* iter = start; iter < end; ++iter) {
|
|
if (*iter > ONE_BYTE_LIMIT) {
|
|
size += char16_t(*iter) > TWO_BYTE_LIMIT ? 2 : 1;
|
|
}
|
|
}
|
|
|
|
// Allocate memory for the new size
|
|
uint32_t oldLen = mBuffer.Length();
|
|
char* buffer;
|
|
if (!mBuffer.GetMutableData(&buffer, oldLen + size)) {
|
|
return;
|
|
}
|
|
buffer += oldLen;
|
|
|
|
// Write type marker
|
|
*(buffer++) = eString + aTypeOffset;
|
|
|
|
// Encode string
|
|
for (const T* iter = start; iter < end; ++iter) {
|
|
if (*iter <= ONE_BYTE_LIMIT) {
|
|
*(buffer++) = *iter + ONE_BYTE_ADJUST;
|
|
}
|
|
else if (char16_t(*iter) <= TWO_BYTE_LIMIT) {
|
|
char16_t c = char16_t(*iter) + TWO_BYTE_ADJUST + 0x8000;
|
|
*(buffer++) = (char)(c >> 8);
|
|
*(buffer++) = (char)(c & 0xFF);
|
|
}
|
|
else {
|
|
uint32_t c = (uint32_t(*iter) << THREE_BYTE_SHIFT) | 0x00C00000;
|
|
*(buffer++) = (char)(c >> 16);
|
|
*(buffer++) = (char)(c >> 8);
|
|
*(buffer++) = (char)c;
|
|
}
|
|
}
|
|
|
|
// Write end marker
|
|
*(buffer++) = eTerminator;
|
|
|
|
NS_ASSERTION(buffer == mBuffer.EndReading(), "Wrote wrong number of bytes");
|
|
}
|
|
|
|
#ifdef ENABLE_INTL_API
|
|
nsresult
|
|
Key::EncodeLocaleString(const nsDependentString& aString, uint8_t aTypeOffset,
|
|
const nsCString& aLocale)
|
|
{
|
|
const int length = aString.Length();
|
|
if (length == 0) {
|
|
return NS_OK;
|
|
}
|
|
const UChar* ustr = reinterpret_cast<const UChar*>(aString.BeginReading());
|
|
|
|
UErrorCode uerror = U_ZERO_ERROR;
|
|
UCollator* collator = ucol_open(aLocale.get(), &uerror);
|
|
if (NS_WARN_IF(U_FAILURE(uerror))) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
MOZ_ASSERT(collator);
|
|
|
|
AutoTArray<uint8_t, 128> keyBuffer;
|
|
int32_t sortKeyLength = ucol_getSortKey(collator, ustr, length,
|
|
keyBuffer.Elements(),
|
|
keyBuffer.Length());
|
|
if (sortKeyLength > (int32_t)keyBuffer.Length()) {
|
|
keyBuffer.SetLength(sortKeyLength);
|
|
sortKeyLength = ucol_getSortKey(collator, ustr, length,
|
|
keyBuffer.Elements(),
|
|
sortKeyLength);
|
|
}
|
|
|
|
ucol_close(collator);
|
|
if (NS_WARN_IF(sortKeyLength == 0)) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
EncodeString(keyBuffer.Elements(),
|
|
keyBuffer.Elements()+sortKeyLength,
|
|
aTypeOffset);
|
|
return NS_OK;
|
|
}
|
|
#endif
|
|
|
|
// static
|
|
nsresult
|
|
Key::DecodeJSVal(const unsigned char*& aPos,
|
|
const unsigned char* aEnd,
|
|
JSContext* aCx,
|
|
JS::MutableHandle<JS::Value> aVal)
|
|
{
|
|
return DecodeJSValInternal(aPos, aEnd, aCx, 0, aVal, 0);
|
|
}
|
|
|
|
// static
|
|
void
|
|
Key::DecodeString(const unsigned char*& aPos, const unsigned char* aEnd,
|
|
nsString& aString)
|
|
{
|
|
NS_ASSERTION(*aPos % eMaxType == eString, "Don't call me!");
|
|
|
|
const unsigned char* buffer = aPos + 1;
|
|
|
|
// First measure how big the decoded string will be.
|
|
uint32_t size = 0;
|
|
const unsigned char* iter;
|
|
for (iter = buffer; iter < aEnd && *iter != eTerminator; ++iter) {
|
|
if (*iter & 0x80) {
|
|
iter += (*iter & 0x40) ? 2 : 1;
|
|
}
|
|
++size;
|
|
}
|
|
|
|
// Set end so that we don't have to check for null termination in the loop
|
|
// below
|
|
if (iter < aEnd) {
|
|
aEnd = iter;
|
|
}
|
|
|
|
char16_t* out;
|
|
if (size && !aString.GetMutableData(&out, size)) {
|
|
return;
|
|
}
|
|
|
|
for (iter = buffer; iter < aEnd;) {
|
|
if (!(*iter & 0x80)) {
|
|
*out = *(iter++) - ONE_BYTE_ADJUST;
|
|
}
|
|
else if (!(*iter & 0x40)) {
|
|
char16_t c = (char16_t(*(iter++)) << 8);
|
|
if (iter < aEnd) {
|
|
c |= *(iter++);
|
|
}
|
|
*out = c - TWO_BYTE_ADJUST - 0x8000;
|
|
}
|
|
else {
|
|
uint32_t c = uint32_t(*(iter++)) << (16 - THREE_BYTE_SHIFT);
|
|
if (iter < aEnd) {
|
|
c |= uint32_t(*(iter++)) << (8 - THREE_BYTE_SHIFT);
|
|
}
|
|
if (iter < aEnd) {
|
|
c |= *(iter++) >> THREE_BYTE_SHIFT;
|
|
}
|
|
*out = (char16_t)c;
|
|
}
|
|
|
|
++out;
|
|
}
|
|
|
|
NS_ASSERTION(!size || out == aString.EndReading(),
|
|
"Should have written the whole string");
|
|
|
|
aPos = iter + 1;
|
|
}
|
|
|
|
union Float64Union {
|
|
double d;
|
|
uint64_t u;
|
|
};
|
|
|
|
void
|
|
Key::EncodeNumber(double aFloat, uint8_t aType)
|
|
{
|
|
// Allocate memory for the new size
|
|
uint32_t oldLen = mBuffer.Length();
|
|
char* buffer;
|
|
if (!mBuffer.GetMutableData(&buffer, oldLen + 1 + sizeof(double))) {
|
|
return;
|
|
}
|
|
buffer += oldLen;
|
|
|
|
*(buffer++) = aType;
|
|
|
|
Float64Union pun;
|
|
pun.d = aFloat;
|
|
// Note: The subtraction from 0 below is necessary to fix
|
|
// MSVC build warning C4146 (negating an unsigned value).
|
|
uint64_t number = pun.u & PR_UINT64(0x8000000000000000) ?
|
|
(0 - pun.u) :
|
|
(pun.u | PR_UINT64(0x8000000000000000));
|
|
|
|
mozilla::BigEndian::writeUint64(buffer, number);
|
|
}
|
|
|
|
// static
|
|
double
|
|
Key::DecodeNumber(const unsigned char*& aPos, const unsigned char* aEnd)
|
|
{
|
|
NS_ASSERTION(*aPos % eMaxType == eFloat ||
|
|
*aPos % eMaxType == eDate, "Don't call me!");
|
|
|
|
++aPos;
|
|
|
|
uint64_t number = 0;
|
|
memcpy(&number, aPos, std::min<size_t>(sizeof(number), aEnd - aPos));
|
|
number = mozilla::NativeEndian::swapFromBigEndian(number);
|
|
|
|
aPos += sizeof(number);
|
|
|
|
Float64Union pun;
|
|
// Note: The subtraction from 0 below is necessary to fix
|
|
// MSVC build warning C4146 (negating an unsigned value).
|
|
pun.u = number & PR_UINT64(0x8000000000000000) ?
|
|
(number & ~PR_UINT64(0x8000000000000000)) :
|
|
(0 - number);
|
|
|
|
return pun.d;
|
|
}
|
|
|
|
nsresult
|
|
Key::BindToStatement(mozIStorageStatement* aStatement,
|
|
const nsACString& aParamName) const
|
|
{
|
|
nsresult rv;
|
|
if (IsUnset()) {
|
|
rv = aStatement->BindNullByName(aParamName);
|
|
} else {
|
|
rv = aStatement->BindBlobByName(aParamName,
|
|
reinterpret_cast<const uint8_t*>(mBuffer.get()), mBuffer.Length());
|
|
}
|
|
|
|
return NS_SUCCEEDED(rv) ? NS_OK : NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
|
|
nsresult
|
|
Key::SetFromStatement(mozIStorageStatement* aStatement,
|
|
uint32_t aIndex)
|
|
{
|
|
return SetFromSource(aStatement, aIndex);
|
|
}
|
|
|
|
nsresult
|
|
Key::SetFromValueArray(mozIStorageValueArray* aValues,
|
|
uint32_t aIndex)
|
|
{
|
|
return SetFromSource(aValues, aIndex);
|
|
}
|
|
|
|
nsresult
|
|
Key::SetFromJSVal(JSContext* aCx,
|
|
JS::Handle<JS::Value> aVal)
|
|
{
|
|
mBuffer.Truncate();
|
|
|
|
if (aVal.isNull() || aVal.isUndefined()) {
|
|
Unset();
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult rv = EncodeJSVal(aCx, aVal, 0);
|
|
if (NS_FAILED(rv)) {
|
|
Unset();
|
|
return rv;
|
|
}
|
|
TrimBuffer();
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
Key::ToJSVal(JSContext* aCx,
|
|
JS::MutableHandle<JS::Value> aVal) const
|
|
{
|
|
if (IsUnset()) {
|
|
aVal.setUndefined();
|
|
return NS_OK;
|
|
}
|
|
|
|
const unsigned char* pos = BufferStart();
|
|
nsresult rv = DecodeJSVal(pos, BufferEnd(), aCx, aVal);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
|
|
MOZ_ASSERT(pos >= BufferEnd());
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
Key::ToJSVal(JSContext* aCx,
|
|
JS::Heap<JS::Value>& aVal) const
|
|
{
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
nsresult rv = ToJSVal(aCx, &value);
|
|
if (NS_SUCCEEDED(rv)) {
|
|
aVal = value;
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
nsresult
|
|
Key::AppendItem(JSContext* aCx, bool aFirstOfArray, JS::Handle<JS::Value> aVal)
|
|
{
|
|
nsresult rv = EncodeJSVal(aCx, aVal, aFirstOfArray ? eMaxType : 0);
|
|
if (NS_FAILED(rv)) {
|
|
Unset();
|
|
return rv;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
template <typename T>
|
|
nsresult
|
|
Key::SetFromSource(T* aSource, uint32_t aIndex)
|
|
{
|
|
const uint8_t* data;
|
|
uint32_t dataLength = 0;
|
|
|
|
nsresult rv = aSource->GetSharedBlob(aIndex, &dataLength, &data);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
}
|
|
|
|
mBuffer.Assign(reinterpret_cast<const char*>(data), dataLength);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
Key::Assert(bool aCondition) const
|
|
{
|
|
MOZ_ASSERT(aCondition);
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
} // namespace indexedDB
|
|
} // namespace dom
|
|
} // namespace mozilla
|