1/*2* LibXDiff by Davide Libenzi ( File Differential Library )3* Copyright (C) 2003 Davide Libenzi4*5* This library is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* This library is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with this library; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*19* Davide Libenzi <davidel@xmailserver.org>20*21*/2223#include "xinclude.h"24252627#define XDL_GUESS_NLINES 2562829303132long xdl_bogosqrt(long n) {33long i;3435/*36* Classical integer square root approximation using shifts.37*/38for (i = 1; n > 0; n >>= 2)39i <<= 1;4041return i;42}434445int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,46xdemitcb_t *ecb) {47int i = 2;48mmbuffer_t mb[3];4950mb[0].ptr = (char *) pre;51mb[0].size = psize;52mb[1].ptr = (char *) rec;53mb[1].size = size;54if (size > 0 && rec[size - 1] != '\n') {55mb[2].ptr = (char *) "\n\\ No newline at end of file\n";56mb[2].size = strlen(mb[2].ptr);57i++;58}59if (ecb->outf(ecb->priv, mb, i) < 0) {6061return -1;62}6364return 0;65}6667void *xdl_mmfile_first(mmfile_t *mmf, long *size)68{69*size = mmf->size;70return mmf->ptr;71}727374void *xdl_mmfile_next(mmfile_t *mmf, long *size)75{76return NULL;77}787980long xdl_mmfile_size(mmfile_t *mmf)81{82return mmf->size;83}848586int xdl_cha_init(chastore_t *cha, long isize, long icount) {8788cha->head = cha->tail = NULL;89cha->isize = isize;90cha->nsize = icount * isize;91cha->ancur = cha->sncur = NULL;92cha->scurr = 0;9394return 0;95}969798void xdl_cha_free(chastore_t *cha) {99chanode_t *cur, *tmp;100101for (cur = cha->head; (tmp = cur) != NULL;) {102cur = cur->next;103xdl_free(tmp);104}105}106107108void *xdl_cha_alloc(chastore_t *cha) {109chanode_t *ancur;110void *data;111112if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) {113if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) {114115return NULL;116}117ancur->icurr = 0;118ancur->next = NULL;119if (cha->tail)120cha->tail->next = ancur;121if (!cha->head)122cha->head = ancur;123cha->tail = ancur;124cha->ancur = ancur;125}126127data = (char *) ancur + sizeof(chanode_t) + ancur->icurr;128ancur->icurr += cha->isize;129130return data;131}132133134void *xdl_cha_first(chastore_t *cha) {135chanode_t *sncur;136137if (!(cha->sncur = sncur = cha->head))138return NULL;139140cha->scurr = 0;141142return (char *) sncur + sizeof(chanode_t) + cha->scurr;143}144145146void *xdl_cha_next(chastore_t *cha) {147chanode_t *sncur;148149if (!(sncur = cha->sncur))150return NULL;151cha->scurr += cha->isize;152if (cha->scurr == sncur->icurr) {153if (!(sncur = cha->sncur = sncur->next))154return NULL;155cha->scurr = 0;156}157158return (char *) sncur + sizeof(chanode_t) + cha->scurr;159}160161162long xdl_guess_lines(mmfile_t *mf) {163long nl = 0, size, tsize = 0;164char const *data, *cur, *top;165166if ((cur = data = xdl_mmfile_first(mf, &size)) != NULL) {167for (top = data + size; nl < XDL_GUESS_NLINES;) {168if (cur >= top) {169tsize += (long) (cur - data);170if (!(cur = data = xdl_mmfile_next(mf, &size)))171break;172top = data + size;173}174nl++;175if (!(cur = memchr(cur, '\n', top - cur)))176cur = top;177else178cur++;179}180tsize += (long) (cur - data);181}182183if (nl && tsize)184nl = xdl_mmfile_size(mf) / (tsize / nl);185186return nl + 1;187}188189int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)190{191int i1, i2;192193if (flags & XDF_IGNORE_WHITESPACE) {194for (i1 = i2 = 0; i1 < s1 && i2 < s2; ) {195if (isspace(l1[i1]))196while (isspace(l1[i1]) && i1 < s1)197i1++;198if (isspace(l2[i2]))199while (isspace(l2[i2]) && i2 < s2)200i2++;201if (i1 < s1 && i2 < s2 && l1[i1++] != l2[i2++])202return 0;203}204return (i1 >= s1 && i2 >= s2);205} else if (flags & XDF_IGNORE_WHITESPACE_CHANGE) {206for (i1 = i2 = 0; i1 < s1 && i2 < s2; ) {207if (isspace(l1[i1])) {208if (!isspace(l2[i2]))209return 0;210while (isspace(l1[i1]) && i1 < s1)211i1++;212while (isspace(l2[i2]) && i2 < s2)213i2++;214} else if (l1[i1++] != l2[i2++])215return 0;216}217return (i1 >= s1 && i2 >= s2);218} else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL) {219for (i1 = i2 = 0; i1 < s1 && i2 < s2; ) {220if (l1[i1] != l2[i2]) {221while (i1 < s1 && isspace(l1[i1]))222i1++;223while (i2 < s2 && isspace(l2[i2]))224i2++;225if (i1 < s1 || i2 < s2)226return 0;227return 1;228}229i1++;230i2++;231}232return i1 >= s1 && i2 >= s2;233} else234return s1 == s2 && !memcmp(l1, l2, s1);235236return 0;237}238239unsigned long xdl_hash_record(char const **data, char const *top, long flags) {240unsigned long ha = 5381;241char const *ptr = *data;242243for (; ptr < top && *ptr != '\n'; ptr++) {244if (isspace(*ptr) && (flags & XDF_WHITESPACE_FLAGS)) {245const char *ptr2 = ptr;246while (ptr + 1 < top && isspace(ptr[1])247&& ptr[1] != '\n')248ptr++;249if (flags & XDF_IGNORE_WHITESPACE_CHANGE250&& ptr[1] != '\n') {251ha += (ha << 5);252ha ^= (unsigned long) ' ';253}254if (flags & XDF_IGNORE_WHITESPACE_AT_EOL255&& ptr[1] != '\n') {256while (ptr2 != ptr + 1) {257ha += (ha << 5);258ha ^= (unsigned long) *ptr2;259ptr2++;260}261}262continue;263}264ha += (ha << 5);265ha ^= (unsigned long) *ptr;266}267*data = ptr < top ? ptr + 1: ptr;268269return ha;270}271272273unsigned int xdl_hashbits(unsigned int size) {274unsigned int val = 1, bits = 0;275276for (; val < size && bits < CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++);277return bits ? bits: 1;278}279280281int xdl_num_out(char *out, long val) {282char *ptr, *str = out;283char buf[32];284285ptr = buf + sizeof(buf) - 1;286*ptr = '\0';287if (val < 0) {288*--ptr = '-';289val = -val;290}291for (; val && ptr > buf; val /= 10)292*--ptr = "0123456789"[val % 10];293if (*ptr)294for (; *ptr; ptr++, str++)295*str = *ptr;296else297*str++ = '0';298*str = '\0';299300return str - out;301}302303304long xdl_atol(char const *str, char const **next) {305long val, base;306char const *top;307308for (top = str; XDL_ISDIGIT(*top); top++);309if (next)310*next = top;311for (val = 0, base = 1, top--; top >= str; top--, base *= 10)312val += base * (long)(*top - '0');313return val;314}315316317int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2,318const char *func, long funclen, xdemitcb_t *ecb) {319int nb = 0;320mmbuffer_t mb;321char buf[128];322323memcpy(buf, "@@ -", 4);324nb += 4;325326nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1);327328if (c1 != 1) {329memcpy(buf + nb, ",", 1);330nb += 1;331332nb += xdl_num_out(buf + nb, c1);333}334335memcpy(buf + nb, " +", 2);336nb += 2;337338nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1);339340if (c2 != 1) {341memcpy(buf + nb, ",", 1);342nb += 1;343344nb += xdl_num_out(buf + nb, c2);345}346347memcpy(buf + nb, " @@", 3);348nb += 3;349if (func && funclen) {350buf[nb++] = ' ';351if (funclen > sizeof(buf) - nb - 1)352funclen = sizeof(buf) - nb - 1;353memcpy(buf + nb, func, funclen);354nb += funclen;355}356buf[nb++] = '\n';357358mb.ptr = buf;359mb.size = nb;360if (ecb->outf(ecb->priv, &mb, 1) < 0)361return -1;362363return 0;364}365