memencode.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * unsigned memencode (void * memory, size_t extent, char const * format, char const * string);
  11. *
  12. * memory.h
  13. *
  14. *--------------------------------------------------------------------*/
  15. #ifndef MEMENCODE_SOURCE
  16. #define MEMENCODE_SOURCE
  17. #include <string.h>
  18. #include "../tools/memory.h"
  19. #include "../tools/number.h"
  20. #include "../tools/error.h"
  21. #include "../pib/pib.h"
  22. static size_t memstring (void * memory, size_t extent, char const * format, char const * string, size_t length)
  23. {
  24. if (extent < length)
  25. {
  26. error (1, ECANCELED, "Overflow at %s %s", format, string);
  27. }
  28. memset (memory, 0, length);
  29. strncpy (memory, string, length - 1);
  30. return (length);
  31. }
  32. size_t memencode (void * memory, size_t extent, char const * format, char const * string)
  33. {
  34. if (! strcmp (format, "byte"))
  35. {
  36. uint8_t * number = (uint8_t *) (memory);
  37. if (extent < sizeof (* number))
  38. {
  39. error (1, ECANCELED, "Overflow at %s %s", format, string);
  40. }
  41. * number = (uint8_t) (basespec (string, 0, sizeof (* number)));
  42. return (sizeof (* number));
  43. }
  44. if (! strcmp (format, "word"))
  45. {
  46. uint16_t * number = (uint16_t *) (memory);
  47. if (extent < sizeof (* number))
  48. {
  49. error (1, ECANCELED, "Overflow at %s %s", format, string);
  50. }
  51. * number = (uint16_t) (basespec (string, 0, sizeof (* number)));
  52. * number = HTOLE16 (* number);
  53. return (sizeof (* number));
  54. }
  55. if (! strcmp (format, "long"))
  56. {
  57. uint32_t * number = (uint32_t *) (memory);
  58. if (extent < sizeof (* number))
  59. {
  60. error (1, ECANCELED, "Overflow at %s %s", format, string);
  61. }
  62. * number = (uint32_t) (basespec (string, 0, sizeof (* number)));
  63. * number = HTOLE32 (* number);
  64. return (sizeof (* number));
  65. }
  66. if (! strcmp (format, "huge"))
  67. {
  68. uint64_t * number = (uint64_t *) (memory);
  69. if (extent < sizeof (* number))
  70. {
  71. error (1, ECANCELED, "Overflow at %s %s", format, string);
  72. }
  73. * number = (uint64_t) (basespec (string, 0, sizeof (* number)));
  74. * number = HTOLE64 (* number);
  75. return (sizeof (* number));
  76. }
  77. if (! strcmp (format, "text"))
  78. {
  79. extent = (unsigned) (strlen (string));
  80. memcpy (memory, string, extent);
  81. return (extent);
  82. }
  83. if (! strcmp (format, "data"))
  84. {
  85. extent = (unsigned) (dataspec (string, memory, extent));
  86. return (extent);
  87. }
  88. if (! strcmp (format, "fill"))
  89. {
  90. extent = (unsigned) (uintspec (string, 0, extent));
  91. memset (memory, ~ 0, extent);
  92. return (extent);
  93. }
  94. if (! strcmp (format, "zero"))
  95. {
  96. extent = (unsigned) (uintspec (string, 0, extent));
  97. memset (memory, 0, extent);
  98. return (extent);
  99. }
  100. if (! strcmp (format, "skip"))
  101. {
  102. extent = (unsigned) (uintspec (string, 0, extent));
  103. return (extent);
  104. }
  105. #if 1
  106. /*
  107. * tr-069 specific fields that don't really belong in the PIB;
  108. */
  109. if (! strcmp (format, "adminusername") || ! strcmp (format, "adminpassword") || ! strcmp (format, "accessusername"))
  110. {
  111. return (memstring (memory, extent, format, string, PIB_NAME_LEN + 1));
  112. }
  113. if (! strcmp (format, "accesspassword"))
  114. {
  115. return (memstring (memory, extent, format, string, PIB_HFID_LEN + 1));
  116. }
  117. if (! strcmp (format, "username") || ! strcmp (format, "password") || ! strcmp (format, "url"))
  118. {
  119. return (memstring (memory, extent, format, string, PIB_TEXT_LEN + 1));
  120. }
  121. #endif
  122. #if 1
  123. /*
  124. * HPAV specific fields that belong in the PIB;
  125. */
  126. if (! strcmp (format, "hfid"))
  127. {
  128. return (memstring (memory, extent, format, string, PIB_HFID_LEN));
  129. }
  130. if (! strcmp (format, "mac"))
  131. {
  132. return (bytespec (string, memory, ETHER_ADDR_LEN));
  133. }
  134. if (! strcmp (format, "key"))
  135. {
  136. return (bytespec (string, memory, PIB_KEY_LEN));
  137. }
  138. #endif
  139. error (1, ENOTSUP, "%s", format);
  140. return (0);
  141. }
  142. #endif