|
1 /* |
|
2 * Copyright (c) 2001-2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: EAP and WLAN authentication protocols. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // This is enumeration of EAPOL source code. |
|
20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES) |
|
21 #undef EAP_FILE_NUMBER_ENUM |
|
22 #define EAP_FILE_NUMBER_ENUM 42 |
|
23 #undef EAP_FILE_NUMBER_DATE |
|
24 #define EAP_FILE_NUMBER_DATE 1127594498 |
|
25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES) |
|
26 |
|
27 |
|
28 |
|
29 #include "eap_am_memory.h" |
|
30 #include "eap_variable_data.h" |
|
31 #include "abs_eap_am_tools.h" |
|
32 #include "eap_am_tools.h" |
|
33 |
|
34 //-------------------------------------------------- |
|
35 |
|
36 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::initialize_members() |
|
37 { |
|
38 m_data = new eap_variable_data_impl_str; |
|
39 if (m_data == 0) |
|
40 { |
|
41 EAP_TRACE_ERROR( |
|
42 m_am_tools, |
|
43 TRACE_FLAGS_DEFAULT, |
|
44 (EAPL("eap_variable_data_c::initialize_members(): Cannot allocate %d bytes.\n"), |
|
45 sizeof(eap_variable_data_impl_str))); |
|
46 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
47 } |
|
48 |
|
49 m_data->m_buffer = 0; |
|
50 m_data->m_buffer_length = 0ul; |
|
51 m_data->m_real_data_start_index = 0ul; |
|
52 m_data->m_real_data_length = 0ul; |
|
53 m_data->m_free_buffer = false; |
|
54 m_data->m_is_writable = false; |
|
55 |
|
56 m_data->m_is_valid = true; |
|
57 |
|
58 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
59 } |
|
60 |
|
61 //-------------------------------------------------- |
|
62 |
|
63 EAP_FUNC_EXPORT eap_variable_data_c::~eap_variable_data_c() |
|
64 { |
|
65 if (m_data != 0) |
|
66 { |
|
67 if (m_data->m_free_buffer == true) |
|
68 { |
|
69 // Buffer was allocated. |
|
70 |
|
71 #if !defined(NO_EAP_VARIABLE_DATA_MEMORY_ZERO) |
|
72 if (m_am_tools != 0) |
|
73 { |
|
74 m_am_tools->memset(m_data->m_buffer, 0, m_data->m_buffer_length); |
|
75 } |
|
76 #endif //#if !defined(NO_EAP_VARIABLE_DATA_MEMORY_ZERO) |
|
77 |
|
78 delete [] m_data->m_buffer; |
|
79 } |
|
80 m_data->m_buffer = 0; |
|
81 m_data->m_buffer_length = 0ul; |
|
82 m_data->m_real_data_start_index = 0ul; |
|
83 m_data->m_real_data_length = 0ul; |
|
84 m_data->m_free_buffer = false; |
|
85 |
|
86 m_data->m_is_valid = false; |
|
87 |
|
88 delete m_data; |
|
89 m_data = 0; |
|
90 } |
|
91 } |
|
92 |
|
93 //-------------------------------------------------- |
|
94 |
|
95 EAP_FUNC_EXPORT eap_variable_data_c::eap_variable_data_c( |
|
96 abs_eap_am_tools_c * const tools) |
|
97 : m_am_tools(tools) |
|
98 , m_data(0) |
|
99 { |
|
100 eap_status_e status = initialize_members(); |
|
101 if (status != eap_status_ok) |
|
102 { |
|
103 // ERROR. |
|
104 return; |
|
105 } |
|
106 } |
|
107 |
|
108 //-------------------------------------------------- |
|
109 |
|
110 EAP_FUNC_EXPORT eap_variable_data_c::eap_variable_data_c( |
|
111 abs_eap_am_tools_c * const tools, |
|
112 const void * const buffer, |
|
113 const u32_t buffer_length, |
|
114 bool free_buffer, |
|
115 bool is_writable) |
|
116 : m_am_tools(tools) |
|
117 , m_data(0) |
|
118 { |
|
119 eap_status_e status = initialize_members(); |
|
120 if (status != eap_status_ok) |
|
121 { |
|
122 // ERROR. |
|
123 return; |
|
124 } |
|
125 |
|
126 status = set_buffer( |
|
127 buffer, |
|
128 buffer_length, |
|
129 free_buffer, |
|
130 is_writable); |
|
131 if (status != eap_status_ok) |
|
132 { |
|
133 // ERROR. |
|
134 set_is_invalid(); |
|
135 return; |
|
136 } |
|
137 } |
|
138 |
|
139 //-------------------------------------------------- |
|
140 |
|
141 EAP_FUNC_EXPORT bool eap_variable_data_c::get_is_valid() const |
|
142 { |
|
143 if (m_data == 0) |
|
144 { |
|
145 return false; |
|
146 } |
|
147 |
|
148 return m_data->m_is_valid; |
|
149 } |
|
150 |
|
151 //-------------------------------------------------- |
|
152 |
|
153 EAP_FUNC_EXPORT bool eap_variable_data_c::get_is_valid_data() const |
|
154 { |
|
155 if (m_data == 0) |
|
156 { |
|
157 return false; |
|
158 } |
|
159 |
|
160 if (m_data->m_is_valid == false) |
|
161 { |
|
162 return false; |
|
163 } |
|
164 |
|
165 // Note the data length could be zero. |
|
166 if (m_data->m_buffer == 0) |
|
167 { |
|
168 return false; |
|
169 } |
|
170 |
|
171 return true; |
|
172 } |
|
173 |
|
174 //-------------------------------------------------- |
|
175 |
|
176 EAP_FUNC_EXPORT void eap_variable_data_c::set_is_valid() |
|
177 { |
|
178 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
179 |
|
180 if (m_data == 0) |
|
181 { |
|
182 return; |
|
183 } |
|
184 |
|
185 m_data->m_is_valid = true; |
|
186 } |
|
187 |
|
188 //-------------------------------------------------- |
|
189 |
|
190 EAP_FUNC_EXPORT void eap_variable_data_c::set_is_invalid() |
|
191 { |
|
192 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
193 |
|
194 if (m_data == 0) |
|
195 { |
|
196 return; |
|
197 } |
|
198 |
|
199 m_data->m_is_valid = false; |
|
200 } |
|
201 |
|
202 //-------------------------------------------------- |
|
203 |
|
204 EAP_FUNC_EXPORT bool eap_variable_data_c::get_is_writable() const |
|
205 { |
|
206 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
207 |
|
208 if (m_data == 0) |
|
209 { |
|
210 return false; |
|
211 } |
|
212 |
|
213 return m_data->m_is_writable; |
|
214 } |
|
215 |
|
216 //-------------------------------------------------- |
|
217 |
|
218 EAP_FUNC_EXPORT u8_t * eap_variable_data_c::get_buffer_offset( |
|
219 const u32_t offset, |
|
220 const u32_t buffer_length) const |
|
221 { |
|
222 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
223 |
|
224 if (m_data != 0 |
|
225 && m_data->m_is_valid == true |
|
226 && m_data->m_buffer_length >= (m_data->m_real_data_start_index+offset+buffer_length)) |
|
227 { |
|
228 return m_data->m_buffer + (m_data->m_real_data_start_index + offset); |
|
229 } |
|
230 else |
|
231 { |
|
232 EAP_ASSERT_TOOLS( |
|
233 m_am_tools, |
|
234 m_data != 0 |
|
235 && m_data->m_is_valid == true |
|
236 && m_data->m_buffer_length |
|
237 >= (m_data->m_real_data_start_index+offset+buffer_length)); |
|
238 return 0; |
|
239 } |
|
240 } |
|
241 |
|
242 //-------------------------------------------------- |
|
243 |
|
244 EAP_FUNC_EXPORT u8_t * eap_variable_data_c::get_buffer(const u32_t buffer_length) const |
|
245 { |
|
246 return get_buffer_offset(0u, buffer_length); |
|
247 } |
|
248 |
|
249 //-------------------------------------------------- |
|
250 |
|
251 EAP_FUNC_EXPORT u8_t * eap_variable_data_c::get_data_offset( |
|
252 const u32_t offset, |
|
253 const u32_t data_length) const |
|
254 { |
|
255 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
256 |
|
257 if (m_data != 0 |
|
258 && m_data->m_is_valid == true |
|
259 && m_data->m_real_data_length >= (offset+data_length)) |
|
260 { |
|
261 return get_buffer_offset(offset, data_length); |
|
262 } |
|
263 else |
|
264 { |
|
265 EAP_ASSERT_TOOLS( |
|
266 m_am_tools, |
|
267 m_data != 0 |
|
268 && m_data->m_is_valid == true |
|
269 && m_data->m_real_data_length >= (offset+data_length)); |
|
270 return 0; |
|
271 } |
|
272 } |
|
273 |
|
274 //-------------------------------------------------- |
|
275 |
|
276 #if !defined(USE_EAP_INLINE_FUNCTIONS) |
|
277 |
|
278 EAP_FUNC_EXPORT u8_t * eap_variable_data_c::get_data() const |
|
279 { |
|
280 return get_data(get_data_length()); |
|
281 } |
|
282 |
|
283 #endif //#if !defined(USE_EAP_INLINE_FUNCTIONS) |
|
284 |
|
285 |
|
286 //-------------------------------------------------- |
|
287 |
|
288 EAP_FUNC_EXPORT u8_t * eap_variable_data_c::get_data(const u32_t data_length) const |
|
289 { |
|
290 return get_data_offset(0u, data_length); |
|
291 } |
|
292 |
|
293 //-------------------------------------------------- |
|
294 |
|
295 #if !defined(USE_EAP_INLINE_FUNCTIONS) |
|
296 |
|
297 EAP_FUNC_EXPORT u32_t eap_variable_data_c::get_data_length() const |
|
298 { |
|
299 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
300 |
|
301 if (m_data != 0 |
|
302 && m_data->m_is_valid == true) |
|
303 { |
|
304 return m_data->m_real_data_length; |
|
305 } |
|
306 else |
|
307 { |
|
308 return 0u; |
|
309 } |
|
310 } |
|
311 |
|
312 #endif //#if !defined(USE_EAP_INLINE_FUNCTIONS) |
|
313 |
|
314 //-------------------------------------------------- |
|
315 |
|
316 #if !defined(USE_EAP_INLINE_FUNCTIONS) |
|
317 |
|
318 EAP_FUNC_EXPORT u32_t eap_variable_data_c::get_buffer_length() const |
|
319 { |
|
320 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
321 |
|
322 if (m_data != 0 |
|
323 && m_data->m_is_valid == true) |
|
324 { |
|
325 return m_data->m_buffer_length; |
|
326 } |
|
327 else |
|
328 { |
|
329 return 0u; |
|
330 } |
|
331 } |
|
332 |
|
333 #endif //#if !defined(USE_EAP_INLINE_FUNCTIONS) |
|
334 |
|
335 //-------------------------------------------------- |
|
336 |
|
337 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::reset_start_offset_and_data_length() |
|
338 { |
|
339 if (m_data != 0 |
|
340 && m_data->m_is_valid == true) |
|
341 { |
|
342 m_data->m_real_data_start_index = 0ul; |
|
343 m_data->m_real_data_length = 0ul; |
|
344 |
|
345 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
346 } |
|
347 else |
|
348 { |
|
349 EAP_ASSERT_TOOLS( |
|
350 m_am_tools, |
|
351 m_data != 0 |
|
352 && m_data->m_is_valid == true); |
|
353 |
|
354 return EAP_STATUS_RETURN(m_am_tools, eap_status_process_general_error); |
|
355 } |
|
356 } |
|
357 |
|
358 //-------------------------------------------------- |
|
359 |
|
360 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_start_offset(const u32_t index) |
|
361 { |
|
362 if (m_data != 0 |
|
363 && m_data->m_is_valid == true |
|
364 && m_data->m_real_data_length >= index) |
|
365 { |
|
366 m_data->m_real_data_start_index += index; |
|
367 m_data->m_real_data_length -= index; |
|
368 |
|
369 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
370 } |
|
371 else |
|
372 { |
|
373 EAP_ASSERT_TOOLS( |
|
374 m_am_tools, |
|
375 m_data != 0 |
|
376 && m_data->m_is_valid == true |
|
377 && m_data->m_real_data_length >= index); |
|
378 |
|
379 return EAP_STATUS_RETURN(m_am_tools, eap_status_process_general_error); |
|
380 } |
|
381 } |
|
382 |
|
383 //-------------------------------------------------- |
|
384 |
|
385 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_data_length( |
|
386 const u32_t length) |
|
387 { |
|
388 if (m_data != 0 |
|
389 && m_data->m_is_valid == true |
|
390 && m_data->m_buffer_length >= (m_data->m_real_data_start_index + length)) |
|
391 { |
|
392 m_data->m_real_data_length = length; |
|
393 |
|
394 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
395 } |
|
396 else |
|
397 { |
|
398 EAP_ASSERT_TOOLS( |
|
399 m_am_tools, |
|
400 m_data != 0 |
|
401 && m_data->m_is_valid == true |
|
402 && m_data->m_buffer_length >= (m_data->m_real_data_start_index + length)); |
|
403 |
|
404 return EAP_STATUS_RETURN(m_am_tools, eap_status_process_general_error); |
|
405 } |
|
406 } |
|
407 |
|
408 //-------------------------------------------------- |
|
409 |
|
410 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::reset() |
|
411 { |
|
412 if (m_data == 0) |
|
413 { |
|
414 eap_status_e status = initialize_members(); |
|
415 if (status != eap_status_ok) |
|
416 { |
|
417 // ERROR. |
|
418 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
419 } |
|
420 } |
|
421 else |
|
422 { |
|
423 if (m_data->m_free_buffer == true) |
|
424 { |
|
425 |
|
426 #if !defined(NO_EAP_VARIABLE_DATA_MEMORY_ZERO) |
|
427 m_am_tools->memset(m_data->m_buffer, 0, m_data->m_buffer_length); |
|
428 #endif //#if !defined(NO_EAP_VARIABLE_DATA_MEMORY_ZERO) |
|
429 |
|
430 delete [] m_data->m_buffer; |
|
431 m_data->m_free_buffer = false; |
|
432 } |
|
433 m_data->m_buffer = 0; |
|
434 m_data->m_buffer_length = 0u; |
|
435 m_data->m_real_data_start_index = 0ul; |
|
436 m_data->m_real_data_length = 0u; |
|
437 } |
|
438 |
|
439 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
440 } |
|
441 |
|
442 //-------------------------------------------------- |
|
443 |
|
444 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_buffer( |
|
445 const void * const p_buffer, |
|
446 const u32_t buffer_length, |
|
447 bool free_buffer, |
|
448 bool is_writable) |
|
449 { |
|
450 return set_buffer( |
|
451 const_cast<void *>(p_buffer), |
|
452 buffer_length, |
|
453 free_buffer, |
|
454 is_writable); |
|
455 } |
|
456 |
|
457 //-------------------------------------------------- |
|
458 |
|
459 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_buffer( |
|
460 void * const p_buffer, |
|
461 const u32_t buffer_length, |
|
462 bool free_buffer, |
|
463 bool is_writable) |
|
464 { |
|
465 if (m_data == 0) |
|
466 { |
|
467 eap_status_e status = initialize_members(); |
|
468 if (status != eap_status_ok) |
|
469 { |
|
470 // ERROR. |
|
471 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
472 } |
|
473 } |
|
474 |
|
475 if (p_buffer == 0 |
|
476 && buffer_length != 0UL) |
|
477 { |
|
478 return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); |
|
479 } |
|
480 |
|
481 reset(); |
|
482 |
|
483 m_data->m_free_buffer = free_buffer; |
|
484 m_data->m_is_writable = is_writable; |
|
485 m_data->m_buffer = static_cast<u8_t *>(p_buffer); |
|
486 m_data->m_buffer_length = buffer_length; |
|
487 m_data->m_real_data_start_index = 0ul; |
|
488 m_data->m_real_data_length = buffer_length; |
|
489 |
|
490 // This cannot fail. |
|
491 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
492 } |
|
493 |
|
494 //-------------------------------------------------- |
|
495 |
|
496 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_buffer( |
|
497 const eap_variable_data_c * const buffer) |
|
498 { |
|
499 if (m_data == 0) |
|
500 { |
|
501 eap_status_e status = initialize_members(); |
|
502 if (status != eap_status_ok) |
|
503 { |
|
504 // ERROR. |
|
505 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
506 } |
|
507 } |
|
508 |
|
509 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
510 |
|
511 if (buffer == 0) |
|
512 { |
|
513 return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); |
|
514 } |
|
515 |
|
516 if (buffer == this) |
|
517 { |
|
518 // Here we are setting buffer from from itself. |
|
519 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
520 } |
|
521 |
|
522 return EAP_STATUS_RETURN(m_am_tools, set_buffer( |
|
523 buffer->get_data(), |
|
524 buffer->get_data_length(), |
|
525 false, |
|
526 true)); |
|
527 } |
|
528 |
|
529 //-------------------------------------------------- |
|
530 |
|
531 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_copy_of_buffer( |
|
532 const void * const buffer, |
|
533 const u32_t buffer_length) |
|
534 { |
|
535 if (m_data == 0) |
|
536 { |
|
537 eap_status_e status = initialize_members(); |
|
538 if (status != eap_status_ok) |
|
539 { |
|
540 // ERROR. |
|
541 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
542 } |
|
543 } |
|
544 |
|
545 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
546 |
|
547 if (buffer == m_data->m_buffer) |
|
548 { |
|
549 // Here we are copying from itself. |
|
550 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
551 } |
|
552 |
|
553 if (buffer == 0) |
|
554 { |
|
555 return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); |
|
556 } |
|
557 |
|
558 eap_status_e status = init(buffer_length); |
|
559 if (status != eap_status_ok) |
|
560 { |
|
561 return EAP_STATUS_RETURN(m_am_tools, status); |
|
562 } |
|
563 set_is_valid(); |
|
564 |
|
565 if (buffer_length > 0u) |
|
566 { |
|
567 m_am_tools->memmove(m_data->m_buffer, buffer, buffer_length); |
|
568 } |
|
569 |
|
570 set_data_length(buffer_length); |
|
571 |
|
572 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
573 } |
|
574 |
|
575 //-------------------------------------------------- |
|
576 |
|
577 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_copy_of_buffer( |
|
578 const eap_variable_data_c * const buffer) |
|
579 { |
|
580 if (m_data == 0) |
|
581 { |
|
582 eap_status_e status = initialize_members(); |
|
583 if (status != eap_status_ok) |
|
584 { |
|
585 // ERROR. |
|
586 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
587 } |
|
588 } |
|
589 |
|
590 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
591 |
|
592 if (buffer == 0 |
|
593 || buffer->get_is_valid() == false) |
|
594 { |
|
595 return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); |
|
596 } |
|
597 |
|
598 if (buffer == this) |
|
599 { |
|
600 // Here we are copying from itself. |
|
601 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
602 } |
|
603 |
|
604 return EAP_STATUS_RETURN(m_am_tools, set_copy_of_buffer( |
|
605 buffer->get_data(), |
|
606 buffer->get_data_length())); |
|
607 } |
|
608 |
|
609 |
|
610 //-------------------------------------------------- |
|
611 |
|
612 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::allocate_buffer( |
|
613 const u32_t required_buffer_length) |
|
614 { |
|
615 u8_t *tmp_buffer = new u8_t[required_buffer_length]; |
|
616 if (tmp_buffer == 0) |
|
617 { |
|
618 EAP_TRACE_ERROR( |
|
619 m_am_tools, |
|
620 TRACE_FLAGS_DEFAULT, |
|
621 (EAPL("eap_variable_data_c::allocate_buffer(): Cannot allocate %d bytes.\n"), |
|
622 required_buffer_length)); |
|
623 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
624 } |
|
625 |
|
626 // This is for testing. It is better leave buffer uninitialized. |
|
627 // Memory checking programs could track use of uninitialized data. |
|
628 |
|
629 if (m_data->m_real_data_length > 0u) |
|
630 { |
|
631 // Note the data is copied from start index of original buffer |
|
632 // to the begin of the new buffer. |
|
633 m_am_tools->memmove( |
|
634 tmp_buffer, |
|
635 m_data->m_buffer + m_data->m_real_data_start_index, |
|
636 m_data->m_real_data_length); |
|
637 } |
|
638 |
|
639 if (m_data->m_free_buffer == true) |
|
640 { |
|
641 |
|
642 #if !defined(NO_EAP_VARIABLE_DATA_MEMORY_ZERO) |
|
643 m_am_tools->memset(m_data->m_buffer, 0, m_data->m_buffer_length); |
|
644 #endif //#if !defined(NO_EAP_VARIABLE_DATA_MEMORY_ZERO) |
|
645 |
|
646 delete [] m_data->m_buffer; |
|
647 } |
|
648 |
|
649 m_data->m_buffer = tmp_buffer; |
|
650 m_data->m_buffer_length = required_buffer_length; |
|
651 m_data->m_real_data_start_index = 0ul; // Data is now in the begin of the new buffer. |
|
652 m_data->m_free_buffer = true; |
|
653 m_data->m_is_writable = true; |
|
654 set_is_valid(); |
|
655 |
|
656 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
657 } |
|
658 |
|
659 //-------------------------------------------------- |
|
660 |
|
661 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::set_buffer_length( |
|
662 const u32_t p_buffer_length) |
|
663 { |
|
664 if (m_data == 0) |
|
665 { |
|
666 eap_status_e status = initialize_members(); |
|
667 if (status != eap_status_ok) |
|
668 { |
|
669 // ERROR. |
|
670 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
671 } |
|
672 } |
|
673 |
|
674 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
675 |
|
676 u32_t required_buffer_length = p_buffer_length; |
|
677 if (required_buffer_length < 1ul) |
|
678 { |
|
679 required_buffer_length = 1ul; |
|
680 } |
|
681 |
|
682 if (m_data->m_buffer_length < required_buffer_length) |
|
683 { |
|
684 eap_status_e status = allocate_buffer(required_buffer_length); |
|
685 if (status != eap_status_ok) |
|
686 { |
|
687 return EAP_STATUS_RETURN(m_am_tools, status); |
|
688 } |
|
689 } |
|
690 |
|
691 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
692 } |
|
693 |
|
694 //-------------------------------------------------- |
|
695 |
|
696 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::add_data_to_offset( |
|
697 const u32_t offset, |
|
698 const void * const buffer, |
|
699 const u32_t buffer_length) |
|
700 { |
|
701 if (m_data == 0) |
|
702 { |
|
703 eap_status_e status = initialize_members(); |
|
704 if (status != eap_status_ok) |
|
705 { |
|
706 // ERROR. |
|
707 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
708 } |
|
709 } |
|
710 |
|
711 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
712 EAP_ASSERT_TOOLS(m_am_tools, m_data->m_is_valid == true); |
|
713 |
|
714 const u32_t offset_and_buffer_length = offset + buffer_length; |
|
715 |
|
716 u32_t required_buffer_length |
|
717 = m_data->m_real_data_start_index; |
|
718 |
|
719 if (m_data->m_real_data_length > offset_and_buffer_length) |
|
720 { |
|
721 required_buffer_length |
|
722 += m_data->m_real_data_length; |
|
723 } |
|
724 else |
|
725 { |
|
726 required_buffer_length |
|
727 += offset_and_buffer_length; |
|
728 |
|
729 if (required_buffer_length == 0ul) |
|
730 { |
|
731 ++required_buffer_length; |
|
732 } |
|
733 } |
|
734 |
|
735 if (m_data->m_buffer_length < required_buffer_length) |
|
736 { |
|
737 eap_status_e status = allocate_buffer(required_buffer_length); |
|
738 if (status != eap_status_ok) |
|
739 { |
|
740 return EAP_STATUS_RETURN(m_am_tools, status); |
|
741 } |
|
742 } |
|
743 |
|
744 if (buffer_length != 0u |
|
745 && buffer != 0) |
|
746 { |
|
747 m_am_tools->memmove( |
|
748 m_data->m_buffer |
|
749 + (m_data->m_real_data_start_index |
|
750 + offset), |
|
751 buffer, |
|
752 buffer_length); |
|
753 } |
|
754 |
|
755 if (offset_and_buffer_length > m_data->m_real_data_length) |
|
756 { |
|
757 m_data->m_real_data_length = offset_and_buffer_length; |
|
758 } |
|
759 |
|
760 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
761 } |
|
762 |
|
763 //-------------------------------------------------- |
|
764 |
|
765 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::add_data_to_offset( |
|
766 const u32_t offset, |
|
767 const eap_variable_data_c * const buffer) |
|
768 { |
|
769 if (get_is_valid() == false) |
|
770 { |
|
771 // ERROR. |
|
772 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
773 } |
|
774 |
|
775 return EAP_STATUS_RETURN(m_am_tools, add_data_to_offset( |
|
776 offset, |
|
777 buffer->get_data(), |
|
778 buffer->get_data_length())); |
|
779 } |
|
780 |
|
781 //-------------------------------------------------- |
|
782 |
|
783 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::add_data( |
|
784 const void * const buffer, |
|
785 const u32_t buffer_length) |
|
786 { |
|
787 if (get_is_valid() == false) |
|
788 { |
|
789 // ERROR. |
|
790 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
791 } |
|
792 |
|
793 return EAP_STATUS_RETURN( |
|
794 m_am_tools, |
|
795 add_data_to_offset( |
|
796 get_data_length(), |
|
797 buffer, |
|
798 buffer_length)); |
|
799 } |
|
800 |
|
801 //-------------------------------------------------- |
|
802 |
|
803 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::add_data( |
|
804 const eap_variable_data_c * const buffer) |
|
805 { |
|
806 if (buffer == 0) |
|
807 { |
|
808 return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); |
|
809 } |
|
810 |
|
811 return EAP_STATUS_RETURN(m_am_tools, add_data( |
|
812 buffer->get_data(), |
|
813 buffer->get_data_length())); |
|
814 } |
|
815 |
|
816 //-------------------------------------------------- |
|
817 |
|
818 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::add_end_null() |
|
819 { |
|
820 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
821 |
|
822 u16_t zero(0ul); |
|
823 eap_status_e status = add_data(&zero, sizeof(zero)); |
|
824 if (status != eap_status_ok) |
|
825 { |
|
826 return EAP_STATUS_RETURN(m_am_tools, status); |
|
827 } |
|
828 |
|
829 // This removes the null from data length. |
|
830 set_data_length(get_data_length() - sizeof(zero)); |
|
831 |
|
832 return EAP_STATUS_RETURN(m_am_tools, status); |
|
833 } |
|
834 |
|
835 //-------------------------------------------------- |
|
836 |
|
837 EAP_FUNC_EXPORT eap_status_e eap_variable_data_c::init( |
|
838 const u32_t length) |
|
839 { |
|
840 if (m_data == 0) |
|
841 { |
|
842 eap_status_e status = initialize_members(); |
|
843 if (status != eap_status_ok) |
|
844 { |
|
845 // ERROR. |
|
846 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
847 } |
|
848 } |
|
849 |
|
850 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
851 |
|
852 u32_t required_buffer_length = length; |
|
853 if (length == 0u) |
|
854 { |
|
855 required_buffer_length = 1ul; |
|
856 } |
|
857 |
|
858 if (required_buffer_length > m_data->m_buffer_length // Too small buffer. |
|
859 || m_data->m_is_writable == false) // Old buffer is read only. |
|
860 { |
|
861 // Must allocate a new m_buffer. |
|
862 |
|
863 reset(); |
|
864 |
|
865 m_data->m_buffer = new u8_t[required_buffer_length]; |
|
866 if (m_data->m_buffer == 0) |
|
867 { |
|
868 EAP_TRACE_ERROR( |
|
869 m_am_tools, |
|
870 TRACE_FLAGS_DEFAULT, |
|
871 (EAPL("eap_variable_data_c::init(): Cannot allocate %d bytes.\n"), |
|
872 required_buffer_length)); |
|
873 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
874 } |
|
875 |
|
876 // This is for testing. It is better leave buffer uninitialized. |
|
877 // Memory checking programs could track use of uninitialized data. |
|
878 |
|
879 m_data->m_free_buffer = true; |
|
880 m_data->m_is_writable = true; |
|
881 m_data->m_buffer_length = required_buffer_length; |
|
882 } |
|
883 |
|
884 m_data->m_real_data_start_index = 0ul; |
|
885 m_data->m_real_data_length = 0u; |
|
886 |
|
887 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
888 } |
|
889 |
|
890 //-------------------------------------------------- |
|
891 |
|
892 EAP_FUNC_EXPORT eap_variable_data_c * eap_variable_data_c::copy() const |
|
893 { |
|
894 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
895 |
|
896 if (m_data == 0) |
|
897 { |
|
898 // ERROR. |
|
899 return 0; |
|
900 } |
|
901 |
|
902 eap_variable_data_c *new_data = new eap_variable_data_c(m_am_tools); |
|
903 |
|
904 if (new_data == 0 |
|
905 || new_data->get_is_valid() == false) |
|
906 { |
|
907 delete new_data; |
|
908 EAP_TRACE_ERROR( |
|
909 m_am_tools, |
|
910 TRACE_FLAGS_DEFAULT, |
|
911 (EAPL("eap_variable_data_c::copy(): Cannot allocate %d bytes.\n"), |
|
912 sizeof(eap_variable_data_c))); |
|
913 return 0; |
|
914 } |
|
915 |
|
916 eap_status_e status = new_data->set_copy_of_buffer(this); |
|
917 if (status != eap_status_ok) |
|
918 { |
|
919 delete new_data; |
|
920 return 0; |
|
921 } |
|
922 |
|
923 return new_data; |
|
924 } |
|
925 |
|
926 //-------------------------------------------------- |
|
927 |
|
928 EAP_FUNC_EXPORT i32_t eap_variable_data_c::compare_length( |
|
929 const void * const data, |
|
930 const u32_t data_length, |
|
931 const u32_t compare_length_of_data) const |
|
932 { |
|
933 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
934 |
|
935 if (m_data == 0) |
|
936 { |
|
937 return -1; |
|
938 } |
|
939 |
|
940 if (get_data_length() < compare_length_of_data) |
|
941 { |
|
942 // Different length, cannot match. |
|
943 return -1; |
|
944 } |
|
945 else if (data_length < compare_length_of_data) |
|
946 { |
|
947 // Different length, cannot match. |
|
948 return 1; |
|
949 } |
|
950 |
|
951 return m_am_tools->memcmp( |
|
952 get_data(), |
|
953 data, |
|
954 compare_length_of_data); |
|
955 } |
|
956 |
|
957 |
|
958 //-------------------------------------------------- |
|
959 |
|
960 EAP_FUNC_EXPORT i32_t eap_variable_data_c::compare_length( |
|
961 const eap_variable_data_c * const data, |
|
962 const u32_t compare_length_of_data) const |
|
963 { |
|
964 return compare_length( |
|
965 data->get_data(), |
|
966 data->get_data_length(), |
|
967 compare_length_of_data); |
|
968 } |
|
969 |
|
970 |
|
971 //-------------------------------------------------- |
|
972 |
|
973 EAP_FUNC_EXPORT i32_t eap_variable_data_c::compare( |
|
974 const void * const data, |
|
975 const u32_t data_length) const |
|
976 { |
|
977 EAP_ASSERT_TOOLS(m_am_tools, m_data != 0); |
|
978 |
|
979 if (m_data == 0) |
|
980 { |
|
981 return -1; |
|
982 } |
|
983 |
|
984 if (get_data_length() < data_length) |
|
985 { |
|
986 // Different length, cannot match. |
|
987 return -1; |
|
988 } |
|
989 else if (get_data_length() > data_length) |
|
990 { |
|
991 // Different length, cannot match. |
|
992 return 1; |
|
993 } |
|
994 |
|
995 return m_am_tools->memcmp( |
|
996 get_data(), |
|
997 data, |
|
998 data_length); |
|
999 } |
|
1000 |
|
1001 |
|
1002 //-------------------------------------------------- |
|
1003 |
|
1004 EAP_FUNC_EXPORT i32_t eap_variable_data_c::compare( |
|
1005 const eap_variable_data_c * const data) const |
|
1006 { |
|
1007 return compare( |
|
1008 data->get_data(), |
|
1009 data->get_data_length()); |
|
1010 } |
|
1011 |
|
1012 |
|
1013 //-------------------------------------------------- |
|
1014 |
|
1015 EAP_FUNC_EXPORT u32_t eap_variable_data_c::hash( |
|
1016 const u32_t size) const |
|
1017 { |
|
1018 if (m_data == 0) |
|
1019 { |
|
1020 return 0ul; |
|
1021 } |
|
1022 |
|
1023 u32_t ihash = 0x55555555; |
|
1024 const u32_t length = get_data_length(); |
|
1025 const u8_t * const id = get_data(length); |
|
1026 |
|
1027 if (id == 0) |
|
1028 { |
|
1029 return 0ul; |
|
1030 } |
|
1031 |
|
1032 for(u32_t ind = 0u; ind < length; ind++) |
|
1033 { |
|
1034 ihash += static_cast<u32_t>(id[ind]) + static_cast<u32_t>((~ind) << 15); |
|
1035 } |
|
1036 |
|
1037 return ihash % size; |
|
1038 } |
|
1039 |
|
1040 //-------------------------------------------------- |
|
1041 |
|
1042 |
|
1043 |
|
1044 // End. |