ObjFW  Check-in [65b31518fc]

Overview
Comment:OFWindowsRegistryKey: Support for binary values
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 65b31518fcbb7e0d3f9a289d06a43d278c72d937adf29031b813389631bbec4f
User & Date: js on 2019-02-10 23:58:30
Other Links: manifest | tags
Context
2019-02-17
03:05
runtime/linklib: Use CONSTRUCTOR_P on MorphOS check-in: 98e5c779c8 user: js tags: trunk
2019-02-10
23:58
OFWindowsRegistryKey: Support for binary values check-in: 65b31518fc user: js tags: trunk
2019-02-09
17:44
OFASN1*: Add designated initializer check-in: c60933a59b user: js tags: trunk
Changes

Modified src/OFDNSResolver.m from [037adc045f] to [2303b7b240].

1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213

	[self of_setDefaults];

#if defined(OF_WINDOWS)
# ifdef OF_HAVE_FILES
	path = [[OFWindowsRegistryKey localMachineKey]
	    stringForValue: @"DataBasePath"
		subKeyPath: @"SYSTEM\\CurrentControlSet\\Services\\"
			    @"Tcpip\\Parameters"];
	path = [path stringByAppendingPathComponent: @"hosts"];

	if (path != nil)
		[self of_parseHosts: path];
# endif








|







1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213

	[self of_setDefaults];

#if defined(OF_WINDOWS)
# ifdef OF_HAVE_FILES
	path = [[OFWindowsRegistryKey localMachineKey]
	    stringForValue: @"DataBasePath"
		subkeyPath: @"SYSTEM\\CurrentControlSet\\Services\\"
			    @"Tcpip\\Parameters"];
	path = [path stringByAppendingPathComponent: @"hosts"];

	if (path != nil)
		[self of_parseHosts: path];
# endif

Modified src/OFWindowsRegistryKey.h from [a97b607515] to [e752ad4a7b].

18
19
20
21
22
23
24


25
26
27
28
29
30
31
#import "OFObject.h"
#import "OFString.h"

#include <windows.h>

OF_ASSUME_NONNULL_BEGIN



/*!
 * @class OFWindowsRegistryKey \
 *	  OFWindowsRegistryKey.h ObjFW/OFWindowsRegistryKey.h
 */
