?login_element?

Subversion Repositories NedoOS

Rev

Rev 539 | Blame | Compare with Previous | Last modification | View Log | Download

  1. // Part of CRC-32C library: https://crc32c.machinezoo.com/
  2. /*
  3.   Copyright (c) 2013 - 2014, 2016 Mark Adler, Robert Vazan, Max Vysokikh
  4.  
  5.   This software is provided 'as-is', without any express or implied
  6.   warranty.  In no event will the author be held liable for any damages
  7.   arising from the use of this software.
  8.  
  9.   Permission is granted to anyone to use this software for any purpose,
  10.   including commercial applications, and to alter it and redistribute it
  11.   freely, subject to the following restrictions:
  12.  
  13.   1. The origin of this software must not be misrepresented; you must not
  14.   claim that you wrote the original software. If you use this software
  15.   in a product, an acknowledgment in the product documentation would be
  16.   appreciated but is not required.
  17.   2. Altered source versions must be plainly marked as such, and must not be
  18.   misrepresented as being the original software.
  19.   3. This notice may not be removed or altered from any source distribution.
  20. */
  21.  
  22. /*
  23.  This is *MODIFIED* version of: https://github.com/robertvazan/crc32c-hw
  24.  License: zlib license
  25.  
  26.  Modified by Peter Helcmanovsky (c) 2020
  27.  (I believe the zlib license is compatible with the sjasmplus BSD license)
  28. */
  29.  
  30. #include "crc32c.h"
  31.  
  32. #define POLY 0x82f63b78
  33.  
  34. typedef const uint8_t *buffer;
  35.  
  36. static uint32_t table[16][256];
  37.  
  38. static bool _tableInitialized = false;
  39.  
  40. extern "C" void crc32_init()
  41. {
  42.         if (_tableInitialized) return;
  43.  
  44.         for(int i = 0; i < 256; i++)
  45.         {
  46.                 uint32_t res = (uint32_t)i;
  47.                 for(int t = 0; t < 16; t++) {
  48.                         for (int k = 0; k < 8; k++) res = (res & 1) == 1 ? POLY ^ (res >> 1) : (res >> 1);
  49.                         table[t][i] = res;
  50.                 }
  51.         }
  52.  
  53.         _tableInitialized = true;
  54. }
  55.  
  56. /* Table-driven software version as a fall-back.  This is about 15 times slower
  57.    than using the hardware instructions.  This assumes little-endian integers,
  58.    as is the case on Intel processors that the assembler code here is for. */
  59. extern "C" uint32_t crc32c_append_sw(uint32_t crci, buffer input, size_t length)
  60. {
  61.     buffer next = input;
  62.     uint32_t crc;
  63.  
  64.     crc = crci ^ 0xffffffff;
  65.     while (length && ((uintptr_t)next & 3) != 0)
  66.     {
  67.         crc = table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
  68.         --length;
  69.     }
  70.     while (length >= 12)
  71.     {
  72.         crc ^= *(uint32_t *)next;
  73.         uint32_t high = *(uint32_t *)(next + 4);
  74.         uint32_t high2 = *(uint32_t *)(next + 8);
  75.         crc = table[11][crc & 0xff]
  76.             ^ table[10][(crc >> 8) & 0xff]
  77.             ^ table[9][(crc >> 16) & 0xff]
  78.             ^ table[8][crc >> 24]
  79.             ^ table[7][high & 0xff]
  80.             ^ table[6][(high >> 8) & 0xff]
  81.             ^ table[5][(high >> 16) & 0xff]
  82.             ^ table[4][high >> 24]
  83.             ^ table[3][high2 & 0xff]
  84.             ^ table[2][(high2 >> 8) & 0xff]
  85.             ^ table[1][(high2 >> 16) & 0xff]
  86.             ^ table[0][high2 >> 24];
  87.         next += 12;
  88.         length -= 12;
  89.     }
  90.     while (length)
  91.     {
  92.         crc = table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
  93.         --length;
  94.     }
  95.     return (uint32_t)crc ^ 0xffffffff;
  96. }
  97.