public: crclib now is part of public library

This commit is contained in:
Alibek Omarov 2019-06-06 04:33:57 +03:00
parent 9735dedf2f
commit 70d45f23de
6 changed files with 189 additions and 166 deletions

View File

@ -117,6 +117,7 @@ typedef enum
#include "crtlib.h"
#include "cvar.h"
#include "con_nprint.h"
#include "crclib.h"
#define XASH_VERSION "0.99" // engine current version
@ -296,14 +297,6 @@ typedef enum
key_message
} keydest_t;
// MD5 Hash
typedef struct
{
uint buf[4];
uint bits[2];
byte in[64];
} MD5Context_t;
typedef enum
{
RD_NONE = 0,
@ -541,6 +534,8 @@ const char *FS_GetDiskPath( const char *name, qboolean gamedironly );
byte *W_LoadLump( wfile_t *wad, const char *lumpname, size_t *lumpsizeptr, const char type );
void W_Close( wfile_t *wad );
byte *FS_LoadFile( const char *path, fs_offset_t *filesizeptr, qboolean gamedironly );
qboolean CRC32_File( dword *crcvalue, const char *filename );
qboolean MD5_HashFile( byte digest[16], const char *pszFileName, uint seed[4] );
byte *FS_LoadDirectFile( const char *path, fs_offset_t *filesizeptr );
qboolean FS_WriteFile( const char *filename, const void *data, fs_offset_t len );
qboolean COM_ParseVector( char **pfile, float *v, size_t size );
@ -792,23 +787,6 @@ void Con_CompleteCommand( field_t *field );
void Cmd_AutoComplete( char *complete_string );
void Cmd_AutoCompleteClear( void );
//
// crclib.c
//
void CRC32_Init( dword *pulCRC );
byte CRC32_BlockSequence( byte *base, int length, int sequence );
void CRC32_ProcessBuffer( dword *pulCRC, const void *pBuffer, int nBuffer );
void CRC32_ProcessByte( dword *pulCRC, byte ch );
dword CRC32_Final( dword pulCRC );
qboolean CRC32_File( dword *crcvalue, const char *filename );
qboolean CRC32_MapFile( dword *crcvalue, const char *filename, qboolean multiplayer );
void MD5Init( MD5Context_t *ctx );
void MD5Update( MD5Context_t *ctx, const byte *buf, uint len );
void MD5Final( byte digest[16], MD5Context_t *ctx );
qboolean MD5_HashFile( byte digest[16], const char *pszFileName, uint seed[4] );
uint COM_HashKey( const char *string, uint hashSize );
char *MD5_Print( byte hash[16] );
//
// custom.c
//

View File

@ -2942,6 +2942,68 @@ byte *FS_LoadFile( const char *path, fs_offset_t *filesizeptr, qboolean gamediro
return buf;
}
qboolean CRC32_File( dword *crcvalue, const char *filename )
{
char buffer[1024];
int num_bytes;
file_t *f;
f = FS_Open( filename, "rb", false );
if( !f ) return false;
Assert( crcvalue != NULL );
CRC32_Init( crcvalue );
while( 1 )
{
num_bytes = FS_Read( f, buffer, sizeof( buffer ));
if( num_bytes > 0 )
CRC32_ProcessBuffer( crcvalue, buffer, num_bytes );
if( FS_Eof( f )) break;
}
FS_Close( f );
return true;
}
qboolean MD5_HashFile( byte digest[16], const char *pszFileName, uint seed[4] )
{
file_t *file;
char buffer[1024];
MD5Context_t MD5_Hash;
int bytes;
if(( file = FS_Open( pszFileName, "rb", false )) == NULL )
return false;
memset( &MD5_Hash, 0, sizeof( MD5Context_t ));
MD5Init( &MD5_Hash );
if( seed )
{
MD5Update( &MD5_Hash, (const byte *)seed, 16 );
}
while( 1 )
{
bytes = FS_Read( file, buffer, sizeof( buffer ));
if( bytes > 0 )
MD5Update( &MD5_Hash, buffer, bytes );
if( FS_Eof( file ))
break;
}
FS_Close( file );
MD5Final( digest, &MD5_Hash );
return true;
}
/*
============
FS_LoadFile

View File

@ -759,6 +759,83 @@ void SV_SetupClients( void )
ClearBits( sv_maxclients->flags, FCVAR_CHANGED );
}
static qboolean CRC32_MapFile( dword *crcvalue, const char *filename, qboolean multiplayer )
{
char headbuf[1024], buffer[1024];
int i, num_bytes, lumplen;
int version, hdr_size;
dheader_t *header;
file_t *f;
if( !crcvalue ) return false;
// always calc same checksum for singleplayer
if( multiplayer == false )
{
*crcvalue = (('H'<<24)+('S'<<16)+('A'<<8)+'X');
return true;
}
f = FS_Open( filename, "rb", false );
if( !f ) return false;
// read version number
FS_Read( f, &version, sizeof( int ));
FS_Seek( f, 0, SEEK_SET );
hdr_size = sizeof( int ) + sizeof( dlump_t ) * HEADER_LUMPS;
num_bytes = FS_Read( f, headbuf, hdr_size );
// corrupted map ?
if( num_bytes != hdr_size )
{
FS_Close( f );
return false;
}
header = (dheader_t *)headbuf;
// invalid version ?
switch( header->version )
{
case Q1BSP_VERSION:
case HLBSP_VERSION:
case QBSP2_VERSION:
break;
default:
FS_Close( f );
return false;
}
CRC32_Init( crcvalue );
for( i = LUMP_PLANES; i < HEADER_LUMPS; i++ )
{
lumplen = header->lumps[i].filelen;
FS_Seek( f, header->lumps[i].fileofs, SEEK_SET );
while( lumplen > 0 )
{
if( lumplen >= sizeof( buffer ))
num_bytes = FS_Read( f, buffer, sizeof( buffer ));
else num_bytes = FS_Read( f, buffer, lumplen );
if( num_bytes > 0 )
{
lumplen -= num_bytes;
CRC32_ProcessBuffer( crcvalue, buffer, num_bytes );
}
// file unexpected end ?
if( FS_Eof( f )) break;
}
}
FS_Close( f );
return 1;
}
/*
================
SV_SpawnServer

View File

@ -13,8 +13,10 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "common.h"
#include "client.h"
#include "crclib.h"
#include "crtlib.h"
#include <string.h>
#include <stdlib.h>
#define NUM_BYTES 256
#define CRC32_INIT_VALUE 0xFFFFFFFFUL
@ -204,109 +206,6 @@ byte CRC32_BlockSequence( byte *base, int length, int sequence )
return (byte)CRC;
}
qboolean CRC32_File( dword *crcvalue, const char *filename )
{
char buffer[1024];
int num_bytes;
file_t *f;
f = FS_Open( filename, "rb", false );
if( !f ) return false;
Assert( crcvalue != NULL );
CRC32_Init( crcvalue );
while( 1 )
{
num_bytes = FS_Read( f, buffer, sizeof( buffer ));
if( num_bytes > 0 )
CRC32_ProcessBuffer( crcvalue, buffer, num_bytes );
if( FS_Eof( f )) break;
}
FS_Close( f );
return true;
}
qboolean CRC32_MapFile( dword *crcvalue, const char *filename, qboolean multiplayer )
{
char headbuf[1024], buffer[1024];
int i, num_bytes, lumplen;
int version, hdr_size;
dheader_t *header;
file_t *f;
if( !crcvalue ) return false;
// always calc same checksum for singleplayer
if( multiplayer == false )
{
*crcvalue = (('H'<<24)+('S'<<16)+('A'<<8)+'X');
return true;
}
f = FS_Open( filename, "rb", false );
if( !f ) return false;
// read version number
FS_Read( f, &version, sizeof( int ));
FS_Seek( f, 0, SEEK_SET );
hdr_size = sizeof( int ) + sizeof( dlump_t ) * HEADER_LUMPS;
num_bytes = FS_Read( f, headbuf, hdr_size );
// corrupted map ?
if( num_bytes != hdr_size )
{
FS_Close( f );
return false;
}
header = (dheader_t *)headbuf;
// invalid version ?
switch( header->version )
{
case Q1BSP_VERSION:
case HLBSP_VERSION:
case QBSP2_VERSION:
break;
default:
FS_Close( f );
return false;
}
CRC32_Init( crcvalue );
for( i = LUMP_PLANES; i < HEADER_LUMPS; i++ )
{
lumplen = header->lumps[i].filelen;
FS_Seek( f, header->lumps[i].fileofs, SEEK_SET );
while( lumplen > 0 )
{
if( lumplen >= sizeof( buffer ))
num_bytes = FS_Read( f, buffer, sizeof( buffer ));
else num_bytes = FS_Read( f, buffer, lumplen );
if( num_bytes > 0 )
{
lumplen -= num_bytes;
CRC32_ProcessBuffer( crcvalue, buffer, num_bytes );
}
// file unexpected end ?
if( FS_Eof( f )) break;
}
}
FS_Close( f );
return 1;
}
void MD5Transform( uint buf[4], const uint in[16] );
/*
@ -529,42 +428,6 @@ void MD5Transform( uint buf[4], const uint in[16] )
buf[3] += d;
}
qboolean MD5_HashFile( byte digest[16], const char *pszFileName, uint seed[4] )
{
file_t *file;
char buffer[1024];
MD5Context_t MD5_Hash;
int bytes;
if(( file = FS_Open( pszFileName, "rb", false )) == NULL )
return false;
memset( &MD5_Hash, 0, sizeof( MD5Context_t ));
MD5Init( &MD5_Hash );
if( seed )
{
MD5Update( &MD5_Hash, (const byte *)seed, 16 );
}
while( 1 )
{
bytes = FS_Read( file, buffer, sizeof( buffer ));
if( bytes > 0 )
MD5Update( &MD5_Hash, buffer, bytes );
if( FS_Eof( file ))
break;
}
FS_Close( file );
MD5Final( digest, &MD5_Hash );
return true;
}
/*
=================
MD5_Print

41
public/crclib.h Normal file
View File

@ -0,0 +1,41 @@
/*
crclib.c - generate crc stuff
Copyright (C) 2007 Uncle Mike
Copyright (C) 2019 a1batross
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#pragma once
#ifndef CRCLIB_H
#define CRCLIB_H
#include "xash3d_types.h"
typedef struct
{
uint buf[4];
uint bits[2];
byte in[64];
} MD5Context_t;
void CRC32_Init( dword *pulCRC );
byte CRC32_BlockSequence( byte *base, int length, int sequence );
void CRC32_ProcessBuffer( dword *pulCRC, const void *pBuffer, int nBuffer );
void CRC32_ProcessByte( dword *pulCRC, byte ch );
dword CRC32_Final( dword pulCRC );
void MD5Init( MD5Context_t *ctx );
void MD5Update( MD5Context_t *ctx, const byte *buf, uint len );
void MD5Final( byte digest[16], MD5Context_t *ctx );
uint COM_HashKey( const char *string, uint hashSize );
char *MD5_Print( byte hash[16] );
#endif // CRCLIB_H

View File

@ -16,6 +16,8 @@ GNU General Public License for more details.
#ifndef STDLIB_H
#define STDLIB_H
#include <stdarg.h>
#ifdef __GNUC__
#define _format(x) __attribute__((format(printf, x, x+1)))
#else