@interface OFWindowsRegistryKey: OFObject
{
	HKEY _hKey;







>
>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#import "OFObject.h"
#import "OFString.h"

#include <windows.h>

OF_ASSUME_NONNULL_BEGIN

@class OFData;

/*!
 * @class OFWindowsRegistryKey \
 *	  OFWindowsRegistryKey.h ObjFW/OFWindowsRegistryKey.h
 */
@interface OFWindowsRegistryKey: OFObject
{
	HKEY _hKey;
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
	 securityAttributes: (nullable LPSECURITY_ATTRIBUTES)securityAttributes
		disposition: (nullable LPDWORD)disposition;

/*!
 * @brief Returns the string for the specified value at the specified path.
 *
 * @param value The name of the value to return
 * @param subKeyPath The path of the key from which to retrieve the value
 * @return The string for the specified value
 */
- (nullable OFString *)stringForValue: (nullable OFString *)value
			   subKeyPath: (nullable OFString *)subKeyPath;

/*!
 * @brief Returns the string for the specified value at the specified path.
 *
 * @param value The name of the value to return
 * @param subKeyPath The path of the key from which to retrieve the value
 * @param flags Extra flags for `RegGetValue()`. Usually 0.
 * @param type A pointer to store the type of the value, or NULL
 * @return The string for the specified value
 */
- (nullable OFString *)stringForValue: (nullable OFString *)value
			   subKeyPath: (nullable OFString *)subKeyPath
				flags: (DWORD)flags
				 type: (nullable LPDWORD)type;
@end

OF_ASSUME_NONNULL_END







|



|


|


|


|

|
|





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
	 securityAttributes: (nullable LPSECURITY_ATTRIBUTES)securityAttributes
		disposition: (nullable LPDWORD)disposition;

/*!
 * @brief Returns the string for the specified value at the specified path.
 *
 * @param value The name of the value to return
 * @param subkeyPath The path of the key from which to retrieve the value
 * @return The string for the specified value
 */
- (nullable OFString *)stringForValue: (nullable OFString *)value
			   subkeyPath: (nullable OFString *)subkeyPath;

/*!
 * @brief Returns the data for the specified value at the specified path.
 *
 * @param value The name of the value to return
 * @param subkeyPath The path of the key from which to retrieve the value
 * @param flags Extra flags for `RegGetValue()`. Usually 0.
 * @param type A pointer to store the type of the value, or NULL
 * @return The data for the specified value
 */
- (nullable OFData *)dataForValue: (nullable OFString *)value
		       subkeyPath: (nullable OFString *)subkeyPath
				flags: (DWORD)flags
				 type: (nullable LPDWORD)type;
@end

OF_ASSUME_NONNULL_END

Modified src/OFWindowsRegistryKey.m from [4590e9ff33] to [d4e7766886].

14
15
16
17
18
19
20

21
22
23
24

25
26
27
28
29
30
31
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFWindowsRegistryKey.h"


#include <windows.h>

#import "OFCreateWindowsRegistryKeyFailedException.h"

#import "OFOpenWindowsRegistryKeyFailedException.h"
#import "OFReadWindowsRegistryValueFailedException.h"

@interface OFWindowsRegistryKey ()
- (instancetype)of_initWithHKey: (HKEY)hKey
			  close: (bool)close;
@end







>




>







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFWindowsRegistryKey.h"
#import "OFData.h"

#include <windows.h>

#import "OFCreateWindowsRegistryKeyFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFOpenWindowsRegistryKeyFailedException.h"
#import "OFReadWindowsRegistryValueFailedException.h"

@interface OFWindowsRegistryKey ()
- (instancetype)of_initWithHKey: (HKEY)hKey
			  close: (bool)close;
@end
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

	return [[[OFWindowsRegistryKey alloc] of_initWithHKey: subKey
							close: true]
	    autorelease];
}

- (OFString *)stringForValue: (OFString *)value
		  subKeyPath: (OFString *)subKeyPath
{

	return [self stringForValue: value
			 subKeyPath: subKeyPath
			      flags: 0
			       type: NULL];



}






























- (OFString *)stringForValue: (OFString *)value
		  subKeyPath: (OFString *)subKeyPath
		       flags: (DWORD)flags
			type: (LPDWORD)type
{
	void *pool = objc_autoreleasePoolPush();
	of_char16_t stackBuffer[256], *buffer = stackBuffer;
	DWORD length = sizeof(stackBuffer);

	LSTATUS status;
	OFString *ret;


	if ((status = RegGetValueW(_hKey, [subKeyPath UTF16String],
	    [value UTF16String], flags | RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ,
	    type, buffer, &length)) != ERROR_SUCCESS) {
		OFObject *tmp;

		if (status == ERROR_FILE_NOT_FOUND) {


			objc_autoreleasePoolPop(pool);
			return nil;
		}

		if (status != ERROR_MORE_DATA)
			@throw [OFReadWindowsRegistryValueFailedException
			    exceptionWithRegistryKey: self
					       value: value
					  subKeyPath: subKeyPath
					       flags: flags
					      status: status];

		tmp = [[[OFObject alloc] init] autorelease];
		buffer = [tmp allocMemoryWithSize: length];

		if ((status = RegGetValueW(_hKey, [subKeyPath UTF16String],
		    [value UTF16String], flags | RRF_RT_REG_SZ |
		    RRF_RT_REG_EXPAND_SZ, NULL, buffer, &length)) !=
		    ERROR_SUCCESS)
			@throw [OFReadWindowsRegistryValueFailedException
			    exceptionWithRegistryKey: self
					       value: value
					  subKeyPath: subKeyPath
					       flags: flags
					      status: status];
	}

	/*
	 * We do not specify a length, as the length returned by RegGetValue()
	 * sometimes seems to be larger than the string.
	 */
	ret = [[OFString alloc] initWithUTF16String: buffer];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}























@end







|

>
|
|
|

>
>
>
|
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|




|

>

<

>
|
|
<
<

|
>
>

<
|
|
<
<
<
|
<
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

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

