File: _pack.c

package info (click to toggle)
dulwich 0.6.1-1+deb6u1
  • links: PTS
  • area: main
  • in suites: squeeze-lts
  • size: 1,056 kB
  • ctags: 1,590
  • sloc: python: 8,704; ansic: 389; makefile: 50
file content (242 lines) | stat: -rw-r--r-- 6,158 bytes parent folder | download | duplicates (2)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* 
 * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License or (at your option) a later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

#include <Python.h>
#include <stdint.h>

static int py_is_sha(PyObject *sha)
{
    if (!PyString_CheckExact(sha))
        return 0;

    if (PyString_Size(sha) != 20)
        return 0;

    return 1;
}


static size_t get_delta_header_size(uint8_t *delta, int *index, int length)
{
	size_t size = 0;
	int i = 0;
	while ((*index) < length) {
		uint8_t cmd = delta[*index];
		(*index)++;
		size |= (cmd & ~0x80) << i;
		i += 7;
		if (!(cmd & 0x80))
			break;
	}
	return size;
}

static PyObject *py_chunked_as_string(PyObject *py_buf)
{
	if (PyList_Check(py_buf)) {
		PyObject *sep = PyString_FromString("");
		if (sep == NULL) {
			PyErr_NoMemory();
			return NULL;
		}
		py_buf = _PyString_Join(sep, py_buf);
		Py_DECREF(sep);
		if (py_buf == NULL) {
			PyErr_NoMemory();
			return NULL;
		}
	} else if (PyString_Check(py_buf)) {
		Py_INCREF(py_buf);
	} else {
		PyErr_SetString(PyExc_TypeError,
			"src_buf is not a string or a list of chunks");
		return NULL;
	}
    return py_buf;
}

static PyObject *py_apply_delta(PyObject *self, PyObject *args)
{
	uint8_t *src_buf, *delta;
	int src_buf_len, delta_len;
	size_t src_size, dest_size;
	size_t outindex = 0;
	int index;
	uint8_t *out;
	PyObject *ret, *py_src_buf, *py_delta;

	if (!PyArg_ParseTuple(args, "OO", &py_src_buf, &py_delta))
		return NULL;

    py_src_buf = py_chunked_as_string(py_src_buf);
    if (py_src_buf == NULL)
        return NULL;

    py_delta = py_chunked_as_string(py_delta);
    if (py_delta == NULL) {
        Py_DECREF(py_src_buf);
        return NULL;
    }

	src_buf = (uint8_t *)PyString_AS_STRING(py_src_buf);
	src_buf_len = PyString_GET_SIZE(py_src_buf);

    delta = (uint8_t *)PyString_AS_STRING(py_delta);
    delta_len = PyString_GET_SIZE(py_delta);

    index = 0;
    src_size = get_delta_header_size(delta, &index, delta_len);
    if (src_size != src_buf_len) {
		PyErr_Format(PyExc_ValueError, 
			"Unexpected source buffer size: %lu vs %d", src_size, src_buf_len);
		Py_DECREF(py_src_buf);
		Py_DECREF(py_delta);
		return NULL;
	}
    dest_size = get_delta_header_size(delta, &index, delta_len);
	ret = PyString_FromStringAndSize(NULL, dest_size);
	if (ret == NULL) {
		PyErr_NoMemory();
		Py_DECREF(py_src_buf);
		Py_DECREF(py_delta);
		return NULL;
	}
	out = (uint8_t *)PyString_AsString(ret);
    while (index < delta_len) {
        char cmd = delta[index];
        index++;
        if (cmd & 0x80) {
            size_t cp_off = 0, cp_size = 0;
			int i;
            for (i = 0; i < 4; i++) {
                if (cmd & (1 << i)) {
                    uint8_t x = delta[index];
                    index++;
                    cp_off |= x << (i * 8);
				}
			}
            for (i = 0; i < 3; i++) {
                if (cmd & (1 << (4+i))) {
                    uint8_t x = delta[index];
                    index++;
                    cp_size |= x << (i * 8);
				}
			}
            if (cp_size == 0)
                cp_size = 0x10000;
            if (cp_off + cp_size < cp_size ||
                cp_off + cp_size > src_size ||
                cp_size > dest_size)
                break;
			memcpy(out+outindex, src_buf+cp_off, cp_size);
			outindex += cp_size;
			dest_size -= cp_size;
		} else if (cmd != 0) {
			if (cmd > dest_size)
				break;
			memcpy(out+outindex, delta+index, cmd);
			outindex += cmd;
			index += cmd;
			dest_size -= cmd;
		} else {
			PyErr_SetString(PyExc_ValueError, "Invalid opcode 0");
			Py_DECREF(ret);
            Py_DECREF(py_delta);
			Py_DECREF(py_src_buf);
			return NULL;
		}
	}
	Py_DECREF(py_src_buf);
    Py_DECREF(py_delta);
    
    if (index != delta_len) {
		PyErr_SetString(PyExc_ValueError, "delta not empty");
		Py_DECREF(ret);
		return NULL;
	}

	if (dest_size != 0) {
        PyErr_SetString(PyExc_ValueError, "dest size incorrect");
		Py_DECREF(ret);
		return NULL;
	}

    return Py_BuildValue("[N]", ret);
}

static PyObject *py_bisect_find_sha(PyObject *self, PyObject *args)
{
    PyObject *unpack_name;
    char *sha;
    int sha_len;
	int start, end;
    if (!PyArg_ParseTuple(args, "iis#O", &start, &end, 
						  &sha, &sha_len, &unpack_name))
        return NULL;

    if (sha_len != 20) {
        PyErr_SetString(PyExc_ValueError, "Sha is not 20 bytes long");
        return NULL;
    }
    if (start > end) {
        PyErr_SetString(PyExc_AssertionError, "start > end");
        return NULL;
    }

    while (start <= end) {
        PyObject *file_sha;
        int i = (start + end)/2;
        int cmp;
        file_sha = PyObject_CallFunction(unpack_name, "i", i);
        if (file_sha == NULL) {
            return NULL;
        }
        if (!py_is_sha(file_sha)) {
            PyErr_SetString(PyExc_TypeError, "unpack_name returned non-sha object");
			Py_DECREF(file_sha);
            return NULL;
        }
        cmp = memcmp(PyString_AsString(file_sha), sha, 20);
		Py_DECREF(file_sha);
        if (cmp < 0)
            start = i + 1;
        else if (cmp > 0)
            end = i - 1;
        else {
			return PyInt_FromLong(i);
        }
    }
    Py_RETURN_NONE;
}


static PyMethodDef py_pack_methods[] = {
	{ "apply_delta", (PyCFunction)py_apply_delta, METH_VARARGS, NULL },
	{ "bisect_find_sha", (PyCFunction)py_bisect_find_sha, METH_VARARGS, NULL },
	{ NULL, NULL, 0, NULL }
};

void init_pack(void)
{
	PyObject *m;

	m = Py_InitModule3("_pack", py_pack_methods, NULL);
	if (m == NULL)
		return;
}