diff-delta.con commit Merge branch 'se/rebase' into next (3b24343)
   1/*
   2 * diff-delta.c: generate a delta between two buffers
   3 *
   4 *  Many parts of this file have been lifted from LibXDiff version 0.10.
   5 *  http://www.xmailserver.org/xdiff-lib.html
   6 *
   7 *  LibXDiff was written by Davide Libenzi <davidel@xmailserver.org>
   8 *  Copyright (C) 2003  Davide Libenzi
   9 *
  10 *  Many mods for GIT usage by Nicolas Pitre <nico@cam.org>, (C) 2005.
  11 *
  12 *  This file is free software; you can redistribute it and/or
  13 *  modify it under the terms of the GNU Lesser General Public
  14 *  License as published by the Free Software Foundation; either
  15 *  version 2.1 of the License, or (at your option) any later version.
  16 *
  17 *  Use of this within git automatically means that the LGPL
  18 *  licensing gets turned into GPLv2 within this project.
  19 */
  20
  21#include <stdlib.h>
  22#include <string.h>
  23#include <zlib.h>
  24#include "delta.h"
  25
  26
  27/* block size: min = 16, max = 64k, power of 2 */
  28#define BLK_SIZE 16
  29
  30/* maximum hash entry list for the same hash bucket */
  31#define HASH_LIMIT 64
  32
  33#define GR_PRIME 0x9e370001
  34#define HASH(v, shift) (((unsigned int)(v) * GR_PRIME) >> (shift))
  35
  36struct index_entry {
  37        const unsigned char *ptr;
  38        unsigned int val;
  39        struct index_entry *next;
  40};
  41
  42struct delta_index {
  43        const void *src_buf;
  44        unsigned long src_size;
  45        unsigned int hash_shift;
  46        struct index_entry *hash[0];
  47};
  48
  49struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
  50{
  51        unsigned int i, hsize, hshift, entries, *hash_count;
  52        const unsigned char *data, *buffer = buf;
  53        struct delta_index *index;
  54        struct index_entry *entry, **hash;
  55        void *mem;
  56
  57        if (!buf || !bufsize)
  58                return NULL;
  59
  60        /* determine index hash size */
  61        entries = bufsize  / BLK_SIZE;
  62        hsize = entries / 4;
  63        for (i = 4; (1 << i) < hsize && i < 31; i++);
  64        hsize = 1 << i;
  65        hshift = 32 - i;
  66
  67        /* allocate lookup index */
  68        mem = malloc(sizeof(*index) +
  69                     sizeof(*hash) * hsize +
  70                     sizeof(*entry) * entries);
  71        if (!mem)
  72                return NULL;
  73        index = mem;
  74        mem = index + 1;
  75        hash = mem;
  76        mem = hash + hsize;
  77        entry = mem;
  78
  79        index->src_buf = buf;
  80        index->src_size = bufsize;
  81        index->hash_shift = hshift;
  82        memset(hash, 0, hsize * sizeof(*hash));
  83
  84        /* allocate an array to count hash entries */
  85        hash_count = calloc(hsize, sizeof(*hash_count));
  86        if (!hash_count) {
  87                free(index);
  88                return NULL;
  89        }
  90
  91        /* then populate the index */
  92        data = buffer + entries * BLK_SIZE - BLK_SIZE;
  93        while (data >= buffer) {
  94                unsigned int val = adler32(0, data, BLK_SIZE);
  95                i = HASH(val, hshift);
  96                entry->ptr = data;
  97                entry->val = val;
  98                entry->next = hash[i];
  99                hash[i] = entry++;
 100                hash_count[i]++;
 101                data -= BLK_SIZE;
 102        }
 103
 104        /*
 105         * Determine a limit on the number of entries in the same hash
 106         * bucket.  This guard us against patological data sets causing
 107         * really bad hash distribution with most entries in the same hash
 108         * bucket that would bring us to O(m*n) computing costs (m and n
 109         * corresponding to reference and target buffer sizes).
 110         *
 111         * Make sure none of the hash buckets has more entries than
 112         * we're willing to test.  Otherwise we cull the entry list
 113         * uniformly to still preserve a good repartition across
 114         * the reference buffer.
 115         */
 116        for (i = 0; i < hsize; i++) {
 117                if (hash_count[i] < HASH_LIMIT)
 118                        continue;
 119                entry = hash[i];
 120                do {
 121                        struct index_entry *keep = entry;
 122                        int skip = hash_count[i] / HASH_LIMIT / 2;
 123                        do {
 124                                entry = entry->next;
 125                        } while(--skip && entry);
 126                        keep->next = entry;
 127                } while(entry);
 128        }
 129        free(hash_count);
 130
 131        return index;
 132}
 133
 134void free_delta_index(struct delta_index *index)
 135{
 136        free(index);
 137}
 138
 139/* provide the size of the copy opcode given the block offset and size */
 140#define COPYOP_SIZE(o, s) \
 141    (!!(o & 0xff) + !!(o & 0xff00) + !!(o & 0xff0000) + !!(o & 0xff000000) + \
 142     !!(s & 0xff) + !!(s & 0xff00) + 1)
 143
 144/* the maximum size for any opcode */
 145#define MAX_OP_SIZE COPYOP_SIZE(0xffffffff, 0xffffffff)
 146
 147void *
 148create_delta(const struct delta_index *index,
 149             const void *trg_buf, unsigned long trg_size,
 150             unsigned long *delta_size, unsigned long max_size)
 151{
 152        unsigned int i, outpos, outsize, hash_shift;
 153        int inscnt;
 154        const unsigned char *ref_data, *ref_top, *data, *top;
 155        unsigned char *out;
 156
 157        if (!trg_buf || !trg_size)
 158                return NULL;
 159
 160        outpos = 0;
 161        outsize = 8192;
 162        if (max_size && outsize >= max_size)
 163                outsize = max_size + MAX_OP_SIZE + 1;
 164        out = malloc(outsize);
 165        if (!out)
 166                return NULL;
 167
 168        /* store reference buffer size */
 169        i = index->src_size;
 170        while (i >= 0x80) {
 171                out[outpos++] = i | 0x80;
 172                i >>= 7;
 173        }
 174        out[outpos++] = i;
 175
 176        /* store target buffer size */
 177        i = trg_size;
 178        while (i >= 0x80) {
 179                out[outpos++] = i | 0x80;
 180                i >>= 7;
 181        }
 182        out[outpos++] = i;
 183
 184        ref_data = index->src_buf;
 185        ref_top = ref_data + index->src_size;
 186        data = trg_buf;
 187        top = trg_buf + trg_size;
 188        hash_shift = index->hash_shift;
 189        inscnt = 0;
 190
 191        while (data < top) {
 192                unsigned int moff = 0, msize = 0;
 193                struct index_entry *entry;
 194                unsigned int val = adler32(0, data, BLK_SIZE);
 195                i = HASH(val, hash_shift);
 196                for (entry = index->hash[i]; entry; entry = entry->next) {
 197                        const unsigned char *ref = entry->ptr;
 198                        const unsigned char *src = data;
 199                        unsigned int ref_size = ref_top - ref;
 200                        if (entry->val != val)
 201                                continue;
 202                        if (ref_size > top - src)
 203                                ref_size = top - src;
 204                        if (ref_size > 0x10000)
 205                                ref_size = 0x10000;
 206                        if (ref_size <= msize)
 207                                break;
 208                        while (ref_size-- && *src++ == *ref)
 209                                ref++;
 210                        if (msize < ref - entry->ptr) {
 211                                /* this is our best match so far */
 212                                msize = ref - entry->ptr;
 213                                moff = entry->ptr - ref_data;
 214                        }
 215                }
 216
 217                if (!msize || msize < COPYOP_SIZE(moff, msize)) {
 218                        if (!inscnt)
 219                                outpos++;
 220                        out[outpos++] = *data++;
 221                        inscnt++;
 222                        if (inscnt == 0x7f) {
 223                                out[outpos - inscnt - 1] = inscnt;
 224                                inscnt = 0;
 225                        }
 226                } else {
 227                        unsigned char *op;
 228
 229                        if (inscnt) {
 230                                while (moff && ref_data[moff-1] == data[-1]) {
 231                                        if (msize == 0x10000)
 232                                                break;
 233                                        /* we can match one byte back */
 234                                        msize++;
 235                                        moff--;
 236                                        data--;
 237                                        outpos--;
 238                                        if (--inscnt)
 239                                                continue;
 240                                        outpos--;  /* remove count slot */
 241                                        inscnt--;  /* make it -1 */
 242                                        break;
 243                                }
 244                                out[outpos - inscnt - 1] = inscnt;
 245                                inscnt = 0;
 246                        }
 247
 248                        data += msize;
 249                        op = out + outpos++;
 250                        i = 0x80;
 251
 252                        if (moff & 0xff) { out[outpos++] = moff; i |= 0x01; }
 253                        moff >>= 8;
 254                        if (moff & 0xff) { out[outpos++] = moff; i |= 0x02; }
 255                        moff >>= 8;
 256                        if (moff & 0xff) { out[outpos++] = moff; i |= 0x04; }
 257                        moff >>= 8;
 258                        if (moff & 0xff) { out[outpos++] = moff; i |= 0x08; }
 259
 260                        if (msize & 0xff) { out[outpos++] = msize; i |= 0x10; }
 261                        msize >>= 8;
 262                        if (msize & 0xff) { out[outpos++] = msize; i |= 0x20; }
 263
 264                        *op = i;
 265                }
 266
 267                if (outpos >= outsize - MAX_OP_SIZE) {
 268                        void *tmp = out;
 269                        outsize = outsize * 3 / 2;
 270                        if (max_size && outsize >= max_size)
 271                                outsize = max_size + MAX_OP_SIZE + 1;
 272                        if (max_size && outpos > max_size)
 273                                out = NULL;
 274                        else
 275                                out = realloc(out, outsize);
 276                        if (!out) {
 277                                free(tmp);
 278                                return NULL;
 279                        }
 280                }
 281        }
 282
 283        if (inscnt)
 284                out[outpos - inscnt - 1] = inscnt;
 285
 286        *delta_size = outpos;
 287        return out;
 288}