123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /*====================================================================*
- *
- * Copyright (c) 2013 Qualcomm Atheros, Inc.
- *
- * All rights reserved.
- *
- *====================================================================*/
- #ifndef MEMDECODE_SOURCE
- #define MEMDECODE_SOURCE
- #include <unistd.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <string.h>
- #include <ctype.h>
- #include "../tools/memory.h"
- #include "../tools/number.h"
- #include "../tools/error.h"
- #include "../pib/pib.h"
- /*====================================================================*
- *
- * void getmemory (byte const * memory, size_t extent, char const * object, size_t length);
- *
- *--------------------------------------------------------------------*/
- static void getmemory (byte const * memory, size_t extent, char const * object, size_t length)
- {
- if (length > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB length of " SIZE_T_SPEC " bytes", object, length);
- }
- hexout (memory, length, ':', 0, stdout);
- return;
- }
- /*====================================================================*
- *
- * void getstring (byte const * memory, size_t extent, char const * object, size_t length);
- *
- *--------------------------------------------------------------------*/
- static void getstring (char const * string, size_t extent, char const * object, size_t length)
- {
- if (length > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB length " SIZE_T_SPEC " bytes", object, length);
- }
- while (isprint ((unsigned char)* string) && (length--))
- {
- putc (* string++, stdout);
- }
- return;
- }
- /*====================================================================*
- *
- * void memdecode (void const * memory, size_t extent, char const * format, char * string)
- *
- * extract and print the specified data strings from memory;
- *
- *--------------------------------------------------------------------*/
- size_t memdecode (void const * memory, size_t extent, char const * object, char const * string)
- {
- unsigned length = 0;
- void const * origin = memory;
- if (! strcmp (object, "byte"))
- {
- uint8_t * number = (uint8_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("%u", * number);
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- else if (! strcmp (object, "word"))
- {
- uint16_t * number = (uint16_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("%u", LE16TOH (* number));
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- else if (! strcmp (object, "long"))
- {
- uint32_t * number = (uint32_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("%u", LE32TOH (* number));
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- else if (! strcmp (object, "huge"))
- {
- uint64_t * number = (uint64_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("%llu", LE64TOH (* number));
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- #if 1
- else if (! strcmp (object, "xbyte"))
- {
- uint8_t * number = (uint8_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("0x%02X", * number);
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- else if (! strcmp (object, "xword"))
- {
- uint16_t * number = (uint16_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("0x%04X", LE16TOH (* number));
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- else if (! strcmp (object, "xlong"))
- {
- uint32_t * number = (uint32_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("0x%08X", LE32TOH (* number));
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- else if (! strcmp (object, "xhuge"))
- {
- uint64_t * number = (uint64_t *) (memory);
- if (sizeof (* number) > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- printf ("0x%016LX", LE64TOH (* number));
- memory += sizeof (* number);
- extent -= sizeof (* number);
- }
- #endif
- else if (! strcmp (object, "mac"))
- {
- length = ETHER_ADDR_LEN;
- if (length > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- getmemory (memory, extent, object, length);
- memory += length;
- extent -= length;
- }
- else if (! strcmp (object, "key"))
- {
- length = PIB_KEY_LEN;
- if (length > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- getmemory (memory, extent, object, length);
- memory += length;
- extent -= length;
- }
- else if (! strcmp (object, "hfid"))
- {
- length = PIB_HFID_LEN;
- if (length > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- getstring (memory, extent, object, length);
- memory += length;
- extent -= length;
- }
- #if 1
- else if (! strcmp (object, "adminusername") || ! strcmp (object, "adminpassword") || ! strcmp (object, "accessusername"))
- {
- length = PIB_NAME_LEN + 1;
- if (length > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- getstring (memory, extent, object, length);
- memory += length;
- extent -= length;
- }
- else if (! strcmp (object, "accesspassword"))
- {
- length = PIB_HFID_LEN + 1;
- if (length > extent)
- {
- error (1, ECANCELED, "%s exceeds PIB extent " SIZE_T_SPEC, object, extent);
- }
- getstring (memory, extent, object, length);
- memory += length;
- extent -= length;
- }
- else if (! strcmp (object, "username") || ! strcmp (object, "password") || ! strcmp (object, "url"))
- {
- length = PIB_TEXT_LEN + 1;
- getstring (memory, extent, object, length);
- memory += length;
- extent -= length;
- }
- #endif
- #if 1
- else if (! strcmp (object, "data"))
- {
- if (! string || ! (* string))
- {
- error (1, EINVAL, "%s needs a length", object);
- }
- length = (unsigned) (uintspec (string, 1, extent));
- hexout (memory, length, 0, 0, stdout);
- memory += length;
- extent -= length;
- }
- else if (! strcmp (object, "text"))
- {
- if (! string || ! (* string))
- {
- error (1, EINVAL, "%s needs a length", object);
- }
- length = (unsigned) (uintspec (string, 1, extent));
- getstring (memory, extent, object, length);
- memory += length;
- extent -= length;
- }
- else if (! strcmp (object, "skip"))
- {
- if (! string || ! (* string))
- {
- error (1, EINVAL, "%s needs a length", object);
- }
- length = (unsigned) (uintspec (string, 1, extent));
- memory += length;
- extent -= length;
- }
- #endif
- else
- {
- error (1, ENOTSUP, "%s", object);
- }
- return ((byte *) (memory) - (byte *) (origin));
- }
- /*====================================================================*
- *
- *--------------------------------------------------------------------*/
- #endif
|