2011-12-20 14:58:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
#include "Key.h"
|
2014-01-28 04:37:05 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
#include <algorithm>
|
2017-06-19 14:03:06 +03:00
|
|
|
#include <stdint.h> // for UINT32_MAX, uintptr_t
|
2014-08-05 00:59:57 +04:00
|
|
|
#include "IndexedDatabaseManager.h"
|
2019-03-05 02:19:16 +03:00
|
|
|
#include "js/ArrayBuffer.h" // JS::{IsArrayBufferObject,NewArrayBuffer{,WithContents},GetArrayBufferLengthAndData}
|
2015-05-02 05:12:52 +03:00
|
|
|
#include "js/Date.h"
|
2018-11-22 05:11:15 +03:00
|
|
|
#include "js/MemoryFunctions.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "js/Value.h"
|
2012-01-15 12:13:07 +04:00
|
|
|
#include "jsfriendapi.h"
|
2016-10-24 20:22:54 +03:00
|
|
|
#include "mozilla/Casting.h"
|
2017-06-19 14:03:06 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2016-05-22 23:31:11 +03:00
|
|
|
#include "mozilla/EndianUtils.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozIStorageStatement.h"
|
2015-02-16 20:48:14 +03:00
|
|
|
#include "mozIStorageValueArray.h"
|
2012-01-06 13:56:52 +04:00
|
|
|
#include "nsAlgorithm.h"
|
2011-12-20 14:58:44 +04:00
|
|
|
#include "nsJSUtils.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "ReportInternalError.h"
|
2014-08-05 00:59:57 +04:00
|
|
|
#include "unicode/ucol.h"
|
2017-09-25 19:32:23 +03:00
|
|
|
#include "xpcpublic.h"
|
2014-08-05 00:59:57 +04:00
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace indexedDB {
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
Here's how we encode keys:
|
|
|
|
|
|
|
|
Basic strategy is the following
|
|
|
|
|
2016-08-12 10:03:59 +03:00
|
|
|
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)
|
|
|
|
Binaries: 0x40 s s s ... 0 ("s"s are encoded unicode bytes)
|
|
|
|
Arrays: 0x50 i i i ... 0 ("i"s are encoded array items)
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
value < 0 ?
|
|
|
|
(-to64bitInt(value)) :
|
|
|
|
(to64bitInt(value) | 0x8000000000000000)
|
|
|
|
|
|
|
|
|
|
|
|
When encoding strings, we use variable-size encoding per the following table
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
Chars 0 - 7E are encoded as 0xxxxxxx with 1 added
|
2018-11-28 03:54:56 +03:00
|
|
|
Chars 7F - (3FFF+7F) are encoded as 10xxxxxx xxxxxxxx with 7F
|
|
|
|
subtracted
|
2011-12-20 14:58:44 +04:00
|
|
|
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
|
2019-07-26 23:00:03 +03:00
|
|
|
string terminator (per basic-stategy table) sorts before any character.
|
2011-12-20 14:58:44 +04:00
|
|
|
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.
|
|
|
|
|
2016-08-12 10:03:59 +03:00
|
|
|
When encoding binaries, the algorithm is the same to how strings are encoded.
|
|
|
|
Since each octet in binary is in the range of [0-255], it'll take 1 to 2
|
|
|
|
encoded unicode bytes.
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
When encoding Arrays, we use an additional trick. Rather than adding a byte
|
2015-02-16 20:48:14 +03:00
|
|
|
containing the value 0x50 to indicate type, we instead add 0x50 to the next
|
2011-12-20 14:58:44 +04:00
|
|
|
byte. This is usually the byte containing the type of the first item in the
|
|
|
|
array. So simple examples are
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
["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
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
Whe do this iteratively if the first item in the array is also an array
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
[["foo"]] 0xA0 s s s 0 0 0
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
[[["foo"]]] 0xF0 0x30 s s s 0 0 0 0
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
There is another edge case that can happen though, which is that the array
|
2015-02-16 20:48:14 +03:00
|
|
|
doesn't have a first item to which we can add 0x50 to the type. Instead the
|
2011-12-20 14:58:44 +04:00
|
|
|
next byte would normally be the array terminator (per basic-strategy table)
|
2015-02-16 20:48:14 +03:00
|
|
|
so we simply add the 0x50 there.
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
[[]] 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
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
Note that the max-3-times rule kicks in before we get a chance to add to the
|
|
|
|
array terminator
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
[[[]]] 0xF0 0 0 0 // 0xF0 is 0x50 + 0x50 + 0x50
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
As a final optimization we do a post-encoding step which drops all 0s at the
|
|
|
|
end of the encoded buffer.
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
"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
|
2011-12-20 14:58:44 +04:00
|
|
|
*/
|
2017-09-25 19:32:23 +03:00
|
|
|
|
2019-04-20 04:34:50 +03:00
|
|
|
// |aPos| should point to the type indicator.
|
|
|
|
// The returned length doesn't include the type indicator
|
|
|
|
// or the terminator.
|
2019-07-26 23:00:03 +03:00
|
|
|
static size_t LengthOfEncodedBinary(const unsigned char* aPos,
|
|
|
|
const unsigned char* aEnd) {
|
2019-04-20 04:34:50 +03:00
|
|
|
MOZ_ASSERT(*aPos % Key::eMaxType == Key::eBinary, "Don't call me!");
|
2019-07-26 23:00:03 +03:00
|
|
|
auto iter = ++aPos;
|
|
|
|
for (; iter < aEnd && *iter != Key::eTerminator; ++iter) {
|
|
|
|
if (*iter & 0x80) {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iter - aPos;
|
2019-04-20 04:34:50 +03:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::ToLocaleBasedKey(Key& aTarget, const nsCString& aLocale) const {
|
2014-08-05 00:59:57 +04:00
|
|
|
if (IsUnset()) {
|
|
|
|
aTarget.Unset();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_OK;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:03:59 +03:00
|
|
|
if (IsFloat() || IsDate() || IsBinary()) {
|
2014-08-05 00:59:57 +04:00
|
|
|
aTarget.mBuffer = mBuffer;
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_OK;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aTarget.mBuffer.Truncate();
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
auto* it = reinterpret_cast<const unsigned char*>(mBuffer.BeginReading());
|
|
|
|
auto* end = reinterpret_cast<const unsigned char*>(mBuffer.EndReading());
|
2014-08-05 00:59:57 +04:00
|
|
|
|
|
|
|
// 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;
|
2019-04-20 04:34:50 +03:00
|
|
|
if (type == eTerminator) {
|
2014-08-05 00:59:57 +04:00
|
|
|
it++;
|
|
|
|
} else if (type == eFloat || type == eDate) {
|
|
|
|
it++;
|
|
|
|
it += std::min(sizeof(uint64_t), size_t(end - it));
|
2019-04-20 04:34:50 +03:00
|
|
|
} else if (type == eBinary) {
|
|
|
|
// skip all binary data
|
|
|
|
auto binaryLength = LengthOfEncodedBinary(it, end);
|
|
|
|
it++;
|
|
|
|
it += binaryLength;
|
2014-08-05 00:59:57 +04:00
|
|
|
} else {
|
|
|
|
// We have a string!
|
|
|
|
canShareBuffers = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canShareBuffers) {
|
|
|
|
MOZ_ASSERT(it == end);
|
|
|
|
aTarget.mBuffer = mBuffer;
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_OK;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
2018-09-25 18:36:31 +03:00
|
|
|
aTarget.mBuffer.SetCapacity(mBuffer.Length());
|
|
|
|
|
2014-08-05 00:59:57 +04:00
|
|
|
// A string was found, so we need to copy the data we've read so far
|
2019-07-26 23:00:03 +03:00
|
|
|
auto* start = reinterpret_cast<const unsigned char*>(mBuffer.BeginReading());
|
2014-08-05 00:59:57 +04:00
|
|
|
if (it > start) {
|
|
|
|
char* buffer;
|
|
|
|
if (!aTarget.mBuffer.GetMutableData(&buffer, it - start)) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:34:50 +03:00
|
|
|
std::copy(start, it, buffer);
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now continue decoding
|
|
|
|
while (it < end) {
|
|
|
|
char* buffer;
|
|
|
|
uint32_t oldLen = aTarget.mBuffer.Length();
|
|
|
|
auto type = *it % eMaxType;
|
|
|
|
|
2019-04-20 04:34:50 +03:00
|
|
|
// Note: Do not modify |it| before calling |updateBufferAndIter|;
|
|
|
|
// |byteCount| doesn't include the type indicator
|
|
|
|
auto updateBufferAndIter = [&](size_t byteCount) -> bool {
|
|
|
|
if (!aTarget.mBuffer.GetMutableData(&buffer, oldLen + 1 + byteCount)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
buffer += oldLen;
|
|
|
|
|
|
|
|
// should also copy the type indicator at the begining
|
|
|
|
std::copy_n(it, byteCount + 1, buffer);
|
|
|
|
it += (byteCount + 1);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (type == eTerminator) {
|
2014-08-05 00:59:57 +04:00
|
|
|
// Copy array TypeID and terminator from raw key
|
2019-04-20 04:34:50 +03:00
|
|
|
if (!updateBufferAndIter(0)) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
} else if (type == eFloat || type == eDate) {
|
|
|
|
// Copy number from raw key
|
2019-04-20 04:34:50 +03:00
|
|
|
const size_t byteCount = std::min(sizeof(uint64_t), size_t(end - it - 1));
|
|
|
|
|
|
|
|
if (!updateBufferAndIter(byteCount)) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
2019-04-20 04:34:50 +03:00
|
|
|
} else if (type == eBinary) {
|
|
|
|
// skip all binary data
|
|
|
|
auto binaryLength = LengthOfEncodedBinary(it, end);
|
2014-08-05 00:59:57 +04:00
|
|
|
|
2019-04-20 04:34:50 +03:00
|
|
|
if (!updateBufferAndIter(binaryLength)) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Decode string and reencode
|
|
|
|
uint8_t typeOffset = *it - eString;
|
|
|
|
MOZ_ASSERT((typeOffset % eArray == 0) && (typeOffset / eArray <= 2));
|
|
|
|
|
|
|
|
nsDependentString str;
|
|
|
|
DecodeString(it, end, str);
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult rv = aTarget.EncodeLocaleString(str, typeOffset, aLocale);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2017-06-19 14:03:06 +03:00
|
|
|
}
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aTarget.TrimBuffer();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_OK;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::EncodeJSValInternal(JSContext* aCx, JS::Handle<JS::Value> aVal,
|
|
|
|
uint8_t aTypeOffset,
|
|
|
|
uint16_t aRecursionDepth) {
|
2019-07-26 17:14:22 +03:00
|
|
|
static_assert(eMaxType * kMaxArrayCollapse < 256, "Unable to encode jsvals.");
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2019-07-26 17:14:22 +03:00
|
|
|
if (NS_WARN_IF(aRecursionDepth == kMaxRecursionDepth)) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2019-07-26 17:14:22 +03:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
if (aVal.isString()) {
|
|
|
|
nsAutoJSString str;
|
|
|
|
if (!str.init(aCx, aVal)) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2019-07-26 17:14:22 +03:00
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
return EncodeString(str, aTypeOffset);
|
2019-07-26 17:14:22 +03:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
if (aVal.isNumber()) {
|
|
|
|
double d = aVal.toNumber();
|
|
|
|
if (mozilla::IsNaN(d)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
EncodeNumber(d, eFloat + aTypeOffset);
|
|
|
|
return NS_OK;
|
2019-07-26 17:14:22 +03:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2019-07-26 17:14:22 +03:00
|
|
|
if (aVal.isObject()) {
|
2019-07-26 23:00:03 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aVal.toObject());
|
2019-07-26 17:14:22 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
js::ESClass cls;
|
|
|
|
if (!js::GetBuiltinClass(aCx, obj, &cls)) {
|
2019-07-26 17:14:22 +03:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2019-07-26 17:14:22 +03:00
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
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");
|
2019-07-26 17:14:22 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
uint32_t length;
|
|
|
|
if (!JS_GetArrayLength(aCx, obj, &length)) {
|
2015-08-29 07:55:40 +03:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2012-05-25 22:52:19 +04:00
|
|
|
}
|
2019-07-26 17:14:22 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
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;
|
2015-08-29 07:55:40 +03:00
|
|
|
}
|
2019-07-26 17:14:22 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
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;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2016-08-12 10:03:59 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
if (JS::IsArrayBufferObject(obj)) {
|
|
|
|
return EncodeBinary(obj, /* aIsViewObject */ false, aTypeOffset);
|
2016-08-12 10:03:59 +03:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
if (JS_IsArrayBufferViewObject(obj)) {
|
|
|
|
return EncodeBinary(obj, /* aIsViewObject */ true, aTypeOffset);
|
2016-08-12 10:03:59 +03:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::DecodeJSValInternal(const unsigned char*& aPos,
|
|
|
|
const unsigned char* aEnd, JSContext* aCx,
|
2013-06-18 14:00:37 +04:00
|
|
|
uint8_t aTypeOffset,
|
|
|
|
JS::MutableHandle<JS::Value> aVal,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t aRecursionDepth) {
|
2015-02-16 20:48:14 +03:00
|
|
|
if (NS_WARN_IF(aRecursionDepth == kMaxRecursionDepth)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2012-05-11 21:47:27 +04:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
if (*aPos - aTypeOffset >= eArray) {
|
2014-02-12 14:50:46 +04:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, 0));
|
2011-12-20 14:58:44 +04:00
|
|
|
if (!array) {
|
|
|
|
NS_WARNING("Failed to make array!");
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aTypeOffset += eMaxType;
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
if (aTypeOffset == eMaxType * kMaxArrayCollapse) {
|
2011-12-20 14:58:44 +04:00
|
|
|
++aPos;
|
|
|
|
aTypeOffset = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-07 03:52:55 +04:00
|
|
|
uint32_t index = 0;
|
2013-08-05 17:02:47 +04:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2011-12-20 14:58:44 +04:00
|
|
|
while (aPos < aEnd && *aPos - aTypeOffset != eTerminator) {
|
2012-05-11 21:47:27 +04:00
|
|
|
nsresult rv = DecodeJSValInternal(aPos, aEnd, aCx, aTypeOffset, &val,
|
2013-06-18 14:00:37 +04:00
|
|
|
aRecursionDepth + 1);
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aTypeOffset = 0;
|
|
|
|
|
2015-03-02 16:31:15 +03:00
|
|
|
if (!JS_DefineElement(aCx, array, index++, val, JSPROP_ENUMERATE)) {
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_WARNING("Failed to set array element!");
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(aPos >= aEnd || (*aPos % eMaxType) == eTerminator,
|
|
|
|
"Should have found end-of-array marker");
|
|
|
|
++aPos;
|
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
aVal.setObject(*array);
|
2011-12-20 14:58:44 +04:00
|
|
|
} else if (*aPos - aTypeOffset == eString) {
|
|
|
|
nsString key;
|
|
|
|
DecodeString(aPos, aEnd, key);
|
2013-10-12 09:02:39 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, key, aVal)) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
} else if (*aPos - aTypeOffset == eDate) {
|
2012-02-25 02:19:52 +04:00
|
|
|
double msec = static_cast<double>(DecodeNumber(aPos, aEnd));
|
2015-05-02 05:12:52 +03:00
|
|
|
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);
|
2011-12-20 14:58:44 +04:00
|
|
|
if (!date) {
|
2014-01-28 04:37:05 +04:00
|
|
|
IDB_WARNING("Failed to make date!");
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:37 +04:00
|
|
|
aVal.setObject(*date);
|
2011-12-20 14:58:44 +04:00
|
|
|
} else if (*aPos - aTypeOffset == eFloat) {
|
2013-06-18 14:00:37 +04:00
|
|
|
aVal.setDouble(DecodeNumber(aPos, aEnd));
|
2016-08-12 10:03:59 +03:00
|
|
|
} else if (*aPos - aTypeOffset == eBinary) {
|
|
|
|
JSObject* binary = DecodeBinary(aPos, aEnd, aCx);
|
|
|
|
if (!binary) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aVal.setObject(*binary);
|
2011-12-20 14:58:44 +04:00
|
|
|
} else {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown key type!");
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::EncodeJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal,
|
|
|
|
uint8_t aTypeOffset) {
|
|
|
|
return EncodeJSValInternal(aCx, aVal, aTypeOffset, 0);
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::EncodeString(const nsAString& aString, uint8_t aTypeOffset) {
|
2014-08-05 00:59:57 +04:00
|
|
|
const char16_t* start = aString.BeginReading();
|
|
|
|
const char16_t* end = aString.EndReading();
|
2019-07-26 23:00:03 +03:00
|
|
|
return EncodeString(start, end, aTypeOffset);
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::EncodeString(const T* aStart, const T* aEnd,
|
|
|
|
uint8_t aTypeOffset) {
|
|
|
|
return EncodeAsString(aStart, aEnd, eString + aTypeOffset);
|
2016-08-12 10:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::EncodeAsString(const T* aStart, const T* aEnd, uint8_t aType) {
|
2011-12-20 14:58:44 +04:00
|
|
|
// First measure how long the encoded string will be.
|
2017-06-19 14:03:06 +03:00
|
|
|
if (NS_WARN_IF(aStart > aEnd || UINT32_MAX - 2 < uintptr_t(aEnd - aStart))) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2017-06-19 14:03:06 +03:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
// The +2 is for initial 3 and trailing 0. We'll compensate for multi-byte
|
2011-12-20 14:58:44 +04:00
|
|
|
// chars below.
|
2017-06-19 14:03:06 +03:00
|
|
|
uint32_t checkedSize = aEnd - aStart;
|
|
|
|
CheckedUint32 size = checkedSize;
|
|
|
|
size += 2;
|
|
|
|
|
|
|
|
MOZ_ASSERT(size.isValid());
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2014-08-05 00:59:57 +04:00
|
|
|
const T* start = aStart;
|
|
|
|
const T* end = aEnd;
|
|
|
|
for (const T* iter = start; iter < end; ++iter) {
|
2011-12-20 14:58:44 +04:00
|
|
|
if (*iter > ONE_BYTE_LIMIT) {
|
2014-08-05 00:59:57 +04:00
|
|
|
size += char16_t(*iter) > TWO_BYTE_LIMIT ? 2 : 1;
|
2017-06-19 14:03:06 +03:00
|
|
|
if (!size.isValid()) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2017-06-19 14:03:06 +03:00
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate memory for the new size
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldLen = mBuffer.Length();
|
2017-06-19 14:03:06 +03:00
|
|
|
size += oldLen;
|
|
|
|
|
|
|
|
if (!size.isValid()) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2017-06-19 14:03:06 +03:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
char* buffer;
|
2017-06-19 14:03:06 +03:00
|
|
|
if (!mBuffer.GetMutableData(&buffer, size.value())) {
|
|
|
|
IDB_REPORT_INTERNAL_ERR();
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
buffer += oldLen;
|
|
|
|
|
|
|
|
// Write type marker
|
2016-08-12 10:03:59 +03:00
|
|
|
*(buffer++) = aType;
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
// Encode string
|
2014-08-05 00:59:57 +04:00
|
|
|
for (const T* iter = start; iter < end; ++iter) {
|
2011-12-20 14:58:44 +04:00
|
|
|
if (*iter <= ONE_BYTE_LIMIT) {
|
|
|
|
*(buffer++) = *iter + ONE_BYTE_ADJUST;
|
2014-08-05 00:59:57 +04:00
|
|
|
} else if (char16_t(*iter) <= TWO_BYTE_LIMIT) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c = char16_t(*iter) + TWO_BYTE_ADJUST + 0x8000;
|
2011-12-20 14:58:44 +04:00
|
|
|
*(buffer++) = (char)(c >> 8);
|
|
|
|
*(buffer++) = (char)(c & 0xFF);
|
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t c = (uint32_t(*iter) << THREE_BYTE_SHIFT) | 0x00C00000;
|
2011-12-20 14:58:44 +04:00
|
|
|
*(buffer++) = (char)(c >> 16);
|
|
|
|
*(buffer++) = (char)(c >> 8);
|
|
|
|
*(buffer++) = (char)c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write end marker
|
|
|
|
*(buffer++) = eTerminator;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(buffer == mBuffer.EndReading(), "Wrote wrong number of bytes");
|
2017-06-19 14:03:06 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_OK;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::EncodeLocaleString(const nsDependentString& aString,
|
|
|
|
uint8_t aTypeOffset,
|
|
|
|
const nsCString& aLocale) {
|
2014-08-05 00:59:57 +04:00
|
|
|
const int length = aString.Length();
|
|
|
|
if (length == 0) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_OK;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
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))) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(collator);
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<uint8_t, 128> keyBuffer;
|
2014-08-05 00:59:57 +04:00
|
|
|
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)) {
|
2019-07-26 23:00:03 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
2017-06-19 14:03:06 +03:00
|
|
|
return EncodeString(keyBuffer.Elements(),
|
2019-07-26 23:00:03 +03:00
|
|
|
keyBuffer.Elements() + sortKeyLength, aTypeOffset);
|
2014-08-05 00:59:57 +04:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
// static
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::DecodeJSVal(const unsigned char*& aPos, const unsigned char* aEnd,
|
|
|
|
JSContext* aCx, JS::MutableHandle<JS::Value> aVal) {
|
2015-02-16 20:48:14 +03:00
|
|
|
return DecodeJSValInternal(aPos, aEnd, aCx, 0, aVal, 0);
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
// static
|
2019-07-26 23:00:03 +03:00
|
|
|
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) {
|
2011-12-20 14:58:44 +04:00
|
|
|
if (*iter & 0x80) {
|
2019-07-26 23:00:03 +03:00
|
|
|
iter += (*iter & 0x40) ? 2 : 1;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
++size;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
// 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;) {
|
2011-12-20 14:58:44 +04:00
|
|
|
if (!(*iter & 0x80)) {
|
2019-07-26 23:00:03 +03:00
|
|
|
*out = *(iter++) - ONE_BYTE_ADJUST;
|
|
|
|
} else if (!(*iter & 0x40)) {
|
|
|
|
char16_t c = (char16_t(*(iter++)) << 8);
|
|
|
|
if (iter < aEnd) {
|
2011-12-20 14:58:44 +04:00
|
|
|
c |= *(iter++);
|
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
*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);
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
if (iter < aEnd) {
|
2011-12-20 14:58:44 +04:00
|
|
|
c |= *(iter++) >> THREE_BYTE_SHIFT;
|
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
*out = (char16_t)c;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
++out;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
NS_ASSERTION(!size || out == aString.EndReading(),
|
|
|
|
"Should have written the whole string");
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
aPos = iter + 1;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void Key::EncodeNumber(double aFloat, uint8_t aType) {
|
2011-12-20 14:58:44 +04:00
|
|
|
// Allocate memory for the new size
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldLen = mBuffer.Length();
|
2011-12-20 14:58:44 +04:00
|
|
|
char* buffer;
|
|
|
|
if (!mBuffer.GetMutableData(&buffer, oldLen + 1 + sizeof(double))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer += oldLen;
|
|
|
|
|
|
|
|
*(buffer++) = aType;
|
|
|
|
|
2016-10-24 20:22:54 +03:00
|
|
|
uint64_t bits = BitwiseCast<uint64_t>(aFloat);
|
2013-04-21 00:36:43 +04:00
|
|
|
// Note: The subtraction from 0 below is necessary to fix
|
|
|
|
// MSVC build warning C4146 (negating an unsigned value).
|
2016-10-24 20:22:54 +03:00
|
|
|
const uint64_t signbit = FloatingPoint<double>::kSignBit;
|
|
|
|
uint64_t number = bits & signbit ? (0 - bits) : (bits | signbit);
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2013-04-24 11:40:33 +04:00
|
|
|
mozilla::BigEndian::writeUint64(buffer, number);
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2019-07-26 23:00:03 +03:00
|
|
|
double Key::DecodeNumber(const unsigned char*& aPos,
|
|
|
|
const unsigned char* aEnd) {
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ASSERTION(*aPos % eMaxType == eFloat || *aPos % eMaxType == eDate,
|
|
|
|
"Don't call me!");
|
|
|
|
|
|
|
|
++aPos;
|
2012-01-06 13:56:52 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t number = 0;
|
2013-01-15 16:22:03 +04:00
|
|
|
memcpy(&number, aPos, std::min<size_t>(sizeof(number), aEnd - aPos));
|
2013-04-24 11:40:33 +04:00
|
|
|
number = mozilla::NativeEndian::swapFromBigEndian(number);
|
2012-01-06 13:56:52 +04:00
|
|
|
|
|
|
|
aPos += sizeof(number);
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2013-04-21 00:36:43 +04:00
|
|
|
// Note: The subtraction from 0 below is necessary to fix
|
|
|
|
// MSVC build warning C4146 (negating an unsigned value).
|
2016-10-24 20:22:54 +03:00
|
|
|
const uint64_t signbit = FloatingPoint<double>::kSignBit;
|
|
|
|
uint64_t bits = number & signbit ? (number & ~signbit) : (0 - number);
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2016-10-24 20:22:54 +03:00
|
|
|
return BitwiseCast<double>(bits);
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::EncodeBinary(JSObject* aObject, bool aIsViewObject,
|
|
|
|
uint8_t aTypeOffset) {
|
2016-08-12 10:03:59 +03:00
|
|
|
uint8_t* bufferData;
|
|
|
|
uint32_t bufferLength;
|
|
|
|
bool unused;
|
|
|
|
|
|
|
|
if (aIsViewObject) {
|
2019-04-17 15:59:20 +03:00
|
|
|
// We must use JS_GetObjectAsArrayBufferView() instead of
|
|
|
|
// js::GetArrayBufferLengthAndData(). Because we check ArrayBufferView
|
|
|
|
// via JS_IsArrayBufferViewObject(), the object might be wrapped,
|
|
|
|
// the former will handle the wrapped case, the later won't.
|
|
|
|
JS_GetObjectAsArrayBufferView(aObject, &bufferLength, &unused, &bufferData);
|
|
|
|
|
2016-08-12 10:03:59 +03:00
|
|
|
} else {
|
2019-03-05 02:19:16 +03:00
|
|
|
JS::GetArrayBufferLengthAndData(aObject, &bufferLength, &unused,
|
2016-08-12 10:03:59 +03:00
|
|
|
&bufferData);
|
|
|
|
}
|
|
|
|
|
2017-06-19 14:03:06 +03:00
|
|
|
return EncodeAsString(bufferData, bufferData + bufferLength,
|
2019-07-26 23:00:03 +03:00
|
|
|
eBinary + aTypeOffset);
|
2016-08-12 10:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2019-07-26 23:00:03 +03:00
|
|
|
JSObject* Key::DecodeBinary(const unsigned char*& aPos,
|
|
|
|
const unsigned char* aEnd, JSContext* aCx) {
|
|
|
|
MOZ_ASSERT(*aPos % eMaxType == eBinary, "Don't call me!");
|
|
|
|
|
|
|
|
const unsigned char* buffer = ++aPos;
|
|
|
|
|
|
|
|
// First measure how big the decoded array buffer will be.
|
|
|
|
size_t size = 0;
|
|
|
|
const unsigned char* iter;
|
|
|
|
for (iter = buffer; iter < aEnd && *iter != eTerminator; ++iter) {
|
|
|
|
if (*iter & 0x80) {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
++size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
return JS::NewArrayBuffer(aCx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* out = static_cast<uint8_t*>(JS_malloc(aCx, size));
|
|
|
|
if (NS_WARN_IF(!out)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* pos = out;
|
|
|
|
|
|
|
|
// Set end so that we don't have to check for null termination in the loop
|
|
|
|
// below
|
|
|
|
if (iter < aEnd) {
|
|
|
|
aEnd = iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (iter = buffer; iter < aEnd;) {
|
|
|
|
if (!(*iter & 0x80)) {
|
|
|
|
*pos = *(iter++) - ONE_BYTE_ADJUST;
|
|
|
|
} else {
|
|
|
|
uint16_t c = (uint16_t(*(iter++)) << 8);
|
|
|
|
if (iter < aEnd) {
|
|
|
|
c |= *(iter++);
|
|
|
|
}
|
|
|
|
*pos = static_cast<uint8_t>(c - TWO_BYTE_ADJUST - 0x8000);
|
|
|
|
}
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
aPos = iter + 1;
|
|
|
|
|
|
|
|
MOZ_ASSERT(static_cast<size_t>(pos - out) == size,
|
|
|
|
"Should have written the whole buffer");
|
|
|
|
|
|
|
|
return JS::NewArrayBufferWithContents(aCx, size, out);
|
2016-08-12 10:03:59 +03:00
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
nsresult Key::BindToStatement(mozIStorageStatement* aStatement,
|
|
|
|
const nsACString& aParamName) const {
|
2015-09-04 22:12:29 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (IsUnset()) {
|
|
|
|
rv = aStatement->BindNullByName(aParamName);
|
|
|
|
} else {
|
|
|
|
rv = aStatement->BindBlobByName(
|
|
|
|
aParamName, reinterpret_cast<const uint8_t*>(mBuffer.get()),
|
|
|
|
mBuffer.Length());
|
|
|
|
}
|
2014-09-27 03:21:57 +04:00
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv) ? NS_OK : NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Key::SetFromStatement(mozIStorageStatement* aStatement,
|
|
|
|
uint32_t aIndex) {
|
2015-02-16 20:48:14 +03:00
|
|
|
return SetFromSource(aStatement, aIndex);
|
|
|
|
}
|
2015-03-31 05:45:55 +03:00
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
nsresult Key::SetFromValueArray(mozIStorageValueArray* aValues,
|
|
|
|
uint32_t aIndex) {
|
|
|
|
return SetFromSource(aValues, aIndex);
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
void Key::SetFromJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal,
|
|
|
|
ErrorResult& aRv) {
|
2014-09-27 03:21:57 +04:00
|
|
|
mBuffer.Truncate();
|
|
|
|
|
|
|
|
if (aVal.isNull() || aVal.isUndefined()) {
|
|
|
|
Unset();
|
2019-07-26 23:00:03 +03:00
|
|
|
return;
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
aRv = EncodeJSVal(aCx, aVal, 0);
|
|
|
|
if (aRv.Failed()) {
|
2014-09-27 03:21:57 +04:00
|
|
|
Unset();
|
2019-07-26 23:00:03 +03:00
|
|
|
return;
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
TrimBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Key::ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aVal) const {
|
|
|
|
if (IsUnset()) {
|
|
|
|
aVal.setUndefined();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
const unsigned char* pos = BufferStart();
|
2015-02-16 20:48:14 +03:00
|
|
|
nsresult rv = DecodeJSVal(pos, BufferEnd(), aCx, aVal);
|
2014-09-27 03:21:57 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:00:03 +03:00
|
|
|
nsresult Key::AppendItem(JSContext* aCx, bool aFirstOfArray,
|
|
|
|
JS::Handle<JS::Value> aVal) {
|
|
|
|
nsresult rv = EncodeJSVal(aCx, aVal, aFirstOfArray ? eMaxType : 0);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-09-27 03:21:57 +04:00
|
|
|
Unset();
|
2019-07-26 23:00:03 +03:00
|
|
|
return rv;
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
2019-07-26 23:00:03 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2014-09-27 03:21:57 +04:00
|
|
|
}
|
|
|
|
|
2015-02-16 20:48:14 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:21:57 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
void Key::Assert(bool aCondition) const { MOZ_ASSERT(aCondition); }
|
|
|
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
} // namespace indexedDB
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|