forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSKextVersion.c
495 lines (431 loc) · 15.1 KB
/
OSKextVersion.c
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright (c) 2000-2016 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
#ifdef KERNEL
#include <sys/systm.h>
#include <libkern/OSKextLib.h>
#include <libkern/OSKextLibPrivate.h>
#else
#include <libc.h>
#include <libkern/OSKextLib.h>
#include <System/libkern/OSKextLibPrivate.h>
#endif /* KERNEL */
#include <libkern/OSKextLibPrivate.h>
#define VERS_MAJOR_DIGITS (4)
#define VERS_MINOR_DIGITS (2)
#define VERS_REVISION_DIGITS (2)
#define VERS_STAGE_DIGITS (1)
#define VERS_STAGE_LEVEL_DIGITS (3)
#define VERS_MAJOR_MAX (9999)
#define VERS_STAGE_LEVEL_MAX (255)
#define VERS_MAJOR_MULT (100000000)
#define VERS_MINOR_MULT (1000000)
#define VERS_REVISION_MULT (10000)
#define VERS_STAGE_MULT (1000)
typedef enum {
kOSKextVersionStageInvalid = 0,
kOSKextVersionStageDevelopment = 1,
kOSKextVersionStageAlpha = 3,
kOSKextVersionStageBeta = 5,
kOSKextVersionStageCandidate = 7,
kOSKextVersionStageRelease = 9,
} OSKextVersionStage;
/*********************************************************************
*********************************************************************/
static int __vers_isdigit(char c) {
return (c == '0' ||
c == '1' || c == '2' || c == '3' ||
c == '4' || c == '5' || c == '6' ||
c == '7' || c == '8' || c == '9');
}
/*********************************************************************
*********************************************************************/
static int __vers_isspace(char c) {
return (c == ' ' ||
c == '\t' ||
c == '\r' ||
c == '\n');
}
/*********************************************************************
*********************************************************************/
static int
__vers_digit_for_char(char c) {
switch (c) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default: return -1;
}
}
/*********************************************************************
*********************************************************************/
static int __VERS_isreleasestate(char c) {
return (c == 'd' || c == 'a' || c == 'b' || c == 'f');
}
/*********************************************************************
*********************************************************************/
static OSKextVersionStage __OSKextVersionStageForString(const char ** string_p) {
const char * string;
if (!string_p || !*string_p) {
return kOSKextVersionStageInvalid;
}
string = *string_p;
if (__vers_isspace(string[0]) || string[0] == '\0') {
return kOSKextVersionStageRelease;
} else {
switch (string[0]) {
case 'd':
if (__vers_isdigit(string[1])) {
*string_p = &string[1];
return kOSKextVersionStageDevelopment;
}
break;
case 'a':
if (__vers_isdigit(string[1])) {
*string_p = &string[1];
return kOSKextVersionStageAlpha;
}
break;
case 'b':
if (__vers_isdigit(string[1])) {
*string_p = &string[1];
return kOSKextVersionStageBeta;
}
break;
case 'f':
if (__vers_isdigit(string[1])) {
*string_p = &string[1];
return kOSKextVersionStageCandidate;
} else if (string[1] == 'c' && __vers_isdigit(string[2])) {
*string_p = &string[2];
return kOSKextVersionStageCandidate;
} else {
return kOSKextVersionStageInvalid;
}
default:
return kOSKextVersionStageInvalid;
}
}
return kOSKextVersionStageInvalid;
}
/*********************************************************************
*********************************************************************/
static const char *
__OSKextVersionStringForStage(OSKextVersionStage stage)
{
switch (stage) {
case kOSKextVersionStageInvalid: return NULL;
case kOSKextVersionStageDevelopment: return "d";
case kOSKextVersionStageAlpha: return "a";
case kOSKextVersionStageBeta: return "b";
case kOSKextVersionStageCandidate: return "f";
case kOSKextVersionStageRelease: return "";
}
}
/*********************************************************************
*********************************************************************/
OSKextVersion OSKextParseVersionString(const char * versionString)
{
OSKextVersion result = -1;
int vers_digit = -1;
int num_digits_scanned = 0;
OSKextVersion vers_major = 0;
OSKextVersion vers_minor = 0;
OSKextVersion vers_revision = 0;
OSKextVersion vers_stage = 0;
OSKextVersion vers_stage_level = 0;
const char * current_char_p;
if (!versionString || *versionString == '\0') {
return -1;
}
current_char_p = (const char *)&versionString[0];
/*****
* Check for an initial digit of the major release number.
*/
vers_major = __vers_digit_for_char(*current_char_p);
if (vers_major < 0) {
return -1;
}
current_char_p++;
num_digits_scanned = 1;
/* Complete scan for major version number. Legal characters are
* any digit, period, any buildstage letter.
*/
while (num_digits_scanned < VERS_MAJOR_DIGITS) {
if (__vers_isspace(*current_char_p) || *current_char_p == '\0') {
vers_stage = kOSKextVersionStageRelease;
goto finish;
} else if (__vers_isdigit(*current_char_p)) {
vers_digit = __vers_digit_for_char(*current_char_p);
if (vers_digit < 0) {
return -1;
}
vers_major = (vers_major) * 10 + vers_digit;
current_char_p++;
num_digits_scanned++;
} else if (__VERS_isreleasestate(*current_char_p)) {
goto release_state;
} else if (*current_char_p == '.') {
current_char_p++;
goto minor_version;
} else {
return -1;
}
}
/* Check for too many digits.
*/
if (num_digits_scanned == VERS_MAJOR_DIGITS) {
if (*current_char_p == '.') {
current_char_p++;
} else if (__vers_isdigit(*current_char_p)) {
return -1;
}
}
minor_version:
num_digits_scanned = 0;
/* Scan for minor version number. Legal characters are
* any digit, period, any buildstage letter.
*/
while (num_digits_scanned < VERS_MINOR_DIGITS) {
if (__vers_isspace(*current_char_p) || *current_char_p == '\0') {
vers_stage = kOSKextVersionStageRelease;
goto finish;
} else if (__vers_isdigit(*current_char_p)) {
vers_digit = __vers_digit_for_char(*current_char_p);
if (vers_digit < 0) {
return -1;
}
vers_minor = (vers_minor) * 10 + vers_digit;
current_char_p++;
num_digits_scanned++;
} else if (__VERS_isreleasestate(*current_char_p)) {
goto release_state;
} else if (*current_char_p == '.') {
current_char_p++;
goto revision;
} else {
return -1;
}
}
/* Check for too many digits.
*/
if (num_digits_scanned == VERS_MINOR_DIGITS) {
if (*current_char_p == '.') {
current_char_p++;
} else if (__vers_isdigit(*current_char_p)) {
return -1;
}
}
revision:
num_digits_scanned = 0;
/* Scan for revision version number. Legal characters are
* any digit, any buildstage letter (NOT PERIOD).
*/
while (num_digits_scanned < VERS_REVISION_DIGITS) {
if (__vers_isspace(*current_char_p) || *current_char_p == '\0') {
vers_stage = kOSKextVersionStageRelease;
goto finish;
} else if (__vers_isdigit(*current_char_p)) {
vers_digit = __vers_digit_for_char(*current_char_p);
if (vers_digit < 0) {
return -1;
}
vers_revision = (vers_revision) * 10 + vers_digit;
current_char_p++;
num_digits_scanned++;
} else if (__VERS_isreleasestate(*current_char_p)) {
goto release_state;
} else {
return -1;
}
}
/* Check for too many digits.
*/
if (num_digits_scanned == VERS_REVISION_DIGITS) {
if (*current_char_p == '.') {
current_char_p++;
} else if (__vers_isdigit(*current_char_p)) {
return -1;
}
}
release_state:
/*****
* Check for the release state.
*/
if (__vers_isspace(*current_char_p) || *current_char_p == '\0') {
vers_stage = kOSKextVersionStageRelease;
goto finish;
} else {
vers_stage = __OSKextVersionStageForString(¤t_char_p);
if (vers_stage == kOSKextVersionStageInvalid) {
return -1;
}
}
// stage level
num_digits_scanned = 0;
/* Scan for stage level number. Legal characters are
* any digit only.
*/
while (num_digits_scanned < VERS_STAGE_LEVEL_DIGITS) {
if (__vers_isspace(*current_char_p) || *current_char_p == '\0') {
if (num_digits_scanned) {
goto finish;
} else {
return -1;
}
} else if (__vers_isdigit(*current_char_p)) {
vers_digit = __vers_digit_for_char(*current_char_p);
if (vers_digit < 0) {
return -1;
}
vers_stage_level = (vers_stage_level) * 10 + vers_digit;
current_char_p++;
num_digits_scanned++;
} else {
return -1;
}
}
/* Check for too many digits.
*/
if ((num_digits_scanned == VERS_STAGE_LEVEL_DIGITS) &&
! (__vers_isspace(*current_char_p) || (*current_char_p == '\0'))) {
return -1;
}
if (vers_stage_level > VERS_STAGE_LEVEL_MAX) {
return -1;
}
finish:
if (vers_stage == kOSKextVersionStageCandidate && vers_stage_level == 0) {
return -1;
}
result = (vers_major * VERS_MAJOR_MULT) +
(vers_minor * VERS_MINOR_MULT) +
(vers_revision * VERS_REVISION_MULT) +
(vers_stage * VERS_STAGE_MULT) +
vers_stage_level;
return result;
}
/*********************************************************************
* This function must be safe to call in panic context.
*********************************************************************/
Boolean OSKextVersionGetString(
OSKextVersion aVersion,
char * buffer,
uint32_t bufferLength)
{
int cpos = 0;
OSKextVersion vers_major = 0;
OSKextVersion vers_minor = 0;
OSKextVersion vers_revision = 0;
OSKextVersion vers_stage = 0;
OSKextVersion vers_stage_level = 0;
const char * stage_string = NULL; // don't free
/* No buffer or length less than longest possible vers string,
* return 0.
*/
if (!buffer || bufferLength < kOSKextVersionMaxLength) {
return FALSE;
}
bzero(buffer, bufferLength * sizeof(char));
if (aVersion < 0) {
strlcpy(buffer, "(invalid)", bufferLength);
return TRUE;
}
if (aVersion == 0) {
strlcpy(buffer, "(missing)", bufferLength);
return TRUE;
}
vers_major = aVersion / VERS_MAJOR_MULT;
if (vers_major > VERS_MAJOR_MAX) {
strlcpy(buffer, "(invalid)", bufferLength);
return TRUE;
}
vers_minor = aVersion - (vers_major * VERS_MAJOR_MULT);
vers_minor /= VERS_MINOR_MULT;
vers_revision = aVersion -
( (vers_major * VERS_MAJOR_MULT) + (vers_minor * VERS_MINOR_MULT) );
vers_revision /= VERS_REVISION_MULT;
vers_stage = aVersion -
( (vers_major * VERS_MAJOR_MULT) + (vers_minor * VERS_MINOR_MULT) +
(vers_revision * VERS_REVISION_MULT));
vers_stage /= VERS_STAGE_MULT;
vers_stage_level = aVersion -
( (vers_major * VERS_MAJOR_MULT) + (vers_minor * VERS_MINOR_MULT) +
(vers_revision * VERS_REVISION_MULT) + (vers_stage * VERS_STAGE_MULT));
if (vers_stage_level > VERS_STAGE_LEVEL_MAX) {
strlcpy(buffer, "(invalid)", bufferLength);
return TRUE;
}
cpos = snprintf(buffer, bufferLength, "%u", (uint32_t)vers_major);
/* Always include the minor version; it just looks weird without.
*/
buffer[cpos] = '.';
cpos++;
cpos += snprintf(buffer+cpos, bufferLength - cpos, "%u", (uint32_t)vers_minor);
/* The revision is displayed only if nonzero.
*/
if (vers_revision) {
buffer[cpos] = '.';
cpos++;
cpos += snprintf(buffer+cpos, bufferLength - cpos, "%u",
(uint32_t)vers_revision);
}
stage_string = __OSKextVersionStringForStage(vers_stage);
if (!stage_string) {
strlcpy(buffer, "(invalid)", bufferLength);
return TRUE;
}
if (stage_string[0]) {
strlcat(buffer, stage_string, bufferLength);
cpos += strlen(stage_string);
}
if (vers_stage < kOSKextVersionStageRelease) {
snprintf(buffer+cpos, bufferLength - cpos, "%u", (uint32_t)vers_stage_level);
}
return TRUE;
}
/*********************************************************************
*********************************************************************/
#ifndef KERNEL
OSKextVersion OSKextParseVersionCFString(CFStringRef versionString)
{
OSKextVersion result = -1;
char versBuffer[kOSKextVersionMaxLength];
if (CFStringGetCString(versionString, versBuffer,
sizeof(versBuffer), kCFStringEncodingASCII)) {
result = OSKextParseVersionString(versBuffer);
}
return result;
}
#endif