memencode.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * unsigned memencode (void * memory, size_t extent, char const * format, char const * string);
  44. *
  45. * memory.h
  46. *
  47. *--------------------------------------------------------------------*/
  48. #ifndef MEMENCODE_SOURCE
  49. #define MEMENCODE_SOURCE
  50. #include <string.h>
  51. #include "../tools/memory.h"
  52. #include "../tools/number.h"
  53. #include "../tools/error.h"
  54. #include "../pib/pib.h"
  55. static size_t memstring (void * memory, size_t extent, char const * format, char const * string, size_t length)
  56. {
  57. if (extent < length)
  58. {
  59. error (1, ECANCELED, "Overflow at %s %s", format, string);
  60. }
  61. memset (memory, 0, length);
  62. strncpy (memory, string, length-1);
  63. return (length);
  64. }
  65. size_t memencode (void * memory, size_t extent, char const * format, char const * string)
  66. {
  67. if (!strcmp (format, "byte"))
  68. {
  69. uint8_t * number = (uint8_t *)(memory);
  70. if (extent < sizeof (* number))
  71. {
  72. error (1, ECANCELED, "Overflow at %s %s", format, string);
  73. }
  74. * number = (uint8_t)(basespec (string, 0, sizeof (* number)));
  75. return (sizeof (* number));
  76. }
  77. if (!strcmp (format, "word"))
  78. {
  79. uint16_t * number = (uint16_t *)(memory);
  80. if (extent < sizeof (* number))
  81. {
  82. error (1, ECANCELED, "Overflow at %s %s", format, string);
  83. }
  84. * number = (uint16_t)(basespec (string, 0, sizeof (* number)));
  85. * number = HTOLE16 (* number);
  86. return (sizeof (* number));
  87. }
  88. if (!strcmp (format, "long"))
  89. {
  90. uint32_t * number = (uint32_t *)(memory);
  91. if (extent < sizeof (* number))
  92. {
  93. error (1, ECANCELED, "Overflow at %s %s", format, string);
  94. }
  95. * number = (uint32_t)(basespec (string, 0, sizeof (* number)));
  96. * number = HTOLE32 (* number);
  97. return (sizeof (* number));
  98. }
  99. if (!strcmp (format, "huge"))
  100. {
  101. uint64_t * number = (uint64_t *)(memory);
  102. if (extent < sizeof (* number))
  103. {
  104. error (1, ECANCELED, "Overflow at %s %s", format, string);
  105. }
  106. * number = (uint64_t)(basespec (string, 0, sizeof (* number)));
  107. * number = HTOLE64 (* number);
  108. return (sizeof (* number));
  109. }
  110. if (!strcmp (format, "text"))
  111. {
  112. extent = (unsigned)(strlen (string));
  113. memcpy (memory, string, extent);
  114. return (extent);
  115. }
  116. if (!strcmp (format, "data"))
  117. {
  118. extent = (unsigned)(dataspec (string, memory, extent));
  119. return (extent);
  120. }
  121. if (!strcmp (format, "fill"))
  122. {
  123. extent = (unsigned)(uintspec (string, 0, extent));
  124. memset (memory, ~0, extent);
  125. return (extent);
  126. }
  127. if (!strcmp (format, "zero"))
  128. {
  129. extent = (unsigned)(uintspec (string, 0, extent));
  130. memset (memory, 0, extent);
  131. return (extent);
  132. }
  133. if (!strcmp (format, "skip"))
  134. {
  135. extent = (unsigned)(uintspec (string, 0, extent));
  136. return (extent);
  137. }
  138. #if 1
  139. /*
  140. * tr-069 specific fields that don't really belong in the PIB;
  141. */
  142. if (!strcmp (format, "adminusername") || !strcmp (format, "adminpassword") || !strcmp (format, "accessusername"))
  143. {
  144. return (memstring (memory, extent, format, string, PIB_NAME_LEN + 1));
  145. }
  146. if (!strcmp (format, "accesspassword"))
  147. {
  148. return (memstring (memory, extent, format, string, PIB_HFID_LEN + 1));
  149. }
  150. if (!strcmp (format, "username") || !strcmp (format, "password") || !strcmp (format, "url"))
  151. {
  152. return (memstring (memory, extent, format, string, PIB_TEXT_LEN + 1));
  153. }
  154. #endif
  155. #if 1
  156. /*
  157. * HPAV specific fields that belong in the PIB;
  158. */
  159. if (!strcmp (format, "hfid"))
  160. {
  161. return (memstring (memory, extent, format, string, PIB_HFID_LEN));
  162. }
  163. if (!strcmp (format, "mac"))
  164. {
  165. return (bytespec (string, memory, ETHER_ADDR_LEN));
  166. }
  167. if (!strcmp (format, "key"))
  168. {
  169. return (bytespec (string, memory, PIB_KEY_LEN));
  170. }
  171. #endif
  172. error (1, ENOTSUP, "%s", format);
  173. return (0);
  174. }
  175. #endif