helpers.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009-2010 Carl-Daniel Hailfinger
  5. * Copyright (C) 2013 Stefan Tauner
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "flash.h"
  25. /* Returns the minimum number of bits needed to represent the given address.
  26. * FIXME: use mind-blowing implementation. */
  27. uint32_t address_to_bits(uint32_t addr)
  28. {
  29. unsigned int lzb = 0;
  30. while (((1 << (31 - lzb)) & ~addr) != 0)
  31. lzb++;
  32. return 32 - lzb;
  33. }
  34. int bitcount(unsigned long a)
  35. {
  36. int i = 0;
  37. for (; a != 0; a >>= 1)
  38. if (a & 1)
  39. i++;
  40. return i;
  41. }
  42. int max(int a, int b)
  43. {
  44. return (a > b) ? a : b;
  45. }
  46. int min(int a, int b)
  47. {
  48. return (a < b) ? a : b;
  49. }
  50. char *strcat_realloc(char *dest, const char *src)
  51. {
  52. dest = realloc(dest, strlen(dest) + strlen(src) + 1);
  53. if (!dest) {
  54. msg_gerr("Out of memory!\n");
  55. return NULL;
  56. }
  57. strcat(dest, src);
  58. return dest;
  59. }
  60. void tolower_string(char *str)
  61. {
  62. for (; *str != '\0'; str++)
  63. *str = (char)tolower((unsigned char)*str);
  64. }
  65. /* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */
  66. #ifdef __MINGW32__
  67. char* strtok_r(char *str, const char *delim, char **nextp)
  68. {
  69. if (str == NULL)
  70. str = *nextp;
  71. str += strspn(str, delim); /* Skip leading delimiters */
  72. if (*str == '\0')
  73. return NULL;
  74. char *ret = str;
  75. str += strcspn(str, delim); /* Find end of token */
  76. if (*str != '\0')
  77. *str++ = '\0';
  78. *nextp = str;
  79. return ret;
  80. }
  81. #endif
  82. /* There is no strnlen in DJGPP */
  83. #if defined(__DJGPP__) || !defined(HAVE_STRNLEN)
  84. size_t strnlen(const char *str, size_t n)
  85. {
  86. size_t i;
  87. for (i = 0; i < n && str[i] != '\0'; i++)
  88. ;
  89. return i;
  90. }
  91. #endif