	return [[[OFWindowsRegistryKey alloc] of_initWithHKey: subKey
							close: true]
	    autorelease];
}

- (OFString *)stringForValue: (OFString *)value
		  subkeyPath: (OFString *)subkeyPath
{
	void *pool = objc_autoreleasePoolPush();
	OFData *data = [self dataForValue: value
			       subkeyPath: subkeyPath
				    flags: RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ
			       type: NULL];
	const of_char16_t *UTF16String;
	size_t length;
	OFString *ret;

	if (data == nil)
		return nil;

	UTF16String = [data items];
	length = [data count];

	if ([data itemSize] != 1 || length % 2 == 1)
		@throw [OFInvalidFormatException exception];

	length /= 2;

	/*
	 * REG_SZ and REG_EXPAND_SZ contain a \0, but can contain data after it
	 * that should be ignored.
	 */
	for (size_t i = 0; i < length; i++) {
		if (UTF16String[i] == 0) {
			length = i;
			break;
		}
	}

	ret = [[OFString alloc] initWithUTF16String: UTF16String
					     length: length];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}

- (OFData *)dataForValue: (OFString *)value
	      subkeyPath: (OFString *)subkeyPath
		       flags: (DWORD)flags
			type: (LPDWORD)type
{
	void *pool = objc_autoreleasePoolPush();
	char stackBuffer[256], *buffer = stackBuffer;
	DWORD length = sizeof(stackBuffer);
	OFMutableData *ret = nil;
	LSTATUS status;


	for (;;) {
		status = RegGetValueW(_hKey, [subkeyPath UTF16String],
		    [value UTF16String], flags, type, buffer, &length);



		switch (status) {
		case ERROR_SUCCESS:
			if (buffer == stackBuffer) {
			objc_autoreleasePoolPop(pool);


				return [OFData dataWithItems: buffer



						       count: length];



			} else {


				[ret makeImmutable];











				[ret retain];






	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
		case ERROR_FILE_NOT_FOUND:
			objc_autoreleasePoolPop(pool);

			return nil;
		case ERROR_MORE_DATA:
			objc_autoreleasePoolPop(pool);
			pool = objc_autoreleasePoolPush();

			ret = [OFMutableData dataWithCapacity: length];
			[ret increaseCountBy: length];
			buffer = [ret items];

			continue;
		default:
			@throw [OFReadWindowsRegistryValueFailedException
			    exceptionWithRegistryKey: self
					       value: value
					  subkeyPath: subkeyPath
					       flags: flags
					      status: status];
		}
	}
}
@end

Modified src/exceptions/OFCreateWindowsRegistryKeyFailedException.h from [76ae84142f] to [3f883dcbc0].

whitespace changes only

Modified src/exceptions/OFCreateWindowsRegistryKeyFailedException.m from [9fce5e80ec] to [26992d06a4].

whitespace changes only

Modified src/exceptions/OFOpenWindowsRegistryKeyFailedException.h from [32884eb323] to [3f6ec7bdbe].

whitespace changes only

Modified src/exceptions/OFOpenWindowsRegistryKeyFailedException.m from [5b80fd3032] to [1c8f2244f2].

whitespace changes only

Modified src/exceptions/OFReadWindowsRegistryValueFailedException.h from [91824bda00] to [aa62c3dfb2].

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
 *	  ObjFW/OFReadWindowsRegistryValueFailedException.h
 *
 * @brief An exception indicating that reading a Windows registry value failed.
 */
@interface OFReadWindowsRegistryValueFailedException: OFException
{
	OFWindowsRegistryKey *_registryKey;
	OFString *_Nullable _value, *_Nullable _subKeyPath;
	DWORD _flags;
	LSTATUS _status;
}

/*!
 * @brief The registry key on which reading the value at the sub key path
 *	  failed.
 */
@property (readonly, nonatomic) OFWindowsRegistryKey *registryKey;

/*!
 * @brief The value for which reading failed.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *value;

/*!
 * @brief The sub key path at which reading the value failed.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *subKeyPath;

/*!
 * @brief The flags with which reading the value failed.
 */
@property (readonly, nonatomic) DWORD flags;

/*!
 * @brief The status returned by RegGetValueEx().
 */
@property (readonly, nonatomic) LSTATUS status;

/*!
 * @brief Creates a new, autoreleased read Windows registry value failed
 *	  exception.
 *
 * @param registryKey The registry key on which reading the value at the sub
 *		      key path failed
 * @param value The value for which reading failed
 * @param subKeyPath The sub key path at which reading the value failed
 * @param flags The flags with which reading the value failed
 * @param status The status returned by RegGetValueEx()
 * @return A new, autoreleased read Windows registry value failed exception
 */
+ (instancetype)exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey
				   value: (nullable OFString *)value
			      subKeyPath: (nullable OFString *)subKeyPath
				   flags: (DWORD)flags
				  status: (LSTATUS)status;

- (instancetype)init OF_UNAVAILABLE;

/*!
 * @brief Initializes an already allocated read Windows registry value failed
 *	  exception.
 *
 * @param registryKey The registry key on which reading the value at the sub
 *		      key path failed
 * @param value The value for which reading failed
 * @param subKeyPath The sub key path at which reading the value failed
 * @param flags The flags with which reading the value failed
 * @param status The status returned by RegGetValueEx()
 * @return An initialized read Windows registry value failed exception
 */
- (instancetype)initWithRegistryKey: (OFWindowsRegistryKey *)registryKey
			      value: (nullable OFString *)value
			 subKeyPath: (nullable OFString *)subKeyPath
			      flags: (DWORD)flags
			     status: (LSTATUS)status OF_DESIGNATED_INITIALIZER;
@end

OF_ASSUME_NONNULL_END







|





|
<











|


















|






|












|






|





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
 *	  ObjFW/OFReadWindowsRegistryValueFailedException.h
 *
 * @brief An exception indicating that reading a Windows registry value failed.
 */
@interface OFReadWindowsRegistryValueFailedException: OFException
{
	OFWindowsRegistryKey *_registryKey;
	OFString *_Nullable _value, *_Nullable _subkeyPath;
	DWORD _flags;
	LSTATUS _status;
}

/*!
 * @brief The registry key on which reading the value at the key path failed.

 */
@property (readonly, nonatomic) OFWindowsRegistryKey *registryKey;

/*!
 * @brief The value for which reading failed.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *value;

/*!
 * @brief The sub key path at which reading the value failed.
 */
@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *subkeyPath;

/*!
 * @brief The flags with which reading the value failed.
 */
@property (readonly, nonatomic) DWORD flags;

/*!
 * @brief The status returned by RegGetValueEx().
 */
@property (readonly, nonatomic) LSTATUS status;

/*!
 * @brief Creates a new, autoreleased read Windows registry value failed
 *	  exception.
 *
 * @param registryKey The registry key on which reading the value at the sub
 *		      key path failed
 * @param value The value for which reading failed
 * @param subkeyPath The subkey path at which reading the value failed
 * @param flags The flags with which reading the value failed
 * @param status The status returned by RegGetValueEx()
 * @return A new, autoreleased read Windows registry value failed exception
 */
+ (instancetype)exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey
				   value: (nullable OFString *)value
			      subkeyPath: (nullable OFString *)subkeyPath
				   flags: (DWORD)flags
				  status: (LSTATUS)status;

- (instancetype)init OF_UNAVAILABLE;

/*!
 * @brief Initializes an already allocated read Windows registry value failed
 *	  exception.
 *
 * @param registryKey The registry key on which reading the value at the sub
 *		      key path failed
 * @param value The value for which reading failed
 * @param subkeyPath The subkey path at which reading the value failed
 * @param flags The flags with which reading the value failed
 * @param status The status returned by RegGetValueEx()
 * @return An initialized read Windows registry value failed exception
 */
- (instancetype)initWithRegistryKey: (OFWindowsRegistryKey *)registryKey
			      value: (nullable OFString *)value
			 subkeyPath: (nullable OFString *)subkeyPath
			      flags: (DWORD)flags
			     status: (LSTATUS)status OF_DESIGNATED_INITIALIZER;
@end

OF_ASSUME_NONNULL_END

Modified src/exceptions/OFReadWindowsRegistryValueFailedException.m from [6e76beaab3] to [e8e1f7712a].

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

#include "config.h"

#import "OFReadWindowsRegistryValueFailedException.h"

@implementation OFReadWindowsRegistryValueFailedException
@synthesize registryKey = _registryKey, value = _value;
@synthesize subKeyPath = _subKeyPath, flags = _flags, status = _status;

+ (instancetype)exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey
				   value: (nullable OFString *)value
			      subKeyPath: (nullable OFString *)subKeyPath
				   flags: (DWORD)flags
				  status: (LSTATUS)status
{
	return [[[self alloc] initWithRegistryKey: registryKey
					    value: value
				       subKeyPath: subKeyPath
					    flags: flags
					   status: status] autorelease];
}

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithRegistryKey: (OFWindowsRegistryKey *)registryKey
			      value: (nullable OFString *)value
			 subKeyPath: (nullable OFString *)subKeyPath
			      flags: (DWORD)flags
			     status: (LSTATUS)status
{
	self = [super init];

	@try {
		_registryKey = [registryKey retain];
		_value = [value copy];
		_subKeyPath = [subKeyPath copy];
		_flags = flags;
		_status = status;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_registryKey release];
	[_value release];
	[_subKeyPath release];

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to read value %@ at sub key path %@: Status code %u!",
	    _value, _subKeyPath, _status];
}
@end







