aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md25
-rw-r--r--configure.ac16
-rw-r--r--include/dictionary.h52
-rw-r--r--include/libiniparser.h218
-rw-r--r--jffsX-utils/jffs2dump.c9
-rw-r--r--jffsX-utils/jffs2reader.c3
-rw-r--r--lib/dictionary.c428
-rw-r--r--lib/libiniparser.c502
-rw-r--r--misc-utils/flash_erase.c2
-rw-r--r--misc-utils/flashcp.c2
-rw-r--r--misc-utils/ftl_check.c4
-rw-r--r--nand-utils/nanddump.c5
-rw-r--r--nand-utils/nandflipbits.c2
-rw-r--r--tests/checkfs/comm.c2
-rw-r--r--ubi-utils/mtdinfo.c6
-rw-r--r--ubi-utils/ubinize.c4
-rw-r--r--ubi-utils/ubirsvol.c2
-rw-r--r--ubifs-utils/Makemodule.am4
-rw-r--r--ubifs-utils/common/README17
-rw-r--r--ubifs-utils/libubifs/README2
-rw-r--r--ubifs-utils/libubifs/find.c40
-rw-r--r--ubifs-utils/libubifs/lpt.c1
-rw-r--r--ubifs-utils/libubifs/lpt_commit.c4
-rw-r--r--ubifs-utils/libubifs/replay.c1
-rw-r--r--ubifs-utils/libubifs/tnc_commit.c2
25 files changed, 851 insertions, 502 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dfad584..204ee0f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,31 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
+## [2.3.0] - 2025-02-15
+### Added
+ - fsck.ubifs: Add fsck utility for ubifs
+ - ubifs-utils: Support Address-Sanitizier debug
+ - nand-utils: nanddump: Add support for testing continuous reads
+ - mtd-tests: nandbiterrs: Add support for testing continuous reads
+ - mtd-tests: flash_speed: Benchmark continuous reads
+
+### Fixed
+ - Various integer handling errors (potential overflows, divide by zero)
+ - mkfs.jffs2: fix segfault when parsing dev table
+ - nand-utils: nanddump: Explicitely use the page size when relevant
+ - misc-utils: add missing error handling for 'bam' allocation in ftl_check.c
+ - mtdinfo: type mis-match in printf format string
+ - nanddump: const cast warning
+ - flashcp: uninitialized variable
+ - jffs2reader: potential null pointer dereference
+ - tests: checkfs: Add previous prototype for do_pwr_dn()
+
+### Changed
+ - mkfs.ubifs: re-sync with the kernel code
+ - mkfs.ubifs: move most of the code into a libubifs library
+ - Import a more recent version of libiniparser
+ - mtd-tests: flash_speed: cleanup/refactor
+
## [2.2.1] - 2024-09-25
### Fixed
- fectest: Fix time formatting with _TIME_BITS=64 on 32-bit system
diff --git a/configure.ac b/configure.ac
index 7b5d609..2a79ba8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,6 +1,6 @@
AC_PREREQ([2.60])
-m4_define([RELEASE], 2.2.1)
+m4_define([RELEASE], 2.3.0)
AC_INIT([mtd-utils], [RELEASE], [linux-mtd@lists.infradead.org], mtd-utils)
@@ -105,6 +105,10 @@ AC_ARG_WITH([crypto],
[AS_HELP_STRING([--with-crypto], [Support for UBIFS crypto features])],
[], [with_crypto="check"])
+AC_ARG_ENABLE([asan],
+ [AS_HELP_STRING([--enable-asan], [Support AddressSanitizer debug])],
+ [], [enable_asan="no"])
+
##### search for dependencies #####
need_clock_gettime="no"
@@ -146,6 +150,14 @@ AS_IF([test "x$with_zlib" != "xno"], [
[with_zlib="no"])])
], [])
+AC_ARG_VAR([ASAN_LIBS], [linker flags for lasan])
+AS_IF([test "x$enable_asan" = "xyes"], [
+ AC_CHECK_LIB([asan], [_init], [
+ ASAN_LIBS="-lasan -fsanitize=address -fsanitize-recover=address -g"], [
+ AC_MSG_ERROR([cannot find libasan])
+ ])
+])
+
if test "x$need_uuid" = "xyes"; then
PKG_CHECK_MODULES(UUID, [uuid], [],
[PKG_CHECK_MODULES_STATIC(UUID, [uuid], [], [uuid_missing="yes"])])
@@ -275,6 +287,7 @@ AM_CONDITIONAL([WITH_XATTR], [test "x$with_xattr" = "xyes"])
AM_CONDITIONAL([WITH_SELINUX], [test "x$with_selinux" = "xyes"])
AM_CONDITIONAL([WITH_CRYPTO], [test "x$with_crypto" = "xyes"])
AM_CONDITIONAL([WITH_UBIHEALTHD], [test "x$enable_ubihealthd" = "xyes"])
+AM_CONDITIONAL([WITH_ASAN], [test "x$enable_asan" = "xyes"])
AM_CONDITIONAL([BUILD_UBIFS], [test "x$with_ubifs" = "xyes"])
AM_CONDITIONAL([BUILD_JFFSX], [test "x$with_jffs" = "xyes"])
@@ -321,6 +334,7 @@ AC_MSG_RESULT([
compiler: ${CC}
cflags: ${CFLAGS}
ldflags: ${LDFLAGS}
+ asan debug: ${enable_asan}
lzo support: ${with_lzo}
zlib support: ${with_zlib}
diff --git a/include/dictionary.h b/include/dictionary.h
index c7d1790..f459cfe 100644
--- a/include/dictionary.h
+++ b/include/dictionary.h
@@ -3,8 +3,6 @@
/**
@file dictionary.h
@author N. Devillard
- @date Sep 2007
- @version $Revision: 1.12 $
@brief Implements a dictionary for string variables.
This module implements a simple dictionary object, i.e. a list
@@ -13,33 +11,27 @@
*/
/*--------------------------------------------------------------------------*/
-/*
- $Id: dictionary.h,v 1.12 2007-11-23 21:37:00 ndevilla Exp $
- $Author: ndevilla $
- $Date: 2007-11-23 21:37:00 $
- $Revision: 1.12 $
-*/
-
#ifndef _DICTIONARY_H_
#define _DICTIONARY_H_
/*---------------------------------------------------------------------------
- Includes
+ Includes
---------------------------------------------------------------------------*/
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
/*---------------------------------------------------------------------------
- New types
+ New types
---------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/**
- @brief Dictionary object
+ @brief Dictionary object
This object contains a list of string/string associations. Each
association is identified by a unique string key. Looking up values
@@ -48,16 +40,16 @@
*/
/*-------------------------------------------------------------------------*/
typedef struct _dictionary_ {
- int n ; /** Number of entries in dictionary */
- int size ; /** Storage size */
- char ** val ; /** List of string values */
- char ** key ; /** List of string keys */
- unsigned * hash ; /** List of hash values for keys */
+ unsigned n ; /** Number of entries in dictionary */
+ size_t size ; /** Storage size */
+ char ** val ; /** List of string values */
+ char ** key ; /** List of string keys */
+ unsigned * hash ; /** List of hash values for keys */
} dictionary ;
/*---------------------------------------------------------------------------
- Function prototypes
+ Function prototypes
---------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
@@ -72,20 +64,20 @@ typedef struct _dictionary_ {
by comparing the key itself in last resort.
*/
/*--------------------------------------------------------------------------*/
-unsigned dictionary_hash(char * key);
+unsigned dictionary_hash(const char * key);
/*-------------------------------------------------------------------------*/
/**
@brief Create a new dictionary object.
@param size Optional initial size of the dictionary.
- @return 1 newly allocated dictionary objet.
+ @return 1 newly allocated dictionary object.
This function allocates a new dictionary object of given size and returns
it. If you do not know in advance (roughly) the number of entries in the
dictionary, give size=0.
*/
/*--------------------------------------------------------------------------*/
-dictionary * dictionary_new(int size);
+dictionary * dictionary_new(size_t size);
/*-------------------------------------------------------------------------*/
/**
@@ -112,7 +104,7 @@ void dictionary_del(dictionary * vd);
dictionary object, you should not try to free it or modify it.
*/
/*--------------------------------------------------------------------------*/
-char * dictionary_get(dictionary * d, char * key, char * def);
+const char * dictionary_get(const dictionary * d, const char * key, const char * def);
/*-------------------------------------------------------------------------*/
@@ -141,7 +133,7 @@ char * dictionary_get(dictionary * d, char * key, char * def);
This function returns non-zero in case of failure.
*/
/*--------------------------------------------------------------------------*/
-int dictionary_set(dictionary * vd, char * key, char * val);
+int dictionary_set(dictionary * vd, const char * key, const char * val);
/*-------------------------------------------------------------------------*/
/**
@@ -154,7 +146,7 @@ int dictionary_set(dictionary * vd, char * key, char * val);
key cannot be found.
*/
/*--------------------------------------------------------------------------*/
-void dictionary_unset(dictionary * d, char * key);
+void dictionary_unset(dictionary * d, const char * key);
/*-------------------------------------------------------------------------*/
@@ -169,6 +161,10 @@ void dictionary_unset(dictionary * d, char * key);
output file pointers.
*/
/*--------------------------------------------------------------------------*/
-void dictionary_dump(dictionary * d, FILE * out);
+void dictionary_dump(const dictionary * d, FILE * out);
+
+#ifdef __cplusplus
+}
+#endif
#endif
diff --git a/include/libiniparser.h b/include/libiniparser.h
index abd77aa..2fe0a3d 100644
--- a/include/libiniparser.h
+++ b/include/libiniparser.h
@@ -3,43 +3,23 @@
/**
@file iniparser.h
@author N. Devillard
- @date Sep 2007
- @version 3.0
@brief Parser for ini files.
*/
/*--------------------------------------------------------------------------*/
-/*
- $Id: iniparser.h,v 1.24 2007-11-23 21:38:19 ndevilla Exp $
- $Revision: 1.24 $
-*/
-
#ifndef _INIPARSER_H_
#define _INIPARSER_H_
/*---------------------------------------------------------------------------
- Includes
+ Includes
---------------------------------------------------------------------------*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-/*
- * The following #include is necessary on many Unixes but not Linux.
- * It is not needed for Windows platforms.
- * Uncomment it if needed.
- */
-/* #include <unistd.h> */
-
#include "dictionary.h"
+#include <stdint.h>
-/*---------------------------------------------------------------------------
- Macros
- ---------------------------------------------------------------------------*/
-/** For backwards compatibility only */
-#define iniparser_getstr(d, k) iniparser_getstring(d, k, NULL)
-#define iniparser_setstr iniparser_setstring
+#ifdef __cplusplus
+extern "C" {
+#endif
/*-------------------------------------------------------------------------*/
/**
@@ -60,7 +40,7 @@
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getnsec(dictionary * d);
+int iniparser_getnsec(const dictionary * d);
/*-------------------------------------------------------------------------*/
@@ -78,7 +58,7 @@ int iniparser_getnsec(dictionary * d);
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getsecname(dictionary * d, int n);
+const char * iniparser_getsecname(const dictionary * d, int n);
/*-------------------------------------------------------------------------*/
@@ -86,21 +66,39 @@ char * iniparser_getsecname(dictionary * d, int n);
@brief Save a dictionary to a loadable ini file
@param d Dictionary to dump
@param f Opened file pointer to dump to
- @return void
This function dumps a given dictionary into a loadable ini file.
It is Ok to specify @c stderr or @c stdout as output files.
+
+ All values are quoted, these charecters are escaped:
+
+ - ' : the quote character (e.g. "String with \"Quotes\"")
+ - \ : the backslash character (e.g. "C:\\tmp")
+
*/
/*--------------------------------------------------------------------------*/
-void iniparser_dump_ini(dictionary * d, FILE * f);
+void iniparser_dump_ini(const dictionary * d, FILE * f);
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Save a dictionary section to a loadable ini file
+ @param d Dictionary to dump
+ @param s Section name of dictionary to dump
+ @param f Opened file pointer to dump to
+
+ This function dumps a given section of a given dictionary into a loadable ini
+ file. It is Ok to specify @c stderr or @c stdout as output files.
+ */
+/*--------------------------------------------------------------------------*/
+
+void iniparser_dumpsection_ini(const dictionary * d, const char * s, FILE * f);
/*-------------------------------------------------------------------------*/
/**
@brief Dump a dictionary to an opened file pointer.
@param d Dictionary to dump.
@param f Opened file pointer to dump to.
- @return void
This function prints out the contents of a dictionary, one element by
line, onto the provided file pointer. It is OK to specify @c stderr
@@ -108,7 +106,7 @@ void iniparser_dump_ini(dictionary * d, FILE * f);
purposes mostly.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_dump(dictionary * d, FILE * f);
+void iniparser_dump(const dictionary * d, FILE * f);
/*-------------------------------------------------------------------------*/
/**
@@ -125,7 +123,7 @@ void iniparser_dump(dictionary * d, FILE * f);
the dictionary, do not free or modify it.
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getstring(dictionary * d, const char * key, char * def);
+const char * iniparser_getstring(const dictionary * d, const char * key, const char * def);
/*-------------------------------------------------------------------------*/
/**
@@ -154,7 +152,94 @@ char * iniparser_getstring(dictionary * d, const char * key, char * def);
Credits: Thanks to A. Becker for suggesting strtol()
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getint(dictionary * d, const char * key, int notfound);
+int iniparser_getint(const dictionary * d, const char * key, int notfound);
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to an long int
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return integer
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+
+ Supported values for integers include the usual C notation
+ so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
+ are supported. Examples:
+
+ - "42" -> 42
+ - "042" -> 34 (octal -> decimal)
+ - "0x42" -> 66 (hexa -> decimal)
+
+ Warning: the conversion may overflow in various ways. Conversion is
+ totally outsourced to strtol(), see the associated man page for overflow
+ handling.
+ */
+/*--------------------------------------------------------------------------*/
+long int iniparser_getlongint(const dictionary * d, const char * key, long int notfound);
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to an int64_t
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return integer
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+
+ Supported values for integers include the usual C notation
+ so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
+ are supported. Examples:
+
+ - "42" -> 42
+ - "042" -> 34 (octal -> decimal)
+ - "0x42" -> 66 (hexa -> decimal)
+
+ Warning: the conversion may overflow in various ways. Conversion is
+ totally outsourced to strtoimax(), see the associated man page for overflow
+ handling.
+
+ This function is usefull on 32bit architectures where `long int` is only
+ 32bit.
+ */
+/*--------------------------------------------------------------------------*/
+int64_t iniparser_getint64(const dictionary * d, const char * key, int64_t notfound);
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to an uint64_t
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return integer
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+
+ Supported values for integers include the usual C notation
+ so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
+ are supported. Examples:
+
+ - "42" -> 42
+ - "042" -> 34 (octal -> decimal)
+ - "0x42" -> 66 (hexa -> decimal)
+
+ Warning: the conversion may overflow in various ways. Conversion is
+ totally outsourced to strtoumax(), see the associated man page for overflow
+ handling.
+
+ This function is usefull on 32bit architectures where `long int` is only
+ 32bit.
+ */
+/*--------------------------------------------------------------------------*/
+uint64_t iniparser_getuint64(const dictionary * d, const char * key, uint64_t notfound);
/*-------------------------------------------------------------------------*/
/**
@@ -188,36 +273,18 @@ int iniparser_getint(dictionary * d, const char * key, int notfound);
necessarily have to be 0 or 1.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getboolean(dictionary * d, const char * key, int notfound);
-
-
-/*-------------------------------------------------------------------------*/
-/**
- @brief Set an entry in a dictionary.
- @param ini Dictionary to modify.
- @param entry Entry to modify (entry name)
- @param val New value to associate to the entry.
- @return int 0 if Ok, -1 otherwise.
-
- If the given entry can be found in the dictionary, it is modified to
- contain the provided value. If it cannot be found, -1 is returned.
- It is Ok to set val to NULL.
- */
-/*--------------------------------------------------------------------------*/
-int iniparser_setstring(dictionary * ini, char * entry, char * val);
-
+int iniparser_getboolean(const dictionary * d, const char * key, int notfound);
/*-------------------------------------------------------------------------*/
/**
@brief Delete an entry in a dictionary
@param ini Dictionary to modify
@param entry Entry to delete (entry name)
- @return void
If the given entry can be found, it is deleted from the dictionary.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_unset(dictionary * ini, char * entry);
+void iniparser_unset(dictionary * ini, const char * entry);
/*-------------------------------------------------------------------------*/
/**
@@ -231,7 +298,7 @@ void iniparser_unset(dictionary * ini, char * entry);
of querying for the presence of sections in a dictionary.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_find_entry(dictionary * ini, char * entry) ;
+int iniparser_find_entry(const dictionary * ini, const char * entry) ;
/*-------------------------------------------------------------------------*/
/**
@@ -244,6 +311,17 @@ int iniparser_find_entry(dictionary * ini, char * entry) ;
should not be accessed directly, but through accessor functions
instead.
+ Iff the value is a quoted string it supports some escape sequences:
+
+ - \" or ' : the quote character
+ (e.g. 'String with "Quotes"' or "String with 'Quotes'")
+ - \ : the backslash character (e.g. "C:\tmp")
+
+ Escape sequences always start with a backslash. Additional escape sequences
+ might be added in the future. Backslash characters must be escaped. Any other
+ sequence then those outlined above is invalid and may lead to unpredictable
+ results.
+
The returned dictionary must be freed using iniparser_freedict().
*/
/*--------------------------------------------------------------------------*/
@@ -251,9 +329,35 @@ dictionary * iniparser_load(const char * ininame);
/*-------------------------------------------------------------------------*/
/**
+ @brief Parse an ini file and return an allocated dictionary object
+ @param in File to read.
+ @param ininame Name of the ini file to read (only used for nicer error messages)
+ @return Pointer to newly allocated dictionary
+
+ This is the parser for ini files. This function is called, providing
+ the file to be read. It returns a dictionary object that should not
+ be accessed directly, but through accessor functions instead.
+
+ Iff the value is a quoted string it supports some escape sequences:
+
+ - \" or ' : the quote character
+ (e.g. 'String with "Quotes"' or "String with 'Quotes'")
+ - \ : the backslash character (e.g. "C:\tmp")
+
+ Escape sequences always start with a backslash. Additional escape sequences
+ might be added in the future. Backslash characters must be escaped. Any other
+ sequence then those outlined above is invalid and may lead to unpredictable
+ results.
+
+ The returned dictionary must be freed using iniparser_freedict().
+ */
+/*--------------------------------------------------------------------------*/
+dictionary * iniparser_load_file(FILE * in, const char * ininame);
+
+/*-------------------------------------------------------------------------*/
+/**
@brief Free all memory associated to an ini dictionary
@param d Dictionary to free
- @return void
Free all memory associated to an ini dictionary.
It is mandatory to call this function before the dictionary object
@@ -262,4 +366,8 @@ dictionary * iniparser_load(const char * ininame);
/*--------------------------------------------------------------------------*/
void iniparser_freedict(dictionary * d);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/jffsX-utils/jffs2dump.c b/jffsX-utils/jffs2dump.c
index 30455ea..b757ebe 100644
--- a/jffsX-utils/jffs2dump.c
+++ b/jffsX-utils/jffs2dump.c
@@ -772,6 +772,13 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE);
}
+ if (datsize < 0 || oobsize < 0 || datsize > imglen || (long)datsize + oobsize < 0) {
+ fprintf(stderr, "Error: invalid datsize/oobsize.\n");
+ free(data);
+ close (fd);
+ exit(EXIT_FAILURE);
+ }
+
if (datsize && oobsize) {
int idx = 0;
long len = imglen;
@@ -783,7 +790,7 @@ int main(int argc, char **argv)
read_nocheck (fd, oob, oobsize);
idx += datsize;
imglen -= oobsize;
- len -= datsize + oobsize;
+ len -= (long)datsize + oobsize;
}
} else {
diff --git a/jffsX-utils/jffs2reader.c b/jffsX-utils/jffs2reader.c
index 87a2167..548fc8d 100644
--- a/jffsX-utils/jffs2reader.c
+++ b/jffsX-utils/jffs2reader.c
@@ -694,6 +694,9 @@ static struct jffs2_raw_dirent *resolvepath0(char *o, size_t size, uint32_t ino,
pp = path = xstrdup(p);
+ if (path == NULL)
+ return NULL;
+
if (*path == '/') {
path++;
ino = 1;
diff --git a/lib/dictionary.c b/lib/dictionary.c
index f4b7468..85dfdc0 100644
--- a/lib/dictionary.c
+++ b/lib/dictionary.c
@@ -1,10 +1,8 @@
/*-------------------------------------------------------------------------*/
/**
- @file dictionary.c
- @author N. Devillard
- @date Sep 2007
- @version $Revision: 1.27 $
- @brief Implements a dictionary for string variables.
+ @file dictionary.c
+ @author N. Devillard
+ @brief Implements a dictionary for string variables.
This module implements a simple dictionary object, i.e. a list
of string/string associations. This object is useful to store e.g.
@@ -12,48 +10,22 @@
*/
/*--------------------------------------------------------------------------*/
-/*
- $Id: dictionary.c,v 1.27 2007-11-23 21:39:18 ndevilla Exp $
- $Revision: 1.27 $
-*/
/*---------------------------------------------------------------------------
- Includes
+ Includes
---------------------------------------------------------------------------*/
#include "dictionary.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
-
-/** Maximum value size for integers and doubles. */
-#define MAXVALSZ 1024
/** Minimal allocated number of entries in a dictionary */
-#define DICTMINSZ 128
-
-/** Invalid key token */
-#define DICT_INVALID_KEY ((char*)-1)
+#define DICTMINSZ 128
/*---------------------------------------------------------------------------
- Private functions
+ Private functions
---------------------------------------------------------------------------*/
-/* Doubles the allocated size associated to a pointer */
-/* 'size' is the current allocated size. */
-static void * mem_double(void * ptr, int size)
-{
- void * newptr ;
-
- newptr = calloc(2*size, 1);
- if (newptr==NULL) {
- return NULL ;
- }
- memcpy(newptr, ptr, size);
- free(ptr);
- return newptr ;
-}
-
/*-------------------------------------------------------------------------*/
/**
@brief Duplicate a string
@@ -64,26 +36,71 @@ static void * mem_double(void * ptr, int size)
for systems that do not have it.
*/
/*--------------------------------------------------------------------------*/
-static char * xstrdup(char * s)
+static char * xstrdup(const char * s)
{
char * t ;
+ size_t len ;
if (!s)
return NULL ;
- t = malloc(strlen(s)+1) ;
+
+ len = strlen(s) + 1 ;
+ t = (char*) malloc(len) ;
if (t) {
- strcpy(t,s);
+ memcpy(t, s, len) ;
}
return t ;
}
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Double the size of the dictionary
+ @param d Dictionary to grow
+ @return This function returns non-zero in case of failure
+ */
+/*--------------------------------------------------------------------------*/
+static int dictionary_grow(dictionary * d)
+{
+ char ** new_val ;
+ char ** new_key ;
+ unsigned * new_hash ;
+
+ new_val = (char**) calloc(d->size * 2, sizeof *d->val);
+ new_key = (char**) calloc(d->size * 2, sizeof *d->key);
+ new_hash = (unsigned*) calloc(d->size * 2, sizeof *d->hash);
+ if (!new_val || !new_key || !new_hash) {
+ /* An allocation failed, leave the dictionary unchanged */
+ if (new_val)
+ free(new_val);
+ if (new_key)
+ free(new_key);
+ if (new_hash)
+ free(new_hash);
+ return -1 ;
+ }
+ /* Initialize the newly allocated space */
+ memcpy(new_val, d->val, d->size * sizeof(char *));
+ memcpy(new_key, d->key, d->size * sizeof(char *));
+ memcpy(new_hash, d->hash, d->size * sizeof(unsigned));
+ /* Delete previous data */
+ free(d->val);
+ free(d->key);
+ free(d->hash);
+ /* Actually update the dictionary */
+ d->size *= 2 ;
+ d->val = new_val;
+ d->key = new_key;
+ d->hash = new_hash;
+ return 0 ;
+}
+
/*---------------------------------------------------------------------------
- Function codes
+ Function codes
---------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/**
- @brief Compute the hash key for a string.
- @param key Character string to use for key.
- @return 1 unsigned int on at least 32 bits.
+ @brief Compute the hash key for a string.
+ @param key Character string to use for key.
+ @return 1 unsigned int on at least 32 bits.
This hash function has been taken from an Article in Dr Dobbs Journal.
This is normally a collision-free function, distributing keys evenly.
@@ -91,86 +108,97 @@ static char * xstrdup(char * s)
by comparing the key itself in last resort.
*/
/*--------------------------------------------------------------------------*/
-unsigned dictionary_hash(char * key)
+unsigned dictionary_hash(const char * key)
{
- int len ;
- unsigned hash ;
- int i ;
-
- len = strlen(key);
- for (hash=0, i=0 ; i<len ; i++) {
- hash += (unsigned)key[i] ;
- hash += (hash<<10);
- hash ^= (hash>>6) ;
- }
- hash += (hash <<3);
- hash ^= (hash >>11);
- hash += (hash <<15);
- return hash ;
+ size_t len ;
+ unsigned hash ;
+ size_t i ;
+
+ if (!key)
+ return 0 ;
+
+ len = strlen(key);
+ for (hash=0, i=0 ; i<len ; i++) {
+ hash += (unsigned)key[i] ;
+ hash += (hash<<10);
+ hash ^= (hash>>6) ;
+ }
+ hash += (hash <<3);
+ hash ^= (hash >>11);
+ hash += (hash <<15);
+ return hash ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Create a new dictionary object.
- @param size Optional initial size of the dictionary.
- @return 1 newly allocated dictionary objet.
+ @brief Create a new dictionary object.
+ @param size Optional initial size of the dictionary.
+ @return 1 newly allocated dictionary object.
This function allocates a new dictionary object of given size and returns
it. If you do not know in advance (roughly) the number of entries in the
dictionary, give size=0.
*/
-/*--------------------------------------------------------------------------*/
-dictionary * dictionary_new(int size)
+/*-------------------------------------------------------------------------*/
+dictionary * dictionary_new(size_t size)
{
- dictionary * d ;
-
- /* If no size was specified, allocate space for DICTMINSZ */
- if (size<DICTMINSZ) size=DICTMINSZ ;
-
- if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) {
- return NULL;
- }
- d->size = size ;
- d->val = (char **)calloc(size, sizeof(char*));
- d->key = (char **)calloc(size, sizeof(char*));
- d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
- return d ;
+ dictionary * d ;
+
+ /* If no size was specified, allocate space for DICTMINSZ */
+ if (size<DICTMINSZ) size=DICTMINSZ ;
+
+ d = (dictionary*) calloc(1, sizeof *d) ;
+
+ if (d) {
+ d->size = size ;
+ d->val = (char**) calloc(size, sizeof *d->val);
+ d->key = (char**) calloc(size, sizeof *d->key);
+ d->hash = (unsigned*) calloc(size, sizeof *d->hash);
+ if (!d->size || !d->val || !d->hash) {
+ free((void *) d->size);
+ free((void *) d->val);
+ free((void *) d->hash);
+ free(d);
+ d = NULL;
+ }
+ }
+ return d ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Delete a dictionary object
- @param d dictionary object to deallocate.
- @return void
+ @brief Delete a dictionary object
+ @param d dictionary object to deallocate.
+ @return void
Deallocate a dictionary object and all memory associated to it.
*/
/*--------------------------------------------------------------------------*/
void dictionary_del(dictionary * d)
{
- int i ;
-
- if (d==NULL) return ;
- for (i=0 ; i<d->size ; i++) {
- if (d->key[i]!=NULL)
- free(d->key[i]);
- if (d->val[i]!=NULL)
- free(d->val[i]);
- }
- free(d->val);
- free(d->key);
- free(d->hash);
- free(d);
- return ;
+ size_t i ;
+
+ if (d==NULL) return ;
+ for (i=0 ; i<d->size ; i++) {
+ if (d->key[i]!=NULL)
+ free(d->key[i]);
+ if (d->val[i]!=NULL)
+ free(d->val[i]);
+ }
+ free(d->val);
+ free(d->key);
+ free(d->hash);
+ free(d);
+ return ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Get a value from a dictionary.
- @param d dictionary object to search.
- @param key Key to look for in the dictionary.
+ @brief Get a value from a dictionary.
+ @param d dictionary object to search.
+ @param key Key to look for in the dictionary.
@param def Default value to return if key not found.
- @return 1 pointer to internally allocated character string.
+ @return 1 pointer to internally allocated character string.
This function locates a key in a dictionary and returns a pointer to its
value, or the passed 'def' pointer if no such key can be found in
@@ -178,24 +206,27 @@ void dictionary_del(dictionary * d)
dictionary object, you should not try to free it or modify it.
*/
/*--------------------------------------------------------------------------*/
-char * dictionary_get(dictionary * d, char * key, char * def)
+const char * dictionary_get(const dictionary * d, const char * key, const char * def)
{
- unsigned hash ;
- int i ;
+ unsigned hash ;
+ size_t i ;
- hash = dictionary_hash(key);
- for (i=0 ; i<d->size ; i++) {
+ if(d == NULL || key == NULL)
+ return def ;
+
+ hash = dictionary_hash(key);
+ for (i=0 ; i<d->size ; i++) {
if (d->key[i]==NULL)
continue ;
/* Compare hash */
- if (hash==d->hash[i]) {
+ if (hash==d->hash[i]) {
/* Compare string, to avoid hash collisions */
if (!strcmp(key, d->key[i])) {
- return d->val[i] ;
- }
- }
- }
- return def ;
+ return d->val[i] ;
+ }
+ }
+ }
+ return def ;
}
/*-------------------------------------------------------------------------*/
@@ -224,96 +255,87 @@ char * dictionary_get(dictionary * d, char * key, char * def)
This function returns non-zero in case of failure.
*/
/*--------------------------------------------------------------------------*/
-int dictionary_set(dictionary * d, char * key, char * val)
+int dictionary_set(dictionary * d, const char * key, const char * val)
{
- int i ;
- unsigned hash ;
+ size_t i ;
+ unsigned hash ;
- if (d==NULL || key==NULL) return -1 ;
+ if (d==NULL || key==NULL) return -1 ;
- /* Compute hash for this key */
- hash = dictionary_hash(key) ;
- /* Find if value is already in dictionary */
- if (d->n>0) {
- for (i=0 ; i<d->size ; i++) {
+ /* Compute hash for this key */
+ hash = dictionary_hash(key) ;
+ /* Find if value is already in dictionary */
+ if (d->n>0) {
+ for (i=0 ; i<d->size ; i++) {
if (d->key[i]==NULL)
continue ;
- if (hash==d->hash[i]) { /* Same hash value */
- if (!strcmp(key, d->key[i])) { /* Same key */
- /* Found a value: modify and return */
- if (d->val[i]!=NULL)
- free(d->val[i]);
- d->val[i] = val ? xstrdup(val) : NULL ;
+ if (hash==d->hash[i]) { /* Same hash value */
+ if (!strcmp(key, d->key[i])) { /* Same key */
+ /* Found a value: modify and return */
+ if (d->val[i]!=NULL)
+ free(d->val[i]);
+ d->val[i] = (val ? xstrdup(val) : NULL);
/* Value has been modified: return */
- return 0 ;
- }
- }
- }
- }
- /* Add a new value */
- /* See if dictionary needs to grow */
- if (d->n==d->size) {
-
- /* Reached maximum size: reallocate dictionary */
- d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ;
- d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ;
- d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
- if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) {
- /* Cannot grow dictionary */
- return -1 ;
+ return 0 ;
+ }
+ }
}
- /* Double size */
- d->size *= 2 ;
- }
+ }
+ /* Add a new value */
+ /* See if dictionary needs to grow */
+ if (d->n==d->size) {
+ /* Reached maximum size: reallocate dictionary */
+ if (dictionary_grow(d) != 0)
+ return -1;
+ }
- /* Insert key in the first empty slot */
- for (i=0 ; i<d->size ; i++) {
- if (d->key[i]==NULL) {
- /* Add key here */
- break ;
- }
+ /* Insert key in the first empty slot. Start at d->n and wrap at
+ d->size. Because d->n < d->size this will necessarily
+ terminate. */
+ for (i=d->n ; d->key[i] ; ) {
+ if(++i == d->size) i = 0;
}
- /* Copy key */
- d->key[i] = xstrdup(key);
- d->val[i] = val ? xstrdup(val) : NULL ;
- d->hash[i] = hash;
- d->n ++ ;
- return 0 ;
+ /* Copy key */
+ d->key[i] = xstrdup(key);
+ d->val[i] = (val ? xstrdup(val) : NULL) ;
+ d->hash[i] = hash;
+ d->n ++ ;
+ return 0 ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Delete a key in a dictionary
- @param d dictionary object to modify.
- @param key Key to remove.
+ @brief Delete a key in a dictionary
+ @param d dictionary object to modify.
+ @param key Key to remove.
@return void
This function deletes a key in a dictionary. Nothing is done if the
key cannot be found.
*/
/*--------------------------------------------------------------------------*/
-void dictionary_unset(dictionary * d, char * key)
+void dictionary_unset(dictionary * d, const char * key)
{
- unsigned hash ;
- int i ;
+ unsigned hash ;
+ size_t i ;
- if (key == NULL) {
- return;
- }
+ if (key == NULL || d == NULL) {
+ return;
+ }
- hash = dictionary_hash(key);
- for (i=0 ; i<d->size ; i++) {
+ hash = dictionary_hash(key);
+ for (i=0 ; i<d->size ; i++) {
if (d->key[i]==NULL)
continue ;
/* Compare hash */
- if (hash==d->hash[i]) {
+ if (hash==d->hash[i]) {
/* Compare string, to avoid hash collisions */
if (!strcmp(key, d->key[i])) {
/* Found key */
break ;
- }
- }
- }
+ }
+ }
+ }
if (i>=d->size)
/* Key not found */
return ;
@@ -331,75 +353,31 @@ void dictionary_unset(dictionary * d, char * key)
/*-------------------------------------------------------------------------*/
/**
- @brief Dump a dictionary to an opened file pointer.
- @param d Dictionary to dump
- @param f Opened file pointer.
- @return void
+ @brief Dump a dictionary to an opened file pointer.
+ @param d Dictionary to dump
+ @param f Opened file pointer.
+ @return void
Dumps a dictionary onto an opened file pointer. Key pairs are printed out
as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
output file pointers.
*/
/*--------------------------------------------------------------------------*/
-void dictionary_dump(dictionary * d, FILE * out)
+void dictionary_dump(const dictionary * d, FILE * out)
{
- int i ;
-
- if (d==NULL || out==NULL) return ;
- if (d->n<1) {
- fprintf(out, "empty dictionary\n");
- return ;
- }
- for (i=0 ; i<d->size ; i++) {
+ size_t i ;
+
+ if (d==NULL || out==NULL) return ;
+ if (d->n<1) {
+ fprintf(out, "empty dictionary\n");
+ return ;
+ }
+ for (i=0 ; i<d->size ; i++) {
if (d->key[i]) {
fprintf(out, "%20s\t[%s]\n",
d->key[i],
d->val[i] ? d->val[i] : "UNDEF");
}
- }
- return ;
-}
-
-
-/* Test code */
-#ifdef TESTDIC
-#define NVALS 20000
-int main(int argc, char *argv[])
-{
- dictionary * d ;
- char * val ;
- int i ;
- char cval[90] ;
-
- /* Allocate dictionary */
- printf("allocating...\n");
- d = dictionary_new(0);
-
- /* Set values in dictionary */
- printf("setting %d values...\n", NVALS);
- for (i=0 ; i<NVALS ; i++) {
- sprintf(cval, "%04d", i);
- dictionary_set(d, cval, "salut");
- }
- printf("getting %d values...\n", NVALS);
- for (i=0 ; i<NVALS ; i++) {
- sprintf(cval, "%04d", i);
- val = dictionary_get(d, cval, DICT_INVALID_KEY);
- if (val==DICT_INVALID_KEY) {
- printf("cannot get value for key [%s]\n", cval);
- }
- }
- printf("unsetting %d values...\n", NVALS);
- for (i=0 ; i<NVALS ; i++) {
- sprintf(cval, "%04d", i);
- dictionary_unset(d, cval);
- }
- if (d->n != 0) {
- printf("error deleting values\n");
}
- printf("deallocating...\n");
- dictionary_del(d);
- return 0 ;
+ return ;
}
-#endif
-/* vim: set ts=4 et sw=4 tw=75 */
diff --git a/lib/libiniparser.c b/lib/libiniparser.c
index a6ddcc7..4b21b34 100644
--- a/lib/libiniparser.c
+++ b/lib/libiniparser.c
@@ -3,19 +3,16 @@
/**
@file iniparser.c
@author N. Devillard
- @date Sep 2007
- @version 3.0
@brief Parser for ini files.
*/
/*--------------------------------------------------------------------------*/
-/*
- $Id: iniparser.c,v 2.18 2008-01-03 18:35:39 ndevilla Exp $
- $Revision: 2.18 $
- $Date: 2008-01-03 18:35:39 $
-*/
/*---------------------------- Includes ------------------------------------*/
#include <ctype.h>
-#include <libiniparser.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include "libiniparser.h"
/*---------------------------- Defines -------------------------------------*/
#define ASCIILINESZ (1024)
@@ -38,68 +35,101 @@ typedef enum _line_status_ {
/*-------------------------------------------------------------------------*/
/**
- @brief Convert a string to lowercase.
- @param s String to convert.
- @return ptr to statically allocated string.
-
- This function returns a pointer to a statically allocated string
- containing a lowercased version of the input string. Do not free
- or modify the returned string! Since the returned string is statically
- allocated, it will be modified at each function call (not re-entrant).
+ @brief Convert a string to lowercase.
+ @param in String to convert.
+ @param out Output buffer.
+ @param len Size of the out buffer.
+ @return ptr to the out buffer or NULL if an error occured.
+
+ This function convert a string into lowercase.
+ At most len - 1 elements of the input string will be converted.
*/
/*--------------------------------------------------------------------------*/
-static char * strlwc(const char * s)
+static const char * strlwc(const char * in, char *out, unsigned len)
{
- static char l[ASCIILINESZ+1];
- int i ;
+ unsigned i ;
- if (s==NULL) return NULL ;
- memset(l, 0, ASCIILINESZ+1);
+ if (in==NULL || out == NULL || len==0) return NULL ;
i=0 ;
- while (s[i] && i<ASCIILINESZ) {
- l[i] = (char)tolower((int)s[i]);
+ while (in[i] != '\0' && i < len-1) {
+ out[i] = (char)tolower((int)in[i]);
i++ ;
}
- l[ASCIILINESZ]=(char)0;
- return l ;
+ out[i] = '\0';
+ return out ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Duplicate a string
+ @param s String to duplicate
+ @return Pointer to a newly allocated string, to be freed with free()
+
+ This is a replacement for strdup(). This implementation is provided
+ for systems that do not have it.
+ */
+/*--------------------------------------------------------------------------*/
+static char * xstrdup(const char * s)
+{
+ char * t ;
+ size_t len ;
+ if (!s)
+ return NULL ;
+
+ len = strlen(s) + 1 ;
+ t = (char*) malloc(len) ;
+ if (t) {
+ memcpy(t, s, len) ;
+ }
+ return t ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Remove blanks at the beginning and the end of a string.
- @param s String to parse.
- @return ptr to statically allocated string.
-
- This function returns a pointer to a statically allocated string,
- which is identical to the input string, except that all blank
- characters at the end and the beg. of the string have been removed.
- Do not free or modify the returned string! Since the returned string
- is statically allocated, it will be modified at each function call
- (not re-entrant).
+ @brief Remove blanks at the beginning and the end of a string.
+ @param str String to parse and alter.
+ @return unsigned New size of the string.
*/
/*--------------------------------------------------------------------------*/
-static char * strstrip(char * s)
+static unsigned strstrip(char * s)
{
- static char l[ASCIILINESZ+1];
- char * last ;
-
- if (s==NULL) return NULL ;
-
- while (isspace((int)*s) && *s) s++;
- memset(l, 0, ASCIILINESZ+1);
- strcpy(l, s);
- last = l + strlen(l);
- while (last > l) {
- if (!isspace((int)*(last-1)))
- break ;
- last -- ;
- }
- *last = (char)0;
- return (char*)l ;
+ char *last = NULL ;
+ char *dest = s;
+
+ if (s==NULL) return 0;
+
+ last = s + strlen(s);
+ while (isspace((int)*s) && *s) s++;
+ while (last > s) {
+ if (!isspace((int)*(last-1)))
+ break ;
+ last -- ;
+ }
+ *last = (char)0;
+
+ memmove(dest,s,last - s + 1);
+ return last - s;
}
/*-------------------------------------------------------------------------*/
/**
+ @brief Default error callback for iniparser: wraps `fprintf(stderr, ...)`.
+ */
+/*--------------------------------------------------------------------------*/
+static int default_error_callback(const char *format, ...)
+{
+ int ret;
+ va_list argptr;
+ va_start(argptr, format);
+ ret = vfprintf(stderr, format, argptr);
+ va_end(argptr);
+ return ret;
+}
+
+static int (*iniparser_error_callback)(const char*, ...) = default_error_callback;
+
+/*-------------------------------------------------------------------------*/
+/**
@brief Get number of sections in a dictionary
@param d Dictionary to examine
@return int Number of sections found in dictionary
@@ -116,9 +146,9 @@ static char * strstrip(char * s)
This function returns -1 in case of error.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getnsec(dictionary * d)
+int iniparser_getnsec(const dictionary * d)
{
- int i ;
+ size_t i ;
int nsec ;
if (d==NULL) return -1 ;
@@ -147,9 +177,9 @@ int iniparser_getnsec(dictionary * d)
This function returns NULL in case of error.
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getsecname(dictionary * d, int n)
+const char * iniparser_getsecname(const dictionary * d, int n)
{
- int i ;
+ size_t i ;
int foundsec ;
if (d==NULL || n<0) return NULL ;
@@ -182,9 +212,9 @@ char * iniparser_getsecname(dictionary * d, int n)
purposes mostly.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_dump(dictionary * d, FILE * f)
+void iniparser_dump(const dictionary * d, FILE * f)
{
- int i ;
+ size_t i ;
if (d==NULL || f==NULL) return ;
for (i=0 ; i<d->size ; i++) {
@@ -199,6 +229,26 @@ void iniparser_dump(dictionary * d, FILE * f)
return ;
}
+static void escape_value(char *escaped, char *value) {
+ char c;
+ int v = 0;
+ int e = 0;
+
+ if(!escaped || !value)
+ return;
+
+ while((c = value[v]) != '\0') {
+ if(c == '\\' || c == '"') {
+ escaped[e] = '\\';
+ e++;
+ }
+ escaped[e] = c;
+ v++;
+ e++;
+ }
+ escaped[e] = '\0';
+}
+
/*-------------------------------------------------------------------------*/
/**
@brief Save a dictionary to a loadable ini file
@@ -210,13 +260,12 @@ void iniparser_dump(dictionary * d, FILE * f)
It is Ok to specify @c stderr or @c stdout as output files.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_dump_ini(dictionary * d, FILE * f)
+void iniparser_dump_ini(const dictionary * d, FILE * f)
{
- int i, j ;
- char keym[ASCIILINESZ+1];
- int nsec ;
- char * secname ;
- int seclen ;
+ size_t i ;
+ size_t nsec ;
+ const char * secname ;
+ char escaped[ASCIILINESZ+1] = "";
if (d==NULL || f==NULL) return ;
@@ -226,24 +275,50 @@ void iniparser_dump_ini(dictionary * d, FILE * f)
for (i=0 ; i<d->size ; i++) {
if (d->key[i]==NULL)
continue ;
- fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
+ escape_value(escaped, d->val[i]);
+ fprintf(f, "%s = \"%s\"\n", d->key[i], escaped);
}
return ;
}
for (i=0 ; i<nsec ; i++) {
secname = iniparser_getsecname(d, i) ;
- seclen = (int)strlen(secname);
- fprintf(f, "\n[%s]\n", secname);
- sprintf(keym, "%s:", secname);
- for (j=0 ; j<d->size ; j++) {
- if (d->key[j]==NULL)
- continue ;
- if (!strncmp(d->key[j], keym, seclen+1)) {
- fprintf(f,
- "%-30s = %s\n",
- d->key[j]+seclen+1,
- d->val[j] ? d->val[j] : "");
- }
+ iniparser_dumpsection_ini(d, secname, f);
+ }
+ fprintf(f, "\n");
+ return ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Save a dictionary section to a loadable ini file
+ @param d Dictionary to dump
+ @param s Section name of dictionary to dump
+ @param f Opened file pointer to dump to
+ @return void
+
+ This function dumps a given section of a given dictionary into a loadable ini
+ file. It is Ok to specify @c stderr or @c stdout as output files.
+ */
+/*--------------------------------------------------------------------------*/
+void iniparser_dumpsection_ini(const dictionary * d, const char * s, FILE * f)
+{
+ size_t j ;
+ char keym[ASCIILINESZ+1];
+ int seclen ;
+ char escaped[ASCIILINESZ+1] = "";
+
+ if (d==NULL || f==NULL) return ;
+ if (! iniparser_find_entry(d, s)) return ;
+
+ seclen = (int)strlen(s);
+ fprintf(f, "\n[%s]\n", s);
+ sprintf(keym, "%s:", s);
+ for (j=0 ; j<d->size ; j++) {
+ if (d->key[j]==NULL)
+ continue ;
+ if (!strncmp(d->key[j], keym, seclen+1)) {
+ escape_value(escaped, d->val[j]);
+ fprintf(f, "%-30s = \"%s\"\n", d->key[j]+seclen+1, escaped);
}
}
fprintf(f, "\n");
@@ -265,26 +340,27 @@ void iniparser_dump_ini(dictionary * d, FILE * f)
the dictionary, do not free or modify it.
*/
/*--------------------------------------------------------------------------*/
-char * iniparser_getstring(dictionary * d, const char * key, char * def)
+const char * iniparser_getstring(const dictionary * d, const char * key, const char * def)
{
- char * lc_key ;
- char * sval ;
+ const char * lc_key ;
+ const char * sval ;
+ char tmp_str[ASCIILINESZ+1];
if (d==NULL || key==NULL)
return def ;
- lc_key = strlwc(key);
+ lc_key = strlwc(key, tmp_str, sizeof(tmp_str));
sval = dictionary_get(d, lc_key, def);
return sval ;
}
/*-------------------------------------------------------------------------*/
/**
- @brief Get the string associated to a key, convert to an int
+ @brief Get the string associated to a key, convert to an long int
@param d Dictionary to search
@param key Key string to look for
@param notfound Value to return in case of error
- @return integer
+ @return long integer
This function queries a dictionary for a key. A key as read from an
ini file is given as "section:key". If the key cannot be found,
@@ -305,13 +381,65 @@ char * iniparser_getstring(dictionary * d, const char * key, char * def)
Credits: Thanks to A. Becker for suggesting strtol()
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getint(dictionary * d, const char * key, int notfound)
+long int iniparser_getlongint(const dictionary * d, const char * key, long int notfound)
{
- char * str ;
+ const char * str ;
str = iniparser_getstring(d, key, INI_INVALID_KEY);
- if (str==INI_INVALID_KEY) return notfound ;
- return (int)strtol(str, NULL, 0);
+ if (str==NULL || str==INI_INVALID_KEY) return notfound ;
+ return strtol(str, NULL, 0);
+}
+
+int64_t iniparser_getint64(const dictionary * d, const char * key, int64_t notfound)
+{
+ const char * str ;
+
+ str = iniparser_getstring(d, key, INI_INVALID_KEY);
+ if (str==NULL || str==INI_INVALID_KEY) return notfound ;
+ return strtoimax(str, NULL, 0);
+}
+
+uint64_t iniparser_getuint64(const dictionary * d, const char * key, uint64_t notfound)
+{
+ const char * str ;
+
+ str = iniparser_getstring(d, key, INI_INVALID_KEY);
+ if (str==NULL || str==INI_INVALID_KEY) return notfound ;
+ return strtoumax(str, NULL, 0);
+}
+
+
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Get the string associated to a key, convert to an int
+ @param d Dictionary to search
+ @param key Key string to look for
+ @param notfound Value to return in case of error
+ @return integer
+
+ This function queries a dictionary for a key. A key as read from an
+ ini file is given as "section:key". If the key cannot be found,
+ the notfound value is returned.
+
+ Supported values for integers include the usual C notation
+ so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
+ are supported. Examples:
+
+ "42" -> 42
+ "042" -> 34 (octal -> decimal)
+ "0x42" -> 66 (hexa -> decimal)
+
+ Warning: the conversion may overflow in various ways. Conversion is
+ totally outsourced to strtol(), see the associated man page for overflow
+ handling.
+
+ Credits: Thanks to A. Becker for suggesting strtol()
+ */
+/*--------------------------------------------------------------------------*/
+int iniparser_getint(const dictionary * d, const char * key, int notfound)
+{
+ return (int)iniparser_getlongint(d, key, notfound);
}
/*-------------------------------------------------------------------------*/
@@ -346,13 +474,13 @@ int iniparser_getint(dictionary * d, const char * key, int notfound)
necessarily have to be 0 or 1.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_getboolean(dictionary * d, const char * key, int notfound)
+int iniparser_getboolean(const dictionary * d, const char * key, int notfound)
{
- char * c ;
- int ret ;
+ int ret ;
+ const char * c ;
c = iniparser_getstring(d, key, INI_INVALID_KEY);
- if (c==INI_INVALID_KEY) return notfound ;
+ if (c==NULL || c==INI_INVALID_KEY) return notfound ;
if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
ret = 1 ;
} else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
@@ -375,10 +503,7 @@ int iniparser_getboolean(dictionary * d, const char * key, int notfound)
of querying for the presence of sections in a dictionary.
*/
/*--------------------------------------------------------------------------*/
-int iniparser_find_entry(
- dictionary * ini,
- char * entry
-)
+int iniparser_find_entry(const dictionary * ini, const char * entry)
{
int found=0 ;
if (iniparser_getstring(ini, entry, INI_INVALID_KEY)!=INI_INVALID_KEY) {
@@ -397,14 +522,53 @@ int iniparser_find_entry(
If the given entry can be found, it is deleted from the dictionary.
*/
/*--------------------------------------------------------------------------*/
-void iniparser_unset(dictionary * ini, char * entry)
+void iniparser_unset(dictionary * ini, const char * entry)
{
- dictionary_unset(ini, strlwc(entry));
+ char tmp_str[ASCIILINESZ+1];
+ dictionary_unset(ini, strlwc(entry, tmp_str, sizeof(tmp_str)));
+}
+
+static void parse_quoted_value(char *value, char quote) {
+ char c;
+ char *quoted;
+ int q = 0, v = 0;
+ int esc = 0;
+
+ if(!value)
+ return;
+
+ quoted = xstrdup(value);
+
+ if(!quoted) {
+ iniparser_error_callback("iniparser: memory allocation failure\n");
+ goto end_of_value;
+ }
+
+ while((c = quoted[q]) != '\0') {
+ if(!esc) {
+ if(c == '\\') {
+ esc = 1;
+ q++;
+ continue;
+ }
+
+ if(c == quote) {
+ goto end_of_value;
+ }
+ }
+ esc = 0;
+ value[v] = c;
+ v++;
+ q++;
+ }
+end_of_value:
+ value[v] = '\0';
+ free(quoted);
}
/*-------------------------------------------------------------------------*/
/**
- @brief Load a single line from an INI file
+ @brief Load a single line from an INI file
@param input_line Input line, may be concatenated multi-line input
@param section Output space to store section
@param key Output space to store key
@@ -413,38 +577,54 @@ void iniparser_unset(dictionary * ini, char * entry)
*/
/*--------------------------------------------------------------------------*/
static line_status iniparser_line(
- char * input_line,
+ const char * input_line,
char * section,
char * key,
char * value)
{
line_status sta ;
- char line[ASCIILINESZ+1];
- int len ;
+ char * line = NULL;
+ size_t len ;
+ int d_quote;
- strcpy(line, strstrip(input_line));
- len = (int)strlen(line);
+ line = xstrdup(input_line);
+ len = strstrip(line);
sta = LINE_UNPROCESSED ;
if (len<1) {
/* Empty line */
sta = LINE_EMPTY ;
- } else if (line[0]=='#') {
+ } else if (line[0]=='#' || line[0]==';') {
/* Comment line */
sta = LINE_COMMENT ;
} else if (line[0]=='[' && line[len-1]==']') {
- /* Section name */
- sscanf(line, "[%[^]]", section);
- strcpy(section, strstrip(section));
- strcpy(section, strlwc(section));
+ /* Section name without opening square bracket */
+ sscanf(line, "[%[^\n]", section);
+ len = strlen(section);
+ /* Section name without closing square bracket */
+ if(section[len-1] == ']')
+ {
+ section[len-1] = '\0';
+ }
+ strstrip(section);
+ strlwc(section, section, len);
sta = LINE_SECTION ;
- } else if (sscanf (line, "%[^=] = \"%[^\"]\"", key, value) == 2
- || sscanf (line, "%[^=] = '%[^\']'", key, value) == 2
- || sscanf (line, "%[^=] = %[^;#]", key, value) == 2) {
- /* Usual key=value, with or without comments */
- strcpy(key, strstrip(key));
- strcpy(key, strlwc(key));
- strcpy(value, strstrip(value));
+ } else if ((d_quote = sscanf (line, "%[^=] = \"%[^\n]\"", key, value)) == 2
+ || sscanf (line, "%[^=] = '%[^\n]'", key, value) == 2) {
+ /* Usual key=value with quotes, with or without comments */
+ strstrip(key);
+ strlwc(key, key, len);
+ if(d_quote == 2)
+ parse_quoted_value(value, '"');
+ else
+ parse_quoted_value(value, '\'');
+ /* Don't strip spaces from values surrounded with quotes */
+ sta = LINE_VALUE ;
+ } else if (sscanf (line, "%[^=] = %[^;#]", key, value) == 2) {
+ /* Usual key=value without quotes, with or without comments */
+ strstrip(key);
+ strlwc(key, key, len);
+ strstrip(value);
/*
* sscanf cannot handle '' or "" as empty values
* this is done here
@@ -461,56 +641,51 @@ static line_status iniparser_line(
* key=;
* key=#
*/
- strcpy(key, strstrip(key));
- strcpy(key, strlwc(key));
+ strstrip(key);
+ strlwc(key, key, len);
value[0]=0 ;
sta = LINE_VALUE ;
} else {
/* Generate syntax error */
sta = LINE_ERROR ;
}
+
+ free(line);
return sta ;
}
/*-------------------------------------------------------------------------*/
/**
@brief Parse an ini file and return an allocated dictionary object
- @param ininame Name of the ini file to read.
+ @param in File to read.
+ @param ininame Name of the ini file to read (only used for nicer error messages)
@return Pointer to newly allocated dictionary
This is the parser for ini files. This function is called, providing
- the name of the file to be read. It returns a dictionary object that
- should not be accessed directly, but through accessor functions
- instead.
+ the file to be read. It returns a dictionary object that should not
+ be accessed directly, but through accessor functions instead.
The returned dictionary must be freed using iniparser_freedict().
*/
/*--------------------------------------------------------------------------*/
-dictionary * iniparser_load(const char * ininame)
+dictionary * iniparser_load_file(FILE * in, const char * ininame)
{
- FILE * in ;
-
char line [ASCIILINESZ+1] ;
char section [ASCIILINESZ+1] ;
char key [ASCIILINESZ+1] ;
- char tmp [ASCIILINESZ+1] ;
+ char tmp [(ASCIILINESZ * 2) + 2] ;
char val [ASCIILINESZ+1] ;
int last=0 ;
int len ;
int lineno=0 ;
int errs=0;
+ int mem_err=0;
dictionary * dict ;
- if ((in=fopen(ininame, "r"))==NULL) {
- fprintf(stderr, "iniparser: cannot open %s\n", ininame);
- return NULL ;
- }
-
dict = dictionary_new(0) ;
if (!dict) {
- fclose(in);
return NULL ;
}
@@ -523,14 +698,15 @@ dictionary * iniparser_load(const char * ininame)
while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
lineno++ ;
len = (int)strlen(line)-1;
+ if (len<=0)
+ continue;
/* Safety check against buffer overflows */
- if (line[len]!='\n') {
- fprintf(stderr,
- "iniparser: input line too long in %s (%d)\n",
- ininame,
- lineno);
+ if (line[len]!='\n' && !feof(in)) {
+ iniparser_error_callback(
+ "iniparser: input line too long in %s (%d)\n",
+ ininame,
+ lineno);
dictionary_del(dict);
- fclose(in);
return NULL ;
}
/* Get rid of \n and spaces at end of line */
@@ -539,6 +715,9 @@ dictionary * iniparser_load(const char * ininame)
line[len]=0 ;
len-- ;
}
+ if (len < 0) { /* Line was entirely \n and/or spaces */
+ len = 0;
+ }
/* Detect multi-line */
if (line[len]=='\\') {
/* Multi-line value */
@@ -553,19 +732,20 @@ dictionary * iniparser_load(const char * ininame)
break ;
case LINE_SECTION:
- errs = dictionary_set(dict, section, NULL);
+ mem_err = dictionary_set(dict, section, NULL);
break ;
case LINE_VALUE:
sprintf(tmp, "%s:%s", section, key);
- errs = dictionary_set(dict, tmp, val) ;
+ mem_err = dictionary_set(dict, tmp, val);
break ;
case LINE_ERROR:
- fprintf(stderr, "iniparser: syntax error in %s (%d):\n",
- ininame,
- lineno);
- fprintf(stderr, "-> %s\n", line);
+ iniparser_error_callback(
+ "iniparser: syntax error in %s (%d):\n-> %s\n",
+ ininame,
+ lineno,
+ line);
errs++ ;
break;
@@ -574,8 +754,8 @@ dictionary * iniparser_load(const char * ininame)
}
memset(line, 0, ASCIILINESZ);
last=0;
- if (errs<0) {
- fprintf(stderr, "iniparser: memory allocation failure\n");
+ if (mem_err<0) {
+ iniparser_error_callback("iniparser: memory allocation failure\n");
break ;
}
}
@@ -583,10 +763,40 @@ dictionary * iniparser_load(const char * ininame)
dictionary_del(dict);
dict = NULL ;
}
+ return dict ;
+}
+
+/*-------------------------------------------------------------------------*/
+/**
+ @brief Parse an ini file and return an allocated dictionary object
+ @param ininame Name of the ini file to read.
+ @return Pointer to newly allocated dictionary
+
+ This is the parser for ini files. This function is called, providing
+ the name of the file to be read. It returns a dictionary object that
+ should not be accessed directly, but through accessor functions
+ instead.
+
+ The returned dictionary must be freed using iniparser_freedict().
+ */
+/*--------------------------------------------------------------------------*/
+dictionary * iniparser_load(const char * ininame)
+{
+ FILE * in ;
+ dictionary * dict ;
+
+ if ((in=fopen(ininame, "r"))==NULL) {
+ iniparser_error_callback("iniparser: cannot open %s\n", ininame);
+ return NULL ;
+ }
+
+ dict = iniparser_load_file(in, ininame);
fclose(in);
+
return dict ;
}
+
/*-------------------------------------------------------------------------*/
/**
@brief Free all memory associated to an ini dictionary
@@ -602,5 +812,3 @@ void iniparser_freedict(dictionary * d)
{
dictionary_del(d);
}
-
-/* vim: set ts=4 et sw=4 tw=75 */
diff --git a/misc-utils/flash_erase.c b/misc-utils/flash_erase.c
index c6f6f66..36f8d57 100644
--- a/misc-utils/flash_erase.c
+++ b/misc-utils/flash_erase.c
@@ -239,7 +239,7 @@ int main(int argc, char *argv[])
if (eb_cnt == 0)
eb_cnt = (mtd.size / mtd.eb_size) - eb_start;
- if (eb_start == 0 && mtd.size == eb_cnt * mtd.eb_size)
+ if (eb_start == 0 && mtd.size == (long long)eb_cnt * mtd.eb_size)
erase_chip = true;
/* If MTD device may have bad eraseblocks,
diff --git a/misc-utils/flashcp.c b/misc-utils/flashcp.c
index 9c48637..6065a8c 100644
--- a/misc-utils/flashcp.c
+++ b/misc-utils/flashcp.c
@@ -221,7 +221,7 @@ int main (int argc,char *argv[])
struct mtd_info_user mtd;
struct erase_info_user erase;
struct stat filestat;
- unsigned char *src,*dest,*wrlast_buf;
+ unsigned char *src, *dest, *wrlast_buf = NULL;
unsigned long long wrlast_len = 0;
int error = 0;
diff --git a/misc-utils/ftl_check.c b/misc-utils/ftl_check.c
index 5b2dae5..fe6a91a 100644
--- a/misc-utils/ftl_check.c
+++ b/misc-utils/ftl_check.c
@@ -121,6 +121,10 @@ static void check_partition(int fd)
/* Create basic block allocation table for control blocks */
nbam = (mtd.erasesize >> hdr.BlockSize);
bam = malloc(nbam * sizeof(u_int));
+ if (!bam) {
+ perror("malloc failed");
+ return;
+ }
for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) {
if (lseek(fd, (i << hdr.EraseUnitSize), SEEK_SET) == -1) {
diff --git a/nand-utils/nanddump.c b/nand-utils/nanddump.c
index bc22858..b4de05e 100644
--- a/nand-utils/nanddump.c
+++ b/nand-utils/nanddump.c
@@ -102,6 +102,7 @@ static void process_options(int argc, char * const argv[])
{
int error = 0;
bool oob_default = true;
+ char *dumpfile_tmp = NULL;
for (;;) {
int option_index = 0;
@@ -165,8 +166,8 @@ static void process_options(int argc, char * const argv[])
start_addr = simple_strtoll(optarg, &error);
break;
case 'f':
- free(dumpfile);
- dumpfile = xstrdup(optarg);
+ free(dumpfile_tmp);
+ dumpfile = dumpfile_tmp = xstrdup(optarg);
break;
case 'l':
length = simple_strtoll(optarg, &error);
diff --git a/nand-utils/nandflipbits.c b/nand-utils/nandflipbits.c
index 7066408..a417189 100644
--- a/nand-utils/nandflipbits.c
+++ b/nand-utils/nandflipbits.c
@@ -251,7 +251,7 @@ int main(int argc, char **argv)
bufoffs += mtd.min_io_size;
ret = mtd_read_oob(mtd_desc, &mtd, fd,
- bit_to_flip->block * mtd.eb_size +
+ (unsigned long long)bit_to_flip->block * mtd.eb_size +
blkoffs,
mtd.oob_size, buffer + bufoffs);
if (ret) {
diff --git a/tests/checkfs/comm.c b/tests/checkfs/comm.c
index c8c457e..9f4e20d 100644
--- a/tests/checkfs/comm.c
+++ b/tests/checkfs/comm.c
@@ -28,6 +28,8 @@
#include <unistd.h>
#include <string.h>
+int do_pwr_dn(int fd, int cycleCnt);
+
/*
This is the routine that forms and
sends the "ok to pwr me down" message
diff --git a/ubi-utils/mtdinfo.c b/ubi-utils/mtdinfo.c
index 7dff0de..61ce7bc 100644
--- a/ubi-utils/mtdinfo.c
+++ b/ubi-utils/mtdinfo.c
@@ -185,7 +185,7 @@ static void print_ubi_info(const struct mtd_info *mtd_info,
static void print_region_map(const struct mtd_dev_info *mtd, int fd,
const region_info_t *reginfo)
{
- unsigned long start;
+ unsigned long long start;
int i, width;
int ret_locked, errno_locked, ret_bad, errno_bad;
@@ -203,8 +203,8 @@ static void print_region_map(const struct mtd_dev_info *mtd, int fd,
ret_locked = ret_bad = errno_locked = errno_bad = 0;
for (i = 0; i < reginfo->numblocks; ++i) {
- start = reginfo->offset + i * reginfo->erasesize;
- printf(" %*i: %08lx ", width, i, start);
+ start = reginfo->offset + (unsigned long long)i * reginfo->erasesize;
+ printf(" %*i: %08llx ", width, i, start);
if (ret_locked != -1) {
ret_locked = mtd_is_locked(mtd, fd, i);
diff --git a/ubi-utils/ubinize.c b/ubi-utils/ubinize.c
index ac8c1e5..9c950b1 100644
--- a/ubi-utils/ubinize.c
+++ b/ubi-utils/ubinize.c
@@ -375,8 +375,8 @@ static int read_section(const struct ubigen_info *ui, const char *sname,
vi->alignment = iniparser_getint(args.dict, buf, -1);
if (vi->alignment == -1)
vi->alignment = 1;
- else if (vi->id < 0)
- return errmsg("negative volume alignment %d in section \"%s\"",
+ else if (vi->alignment <= 0)
+ return errmsg("not positive volume alignment %d in section \"%s\"",
vi->alignment, sname);
verbose(args.verbose, "volume alignment: %d", vi->alignment);
diff --git a/ubi-utils/ubirsvol.c b/ubi-utils/ubirsvol.c
index 0854abc..73d2f68 100644
--- a/ubi-utils/ubirsvol.c
+++ b/ubi-utils/ubirsvol.c
@@ -231,7 +231,7 @@ int main(int argc, char * const argv[])
}
if (args.lebs != -1)
- args.bytes = vol_info.leb_size * args.lebs;
+ args.bytes = (long long)vol_info.leb_size * args.lebs;
err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
if (err) {
diff --git a/ubifs-utils/Makemodule.am b/ubifs-utils/Makemodule.am
index a511c40..21ba059 100644
--- a/ubifs-utils/Makemodule.am
+++ b/ubifs-utils/Makemodule.am
@@ -72,7 +72,7 @@ mkfs_ubifs_SOURCES = \
ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
mkfs_ubifs_LDADD = libmtd.a libubi.a $(ZLIB_LIBS) $(LZO_LIBS) $(ZSTD_LIBS) $(UUID_LIBS) $(LIBSELINUX_LIBS) $(OPENSSL_LIBS) \
- $(DUMP_STACK_LD) -lm -lpthread
+ $(DUMP_STACK_LD) $(ASAN_LIBS) -lm -lpthread
mkfs_ubifs_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS) $(LZO_CFLAGS) $(ZSTD_CFLAGS) $(UUID_CFLAGS) $(LIBSELINUX_CFLAGS) \
-I$(top_srcdir)/ubi-utils/include -I$(top_srcdir)/ubifs-utils/common -I $(top_srcdir)/ubifs-utils/libubifs
@@ -90,7 +90,7 @@ fsck_ubifs_SOURCES = \
ubifs-utils/fsck.ubifs/handle_disconnected.c
fsck_ubifs_LDADD = libmtd.a libubi.a $(ZLIB_LIBS) $(LZO_LIBS) $(ZSTD_LIBS) $(UUID_LIBS) $(LIBSELINUX_LIBS) $(OPENSSL_LIBS) \
- $(DUMP_STACK_LD) -lm -lpthread
+ $(DUMP_STACK_LD) $(ASAN_LIBS) -lm -lpthread
fsck_ubifs_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS) $(LZO_CFLAGS) $(ZSTD_CFLAGS) $(UUID_CFLAGS) $(LIBSELINUX_CFLAGS) \
-I$(top_srcdir)/ubi-utils/include -I$(top_srcdir)/ubifs-utils/common -I $(top_srcdir)/ubifs-utils/libubifs \
-I$(top_srcdir)/ubifs-utils/fsck.ubifs
diff --git a/ubifs-utils/common/README b/ubifs-utils/common/README
index a93ebd0..8fe716e 100644
--- a/ubifs-utils/common/README
+++ b/ubifs-utils/common/README
@@ -1,14 +1,13 @@
Common Library
-* crc16.h and crc16.c were copied from the linux kernel.
-* crc32.h and crc32.c were copied from mtd-utils and amended.
+* crc16.h and crc16.c were copied from the linux kernel(https://elixir.bootlin.com/linux/v5.10.232/source/lib/crc16.c).
* defs.h is a bunch of definitions to smooth things over.
* hashtable/* was downloaded from http://www.cl.cam.ac.uk/~cwc22/hashtable/
* atomic.h was downloaded from https://the-linux-channel.the-toffee-project.org/index.php?page=6-tutorials-linux-user-space-atomic-operations
-* bitops.h and bitops.c were copied from the linux kernel.
-* compiler_attributes.h was copied from the linux kernel.
-* linux_types.h was copied from the linux kernel.
-* linux_err.h was copied from the linux kernel.
-* hexdump.c was copied from the linux kernel.
-* kmem.h and kmem.c were partial copied from xfsprogs-dev (https://git.kernel.org/pub/scm/fs/xfs/xfsprogs-dev.git/)
-* sort.h and sort.c were copied from the linux kernel.
+* bitops.h and bitops.c were copied from the linux kernel(https://elixir.bootlin.com/linux/v5.10.232/source/lib/find_bit.c).
+* compiler_attributes.h was copied from the linux kernel(https://elixir.bootlin.com/linux/v5.10.232/source/include/linux/compiler_attributes.h).
+* linux_types.h was copied from the linux kernel(https://elixir.bootlin.com/linux/v5.10.232/source/include/linux/types.h overflow.h fscrypt.h).
+* linux_err.h was copied from the linux kernel(https://elixir.bootlin.com/linux/v5.10.232/source/include/linux/err.h).
+* hexdump.c was copied from the linux kernel(https://elixir.bootlin.com/linux/v5.10.232/source/lib/hexdump.c).
+* kmem.h and kmem.c were partial copied from xfsprogs-dev(https://git.kernel.org/pub/scm/fs/xfs/xfsprogs-dev.git/).
+* sort.h and sort.c were copied from the linux kernel(https://elixir.bootlin.com/linux/v5.10.232/source/lib/sort.c).
diff --git a/ubifs-utils/libubifs/README b/ubifs-utils/libubifs/README
index 551ed8e..dd9322a 100644
--- a/ubifs-utils/libubifs/README
+++ b/ubifs-utils/libubifs/README
@@ -1,4 +1,4 @@
-UBIFS Library (Imported from linux kernel 6.8-rc2 41bccc98fb7931d63)
+UBIFS Library (Imported from linux kernel 6.13-rc7 aa22f4da2a46)
* ubifs.h is a selection of definitions from fs/ubifs/ubifs.h from the linux kernel.
* key.h is copied from fs/ubifs/key.h from the linux kernel.
diff --git a/ubifs-utils/libubifs/find.c b/ubifs-utils/libubifs/find.c
index ecf689c..364252e 100644
--- a/ubifs-utils/libubifs/find.c
+++ b/ubifs-utils/libubifs/find.c
@@ -80,7 +80,7 @@ static int valuable(struct ubifs_info *c, const struct ubifs_lprops *lprops)
* @c: the UBIFS file-system description object
* @lprops: LEB properties to scan
* @in_tree: whether the LEB properties are in main memory
- * @data: information passed to and from the caller of the scan
+ * @arg: information passed to and from the caller of the scan
*
* This function returns a code that indicates whether the scan should continue
* (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
@@ -89,8 +89,9 @@ static int valuable(struct ubifs_info *c, const struct ubifs_lprops *lprops)
*/
static int scan_for_dirty_cb(struct ubifs_info *c,
const struct ubifs_lprops *lprops, int in_tree,
- struct scan_data *data)
+ void *arg)
{
+ struct scan_data *data = arg;
int ret = LPT_SCAN_CONTINUE;
/* Exclude LEBs that are currently in use */
@@ -173,8 +174,7 @@ static const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
data.pick_free = pick_free;
data.lnum = -1;
data.exclude_index = exclude_index;
- err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
- (ubifs_lpt_scan_callback)scan_for_dirty_cb,
+ err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum, scan_for_dirty_cb,
&data);
if (err)
return ERR_PTR(err);
@@ -347,7 +347,7 @@ out:
* @c: the UBIFS file-system description object
* @lprops: LEB properties to scan
* @in_tree: whether the LEB properties are in main memory
- * @data: information passed to and from the caller of the scan
+ * @arg: information passed to and from the caller of the scan
*
* This function returns a code that indicates whether the scan should continue
* (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
@@ -356,8 +356,9 @@ out:
*/
static int scan_for_free_cb(struct ubifs_info *c,
const struct ubifs_lprops *lprops, int in_tree,
- struct scan_data *data)
+ void *arg)
{
+ struct scan_data *data = arg;
int ret = LPT_SCAN_CONTINUE;
/* Exclude LEBs that are currently in use */
@@ -453,7 +454,7 @@ const struct ubifs_lprops *do_find_free_space(struct ubifs_info *c,
data.pick_free = pick_free;
data.lnum = -1;
err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
- (ubifs_lpt_scan_callback)scan_for_free_cb,
+ scan_for_free_cb,
&data);
if (err)
return ERR_PTR(err);
@@ -587,7 +588,7 @@ out:
* @c: the UBIFS file-system description object
* @lprops: LEB properties to scan
* @in_tree: whether the LEB properties are in main memory
- * @data: information passed to and from the caller of the scan
+ * @arg: information passed to and from the caller of the scan
*
* This function returns a code that indicates whether the scan should continue
* (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
@@ -596,8 +597,9 @@ out:
*/
static int scan_for_idx_cb(struct ubifs_info *c,
const struct ubifs_lprops *lprops, int in_tree,
- struct scan_data *data)
+ void *arg)
{
+ struct scan_data *data = arg;
int ret = LPT_SCAN_CONTINUE;
/* Exclude LEBs that are currently in use */
@@ -632,8 +634,7 @@ static const struct ubifs_lprops *scan_for_leb_for_idx(struct ubifs_info *c)
int err;
data.lnum = -1;
- err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
- (ubifs_lpt_scan_callback)scan_for_idx_cb,
+ err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum, scan_for_idx_cb,
&data);
if (err)
return ERR_PTR(err);
@@ -733,11 +734,10 @@ out:
return err;
}
-static int cmp_dirty_idx(const struct ubifs_lprops **a,
- const struct ubifs_lprops **b)
+static int cmp_dirty_idx(const void *a, const void *b)
{
- const struct ubifs_lprops *lpa = *a;
- const struct ubifs_lprops *lpb = *b;
+ const struct ubifs_lprops *lpa = *(const struct ubifs_lprops **)a;
+ const struct ubifs_lprops *lpb = *(const struct ubifs_lprops **)b;
return lpa->dirty + lpa->free - lpb->dirty - lpb->free;
}
@@ -761,7 +761,7 @@ int ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
sizeof(void *) * c->dirty_idx.cnt);
/* Sort it so that the dirtiest is now at the end */
sort(c->dirty_idx.arr, c->dirty_idx.cnt, sizeof(void *),
- (int (*)(const void *, const void *))cmp_dirty_idx, NULL);
+ cmp_dirty_idx, NULL);
dbg_find("found %d dirty index LEBs", c->dirty_idx.cnt);
if (c->dirty_idx.cnt)
dbg_find("dirtiest index LEB is %d with dirty %d and free %d",
@@ -780,7 +780,7 @@ int ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
* @c: the UBIFS file-system description object
* @lprops: LEB properties to scan
* @in_tree: whether the LEB properties are in main memory
- * @data: information passed to and from the caller of the scan
+ * @arg: information passed to and from the caller of the scan
*
* This function returns a code that indicates whether the scan should continue
* (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
@@ -789,8 +789,9 @@ int ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
*/
static int scan_dirty_idx_cb(struct ubifs_info *c,
const struct ubifs_lprops *lprops, int in_tree,
- struct scan_data *data)
+ void *arg)
{
+ struct scan_data *data = arg;
int ret = LPT_SCAN_CONTINUE;
/* Exclude LEBs that are currently in use */
@@ -849,8 +850,7 @@ static int find_dirty_idx_leb(struct ubifs_info *c)
if (c->pnodes_have >= c->pnode_cnt)
/* All pnodes are in memory, so skip scan */
return -ENOSPC;
- err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
- (ubifs_lpt_scan_callback)scan_dirty_idx_cb,
+ err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum, scan_dirty_idx_cb,
&data);
if (err)
return err;
diff --git a/ubifs-utils/libubifs/lpt.c b/ubifs-utils/libubifs/lpt.c
index 8e20a17..f2f2727 100644
--- a/ubifs-utils/libubifs/lpt.c
+++ b/ubifs-utils/libubifs/lpt.c
@@ -2009,6 +2009,7 @@ out_err:
* @pnode: where to keep a pnode
* @cnode: where to keep a cnode
* @in_tree: is the node in the tree in memory
+ * @ptr: union of node pointers
* @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
* the tree
* @ptr.pnode: ditto for pnode
diff --git a/ubifs-utils/libubifs/lpt_commit.c b/ubifs-utils/libubifs/lpt_commit.c
index ee84f80..79f7b14 100644
--- a/ubifs-utils/libubifs/lpt_commit.c
+++ b/ubifs-utils/libubifs/lpt_commit.c
@@ -580,7 +580,7 @@ struct ubifs_pnode *ubifs_find_next_pnode(struct ubifs_info *c,
/* Go right */
nnode = ubifs_get_nnode(c, nnode, iip);
if (IS_ERR(nnode))
- return (void *)nnode;
+ return ERR_CAST(nnode);
/* Go down to level 1 */
while (nnode->level > 1) {
@@ -597,7 +597,7 @@ struct ubifs_pnode *ubifs_find_next_pnode(struct ubifs_info *c,
}
nnode = ubifs_get_nnode(c, nnode, iip);
if (IS_ERR(nnode))
- return (void *)nnode;
+ return ERR_CAST(nnode);
}
for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
diff --git a/ubifs-utils/libubifs/replay.c b/ubifs-utils/libubifs/replay.c
index 3943b32..9d61133 100644
--- a/ubifs-utils/libubifs/replay.c
+++ b/ubifs-utils/libubifs/replay.c
@@ -34,6 +34,7 @@
* @lnum: logical eraseblock number of the node
* @offs: node offset
* @len: node length
+ * @hash: node hash
* @deletion: non-zero if this entry corresponds to a node deletion
* @sqnum: node sequence number
* @list: links the replay list
diff --git a/ubifs-utils/libubifs/tnc_commit.c b/ubifs-utils/libubifs/tnc_commit.c
index d797006..66922d4 100644
--- a/ubifs-utils/libubifs/tnc_commit.c
+++ b/ubifs-utils/libubifs/tnc_commit.c
@@ -663,6 +663,8 @@ static int get_znodes_to_commit(struct ubifs_info *c)
znode->alt = 0;
cnext = find_next_dirty(znode);
if (!cnext) {
+ ubifs_assert(c, !znode->parent);
+ znode->cparent = NULL;
znode->cnext = c->cnext;
break;
}