1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLATFORM_QUALIFY_KERNEL_VERSION_H_
8 : #define NATIVE_CLIENT_SRC_TRUSTED_PLATFORM_QUALIFY_KERNEL_VERSION_H_
9 :
10 : #include <stdlib.h>
11 : #include <string.h>
12 : #include "native_client/src/include/nacl_base.h"
13 :
14 : EXTERN_C_BEGIN
15 :
16 : /*
17 : * Returns 1 on success and fills num1.num2.num3 appropriately.
18 : */
19 25 : static INLINE int NaClParseKernelVersion(const char *version, int *num1,
20 25 : int *num2, int *num3) {
21 25 : char *next;
22 25 : *num1 = strtol(version, &next, 10);
23 50 : if (next == NULL || *next != '.') {
24 3 : return 0;
25 : }
26 22 : *num2 = strtol(next + 1, &next, 10);
27 22 : if (next == NULL) {
28 0 : return 0;
29 : }
30 22 : if (*next != '.') {
31 8 : *num3 = 0;
32 8 : return 1;
33 : }
34 14 : *num3 = strtol(next + 1, &next, 10);
35 14 : if (next == NULL) {
36 0 : return 0;
37 : }
38 : /* Ignore the rest of the version string. */
39 14 : return 1;
40 25 : }
41 :
42 : /*
43 : * Returns 1 on success and fills res with -1, 0 or 1,
44 : * if version1 <, == or > version2 accordingly.
45 : */
46 8 : static INLINE int NaClCompareKernelVersions(const char *version1,
47 8 : const char *version2,
48 8 : int *res) {
49 8 : int i;
50 8 : int num1[3];
51 8 : int num2[3];
52 8 : if (!NaClParseKernelVersion(version1, &num1[0], &num1[1], &num1[2])) {
53 1 : return 0;
54 : }
55 7 : if (!NaClParseKernelVersion(version2, &num2[0], &num2[1], &num2[2])) {
56 0 : return 0;
57 : }
58 38 : for (i = 0; i < 3; i++) {
59 17 : if (num1[i] < num2[i]) {
60 3 : *res = -1;
61 3 : return 1;
62 : }
63 14 : if (num1[i] > num2[i]) {
64 2 : *res = 1;
65 2 : return 1;
66 : }
67 12 : }
68 2 : *res = 0;
69 2 : return 1;
70 8 : }
71 :
72 : EXTERN_C_END
73 :
74 : #endif /* NATIVE_CLIENT_SRC_TRUSTED_PLATFORM_QUALIFY_KERNEL_VERSION_H_ */
|