Annotation of libaitsync/src/patch.c, revision 1.1

1.1     ! misho       1: #include "global.h"
        !             2: #include "patch.h"
        !             3: 
        !             4: 
        !             5: int sync_buildPatch(void * __restrict buf, u_int buflen, struct tagPiece ** __restrict arr)
        !             6: {
        !             7:        void *pos;
        !             8:        sync_chunk_t *prefix, *suffix;
        !             9:        struct tagPiece *a = *arr;
        !            10:        register int i;
        !            11:        u_int len = buflen;
        !            12: 
        !            13:        if (!buf || !arr)
        !            14:                return -1;
        !            15: 
        !            16:        a = malloc(sizeof(struct tagPiece));
        !            17:        if (!a) {
        !            18:                SETERR;
        !            19:                return -1;
        !            20:        } else
        !            21:                memset(a, 0, sizeof(struct tagPiece));
        !            22: 
        !            23:        for (pos = buf, i = 0; len && pos < buf + buflen; i++) {
        !            24:                a = realloc(a, sizeof(struct tagPiece) * (i + 2));
        !            25:                if (!a) {
        !            26:                        SETERR;
        !            27:                        return -1;
        !            28:                } else {
        !            29:                        memset(&a[i + 1], 0, sizeof(struct tagPiece));
        !            30:                        // printf("%d.prefix_pos=%p len=%d\n", i, pos, len);
        !            31: 
        !            32:                        // prefix chunk
        !            33:                        prefix = pos;
        !            34:                        if (prefix->sc_magic != DLTSYNC_MAGIC) {
        !            35:                                if (a) {
        !            36:                                        free(a);
        !            37:                                        a = NULL;
        !            38:                                }
        !            39:                                return -1;
        !            40:                        } else {
        !            41:                                a[i].pfx = prefix;
        !            42:                                pos += sizeof(sync_chunk_t);
        !            43:                                len -= sizeof(sync_chunk_t);
        !            44:                        }
        !            45:                        // printf("%d.prefix=%p pos=%p len=%d\n", i, a[i].pfx, pos, len);
        !            46: 
        !            47:                        // data
        !            48:                        if (!len) {
        !            49:                                if (a) {
        !            50:                                        free(a);
        !            51:                                        a = NULL;
        !            52:                                }
        !            53:                                return -1;
        !            54:                        } else {
        !            55:                                a[i].buf = pos;
        !            56:                                pos += prefix->sc_len;
        !            57:                                len -= prefix->sc_len;
        !            58:                        }
        !            59:                        // printf("%d.data=%p pos=%p len=%d\n", i, a[i].buf, pos, len);
        !            60: 
        !            61:                        // if find sync chunk
        !            62:                        if (len) {
        !            63:                                suffix = pos;
        !            64:                                if (SIGSYNC_MAGIC == suffix->sc_magic) {
        !            65:                                        a[i].sfx = suffix;
        !            66:                                        pos += sizeof(sync_chunk_t);
        !            67:                                        len -= sizeof(sync_chunk_t);
        !            68:                                }
        !            69:                                // printf("%d.data=%p pos=%p len=%d\n", i, a[i].sfx, pos, len);
        !            70:                        }
        !            71:                }
        !            72:        }
        !            73: 
        !            74:        *arr = a;
        !            75:        return i;
        !            76: }

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