Bitdefender Hypervisor Memory Introspection
conv.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "introtypes.h"
6 
7 //
8 // ANSI character macros
9 //
10 #define crt_tolower(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
11 #define crt_toupper(c) ((((c) >= 'a') && ((c) <= 'z')) ? ((c) - 'a' + 'A') : (c))
12 
13 int
14 tolower(int c)
15 {
16  return crt_tolower(c);
17 }
18 
19 int
20 toupper(int c)
21 {
22  return crt_toupper(c);
23 }
24 
25 #define isalpha(c) (((((c) >= 'A') && ((c) <= 'Z')) || (((c) >= 'a') && ((c) <= 'z'))) ? 1 : 0)
26 
27 #define isdigit(c) ((((c) >= '0') && ((c) <= '9')) ? 1 : 0)
28 
29 #define isxdigit(c) (((((c) >= 'A') && ((c) <= 'F')) || (((c) >= 'a') && \
30  ((c) <= 'f')) || (((c) >= '0') && ((c) <= '9'))) ? 1 : 0)
31 
32 #define isprint(c) (((c) >= ' ' && (c) <= '~') ? 1 : 0)
33 #define isspace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\v') || ((c) == '\f') || ((c) == '\r'))
34 
35 //
36 // helper routines
37 //
38 
39 static UINT64
41  const char *Ptr,
42  const char **EndPtr
43  )
44 {
45  UINT64 result = 0;
46  bool neg = false, hex = false;
47  size_t i = 0;
48 
49  while (Ptr[i] == ' ')
50  {
51  i++;
52  }
53 
54  if (Ptr[i] == '-')
55  {
56  neg = true, i++;
57  }
58  else if (Ptr[i] == '+')
59  {
60  neg = false, i++;
61  }
62 
63  if (Ptr[i] == '0' && (Ptr[i + 1] == 'x' || Ptr[i + 1] == 'X'))
64  {
65  hex = true, i += 2;
66  }
67 
68  for (; Ptr[i]; i++)
69  {
70  if (Ptr[i] >= '0' && Ptr[i] <= '9')
71  {
72  result = result * (hex ? 16 : 10) + Ptr[i] - '0';
73  }
74  else if (hex && Ptr[i] >= 'A' && Ptr[i] <= 'F')
75  {
76  result = result * 16 + Ptr[i] - 'A' + 10;
77  }
78  else if (hex && Ptr[i] >= 'a' && Ptr[i] <= 'f')
79  {
80  result = result * 16 + Ptr[i] - 'a' + 10;
81  }
82  else
83  {
84  break;
85  }
86  }
87 
88  if (EndPtr)
89  {
90  *EndPtr = Ptr + i;
91  }
92 
93  if (neg && result != 0)
94  {
95  result = ~result + 1;
96  }
97 
98  return result;
99 }
100 
101 
102 //
103 // crt_strtol
104 //
105 INT32 __cdecl
107  _In_z_ const INT8 *nptr,
108  _Out_opt_ INT8 **endptr,
109  _In_ INT32 ibase
110  )
111 {
112  ibase;
113 
114  return (INT32)quick_convert(nptr, (const INT8 **)endptr);
115 }
116 
117 
118 //
119 // crt_strtoul
120 //
121 UINT32 __cdecl
123  _In_z_ const INT8 *nptr,
124  _Out_opt_ INT8 **endptr,
125  _In_ INT32 ibase
126  )
127 {
128  ibase;
129 
130  return (UINT32)quick_convert(nptr, (const INT8 **)endptr);
131 }
132 
133 
134 //
135 // crt_strtoll
136 //
137 INT64 __cdecl
139  _In_z_ const INT8 *nptr,
140  _Out_opt_ INT8 **endptr,
141  _In_ INT32 ibase
142  )
143 {
144  ibase;
145 
146  return (INT64)quick_convert(nptr, (const INT8 **)endptr);
147 }
148 
149 
150 //
151 // crt_strtoull
152 //
153 UINT64 __cdecl
155  _In_z_ const INT8 *nptr,
156  _Out_opt_ INT8 **endptr,
157  _In_ INT32 ibase
158  )
159 {
160  ibase;
161 
162  return quick_convert(nptr, (const INT8 **)endptr);
163 }
#define crt_toupper(c)
Definition: conv.c:11
long long INT64
Definition: intro_types.h:45
int toupper(int c)
Definition: conv.c:20
#define _In_
Definition: intro_sal.h:21
INT32 __cdecl strtol(const INT8 *nptr, INT8 **endptr, INT32 ibase)
Definition: conv.c:106
static UINT64 quick_convert(const char *Ptr, const char **EndPtr)
Definition: conv.c:40
uint32_t UINT32
Definition: intro_types.h:39
int32_t INT32
Definition: intro_types.h:44
#define _Out_opt_
Definition: intro_sal.h:30
INT64 __cdecl strtoll(const INT8 *nptr, INT8 **endptr, INT32 ibase)
Definition: conv.c:138
unsigned long long UINT64
Definition: intro_types.h:40
UINT64 __cdecl strtoull(const INT8 *nptr, INT8 **endptr, INT32 ibase)
Definition: conv.c:154
UINT32 __cdecl strtoul(const INT8 *nptr, INT8 **endptr, INT32 ibase)
Definition: conv.c:122
#define _In_z_
Definition: intro_sal.h:17
#define crt_tolower(c)
Definition: conv.c:10
int8_t INT8
Definition: intro_types.h:42
int tolower(int c)
Definition: conv.c:14