diff options
author | Marek Vasut <marex@denx.de> | 2016-12-09 02:47:41 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2016-12-09 09:06:45 +0100 |
commit | f5b47ef5112326b06fd875982379769c55a71703 (patch) | |
tree | 4380e15fc33cc96c28c298418b60fce140880fb0 /include | |
parent | abbed09157dc42f11d54277ad895edaf357d92d8 (diff) |
nandwrite: Factor out buffer checking code
Pull the buffer content checking code into separate function and
simplify the code invoking it slightly.
Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'include')
-rw-r--r-- | include/common.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/include/common.h b/include/common.h index 32b5d23..2812f49 100644 --- a/include/common.h +++ b/include/common.h @@ -181,6 +181,31 @@ static inline int is_power_of_2(unsigned long long n) return (n != 0 && ((n & (n - 1)) == 0)); } +/* Check whether buffer is filled with character 'pattern' */ +static inline int buffer_check_pattern(unsigned char *buffer, size_t size, + unsigned char pattern) +{ + /* Invalid input */ + if (!buffer || (size == 0)) + return 0; + + /* No match on first byte */ + if (*buffer != pattern) + return 0; + + /* First byte matched and buffer is 1 byte long, OK. */ + if (size == 1) + return 1; + + /* + * Check buffer longer than 1 byte. We already know that buffer[0] + * matches the pattern, so the test below only checks whether the + * buffer[0...size-2] == buffer[1...size-1] , which is a test for + * whether the buffer is filled with constant value. + */ + return !memcmp(buffer, buffer + 1, size - 1); +} + /** * simple_strtoX - convert a hex/dec/oct string into a number * @snum: buffer to convert |