source: abuse/trunk/src/crc.cpp @ 481

Last change on this file since 481 was 481, checked in by Sam Hocevar, 11 years ago

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

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