summaryrefslogtreecommitdiff
path: root/ANDROID_3.4.5/fs/yaffs2/yaffs_nameval.c
blob: daa36f989d318bf6dd5684973576a875144d455f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
 *
 * Copyright (C) 2002-2010 Aleph One Ltd.
 *   for Toby Churchill Ltd and Brightstar Engineering
 *
 * Created by Charles Manning <charles@aleph1.co.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * This simple implementation of a name-value store assumes a small number of values and fits
 * into a small finite buffer.
 *
 * Each attribute is stored as a record:
 *  sizeof(int) bytes   record size.
 *  strnlen+1 bytes name null terminated.
 *  nbytes    value.
 *  ----------
 *  total size  stored in record size 
 *
 * This code has not been tested with unicode yet.
 */

#include "yaffs_nameval.h"

#include "yportenv.h"

static int nval_find(const char *xb, int xb_size, const YCHAR * name,
		     int *exist_size)
{
	int pos = 0;
	int size;

	memcpy(&size, xb, sizeof(int));
	while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
		if (strncmp
		    ((YCHAR *) (xb + pos + sizeof(int)), name, size) == 0) {
			if (exist_size)
				*exist_size = size;
			return pos;
		}
		pos += size;
		if (pos < xb_size - sizeof(int))
			memcpy(&size, xb + pos, sizeof(int));
		else
			size = 0;
	}
	if (exist_size)
		*exist_size = 0;
	return -1;
}

static int nval_used(const char *xb, int xb_size)
{
	int pos = 0;
	int size;

	memcpy(&size, xb + pos, sizeof(int));
	while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
		pos += size;
		if (pos < xb_size - sizeof(int))
			memcpy(&size, xb + pos, sizeof(int));
		else
			size = 0;
	}
	return pos;
}

int nval_del(char *xb, int xb_size, const YCHAR * name)
{
	int pos = nval_find(xb, xb_size, name, NULL);
	int size;

	if (pos >= 0 && pos < xb_size) {
		/* Find size, shift rest over this record, then zero out the rest of buffer */
		memcpy(&size, xb + pos, sizeof(int));
		memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
		memset(xb + (xb_size - size), 0, size);
		return 0;
	} else {
		return -ENODATA;
        }
}

int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
	     int bsize, int flags)
{
	int pos;
	int namelen = strnlen(name, xb_size);
	int reclen;
	int size_exist = 0;
	int space;
	int start;

	pos = nval_find(xb, xb_size, name, &size_exist);

	if (flags & XATTR_CREATE && pos >= 0)
		return -EEXIST;
	if (flags & XATTR_REPLACE && pos < 0)
		return -ENODATA;

	start = nval_used(xb, xb_size);
	space = xb_size - start + size_exist;

	reclen = (sizeof(int) + namelen + 1 + bsize);

	if (reclen > space)
		return -ENOSPC;

	if (pos >= 0) {
		nval_del(xb, xb_size, name);
		start = nval_used(xb, xb_size);
	}

	pos = start;

	memcpy(xb + pos, &reclen, sizeof(int));
	pos += sizeof(int);
	strncpy((YCHAR *) (xb + pos), name, reclen);
	pos += (namelen + 1);
	memcpy(xb + pos, buf, bsize);
	return 0;
}

int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
	     int bsize)
{
	int pos = nval_find(xb, xb_size, name, NULL);
	int size;

	if (pos >= 0 && pos < xb_size) {

		memcpy(&size, xb + pos, sizeof(int));
		pos += sizeof(int);	/* advance past record length */
		size -= sizeof(int);

		/* Advance over name string */
		while (xb[pos] && size > 0 && pos < xb_size) {
			pos++;
			size--;
		}
		/*Advance over NUL */
		pos++;
		size--;

		if (size <= bsize) {
			memcpy(buf, xb + pos, size);
			return size;
		}

	}
	if (pos >= 0)
		return -ERANGE;
	else
		return -ENODATA;
}

int nval_list(const char *xb, int xb_size, char *buf, int bsize)
{
	int pos = 0;
	int size;
	int name_len;
	int ncopied = 0;
	int filled = 0;

	memcpy(&size, xb + pos, sizeof(int));
	while (size > sizeof(int) && size <= xb_size && (pos + size) < xb_size
	       && !filled) {
		pos += sizeof(int);
		size -= sizeof(int);
		name_len = strnlen((YCHAR *) (xb + pos), size);
		if (ncopied + name_len + 1 < bsize) {
			memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
			buf += name_len;
			*buf = '\0';
			buf++;
			if (sizeof(YCHAR) > 1) {
				*buf = '\0';
				buf++;
			}
			ncopied += (name_len + 1);
		} else {
			filled = 1;
                }
		pos += size;
		if (pos < xb_size - sizeof(int))
			memcpy(&size, xb + pos, sizeof(int));
		else
			size = 0;
	}
	return ncopied;
}

int nval_hasvalues(const char *xb, int xb_size)
{
	return nval_used(xb, xb_size) > 0;
}