blob: 289848d7d6cdd3b64eaff5d130145c96472537d2 [file] [log] [blame]
Edward O'Callaghan8dd57922019-03-15 16:21:34 +11001/*
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
18#include <ctype.h>
19#include <stdlib.h>
20#include <string.h>
21#include "flash.h"
22
23/* Returns the minimum number of bits needed to represent the given address.
24 * FIXME: use mind-blowing implementation. */
25uint32_t address_to_bits(uint32_t addr)
26{
27 unsigned int lzb = 0;
28 while (((1 << (31 - lzb)) & ~addr) != 0)
29 lzb++;
30 return 32 - lzb;
31}
32
Edward O'Callaghan2fc166e2019-09-09 00:51:20 +100033unsigned int bitcount(unsigned long a)
Edward O'Callaghan8dd57922019-03-15 16:21:34 +110034{
Edward O'Callaghan2fc166e2019-09-09 00:51:20 +100035 unsigned int i = 0;
Edward O'Callaghan8dd57922019-03-15 16:21:34 +110036 for (; a != 0; a >>= 1)
37 if (a & 1)
38 i++;
39 return i;
40}
41
42int max(int a, int b)
43{
44 return (a > b) ? a : b;
45}
46
47int min(int a, int b)
48{
49 return (a < b) ? a : b;
50}
51
52char *strcat_realloc(char *dest, const char *src)
53{
54 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
55 if (!dest) {
56 msg_gerr("Out of memory!\n");
57 return NULL;
58 }
59 strcat(dest, src);
60 return dest;
61}
62
63void tolower_string(char *str)
64{
65 for (; *str != '\0'; str++)
66 *str = (char)tolower((unsigned char)*str);
67}
68
69uint8_t reverse_byte(uint8_t x)
70{
71 x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
72 x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
73 x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
74
75 return x;
76}
77
78void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length)
79{
80 size_t i;
81
82 for (i = 0; i < length; i++)
83 dst[i] = reverse_byte(src[i]);
84}
85
86/* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */
87#ifdef __MINGW32__
88char* strtok_r(char *str, const char *delim, char **nextp)
89{
90 if (str == NULL)
91 str = *nextp;
92
93 str += strspn(str, delim); /* Skip leading delimiters */
94 if (*str == '\0')
95 return NULL;
96
97 char *ret = str;
98 str += strcspn(str, delim); /* Find end of token */
99 if (*str != '\0')
100 *str++ = '\0';
101
102 *nextp = str;
103 return ret;
104}
Edward O'Callaghane4f8af72019-11-26 15:35:37 +1100105
106/* strndup is a POSIX function not present in MinGW */
107char *strndup(const char *src, size_t maxlen)
108{
Xiang Wang20fd7452021-01-20 17:31:19 +0800109 char *retbuf;
110 size_t len;
111 for (len = 0; len < maxlen; len++)
112 if (src[len] == '\0')
113 break;
114 if ((retbuf = malloc(1 + len)) != NULL) {
115 memcpy(retbuf, src, len);
116 retbuf[len] = '\0';
Edward O'Callaghane4f8af72019-11-26 15:35:37 +1100117 }
Xiang Wang20fd7452021-01-20 17:31:19 +0800118 return retbuf;
Edward O'Callaghane4f8af72019-11-26 15:35:37 +1100119}
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100120#endif
121
122/* There is no strnlen in DJGPP */
123#if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN))
124size_t strnlen(const char *str, size_t n)
125{
126 size_t i;
127 for (i = 0; i < n && str[i] != '\0'; i++)
128 ;
129 return i;
130}
131#endif