CryptoPassphrase  Diff

Differences From Artifact [5a420f4ea2]:

To Artifact [47936472f3]:


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
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







-
+






+


-
+




-
-
-
+
+
+
+

-
-
+
+
+

-
-
+
+

-
-
-
-
-
-
+
+
+
+
+
+

-
-
+
+
-



















+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+

-
-
+
+

-
-
+
+
+










-
+









-
+







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



+

-
+
+
+

+
+
+
+
+
+
-
-
+
+
-
-
-
-
+
+
-










-
+
-
-
+

-
-
-
+
+
+
-



#import "SiteStorage.h"

@interface SiteStorage ()
- (void)_update;
@end

static OFNumber *lengthField, *legacyField;
static OFNumber *lengthField, *legacyField, *keyFileField;

@implementation SiteStorage
+ (void)initialize
{
	lengthField = [@(UINT8_C(0)) retain];
	legacyField = [@(UINT8_C(1)) retain];
	keyFileField = [@(UINT8_C(2)) retain];
}

- init
- (instancetype)init
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFFileManager *fileManager = [OFFileManager defaultManager];
		OFString *userDataPath = [OFSystemInfo userDataPath];
		@autoreleasepool {
			OFFileManager *fileManager =
			    OFFileManager.defaultManager;
			OFString *userDataPath = OFSystemInfo.userDataPath;

		if (![fileManager directoryExistsAtPath: userDataPath])
			[fileManager createDirectoryAtPath: userDataPath];
			if (![fileManager directoryExistsAtPath: userDataPath])
				[fileManager
				    createDirectoryAtPath: userDataPath];

		_path = [[userDataPath stringByAppendingPathComponent:
		    @"sites.msgpack"] retain];
			_path = [[userDataPath stringByAppendingPathComponent:
			    @"sites.msgpack"] copy];

		@try {
			_storage = [[[OFData dataWithContentsOfFile: _path]
			    messagePackValue] mutableCopy];
		} @catch (id e) {
			_storage = [[OFMutableDictionary alloc] init];
		}
			@try {
				_storage = [[OFData dataWithContentsOfFile:
				    _path].messagePackValue mutableCopy];
			} @catch (id e) {
				_storage = [[OFMutableDictionary alloc] init];
			}

		_sites = [[[_storage allKeys] sortedArray] retain];

			_sites = [_storage.allKeys.sortedArray retain];
		}
		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_path release];
	[_storage release];
	[_sites release];

	[super dealloc];
}

- (OFArray<OFString *> *)sitesWithFilter: (OFString *)filter
{
	OFArray<OFString *> *sites;

	void *pool = objc_autoreleasePoolPush();
	/*
	 * FIXME: We need case folding here, but there is no method for it yet.
	 */
	filter = [filter lowercaseString];
	OFArray *sites = [[[_storage allKeys] sortedArray]
	    filteredArrayUsingBlock: ^ (id name, size_t index) {
		if (filter == nil)
			return true;
	@autoreleasepool {
		/*
		 * FIXME: We need case folding here, but there is no method for
		 *	  it yet.
		 */
		filter = filter.lowercaseString;
		sites = [_storage.allKeys.sortedArray
		    filteredArrayUsingBlock: ^ (OFString *name, size_t index) {
			if (filter == nil)
				return true;

		return [[name lowercaseString] containsString: filter];
	}];
			return [name.lowercaseString containsString: filter];
		}];

	[sites retain];
	objc_autoreleasePoolPop(pool);
		[sites retain];
	}

	return [sites autorelease];
}

- (bool)hasSite: (OFString *)name
{
	return (_storage[name] != nil);
}

- (size_t)lengthForSite: (OFString *)name
{
	OFDictionary *site = _storage[name];
	OFDictionary<OFNumber *, id> *site = _storage[name];

	if (site == nil)
		@throw [OFInvalidArgumentException exception];

	return [site[lengthField] sizeValue];
}

- (bool)isSiteLegacy: (OFString *)name
{
	OFDictionary *site = _storage[name];
	OFDictionary<OFNumber *, id> *site = _storage[name];

	if (site == nil)
		@throw [OFInvalidArgumentException exception];

	return [site[legacyField] boolValue];
}

- (OFString *)keyFileForSite: (OFString *)name
{
	OFDictionary<OFNumber *, id> *site = _storage[name];
	OFString *keyFile;

	if (site == nil)
		@throw [OFInvalidArgumentException exception];

	keyFile = site[keyFileField];

	if ([keyFile isEqual: [OFNull null]])
		return nil;

	return keyFile;
}

- (void)setSite: (OFString *)site
	 length: (size_t)length
	 legacy: (bool)legacy
	keyFile: (OFString *)keyFile
{
	void *pool = objc_autoreleasePoolPush();
	@autoreleasepool {
		OFMutableDictionary *siteDictionary =
		    [OFMutableDictionary dictionary];

		siteDictionary[lengthField] = @(length);
		siteDictionary[legacyField] = @(legacy);
		siteDictionary[keyFileField] = keyFile;

		[siteDictionary makeImmutable];

	_storage[site] = @{
		lengthField: @(length),
		_storage[site] = siteDictionary;

		legacyField: @(legacy)
	};
	[self _update];

		[self _update];
	}
	objc_autoreleasePoolPop(pool);
}

- (void)removeSite: (OFString *)name
{
	[_storage removeObjectForKey: name];
	[self _update];
}

- (void)_update
{
	void *pool = objc_autoreleasePoolPush();
	@autoreleasepool {

	[[_storage messagePackRepresentation] writeToFile: _path];
		[_storage.messagePackRepresentation writeToFile: _path];

	[_sites release];
	_sites = [[[_storage allKeys] sortedArray] retain];

		[_sites release];
		_sites = [_storage.allKeys.sortedArray retain];
	}
	objc_autoreleasePoolPop(pool);
}
@end