|



|





|











|








|














|








|


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

#include "config.h"

#import "OFReadWindowsRegistryValueFailedException.h"

@implementation OFReadWindowsRegistryValueFailedException
@synthesize registryKey = _registryKey, value = _value;
@synthesize subkeyPath = _subkeyPath, flags = _flags, status = _status;

+ (instancetype)exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey
				   value: (nullable OFString *)value
			      subkeyPath: (nullable OFString *)subkeyPath
				   flags: (DWORD)flags
				  status: (LSTATUS)status
{
	return [[[self alloc] initWithRegistryKey: registryKey
					    value: value
				       subkeyPath: subkeyPath
					    flags: flags
					   status: status] autorelease];
}

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithRegistryKey: (OFWindowsRegistryKey *)registryKey
			      value: (nullable OFString *)value
			 subkeyPath: (nullable OFString *)subkeyPath
			      flags: (DWORD)flags
			     status: (LSTATUS)status
{
	self = [super init];

	@try {
		_registryKey = [registryKey retain];
		_value = [value copy];
		_subkeyPath = [subkeyPath copy];
		_flags = flags;
		_status = status;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_registryKey release];
	[_value release];
	[_subkeyPath release];

	[super dealloc];
}

- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"Failed to read value %@ at sub key path %@: Status code %u!",
	    _value, _subkeyPath, _status];
}
@end