diff options
Diffstat (limited to 'src/utilfuns')
34 files changed, 1204 insertions, 269 deletions
diff --git a/src/utilfuns/Greek2Greek.cpp b/src/utilfuns/Greek2Greek.cpp index 7e81f0f..d39e00b 100644 --- a/src/utilfuns/Greek2Greek.cpp +++ b/src/utilfuns/Greek2Greek.cpp @@ -22,8 +22,8 @@ //***************************************************************************** #include <stdio.h> -#include <string.h> #include <ctype.h> +#include <string.h> #include "Greek2Greek.h" #include "GreekChars.h" @@ -34,6 +34,8 @@ // method of transliteration. //***************************************************************************** +SWORD_NAMESPACE_START + unsigned char Greek2bGreek( unsigned char *sResult, unsigned char *sGreekText, @@ -899,3 +901,5 @@ int main() } #endif // __TEST + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/Makefile.am b/src/utilfuns/Makefile.am index e7b2258..2f8c78c 100644 --- a/src/utilfuns/Makefile.am +++ b/src/utilfuns/Makefile.am @@ -2,9 +2,21 @@ utilfunsdir = $(top_srcdir)/src/utilfuns libsword_la_SOURCES += $(utilfunsdir)/Greek2Greek.cpp libsword_la_SOURCES += $(utilfunsdir)/utilstr.cpp +libsword_la_SOURCES += $(utilfunsdir)/utilweb.cpp +libsword_la_SOURCES += $(utilfunsdir)/utilxml.cpp libsword_la_SOURCES += $(utilfunsdir)/unixstr.cpp libsword_la_SOURCES += $(utilfunsdir)/swunicod.cpp libsword_la_SOURCES += $(utilfunsdir)/swversion.cpp +libsword_la_SOURCES += $(utilfunsdir)/swbuf.cpp +libsword_la_SOURCES += $(utilfunsdir)/ftpparse.c + +if ZLIB +UNTGZ = $(utilfunsdir)/zlib/untgz.c +else +UNTGZ = +endif +libsword_la_SOURCES += $(UNTGZ) + if MINGW SWREGEX = $(utilfunsdir)/regex.c @@ -14,6 +26,6 @@ endif libsword_la_SOURCES += $(SWREGEX) -libsword_la_SOURCES += $(utilfunsdir)/roman.c +libsword_la_SOURCES += $(utilfunsdir)/roman.cpp diff --git a/src/utilfuns/ftpparse.c b/src/utilfuns/ftpparse.c new file mode 100644 index 0000000..58a692e --- /dev/null +++ b/src/utilfuns/ftpparse.c @@ -0,0 +1,446 @@ +/* ftpparse.c, ftpparse.h: library for parsing FTP LIST responses +20001223 +D. J. Bernstein, djb@cr.yp.to +http://cr.yp.to/ftpparse.html + +Commercial use is fine, if you let me know what programs you're using this in. + +Currently covered formats: +EPLF. +UNIX ls, with or without gid. +Microsoft FTP Service. +Windows NT FTP Server. +VMS. +WFTPD. +NetPresenz (Mac). +NetWare. +MSDOS. + +Definitely not covered: +Long VMS filenames, with information split across two lines. +NCSA Telnet FTP server. Has LIST = NLST (and bad NLST for directories). +*/ + +#include <ftpparse.h> + +static long totai(long year,long month,long mday) +{ + long result; + if (month >= 2) month -= 2; + else { month += 10; --year; } + result = (mday - 1) * 10 + 5 + 306 * month; + result /= 10; + if (result == 365) { year -= 3; result = 1460; } + else result += 365 * (year % 4); + year /= 4; + result += 1461 * (year % 25); + year /= 25; + if (result == 36524) { year -= 3; result = 146096; } + else { result += 36524 * (year % 4); } + year /= 4; + result += 146097 * (year - 5); + result += 11017; + return result * 86400; +} + +static int flagneedbase = 1; +static time_t base; /* time() value on this OS at the beginning of 1970 TAI */ +static long now; /* current time */ +static int flagneedcurrentyear = 1; +static long currentyear; /* approximation to current year */ + +static void initbase(void) +{ + struct tm *t; + if (!flagneedbase) return; + + base = 0; + t = gmtime(&base); + base = -(totai(t->tm_year + 1900,t->tm_mon,t->tm_mday) + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec); + /* assumes the right time_t, counting seconds. */ + /* base may be slightly off if time_t counts non-leap seconds. */ + flagneedbase = 0; +} + +static void initnow(void) +{ + long day; + long year; + + initbase(); + now = time((time_t *) 0) - base; + + if (flagneedcurrentyear) { + day = now / 86400; + if ((now % 86400) < 0) --day; + day -= 11017; + year = 5 + day / 146097; + day = day % 146097; + if (day < 0) { day += 146097; --year; } + year *= 4; + if (day == 146096) { year += 3; day = 36524; } + else { year += day / 36524; day %= 36524; } + year *= 25; + year += day / 1461; + day %= 1461; + year *= 4; + if (day == 1460) { year += 3; day = 365; } + else { year += day / 365; day %= 365; } + day *= 10; + if ((day + 5) / 306 >= 10) ++year; + currentyear = year; + flagneedcurrentyear = 0; + } +} + +/* UNIX ls does not show the year for dates in the last six months. */ +/* So we have to guess the year. */ +/* Apparently NetWare uses ``twelve months'' instead of ``six months''; ugh. */ +/* Some versions of ls also fail to show the year for future dates. */ +static long guesstai(long month,long mday) +{ + long year; + long t; + + initnow(); + + for (year = currentyear - 1;year < currentyear + 100;++year) { + t = totai(year,month,mday); + if (now - t < 350 * 86400) + return t; + } +} + +static int check(char *buf,char *monthname) +{ + if ((buf[0] != monthname[0]) && (buf[0] != monthname[0] - 32)) return 0; + if ((buf[1] != monthname[1]) && (buf[1] != monthname[1] - 32)) return 0; + if ((buf[2] != monthname[2]) && (buf[2] != monthname[2] - 32)) return 0; + return 1; +} + +static char *months[12] = { + "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" +} ; + +static int getmonth(char *buf,int len) +{ + int i; + if (len == 3) + for (i = 0;i < 12;++i) + if (check(buf,months[i])) return i; + return -1; +} + +static long getlong(char *buf,int len) +{ + long u = 0; + while (len-- > 0) + u = u * 10 + (*buf++ - '0'); + return u; +} + +int ftpparse(struct ftpparse *fp,char *buf,int len) +{ + int i; + int j; + int state; + long size; + long year; + long month; + long mday; + long hour; + long minute; + + fp->name = 0; + fp->namelen = 0; + fp->flagtrycwd = 0; + fp->flagtryretr = 0; + fp->sizetype = FTPPARSE_SIZE_UNKNOWN; + fp->size = 0; + fp->mtimetype = FTPPARSE_MTIME_UNKNOWN; + fp->mtime = 0; + fp->idtype = FTPPARSE_ID_UNKNOWN; + fp->id = 0; + fp->idlen = 0; + + if (len < 2) /* an empty name in EPLF, with no info, could be 2 chars */ + return 0; + + switch(*buf) { + /* see http://pobox.com/~djb/proto/eplf.txt */ + /* "+i8388621.29609,m824255902,/,\tdev" */ + /* "+i8388621.44468,m839956783,r,s10376,\tRFCEPLF" */ + case '+': + i = 1; + for (j = 1;j < len;++j) { + if (buf[j] == 9) { + fp->name = buf + j + 1; + fp->namelen = len - j - 1; + return 1; + } + if (buf[j] == ',') { + switch(buf[i]) { + case '/': + fp->flagtrycwd = 1; + break; + case 'r': + fp->flagtryretr = 1; + break; + case 's': + fp->sizetype = FTPPARSE_SIZE_BINARY; + fp->size = getlong(buf + i + 1,j - i - 1); + break; + case 'm': + fp->mtimetype = FTPPARSE_MTIME_LOCAL; + initbase(); + fp->mtime = base + getlong(buf + i + 1,j - i - 1); + break; + case 'i': + fp->idtype = FTPPARSE_ID_FULL; + fp->id = buf + i + 1; + fp->idlen = j - i - 1; + } + i = j + 1; + } + } + return 0; + + /* UNIX-style listing, without inum and without blocks */ + /* "-rw-r--r-- 1 root other 531 Jan 29 03:26 README" */ + /* "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc" */ + /* "dr-xr-xr-x 2 root 512 Apr 8 1994 etc" */ + /* "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin" */ + /* Also produced by Microsoft's FTP servers for Windows: */ + /* "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z" */ + /* "d--------- 1 owner group 0 May 9 19:45 Softlib" */ + /* Also WFTPD for MSDOS: */ + /* "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp" */ + /* Also NetWare: */ + /* "d [R----F--] supervisor 512 Jan 16 18:53 login" */ + /* "- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe" */ + /* Also NetPresenz for the Mac: */ + /* "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit" */ + /* "drwxrwxr-x folder 2 May 10 1996 network" */ + case 'b': + case 'c': + case 'd': + case 'l': + case 'p': + case 's': + case '-': + + if (*buf == 'd') fp->flagtrycwd = 1; + if (*buf == '-') fp->flagtryretr = 1; + if (*buf == 'l') fp->flagtrycwd = fp->flagtryretr = 1; + + state = 1; + i = 0; + for (j = 1;j < len;++j) + if ((buf[j] == ' ') && (buf[j - 1] != ' ')) { + switch(state) { + case 1: /* skipping perm */ + state = 2; + break; + case 2: /* skipping nlink */ + state = 3; + if ((j - i == 6) && (buf[i] == 'f')) /* for NetPresenz */ + state = 4; + break; + case 3: /* skipping uid */ + state = 4; + break; + case 4: /* getting tentative size */ + size = getlong(buf + i,j - i); + state = 5; + break; + case 5: /* searching for month, otherwise getting tentative size */ + month = getmonth(buf + i,j - i); + if (month >= 0) + state = 6; + else + size = getlong(buf + i,j - i); + break; + case 6: /* have size and month */ + mday = getlong(buf + i,j - i); + state = 7; + break; + case 7: /* have size, month, mday */ + if ((j - i == 4) && (buf[i + 1] == ':')) { + hour = getlong(buf + i,1); + minute = getlong(buf + i + 2,2); + fp->mtimetype = FTPPARSE_MTIME_REMOTEMINUTE; + initbase(); + fp->mtime = base + guesstai(month,mday) + hour * 3600 + minute * 60; + } else if ((j - i == 5) && (buf[i + 2] == ':')) { + hour = getlong(buf + i,2); + minute = getlong(buf + i + 3,2); + fp->mtimetype = FTPPARSE_MTIME_REMOTEMINUTE; + initbase(); + fp->mtime = base + guesstai(month,mday) + hour * 3600 + minute * 60; + } + else if (j - i >= 4) { + year = getlong(buf + i,j - i); + fp->mtimetype = FTPPARSE_MTIME_REMOTEDAY; + initbase(); + fp->mtime = base + totai(year,month,mday); + } + else + return 0; + fp->name = buf + j + 1; + fp->namelen = len - j - 1; + state = 8; + break; + case 8: /* twiddling thumbs */ + break; + } + i = j + 1; + while ((i < len) && (buf[i] == ' ')) ++i; + } + + if (state != 8) + return 0; + + fp->size = size; + fp->sizetype = FTPPARSE_SIZE_BINARY; + + if (*buf == 'l') + for (i = 0;i + 3 < fp->namelen;++i) + if (fp->name[i] == ' ') + if (fp->name[i + 1] == '-') + if (fp->name[i + 2] == '>') + if (fp->name[i + 3] == ' ') { + fp->namelen = i; + break; + } + + /* eliminate extra NetWare spaces */ + if ((buf[1] == ' ') || (buf[1] == '[')) + if (fp->namelen > 3) + if (fp->name[0] == ' ') + if (fp->name[1] == ' ') + if (fp->name[2] == ' ') { + fp->name += 3; + fp->namelen -= 3; + } + + return 1; + } + + /* MultiNet (some spaces removed from examples) */ + /* "00README.TXT;1 2 30-DEC-1996 17:44 [SYSTEM] (RWED,RWED,RE,RE)" */ + /* "CORE.DIR;1 1 8-SEP-1996 16:09 [SYSTEM] (RWE,RWE,RE,RE)" */ + /* and non-MutliNet VMS: */ + /* "CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12 [ANONYMOU,ANONYMOUS] (RWED,RWED,,)" */ + for (i = 0;i < len;++i) + if (buf[i] == ';') + break; + if (i < len) { + fp->name = buf; + fp->namelen = i; + if (i > 4) + if (buf[i - 4] == '.') + if (buf[i - 3] == 'D') + if (buf[i - 2] == 'I') + if (buf[i - 1] == 'R') { + fp->namelen -= 4; + fp->flagtrycwd = 1; + } + if (!fp->flagtrycwd) + fp->flagtryretr = 1; + while (buf[i] != ' ') if (++i == len) return 0; + while (buf[i] == ' ') if (++i == len) return 0; + while (buf[i] != ' ') if (++i == len) return 0; + while (buf[i] == ' ') if (++i == len) return 0; + j = i; + while (buf[j] != '-') if (++j == len) return 0; + mday = getlong(buf + i,j - i); + while (buf[j] == '-') if (++j == len) return 0; + i = j; + while (buf[j] != '-') if (++j == len) return 0; + month = getmonth(buf + i,j - i); + if (month < 0) return 0; + while (buf[j] == '-') if (++j == len) return 0; + i = j; + while (buf[j] != ' ') if (++j == len) return 0; + year = getlong(buf + i,j - i); + while (buf[j] == ' ') if (++j == len) return 0; + i = j; + while (buf[j] != ':') if (++j == len) return 0; + hour = getlong(buf + i,j - i); + while (buf[j] == ':') if (++j == len) return 0; + i = j; + while ((buf[j] != ':') && (buf[j] != ' ')) if (++j == len) return 0; + minute = getlong(buf + i,j - i); + + fp->mtimetype = FTPPARSE_MTIME_REMOTEMINUTE; + initbase(); + fp->mtime = base + totai(year,month,mday) + hour * 3600 + minute * 60; + + return 1; + } + + /* MSDOS format */ + /* 04-27-00 09:09PM <DIR> licensed */ + /* 07-18-00 10:16AM <DIR> pub */ + /* 04-14-00 03:47PM 589 readme.htm */ + if ((*buf >= '0') && (*buf <= '9')) { + i = 0; + j = 0; + while (buf[j] != '-') if (++j == len) return 0; + month = getlong(buf + i,j - i) - 1; + while (buf[j] == '-') if (++j == len) return 0; + i = j; + while (buf[j] != '-') if (++j == len) return 0; + mday = getlong(buf + i,j - i); + while (buf[j] == '-') if (++j == len) return 0; + i = j; + while (buf[j] != ' ') if (++j == len) return 0; + year = getlong(buf + i,j - i); + if (year < 50) year += 2000; + if (year < 1000) year += 1900; + while (buf[j] == ' ') if (++j == len) return 0; + i = j; + while (buf[j] != ':') if (++j == len) return 0; + hour = getlong(buf + i,j - i); + while (buf[j] == ':') if (++j == len) return 0; + i = j; + while ((buf[j] != 'A') && (buf[j] != 'P')) if (++j == len) return 0; + minute = getlong(buf + i,j - i); + if (hour == 12) hour = 0; + if (buf[j] == 'A') if (++j == len) return 0; + if (buf[j] == 'P') { hour += 12; if (++j == len) return 0; } + if (buf[j] == 'M') if (++j == len) return 0; + + while (buf[j] == ' ') if (++j == len) return 0; + if (buf[j] == '<') { + fp->flagtrycwd = 1; + while (buf[j] != ' ') if (++j == len) return 0; + } + else { + i = j; + while (buf[j] != ' ') if (++j == len) return 0; + fp->size = getlong(buf + i,j - i); + fp->sizetype = FTPPARSE_SIZE_BINARY; + fp->flagtryretr = 1; + } + while (buf[j] == ' ') if (++j == len) return 0; + + fp->name = buf + j; + fp->namelen = len - j; + + fp->mtimetype = FTPPARSE_MTIME_REMOTEMINUTE; + initbase(); + fp->mtime = base + totai(year,month,mday) + hour * 3600 + minute * 60; + + return 1; + } + + /* Some useless lines, safely ignored: */ + /* "Total of 11 Files, 10966 Blocks." (VMS) */ + /* "total 14786" (UNIX) */ + /* "DISK$ANONFTP:[ANONYMOUS]" (VMS) */ + /* "Directory DISK$PCSA:[ANONYM]" (VMS) */ + + return 0; +} diff --git a/src/utilfuns/roman.c b/src/utilfuns/roman.cpp index 3c6d190..85f361d 100644 --- a/src/utilfuns/roman.c +++ b/src/utilfuns/roman.cpp @@ -22,17 +22,20 @@ #include <stdlib.h> #include <string.h> #include <roman.h> + +SWORD_NAMESPACE_START + char isroman (const char* str) { char * ch = (char*)str; - for (; *ch; ch++) - if (!strchr ("IVXLCDMivxlcdm ", *ch)) + for (; *ch; ch++) + if (!strchr("IVXLCDMivxlcdm ", *ch)) return 0; return 1; } int from_rom(const char* str) { int i, n = strlen(str); - short * num= calloc(n, sizeof(short)); + short * num= (short *) calloc(n, sizeof(short)); for (i = 0; str[i]; i++) { switch(str[i]) { case 'i': @@ -80,3 +83,5 @@ int from_rom(const char* str) { free(num); return n; } + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/swbuf.cpp b/src/utilfuns/swbuf.cpp new file mode 100644 index 0000000..18f966e --- /dev/null +++ b/src/utilfuns/swbuf.cpp @@ -0,0 +1,163 @@ +/****************************************************************************** +* swbuf.cpp - code for SWBuf used as a transport and utility for data buffers +* +* $Id: swbuf.cpp,v 1.14 2003/08/13 03:56:14 scribe Exp $ +* +* Copyright 2003 CrossWire Bible Society (http://www.crosswire.org) +* CrossWire Bible Society +* P. O. Box 2528 +* Tempe, AZ 85280-2528 +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation version 2. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +*/ + +#include <swbuf.h> + +#include <stdlib.h> +#include <stdarg.h> +#include <stdio.h> + +SWORD_NAMESPACE_START + +char *SWBuf::nullStr = ""; +char SWBuf::junkBuf[JUNKBUFSIZE]; + +/****************************************************************************** +* SWBuf Constructor - Creates an empty SWBuf object or an SWBuf initialized +* to a value from a const char * +* +*/ +SWBuf::SWBuf(const char *initVal, unsigned long initSize) { + init(initSize); + set(initVal); +} + +/****************************************************************************** +* SWBuf Constructor - Creates an SWBuf initialized +* to a value from another SWBuf +* +*/ +SWBuf::SWBuf(const SWBuf &other, unsigned long initSize) { + init(initSize); + set(other); +} + +/****************************************************************************** +* SWBuf Constructor - Creates an SWBuf initialized +* to a value from a char +* +*/ +SWBuf::SWBuf(char initVal, unsigned long initSize) { + init(initSize); + + allocSize = 15; + buf = (char *)calloc(allocSize, 1); + *buf = initVal; + end = buf+1; + endAlloc = buf + allocSize-1; +} + +/* +SWBuf::SWBuf(unsigned long initSize) { + init(initSize); + set((const char *)0); +} +*/ + + +void SWBuf::init(unsigned long initSize) { + fillByte = ' '; + allocSize = 0; + endAlloc = 0; + buf = 0; + end = 0; + if (initSize) + assureSize(initSize); +} + +/****************************************************************************** +* SWBuf Destructor - Cleans up instance of SWBuf +*/ +SWBuf::~SWBuf() { + if (buf) + free(buf); +} + +/****************************************************************************** +* SWBuf::set - sets this buf to a new value +*/ +void SWBuf::set(const char *newVal) { + if (newVal) { + unsigned long len = strlen(newVal) + 1; + assureSize(len); + memcpy(buf, newVal, len); + end = buf + (len - 1); + } + else { + assureSize(1); + end = buf; + *end = 0; + } +} + + +/****************************************************************************** +* SWBuf::set - sets this buf to a new value +*/ +void SWBuf::set(const SWBuf &newVal) { + unsigned long len = newVal.length() + 1; + assureSize(len); + memcpy(buf, newVal.c_str(), len); + end = buf + (len-1); +} + + +/****************************************************************************** +* SWBuf::append - appends a value to the current value of this SWBuf +*/ +void SWBuf::append(const char *str, long max) { + unsigned long len = (max > -1) ? max : strlen(str); + assureMore(++len); + memcpy(end, str, len-1); + end += (len-1); + *end = 0; +} + + +/****************************************************************************** +* SWBuf::setSize - Size this buffer to a specific length +*/ +void SWBuf::setSize(unsigned long len) { + assureSize(len+1); + if ((end - buf) < len) + memset(end, fillByte, len - (end-buf)); + end = buf + len; + *end = 0; +} + +/****************************************************************************** +* SWBuf::appendFormatted - appends formatted strings to the current value of this SWBuf +* WARNING: This function can only write at most +* JUNKBUFSIZE to the string per call. +*/ +void SWBuf::appendFormatted(const char *format, ...) { + va_list argptr; + + va_start(argptr, format); + int len = vsprintf(junkBuf, format, argptr)+1; + va_end(argptr); + assureMore(len); + va_start(argptr, format); + end += vsprintf(end, format, argptr); + va_end(argptr); +} + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/swunicod.cpp b/src/utilfuns/swunicod.cpp index f42fd86..64b40ab 100644 --- a/src/utilfuns/swunicod.cpp +++ b/src/utilfuns/swunicod.cpp @@ -17,6 +17,7 @@ */ #include "swunicod.h" +SWORD_NAMESPACE_START unsigned char* UTF32to8 (unsigned long utf32, unsigned char * utf8) { unsigned int i; for (i = 0; i < 6; i++) utf8[i] = 0; @@ -107,10 +108,6 @@ unsigned char* UTF32to8 (unsigned long utf32, unsigned char * utf8) { return utf8; } -/** Converts a UTF-8 encoded 1-6 byte array into a 32-bit unsigned integer UTF-32 value - * @param utf8 pointer to an array of 6 unsigned chars containing the UTF-8 value, starting in the utf8[0] - * @param utf32 the UTF-32 Unicode code point value - */ unsigned long UTF8to32 (unsigned char * utf8) { unsigned char i = utf8[0]; @@ -137,3 +134,5 @@ unsigned long UTF8to32 (unsigned char * utf8) { } return utf32; } + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/swversion.cpp b/src/utilfuns/swversion.cpp index 14a6f4b..a6c3917 100644 --- a/src/utilfuns/swversion.cpp +++ b/src/utilfuns/swversion.cpp @@ -1,8 +1,9 @@ #include <swversion.h> -#include <string.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> +SWORD_NAMESPACE_START SWVersion SWVersion::currentVersion(SWORDVER); @@ -57,7 +58,7 @@ int SWVersion::compare(const SWVersion &vi) const { } -SWVersion::operator const char *() const { +const char *SWVersion::getText() const { // 255 is safe because there is no way 4 integers (plus 3 '.'s) can have // a string representation that will overrun this buffer @@ -76,3 +77,5 @@ SWVersion::operator const char *() const { return buf; } + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/unixstr.cpp b/src/utilfuns/unixstr.cpp index 7a975a2..bbb0ab7 100644 --- a/src/utilfuns/unixstr.cpp +++ b/src/utilfuns/unixstr.cpp @@ -1,7 +1,10 @@ // Include only if your UNIX compiler does not include stricmp but does include strcasecmp #include <unixstr.h> +#include <string.h> +SWORD_NAMESPACE_START int stricmp(const char *s1, const char *s2) { return strcasecmp(s1, s2); } +SWORD_NAMESPACE_END diff --git a/src/utilfuns/utilconf.cpp b/src/utilfuns/utilconf.cpp index 5a32ca0..b7e333d 100644 --- a/src/utilfuns/utilconf.cpp +++ b/src/utilfuns/utilconf.cpp @@ -1,4 +1,3 @@ -#include <string.h> #include <utilstr.h> @@ -13,7 +12,11 @@ * RET: error status */ +SWORD_NAMESPACE_START + char getconfent(char *filename, char *entryname, char *buf, int len) { return 0; } + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/utilstr.cpp b/src/utilfuns/utilstr.cpp index 366c54f..79904c6 100644 --- a/src/utilfuns/utilstr.cpp +++ b/src/utilfuns/utilstr.cpp @@ -1,6 +1,19 @@ -#include <string.h> #include <utilstr.h> #include <ctype.h> +#include <string.h> + +#ifdef _ICU_ +#include <unicode/utypes.h> +#include <unicode/ucnv.h> +#include <unicode/ustring.h> +#include <unicode/uchar.h> + +#include <unicode/unistr.h> +#include <unicode/translit.h> + +#endif + +SWORD_NAMESPACE_START /****************************************************************************** * stdstr - Sets/gets a string @@ -12,12 +25,12 @@ * RET: *ipstr */ -char *stdstr(char **ipstr, const char *istr) { +char *stdstr(char **ipstr, const char *istr, unsigned int memPadFactor) { if (istr) { if (*ipstr) delete [] *ipstr; int len = strlen(istr) + 1; - *ipstr = new char [ len ]; + *ipstr = new char [ len * memPadFactor ]; memcpy(*ipstr, istr, len); } return *ipstr; @@ -41,8 +54,8 @@ char *strstrip(char *istr) { return istr; rtmp = istr + (len - 1); - while (*rtmp == ' ') *(rtmp--) = 0; - while (*tmp == ' ') tmp++; + while ((rtmp > istr)&&((*rtmp == ' ')||(*rtmp == '\t')||(*rtmp == 10)||(*rtmp == 13))) *(rtmp--) = 0; + while ((*tmp == ' ')||(*tmp == '\t')||(*tmp == 10)||(*tmp == 13)) tmp++; memmove(istr, tmp, (rtmp - tmp) + 1); istr[(rtmp - tmp) + 1] = 0; @@ -95,18 +108,17 @@ const char *stristr(const char *s1, const char *s2) { */ const char strnicmp(const char *s1, const char *s2, int len) { - - int tLen = strlen(s2); - int cLen = strlen(s1); - char diff; - int i; - for (i = 0; ((i < len) && (i < tLen) && (i < cLen)); i++) { - if ((diff = SW_toupper(*s1) - SW_toupper(*s2))) - return diff; + int tLen = strlen(s2); + int cLen = strlen(s1); + char diff; + int i; + for (i = 0; ((i < len) && (i < tLen) && (i < cLen)); i++) { + if ((diff = SW_toupper(*s1) - SW_toupper(*s2))) + return diff; s1++; s2++; - } - return (i < len) ? cLen - tLen : 0; + } + return (i < len) ? cLen - tLen : 0; } /****************************************************************************** @@ -145,8 +157,48 @@ unsigned int strlenw(const char *s1) { char *toupperstr(char *buf) { char *ret = buf; + while (*buf) *buf = SW_toupper(*buf++); return ret; } + + +/****************************************************************************** + * toupperstr - converts a string to uppercase string + * + * ENT: target - string to convert + * + * RET: target + */ + +char *toupperstr_utf8(char *buf, unsigned int max) { + char *ret = buf; + +#ifndef _ICU_ + // try to decide if it's worth trying to toupper. Do we have more + // characters that are probably lower latin than not? + long performOp = 0; + for (const char *ch = buf; *ch; ch++) + performOp += (*ch > 0) ? 1 : -1; + + if (performOp) { + while (*buf) + *buf = SW_toupper(*buf++); + } +#else + if (!max) + max = strlen(ret); + UErrorCode err = U_ZERO_ERROR; + UConverter *conv = ucnv_open("UTF-8", &err); + UnicodeString str(buf, -1, conv, err); + UnicodeString ustr = str.toUpper(); + ustr.extract(ret, max, conv, err); + ucnv_close(conv); +#endif + + return ret; +} + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/utilweb.cpp b/src/utilfuns/utilweb.cpp new file mode 100644 index 0000000..a3dfa0d --- /dev/null +++ b/src/utilfuns/utilweb.cpp @@ -0,0 +1,35 @@ +#include <utilxml.h> +#include <stdio.h> +#include <map> + +SWORD_NAMESPACE_START + +using std::map; + +typedef map<unsigned char,SWBuf> DataMap; + +const SWBuf encodeURL( const SWBuf& url ) { + DataMap m; + for (unsigned short int c = 32; c <= 255; ++c) { //first set all encoding chars + if ( (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || strchr("-_.!~*'()", c)) { + continue; //we don't need an encoding for this char + } + + char s[5]; + sprintf(s, "%-.2X", c); //left-aligned, 2 digits, uppercase hex + m[c] = SWBuf("%") + s; //encoded char is "% + 2 digit hex code of char" + } + //the special encodings for certain chars + m[' '] = '+'; + + SWBuf buf; + const int length = url.length(); + for (int i = 0; i <= length; i++) { //fill "buf" + const char& c = url[i]; + buf += (!m[c].length()) ? (SWBuf)c : (SWBuf)m[c]; + } + + return buf; +} + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/utilxml.cpp b/src/utilfuns/utilxml.cpp new file mode 100644 index 0000000..6914cc9 --- /dev/null +++ b/src/utilfuns/utilxml.cpp @@ -0,0 +1,202 @@ + +#include <utilxml.h> +#include <ctype.h> +#include <utilstr.h> + +SWORD_NAMESPACE_START + +void XMLTag::parse() const { + int i; + int start; + char *name = 0; + char *value = 0; + if (!buf) + return; + for (i = 0; ((buf[i]) && (!isalpha(buf[i]))); i++); + for (; buf[i]; i++) { + if (buf[i] == ' ') { + for (; ((buf[i]) && (!isalpha(buf[i]))); i++); + if (buf[i]) { // we have an attribute name + start = i; + for (; ((buf[i]) && (!strchr(" =", buf[i]))); i++); + if (i-start) { + if (name) + delete [] name; + name = new char [ (i-start) + 1 ]; + strncpy(name, buf+start, i-start); + name[i-start] = 0; + } + for (; ((buf[i]) && (strchr(" =\"", buf[i]))); i++); + if (buf[i]) { // we have attribute value + start = i; + for (; ((buf[i]) && (buf[i] != '\"')); i++); + if (i-start) { + if (value) + delete [] value; + value = new char [ (i-start) + 1 ]; + strncpy(value, buf+start, i-start); + value[i-start] = 0; + attributes[name] = value; + } + } + } + } + if (!buf[i]) + break; + } + for (;i;i--) { + if (buf[i] == '/') + empty = true; + if (!strchr(" \n>\t", buf[i])) + break; + } + + parsed = true; + if (name) delete [] name; + if (value) delete [] value; +} + + +XMLTag::XMLTag(const char *tagString) { + + name = 0; + buf = 0; + setText(tagString); +} + +void XMLTag::setText(const char *tagString) { + parsed = false; + empty = false; + endTag = false; + + if (buf) { + delete [] buf; + buf = 0; + } + + if (!tagString) // assert tagString before proceeding + return; + + stdstr(&buf, tagString); + + int start = 0; + int i; + + // skip beginning silliness + for (i = 0; ((tagString[i]) && (!isalpha(tagString[i]))); i++) { + if (tagString[i] == '/') + endTag = true; + } + start = i; + for (; ((tagString[i]) && (!strchr(" />", tagString[i]))); i++); + if (i-start) { + if (name) + delete [] name; + name = new char [ (i-start) + 1 ]; + strncpy(name, tagString+start, i-start); + name[i-start] = 0; + if (tagString[i] == '/') + empty = true; + } +} + +XMLTag::~XMLTag() { + if (buf) + delete [] buf; + if (name) + delete [] name; +} + +const StringList XMLTag::getAttributeNames() const { + StringList retVal; + + if (!parsed) + parse(); + + for (StringPairMap::iterator it = attributes.begin(); it != attributes.end(); it++) + retVal.push_back(it->first.c_str()); + + return retVal; +} + + +const char *XMLTag::getPart(const char *buf, int partNum, char partSplit) const { + for (; (buf && partNum); partNum--) { + buf = strchr(buf, partSplit); + if (buf) + buf++; + } + if (buf) { + const char *end = strchr(buf, partSplit); + junkBuf = buf; + if (end) + junkBuf.setSize(end - buf); + return junkBuf.c_str(); + } + return 0; +} + + +int XMLTag::getAttributePartCount(const char *attribName, char partSplit) const { + int count; + const char *buf = getAttribute(attribName); + for (count = 0; buf; count++) { + buf = strchr(buf, partSplit); + if (buf) + buf++; + } + return count; +} + + +const char *XMLTag::getAttribute(const char *attribName, int partNum, char partSplit) const { + if (!parsed) + parse(); + + StringPairMap::iterator it = attributes.find(attribName); + const char *retVal = (it == attributes.end()) ? 0 : it->second.c_str(); + if ((retVal) && (partNum > -1)) + retVal = getPart(retVal, partNum, partSplit); + + return retVal; +} + + +const char *XMLTag::setAttribute(const char *attribName, const char *attribValue) { + if (!parsed) + parse(); + if (attribValue) + attributes[attribName] = attribValue; + else attributes.erase(attribName); + return attribValue; +} + +const char *XMLTag::toString() const { + SWBuf tag = "<"; + if (!parsed) + parse(); + + if (isEndTag()) + tag += "/"; + + tag += getName(); + for (StringPairMap::iterator it = attributes.begin(); it != attributes.end(); it++) { + tag.appendFormatted(" %s=\"%s\"", it->first.c_str(), it->second.c_str()); + } + + if (isEmpty()) + tag += "/"; + + tag += ">"; + + + if (buf) + delete [] buf; + buf = new char [ tag.length() + 1 ]; + strcpy(buf, tag.c_str()); + + return buf; +} + + +SWORD_NAMESPACE_END diff --git a/src/utilfuns/zlib/adler32.c b/src/utilfuns/zlib/adler32.c index 14e3abd..f29cf42 100644 --- a/src/utilfuns/zlib/adler32.c +++ b/src/utilfuns/zlib/adler32.c @@ -1,9 +1,9 @@ /* adler32.c -- compute the Adler-32 checksum of a data stream - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ -/* @(#) $Id: adler32.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: adler32.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #include "zlib.h" diff --git a/src/utilfuns/zlib/compress.c b/src/utilfuns/zlib/compress.c index df5fca8..8562957 100644 --- a/src/utilfuns/zlib/compress.c +++ b/src/utilfuns/zlib/compress.c @@ -1,9 +1,9 @@ /* compress.c -- compress a memory buffer - * Copyright (C) 1995-1998 Jean-loup Gailly. + * Copyright (C) 1995-2002 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ -/* @(#) $Id: compress.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: compress.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #include "zlib.h" diff --git a/src/utilfuns/zlib/crc32.c b/src/utilfuns/zlib/crc32.c index fe80e8a..8577b4a 100644 --- a/src/utilfuns/zlib/crc32.c +++ b/src/utilfuns/zlib/crc32.c @@ -1,9 +1,9 @@ /* crc32.c -- compute the CRC-32 of a data stream - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ -/* @(#) $Id: crc32.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: crc32.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #include "zlib.h" diff --git a/src/utilfuns/zlib/deflate.c b/src/utilfuns/zlib/deflate.c index a232eea..1d26f37 100644 --- a/src/utilfuns/zlib/deflate.c +++ b/src/utilfuns/zlib/deflate.c @@ -1,5 +1,5 @@ /* deflate.c -- compress data using the deflation algorithm - * Copyright (C) 1995-1998 Jean-loup Gailly. + * Copyright (C) 1995-2002 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -47,12 +47,12 @@ * */ -/* @(#) $Id: deflate.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: deflate.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #include "deflate.h" const char deflate_copyright[] = - " deflate 1.1.3 Copyright 1995-1998 Jean-loup Gailly "; + " deflate 1.1.4 Copyright 1995-2002 Jean-loup Gailly "; /* If you use the zlib library in a product, an acknowledgment is welcome in the documentation of your product. If for some reason you cannot @@ -242,7 +242,7 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, windowBits = -windowBits; } if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || - windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || + windowBits < 9 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) { return Z_STREAM_ERROR; } diff --git a/src/utilfuns/zlib/deflate.h b/src/utilfuns/zlib/deflate.h index e55d52a..64f6345 100644 --- a/src/utilfuns/zlib/deflate.h +++ b/src/utilfuns/zlib/deflate.h @@ -1,5 +1,5 @@ /* deflate.h -- internal compression state - * Copyright (C) 1995-1998 Jean-loup Gailly + * Copyright (C) 1995-2002 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -8,7 +8,7 @@ subject to change. Applications should only use zlib.h. */ -/* @(#) $Id: deflate.h,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: deflate.h,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #ifndef _DEFLATE_H #define _DEFLATE_H diff --git a/src/utilfuns/zlib/gzio.c b/src/utilfuns/zlib/gzio.c index a2c5b58..645dae1 100644 --- a/src/utilfuns/zlib/gzio.c +++ b/src/utilfuns/zlib/gzio.c @@ -1,11 +1,11 @@ /* gzio.c -- IO on .gz files - * Copyright (C) 1995-1998 Jean-loup Gailly. + * Copyright (C) 1995-2002 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h * * Compile this file with -DNO_DEFLATE to avoid the compression code. */ -/* @(#) $Id: gzio.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: gzio.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #include <stdio.h> diff --git a/src/utilfuns/zlib/infblock.c b/src/utilfuns/zlib/infblock.c index f4920fa..dd7a6d4 100644 --- a/src/utilfuns/zlib/infblock.c +++ b/src/utilfuns/zlib/infblock.c @@ -1,5 +1,5 @@ /* infblock.c -- interpret and process block types to last block - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -249,10 +249,12 @@ int r; &s->sub.trees.tb, s->hufts, z); if (t != Z_OK) { - ZFREE(z, s->sub.trees.blens); r = t; if (r == Z_DATA_ERROR) + { + ZFREE(z, s->sub.trees.blens); s->mode = BAD; + } LEAVE } s->sub.trees.index = 0; @@ -313,11 +315,13 @@ int r; t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), s->sub.trees.blens, &bl, &bd, &tl, &td, s->hufts, z); - ZFREE(z, s->sub.trees.blens); if (t != Z_OK) { if (t == (uInt)Z_DATA_ERROR) + { + ZFREE(z, s->sub.trees.blens); s->mode = BAD; + } r = t; LEAVE } @@ -329,6 +333,7 @@ int r; } s->sub.decode.codes = c; } + ZFREE(z, s->sub.trees.blens); s->mode = CODES; case CODES: UPDATE diff --git a/src/utilfuns/zlib/infblock.h b/src/utilfuns/zlib/infblock.h index bd25c80..173b226 100644 --- a/src/utilfuns/zlib/infblock.h +++ b/src/utilfuns/zlib/infblock.h @@ -1,5 +1,5 @@ /* infblock.h -- header to use infblock.c - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/infcodes.c b/src/utilfuns/zlib/infcodes.c index cfd0807..0d54ea7 100644 --- a/src/utilfuns/zlib/infcodes.c +++ b/src/utilfuns/zlib/infcodes.c @@ -1,5 +1,5 @@ /* infcodes.c -- process literals and length/distance pairs - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -92,10 +92,9 @@ int r; uInt n; /* bytes available there */ Bytef *q; /* output window write pointer */ uInt m; /* bytes to end of window or read pointer */ - Bytef *f; /* pointer to copy strings from */ + unsigned long csf; /* pointer to copy strings from */ inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ - long tryF; -// f = q + /* copy input/output information to locals (UPDATE macro restores) */ LOAD @@ -104,128 +103,120 @@ int r; { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ case START: /* x: set up for LEN */ #ifndef SLOW - if (m >= 258 && n >= 10) - { - UPDATE - r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); - LOAD - if (r != Z_OK) - { - c->mode = r == Z_STREAM_END ? WASH : BADCODE; - break; - } - } + if (m >= 258 && n >= 10) + { + UPDATE + r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); + LOAD + if (r != Z_OK) + { + c->mode = r == Z_STREAM_END ? WASH : BADCODE; + break; + } + } #endif /* !SLOW */ - c->sub.code.need = c->lbits; - c->sub.code.tree = c->ltree; - c->mode = LEN; + c->sub.code.need = c->lbits; + c->sub.code.tree = c->ltree; + c->mode = LEN; case LEN: /* i: get length/literal/eob next */ - j = c->sub.code.need; - NEEDBITS(j) - t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); - DUMPBITS(t->bits) - e = (uInt)(t->exop); - if (e == 0) /* literal */ - { - c->sub.lit = t->base; - Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? - "inflate: literal '%c'\n" : - "inflate: literal 0x%02x\n", t->base)); - c->mode = LIT; - break; - } - if (e & 16) /* length */ - { - c->sub.copy.get = e & 15; - c->len = t->base; - c->mode = LENEXT; - break; - } - if ((e & 64) == 0) /* next table */ - { - c->sub.code.need = e; - c->sub.code.tree = t + t->base; - break; - } - if (e & 32) /* end of block */ - { - Tracevv((stderr, "inflate: end of block\n")); - c->mode = WASH; - break; - } - c->mode = BADCODE; /* invalid code */ - z->msg = (char*)"invalid literal/length code"; - r = Z_DATA_ERROR; - LEAVE + j = c->sub.code.need; + NEEDBITS(j) + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); + DUMPBITS(t->bits) + e = (uInt)(t->exop); + if (e == 0) /* literal */ + { + c->sub.lit = t->base; + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", t->base)); + c->mode = LIT; + break; + } + if (e & 16) /* length */ + { + c->sub.copy.get = e & 15; + c->len = t->base; + c->mode = LENEXT; + break; + } + if ((e & 64) == 0) /* next table */ + { + c->sub.code.need = e; + c->sub.code.tree = t + t->base; + break; + } + if (e & 32) /* end of block */ + { + Tracevv((stderr, "inflate: end of block\n")); + c->mode = WASH; + break; + } + c->mode = BADCODE; /* invalid code */ + z->msg = (char*)"invalid literal/length code"; + r = Z_DATA_ERROR; + LEAVE case LENEXT: /* i: getting length extra (have base) */ - j = c->sub.copy.get; - NEEDBITS(j) - c->len += (uInt)b & inflate_mask[j]; - DUMPBITS(j) - c->sub.code.need = c->dbits; - c->sub.code.tree = c->dtree; - Tracevv((stderr, "inflate: length %u\n", c->len)); - c->mode = DIST; + j = c->sub.copy.get; + NEEDBITS(j) + c->len += (uInt)b & inflate_mask[j]; + DUMPBITS(j) + c->sub.code.need = c->dbits; + c->sub.code.tree = c->dtree; + Tracevv((stderr, "inflate: length %u\n", c->len)); + c->mode = DIST; case DIST: /* i: get distance next */ - j = c->sub.code.need; - NEEDBITS(j) - t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); - DUMPBITS(t->bits) - e = (uInt)(t->exop); - if (e & 16) /* distance */ - { - c->sub.copy.get = e & 15; - c->sub.copy.dist = t->base; - c->mode = DISTEXT; - break; - } - if ((e & 64) == 0) /* next table */ - { - c->sub.code.need = e; - c->sub.code.tree = t + t->base; - break; - } - c->mode = BADCODE; /* invalid code */ - z->msg = (char*)"invalid distance code"; - r = Z_DATA_ERROR; - LEAVE + j = c->sub.code.need; + NEEDBITS(j) + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); + DUMPBITS(t->bits) + e = (uInt)(t->exop); + if (e & 16) /* distance */ + { + c->sub.copy.get = e & 15; + c->sub.copy.dist = t->base; + c->mode = DISTEXT; + break; + } + if ((e & 64) == 0) /* next table */ + { + c->sub.code.need = e; + c->sub.code.tree = t + t->base; + break; + } + c->mode = BADCODE; /* invalid code */ + z->msg = (char*)"invalid distance code"; + r = Z_DATA_ERROR; + LEAVE case DISTEXT: /* i: getting distance extra */ - j = c->sub.copy.get; - NEEDBITS(j) - c->sub.copy.dist += (uInt)b & inflate_mask[j]; - DUMPBITS(j) - Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); - c->mode = COPY; + j = c->sub.copy.get; + NEEDBITS(j) + c->sub.copy.dist += (uInt)b & inflate_mask[j]; + DUMPBITS(j) + Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); + c->mode = COPY; case COPY: /* o: copying bytes in window, waiting for space */ -#ifndef __TURBOC__ /* Turbo C bug for following expression */ - f = (uInt)(q - s->window) < c->sub.copy.dist ? - s->end - (c->sub.copy.dist - (q - s->window)) : - q - c->sub.copy.dist; -#else - tryF = (long)q - c->sub.copy.dist; -// f = q - c->sub.copy.dist; - if ((uInt)(q - s->window) < c->sub.copy.dist) - f = s->end - (c->sub.copy.dist - (uInt)(q - s->window)); - else f = (Bytef *)tryF; -#endif - while (c->len) - { - NEEDOUT - OUTBYTE(*f++) - if (f == s->end) - f = s->window; - c->len--; - } - c->mode = START; - break; + csf = (unsigned long)q - c->sub.copy.dist; + while (csf < (unsigned long)s->window) /* modulo window size-"while" instead */ + csf += (unsigned long)(s->end - s->window); /* of "if" handles invalid distances */ + while (c->len) + { + NEEDOUT + OUTBYTE(*(Bytef *)csf++) + if (csf == (unsigned long)s->end) + csf = (unsigned long)s->window; + c->len--; + } + c->mode = START; + break; case LIT: /* o: got literal, waiting for output space */ - NEEDOUT - OUTBYTE(c->sub.lit) - c->mode = START; - break; + NEEDOUT + OUTBYTE(c->sub.lit) + c->mode = START; + break; case WASH: /* o: got eob, possibly more output */ - if (k > 7) /* return unused byte, if any */ - { + if (k > 7) /* return unused byte, if any */ + { Assert(k < 16, "inflate_codes grabbed too many bytes") k -= 8; n++; diff --git a/src/utilfuns/zlib/infcodes.h b/src/utilfuns/zlib/infcodes.h index 6c750d8..46821a0 100644 --- a/src/utilfuns/zlib/infcodes.h +++ b/src/utilfuns/zlib/infcodes.h @@ -1,5 +1,5 @@ /* infcodes.h -- header to use infcodes.c - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/inffast.c b/src/utilfuns/zlib/inffast.c index 61a78ee..a7ebc64 100644 --- a/src/utilfuns/zlib/inffast.c +++ b/src/utilfuns/zlib/inffast.c @@ -1,5 +1,5 @@ /* inffast.c -- process literals and length/distance pairs fast - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -44,7 +44,7 @@ z_streamp z; uInt md; /* mask for distance tree */ uInt c; /* bytes to copy */ uInt d; /* distance back to copy from */ - Bytef *r; /* copy source pointer */ + unsigned long csp; /* copy source pointer */ /* load input, output, bit values */ LOAD @@ -59,94 +59,107 @@ z_streamp z; GRABBITS(20) /* max bits for literal/length code */ if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) { - DUMPBITS(t->bits) - Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? - "inflate: * literal '%c'\n" : - "inflate: * literal 0x%02x\n", t->base)); - *q++ = (Byte)t->base; - m--; - continue; + DUMPBITS(t->bits) + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? + "inflate: * literal '%c'\n" : + "inflate: * literal 0x%02x\n", t->base)); + *q++ = (Byte)t->base; + m--; + continue; } do { - DUMPBITS(t->bits) - if (e & 16) - { - /* get extra bits for length */ - e &= 15; - c = t->base + ((uInt)b & inflate_mask[e]); - DUMPBITS(e) - Tracevv((stderr, "inflate: * length %u\n", c)); + DUMPBITS(t->bits) + if (e & 16) + { + /* get extra bits for length */ + e &= 15; + c = t->base + ((uInt)b & inflate_mask[e]); + DUMPBITS(e) + Tracevv((stderr, "inflate: * length %u\n", c)); - /* decode distance base of block to copy */ - GRABBITS(15); /* max bits for distance code */ - e = (t = td + ((uInt)b & md))->exop; - do { - DUMPBITS(t->bits) - if (e & 16) - { - /* get extra bits to add to distance base */ - e &= 15; - GRABBITS(e) /* get extra bits (up to 13) */ - d = t->base + ((uInt)b & inflate_mask[e]); - DUMPBITS(e) - Tracevv((stderr, "inflate: * distance %u\n", d)); + /* decode distance base of block to copy */ + GRABBITS(15); /* max bits for distance code */ + e = (t = td + ((uInt)b & md))->exop; + do { + DUMPBITS(t->bits) + if (e & 16) + { + /* get extra bits to add to distance base */ + e &= 15; + GRABBITS(e) /* get extra bits (up to 13) */ + d = t->base + ((uInt)b & inflate_mask[e]); + DUMPBITS(e) + Tracevv((stderr, "inflate: * distance %u\n", d)); - /* do the copy */ - m -= c; - if ((uInt)(q - s->window) >= d) /* offset before dest */ - { /* just copy */ - r = q - d; - *q++ = *r++; c--; /* minimum count is three, */ - *q++ = *r++; c--; /* so unroll loop a little */ - } - else /* else offset after destination */ - { - e = d - (uInt)(q - s->window); /* bytes from offset to end */ - r = s->end - e; /* pointer to offset */ - if (c > e) /* if source crosses, */ - { - c -= e; /* copy to end of window */ - do { - *q++ = *r++; - } while (--e); - r = s->window; /* copy rest from start of window */ - } - } - do { /* copy all or what's left */ - *q++ = *r++; - } while (--c); - break; - } - else if ((e & 64) == 0) - { - t += t->base; - e = (t += ((uInt)b & inflate_mask[e]))->exop; - } - else - { - z->msg = (char*)"invalid distance code"; - UNGRAB - UPDATE - return Z_DATA_ERROR; - } - } while (1); - break; - } - if ((e & 64) == 0) - { - t += t->base; - if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0) - { - DUMPBITS(t->bits) - Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? - "inflate: * literal '%c'\n" : - "inflate: * literal 0x%02x\n", t->base)); - *q++ = (Byte)t->base; - m--; - break; - } - } - else if (e & 32) + /* do the copy */ + m -= c; + csp = (unsigned long)q - d; + if (csp < (unsigned long)s->window) /* wrap if needed */ + { + do { + csp += (unsigned long)(s->end - s->window); /* force pointer in window */ + } while (csp < (unsigned long)s->window); /* covers invalid distances */ + e = (unsigned long)s->end - csp; + if (c > e) + { + c -= e; /* wrapped copy */ + do { + *q++ = *(Bytef *)csp++; + } while (--e); + csp = s->window; + do { + *q++ = *(Bytef *)csp++; + } while (--c); + } + else /* normal copy */ + { + *q++ = *(Bytef *)csp++; c--; + *q++ = *(Bytef *)csp++; c--; + do { + *q++ = *(Bytef *)csp++; + } while (--c); + } + } + else /* normal copy */ + { + *q++ = *(Bytef *)csp++; c--; + *q++ = *(Bytef *)csp++; c--; + do { + *q++ = *(Bytef *)csp++; + } while (--c); + } + break; + } + else if ((e & 64) == 0) + { + t += t->base; + e = (t += ((uInt)b & inflate_mask[e]))->exop; + } + else + { + z->msg = (char*)"invalid distance code"; + UNGRAB + UPDATE + return Z_DATA_ERROR; + } + } while (1); + break; + } + if ((e & 64) == 0) + { + t += t->base; + if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0) + { + DUMPBITS(t->bits) + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? + "inflate: * literal '%c'\n" : + "inflate: * literal 0x%02x\n", t->base)); + *q++ = (Byte)t->base; + m--; + break; + } + } + else if (e & 32) { Tracevv((stderr, "inflate: * end of block\n")); UNGRAB diff --git a/src/utilfuns/zlib/inffast.h b/src/utilfuns/zlib/inffast.h index 8facec5..a31a4bb 100644 --- a/src/utilfuns/zlib/inffast.h +++ b/src/utilfuns/zlib/inffast.h @@ -1,5 +1,5 @@ /* inffast.h -- header to use inffast.c - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/inflate.c b/src/utilfuns/zlib/inflate.c index 32e9b8d..dfb2e86 100644 --- a/src/utilfuns/zlib/inflate.c +++ b/src/utilfuns/zlib/inflate.c @@ -1,5 +1,5 @@ /* inflate.c -- zlib interface to inflate modules - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/inftrees.c b/src/utilfuns/zlib/inftrees.c index ef1e0b6..4c32ca3 100644 --- a/src/utilfuns/zlib/inftrees.c +++ b/src/utilfuns/zlib/inftrees.c @@ -1,5 +1,5 @@ /* inftrees.c -- generate Huffman trees for efficient decoding - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -11,7 +11,7 @@ #endif const char inflate_copyright[] = - " inflate 1.1.3 Copyright 1995-1998 Mark Adler "; + " inflate 1.1.4 Copyright 1995-2002 Mark Adler "; /* If you use the zlib library in a product, an acknowledgment is welcome in the documentation of your product. If for some reason you cannot @@ -104,8 +104,7 @@ uIntf *v; /* working area: values in order of bit length */ /* Given a list of code lengths and a maximum table size, make a set of tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR if the given code set is incomplete (the tables are still built in this - case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of - lengths), or Z_MEM_ERROR if not enough memory. */ + case), or Z_DATA_ERROR if the input is invalid. */ { uInt a; /* counter for codes of length k */ @@ -231,7 +230,7 @@ uIntf *v; /* working area: values in order of bit length */ /* allocate new table */ if (*hn + z > MANY) /* (note: doesn't matter for fixed) */ - return Z_MEM_ERROR; /* not enough memory */ + return Z_DATA_ERROR; /* overflow of MANY */ u[h] = q = hp + *hn; *hn += z; diff --git a/src/utilfuns/zlib/inftrees.h b/src/utilfuns/zlib/inftrees.h index 85853e0..04b73b7 100644 --- a/src/utilfuns/zlib/inftrees.h +++ b/src/utilfuns/zlib/inftrees.h @@ -1,5 +1,5 @@ /* inftrees.h -- header to use inftrees.c - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/infutil.c b/src/utilfuns/zlib/infutil.c index 824dab5..9a07622 100644 --- a/src/utilfuns/zlib/infutil.c +++ b/src/utilfuns/zlib/infutil.c @@ -1,5 +1,5 @@ /* inflate_util.c -- data and routines common to blocks and codes - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/infutil.h b/src/utilfuns/zlib/infutil.h index 99d1135..4401df8 100644 --- a/src/utilfuns/zlib/infutil.h +++ b/src/utilfuns/zlib/infutil.h @@ -1,5 +1,5 @@ /* infutil.h -- types and macros common to blocks and codes - * Copyright (C) 1995-1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/maketree.c b/src/utilfuns/zlib/maketree.c index 949d786..a16d4b1 100644 --- a/src/utilfuns/zlib/maketree.c +++ b/src/utilfuns/zlib/maketree.c @@ -1,5 +1,5 @@ /* maketree.c -- make inffixed.h table for decoding fixed codes - * Copyright (C) 1998 Mark Adler + * Copyright (C) 1995-2002 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h */ diff --git a/src/utilfuns/zlib/trees.c b/src/utilfuns/zlib/trees.c index 4d59d6e..52c33f1 100644 --- a/src/utilfuns/zlib/trees.c +++ b/src/utilfuns/zlib/trees.c @@ -1,5 +1,5 @@ /* trees.c -- output deflated data using Huffman coding - * Copyright (C) 1995-1998 Jean-loup Gailly + * Copyright (C) 1995-2002 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -29,7 +29,7 @@ * Addison-Wesley, 1983. ISBN 0-201-06672-6. */ -/* @(#) $Id: trees.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: trees.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ /* #define GEN_TREES_H */ diff --git a/src/utilfuns/zlib/uncompr.c b/src/utilfuns/zlib/uncompr.c index 0c1051a..42492a7 100644 --- a/src/utilfuns/zlib/uncompr.c +++ b/src/utilfuns/zlib/uncompr.c @@ -1,9 +1,9 @@ /* uncompr.c -- decompress a memory buffer - * Copyright (C) 1995-1998 Jean-loup Gailly. + * Copyright (C) 1995-2002 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ -/* @(#) $Id: uncompr.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: uncompr.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #include "zlib.h" diff --git a/src/utilfuns/zlib/zutil.c b/src/utilfuns/zlib/zutil.c index 2eb7b46..e1c7a6c 100644 --- a/src/utilfuns/zlib/zutil.c +++ b/src/utilfuns/zlib/zutil.c @@ -1,9 +1,9 @@ /* zutil.c -- target dependent utility functions for the compression library - * Copyright (C) 1995-1998 Jean-loup Gailly. + * Copyright (C) 1995-2002 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ -/* @(#) $Id: zutil.c,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: zutil.c,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #include "zutil.h" diff --git a/src/utilfuns/zlib/zutil.h b/src/utilfuns/zlib/zutil.h index 53278ba..bd6348d 100644 --- a/src/utilfuns/zlib/zutil.h +++ b/src/utilfuns/zlib/zutil.h @@ -1,5 +1,5 @@ /* zutil.h -- internal interface and configuration of the compression library - * Copyright (C) 1995-1998 Jean-loup Gailly. + * Copyright (C) 1995-2002 Jean-loup Gailly. * For conditions of distribution and use, see copyright notice in zlib.h */ @@ -8,7 +8,7 @@ subject to change. Applications should only use zlib.h. */ -/* @(#) $Id: zutil.h,v 1.1 2001/03/23 09:00:15 scribe Exp $ */ +/* @(#) $Id: zutil.h,v 1.2 2002/10/07 22:28:15 scribe Exp $ */ #ifndef _Z_UTIL_H #define _Z_UTIL_H |