Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: test/util.c

Issue 2344973002: Update libsrtp to version 2.0 (Closed)
Patch Set: Add '.' back to include_dirs Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * util.c
3 *
4 * Utilities used by the test apps
5 *
6 * John A. Foley
7 * Cisco Systems, Inc.
8 */
9 /*
10 *
11 * Copyright (c) 2014, Cisco Systems, Inc.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
25 *
26 * Neither the name of the Cisco Systems, Inc. nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 */
44
45 #include <string.h>
46 #include <stdint.h>
47 #include "util.h"
48
49 char bit_string[MAX_PRINT_STRING_LEN];
50
51 static inline int hex_char_to_nibble (uint8_t c)
52 {
53 switch (c) {
54 case ('0'): return 0x0;
55 case ('1'): return 0x1;
56 case ('2'): return 0x2;
57 case ('3'): return 0x3;
58 case ('4'): return 0x4;
59 case ('5'): return 0x5;
60 case ('6'): return 0x6;
61 case ('7'): return 0x7;
62 case ('8'): return 0x8;
63 case ('9'): return 0x9;
64 case ('a'): return 0xa;
65 case ('A'): return 0xa;
66 case ('b'): return 0xb;
67 case ('B'): return 0xb;
68 case ('c'): return 0xc;
69 case ('C'): return 0xc;
70 case ('d'): return 0xd;
71 case ('D'): return 0xd;
72 case ('e'): return 0xe;
73 case ('E'): return 0xe;
74 case ('f'): return 0xf;
75 case ('F'): return 0xf;
76 default: return -1; /* this flags an error */
77 }
78 /* NOTREACHED */
79 return -1; /* this keeps compilers from complaining */
80 }
81
82 uint8_t nibble_to_hex_char (uint8_t nibble)
83 {
84 char buf[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
85 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
86
87 return buf[nibble & 0xF];
88 }
89
90 /*
91 * hex_string_to_octet_string converts a hexadecimal string
92 * of length 2 * len to a raw octet string of length len
93 */
94 int hex_string_to_octet_string (char *raw, char *hex, int len)
95 {
96 uint8_t x;
97 int tmp;
98 int hex_len;
99
100 hex_len = 0;
101 while (hex_len < len) {
102 tmp = hex_char_to_nibble(hex[0]);
103 if (tmp == -1) {
104 return hex_len;
105 }
106 x = (tmp << 4);
107 hex_len++;
108 tmp = hex_char_to_nibble(hex[1]);
109 if (tmp == -1) {
110 return hex_len;
111 }
112 x |= (tmp & 0xff);
113 hex_len++;
114 *raw++ = x;
115 hex += 2;
116 }
117 return hex_len;
118 }
119
120 char * octet_string_hex_string (const void *s, int length)
121 {
122 const uint8_t *str = (const uint8_t*)s;
123 int i;
124
125 /* double length, since one octet takes two hex characters */
126 length *= 2;
127
128 /* truncate string if it would be too long */
129 if (length > MAX_PRINT_STRING_LEN) {
130 length = MAX_PRINT_STRING_LEN - 1;
131 }
132
133 for (i = 0; i < length; i += 2) {
134 bit_string[i] = nibble_to_hex_char(*str >> 4);
135 bit_string[i + 1] = nibble_to_hex_char(*str++ & 0xF);
136 }
137 bit_string[i] = 0; /* null terminate string */
138 return bit_string;
139 }
140
141 static const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
142 "abcdefghijklmnopqrstuvwxyz0123456789+/";
143
144 static int base64_block_to_octet_triple (char *out, char *in)
145 {
146 unsigned char sextets[4] = {};
147 int j = 0;
148 int i;
149
150 for (i = 0; i < 4; i++) {
151 char *p = strchr(b64chars, in[i]);
152 if (p != NULL) {
153 sextets[i] = p - b64chars;
154 } else{ j++; }
155 }
156
157 out[0] = (sextets[0] << 2) | (sextets[1] >> 4);
158 if (j < 2) {
159 out[1] = (sextets[1] << 4) | (sextets[2] >> 2);
160 }
161 if (j < 1) {
162 out[2] = (sextets[2] << 6) | sextets[3];
163 }
164 return j;
165 }
166
167 int base64_string_to_octet_string (char *out, int *pad, char *in, int len)
168 {
169 int k = 0;
170 int i = 0;
171 int j = 0;
172
173 if (len % 4 != 0) {
174 return 0;
175 }
176
177 while (i < len && j == 0) {
178 j = base64_block_to_octet_triple(out + k, in + i);
179 k += 3;
180 i += 4;
181 }
182 *pad = j;
183 return i;
184 }
185
OLDNEW
« no previous file with comments | « test/util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698