File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / tool.c
Revision 1.1: 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: MAIN
CVS tags: HEAD
Initial revision

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: tool.c,v 1.1 2010/03/24 16:00:15 misho Exp $
*
*************************************************************************/
#include "global.h"
#include "tool.h"


/*
 * sync_mksig() Make signature from chunk
 * @id = chunk id
 * @off = file offset
 * @buf = chunk buffer for calculate signature
 * @buflen = buffer length in bytes
 * @sc = Chunk structure for fill
*/
inline void sync_mksig(int id, off_t off, u_char * __restrict buf, int buflen, sync_chunk_t * __restrict sc)
{
	MD5_CTX ctx;

	MD5_Init(&ctx);
	memset(sc, 0, sizeof(sync_chunk_t));
	sc->sc_magic = DLTSYNC_MAGIC;
	sc->sc_id = id;
	sc->sc_off = off;
	sc->sc_len = buflen;
	sc->sc_roll = crcAdler(buf, buflen);
	MD5_Update(&ctx, buf, buflen);
	MD5_Final(sc->sc_cksum, &ctx);
}

/*
 * syncWriteNum() Write to handle converted number to bigendian
 * @f = handle
 * @ulNum = number to convert
 * @nNumLen = length of number to bytes
 * return: -1 error, != -1 writed bytes to handle
*/
inline int syncWriteNum(int f, u_long ulNum, int nNumLen)
{
	u_char buf[sizeof ulNum];
	register int i;

	if (nNumLen < 1 || nNumLen > ULONG_MAX) {
		syncSetErr(-1, "Illegal number len %d\n", nNumLen);
		return -1;
	}

	for (i = nNumLen - 1; i; i--) {
		buf[i] = (u_char) ulNum;
		ulNum >>= 8;
	}

	return write(f, buf, nNumLen);
}

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