utils.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2014, Tuan PM
  3. * Email: tuanpm@live.com
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holder nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #include <math.h>
  37. #include <stddef.h>
  38. #include "mqtt/utils.h"
  39. uint8_t ICACHE_FLASH_ATTR UTILS_IsIPV4 (int8_t *str)
  40. {
  41. uint8_t segs = 0; /* Segment count. */
  42. uint8_t chcnt = 0; /* Character count within segment. */
  43. uint8_t accum = 0; /* Accumulator for segment. */
  44. /* Catch NULL pointer. */
  45. if (str == 0)
  46. return 0;
  47. /* Process every character in string. */
  48. while (*str != '\0') {
  49. /* Segment changeover. */
  50. if (*str == '.') {
  51. /* Must have some digits in segment. */
  52. if (chcnt == 0)
  53. return 0;
  54. /* Limit number of segments. */
  55. if (++segs == 4)
  56. return 0;
  57. /* Reset segment values and restart loop. */
  58. chcnt = accum = 0;
  59. str++;
  60. continue;
  61. }
  62. /* Check numeric. */
  63. if ((*str < '0') || (*str > '9'))
  64. return 0;
  65. /* Accumulate and check segment. */
  66. if ((accum = accum * 10 + *str - '0') > 255)
  67. return 0;
  68. /* Advance other segment specific stuff and continue loop. */
  69. chcnt++;
  70. str++;
  71. }
  72. /* Check enough segments and enough characters in last segment. */
  73. if (segs != 3)
  74. return 0;
  75. if (chcnt == 0)
  76. return 0;
  77. /* Address okay. */
  78. return 1;
  79. }
  80. uint8_t ICACHE_FLASH_ATTR UTILS_StrToIP(const int8_t* str, void *ip)
  81. {
  82. /* The count of the number of bytes processed. */
  83. int i;
  84. /* A pointer to the next digit to process. */
  85. const char * start;
  86. start = str;
  87. for (i = 0; i < 4; i++) {
  88. /* The digit being processed. */
  89. char c;
  90. /* The value of this byte. */
  91. int n = 0;
  92. while (1) {
  93. c = * start;
  94. start++;
  95. if (c >= '0' && c <= '9') {
  96. n *= 10;
  97. n += c - '0';
  98. }
  99. /* We insist on stopping at "." if we are still parsing
  100. the first, second, or third numbers. If we have reached
  101. the end of the numbers, we will allow any character. */
  102. else if ((i < 3 && c == '.') || i == 3) {
  103. break;
  104. }
  105. else {
  106. return 0;
  107. }
  108. }
  109. if (n >= 256) {
  110. return 0;
  111. }
  112. ((uint8_t*)ip)[i] = n;
  113. }
  114. return 1;
  115. }
  116. uint32_t ICACHE_FLASH_ATTR UTILS_Atoh(const int8_t *s)
  117. {
  118. uint32_t value = 0, digit;
  119. int8_t c;
  120. while ((c = *s++)) {
  121. if ('0' <= c && c <= '9')
  122. digit = c - '0';
  123. else if ('A' <= c && c <= 'F')
  124. digit = c - 'A' + 10;
  125. else if ('a' <= c && c <= 'f')
  126. digit = c - 'a' + 10;
  127. else break;
  128. value = (value << 4) | digit;
  129. }
  130. return value;
  131. }