Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 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 | |
| 22 | #include <ctype.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include "flash.h" |
| 26 | |
| 27 | /* Returns the minimum number of bits needed to represent the given address. |
| 28 | * FIXME: use mind-blowing implementation. */ |
| 29 | uint32_t address_to_bits(uint32_t addr) |
| 30 | { |
| 31 | unsigned int lzb = 0; |
| 32 | while (((1 << (31 - lzb)) & ~addr) != 0) |
| 33 | lzb++; |
| 34 | return 32 - lzb; |
| 35 | } |
| 36 | |
| 37 | int bitcount(unsigned long a) |
| 38 | { |
| 39 | int i = 0; |
| 40 | for (; a != 0; a >>= 1) |
| 41 | if (a & 1) |
| 42 | i++; |
| 43 | return i; |
| 44 | } |
| 45 | |
| 46 | int max(int a, int b) |
| 47 | { |
| 48 | return (a > b) ? a : b; |
| 49 | } |
| 50 | |
| 51 | int min(int a, int b) |
| 52 | { |
| 53 | return (a < b) ? a : b; |
| 54 | } |
| 55 | |
| 56 | char *strcat_realloc(char *dest, const char *src) |
| 57 | { |
| 58 | dest = realloc(dest, strlen(dest) + strlen(src) + 1); |
| 59 | if (!dest) { |
| 60 | msg_gerr("Out of memory!\n"); |
| 61 | return NULL; |
| 62 | } |
| 63 | strcat(dest, src); |
| 64 | return dest; |
| 65 | } |
| 66 | |
| 67 | void tolower_string(char *str) |
| 68 | { |
| 69 | for (; *str != '\0'; str++) |
| 70 | *str = (char)tolower((unsigned char)*str); |
| 71 | } |
| 72 | |
Stefan Tauner | b41d847 | 2014-11-01 22:56:06 +0000 | [diff] [blame] | 73 | /* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */ |
| 74 | #ifdef __MINGW32__ |
| 75 | char* strtok_r(char *str, const char *delim, char **nextp) |
| 76 | { |
| 77 | if (str == NULL) |
| 78 | str = *nextp; |
| 79 | |
| 80 | str += strspn(str, delim); /* Skip leading delimiters */ |
| 81 | if (*str == '\0') |
| 82 | return NULL; |
| 83 | |
| 84 | char *ret = str; |
| 85 | str += strcspn(str, delim); /* Find end of token */ |
| 86 | if (*str != '\0') |
| 87 | *str++ = '\0'; |
| 88 | |
| 89 | *nextp = str; |
| 90 | return ret; |
| 91 | } |
| 92 | #endif |
| 93 | |
Stefan Tauner | dc62793 | 2015-01-27 18:07:50 +0000 | [diff] [blame] | 94 | /* There is no strnlen in DJGPP */ |
Stefan Tauner | 23e10b8 | 2016-01-23 16:16:49 +0000 | [diff] [blame^] | 95 | #if defined(__DJGPP__) || !defined(HAVE_STRNLEN) |
Stefan Tauner | dc62793 | 2015-01-27 18:07:50 +0000 | [diff] [blame] | 96 | size_t strnlen(const char *str, size_t n) |
| 97 | { |
| 98 | size_t i; |
| 99 | for (i = 0; i < n && str[i] != '\0'; i++) |
| 100 | ; |
| 101 | return i; |
| 102 | } |
| 103 | #endif |