2
#include "tree-walk.h"
3
#include "tree.h"
4
5
+static const char *get_mode(const char *str, unsigned int *modep)
6
+{
7
+ unsigned char c;
8
+ unsigned int mode = 0;
9
+
10
+ while ((c = *str++) != ' ') {
11
+ if (c < '0' || c > '7')
12
+ return NULL;
13
+ mode = (mode << 3) + (c - '0');
14
+ }
15
+ *modep = mode;
16
+ return str;
17
+}
18
+
19
+static void decode_tree_entry(struct tree_desc *desc, const void *buf, unsigned long size)
20
+{
21
+ const char *path;
22
+ unsigned int mode, len;
23
+
24
+ path = get_mode(buf, &mode);
25
+ if (!path)
26
+ die("corrupt tree file");
27
+ len = strlen(path) + 1;
28
+
29
+ /* Initialize the descriptor entry */
30
+ desc->entry.path = path;
31
+ desc->entry.mode = mode;
32
+ desc->entry.sha1 = (const unsigned char *)(path + len);
33
+}
34
+
35
void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
36
{
37
desc->buffer = buffer;
38
desc->size = size;
39
+ if (size)
40
+ decode_tree_entry(desc, buffer, size);
41
}
42
43
void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
68
69
static void entry_extract(struct tree_desc *t, struct name_entry *a)
70
{
39
- a->sha1 = tree_entry_extract(t, &a->path, &a->mode);
71
+ *a = t->entry;
72
}
73
74
void update_tree_entry(struct tree_desc *desc)
75
{
76
const void *buf = desc->buffer;
77
+ const unsigned char *end = desc->entry.sha1 + 20;
78
unsigned long size = desc->size;
46
- int len = strlen(buf) + 1 + 20;
79
+ unsigned long len = end - (const unsigned char *)buf;
80
81
if (size < len)
82
die("corrupt tree file");
50
- desc->buffer = (char *) buf + len;
51
- desc->size = size - len;
52
-}
53
-
54
-static const char *get_mode(const char *str, unsigned int *modep)
55
-{
56
- unsigned char c;
57
- unsigned int mode = 0;
58
-
59
- while ((c = *str++) != ' ') {
60
- if (c < '0' || c > '7')
61
- return NULL;
62
- mode = (mode << 3) + (c - '0');
63
- }
64
- *modep = mode;
65
- return str;
66
-}
67
-
68
-const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
69
-{
70
- const void *tree = desc->buffer;
71
- unsigned long size = desc->size;
72
- int len = strlen(tree)+1;
73
- const unsigned char *sha1 = (unsigned char *) tree + len;
74
- const char *path;
75
- unsigned int mode;
76
-
77
- path = get_mode(tree, &mode);
78
- if (!path || size < len + 20)
79
- die("corrupt tree file");
80
- *pathp = path;
81
- *modep = canon_mode(mode);
82
- return sha1;
83
+ buf = end;
84
+ size -= len;
85
+ desc->buffer = buf;
86
+ desc->size = size;
87
+ if (size)
88
+ decode_tree_entry(desc, buf, size);
89
}
90
91
int tree_entry(struct tree_desc *desc, struct name_entry *entry)
92
{
87
- const void *tree = desc->buffer;
88
- const char *path;
89
- unsigned long len, size = desc->size;
90
-
91
- if (!size)
93
+ if (!desc->size)
94
return 0;
95
94
- path = get_mode(tree, &entry->mode);
95
- if (!path)
96
- die("corrupt tree file");
97
-
98
- entry->path = path;
99
- len = strlen(path);
100
-
101
- path += len + 1;
102
- entry->sha1 = (const unsigned char *) path;
103
-
104
- path += 20;
105
- len = path - (char *) tree;
106
- if (len > size)
107
- die("corrupt tree file");
108
-
109
- desc->buffer = path;
110
- desc->size = size - len;
96
+ *entry = desc->entry;
97
+ update_tree_entry(desc);
98
return 1;
99
}
100