hex2IPv4.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*====================================================================*
  2. * Copyright (c) 2020 Qualcomm Technologies, Inc.
  3. * All Rights Reserved.
  4. * Confidential and Proprietary - Qualcomm Technologies, Inc.
  5. *====================================================================*/
  6. /*====================================================================*
  7. * Motley Tools by Charles Maier <cmaier@cmassoc.net>;
  8. * Copyright (c) 2001-2006 by Charles Maier Associates;
  9. * Licensed under the Internet Software Consortium License;
  10. *====================================================================*/
  11. /*====================================================================*
  12. *
  13. * char const * hex2IPv4 (char buffer [], size_t length, void const * memory, size_t extent);
  14. *
  15. * memory.h
  16. *
  17. * encode a character buffer with a decimal equivalent of hexadecimal character string
  18. * having decimals seperated by DEC_EXTENDER, defined in number.h;
  19. *
  20. *--------------------------------------------------------------------*/
  21. #ifndef HEX2IPv4_SOURCE
  22. #define HEX2IPv4_SOURCE
  23. #include <stdint.h>
  24. #include "../tools/memory.h"
  25. #include "../tools/number.h"
  26. #include "../nvm/nvm.h"
  27. unsigned digitcount(unsigned num)
  28. {
  29. unsigned dec, count;
  30. dec = num;
  31. count = 0;
  32. while(dec != 0)
  33. {
  34. dec = dec / RADIX_DEC;
  35. count++;
  36. }
  37. return count;
  38. }
  39. /*====================================================================*
  40. *
  41. * char * myitoa_nonulltermination (unsigned number, char buffer [], size_t length);
  42. *
  43. * non-standard version of itoa () with no null termination where
  44. * buffer is packed right-to-left to avoid reversing digits; ensure
  45. * buffer is longer than needed;
  46. *
  47. *--------------------------------------------------------------------*/
  48. char * myitoa_nonulltermination (unsigned number, char buffer [], size_t length)
  49. {
  50. do
  51. {
  52. buffer [-- length] = '0' + (number % 10);
  53. number /= 10;
  54. }
  55. while (number);
  56. return (buffer);
  57. }
  58. char const * hex2IPv4 (char buffer [], register size_t length, void const * memory, register size_t extent)
  59. {
  60. register char * string = (char *) (buffer);
  61. register byte * offset = (byte *) (memory);
  62. unsigned num, numlength;
  63. if (length)
  64. {
  65. length /= HEX_DIGITS + 1;
  66. while ((length--) && (extent--))
  67. {
  68. num = 0;
  69. num += (int)((* offset >> 0) & 0x0F);
  70. num += ((int)((* offset >> 4) & 0x0F)) * RADIX_HEX;
  71. numlength = digitcount(num);
  72. myitoa_nonulltermination(num, string, numlength);
  73. string += numlength;
  74. if ((length) && (extent))
  75. {
  76. * string++ = DEC_EXTENDER;
  77. }
  78. offset++;
  79. }
  80. * string = (char) (0);
  81. }
  82. return ((char *) (buffer));
  83. }
  84. #endif