forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Key.cpp
424 lines (338 loc) · 11.7 KB
/
Key.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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 "mozilla/FloatingPoint.h"
#include "Key.h"
#include "nsIStreamBufferAccess.h"
#include "jsfriendapi.h"
#include "nsAlgorithm.h"
#include "nsJSUtils.h"
#include "xpcpublic.h"
#include <algorithm>
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;
const int MaxRecursionDepth = 256;
nsresult
Key::EncodeJSValInternal(JSContext* aCx, const jsval aVal,
uint8_t aTypeOffset, uint16_t aRecursionDepth)
{
NS_ENSURE_TRUE(aRecursionDepth < MaxRecursionDepth, NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
MOZ_STATIC_ASSERT(eMaxType * MaxArrayCollapse < 256,
"Unable to encode jsvals.");
if (JSVAL_IS_STRING(aVal)) {
nsDependentJSString str;
if (!str.init(aCx, aVal)) {
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
EncodeString(str, aTypeOffset);
return NS_OK;
}
if (JSVAL_IS_INT(aVal)) {
EncodeNumber((double)JSVAL_TO_INT(aVal), eFloat + aTypeOffset);
return NS_OK;
}
if (JSVAL_IS_DOUBLE(aVal)) {
double d = JSVAL_TO_DOUBLE(aVal);
if (MOZ_DOUBLE_IS_NaN(d)) {
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
}
EncodeNumber(d, eFloat + aTypeOffset);
return NS_OK;
}
if (!JSVAL_IS_PRIMITIVE(aVal)) {
JSObject* obj = JSVAL_TO_OBJECT(aVal);
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");
uint32_t length;
if (!JS_GetArrayLength(aCx, obj, &length)) {
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
for (uint32_t index = 0; index < length; index++) {
jsval val;
if (!JS_GetElement(aCx, obj, index, &val)) {
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 (JS_ObjectIsDate(aCx, obj)) {
if (!js_DateIsValid(obj)) {
return NS_ERROR_DOM_INDEXEDDB_DATA_ERR;
}
EncodeNumber(js_DateGetMsecSinceEpoch(obj), 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, jsval* aVal,
uint16_t aRecursionDepth)
{
NS_ENSURE_TRUE(aRecursionDepth < MaxRecursionDepth, NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
if (*aPos - aTypeOffset >= eArray) {
JSObject* array = JS_NewArrayObject(aCx, 0, nullptr);
if (!array) {
NS_WARNING("Failed to make array!");
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
aTypeOffset += eMaxType;
if (aTypeOffset == eMaxType * MaxArrayCollapse) {
++aPos;
aTypeOffset = 0;
}
uint32_t index = 0;
while (aPos < aEnd && *aPos - aTypeOffset != eTerminator) {
jsval val;
nsresult rv = DecodeJSValInternal(aPos, aEnd, aCx, aTypeOffset,
&val, aRecursionDepth + 1);
NS_ENSURE_SUCCESS(rv, rv);
aTypeOffset = 0;
if (!JS_SetElement(aCx, array, index++, &val)) {
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;
*aVal = OBJECT_TO_JSVAL(array);
}
else if (*aPos - aTypeOffset == eString) {
nsString key;
DecodeString(aPos, aEnd, key);
if (!xpc::StringToJsval(aCx, key, aVal)) {
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
}
else if (*aPos - aTypeOffset == eDate) {
double msec = static_cast<double>(DecodeNumber(aPos, aEnd));
JSObject* date = JS_NewDateObjectMsec(aCx, msec);
if (!date) {
NS_WARNING("Failed to make date!");
return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR;
}
*aVal = OBJECT_TO_JSVAL(date);
}
else if (*aPos - aTypeOffset == eFloat) {
*aVal = DOUBLE_TO_JSVAL(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
void
Key::EncodeString(const nsAString& aString, 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 = aString.Length() + 2;
const PRUnichar* start = aString.BeginReading();
const PRUnichar* end = aString.EndReading();
for (const PRUnichar* iter = start; iter < end; ++iter) {
if (*iter > ONE_BYTE_LIMIT) {
size += *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 PRUnichar* iter = start; iter < end; ++iter) {
if (*iter <= ONE_BYTE_LIMIT) {
*(buffer++) = *iter + ONE_BYTE_ADJUST;
}
else if (*iter <= TWO_BYTE_LIMIT) {
PRUnichar c = PRUnichar(*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");
}
// 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;
}
PRUnichar* 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)) {
PRUnichar c = (PRUnichar(*(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 = (PRUnichar)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;
uint64_t number = pun.u & PR_UINT64(0x8000000000000000) ?
-pun.u :
(pun.u | PR_UINT64(0x8000000000000000));
number = NS_SWAP64(number);
memcpy(buffer, &number, sizeof(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 = NS_SWAP64(number);
aPos += sizeof(number);
Float64Union pun;
pun.u = number & PR_UINT64(0x8000000000000000) ?
(number & ~PR_UINT64(0x8000000000000000)) :
-number;
return pun.d;
}