source: abuse/branches/lol/src/crc.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 1.3 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "crc.h"
18
19uint16_t Crc::FromData(void const *buf, size_t len)
20{
21    uint8_t const *data = (uint8_t const *)buf;
22    uint8_t c1 = 0, c2 = 0;
23
24    while (len--)
25        c2 += (c1 += *data++);
26
27    return (c2 << 8) | c1;
28}
29
30uint32_t Crc::FromFile(bFILE *fp)
31{
32    int size = 0x1000;
33    uint8_t *buffer = new uint8_t[size];
34    uint8_t crc1 = 0, crc2 = 0, crc3 = 0, crc4 = 0;
35    long old_pos = fp->tell();
36
37    fp->seek(0, 0);
38    for (long l = fp->file_size(); l; )
39    {
40        int nr = fp->read(buffer, size);
41        if (nr == 0)
42            l = 0;
43        else
44        {
45            l -= nr;
46            for (uint8_t *c = buffer; nr; nr--, c++)
47            {
48                crc1 += *c;
49                crc2 += crc1;
50                crc3 += crc2;
51                crc4 += crc3;
52            }
53        }
54    }
55    fp->seek(old_pos, 0);
56    delete[] buffer;
57
58    return crc1 | (crc2 << 8) | (crc3 << 16) | (crc4 << 24);
59}
60
Note: See TracBrowser for help on using the repository browser.