strto.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * linux/lib/vsprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
  7. /*
  8. * Wirzenius wrote this portably, Torvalds fucked it up :-)
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <linux/ctype.h>
  13. /* from lib/kstrtox.c */
  14. static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
  15. {
  16. /* Look for a 0x prefix */
  17. if (s[0] == '0') {
  18. int ch = tolower(s[1]);
  19. if (ch == 'x') {
  20. *basep = 16;
  21. s += 2;
  22. } else if (!*basep) {
  23. /* Only select octal if we don't have a base */
  24. *basep = 8;
  25. }
  26. }
  27. /* Use decimal by default */
  28. if (!*basep)
  29. *basep = 10;
  30. return s;
  31. }
  32. /**
  33. * decode_digit() - Decode a single character into its numeric digit value
  34. *
  35. * This ignore case
  36. *
  37. * @ch: Character to convert (expects '0'..'9', 'a'..'f' or 'A'..'F')
  38. * @return value of digit (0..0xf) or 255 if the character is invalid
  39. */
  40. static uint decode_digit(int ch)
  41. {
  42. if (!isxdigit(ch))
  43. return 256;
  44. ch = tolower(ch);
  45. return ch <= '9' ? ch - '0' : ch - 'a' + 0xa;
  46. }
  47. ulong simple_strtoul(const char *cp, char **endp, uint base)
  48. {
  49. ulong result = 0;
  50. uint value;
  51. cp = _parse_integer_fixup_radix(cp, &base);
  52. while (value = decode_digit(*cp), value < base) {
  53. result = result * base + value;
  54. cp++;
  55. }
  56. if (endp)
  57. *endp = (char *)cp;
  58. return result;
  59. }
  60. ulong hextoul(const char *cp, char **endp)
  61. {
  62. return simple_strtoul(cp, endp, 16);
  63. }
  64. ulong dectoul(const char *cp, char **endp)
  65. {
  66. return simple_strtoul(cp, endp, 10);
  67. }
  68. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  69. {
  70. char *tail;
  71. unsigned long val;
  72. size_t len;
  73. *res = 0;
  74. len = strlen(cp);
  75. if (len == 0)
  76. return -EINVAL;
  77. val = simple_strtoul(cp, &tail, base);
  78. if (tail == cp)
  79. return -EINVAL;
  80. if ((*tail == '\0') ||
  81. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  82. *res = val;
  83. return 0;
  84. }
  85. return -EINVAL;
  86. }
  87. long simple_strtol(const char *cp, char **endp, unsigned int base)
  88. {
  89. if (*cp == '-')
  90. return -simple_strtoul(cp + 1, endp, base);
  91. return simple_strtoul(cp, endp, base);
  92. }
  93. unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
  94. {
  95. unsigned long result = simple_strtoul(cp, endp, base);
  96. switch (tolower(**endp)) {
  97. case 'g':
  98. result *= 1024;
  99. /* fall through */
  100. case 'm':
  101. result *= 1024;
  102. /* fall through */
  103. case 'k':
  104. result *= 1024;
  105. (*endp)++;
  106. if (**endp == 'i')
  107. (*endp)++;
  108. if (**endp == 'B')
  109. (*endp)++;
  110. }
  111. return result;
  112. }
  113. unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
  114. {
  115. unsigned long long result = simple_strtoull(cp, endp, base);
  116. switch (tolower(**endp)) {
  117. case 'g':
  118. result *= 1024;
  119. /* fall through */
  120. case 'm':
  121. result *= 1024;
  122. /* fall through */
  123. case 'k':
  124. result *= 1024;
  125. (*endp)++;
  126. if (**endp == 'i')
  127. (*endp)++;
  128. if (**endp == 'B')
  129. (*endp)++;
  130. }
  131. return result;
  132. }
  133. unsigned long long simple_strtoull(const char *cp, char **endp,
  134. unsigned int base)
  135. {
  136. unsigned long long result = 0;
  137. uint value;
  138. cp = _parse_integer_fixup_radix(cp, &base);
  139. while (value = decode_digit(*cp), value < base) {
  140. result = result * base + value;
  141. cp++;
  142. }
  143. if (endp)
  144. *endp = (char *) cp;
  145. return result;
  146. }
  147. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  148. {
  149. if (*cp == '-')
  150. return -simple_strtoull(cp + 1, endp, base);
  151. return simple_strtoull(cp, endp, base);
  152. }
  153. long trailing_strtoln(const char *str, const char *end)
  154. {
  155. const char *p;
  156. if (!end)
  157. end = str + strlen(str);
  158. if (isdigit(end[-1])) {
  159. for (p = end - 1; p > str; p--) {
  160. if (!isdigit(*p))
  161. return dectoul(p + 1, NULL);
  162. }
  163. }
  164. return -1;
  165. }
  166. long trailing_strtol(const char *str)
  167. {
  168. return trailing_strtoln(str, NULL);
  169. }
  170. void str_to_upper(const char *in, char *out, size_t len)
  171. {
  172. for (; len > 0 && *in; len--)
  173. *out++ = toupper(*in++);
  174. if (len)
  175. *out = '\0';
  176. }