]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/net/https/axTLS/ssl/os_port.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / net / https / axTLS / ssl / os_port.c
1 /*
2 * Copyright (c) 2007, Cameron Rich
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of the axTLS project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * @file os_port.c
33 *
34 * OS specific functions.
35 */
36 #include <time.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include "os_port.h"
41 #include <stdio.h>
42 #include "sockets.h"
43
44 static int memory_buf[400];
45 static char enable = 1;
46 static int nb_entries = 0;
47 static int nb_alloc = 0;
48
49 void disable_memory_buf(void)
50 {
51 enable = 0;
52 }
53 void enable_memory_buf(void)
54 {
55 enable = 1;
56 }
57 void init_memory_buf(void)
58 {
59 for(int i = 0; i < 400; i += 2)
60 {
61 memory_buf[i] = -1;
62 memory_buf[i+1] = 0;
63 }
64 }
65 void print_buf_stats(void)
66 {
67 if(enable)
68 {
69 int used = 0;
70 for(int i = 1; i < 400; i += 2)
71 used += memory_buf[i];
72 printf("%d\n", used);
73 }
74 }
75
76 void print_all_buf_stats(void)
77 {
78 int used = 0;
79 for(int i = 1; i < 400; i += 2)
80 used += memory_buf[i];
81 printf("used: %d bytes\n", used);
82
83 for(int i = 0; i < 400; i += 2)
84 if(memory_buf[i] != -1)
85 printf("ptr:%X, size:%d\n", memory_buf[i], memory_buf[i+1]);
86 }
87
88 static void add_entry(void *x, size_t s, const char* f, const int l)
89 {
90 nb_entries++;
91 for(int i = 0; i < 400; i += 2)
92 {
93 if(memory_buf[i] == -1)
94 {
95 if(enable)
96 printf("new ptr:%X, size:%d at %s:%d\n", x, s, f, l);
97 memory_buf[i] = (int)(x);
98 memory_buf[i+1] = s;
99 return;
100 }
101 }
102 if(enable)
103 printf("No space left in buffer\n");
104 }
105
106 static void remove_entry(void *x, const char* f, const int l)
107 {
108 nb_entries--;
109 for(int i = 0; i < 400; i += 2)
110 {
111 if(memory_buf[i] == (int)(x))
112 {
113 if(enable)
114 printf("free ptr:%X, size:%d at %s:%d\n", memory_buf[i], memory_buf[i+1], f, l);
115 memory_buf[i] = -1;
116 memory_buf[i+1] = 0;
117 return;
118 }
119 }
120 if(enable)
121 printf("not found\n");
122 }
123
124 #ifdef MBED
125 /**
126 * gettimeofday() not in mbed
127 */
128 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
129 {
130 t->tv_sec = time(NULL);
131 t->tv_usec = 0; /* 1sec precision only */
132 }
133
134 #endif
135
136 #ifdef WIN32
137 /**
138 * gettimeofday() not in Win32
139 */
140 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
141 {
142 #if defined(_WIN32_WCE)
143 t->tv_sec = time(NULL);
144 t->tv_usec = 0; /* 1sec precision only */
145 #else
146 struct _timeb timebuffer;
147 _ftime(&timebuffer);
148 t->tv_sec = (long)timebuffer.time;
149 t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
150 #endif
151 }
152
153
154 /**
155 * strcasecmp() not in Win32
156 */
157 EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
158 {
159 while (tolower(*s1) == tolower(*s2++))
160 {
161 if (*s1++ == '\0')
162 {
163 return 0;
164 }
165 }
166
167 return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
168 }
169
170
171 EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
172 {
173 HKEY hKey;
174 unsigned long datatype;
175 unsigned long bufferlength = buf_size;
176
177 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
178 TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
179 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
180 return -1;
181
182 RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
183 RegCloseKey(hKey);
184 return 0;
185 }
186 #endif
187
188 #undef malloc
189 #undef realloc
190 #undef calloc
191 #undef free
192
193 static const char * out_of_mem_str = "out of memory";
194 static const char * file_open_str = "Could not open file \"%s\"";
195
196 /*
197 * Some functions that call display some error trace and then call abort().
198 * This just makes life much easier on embedded systems, since we're
199 * suffering major trauma...
200 */
201 EXP_FUNC void * STDCALL ax_malloc(size_t s, const char* f, const int l)
202 {
203 if(enable)
204 printf("malloc\t");
205
206 void *x;
207
208 if ((x = malloc(s)) == NULL)
209 exit_now(out_of_mem_str);
210 add_entry(x,s, f, l);
211 print_buf_stats();
212
213 return x;
214 }
215
216 EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s, const char* f, const int l)
217 {
218 if(enable)
219 printf("realloc\t");
220
221 void *x;
222
223 if ((x = realloc(y, s)) == NULL)
224 exit_now(out_of_mem_str);
225 remove_entry(y, f, l);
226 add_entry(x,s, f, l);
227 print_buf_stats();
228 return x;
229 }
230
231 EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s, const char* f, const int l)
232 {
233 if(enable)
234 printf("calloc\t");
235 void *x;
236
237 if ((x = calloc(n, s)) == NULL) {
238 exit_now(out_of_mem_str);
239 }
240
241 add_entry(x,n*s, f, l);
242 print_buf_stats();
243 return x;
244 }
245
246 EXP_FUNC void STDCALL ax_free(void *y, const char* f, const int l)
247 {
248 if(enable)
249 printf("free\t");
250
251 remove_entry(y, f, l);
252 print_buf_stats();
253 free(y);
254 }
255 /*
256 EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
257 {
258 int x;
259
260 if ((x = open(pathname, flags)) < 0)
261 exit_now(file_open_str, pathname);
262
263 return x;
264 }
265 */
266
267 /**
268 * This is a call which will deliberately exit an application, but will
269 * display some information before dying.
270 */
271 void exit_now(const char *format, ...)
272 {
273 va_list argp;
274
275 va_start(argp, format);
276 vfprintf(stderr, format, argp);
277 va_end(argp);
278 abort();
279 }
280
Imprint / Impressum