2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * This simple implementation of a name-value store assumes a small number of
16 * values and fits into a small finite buffer.
18 * Each attribute is stored as a record:
19 * sizeof(int) bytes record size.
20 * strnlen+1 bytes name null terminated.
23 * total size stored in record size
25 * This code has not been tested with unicode yet.
28 #include "yaffs_nameval.h"
32 static int nval_find(const char *xb, int xb_size, const YCHAR *name,
38 memcpy(&size, xb, sizeof(int));
39 while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
40 if (!strncmp((YCHAR *) (xb + pos + sizeof(int)), name, size)) {
46 if (pos < xb_size - sizeof(int))
47 memcpy(&size, xb + pos, sizeof(int));
56 static int nval_used(const char *xb, int xb_size)
61 memcpy(&size, xb + pos, sizeof(int));
62 while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
64 if (pos < xb_size - sizeof(int))
65 memcpy(&size, xb + pos, sizeof(int));
72 int nval_del(char *xb, int xb_size, const YCHAR *name)
74 int pos = nval_find(xb, xb_size, name, NULL);
77 if (pos < 0 || pos >= xb_size)
80 /* Find size, shift rest over this record,
81 * then zero out the rest of buffer */
82 memcpy(&size, xb + pos, sizeof(int));
83 memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
84 memset(xb + (xb_size - size), 0, size);
88 int nval_set(char *xb, int xb_size, const YCHAR *name, const char *buf,
92 int namelen = strnlen(name, xb_size);
98 pos = nval_find(xb, xb_size, name, &size_exist);
100 if (flags & XATTR_CREATE && pos >= 0)
102 if (flags & XATTR_REPLACE && pos < 0)
105 start = nval_used(xb, xb_size);
106 space = xb_size - start + size_exist;
108 reclen = (sizeof(int) + namelen + 1 + bsize);
114 nval_del(xb, xb_size, name);
115 start = nval_used(xb, xb_size);
120 memcpy(xb + pos, &reclen, sizeof(int));
122 strncpy((YCHAR *) (xb + pos), name, reclen);
123 pos += (namelen + 1);
124 memcpy(xb + pos, buf, bsize);
128 int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
131 int pos = nval_find(xb, xb_size, name, NULL);
134 if (pos >= 0 && pos < xb_size) {
136 memcpy(&size, xb + pos, sizeof(int));
137 pos += sizeof(int); /* advance past record length */
140 /* Advance over name string */
141 while (xb[pos] && size > 0 && pos < xb_size) {
145 /*Advance over NUL */
149 /* If bsize is zero then this is a size query.
150 * Return the size, but don't copy.
156 memcpy(buf, xb + pos, size);
166 int nval_list(const char *xb, int xb_size, char *buf, int bsize)
174 memcpy(&size, xb + pos, sizeof(int));
175 while (size > sizeof(int) &&
177 (pos + size) < xb_size &&
181 name_len = strnlen((YCHAR *) (xb + pos), size);
182 if (ncopied + name_len + 1 < bsize) {
183 memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
187 if (sizeof(YCHAR) > 1) {
191 ncopied += (name_len + 1);
196 if (pos < xb_size - sizeof(int))
197 memcpy(&size, xb + pos, sizeof(int));
204 int nval_hasvalues(const char *xb, int xb_size)
206 return nval_used(xb, xb_size) > 0;