The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/zlib/adler32.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2008 Apple Inc. All rights reserved.
    3  *
    4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
    5  * 
    6  * This file contains Original Code and/or Modifications of Original Code
    7  * as defined in and that are subject to the Apple Public Source License
    8  * Version 2.0 (the 'License'). You may not use this file except in
    9  * compliance with the License. The rights granted to you under the License
   10  * may not be used to create, or enable the creation or redistribution of,
   11  * unlawful or unlicensed copies of an Apple operating system, or to
   12  * circumvent, violate, or enable the circumvention or violation of, any
   13  * terms of an Apple operating system software license agreement.
   14  * 
   15  * Please obtain a copy of the License at
   16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
   17  * 
   18  * The Original Code and all software distributed under the License are
   19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
   20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
   21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
   22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
   23  * Please see the License for the specific language governing rights and
   24  * limitations under the License.
   25  * 
   26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
   27  */
   28 /* adler32.c -- compute the Adler-32 checksum of a data stream
   29  * Copyright (C) 1995-2004 Mark Adler
   30  * For conditions of distribution and use, see copyright notice in zlib.h
   31  */
   32 
   33 /* @(#) $Id$ */
   34 
   35 #define ZLIB_INTERNAL
   36 #if KERNEL
   37     #include <libkern/zlib.h>
   38 #else
   39     #include "zlib.h"
   40 #endif /* KERNEL */
   41 
   42 #define BASE 65521UL    /* largest prime smaller than 65536 */
   43 #define NMAX 5552
   44 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
   45 
   46 #define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
   47 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
   48 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
   49 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
   50 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
   51 
   52 /* use NO_DIVIDE if your processor does not do division in hardware */
   53 #ifdef NO_DIVIDE
   54 #  define MOD(a) \
   55     do { \
   56         if (a >= (BASE << 16)) a -= (BASE << 16); \
   57         if (a >= (BASE << 15)) a -= (BASE << 15); \
   58         if (a >= (BASE << 14)) a -= (BASE << 14); \
   59         if (a >= (BASE << 13)) a -= (BASE << 13); \
   60         if (a >= (BASE << 12)) a -= (BASE << 12); \
   61         if (a >= (BASE << 11)) a -= (BASE << 11); \
   62         if (a >= (BASE << 10)) a -= (BASE << 10); \
   63         if (a >= (BASE << 9)) a -= (BASE << 9); \
   64         if (a >= (BASE << 8)) a -= (BASE << 8); \
   65         if (a >= (BASE << 7)) a -= (BASE << 7); \
   66         if (a >= (BASE << 6)) a -= (BASE << 6); \
   67         if (a >= (BASE << 5)) a -= (BASE << 5); \
   68         if (a >= (BASE << 4)) a -= (BASE << 4); \
   69         if (a >= (BASE << 3)) a -= (BASE << 3); \
   70         if (a >= (BASE << 2)) a -= (BASE << 2); \
   71         if (a >= (BASE << 1)) a -= (BASE << 1); \
   72         if (a >= BASE) a -= BASE; \
   73     } while (0)
   74 #  define MOD4(a) \
   75     do { \
   76         if (a >= (BASE << 4)) a -= (BASE << 4); \
   77         if (a >= (BASE << 3)) a -= (BASE << 3); \
   78         if (a >= (BASE << 2)) a -= (BASE << 2); \
   79         if (a >= (BASE << 1)) a -= (BASE << 1); \
   80         if (a >= BASE) a -= BASE; \
   81     } while (0)
   82 #else
   83 #  define MOD(a) a %= BASE
   84 #  define MOD4(a) a %= BASE
   85 #endif
   86 
   87 /* ========================================================================= */
   88 uLong ZEXPORT adler32(adler, buf, len)
   89     uLong adler;
   90     const Bytef *buf;
   91     uInt len;
   92 {
   93     unsigned long sum2;
   94     unsigned n;
   95 
   96     /* split Adler-32 into component sums */
   97     sum2 = (adler >> 16) & 0xffff;
   98     adler &= 0xffff;
   99 
  100     /* in case user likes doing a byte at a time, keep it fast */
  101     if (len == 1) {
  102         adler += buf[0];
  103         if (adler >= BASE)
  104             adler -= BASE;
  105         sum2 += adler;
  106         if (sum2 >= BASE)
  107             sum2 -= BASE;
  108         return adler | (sum2 << 16);
  109     }
  110 
  111     /* initial Adler-32 value (deferred check for len == 1 speed) */
  112     if (buf == Z_NULL)
  113         return 1L;
  114 
  115     /* in case short lengths are provided, keep it somewhat fast */
  116     if (len < 16) {
  117         while (len--) {
  118             adler += *buf++;
  119             sum2 += adler;
  120         }
  121         if (adler >= BASE)
  122             adler -= BASE;
  123         MOD4(sum2);             /* only added so many BASE's */
  124         return adler | (sum2 << 16);
  125     }
  126 
  127     /* do length NMAX blocks -- requires just one modulo operation */
  128     while (len >= NMAX) {
  129         len -= NMAX;
  130         n = NMAX / 16;          /* NMAX is divisible by 16 */
  131         do {
  132             DO16(buf);          /* 16 sums unrolled */
  133             buf += 16;
  134         } while (--n);
  135         MOD(adler);
  136         MOD(sum2);
  137     }
  138 
  139     /* do remaining bytes (less than NMAX, still just one modulo) */
  140     if (len) {                  /* avoid modulos if none remaining */
  141         while (len >= 16) {
  142             len -= 16;
  143             DO16(buf);
  144             buf += 16;
  145         }
  146         while (len--) {
  147             adler += *buf++;
  148             sum2 += adler;
  149         }
  150         MOD(adler);
  151         MOD(sum2);
  152     }
  153 
  154     /* return recombined sums */
  155     return adler | (sum2 << 16);
  156 }
  157 
  158 /* ========================================================================= */
  159 uLong ZEXPORT adler32_combine(adler1, adler2, len2)
  160     uLong adler1;
  161     uLong adler2;
  162     z_off_t len2;
  163 {
  164     unsigned long sum1;
  165     unsigned long sum2;
  166     unsigned rem;
  167 
  168     /* the derivation of this formula is left as an exercise for the reader */
  169     rem = (unsigned)(len2 % BASE);
  170     sum1 = adler1 & 0xffff;
  171     sum2 = rem * sum1;
  172     MOD(sum2);
  173     sum1 += (adler2 & 0xffff) + BASE - 1;
  174     sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
  175     if (sum1 > BASE) sum1 -= BASE;
  176     if (sum1 > BASE) sum1 -= BASE;
  177     if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
  178     if (sum2 > BASE) sum2 -= BASE;
  179     return sum1 | (sum2 << 16);
  180 }

Cache object: 2a9a195fb853e42fb3b8493c5a69b20c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.