1/* lzo_util.c -- utilities for the LZO library
2
3   This file is part of the LZO real-time data compression library.
4
5   Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
6   Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7   Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
21   All Rights Reserved.
22
23   The LZO library is free software; you can redistribute it and/or
24   modify it under the terms of the GNU General Public License as
25   published by the Free Software Foundation; either version 2 of
26   the License, or (at your option) any later version.
27
28   The LZO library is distributed in the hope that it will be useful,
29   but WITHOUT ANY WARRANTY; without even the implied warranty of
30   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31   GNU General Public License for more details.
32
33   You should have received a copy of the GNU General Public License
34   along with the LZO library; see the file COPYING.
35   If not, write to the Free Software Foundation, Inc.,
36   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37
38   Markus F.X.J. Oberhumer
39   <markus@oberhumer.com>
40   http://www.oberhumer.com/opensource/lzo/
41 */
42
43
44#include "lzo_conf.h"
45
46
47/***********************************************************************
48//
49************************************************************************/
50
51/* If you use the LZO library in a product, I would appreciate that you
52 * keep this copyright string in the executable of your product.
53.*/
54
55static const char __lzo_copyright[] =
56#if !defined(__LZO_IN_MINLZO)
57    /* save space as some people want a really small decompressor */
58    LZO_VERSION_STRING;
59#else
60    "\r\n\n"
61    "LZO data compression library.\n"
62    "$Copyright: LZO Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\n"
63    "<markus@oberhumer.com>\n"
64    "http://www.oberhumer.com $\n\n"
65    "$Id: LZO version: v" LZO_VERSION_STRING ", " LZO_VERSION_DATE " $\n"
66    "$Info: " LZO_INFO_STRING " $\n";
67#endif
68
69
70LZO_PUBLIC(const lzo_bytep)
71lzo_copyright(void)
72{
73#if (LZO_OS_DOS16 && LZO_CC_TURBOC)
74    return (lzo_voidp) __lzo_copyright;
75#else
76    return (const lzo_bytep) __lzo_copyright;
77#endif
78}
79
80LZO_PUBLIC(unsigned)
81lzo_version(void)
82{
83    return LZO_VERSION;
84}
85
86LZO_PUBLIC(const char *)
87lzo_version_string(void)
88{
89    return LZO_VERSION_STRING;
90}
91
92LZO_PUBLIC(const char *)
93lzo_version_date(void)
94{
95    return LZO_VERSION_DATE;
96}
97
98LZO_PUBLIC(const lzo_charp)
99_lzo_version_string(void)
100{
101    return LZO_VERSION_STRING;
102}
103
104LZO_PUBLIC(const lzo_charp)
105_lzo_version_date(void)
106{
107    return LZO_VERSION_DATE;
108}
109
110
111/***********************************************************************
112// adler32 checksum
113// adapted from free code by Mark Adler <madler@alumni.caltech.edu>
114// see http://www.zlib.org/
115************************************************************************/
116
117#define LZO_BASE 65521u /* largest prime smaller than 65536 */
118#define LZO_NMAX 5552
119/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
120
121#define LZO_DO1(buf,i)  s1 += buf[i]; s2 += s1
122#define LZO_DO2(buf,i)  LZO_DO1(buf,i); LZO_DO1(buf,i+1);
123#define LZO_DO4(buf,i)  LZO_DO2(buf,i); LZO_DO2(buf,i+2);
124#define LZO_DO8(buf,i)  LZO_DO4(buf,i); LZO_DO4(buf,i+4);
125#define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
126
127LZO_PUBLIC(lzo_uint32)
128lzo_adler32(lzo_uint32 adler, const lzo_bytep buf, lzo_uint len)
129{
130    lzo_uint32 s1 = adler & 0xffff;
131    lzo_uint32 s2 = (adler >> 16) & 0xffff;
132    unsigned k;
133
134    if (buf == NULL)
135        return 1;
136
137    while (len > 0)
138    {
139        k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
140        len -= k;
141        if (k >= 16) do
142        {
143            LZO_DO16(buf,0);
144            buf += 16;
145            k -= 16;
146        } while (k >= 16);
147        if (k != 0) do
148        {
149            s1 += *buf++;
150            s2 += s1;
151        } while (--k > 0);
152        s1 %= LZO_BASE;
153        s2 %= LZO_BASE;
154    }
155    return (s2 << 16) | s1;
156}
157
158#undef LZO_DO1
159#undef LZO_DO2
160#undef LZO_DO4
161#undef LZO_DO8
162#undef LZO_DO16
163
164
165/*
166vi:ts=4:et
167*/
168