Introduce helpers.c

Move some suitable functions there, add it to the Makefile, but leave the
declarations in flash.h for now.

Corresponding to flashrom svn r1819.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
diff --git a/Makefile b/Makefile
index d3c1ba5..7a48889 100644
--- a/Makefile
+++ b/Makefile
@@ -353,7 +353,7 @@
 ###############################################################################
 # Library code.
 
-LIB_OBJS = layout.o flashrom.o udelay.o programmer.o
+LIB_OBJS = layout.o flashrom.o udelay.o programmer.o helpers.o
 
 ###############################################################################
 # Frontend related stuff.
diff --git a/at45db.c b/at45db.c
index 41fc497..5396d7f 100644
--- a/at45db.c
+++ b/at45db.c
@@ -215,17 +215,6 @@
 	return 1;
 }
 
-/* Returns the minimum number of bits needed to represent the given address.
- * FIXME: use mind-blowing implementation.
- * FIXME: move to utility module. */
-static uint32_t address_to_bits(uint32_t addr)
-{
-	unsigned int lzb = 0;
-	while (((1 << (31 - lzb)) & ~addr) != 0)
-		lzb++;
-	return 32 - lzb;
-}
-
 /* In case of non-power-of-two page sizes we need to convert the address flashrom uses to the address the
  * DataFlash chips use. The latter uses a segmented address space where the page address is encoded in the
  * more significant bits and the offset within the page is encoded in the less significant bits. The exact
diff --git a/flash.h b/flash.h
index cf496b6..cb53ad1 100644
--- a/flash.h
+++ b/flash.h
@@ -241,6 +241,14 @@
 int print_supported(void);
 void print_supported_wiki(void);
 
+/* helpers.c */
+uint32_t address_to_bits(uint32_t addr);
+int bitcount(unsigned long a);
+int max(int a, int b);
+int min(int a, int b);
+char *strcat_realloc(char *dest, const char *src);
+void tolower_string(char *str);
+
 /* flashrom.c */
 extern int verbose_screen;
 extern int verbose_logfile;
@@ -251,13 +259,9 @@
 int erase_flash(struct flashctx *flash);
 int probe_flash(struct registered_programmer *pgm, int startchip, struct flashctx *fill_flash, int force);
 int read_flash_to_file(struct flashctx *flash, const char *filename);
-int min(int a, int b);
-int max(int a, int b);
-void tolower_string(char *str);
 char *extract_param(const char *const *haystack, const char *needle, const char *delim);
 int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len);
 int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum write_granularity gran);
-char *strcat_realloc(char *dest, const char *src);
 void print_version(void);
 void print_buildinfo(void);
 void print_banner(void);
diff --git a/flashrom.c b/flashrom.c
index a8a8533..c9e0d01 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -535,42 +535,6 @@
 	return 0;
 }
 
-int min(int a, int b)
-{
-	return (a < b) ? a : b;
-}
-
-int max(int a, int b)
-{
-	return (a > b) ? a : b;
-}
-
-int bitcount(unsigned long a)
-{
-	int i = 0;
-	for (; a != 0; a >>= 1)
-		if (a & 1)
-			i++;
-	return i;
-}
-
-void tolower_string(char *str)
-{
-	for (; *str != '\0'; str++)
-		*str = (char)tolower((unsigned char)*str);
-}
-
-char *strcat_realloc(char *dest, const char *src)
-{
-	dest = realloc(dest, strlen(dest) + strlen(src) + 1);
-	if (!dest) {
-		msg_gerr("Out of memory!\n");
-		return NULL;
-	}
-	strcat(dest, src);
-	return dest;
-}
-
 /* This is a somewhat hacked function similar in some ways to strtok().
  * It will look for needle with a subsequent '=' in haystack, return a copy of
  * needle and remove everything from the first occurrence of needle to the next
diff --git a/helpers.c b/helpers.c
new file mode 100644
index 0000000..63fc880
--- /dev/null
+++ b/helpers.c
@@ -0,0 +1,72 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2009-2010 Carl-Daniel Hailfinger
+ * Copyright (C) 2013 Stefan Tauner
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include "flash.h"
+
+/* Returns the minimum number of bits needed to represent the given address.
+ * FIXME: use mind-blowing implementation. */
+uint32_t address_to_bits(uint32_t addr)
+{
+	unsigned int lzb = 0;
+	while (((1 << (31 - lzb)) & ~addr) != 0)
+		lzb++;
+	return 32 - lzb;
+}
+
+int bitcount(unsigned long a)
+{
+	int i = 0;
+	for (; a != 0; a >>= 1)
+		if (a & 1)
+			i++;
+	return i;
+}
+
+int max(int a, int b)
+{
+	return (a > b) ? a : b;
+}
+
+int min(int a, int b)
+{
+	return (a < b) ? a : b;
+}
+
+char *strcat_realloc(char *dest, const char *src)
+{
+	dest = realloc(dest, strlen(dest) + strlen(src) + 1);
+	if (!dest) {
+		msg_gerr("Out of memory!\n");
+		return NULL;
+	}
+	strcat(dest, src);
+	return dest;
+}
+
+void tolower_string(char *str)
+{
+	for (; *str != '\0'; str++)
+		*str = (char)tolower((unsigned char)*str);
+}
+