File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / tool.c
Revision 1.1.1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Mon May 9 13:46:12 2011 UTC (13 years, 3 months ago) by misho
Branches: sync1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
under NetBSD mkstemps() function not exists!!!
added this feature for NetBSD systems

    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.1.1.2.2 2011/05/09 13:46:12 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: }
   60: 
   61: #ifndef HAVE_MKSTEMPS
   62: #define TEMPCHARS       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
   63: #define NUM_CHARS       (sizeof(TEMPCHARS) - 1)
   64: 
   65: int
   66: mkstemps(char *path, int slen)
   67: {
   68: 	char *start, *cp, *ep;
   69: 	const char *tempchars = TEMPCHARS;
   70: 	unsigned int tries = 1;
   71: 	size_t len;
   72: 	int fd;
   73: 
   74: 	assert(path);
   75: 	len = strlen(path);
   76: 	if (len == 0 || slen >= len) {
   77: 		errno = EINVAL;
   78: 		goto end;
   79: 	} else
   80: 		ep = path + len - slen;
   81: 
   82: 	for (start = ep; start > path && start[-1] == 'X'; start--) {
   83: 		if (tries < INT_MAX / NUM_CHARS)
   84: 			tries *= NUM_CHARS;
   85: 	}
   86: 	tries *= 2;
   87: 
   88: 	do {
   89: 		for (cp = start; cp != ep; cp++)
   90: 			*cp = tempchars[arc4random_uniform(NUM_CHARS)];
   91: 
   92: 		fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
   93: 		if (fd != -1 || errno != EEXIST)
   94: 			return fd;
   95: 	} while (--tries);
   96: 
   97: 	errno = EEXIST;
   98: end:
   99: 	return -1;
  100: }
  101: #endif

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