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 | |