File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / patch.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 24 16:00:15 2010 UTC (14 years, 3 months ago) by misho
Branches: misho
CVS tags: sync1_0, start
libaitsync Delta patch library

#include "global.h"
#include "patch.h"


int sync_buildPatch(void * __restrict buf, u_int buflen, struct tagPiece ** __restrict arr)
{
	void *pos;
	sync_chunk_t *prefix, *suffix;
	struct tagPiece *a = *arr;
	register int i;
	u_int len = buflen;

	if (!buf || !arr)
		return -1;

	a = malloc(sizeof(struct tagPiece));
	if (!a) {
		SETERR;
		return -1;
	} else
		memset(a, 0, sizeof(struct tagPiece));

	for (pos = buf, i = 0; len && pos < buf + buflen; i++) {
		a = realloc(a, sizeof(struct tagPiece) * (i + 2));
		if (!a) {
			SETERR;
			return -1;
		} else {
			memset(&a[i + 1], 0, sizeof(struct tagPiece));
			// printf("%d.prefix_pos=%p len=%d\n", i, pos, len);

			// prefix chunk
			prefix = pos;
			if (prefix->sc_magic != DLTSYNC_MAGIC) {
				if (a) {
					free(a);
					a = NULL;
				}
				return -1;
			} else {
				a[i].pfx = prefix;
				pos += sizeof(sync_chunk_t);
				len -= sizeof(sync_chunk_t);
			}
			// printf("%d.prefix=%p pos=%p len=%d\n", i, a[i].pfx, pos, len);

			// data
			if (!len) {
				if (a) {
					free(a);
					a = NULL;
				}
				return -1;
			} else {
				a[i].buf = pos;
				pos += prefix->sc_len;
				len -= prefix->sc_len;
			}
			// printf("%d.data=%p pos=%p len=%d\n", i, a[i].buf, pos, len);

			// if find sync chunk
			if (len) {
				suffix = pos;
				if (SIGSYNC_MAGIC == suffix->sc_magic) {
					a[i].sfx = suffix;
					pos += sizeof(sync_chunk_t);
					len -= sizeof(sync_chunk_t);
				}
				// printf("%d.data=%p pos=%p len=%d\n", i, a[i].sfx, pos, len);
			}
		}
	}

	*arr = a;
	return i;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>