This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOSQLiteStorePersistentRootBackingStoreBinaryFormats.m
58 lines (48 loc) · 2.01 KB
/
COSQLiteStorePersistentRootBackingStoreBinaryFormats.m
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
#import "COSQLiteStorePersistentRootBackingStoreBinaryFormats.h"
#import <EtoileFoundation/ETUUID.h>
void ParseCombinedCommitDataInToUUIDToItemDataDictionary(NSMutableDictionary *dest, NSData *commitData, BOOL replaceExisting, NSSet *restrictToItemUUIDs)
{
// format:
//
// |------------|-----------------------|----------| |---..
// |128-bit UUID| uint_32 little-endian | item data| | next UUID...
// |------------|-----------------------|----------| |---..
// ^- number of bytes in item data
const unsigned char *bytes = [commitData bytes];
const NSUInteger len = [commitData length];
NSUInteger offset = 0;
while (offset < len)
{
ETUUID *uuid = [[ETUUID alloc] initWithUUID: bytes + offset];
offset += 16;
uint32_t length;
memcpy(&length, bytes + offset, 4);
length = CFSwapInt32LittleToHost(length);
offset += 4;
if ((replaceExisting
|| nil == [dest objectForKey: uuid])
&& (nil == restrictToItemUUIDs
|| [restrictToItemUUIDs containsObject: uuid]))
{
NSData *data = [commitData subdataWithRange: NSMakeRange(offset, length)];
[dest setObject: data
forKey: uuid];
}
[uuid release];
offset += length;
}
}
void AddCommitUUIDAndDataToCombinedCommitData(NSMutableData *combinedCommitData, ETUUID *uuidToAdd, NSData *dataToAdd)
{
// TODO: Benchmark, access UUID bytes directly via a pointer?
[combinedCommitData appendBytes: [uuidToAdd UUIDValue] length: 16];
const NSUInteger len = [dataToAdd length];
if (len > UINT32_MAX)
{
[NSException raise: NSInvalidArgumentException format: @"Can't write item data larger than 2^32-1 bytes"];
}
uint32_t swappedInt = CFSwapInt32HostToLittle((uint32_t)len);
[combinedCommitData appendBytes: &swappedInt
length: 4];
[combinedCommitData appendData: dataToAdd];
}