1 /******************************************************************************
3 * Copyright (c) 2012 - 2016 Samsung Electronics Co., Ltd and its Licensors.
6 *****************************************************************************/
9 #include "mib_text_convert.h"
11 #define CSR_TOUPPER(character) (((character) >= 'a') && ((character) <= 'z') ? ((character) - 0x20) : (character))
13 static inline bool CsrIsSpace(u8 c
)
27 static inline char *CsrStrDup(const char *string
)
30 u32 len
= strlen(string
) + 1;
32 return memcpy(kmalloc(len
, GFP_KERNEL
), string
, len
);
37 static int CsrStrNICmp(const char *string1
,
44 for (index
= 0; index
< count
; index
++) {
45 if (CSR_TOUPPER(string1
[index
]) != CSR_TOUPPER(string2
[index
])) {
46 if (CSR_TOUPPER(string1
[index
]) > CSR_TOUPPER(string2
[index
]))
52 if (string1
[index
] == '\0')
58 static bool CsrHexStrToUint8(const char *string
, u8
*returnValue
)
63 if ((string
[currentIndex
] == '0') && (CSR_TOUPPER(string
[currentIndex
+ 1]) == 'X'))
65 if (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F'))) {
66 while (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F'))) {
67 *returnValue
= (u8
)(*returnValue
* 16 + (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) ? string
[currentIndex
] - '0' : CSR_TOUPPER(string
[currentIndex
]) - 'A' + 10));
69 if (currentIndex
>= 2)
77 static bool CsrHexStrToUint16(const char *string
, u16
*returnValue
)
82 if ((string
[currentIndex
] == '0') && (CSR_TOUPPER(string
[currentIndex
+ 1]) == 'X'))
84 if (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F'))) {
85 while (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F'))) {
86 *returnValue
= (u16
)(*returnValue
* 16 + (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) ? string
[currentIndex
] - '0' : CSR_TOUPPER(string
[currentIndex
]) - 'A' + 10));
88 if (currentIndex
>= 4)
96 static bool CsrHexStrToUint32(const char *string
, u32
*returnValue
)
101 if ((string
[currentIndex
] == '0') && (CSR_TOUPPER(string
[currentIndex
+ 1]) == 'X'))
103 if (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F'))) {
104 while (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) || ((CSR_TOUPPER(string
[currentIndex
]) >= 'A') && (CSR_TOUPPER(string
[currentIndex
]) <= 'F'))) {
105 *returnValue
= *returnValue
* 16 + (((string
[currentIndex
] >= '0') && (string
[currentIndex
] <= '9')) ? string
[currentIndex
] - '0' : CSR_TOUPPER(string
[currentIndex
]) - 'A' + 10);
107 if (currentIndex
>= 8)
115 static bool CsrWifiMibConvertStrToUint16(const char *str
, u16
*returnValue
)
117 u16 currentIndex
= 0;
120 return CsrHexStrToUint16(str
, returnValue
);
123 if ((str
[currentIndex
] >= '0') && (str
[currentIndex
] <= '9')) {
124 while (str
[currentIndex
] >= '0' && str
[currentIndex
] <= '9') {
126 *returnValue
+= (u8
)str
[currentIndex
++] - '0';
133 static bool CsrWifiMibConvertStrToUint32(const char *str
, u32
*returnValue
)
135 u16 currentIndex
= 0;
138 return CsrHexStrToUint32(str
, returnValue
);
141 if ((str
[currentIndex
] >= '0') && (str
[currentIndex
] <= '9')) {
142 while (str
[currentIndex
] >= '0' && str
[currentIndex
] <= '9') {
144 *returnValue
+= (u8
)str
[currentIndex
++] - '0';
151 static bool CsrWifiMibConvertTextParseLine(const char *linestr
, struct slsi_mib_data
*mibDataSet
, struct slsi_mib_data
*mibDataGet
)
153 struct slsi_mib_entry entry
;
158 size_t trimmedIndex
= 0;
159 char *trimmed
= kmalloc(strlen(linestr
) + 1, GFP_KERNEL
);
160 const char *current_char
= linestr
;
161 bool processingStr
= false;
163 memset(&entry
, 0x00, sizeof(entry
));
164 while (current_char
[0] != '\0') {
165 if (current_char
[0] == '"')
166 processingStr
= !processingStr
;
167 if (!processingStr
) {
168 if (current_char
[0] == '#')
170 if (CsrIsSpace((u8
)current_char
[0])) {
174 if (!equals
&& (current_char
[0] == '.')) {
180 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') only 2 indexes supported", trimmed
);
184 if (!equals
&& (current_char
[0] == '='))
185 equals
= trimmedIndex
;
187 trimmed
[trimmedIndex
++] = current_char
[0];
191 trimmed
[trimmedIndex
] = '\0';
193 if (strlen(trimmed
) == 0) {
199 char sep
= trimmed
[dot1
? dot1
: equals
];
202 trimmed
[dot1
? dot1
: equals
] = '\0';
203 trimmed
[dot1
? dot1
: equals
] = sep
;
205 if (!CsrWifiMibConvertStrToUint16(trimmed
, &entry
.psid
)) {
206 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') Convert <psid> failed", trimmed
);
209 if (dot1
&& !CsrWifiMibConvertStrToUint16(&trimmed
[dot1
+ 1], &entry
.index
[0])) {
210 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') Convert <index 1> failed", trimmed
);
213 if (dot2
&& !CsrWifiMibConvertStrToUint16(&trimmed
[dot2
+ 1], &entry
.index
[1])) {
214 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') Convert <index 2> failed", trimmed
);
218 if (result
&& !equals
&& mibDataGet
) {
219 entry
.value
.type
= SLSI_MIB_TYPE_NONE
;
220 (void)slsi_mib_encode(mibDataGet
, &entry
);
223 if (result
&& equals
&& mibDataSet
) {
224 char *data
= &trimmed
[equals
+ 1];
225 /*SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') psid:%d, index1:%d, index2:%d, data '%s'", trimmed, entry.psid, entry.index[0], entry.index[1], data); */
226 if (CsrStrNICmp(data
, "true", 4) == 0) {
227 entry
.value
.type
= SLSI_MIB_TYPE_BOOL
;
228 entry
.value
.u
.boolValue
= 1;
229 } else if (CsrStrNICmp(data
, "false", 5) == 0) {
230 entry
.value
.type
= SLSI_MIB_TYPE_BOOL
;
231 entry
.value
.u
.boolValue
= 0;
232 } else if (data
[0] == '"') {
233 /* Null Terminated String */
234 entry
.value
.type
= SLSI_MIB_TYPE_OCTET
;
235 entry
.value
.u
.octetValue
.dataLength
= (u32
)strlen(&data
[1]);
236 entry
.value
.u
.octetValue
.data
= (u8
*)CsrStrDup(&data
[1]);
237 entry
.value
.u
.octetValue
.data
[entry
.value
.u
.octetValue
.dataLength
- 1] = '\0';
238 } else if (data
[0] == '[') {
241 u16 octetLen
= ((u16
)strlen(&data
[1]) - 1) / 2;
243 entry
.value
.type
= SLSI_MIB_TYPE_OCTET
;
244 entry
.value
.u
.octetValue
.dataLength
= octetLen
;
245 entry
.value
.u
.octetValue
.data
= kmalloc(entry
.value
.u
.octetValue
.dataLength
+ 1, GFP_KERNEL
);
246 for (i
= 0; i
< octetLen
; i
++)
247 if (!CsrHexStrToUint8(&data
[1 + (i
* 2)], &entry
.value
.u
.octetValue
.data
[i
])) {
248 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') Convert Hex Bytes <data> failed", trimmed
);
252 entry
.value
.u
.octetValue
.data
[octetLen
] = '\0'; /* Make sure the Octet Stream is NULL terminated in case it is interpreted as a String */
253 } else if (data
[0] == '-') {
254 /* Negative Integer Value */
255 entry
.value
.type
= SLSI_MIB_TYPE_INT
;
256 if (!CsrWifiMibConvertStrToUint32(&data
[1], &entry
.value
.u
.uintValue
)) {
257 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') Convert Integer <data> failed", trimmed
);
260 entry
.value
.u
.intValue
= (s32
)(0 - (u32
)entry
.value
.u
.uintValue
);
262 } else if (!CsrWifiMibConvertStrToUint32(data
, &entry
.value
.u
.uintValue
)) {
263 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine('%s') Convert Unsigned Integer <data> failed", trimmed
);
266 entry
.value
.type
= SLSI_MIB_TYPE_UINT
;
269 (void)slsi_mib_encode(mibDataSet
, &entry
);
270 if (entry
.value
.type
== SLSI_MIB_TYPE_OCTET
)
271 kfree(entry
.value
.u
.octetValue
.data
);
280 static bool CsrWifiMibConvertTextAppend(const char *mibText
, struct slsi_mib_data
*mibDataSet
, struct slsi_mib_data
*mibDataGet
)
283 const char *lineStart
= mibText
;
284 const char *lineEnd
= mibText
;
289 while (lineEnd
[0] != '\0') {
290 if ((lineEnd
[0] == '\n') || (lineEnd
[0] == '\r') || (lineEnd
[1] == '\0')) {
291 size_t strSize
= (size_t)(lineEnd
- lineStart
);
293 if ((lineEnd
[1] == '\0'))
296 char *line
= kmalloc(strSize
+ 1, GFP_KERNEL
);
297 (void)strncpy(line
, lineStart
, strSize
);
298 line
[strSize
] = '\0';
299 if (!CsrWifiMibConvertTextParseLine(line
, mibDataSet
, mibDataGet
)) {
300 SLSI_ERR_NODEV("CsrWifiMibConvertTextParseLine() Failed for line '%s'", line
);
315 bool CsrWifiMibConvertText(const char *mibText
, struct slsi_mib_data
*mibDataSet
, struct slsi_mib_data
*mibDataGet
)
318 mibDataSet
->data
= NULL
;
319 mibDataSet
->dataLength
= 0;
322 mibDataGet
->data
= NULL
;
323 mibDataGet
->dataLength
= 0;
326 return CsrWifiMibConvertTextAppend(mibText
, mibDataSet
, mibDataGet
);