2011-12-20 14:58:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=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
|
|
|
|
2012-01-23 15:43:16 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
#include "Key.h"
|
2012-01-15 12:13:07 +04:00
|
|
|
#include "jsfriendapi.h"
|
2012-01-06 13:56:52 +04:00
|
|
|
#include "nsAlgorithm.h"
|
2011-12-20 14:58:44 +04:00
|
|
|
#include "nsJSUtils.h"
|
2011-12-24 12:27:04 +04:00
|
|
|
#include "xpcpublic.h"
|
2013-04-24 11:40:33 +04:00
|
|
|
#include "mozilla/Endian.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
USING_INDEXEDDB_NAMESPACE
|
|
|
|
|
|
|
|
/*
|
|
|
|
Here's how we encode keys:
|
|
|
|
|
|
|
|
Basic strategy is the following
|
|
|
|
|
|
|
|
Numbers: 1 n n n n n n n n ("n"s are encoded 64bit float)
|
|
|
|
Dates: 2 n n n n n n n n ("n"s are encoded 64bit float)
|
|
|
|
Strings: 3 s s s ... 0 ("s"s are encoded unicode bytes)
|
|
|
|
Arrays: 4 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 '4' to indicate type, we instead add 4 to the next byte.
|
|
|
|
This is usually the byte containing the type of the first item in the array.
|
|
|
|
So simple examples are
|
|
|
|
|
|
|
|
["foo"] 7 s s s 0 0 // 7 is 3 + 4
|
|
|
|
[1, 2] 5 n n n n n n n n 1 n n n n n n n n 0 // 5 is 1 + 4
|
|
|
|
|
|
|
|
Whe do this iteratively if the first item in the array is also an array
|
|
|
|
|
|
|
|
[["foo"]] 11 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"]]] 12 3 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 4 to the type. Instead the
|
|
|
|
next byte would normally be the array terminator (per basic-strategy table)
|
|
|
|
so we simply add the 4 there.
|
|
|
|
|
|
|
|
[[]] 8 0 // 8 is 4 + 4 + 0
|
|
|
|
[] 4 // 4 is 4 + 0
|
|
|
|
[[], "foo"] 8 3 s s s 0 0 // 8 is 4 + 4 + 0
|
|
|
|
|
|
|
|
Note that the max-3-times rule kicks in before we get a chance to add to the
|
|
|
|
array terminator
|
|
|
|
|
|
|
|
[[[]]] 12 0 0 0 // 12 is 4 + 4 + 4
|
|
|
|
|
|
|
|
We could use a much higher number than 3 at no complexity or performance cost,
|
|
|
|
however it seems unlikely that it'll make a practical difference, and the low
|
|
|
|
limit makes testing eaiser.
|
|
|
|
|
|
|
|
|
|
|
|
As a final optimization we do a post-encoding step which drops all 0s at the
|
|
|
|
end of the encoded buffer.
|
|
|
|
|
|
|
|
"foo" // 3 s s s
|
|
|
|
1 // 1 bf f0
|
|
|
|
["a", "b"] // 7 s 3 s
|
|
|
|
[1, 2] // 5 bf f0 0 0 0 0 0 0 1 c0
|
|
|
|
[[]] // 8
|
|
|
|
*/
|
|
|
|
|
|
|
|
const int MaxArrayCollapse = 3;
|
|
|
|
|
2012-05-11 21:47:27 +04:00
|
|
|
const int MaxRecursionDepth = 256;
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
nsresult
|
2014-01-23 23:49:40 +04:00
|
|
|
Key::EncodeJSValInternal(JSContext* aCx, JS::Handle<JS::Value> aVal,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aTypeOffset, uint16_t aRecursionDepth)
|
2011-12-20 14:58:44 +04:00
|
|
|
{
|
2012-05-11 21:47:27 +04:00
|
|
|
NS_ENSURE_TRUE(aRecursionDepth < MaxRecursionDepth, NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
|
|
|
|
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(eMaxType * MaxArrayCollapse < 256,
|
|
|
|
"Unable to encode jsvals.");
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2014-01-23 23:49:40 +04:00
|
|
|
if (aVal.isString()) {
|
2011-12-20 14:58:44 +04:00
|
|
|
nsDependentJSString str;
|
|
|
|
if (!str.init(aCx, aVal)) {
|
2012-05-11 21:47:27 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
EncodeString(str, aTypeOffset);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-23 23:49:40 +04:00
|
|
|
if (aVal.isNumber()) {
|
|
|
|
double d = aVal.toNumber();
|
2013-05-02 00:55:13 +04:00
|
|
|
if (mozilla::IsNaN(d)) {
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
EncodeNumber(d, eFloat + aTypeOffset);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-23 23:49:40 +04:00
|
|
|
if (aVal.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aVal.toObject());
|
2011-12-20 14:58:44 +04:00
|
|
|
if (JS_IsArrayObject(aCx, obj)) {
|
|
|
|
aTypeOffset += eMaxType;
|
|
|
|
|
|
|
|
if (aTypeOffset == eMaxType * MaxArrayCollapse) {
|
|
|
|
mBuffer.Append(aTypeOffset);
|
|
|
|
aTypeOffset = 0;
|
|
|
|
}
|
|
|
|
NS_ASSERTION((aTypeOffset % eMaxType) == 0 &&
|
|
|
|
aTypeOffset < (eMaxType * MaxArrayCollapse),
|
|
|
|
"Wrong typeoffset");
|
|
|
|
|
2012-03-07 03:52:55 +04:00
|
|
|
uint32_t length;
|
2011-12-20 14:58:44 +04:00
|
|
|
if (!JS_GetArrayLength(aCx, obj, &length)) {
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2012-03-07 03:52:55 +04:00
|
|
|
for (uint32_t index = 0; index < length; index++) {
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2013-08-05 17:02:47 +04:00
|
|
|
if (!JS_GetElement(aCx, obj, index, &val)) {
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
2012-05-11 21:47:27 +04:00
|
|
|
nsresult rv = EncodeJSValInternal(aCx, val, aTypeOffset,
|
|
|
|
aRecursionDepth + 1);
|
2012-06-01 21:21:12 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
aTypeOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer.Append(eTerminator + aTypeOffset);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JS_ObjectIsDate(aCx, obj)) {
|
2012-09-21 08:41:49 +04:00
|
|
|
if (!js_DateIsValid(obj)) {
|
2012-05-25 22:52:19 +04:00
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
2012-09-21 08:41:49 +04:00
|
|
|
EncodeNumber(js_DateGetMsecSinceEpoch(obj), eDate + aTypeOffset);
|
2011-12-20 14:58:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
2012-05-11 21:47:27 +04:00
|
|
|
Key::DecodeJSValInternal(const unsigned char*& aPos, const unsigned char* aEnd,
|
2013-06-18 14:00:37 +04:00
|
|
|
JSContext* aCx, uint8_t aTypeOffset, JS::MutableHandle<JS::Value> aVal,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t aRecursionDepth)
|
2011-12-20 14:58:44 +04:00
|
|
|
{
|
2012-05-11 21:47:27 +04:00
|
|
|
NS_ENSURE_TRUE(aRecursionDepth < MaxRecursionDepth, NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
|
|
|
|
|
2011-12-20 14:58:44 +04:00
|
|
|
if (*aPos - aTypeOffset >= eArray) {
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, 0, nullptr));
|
2011-12-20 14:58:44 +04:00
|
|
|
if (!array) {
|
|
|
|
NS_WARNING("Failed to make array!");
|
|
|
|
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aTypeOffset += eMaxType;
|
|
|
|
|
|
|
|
if (aTypeOffset == eMaxType * MaxArrayCollapse) {
|
|
|
|
++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,
|
2013-06-18 14:00:37 +04:00
|
|
|
&val, aRecursionDepth + 1);
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aTypeOffset = 0;
|
|
|
|
|
2013-08-05 17:02:47 +04:00
|
|
|
if (!JS_SetElement(aCx, array, index++, &val)) {
|
2011-12-20 14:58:44 +04:00
|
|
|
NS_WARNING("Failed to set array element!");
|
|
|
|
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)) {
|
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));
|
2011-12-20 14:58:44 +04:00
|
|
|
JSObject* date = JS_NewDateObjectMsec(aCx, msec);
|
|
|
|
if (!date) {
|
|
|
|
NS_WARNING("Failed to make date!");
|
|
|
|
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));
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
Key::EncodeString(const nsAString& aString, uint8_t aTypeOffset)
|
2011-12-20 14:58:44 +04:00
|
|
|
{
|
|
|
|
// 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.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size = aString.Length() + 2;
|
2011-12-20 14:58:44 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* start = aString.BeginReading();
|
|
|
|
const char16_t* end = aString.EndReading();
|
|
|
|
for (const char16_t* iter = start; iter < end; ++iter) {
|
2011-12-20 14:58:44 +04:00
|
|
|
if (*iter > ONE_BYTE_LIMIT) {
|
|
|
|
size += *iter > TWO_BYTE_LIMIT ? 2 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 + size)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer += oldLen;
|
|
|
|
|
|
|
|
// Write type marker
|
|
|
|
*(buffer++) = eString + aTypeOffset;
|
|
|
|
|
|
|
|
// Encode string
|
2014-01-04 19:02:17 +04:00
|
|
|
for (const char16_t* iter = start; iter < end; ++iter) {
|
2011-12-20 14:58:44 +04:00
|
|
|
if (*iter <= ONE_BYTE_LIMIT) {
|
|
|
|
*(buffer++) = *iter + ONE_BYTE_ADJUST;
|
|
|
|
}
|
|
|
|
else if (*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;
|
|
|
|
|
|
|
|
NS_ASSERTION(buffer == mBuffer.EndReading(), "Wrote wrong number of bytes");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size = 0;
|
2011-12-20 14:58:44 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* out;
|
2011-12-20 14:58:44 +04:00
|
|
|
if (size && !aString.GetMutableData(&out, size)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (iter = buffer; iter < aEnd;) {
|
|
|
|
if (!(*iter & 0x80)) {
|
|
|
|
*out = *(iter++) - ONE_BYTE_ADJUST;
|
|
|
|
}
|
|
|
|
else if (!(*iter & 0x40)) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c = (char16_t(*(iter++)) << 8);
|
2011-12-20 14:58:44 +04:00
|
|
|
if (iter < aEnd) {
|
|
|
|
c |= *(iter++);
|
|
|
|
}
|
|
|
|
*out = c - TWO_BYTE_ADJUST - 0x8000;
|
|
|
|
}
|
|
|
|
else {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t c = uint32_t(*(iter++)) << (16 - THREE_BYTE_SHIFT);
|
2011-12-20 14:58:44 +04:00
|
|
|
if (iter < aEnd) {
|
2012-08-22 19:56:38 +04:00
|
|
|
c |= uint32_t(*(iter++)) << (8 - THREE_BYTE_SHIFT);
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
if (iter < aEnd) {
|
|
|
|
c |= *(iter++) >> THREE_BYTE_SHIFT;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
*out = (char16_t)c;
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
++out;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!size || out == aString.EndReading(),
|
|
|
|
"Should have written the whole string");
|
|
|
|
|
|
|
|
aPos = iter + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
union Float64Union {
|
|
|
|
double d;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t u;
|
2011-12-20 14:58:44 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
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;
|
|
|
|
|
|
|
|
Float64Union pun;
|
|
|
|
pun.d = 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).
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t number = pun.u & PR_UINT64(0x8000000000000000) ?
|
2013-04-21 00:36:43 +04:00
|
|
|
(0 - pun.u) :
|
2011-12-20 14:58:44 +04:00
|
|
|
(pun.u | PR_UINT64(0x8000000000000000));
|
|
|
|
|
2013-04-24 11:40:33 +04:00
|
|
|
mozilla::BigEndian::writeUint64(buffer, number);
|
2011-12-20 14:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
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
|
|
|
|
|
|
|
Float64Union pun;
|
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).
|
2011-12-20 14:58:44 +04:00
|
|
|
pun.u = number & PR_UINT64(0x8000000000000000) ?
|
|
|
|
(number & ~PR_UINT64(0x8000000000000000)) :
|
2013-04-21 00:36:43 +04:00
|
|
|
(0 - number);
|
2011-12-20 14:58:44 +04:00
|
|
|
|
|
|
|
return pun.d;
|
|
|
|
}
|