ObjFW  Check-in [14f05841a3]

Overview
Comment:Split of_random() into of_random{16,32,64}()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 14f05841a37519bb5604d2f6a8fdf02cc34fdb59c0156fadb8265de5644a59a6
User & Date: js on 2020-07-12 10:35:10
Other Links: manifest | tags
Context
2020-07-12
11:17
Fix missing include for getrandom() check-in: ed7377d854 user: js tags: trunk
10:54
Merge trunk into branch "tagged-pointers" check-in: be250b4fb3 user: js tags: tagged-pointers
10:35
Split of_random() into of_random{16,32,64}() check-in: 14f05841a3 user: js tags: trunk
10:00
Add support for getrandom() check-in: ba1135b0b2 user: js tags: trunk
Changes

Modified src/OFDNSResolver.m from [7061e87e9f] to [6319b8ebc8].

810
811
812
813
814
815
816
817

818
819
820
821
822
823
824
810
811
812
813
814
815
816

817
818
819
820
821
822
823
824







-
+







{
	void *pool = objc_autoreleasePoolPush();
	OFNumber *ID;
	OFDNSResolverContext *context;

	/* Random, unused ID */
	do {
		ID = [OFNumber numberWithUInt16: (uint16_t)of_random()];
		ID = [OFNumber numberWithUInt16: of_random16()];
	} while ([_queries objectForKey: ID] != nil);

	if (query.domainName.UTF8StringLength > 253)
		@throw [OFOutOfRangeException exception];

	if (_settings->_nameServers.count == 0) {
		id exception = [OFDNSQueryFailedException

Modified src/OFMapTable.m from [8ca8ae429a] to [ec26b7463a].

163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
163
164
165
166
167
168
169

170
171
172
173
174
175
176
177







-
+







		if (_capacity < MIN_CAPACITY)
			_capacity = MIN_CAPACITY;

		_buckets = [self allocZeroedMemoryWithSize: sizeof(*_buckets)
						     count: _capacity];

		if (of_hash_seed != 0)
			_rotate = of_random() & 31;
			_rotate = of_random16() & 31;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
530
531
532
533
534
535
536
537

538
539
540
541
542
543
544
530
531
532
533
534
535
536

537
538
539
540
541
542
543
544







-
+







				count: _capacity];

	/*
	 * Get a new random value for _rotate, so that it is not less secure
	 * than creating a new hash map.
	 */
	if (of_hash_seed != 0)
		_rotate = of_random() & 31;
		_rotate = of_random16() & 31;
}

- (bool)containsObject: (void *)object
{
	if (object == NULL || _count == 0)
		return false;

Modified src/OFObject.h from [89cb21ae56] to [f46ea6f043].

1314
1315
1316
1317
1318
1319
1320
1321
1322




1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1314
1315
1316
1317
1318
1319
1320


1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336







-
-
+
+
+
+












extern void *_Null_unspecified objc_autoreleasePoolPush(void);
extern void objc_autoreleasePoolPop(void *_Null_unspecified pool);
#endif
extern id of_alloc_object(Class class_, size_t extraSize,
    size_t extraAlignment, void *_Nullable *_Nullable extra);
extern void OF_NO_RETURN_FUNC of_method_not_found(id self, SEL _cmd);
extern uint32_t of_hash_seed;
/* This does *NOT* provide cryptographically secure randomness! */
extern uint32_t of_random(void);
/* These do *NOT* provide cryptographically secure randomness! */
extern uint16_t of_random16(void);
extern uint32_t of_random32(void);
extern uint64_t of_random64(void);
#ifdef __cplusplus
}
#endif

OF_ASSUME_NONNULL_END

#ifdef __OBJC__
# import "OFObject+KeyValueCoding.h"
# import "OFObject+Serialization.h"
#endif

#endif

Modified src/OFObject.m from [d3c7a17532] to [f5834cd36c].

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








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

-
+










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







	srandom((unsigned)(tv.tv_sec ^ tv.tv_usec));
# else
	gettimeofday(&tv, NULL);
	srand((unsigned)(tv.tv_sec ^ tv.tv_usec));
# endif
}
#endif

uint16_t
of_random16(void)
{
#if defined(HAVE_ARC4RANDOM)
	return arc4random();
#elif defined(HAVE_GETRANDOM)
	uint16_t buffer;

	OF_ENSURE(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else
	static of_once_t onceControl = OF_ONCE_INIT;

	of_once(&onceControl, initRandom);
# ifdef HAVE_RANDOM
	return random() & 0xFFFF;
# else
	return rand() & 0xFFFF;
# endif
#endif
}

uint32_t
of_random(void)
of_random32(void)
{
#if defined(HAVE_ARC4RANDOM)
	return arc4random();
#elif defined(HAVE_GETRANDOM)
	uint32_t buffer;

	OF_ENSURE(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else
	return ((uint32_t)of_random16() << 16) | of_random16();
#endif
}
	static of_once_t onceControl;

	of_once(&onceControl, initRandom);
# ifdef HAVE_RANDOM
	return (((uint32_t)(random()) << 16) | ((uint32_t)(random()) & 0xFFFF));
# else
	return (((uint32_t)(rand()) << 16) | ((uint32_t)(rand()) & 0xFFFF));

uint64_t
of_random64(void)
{
#if defined(HAVE_ARC4RANDOM)
	uint64_t buffer;

	arc4random_buf(&buffer, sizeof(buffer));

	return buffer;
#elif defined(HAVE_GETRANDOM)
	uint64_t buffer;

	OF_ENSURE(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else
	return ((uint64_t)of_random32() << 32) | of_random32();
# endif
#endif
}

static const char *
typeEncodingForSelector(Class class, SEL selector)
{
	Method method;
287
288
289
290
291
292
293
294

295
296
297
298
299
300
301
323
324
325
326
327
328
329

330
331
332
333
334
335
336
337







-
+







#else
	objc_setForwardHandler((IMP)&of_forward, (IMP)&of_forward_stret);
#endif

	objc_setEnumerationMutationHandler(enumerationMutationHandler);

	do {
		of_hash_seed = of_random();
		of_hash_seed = of_random32();
	} while (of_hash_seed == 0);
}

+ (void)unload
{
}