Skip to content

Commit c195b1e

Browse files
author
Owen L - SFE
committed
Add exactle stack
1 parent 6c3267a commit c195b1e

File tree

293 files changed

+82499
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

293 files changed

+82499
-0
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/*************************************************************************************************/
2+
/*!
3+
* \file utils.c
4+
*
5+
* \brief Utility functions.
6+
*
7+
* $Date: 2016-03-29 14:55:12 -0700 (Tue, 29 Mar 2016) $
8+
* $Revision: 6524 $
9+
*
10+
* Copyright (c) 2015 ARM, Ltd., all rights reserved.
11+
* ARM confidential and proprietary.
12+
*
13+
* IMPORTANT. Your use of this file is governed by a Software License Agreement
14+
* ("Agreement") that must be accepted in order to download or otherwise receive a
15+
* copy of this file. You may not use or copy this file for any purpose other than
16+
* as described in the Agreement. If you do not agree to all of the terms of the
17+
* Agreement do not use this file and delete all copies in your possession or control;
18+
* if you do not have a copy of the Agreement, you must contact ARM, Ltd. prior
19+
* to any use, copying or further distribution of this software.
20+
*/
21+
/*************************************************************************************************/
22+
23+
/***************************************************************************************************
24+
** INCLUDES
25+
***************************************************************************************************/
26+
27+
#include <string.h>
28+
29+
#include "utils.h"
30+
31+
/***************************************************************************************************
32+
** FUNCTION PROTOTYPES
33+
***************************************************************************************************/
34+
35+
static int Util_PrintInt(char *buf, int buf_len, int i, int base, int sign, int width);
36+
37+
/***************************************************************************************************
38+
** FUNCTIONS
39+
***************************************************************************************************/
40+
41+
/*------------------------------------------------------------------------------------------------*/
42+
43+
int32_t Util_ParseMacAddr(const char *s, uint8_t (*addr)[UTIL_MAC_ADDR_LEN])
44+
{
45+
int32_t r = 0;
46+
uint8_t i;
47+
48+
for (i = 0u; i < UTIL_MAC_ADDR_LEN; i++) {
49+
char c;
50+
uint8_t u;
51+
52+
c = *s++;
53+
r++;
54+
if ((c == '\0') || !UTIL_IS_XDIGIT(c)) goto label_error;
55+
u = UTIL_XDIGIT_TO_INT(c) * 16u;
56+
c = *s++;
57+
r++;
58+
if ((c == '\0') || !UTIL_IS_XDIGIT(c)) goto label_error;
59+
u += UTIL_XDIGIT_TO_INT(c);
60+
(*addr)[5u - i] = u;
61+
if (i != 5u) {
62+
c = *s++;
63+
r++;
64+
if (c != ':') goto label_error;
65+
}
66+
}
67+
68+
return r;
69+
70+
label_error:
71+
return 0;
72+
}
73+
74+
/*------------------------------------------------------------------------------------------------*/
75+
76+
int32_t Util_ParseUInt(const char *s, uint32_t *u, uint32_t base)
77+
{
78+
int32_t r = 0;
79+
80+
if (base == 0u) {
81+
if (*s == '0') {
82+
if (((*(s + 1) == 'x') || (*(s + 1) == 'X')) && UTIL_IS_XDIGIT(*(s + 2))) {
83+
r += 2;
84+
s += 2;
85+
base = 16u;
86+
} else {
87+
base = 8u;
88+
}
89+
} else {
90+
base = 10u;
91+
}
92+
} else if (base == 16u) {
93+
if (*s == '0') {
94+
if (((*(s + 1) == 'x') || (*(s + 1) == 'X')) && UTIL_IS_XDIGIT(*(s + 2))) {
95+
r += 2;
96+
s += 2;
97+
}
98+
}
99+
}
100+
/* One character is required; all characters must be consumed. */
101+
*u = 0u;
102+
do {
103+
char c;
104+
uint8_t t;
105+
c = *s++;
106+
if (!UTIL_IS_XDIGIT(c)) {
107+
break;
108+
}
109+
t = UTIL_XDIGIT_TO_INT(c);
110+
if (t >= base) {
111+
break;
112+
}
113+
*u *= base;
114+
*u += t;
115+
r++;
116+
} while (*s != '\0');
117+
118+
return r;
119+
}
120+
121+
/*------------------------------------------------------------------------------------------------*/
122+
123+
int Util_VSNPrintf(char *str, uint32_t size, const char *format, va_list ap)
124+
{
125+
size_t len = 0;
126+
127+
*str = 0;
128+
size--; /* Ensure we null-terminate within our buffer */
129+
130+
while (*format && len < size) {
131+
uint32_t width = 0;
132+
if (*format != '%') { /* Non-special */
133+
*str++ = *format++;
134+
len++;
135+
continue;
136+
}
137+
format++;
138+
if (*format == '%') { /* Escaped % */
139+
*str++ = '%';
140+
len ++;
141+
format++;
142+
continue;
143+
}
144+
if (*format == '0') { /* Width */
145+
format += Util_ParseUInt(format, &width, 10u);
146+
}
147+
if (*format == 'l') {
148+
format++;
149+
// XXX handle LONG?
150+
}
151+
switch(*format) {
152+
case 'c': { /* Character */
153+
char tmp = va_arg(ap, int);
154+
*str++ = tmp;
155+
len++;
156+
break;
157+
}
158+
case 'u': { /* Unsigned integer */
159+
unsigned int temp = va_arg(ap, unsigned int);
160+
uint8_t len_temp = Util_PrintInt(str, size - len, temp, 10, 0, width);
161+
str += len_temp;
162+
len += len_temp;
163+
break;
164+
}
165+
case 'd': { /* Signed integer */
166+
int temp = va_arg(ap, int);
167+
uint8_t len_temp = Util_PrintInt(str, size - len, temp, 10, 1, width);
168+
str += len_temp;
169+
len += len_temp;
170+
break;
171+
}
172+
case 'p': { /* Pointer */
173+
unsigned long temp = va_arg(ap, unsigned long);
174+
uint8_t len_temp = Util_PrintInt(str, size - len, temp, 16, 1, 8u);
175+
str += len_temp;
176+
len += len_temp;
177+
break;
178+
}
179+
case 'x':
180+
case 'X': { /* Unsigned hex number */
181+
unsigned int temp = va_arg(ap, unsigned int);
182+
uint8_t len_temp = Util_PrintInt(str, size - len, temp, 16, 0, width);
183+
str += len_temp;
184+
len += len_temp;
185+
break;
186+
}
187+
case 's': { /* String */
188+
char *temp = va_arg(ap, char *);
189+
while (temp && *temp && len < size) {
190+
*str++ = *temp++;
191+
len++;
192+
}
193+
break;
194+
}
195+
default: {
196+
// char *ptr = va_arg(ap, char *);
197+
// XXX insert error into output?
198+
break;
199+
}
200+
}
201+
format++;
202+
}
203+
204+
/* Null-terminate */
205+
*str = 0;
206+
207+
if (len > size)
208+
return size + 2; /* Compensate for -1 earlier */
209+
210+
return len;
211+
}
212+
213+
/*------------------------------------------------------------------------------------------------*/
214+
215+
int Util_SNPrintf(char *str, uint32_t size, const char *format, ...)
216+
{
217+
va_list ap;
218+
int len;
219+
220+
va_start(ap, format);
221+
len = Util_VSNPrintf(str, size, format, ap);
222+
va_end(ap);
223+
224+
return len;
225+
}
226+
227+
/***************************************************************************************************
228+
** LOCAL FUNCTIONS
229+
***************************************************************************************************/
230+
231+
/*------------------------------------------------------------------------------------------------*/
232+
233+
static int Util_PrintInt(char *buf, int buf_len, int i, int base, int sign, int width)
234+
{
235+
char *s, *p = buf;
236+
unsigned int u = (unsigned int) i;
237+
uint8_t use_width;
238+
int t;
239+
240+
use_width = width;
241+
242+
if (i == 0) {
243+
s = "0\0";
244+
width--;
245+
goto almost;
246+
}
247+
248+
if (sign && base == 10 && i < 0) {
249+
*buf++ = '-';
250+
u = (unsigned int)-i;
251+
width--;
252+
}
253+
s = buf + buf_len - 1;
254+
*s = '\0';
255+
256+
while (u && (!use_width || (width > 0))) {
257+
t = (unsigned int) u % base;
258+
if (t >= 10)
259+
t += 'A' - '0' - 10;
260+
*--s = (char)(t + '0');
261+
u /= base;
262+
width--;
263+
}
264+
265+
almost:
266+
while (width > 0) {
267+
*buf++ = '0';
268+
width--;
269+
}
270+
strcpy(buf, s);
271+
272+
return strlen(p);
273+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*************************************************************************************************/
2+
/*!
3+
* \file utils.h
4+
*
5+
* \brief Utility functions.
6+
*
7+
* $Date: 2016-03-29 14:55:12 -0700 (Tue, 29 Mar 2016) $
8+
* $Revision: 6524 $
9+
*
10+
* Copyright (c) 2015 ARM, Ltd., all rights reserved.
11+
* ARM confidential and proprietary.
12+
*
13+
* IMPORTANT. Your use of this file is governed by a Software License Agreement
14+
* ("Agreement") that must be accepted in order to download or otherwise receive a
15+
* copy of this file. You may not use or copy this file for any purpose other than
16+
* as described in the Agreement. If you do not agree to all of the terms of the
17+
* Agreement do not use this file and delete all copies in your possession or control;
18+
* if you do not have a copy of the Agreement, you must contact ARM, Ltd. prior
19+
* to any use, copying or further distribution of this software.
20+
*/
21+
/*************************************************************************************************/
22+
23+
#ifndef __UTILS_H
24+
#define __UTILS_H
25+
26+
/***************************************************************************************************
27+
** INCLUDES
28+
***************************************************************************************************/
29+
30+
#include <stdarg.h>
31+
32+
#include "wsf_types.h"
33+
34+
#include "am_util.h"
35+
36+
#if defined(__GNUC__) || defined(__CC_ARM)
37+
#define PRINTF_ATTRIBUTE(a, b) __attribute__((format(printf, a, b)))
38+
#else
39+
#define PRINTF_ATTRIBUTE(a, b)
40+
#endif
41+
42+
/***************************************************************************************************
43+
** DEFINES
44+
***************************************************************************************************/
45+
46+
#define UTIL_MAC_ADDR_LEN 6
47+
48+
/*------------------------------------------------------------------------------------------------*/
49+
50+
#define UTIL_IS_DIGIT(c) ((c >= '0') && (c <= '9'))
51+
#define UTIL_IS_XDIGIT(c) (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || \
52+
((c >= 'A') && (c <= 'F')))
53+
54+
/*------------------------------------------------------------------------------------------------*/
55+
56+
#define UTIL_DIGIT_TO_INT(c) (((c >= '0') && (c <= '9')) ? (uint8_t)(c - '0') : 0u)
57+
#define UTIL_XDIGIT_TO_INT(c) (((c >= '0') && (c <= '9')) ? (uint8_t)(c - '0') : \
58+
((c >= 'a') && (c <= 'f')) ? (uint8_t)(c - 'a' + 10u) : \
59+
((c >= 'A') && (c <= 'F')) ? (uint8_t)(c - 'A' + 10u) : 0u)
60+
61+
/***************************************************************************************************
62+
** FUNCTIONS
63+
***************************************************************************************************/
64+
#define Util_VSNPrintf(s, size, format, ap) am_util_stdio_vsprintf(s, format, ap)
65+
#define Util_SNPrintf(s, size, ...) am_util_stdio_vsprintf(s, __VA_ARGS__)
66+
67+
/*--------------------------------------------------------------------------------------------------
68+
** Util_ParseMacAddr()
69+
**
70+
** DESCRIPTION: Parse MAC address from string.
71+
**
72+
** PARAMETERS: s Pointer to string to parse
73+
** addr Pointer to buffer that will receive MAC address
74+
**
75+
** RETURNS: Number of characters consumed from string
76+
**------------------------------------------------------------------------------------------------*/
77+
//int32_t Util_ParseMacAddr(const char *s, uint8_t (*addr)[UTIL_MAC_ADDR_LEN]);
78+
79+
/*--------------------------------------------------------------------------------------------------
80+
** Util_ParseUInt()
81+
**
82+
** DESCRIPTION: Parse unsigned integer from string.
83+
**
84+
** PARAMETERS: s Pointer to string to parse
85+
** u Pointer to variable that will receive integer
86+
** base Base of integer (between 2 and 36, inclusive) or 0, for automatic detection
87+
**
88+
** RETURNS: Number of characters consumed from string
89+
**------------------------------------------------------------------------------------------------*/
90+
//int32_t Util_ParseUInt(const char *s, uint32_t *u, uint32_t base);
91+
92+
#endif /* __UTILS_H */

0 commit comments

Comments
 (0)