2
0

util.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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-2017, 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. #include "config.h"
  45. #include "util.h"
  46. #include <string.h>
  47. #include <stdint.h>
  48. /* include space for null terminator */
  49. static char bit_string[MAX_PRINT_STRING_LEN + 1];
  50. static inline int hex_char_to_nibble(uint8_t c)
  51. {
  52. switch (c) {
  53. case ('0'):
  54. return 0x0;
  55. case ('1'):
  56. return 0x1;
  57. case ('2'):
  58. return 0x2;
  59. case ('3'):
  60. return 0x3;
  61. case ('4'):
  62. return 0x4;
  63. case ('5'):
  64. return 0x5;
  65. case ('6'):
  66. return 0x6;
  67. case ('7'):
  68. return 0x7;
  69. case ('8'):
  70. return 0x8;
  71. case ('9'):
  72. return 0x9;
  73. case ('a'):
  74. return 0xa;
  75. case ('A'):
  76. return 0xa;
  77. case ('b'):
  78. return 0xb;
  79. case ('B'):
  80. return 0xb;
  81. case ('c'):
  82. return 0xc;
  83. case ('C'):
  84. return 0xc;
  85. case ('d'):
  86. return 0xd;
  87. case ('D'):
  88. return 0xd;
  89. case ('e'):
  90. return 0xe;
  91. case ('E'):
  92. return 0xe;
  93. case ('f'):
  94. return 0xf;
  95. case ('F'):
  96. return 0xf;
  97. default:
  98. return -1; /* this flags an error */
  99. }
  100. /* NOTREACHED */
  101. return -1; /* this keeps compilers from complaining */
  102. }
  103. uint8_t nibble_to_hex_char(uint8_t nibble)
  104. {
  105. char buf[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
  106. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  107. return buf[nibble & 0xF];
  108. }
  109. /*
  110. * hex_string_to_octet_string converts a hexadecimal string
  111. * of length 2 * len to a raw octet string of length len
  112. */
  113. int hex_string_to_octet_string(char *raw, char *hex, int len)
  114. {
  115. uint8_t x;
  116. int tmp;
  117. int hex_len;
  118. hex_len = 0;
  119. while (hex_len < len) {
  120. tmp = hex_char_to_nibble(hex[0]);
  121. if (tmp == -1) {
  122. return hex_len;
  123. }
  124. x = (tmp << 4);
  125. hex_len++;
  126. tmp = hex_char_to_nibble(hex[1]);
  127. if (tmp == -1) {
  128. return hex_len;
  129. }
  130. x |= (tmp & 0xff);
  131. hex_len++;
  132. *raw++ = x;
  133. hex += 2;
  134. }
  135. return hex_len;
  136. }
  137. char *octet_string_hex_string(const void *s, int length)
  138. {
  139. const uint8_t *str = (const uint8_t *)s;
  140. int i;
  141. /* double length, since one octet takes two hex characters */
  142. length *= 2;
  143. /* truncate string if it would be too long */
  144. if (length > MAX_PRINT_STRING_LEN) {
  145. length = MAX_PRINT_STRING_LEN;
  146. }
  147. for (i = 0; i < length; i += 2) {
  148. bit_string[i] = nibble_to_hex_char(*str >> 4);
  149. bit_string[i + 1] = nibble_to_hex_char(*str++ & 0xF);
  150. }
  151. bit_string[i] = 0; /* null terminate string */
  152. return bit_string;
  153. }
  154. static const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  155. "abcdefghijklmnopqrstuvwxyz0123456789+/";
  156. static int base64_block_to_octet_triple(char *out, char *in)
  157. {
  158. unsigned char sextets[4] = { 0 };
  159. int j = 0;
  160. int i;
  161. for (i = 0; i < 4; i++) {
  162. char *p = strchr(b64chars, in[i]);
  163. if (p != NULL) {
  164. sextets[i] = p - b64chars;
  165. } else {
  166. j++;
  167. }
  168. }
  169. out[0] = (sextets[0] << 2) | (sextets[1] >> 4);
  170. if (j < 2) {
  171. out[1] = (sextets[1] << 4) | (sextets[2] >> 2);
  172. }
  173. if (j < 1) {
  174. out[2] = (sextets[2] << 6) | sextets[3];
  175. }
  176. return j;
  177. }
  178. int base64_string_to_octet_string(char *out, int *pad, char *in, int len)
  179. {
  180. int k = 0;
  181. int i = 0;
  182. int j = 0;
  183. if (len % 4 != 0) {
  184. return 0;
  185. }
  186. while (i < len && j == 0) {
  187. j = base64_block_to_octet_triple(out + k, in + i);
  188. k += 3;
  189. i += 4;
  190. }
  191. *pad = j;
  192. return i;
  193. }