mbed port of tinydtls
numeric.h@0:04990d454f45, 2013-10-10 (annotated)
- Committer:
- ashleymills
- Date:
- Thu Oct 10 21:38:07 2013 +0000
- Revision:
- 0:04990d454f45
It now works. Found nasty gotcha with non-std sockaddr_in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:04990d454f45 | 1 | /* dtls -- a very basic DTLS implementation |
ashleymills | 0:04990d454f45 | 2 | * |
ashleymills | 0:04990d454f45 | 3 | * Copyright (C) 2011 Olaf Bergmann <bergmann@tzi.org> |
ashleymills | 0:04990d454f45 | 4 | * |
ashleymills | 0:04990d454f45 | 5 | * Permission is hereby granted, free of charge, to any person |
ashleymills | 0:04990d454f45 | 6 | * obtaining a copy of this software and associated documentation |
ashleymills | 0:04990d454f45 | 7 | * files (the "Software"), to deal in the Software without |
ashleymills | 0:04990d454f45 | 8 | * restriction, including without limitation the rights to use, copy, |
ashleymills | 0:04990d454f45 | 9 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
ashleymills | 0:04990d454f45 | 10 | * of the Software, and to permit persons to whom the Software is |
ashleymills | 0:04990d454f45 | 11 | * furnished to do so, subject to the following conditions: |
ashleymills | 0:04990d454f45 | 12 | * |
ashleymills | 0:04990d454f45 | 13 | * The above copyright notice and this permission notice shall be |
ashleymills | 0:04990d454f45 | 14 | * included in all copies or substantial portions of the Software. |
ashleymills | 0:04990d454f45 | 15 | * |
ashleymills | 0:04990d454f45 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
ashleymills | 0:04990d454f45 | 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
ashleymills | 0:04990d454f45 | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ashleymills | 0:04990d454f45 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
ashleymills | 0:04990d454f45 | 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
ashleymills | 0:04990d454f45 | 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
ashleymills | 0:04990d454f45 | 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
ashleymills | 0:04990d454f45 | 23 | * SOFTWARE. |
ashleymills | 0:04990d454f45 | 24 | */ |
ashleymills | 0:04990d454f45 | 25 | |
ashleymills | 0:04990d454f45 | 26 | #ifndef _NUMERIC_H_ |
ashleymills | 0:04990d454f45 | 27 | #define _NUMERIC_H_ |
ashleymills | 0:04990d454f45 | 28 | |
ashleymills | 0:04990d454f45 | 29 | #include <stdint.h> |
ashleymills | 0:04990d454f45 | 30 | |
ashleymills | 0:04990d454f45 | 31 | #ifndef min |
ashleymills | 0:04990d454f45 | 32 | #define min(A,B) ((A) <= (B) ? (A) : (B)) |
ashleymills | 0:04990d454f45 | 33 | #endif |
ashleymills | 0:04990d454f45 | 34 | |
ashleymills | 0:04990d454f45 | 35 | #ifndef max |
ashleymills | 0:04990d454f45 | 36 | #define max(A,B) ((A) < (B) ? (B) : (A)) |
ashleymills | 0:04990d454f45 | 37 | #endif |
ashleymills | 0:04990d454f45 | 38 | |
ashleymills | 0:04990d454f45 | 39 | /** |
ashleymills | 0:04990d454f45 | 40 | * Increments given \p Var of type \p Type by \c 1. |
ashleymills | 0:04990d454f45 | 41 | * |
ashleymills | 0:04990d454f45 | 42 | * \hideinitializer |
ashleymills | 0:04990d454f45 | 43 | */ |
ashleymills | 0:04990d454f45 | 44 | #define inc_uint(Type,Var) { \ |
ashleymills | 0:04990d454f45 | 45 | int i = sizeof(Type); \ |
ashleymills | 0:04990d454f45 | 46 | while (i && !++((Var)[--i])); \ |
ashleymills | 0:04990d454f45 | 47 | } |
ashleymills | 0:04990d454f45 | 48 | |
ashleymills | 0:04990d454f45 | 49 | /* this one is for consistency... */ |
ashleymills | 0:04990d454f45 | 50 | #define dtls_int_to_uint8(Field,Value) do { \ |
ashleymills | 0:04990d454f45 | 51 | *(unsigned char*)(Field) = (Value) & 0xff; \ |
ashleymills | 0:04990d454f45 | 52 | } while(0) |
ashleymills | 0:04990d454f45 | 53 | |
ashleymills | 0:04990d454f45 | 54 | #define dtls_int_to_uint16(Field,Value) do { \ |
ashleymills | 0:04990d454f45 | 55 | *(unsigned char*)(Field) = ((Value) >> 8) & 0xff; \ |
ashleymills | 0:04990d454f45 | 56 | *(((unsigned char*)(Field))+1) = ((Value) & 0xff); \ |
ashleymills | 0:04990d454f45 | 57 | } while(0) |
ashleymills | 0:04990d454f45 | 58 | |
ashleymills | 0:04990d454f45 | 59 | #define dtls_int_to_uint24(Field,Value) do { \ |
ashleymills | 0:04990d454f45 | 60 | *(unsigned char*)(Field) = ((Value) >> 16) & 0xff; \ |
ashleymills | 0:04990d454f45 | 61 | dtls_int_to_uint16((((unsigned char*)(Field))+1),Value); \ |
ashleymills | 0:04990d454f45 | 62 | } while(0) |
ashleymills | 0:04990d454f45 | 63 | |
ashleymills | 0:04990d454f45 | 64 | #define dtls_int_to_uint32(Field,Value) do { \ |
ashleymills | 0:04990d454f45 | 65 | *(unsigned char*)(Field) = ((Value) >> 24) & 0xff; \ |
ashleymills | 0:04990d454f45 | 66 | *(((unsigned char*)(Field))+1) = ((Value) >> 16) & 0xff; \ |
ashleymills | 0:04990d454f45 | 67 | *(((unsigned char*)(Field))+2) = ((Value) >> 8) & 0xff; \ |
ashleymills | 0:04990d454f45 | 68 | *(((unsigned char*)(Field))+3) = (Value) & 0xff; \ |
ashleymills | 0:04990d454f45 | 69 | } while(0) |
ashleymills | 0:04990d454f45 | 70 | |
ashleymills | 0:04990d454f45 | 71 | #define dtls_ulong_to_uint48(Field,Value) do { \ |
ashleymills | 0:04990d454f45 | 72 | *(unsigned char*)(Field) = ((Value) >> 40) & 0xff; \ |
ashleymills | 0:04990d454f45 | 73 | *(((unsigned char*)(Field))+1) = ((Value) >> 32) & 0xff; \ |
ashleymills | 0:04990d454f45 | 74 | *(((unsigned char*)(Field))+2) = ((Value) >> 24) & 0xff; \ |
ashleymills | 0:04990d454f45 | 75 | *(((unsigned char*)(Field))+3) = ((Value) >> 16) & 0xff; \ |
ashleymills | 0:04990d454f45 | 76 | *(((unsigned char*)(Field))+4) = ((Value) >> 8) & 0xff; \ |
ashleymills | 0:04990d454f45 | 77 | *(((unsigned char*)(Field))+5) = (Value) & 0xff; \ |
ashleymills | 0:04990d454f45 | 78 | } while(0) |
ashleymills | 0:04990d454f45 | 79 | |
ashleymills | 0:04990d454f45 | 80 | #define dtls_ulong_to_uint64(Field,Value) do { \ |
ashleymills | 0:04990d454f45 | 81 | *(unsigned char*)(Field) = ((Value) >> 56) & 0xff; \ |
ashleymills | 0:04990d454f45 | 82 | *(((unsigned char*)(Field))+1) = ((Value) >> 48) & 0xff; \ |
ashleymills | 0:04990d454f45 | 83 | *(((unsigned char*)(Field))+2) = ((Value) >> 40) & 0xff; \ |
ashleymills | 0:04990d454f45 | 84 | *(((unsigned char*)(Field))+3) = ((Value) >> 32) & 0xff; \ |
ashleymills | 0:04990d454f45 | 85 | *(((unsigned char*)(Field))+4) = ((Value) >> 24) & 0xff; \ |
ashleymills | 0:04990d454f45 | 86 | *(((unsigned char*)(Field))+5) = ((Value) >> 16) & 0xff; \ |
ashleymills | 0:04990d454f45 | 87 | *(((unsigned char*)(Field))+6) = ((Value) >> 8) & 0xff; \ |
ashleymills | 0:04990d454f45 | 88 | *(((unsigned char*)(Field))+7) = (Value) & 0xff; \ |
ashleymills | 0:04990d454f45 | 89 | } while(0) |
ashleymills | 0:04990d454f45 | 90 | |
ashleymills | 0:04990d454f45 | 91 | #define dtls_uint8_to_int(Field) \ |
ashleymills | 0:04990d454f45 | 92 | (*(unsigned char*)(Field) & 0xFF) |
ashleymills | 0:04990d454f45 | 93 | |
ashleymills | 0:04990d454f45 | 94 | #define dtls_uint16_to_int(Field) \ |
ashleymills | 0:04990d454f45 | 95 | (((*(unsigned char*)(Field)) << 8) | (*(((unsigned char*)(Field))+1))) |
ashleymills | 0:04990d454f45 | 96 | |
ashleymills | 0:04990d454f45 | 97 | #define dtls_uint24_to_int(Field) \ |
ashleymills | 0:04990d454f45 | 98 | (((*(((unsigned char*)(Field)))) << 16) \ |
ashleymills | 0:04990d454f45 | 99 | | ((*(((unsigned char*)(Field))+1)) << 8) \ |
ashleymills | 0:04990d454f45 | 100 | | ((*(((unsigned char*)(Field))+2)))) |
ashleymills | 0:04990d454f45 | 101 | |
ashleymills | 0:04990d454f45 | 102 | #define dtls_uint48_to_ulong(Field) \ |
ashleymills | 0:04990d454f45 | 103 | (((uint64_t) *(unsigned char*)(Field)) << 40) \ |
ashleymills | 0:04990d454f45 | 104 | | (((uint64_t) *(((unsigned char*)(Field))+1)) << 32) \ |
ashleymills | 0:04990d454f45 | 105 | | (((uint64_t) *(((unsigned char*)(Field))+2)) << 24) \ |
ashleymills | 0:04990d454f45 | 106 | | (((uint64_t) *(((unsigned char*)(Field))+3)) << 16) \ |
ashleymills | 0:04990d454f45 | 107 | | (((uint64_t) *(((unsigned char*)(Field))+4)) << 8) \ |
ashleymills | 0:04990d454f45 | 108 | | (((uint64_t) *(((unsigned char*)(n))+5))) |
ashleymills | 0:04990d454f45 | 109 | |
ashleymills | 0:04990d454f45 | 110 | #endif /* _NUMERIC_H_ */ |