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 : static INLINE int NaClParseKernelVersion(const char *version, int *num1,
20 1 : int *num2, int *num3) {
21 : char *next;
22 1 : *num1 = strtol(version, &next, 10);
23 1 : if (next == NULL || *next != '.') {
24 1 : return 0;
25 : }
26 1 : *num2 = strtol(next + 1, &next, 10);
27 1 : if (next == NULL) {
28 0 : return 0;
29 : }
30 1 : if (*next != '.') {
31 1 : *num3 = 0;
32 1 : return 1;
33 : }
34 1 : *num3 = strtol(next + 1, &next, 10);
35 1 : if (next == NULL) {
36 0 : return 0;
37 : }
38 : /* Ignore the rest of the version string. */
39 1 : return 1;
40 1 : }
41 :
42 : /*
43 : * Returns 1 on success and fills res with -1, 0 or 1,
44 : * if version1 <, == or > version2 accordingly.
45 : */
46 : static INLINE int NaClCompareKernelVersions(const char *version1,
47 : const char *version2,
48 1 : int *res) {
49 : int i;
50 : int num1[3];
51 : int num2[3];
52 1 : if (!NaClParseKernelVersion(version1, &num1[0], &num1[1], &num1[2])) {
53 1 : return 0;
54 : }
55 1 : if (!NaClParseKernelVersion(version2, &num2[0], &num2[1], &num2[2])) {
56 0 : return 0;
57 : }
58 1 : for (i = 0; i < 3; i++) {
59 1 : if (num1[i] < num2[i]) {
60 1 : *res = -1;
61 1 : return 1;
62 : }
63 1 : if (num1[i] > num2[i]) {
64 1 : *res = 1;
65 1 : return 1;
66 : }
67 1 : }
68 1 : *res = 0;
69 1 : return 1;
70 1 : }
71 :
72 : EXTERN_C_END
73 :
74 : #endif /* NATIVE_CLIENT_SRC_TRUSTED_PLATFORM_QUALIFY_KERNEL_VERSION_H_ */
|