Hash :
b183ffe7
Author :
Date :
2011-07-26T12:26:12
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
/*
* Copyright (C) 2009-2011 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "tree-cache.h"
static int read_tree_internal(git_tree_cache **out,
const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
{
git_tree_cache *tree;
const char *name_start, *buffer;
int count;
int error = GIT_SUCCESS;
size_t name_len;
buffer = name_start = *buffer_in;
if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
if (++buffer >= buffer_end) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
name_len = strlen(name_start);
if ((tree = git__malloc(sizeof(git_tree_cache) + name_len + 1)) == NULL)
return GIT_ENOMEM;
memset(tree, 0x0, sizeof(git_tree_cache));
tree->parent = parent;
/* NUL-terminated tree name */
memcpy(tree->name, name_start, name_len);
tree->name[name_len] = '\0';
/* Blank-terminated ASCII decimal number of entries in this tree */
if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
/* Invalidated TREE. Free the tree but report success */
if (count == -1) {
/* FIXME: return buffer_end or the end position for
* this single tree entry */
*buffer_in = buffer_end;
*out = NULL;
git_tree_cache_free(tree); /* Needs to be done manually */
return GIT_SUCCESS;
}
tree->entries = count;
if (*buffer != ' ' || ++buffer >= buffer_end) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
/* Number of children of the tree, newline-terminated */
if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS ||
count < 0) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
tree->children_count = count;
if (*buffer != '\n' || ++buffer >= buffer_end) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
/* 160-bit SHA-1 for this tree and it's children */
if (buffer + GIT_OID_RAWSZ > buffer_end) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
buffer += GIT_OID_RAWSZ;
/* Parse children: */
if (tree->children_count > 0) {
unsigned int i;
int err;
tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *));
if (tree->children == NULL)
goto cleanup;
for (i = 0; i < tree->children_count; ++i) {
err = read_tree_internal(&tree->children[i], &buffer, buffer_end, tree);
if (err < GIT_SUCCESS)
goto cleanup;
}
}
*buffer_in = buffer;
*out = tree;
return GIT_SUCCESS;
cleanup:
git_tree_cache_free(tree);
return error;
}
int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size)
{
const char *buffer_end = buffer + buffer_size;
int error;
error = read_tree_internal(tree, &buffer, buffer_end, NULL);
if (buffer < buffer_end)
return GIT_EOBJCORRUPTED;
return error;
}
void git_tree_cache_free(git_tree_cache *tree)
{
unsigned int i;
if (tree == NULL)
return;
for (i = 0; i < tree->children_count; ++i)
git_tree_cache_free(tree->children[i]);
free(tree->children);
free(tree);
}