Adler32.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. /*
  4. * $Id: Adler32.cs,v 1.1 2006-07-31 13:59:25 bouncy Exp $
  5. *
  6. Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. 1. Redistributions of source code must retain the above copyright notice,
  10. this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the distribution.
  14. 3. The names of the authors may not be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  19. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * This program is based on zlib-1.1.3, so all credit should go authors
  29. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  30. * and contributors of zlib.
  31. */
  32. namespace Org.BouncyCastle.Utilities.Zlib {
  33. internal sealed class Adler32{
  34. // largest prime smaller than 65536
  35. private const int BASE=65521;
  36. // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
  37. private const int NMAX=5552;
  38. internal long adler32(long adler, byte[] buf, int index, int len){
  39. if(buf == null){ return 1L; }
  40. long s1=adler&0xffff;
  41. long s2=(adler>>16)&0xffff;
  42. int k;
  43. while(len > 0) {
  44. k=len<NMAX?len:NMAX;
  45. len-=k;
  46. while(k>=16){
  47. s1+=buf[index++]&0xff; s2+=s1;
  48. s1+=buf[index++]&0xff; s2+=s1;
  49. s1+=buf[index++]&0xff; s2+=s1;
  50. s1+=buf[index++]&0xff; s2+=s1;
  51. s1+=buf[index++]&0xff; s2+=s1;
  52. s1+=buf[index++]&0xff; s2+=s1;
  53. s1+=buf[index++]&0xff; s2+=s1;
  54. s1+=buf[index++]&0xff; s2+=s1;
  55. s1+=buf[index++]&0xff; s2+=s1;
  56. s1+=buf[index++]&0xff; s2+=s1;
  57. s1+=buf[index++]&0xff; s2+=s1;
  58. s1+=buf[index++]&0xff; s2+=s1;
  59. s1+=buf[index++]&0xff; s2+=s1;
  60. s1+=buf[index++]&0xff; s2+=s1;
  61. s1+=buf[index++]&0xff; s2+=s1;
  62. s1+=buf[index++]&0xff; s2+=s1;
  63. k-=16;
  64. }
  65. if(k!=0){
  66. do{
  67. s1+=buf[index++]&0xff; s2+=s1;
  68. }
  69. while(--k!=0);
  70. }
  71. s1%=BASE;
  72. s2%=BASE;
  73. }
  74. return (s2<<16)|s1;
  75. }
  76. }
  77. }
  78. #endif