yaffs: Remove tags validity checking code.
[yaffs2.git] / yaffs_nameval.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 /*
15  * This simple implementation of a name-value store assumes a small number of
16 * values and fits into a small finite buffer.
17  *
18  * Each attribute is stored as a record:
19  *  sizeof(int) bytes   record size.
20  *  strnlen+1 bytes name null terminated.
21  *  nbytes    value.
22  *  ----------
23  *  total size  stored in record size
24  *
25  * This code has not been tested with unicode yet.
26  */
27
28 #include "yaffs_nameval.h"
29
30 #include "yportenv.h"
31
32 static int nval_find(const char *xb, int xb_size, const YCHAR *name,
33                      int *exist_size)
34 {
35         int pos = 0;
36         int size;
37
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)) {
41                         if (exist_size)
42                                 *exist_size = size;
43                         return pos;
44                 }
45                 pos += size;
46                 if (pos < xb_size - sizeof(int))
47                         memcpy(&size, xb + pos, sizeof(int));
48                 else
49                         size = 0;
50         }
51         if (exist_size)
52                 *exist_size = 0;
53         return -ENODATA;
54 }
55
56 static int nval_used(const char *xb, int xb_size)
57 {
58         int pos = 0;
59         int size;
60
61         memcpy(&size, xb + pos, sizeof(int));
62         while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
63                 pos += size;
64                 if (pos < xb_size - sizeof(int))
65                         memcpy(&size, xb + pos, sizeof(int));
66                 else
67                         size = 0;
68         }
69         return pos;
70 }
71
72 int nval_del(char *xb, int xb_size, const YCHAR *name)
73 {
74         int pos = nval_find(xb, xb_size, name, NULL);
75         int size;
76
77         if (pos < 0 || pos >= xb_size)
78                 return -ENODATA;
79
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);
85         return 0;
86 }
87
88 int nval_set(char *xb, int xb_size, const YCHAR *name, const char *buf,
89                 int bsize, int flags)
90 {
91         int pos;
92         int namelen = strnlen(name, xb_size);
93         int reclen;
94         int size_exist = 0;
95         int space;
96         int start;
97
98         pos = nval_find(xb, xb_size, name, &size_exist);
99
100         if (flags & XATTR_CREATE && pos >= 0)
101                 return -EEXIST;
102         if (flags & XATTR_REPLACE && pos < 0)
103                 return -ENODATA;
104
105         start = nval_used(xb, xb_size);
106         space = xb_size - start + size_exist;
107
108         reclen = (sizeof(int) + namelen + 1 + bsize);
109
110         if (reclen > space)
111                 return -ENOSPC;
112
113         if (pos >= 0) {
114                 nval_del(xb, xb_size, name);
115                 start = nval_used(xb, xb_size);
116         }
117
118         pos = start;
119
120         memcpy(xb + pos, &reclen, sizeof(int));
121         pos += sizeof(int);
122         strncpy((YCHAR *) (xb + pos), name, reclen);
123         pos += (namelen + 1);
124         memcpy(xb + pos, buf, bsize);
125         return 0;
126 }
127
128 int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
129              int bsize)
130 {
131         int pos = nval_find(xb, xb_size, name, NULL);
132         int size;
133
134         if (pos >= 0 && pos < xb_size) {
135
136                 memcpy(&size, xb + pos, sizeof(int));
137                 pos += sizeof(int);     /* advance past record length */
138                 size -= sizeof(int);
139
140                 /* Advance over name string */
141                 while (xb[pos] && size > 0 && pos < xb_size) {
142                         pos++;
143                         size--;
144                 }
145                 /*Advance over NUL */
146                 pos++;
147                 size--;
148
149                 if (size <= bsize) {
150                         memcpy(buf, xb + pos, size);
151                         return size;
152                 }
153         }
154         if (pos >= 0)
155                 return -ERANGE;
156
157         return -ENODATA;
158 }
159
160 int nval_list(const char *xb, int xb_size, char *buf, int bsize)
161 {
162         int pos = 0;
163         int size;
164         int name_len;
165         int ncopied = 0;
166         int filled = 0;
167
168         memcpy(&size, xb + pos, sizeof(int));
169         while (size > sizeof(int) &&
170                 size <= xb_size &&
171                 (pos + size) < xb_size &&
172                 !filled) {
173                 pos += sizeof(int);
174                 size -= sizeof(int);
175                 name_len = strnlen((YCHAR *) (xb + pos), size);
176                 if (ncopied + name_len + 1 < bsize) {
177                         memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
178                         buf += name_len;
179                         *buf = '\0';
180                         buf++;
181                         if (sizeof(YCHAR) > 1) {
182                                 *buf = '\0';
183                                 buf++;
184                         }
185                         ncopied += (name_len + 1);
186                 } else {
187                         filled = 1;
188                 }
189                 pos += size;
190                 if (pos < xb_size - sizeof(int))
191                         memcpy(&size, xb + pos, sizeof(int));
192                 else
193                         size = 0;
194         }
195         return ncopied;
196 }
197
198 int nval_hasvalues(const char *xb, int xb_size)
199 {
200         return nval_used(xb, xb_size) > 0;
201 }