File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / patch.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Sun Jul 22 22:09:47 2012 UTC (11 years, 11 months ago) by misho
Branches: MAIN
CVS tags: sync1_2, SYNC1_1, HEAD
version 1.1

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: patch.c,v 1.3 2012/07/22 22:09:47 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: #include "patch.h"
   48: 
   49: 
   50: /*
   51:  * sync_buildPatch() Build patch from buffer to pieces array
   52:  * @buf = Input buffer
   53:  * @buflen = Buffer length
   54:  * @arr = Array from pieces
   55:  * return: -1 error, != -1 num pieces
   56: */
   57: int
   58: sync_buildPatch(void * __restrict buf, u_int buflen, struct tagPiece ** __restrict arr)
   59: {
   60: 	void *pos;
   61: 	sync_chunk_t *prefix, *suffix;
   62: 	struct tagPiece *a = *arr;
   63: 	register int i;
   64: 	u_int len = buflen;
   65: 
   66: 	if (!buf || !arr)
   67: 		return -1;
   68: 
   69: 	a = malloc(sizeof(struct tagPiece));
   70: 	if (!a) {
   71: 		SETERR;
   72: 		return -1;
   73: 	} else
   74: 		memset(a, 0, sizeof(struct tagPiece));
   75: 
   76: 	for (pos = buf, i = 0; len && pos < buf + buflen; i++) {
   77: 		a = realloc(a, sizeof(struct tagPiece) * (i + 2));
   78: 		if (!a) {
   79: 			SETERR;
   80: 			return -1;
   81: 		} else {
   82: 			memset(&a[i + 1], 0, sizeof(struct tagPiece));
   83: 			// printf("%d.prefix_pos=%p len=%d\n", i, pos, len);
   84: 
   85: 			// prefix chunk
   86: 			prefix = pos;
   87: 			if (prefix->sc_magic != DLTSYNC_MAGIC) {
   88: 				if (a) {
   89: 					free(a);
   90: 					a = NULL;
   91: 				}
   92: 				return -1;
   93: 			} else {
   94: 				a[i].pfx = prefix;
   95: 				pos += sizeof(sync_chunk_t);
   96: 				len -= sizeof(sync_chunk_t);
   97: 			}
   98: 			// printf("%d.prefix=%p pos=%p len=%d\n", i, a[i].pfx, pos, len);
   99: 
  100: 			// data
  101: 			if (!len) {
  102: 				if (a) {
  103: 					free(a);
  104: 					a = NULL;
  105: 				}
  106: 				return -1;
  107: 			} else {
  108: 				a[i].buf = pos;
  109: 				pos += prefix->sc_len;
  110: 				len -= prefix->sc_len;
  111: 			}
  112: 			// printf("%d.data=%p pos=%p len=%d\n", i, a[i].buf, pos, len);
  113: 
  114: 			// if find sync chunk
  115: 			if (len) {
  116: 				suffix = pos;
  117: 				if (SIGSYNC_MAGIC == suffix->sc_magic) {
  118: 					a[i].sfx = suffix;
  119: 					pos += sizeof(sync_chunk_t);
  120: 					len -= sizeof(sync_chunk_t);
  121: 				}
  122: 				// printf("%d.data=%p pos=%p len=%d\n", i, a[i].sfx, pos, len);
  123: 			}
  124: 		}
  125: 	}
  126: 
  127: 	*arr = a;
  128: 	return i;
  129: }

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