memdecode.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. #ifndef MEMDECODE_SOURCE
  9. #define MEMDECODE_SOURCE
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <limits.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include "../tools/memory.h"
  16. #include "../tools/number.h"
  17. #include "../tools/error.h"
  18. #include "../pib/pib.h"
  19. /*====================================================================*
  20. *
  21. * void getmemory (byte const * memory, size_t extent, char const * object, size_t length);
  22. *
  23. *--------------------------------------------------------------------*/
  24. static void getmemory (byte const * memory, size_t extent, char const * object, size_t length)
  25. {
  26. if (length > extent)
  27. {
  28. error (1, ECANCELED, "%s exceeds PIB length of " SIZE_T_SPEC " bytes", object, length);
  29. }
  30. hexout (memory, length, ':', 0, stdout);
  31. return;
  32. }
  33. /*====================================================================*
  34. *
  35. * void getstring (byte const * memory, size_t extent, char const * object, size_t length);
  36. *
  37. *--------------------------------------------------------------------*/
  38. static void getstring (char const * string, size_t extent, char const * object, size_t length)
  39. {
  40. if (length > extent)
  41. {
  42. error (1, ECANCELED, "%s exceeds PIB length " SIZE_T_SPEC " bytes", object, length);
  43. }
  44. while (isprint ((unsigned char)* string) && (length--))
  45. {
  46. putc (* string++, stdout);
  47. }
  48. return;
  49. }
  50. /*====================================================================*
  51. *
  52. * void memdecode (void const * memory, size_t extent, char const * format, char * string)
  53. *
  54. * extract and print the specified data strings from memory;
  55. *
  56. *--------------------------------------------------------------------*/
  57. size_t memdecode (void const * memory, size_t extent, char const * object, char const * string)
  58. {
  59. unsigned length = 0;
  60. void const * origin = memory;
  61. if (! strcmp (object, "byte"))
  62. {
  63. uint8_t * number = (uint8_t *) (memory);
  64. if (sizeof (* number) > extent)
  65. {
  66. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  67. }
  68. printf ("%u", * number);
  69. memory += sizeof (* number);
  70. extent -= sizeof (* number);
  71. }
  72. else if (! strcmp (object, "word"))
  73. {
  74. uint16_t * number = (uint16_t *) (memory);
  75. if (sizeof (* number) > extent)
  76. {
  77. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  78. }
  79. printf ("%u", LE16TOH (* number));
  80. memory += sizeof (* number);
  81. extent -= sizeof (* number);
  82. }
  83. else if (! strcmp (object, "long"))
  84. {
  85. uint32_t * number = (uint32_t *) (memory);
  86. if (sizeof (* number) > extent)
  87. {
  88. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  89. }
  90. printf ("%u", LE32TOH (* number));
  91. memory += sizeof (* number);
  92. extent -= sizeof (* number);
  93. }
  94. else if (! strcmp (object, "huge"))
  95. {
  96. uint64_t * number = (uint64_t *) (memory);
  97. if (sizeof (* number) > extent)
  98. {
  99. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  100. }
  101. printf ("%llu", LE64TOH (* number));
  102. memory += sizeof (* number);
  103. extent -= sizeof (* number);
  104. }
  105. #if 1
  106. else if (! strcmp (object, "xbyte"))
  107. {
  108. uint8_t * number = (uint8_t *) (memory);
  109. if (sizeof (* number) > extent)
  110. {
  111. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  112. }
  113. printf ("0x%02X", * number);
  114. memory += sizeof (* number);
  115. extent -= sizeof (* number);
  116. }
  117. else if (! strcmp (object, "xword"))
  118. {
  119. uint16_t * number = (uint16_t *) (memory);
  120. if (sizeof (* number) > extent)
  121. {
  122. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  123. }
  124. printf ("0x%04X", LE16TOH (* number));
  125. memory += sizeof (* number);
  126. extent -= sizeof (* number);
  127. }
  128. else if (! strcmp (object, "xlong"))
  129. {
  130. uint32_t * number = (uint32_t *) (memory);
  131. if (sizeof (* number) > extent)
  132. {
  133. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  134. }
  135. printf ("0x%08X", LE32TOH (* number));
  136. memory += sizeof (* number);
  137. extent -= sizeof (* number);
  138. }
  139. else if (! strcmp (object, "xhuge"))
  140. {
  141. uint64_t * number = (uint64_t *) (memory);
  142. if (sizeof (* number) > extent)
  143. {
  144. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  145. }
  146. printf ("0x%016LX", LE64TOH (* number));
  147. memory += sizeof (* number);
  148. extent -= sizeof (* number);
  149. }
  150. #endif
  151. else if (! strcmp (object, "mac"))
  152. {
  153. length = ETHER_ADDR_LEN;
  154. if (length > extent)
  155. {
  156. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  157. }
  158. getmemory (memory, extent, object, length);
  159. memory += length;
  160. extent -= length;
  161. }
  162. else if (! strcmp (object, "key"))
  163. {
  164. length = PIB_KEY_LEN;
  165. if (length > extent)
  166. {
  167. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  168. }
  169. getmemory (memory, extent, object, length);
  170. memory += length;
  171. extent -= length;
  172. }
  173. else if (! strcmp (object, "hfid"))
  174. {
  175. length = PIB_HFID_LEN;
  176. if (length > extent)
  177. {
  178. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  179. }
  180. getstring (memory, extent, object, length);
  181. memory += length;
  182. extent -= length;
  183. }
  184. #if 1
  185. else if (! strcmp (object, "adminusername") || ! strcmp (object, "adminpassword") || ! strcmp (object, "accessusername"))
  186. {
  187. length = PIB_NAME_LEN + 1;
  188. if (length > extent)
  189. {
  190. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  191. }
  192. getstring (memory, extent, object, length);
  193. memory += length;
  194. extent -= length;
  195. }
  196. else if (! strcmp (object, "accesspassword"))
  197. {
  198. length = PIB_HFID_LEN + 1;
  199. if (length > extent)
  200. {
  201. error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
  202. }
  203. getstring (memory, extent, object, length);
  204. memory += length;
  205. extent -= length;
  206. }
  207. else if (! strcmp (object, "username") || ! strcmp (object, "password") || ! strcmp (object, "url"))
  208. {
  209. length = PIB_TEXT_LEN + 1;
  210. getstring (memory, extent, object, length);
  211. memory += length;
  212. extent -= length;
  213. }
  214. #endif
  215. #if 1
  216. else if (! strcmp (object, "data"))
  217. {
  218. if (! string || ! (* string))
  219. {
  220. error (1, EINVAL, "%s needs a length", object);
  221. }
  222. length = (unsigned) (uintspec (string, 1, extent));
  223. hexout (memory, length, 0, 0, stdout);
  224. memory += length;
  225. extent -= length;
  226. }
  227. else if (! strcmp (object, "text"))
  228. {
  229. if (! string || ! (* string))
  230. {
  231. error (1, EINVAL, "%s needs a length", object);
  232. }
  233. length = (unsigned) (uintspec (string, 1, extent));
  234. getstring (memory, extent, object, length);
  235. memory += length;
  236. extent -= length;
  237. }
  238. else if (! strcmp (object, "skip"))
  239. {
  240. if (! string || ! (* string))
  241. {
  242. error (1, EINVAL, "%s needs a length", object);
  243. }
  244. length = (unsigned) (uintspec (string, 1, extent));
  245. memory += length;
  246. extent -= length;
  247. }
  248. #endif
  249. else
  250. {
  251. error (1, ENOTSUP, "%s", object);
  252. }
  253. return ((byte *) (memory) - (byte *) (origin));
  254. }
  255. /*====================================================================*
  256. *
  257. *--------------------------------------------------------------------*/
  258. #endif