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: tool.c,v 1.1 2010/03/24 16:00:15 misho Exp $
7: *
8: *************************************************************************/
9: #include "global.h"
10: #include "tool.h"
11:
12:
13: /*
14: * sync_mksig() Make signature from chunk
15: * @id = chunk id
16: * @off = file offset
17: * @buf = chunk buffer for calculate signature
18: * @buflen = buffer length in bytes
19: * @sc = Chunk structure for fill
20: */
21: inline void sync_mksig(int id, off_t off, u_char * __restrict buf, int buflen, sync_chunk_t * __restrict sc)
22: {
23: MD5_CTX ctx;
24:
25: MD5_Init(&ctx);
26: memset(sc, 0, sizeof(sync_chunk_t));
27: sc->sc_magic = DLTSYNC_MAGIC;
28: sc->sc_id = id;
29: sc->sc_off = off;
30: sc->sc_len = buflen;
31: sc->sc_roll = crcAdler(buf, buflen);
32: MD5_Update(&ctx, buf, buflen);
33: MD5_Final(sc->sc_cksum, &ctx);
34: }
35:
36: /*
37: * syncWriteNum() Write to handle converted number to bigendian
38: * @f = handle
39: * @ulNum = number to convert
40: * @nNumLen = length of number to bytes
41: * return: -1 error, != -1 writed bytes to handle
42: */
43: inline int syncWriteNum(int f, u_long ulNum, int nNumLen)
44: {
45: u_char buf[sizeof ulNum];
46: register int i;
47:
48: if (nNumLen < 1 || nNumLen > ULONG_MAX) {
49: syncSetErr(-1, "Illegal number len %d\n", nNumLen);
50: return -1;
51: }
52:
53: for (i = nNumLen - 1; i; i--) {
54: buf[i] = (u_char) ulNum;
55: ulNum >>= 8;
56: }
57:
58: return write(f, buf, nNumLen);
59: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>