From a19ecac6d3405652f766eb7b18c7ee6a3270a78b Mon Sep 17 00:00:00 2001 From: hemagx Date: Wed, 17 Feb 2016 14:01:22 +0200 Subject: Interface des.c --- src/common/grfio.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/common/grfio.c') diff --git a/src/common/grfio.c b/src/common/grfio.c index c6e47d357..c24f2aee5 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -162,7 +162,7 @@ static void grf_decode_header(unsigned char* buf, size_t len) // first 20 blocks are all des-encrypted for( i = 0; i < 20 && i < nblocks; ++i ) - des_decrypt_block(&p[i]); + des->decrypt_block(&p[i]); // the rest is plaintext, done. } @@ -176,7 +176,7 @@ static void grf_decode_full(unsigned char* buf, size_t len, int cycle) // first 20 blocks are all des-encrypted for( i = 0; i < 20 && i < nblocks; ++i ) - des_decrypt_block(&p[i]); + des->decrypt_block(&p[i]); // after that only one of every 'dcycle' blocks is des-encrypted dcycle = cycle; @@ -190,7 +190,7 @@ static void grf_decode_full(unsigned char* buf, size_t len, int cycle) { if( i % dcycle == 0 ) {// decrypt block - des_decrypt_block(&p[i]); + des->decrypt_block(&p[i]); continue; } @@ -504,7 +504,7 @@ static char* decode_filename(unsigned char* buf, int len) int lop; for(lop=0;lopdecrypt(&buf[lop],8); } return (char*)buf; } -- cgit v1.2.3-70-g09d2 From 3a19ceb903629a9ff7ce85f800c1b4fefc8aa2c5 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 01:32:32 +0100 Subject: Removed unnecessary typedefs from des.h Signed-off-by: Haru --- src/common/des.c | 28 ++++++++++++++-------------- src/common/des.h | 12 +++++++++--- src/common/grfio.c | 16 ++++++++-------- 3 files changed, 31 insertions(+), 25 deletions(-) (limited to 'src/common/grfio.c') diff --git a/src/common/des.c b/src/common/des.c index c811dd96c..706aafbdd 100644 --- a/src/common/des.c +++ b/src/common/des.c @@ -40,9 +40,9 @@ static const uint8_t mask[8] = { /// Initial permutation (IP). -static void IP(BIT64* src) +static void IP(struct BIT64 *src) { - BIT64 tmp = {{0}}; + struct BIT64 tmp = {{0}}; static const uint8_t ip_table[64] = { 58, 50, 42, 34, 26, 18, 10, 2, @@ -68,9 +68,9 @@ static void IP(BIT64* src) /// Final permutation (IP^-1). -static void FP(BIT64* src) +static void FP(struct BIT64 *src) { - BIT64 tmp = {{0}}; + struct BIT64 tmp = {{0}}; static const uint8_t fp_table[64] = { 40, 8, 48, 16, 56, 24, 64, 32, @@ -97,9 +97,9 @@ static void FP(BIT64* src) /// Expansion (E). /// Expands upper four 8-bits (32b) into eight 6-bits (48b). -static void E(BIT64* src) +static void E(struct BIT64 *src) { - BIT64 tmp = {{0}}; + struct BIT64 tmp = {{0}}; #if 0 // original @@ -137,9 +137,9 @@ static void E(BIT64* src) /// Transposition (P-BOX). -static void TP(BIT64* src) +static void TP(struct BIT64 *src) { - BIT64 tmp = {{0}}; + struct BIT64 tmp = {{0}}; static const uint8_t tp_table[32] = { 16, 7, 20, 21, @@ -166,9 +166,9 @@ static void TP(BIT64* src) /// Substitution boxes (S-boxes). /// NOTE: This implementation was optimized to process two nibbles in one step (twice as fast). -static void SBOX(BIT64* src) +static void SBOX(struct BIT64 *src) { - BIT64 tmp = {{0}}; + struct BIT64 tmp = {{0}}; static const uint8_t s_table[4][64] = { { @@ -207,9 +207,9 @@ static void SBOX(BIT64* src) /// DES round function. /// XORs src[0..3] with TP(SBOX(E(src[4..7]))). -static void RoundFunction(BIT64* src) +static void RoundFunction(struct BIT64 *src) { - BIT64 tmp = *src; + struct BIT64 tmp = *src; E(&tmp); SBOX(&tmp); TP(&tmp); @@ -221,7 +221,7 @@ static void RoundFunction(BIT64* src) } -void des_decrypt_block(BIT64* block) +void des_decrypt_block(struct BIT64 *block) { IP(block); RoundFunction(block); @@ -231,7 +231,7 @@ void des_decrypt_block(BIT64* block) void des_decrypt(unsigned char* data, size_t size) { - BIT64* p = (BIT64*)data; + struct BIT64 *p = (struct BIT64 *)data; size_t i; for( i = 0; i*8 < size; i += 8 ) diff --git a/src/common/des.h b/src/common/des.h index e7460e9fd..61cf2c04a 100644 --- a/src/common/des.h +++ b/src/common/des.h @@ -23,12 +23,18 @@ #include "common/hercules.h" +/* Struct definitions */ + /// One 64-bit block. -typedef struct BIT64 { uint8_t b[8]; } BIT64; +struct BIT64 { + uint8_t b[8]; +}; + +/* Interface */ struct des_interface { - void (*decrypt_block) (BIT64* block); - void (*decrypt) (unsigned char* data, size_t size); + void (*decrypt_block) (struct BIT64 *block); + void (*decrypt) (unsigned char *data, size_t size); }; #ifdef HERCULES_CORE diff --git a/src/common/grfio.c b/src/common/grfio.c index c24f2aee5..c5c38455d 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -121,9 +121,9 @@ static uint8_t grf_substitution(uint8_t in) } #if 0 /* this is not used anywhere, is it ok to delete? */ -static void grf_shuffle_enc(BIT64* src) +static void grf_shuffle_enc(struct BIT64 *src) { - BIT64 out; + struct BIT64 out; out.b[0] = src->b[3]; out.b[1] = src->b[4]; @@ -138,9 +138,9 @@ static void grf_shuffle_enc(BIT64* src) } #endif // 0 -static void grf_shuffle_dec(BIT64* src) +static void grf_shuffle_dec(struct BIT64 *src) { - BIT64 out; + struct BIT64 out; out.b[0] = src->b[3]; out.b[1] = src->b[4]; @@ -156,8 +156,8 @@ static void grf_shuffle_dec(BIT64* src) static void grf_decode_header(unsigned char* buf, size_t len) { - BIT64* p = (BIT64*)buf; - size_t nblocks = len / sizeof(BIT64); + struct BIT64 *p = (struct BIT64 *)buf; + size_t nblocks = len / sizeof(struct BIT64); size_t i; // first 20 blocks are all des-encrypted @@ -169,8 +169,8 @@ static void grf_decode_header(unsigned char* buf, size_t len) static void grf_decode_full(unsigned char* buf, size_t len, int cycle) { - BIT64* p = (BIT64*)buf; - size_t nblocks = len / sizeof(BIT64); + struct BIT64 *p = (struct BIT64 *)buf; + size_t nblocks = len / sizeof(struct BIT64); int dcycle, scycle; size_t i, j; -- cgit v1.2.3-70-g09d2 From 1a99077d1cfa1d66b984fec9b956ed8fcd8c4cff Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 19:47:35 +0100 Subject: Various changes to the des interface Mostly stylistic changes. Cleaned up documentation. Signed-off-by: Haru --- src/common/des.c | 145 +++++++++++++++++++++++++++-------------------------- src/common/des.h | 29 +++++++++-- src/common/grfio.c | 16 +++--- 3 files changed, 108 insertions(+), 82 deletions(-) (limited to 'src/common/grfio.c') diff --git a/src/common/des.c b/src/common/des.c index 706aafbdd..c680610e9 100644 --- a/src/common/des.c +++ b/src/common/des.c @@ -24,26 +24,23 @@ #include "common/cbasetypes.h" +/** @file + * Implementation of the des interface. + */ + struct des_interface des_s; struct des_interface *des; -/// DES (Data Encryption Standard) algorithm, modified version. -/// @see http://www.eathena.ws/board/index.php?autocom=bugtracker&showbug=5099. -/// @see http://en.wikipedia.org/wiki/Data_Encryption_Standard -/// @see http://en.wikipedia.org/wiki/DES_supplementary_material - - /// Bitmask for accessing individual bits of a byte. static const uint8_t mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; - -/// Initial permutation (IP). -static void IP(struct BIT64 *src) +/** + * Initial permutation (IP). + */ +static void des_IP(struct des_bit64 *src) { - struct BIT64 tmp = {{0}}; - static const uint8_t ip_table[64] = { 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, @@ -54,24 +51,23 @@ static void IP(struct BIT64 *src) 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7, }; + struct des_bit64 tmp = {{0}}; + int i; - size_t i; - for( i = 0; i < ARRAYLENGTH(ip_table); ++i ) - { + for(i = 0; i < ARRAYLENGTH(ip_table); ++i) { uint8_t j = ip_table[i] - 1; - if( src->b[(j >> 3) & 7] & mask[j & 7] ) - tmp .b[(i >> 3) & 7] |= mask[i & 7]; + if (src->b[(j >> 3) & 7] & mask[j & 7]) + tmp.b[(i >> 3) & 7] |= mask[i & 7]; } *src = tmp; } - -/// Final permutation (IP^-1). -static void FP(struct BIT64 *src) +/** + * Final permutation (IP^-1). + */ +static void des_FP(struct des_bit64 *src) { - struct BIT64 tmp = {{0}}; - static const uint8_t fp_table[64] = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, @@ -82,24 +78,26 @@ static void FP(struct BIT64 *src) 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25, }; + struct des_bit64 tmp = {{0}}; + int i; - size_t i; - for( i = 0; i < ARRAYLENGTH(fp_table); ++i ) - { + for (i = 0; i < ARRAYLENGTH(fp_table); ++i) { uint8_t j = fp_table[i] - 1; - if( src->b[(j >> 3) & 7] & mask[j & 7] ) - tmp .b[(i >> 3) & 7] |= mask[i & 7]; + if (src->b[(j >> 3) & 7] & mask[j & 7]) + tmp.b[(i >> 3) & 7] |= mask[i & 7]; } *src = tmp; } - -/// Expansion (E). -/// Expands upper four 8-bits (32b) into eight 6-bits (48b). -static void E(struct BIT64 *src) +/** + * Expansion (E). + * + * Expands upper four 8-bits (32b) into eight 6-bits (48b). + */ +static void des_E(struct des_bit64 *src) { - struct BIT64 tmp = {{0}}; + struct des_bit64 tmp = {{0}}; #if 0 // original @@ -113,13 +111,12 @@ static void E(struct BIT64 *src) 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1, }; + int i; - size_t i; - for( i = 0; i < ARRAYLENGTH(expand_table); ++i ) - { + for (i = 0; i < ARRAYLENGTH(expand_table); ++i) { uint8_t j = expand_table[i] - 1; - if( src->b[j / 8 + 4] & mask[j % 8] ) - tmp .b[i / 6 + 0] |= mask[i % 6]; + if (src->b[j / 8 + 4] & mask[j % 8]) + tmp.b[i / 6 + 0] |= mask[i % 6]; } #endif // optimized @@ -135,12 +132,11 @@ static void E(struct BIT64 *src) *src = tmp; } - -/// Transposition (P-BOX). -static void TP(struct BIT64 *src) +/** + * Transposition (P-BOX). + */ +static void des_TP(struct des_bit64 *src) { - struct BIT64 tmp = {{0}}; - static const uint8_t tp_table[32] = { 16, 7, 20, 21, 29, 12, 28, 17, @@ -151,25 +147,27 @@ static void TP(struct BIT64 *src) 19, 13, 30, 6, 22, 11, 4, 25, }; + struct des_bit64 tmp = {{0}}; + int i; - size_t i; - for( i = 0; i < ARRAYLENGTH(tp_table); ++i ) - { + for (i = 0; i < ARRAYLENGTH(tp_table); ++i) { uint8_t j = tp_table[i] - 1; - if( src->b[(j >> 3) + 0] & mask[j & 7] ) - tmp .b[(i >> 3) + 4] |= mask[i & 7]; + if (src->b[(j >> 3) + 0] & mask[j & 7]) + tmp.b[(i >> 3) + 4] |= mask[i & 7]; } *src = tmp; } -/// Substitution boxes (S-boxes). -/// NOTE: This implementation was optimized to process two nibbles in one step (twice as fast). -static void SBOX(struct BIT64 *src) +/** + * Substitution boxes (S-boxes). + * + * This implementation was optimized to process two nibbles in one step (twice + * as fast). + */ +static void des_SBOX(struct des_bit64 *src) { - struct BIT64 tmp = {{0}}; - static const uint8_t s_table[4][64] = { { 0xef, 0x03, 0x41, 0xfd, 0xd8, 0x74, 0x1e, 0x47, 0x26, 0xef, 0xfb, 0x22, 0xb3, 0xd8, 0x84, 0x1e, @@ -193,10 +191,10 @@ static void SBOX(struct BIT64 *src) 0xa0, 0x9f, 0xf6, 0x5c, 0x6a, 0x09, 0x8d, 0xf0, 0x0f, 0xe3, 0x53, 0x25, 0x95, 0x36, 0x28, 0xcb, } }; + struct des_bit64 tmp = {{0}}; + int i; - size_t i; - for( i = 0; i < ARRAYLENGTH(s_table); ++i ) - { + for (i = 0; i < ARRAYLENGTH(s_table); ++i) { tmp.b[i] = (s_table[i][src->b[i*2+0]] & 0xf0) | (s_table[i][src->b[i*2+1]] & 0x0f); } @@ -204,15 +202,17 @@ static void SBOX(struct BIT64 *src) *src = tmp; } - -/// DES round function. -/// XORs src[0..3] with TP(SBOX(E(src[4..7]))). -static void RoundFunction(struct BIT64 *src) +/** + * DES round function. + * + * XORs src[0..3] with TP(SBOX(E(src[4..7]))). + */ +static void des_RoundFunction(struct des_bit64 *src) { - struct BIT64 tmp = *src; - E(&tmp); - SBOX(&tmp); - TP(&tmp); + struct des_bit64 tmp = *src; + des_E(&tmp); + des_SBOX(&tmp); + des_TP(&tmp); src->b[0] ^= tmp.b[4]; src->b[1] ^= tmp.b[5]; @@ -220,24 +220,27 @@ static void RoundFunction(struct BIT64 *src) src->b[3] ^= tmp.b[7]; } - -void des_decrypt_block(struct BIT64 *block) +/// @copydoc des_interface::decrypt_block() +void des_decrypt_block(struct des_bit64 *block) { - IP(block); - RoundFunction(block); - FP(block); + des_IP(block); + des_RoundFunction(block); + des_FP(block); } - -void des_decrypt(unsigned char* data, size_t size) +/// @copydoc des_interface::decrypt() +void des_decrypt(unsigned char *data, size_t size) { - struct BIT64 *p = (struct BIT64 *)data; + struct des_bit64 *p = (struct des_bit64 *)data; size_t i; - for( i = 0; i*8 < size; i += 8 ) + for (i = 0; i*8 < size; i += 8) des->decrypt_block(p); } +/** + * Interface base initialization. + */ void des_defaults(void) { des = &des_s; diff --git a/src/common/des.h b/src/common/des.h index 61cf2c04a..9924a044b 100644 --- a/src/common/des.h +++ b/src/common/des.h @@ -23,17 +23,40 @@ #include "common/hercules.h" +/** + * @file + * + * DES (Data Encryption Standard) algorithm, modified version. + * + * @see http://www.eathena.ws/board/index.php?autocom=bugtracker&showbug=5099 + * @see http://en.wikipedia.org/wiki/Data_Encryption_Standard + * @see http://en.wikipedia.org/wiki/DES_supplementary_material + */ + /* Struct definitions */ /// One 64-bit block. -struct BIT64 { +struct des_bit64 { uint8_t b[8]; }; /* Interface */ +/// The des interface. struct des_interface { - void (*decrypt_block) (struct BIT64 *block); + /** + * Decrypts a block. + * + * @param[in,out] block The block to decrypt (in-place). + */ + void (*decrypt_block) (struct des_bit64 *block); + + /** + * Decrypts a buffer. + * + * @param [in,out] data The buffer to decrypt (in-place). + * @param [in] size The size of the data. + */ void (*decrypt) (unsigned char *data, size_t size); }; @@ -41,6 +64,6 @@ struct des_interface { void des_defaults(void); #endif // HERCULES_CORE -HPShared struct des_interface *des; +HPShared struct des_interface *des; ///< Pointer to the des interface implementation. #endif // COMMON_DES_H diff --git a/src/common/grfio.c b/src/common/grfio.c index c5c38455d..7f860acc6 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -121,9 +121,9 @@ static uint8_t grf_substitution(uint8_t in) } #if 0 /* this is not used anywhere, is it ok to delete? */ -static void grf_shuffle_enc(struct BIT64 *src) +static void grf_shuffle_enc(struct des_bit64 *src) { - struct BIT64 out; + struct des_bit64 out; out.b[0] = src->b[3]; out.b[1] = src->b[4]; @@ -138,9 +138,9 @@ static void grf_shuffle_enc(struct BIT64 *src) } #endif // 0 -static void grf_shuffle_dec(struct BIT64 *src) +static void grf_shuffle_dec(struct des_bit64 *src) { - struct BIT64 out; + struct des_bit64 out; out.b[0] = src->b[3]; out.b[1] = src->b[4]; @@ -156,8 +156,8 @@ static void grf_shuffle_dec(struct BIT64 *src) static void grf_decode_header(unsigned char* buf, size_t len) { - struct BIT64 *p = (struct BIT64 *)buf; - size_t nblocks = len / sizeof(struct BIT64); + struct des_bit64 *p = (struct des_bit64 *)buf; + size_t nblocks = len / sizeof(struct des_bit64); size_t i; // first 20 blocks are all des-encrypted @@ -169,8 +169,8 @@ static void grf_decode_header(unsigned char* buf, size_t len) static void grf_decode_full(unsigned char* buf, size_t len, int cycle) { - struct BIT64 *p = (struct BIT64 *)buf; - size_t nblocks = len / sizeof(struct BIT64); + struct des_bit64 *p = (struct des_bit64 *)buf; + size_t nblocks = len / sizeof(struct des_bit64); int dcycle, scycle; size_t i, j; -- cgit v1.2.3-70-g09d2 From d0358265beff02fd9c094ba480f1a9583035cf64 Mon Sep 17 00:00:00 2001 From: hemagx Date: Tue, 1 Mar 2016 08:16:37 +0200 Subject: Interface grfio.c --- src/common/core.c | 2 ++ src/common/grfio.c | 23 +++++++++++++++++++---- src/common/grfio.h | 28 ++++++++++++++++++---------- src/map/clif.c | 5 +++-- src/map/map.c | 18 +++++++++--------- src/plugins/HPMHooking.c | 1 + src/tool/mapcache.c | 10 +++++----- tools/HPMHookGen/HPMDataCheckGen.pl | 2 ++ tools/HPMHookGen/HPMHookGen.pl | 3 +++ vcproj-11/char-server.vcxproj | 16 +++++++++------- vcproj-11/char-server.vcxproj.filters | 6 ++++++ vcproj-11/login-server.vcxproj | 18 ++++++++++-------- vcproj-11/login-server.vcxproj.filters | 6 ++++++ vcproj-12/char-server.vcxproj | 16 +++++++++------- vcproj-12/char-server.vcxproj.filters | 6 ++++++ vcproj-12/login-server.vcxproj | 18 ++++++++++-------- vcproj-12/login-server.vcxproj.filters | 6 ++++++ vcproj-14/char-server.vcxproj | 16 +++++++++------- vcproj-14/char-server.vcxproj.filters | 6 ++++++ vcproj-14/login-server.vcxproj | 18 ++++++++++-------- vcproj-14/login-server.vcxproj.filters | 6 ++++++ 21 files changed, 155 insertions(+), 75 deletions(-) (limited to 'src/common/grfio.c') diff --git a/src/common/core.c b/src/common/core.c index 508a728f0..63123dfea 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -27,6 +27,7 @@ #include "common/console.h" #include "common/db.h" #include "common/des.h" +#include "common/grfio.h" #include "common/memmgr.h" #include "common/mmo.h" #include "common/nullpo.h" @@ -257,6 +258,7 @@ void core_defaults(void) { showmsg_defaults(); cmdline_defaults(); des_defaults(); + grfio_defaults(); // Note: grfio is lazily loaded. grfio->init() and grfio->final() are not automatically called. #ifndef MINICORE mutex_defaults(); libconfig_defaults(); diff --git a/src/common/grfio.c b/src/common/grfio.c index 7f860acc6..fdd599ae7 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -76,6 +76,9 @@ int gentry_maxentry = 0; // the path to the data directory char data_dir[1024] = ""; +struct grfio_interface grfio_s; +struct grfio_interface *grfio; + // little endian char array to uint conversion static unsigned int getlong(unsigned char* p) { @@ -472,7 +475,7 @@ void *grfio_reads(const char *fname, int *size) uLongf len; grf_decode(buf, fsize, entry->type, entry->srclen); len = entry->declen; - decode_zip(buf2, &len, buf, entry->srclen); + grfio->decode_zip(buf2, &len, buf, entry->srclen); if (len != (uLong)entry->declen) { ShowError("decode_zip size mismatch err: %d != %d\n", (int)len, entry->declen); aFree(buf); @@ -639,7 +642,7 @@ static int grfio_entryread(const char *grfname, int gentry) } fclose(fp); grf_filelist = (unsigned char *)aMalloc(eSize); // Get a Extend Size - decode_zip(grf_filelist, &eSize, rBuf, rSize); // Decode function + grfio->decode_zip(grf_filelist, &eSize, rBuf, rSize); // Decode function aFree(rBuf); entrys = getlong(grf_header+0x26) - 7; @@ -759,7 +762,7 @@ static void grfio_resourcecheck(void) } // read resnametable from loaded GRF's, only if it cannot be loaded from the data directory - buf = (char *)grfio_reads("data\\resnametable.txt", &size); + buf = grfio->reads("data\\resnametable.txt", &size); if( buf != NULL ) { char *ptr; @@ -871,3 +874,15 @@ void grfio_init(const char* fname) // Resource check grfio_resourcecheck(); } + +void grfio_defaults(void) +{ + grfio = &grfio_s; + grfio->init = grfio_init; + grfio->final = grfio_final; + grfio->reads = grfio_reads; + grfio->find_file = grfio_find_file; + grfio->crc32 = grfio_crc32; + grfio->decode_zip = decode_zip; + grfio->encode_zip = encode_zip; +} diff --git a/src/common/grfio.h b/src/common/grfio.h index 36ed8fb39..72afad274 100644 --- a/src/common/grfio.h +++ b/src/common/grfio.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -21,16 +21,24 @@ #ifndef COMMON_GRFIO_H #define COMMON_GRFIO_H -#ifdef HERCULES_CORE -void grfio_init(const char* fname); -void grfio_final(void); -void* grfio_reads(const char* fname, int* size); -char* grfio_find_file(const char* fname); -#define grfio_read(fn) grfio_reads((fn), NULL) +#include "common/hercules.h" + +struct grfio_interface { + void (*init) (const char *fname); + void (*final) (void); + void *(*reads) (const char *fname, int *size); + char *(*find_file) (const char *fname); + + unsigned long (*crc32) (const unsigned char *buf, unsigned int len); + int (*decode_zip) (void *dest, unsigned long *destLen, const void *source, unsigned long sourceLen); + int (*encode_zip) (void *dest, unsigned long *destLen, const void *source, unsigned long sourceLen); +}; -unsigned long grfio_crc32(const unsigned char *buf, unsigned int len); -int decode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen); -int encode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen); +#define grfio_read(fn) grfio->reads((fn), NULL) + +#ifdef HERCULES_CORE +void grfio_defaults(void); #endif // HERCULES_CORE +HPShared struct grfio_interface *grfio; #endif /* COMMON_GRFIO_H */ diff --git a/src/map/clif.c b/src/map/clif.c index 9619a7201..3069e95ad 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -12878,7 +12878,8 @@ bool clif_validate_emblem(const uint8 *emblem, unsigned long emblem_len) { int header = 0, bitmap = 0, offbits = 0, palettesize = 0; nullpo_retr(false, emblem); - if( decode_zip(buf, &buf_len, emblem, emblem_len) != 0 || buf_len < BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE + if (grfio->decode_zip(buf, &buf_len, emblem, emblem_len) != 0 + || buf_len < BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE || RBUFW(buf,0) != 0x4d42 // BITMAPFILEHEADER.bfType (signature) || RBUFL(buf,2) != buf_len // BITMAPFILEHEADER.bfSize (file size) || RBUFL(buf,14) != BITMAPINFOHEADER_SIZE // BITMAPINFOHEADER.biSize (other headers are not supported) diff --git a/src/map/map.c b/src/map/map.c index 30c849ed1..491f6fb59 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -2851,7 +2851,7 @@ void map_cellfromcache(struct map_data *m) { size = (unsigned long)info->xs*(unsigned long)info->ys; // TO-DO: Maybe handle the scenario, if the decoded buffer isn't the same size as expected? [Shinryo] - decode_zip(decode_buffer, &size, m->cellPos+sizeof(struct map_cache_map_info), info->len); + grfio->decode_zip(decode_buffer, &size, m->cellPos+sizeof(struct map_cache_map_info), info->len); CREATE(m->cell, struct mapcell, size); // Set cell properties @@ -3585,11 +3585,11 @@ int map_waterheight(char* mapname) //Look up for the rsw snprintf(fn, sizeof(fn), "data\\%s.rsw", mapname); - if ( (found = grfio_find_file(fn)) ) + if ((found = grfio->find_file(fn))) safestrncpy(fn, found, sizeof(fn)); // replace with real name // read & convert fn - rsw = (char *) grfio_read (fn); + rsw = grfio_read(fn); if (rsw) { //Load water height from file int wh = (int) *(float*)(rsw+166); @@ -3613,7 +3613,7 @@ int map_readgat (struct map_data* m) nullpo_ret(m); sprintf(filename, "data\\%s.gat", m->name); - gat = (uint8 *) grfio_read(filename); + gat = grfio_read(filename); if (gat == NULL) return 0; @@ -5677,8 +5677,8 @@ int do_final(void) { map->map_db->destroy(map->map_db, map->db_final); mapindex->final(); - if(map->enable_grf) - grfio_final(); + if (map->enable_grf) + grfio->final(); db_destroy(map->id_db); db_destroy(map->pc_db); @@ -6122,8 +6122,8 @@ int do_init(int argc, char *argv[]) } } - if(map->enable_grf) - grfio_init(map->GRF_PATH_FILENAME); + if (map->enable_grf) + grfio->init(map->GRF_PATH_FILENAME); map->readallmaps(); diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index 97a36f4ce..0b98ea99f 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -97,6 +97,7 @@ #include "map/storage.h" #include "map/trade.h" #include "map/unit.h" +#include "common/grfio.h" #include "common/mapindex.h" #else #define HPM_SERVER_TYPE SERVER_TYPE_UNKNOWN diff --git a/src/tool/mapcache.c b/src/tool/mapcache.c index 41dfcf022..7bb2f4465 100644 --- a/src/tool/mapcache.c +++ b/src/tool/mapcache.c @@ -77,13 +77,13 @@ int read_map(char *name, struct map_data *m) // Open map GAT sprintf(filename,"data\\%s.gat", name); - gat = (unsigned char *)grfio_read(filename); + gat = grfio_read(filename); if (gat == NULL) return 0; // Open map RSW sprintf(filename,"data\\%s.rsw", name); - rsw = (unsigned char *)grfio_read(filename); + rsw = grfio_read(filename); // Read water height if (rsw) { @@ -139,7 +139,7 @@ bool cache_map(char *name, struct map_data *m) len = (unsigned long)m->xs*(unsigned long)m->ys*2; write_buf = (unsigned char *)aMalloc(len); // Compress the cells and get the compressed length - encode_zip(write_buf, &len, m->cells, m->xs*m->ys); + grfio->encode_zip(write_buf, &len, m->cells, m->xs*m->ys); // Fill the map header safestrncpy(info.name, name, MAP_NAME_LENGTH); @@ -286,7 +286,7 @@ int do_init(int argc, char** argv) cmdline->exec(argc, argv, CMDLINE_OPT_NORMAL); ShowStatus("Initializing grfio with %s\n", grf_list_file); - grfio_init(grf_list_file); + grfio->init(grf_list_file); // Attempt to open the map cache file and force rebuild if not found ShowStatus("Opening map cache: %s\n", map_cache_file); @@ -360,7 +360,7 @@ int do_init(int argc, char** argv) fclose(map_cache_fp); ShowStatus("Finalizing grfio\n"); - grfio_final(); + grfio->final(); ShowInfo("%d maps now in cache\n", header.map_count); diff --git a/tools/HPMHookGen/HPMDataCheckGen.pl b/tools/HPMHookGen/HPMDataCheckGen.pl index f8712f329..e78a7bd93 100644 --- a/tools/HPMHookGen/HPMDataCheckGen.pl +++ b/tools/HPMHookGen/HPMDataCheckGen.pl @@ -47,6 +47,8 @@ foreach my $file (@files) { if ($foldername eq 'COMMON') { if ($filename eq 'MAPINDEX_H') { $plugintypes = 'SERVER_TYPE_CHAR|SERVER_TYPE_MAP'; + } elsif ($filename eq 'GRFIO_H') { + $plugintypes = 'SERVER_TYPE_MAP'; } else { $plugintypes = 'SERVER_TYPE_ALL'; } diff --git a/tools/HPMHookGen/HPMHookGen.pl b/tools/HPMHookGen/HPMHookGen.pl index 2f22193f0..3203e0b70 100755 --- a/tools/HPMHookGen/HPMHookGen.pl +++ b/tools/HPMHookGen/HPMHookGen.pl @@ -306,6 +306,9 @@ foreach my $file (@files) { # Loop through the xml files } elsif ($key eq "mapindex_interface") { push @servertypes, ("map", "char"); # Currently not used by the login server $servermask = 'SERVER_TYPE_MAP|SERVER_TYPE_CHAR'; + } elsif ($key eq "grfio_interface") { + push @servertypes, ("map"); # Currently not used by the login and char servers + $servermask = 'SERVER_TYPE_MAP'; } else { push @servertypes, ("map", "char", "login"); $servermask = 'SERVER_TYPE_ALL'; diff --git a/vcproj-11/char-server.vcxproj b/vcproj-11/char-server.vcxproj index abd954904..7107d2e54 100644 --- a/vcproj-11/char-server.vcxproj +++ b/vcproj-11/char-server.vcxproj @@ -54,7 +54,7 @@ Disabled - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) false false @@ -74,9 +74,9 @@ true - libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -98,8 +98,8 @@ true true true - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;LIBCONFIG_STATIC;YY_USE_CONST;FD_SETSIZE=4096;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) true MultiThreaded @@ -113,9 +113,9 @@ true - libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -148,6 +148,7 @@ + @@ -206,6 +207,7 @@ + diff --git a/vcproj-11/char-server.vcxproj.filters b/vcproj-11/char-server.vcxproj.filters index 8cd2d592f..2299a2402 100644 --- a/vcproj-11/char-server.vcxproj.filters +++ b/vcproj-11/char-server.vcxproj.filters @@ -16,6 +16,9 @@ common + + common + common @@ -153,6 +156,9 @@ common + + common + common diff --git a/vcproj-11/login-server.vcxproj b/vcproj-11/login-server.vcxproj index a715ff5e2..6e9088f10 100644 --- a/vcproj-11/login-server.vcxproj +++ b/vcproj-11/login-server.vcxproj @@ -53,8 +53,8 @@ Disabled - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;WITH_SQL;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) false false @@ -73,9 +73,9 @@ true - libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -97,8 +97,8 @@ true true true - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;WITH_SQL;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) true MultiThreaded @@ -116,9 +116,9 @@ true - libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -170,6 +170,7 @@ + @@ -206,6 +207,7 @@ + diff --git a/vcproj-11/login-server.vcxproj.filters b/vcproj-11/login-server.vcxproj.filters index a5beedc71..f41fcdfac 100644 --- a/vcproj-11/login-server.vcxproj.filters +++ b/vcproj-11/login-server.vcxproj.filters @@ -31,6 +31,9 @@ common + + common + common @@ -132,6 +135,9 @@ common + + common + common diff --git a/vcproj-12/char-server.vcxproj b/vcproj-12/char-server.vcxproj index fe4706d14..0c72ef671 100644 --- a/vcproj-12/char-server.vcxproj +++ b/vcproj-12/char-server.vcxproj @@ -53,7 +53,7 @@ Disabled - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) false false @@ -73,9 +73,9 @@ true - libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -97,8 +97,8 @@ true true true - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;LIBCONFIG_STATIC;YY_USE_CONST;FD_SETSIZE=4096;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) true MultiThreaded @@ -112,9 +112,9 @@ true - libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -147,6 +147,7 @@ + @@ -205,6 +206,7 @@ + diff --git a/vcproj-12/char-server.vcxproj.filters b/vcproj-12/char-server.vcxproj.filters index 8cd2d592f..2299a2402 100644 --- a/vcproj-12/char-server.vcxproj.filters +++ b/vcproj-12/char-server.vcxproj.filters @@ -16,6 +16,9 @@ common + + common + common @@ -153,6 +156,9 @@ common + + common + common diff --git a/vcproj-12/login-server.vcxproj b/vcproj-12/login-server.vcxproj index 49d35a400..91fa58082 100644 --- a/vcproj-12/login-server.vcxproj +++ b/vcproj-12/login-server.vcxproj @@ -53,8 +53,8 @@ Disabled - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;WITH_SQL;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) false false @@ -73,9 +73,9 @@ true - libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -97,8 +97,8 @@ true true true - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;WITH_SQL;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) true MultiThreaded @@ -116,9 +116,9 @@ true - libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) true %(IgnoreSpecificDefaultLibraries) true @@ -170,6 +170,7 @@ + @@ -206,6 +207,7 @@ + diff --git a/vcproj-12/login-server.vcxproj.filters b/vcproj-12/login-server.vcxproj.filters index a5beedc71..f41fcdfac 100644 --- a/vcproj-12/login-server.vcxproj.filters +++ b/vcproj-12/login-server.vcxproj.filters @@ -31,6 +31,9 @@ common + + common + common @@ -132,6 +135,9 @@ common + + common + common diff --git a/vcproj-14/char-server.vcxproj b/vcproj-14/char-server.vcxproj index 0868d56ee..542b675bb 100644 --- a/vcproj-14/char-server.vcxproj +++ b/vcproj-14/char-server.vcxproj @@ -54,7 +54,7 @@ Disabled - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) false false @@ -74,9 +74,9 @@ true - libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) %(IgnoreSpecificDefaultLibraries) true $(OutDir)$(ProjectName).pdb @@ -97,8 +97,8 @@ true true true - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;LIBCONFIG_STATIC;YY_USE_CONST;FD_SETSIZE=4096;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) true MultiThreaded @@ -112,9 +112,9 @@ true - libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) %(IgnoreSpecificDefaultLibraries) true $(OutDir)$(ProjectName).pdb @@ -146,6 +146,7 @@ + @@ -204,6 +205,7 @@ + diff --git a/vcproj-14/char-server.vcxproj.filters b/vcproj-14/char-server.vcxproj.filters index 8cd2d592f..2299a2402 100644 --- a/vcproj-14/char-server.vcxproj.filters +++ b/vcproj-14/char-server.vcxproj.filters @@ -16,6 +16,9 @@ common + + common + common @@ -153,6 +156,9 @@ common + + common + common diff --git a/vcproj-14/login-server.vcxproj b/vcproj-14/login-server.vcxproj index d376a9903..445d21ee4 100644 --- a/vcproj-14/login-server.vcxproj +++ b/vcproj-14/login-server.vcxproj @@ -53,8 +53,8 @@ Disabled - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;WITH_SQL;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;_DEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) false false @@ -73,9 +73,9 @@ true - libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmtd.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) %(IgnoreSpecificDefaultLibraries) true $(OutDir)$(ProjectName).pdb @@ -96,8 +96,8 @@ true true true - ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) - WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;WITH_SQL;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) + ..\src;..\3rdparty;..\3rdparty\mysql\include;..\3rdparty\zlib\include;..\3rdparty\msinttypes\include;%(AdditionalIncludeDirectories) + WIN32;_WIN32;__WIN32;NDEBUG;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;FD_SETSIZE=4096;LIBCONFIG_STATIC;YY_USE_CONST;%(PreprocessorDefinitions) true MultiThreaded @@ -115,9 +115,9 @@ true - libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;%(AdditionalDependencies) + libcmt.lib;oldnames.lib;ws2_32.lib;libmysql.lib;zdll.lib;%(AdditionalDependencies) $(OutDir)$(ProjectName).exe - ..\3rdparty\mysql\lib;%(AdditionalLibraryDirectories) + ..\3rdparty\mysql\lib;..\3rdparty\zlib\lib;%(AdditionalLibraryDirectories) %(IgnoreSpecificDefaultLibraries) true $(OutDir)$(ProjectName).pdb @@ -168,6 +168,7 @@ + @@ -204,6 +205,7 @@ + diff --git a/vcproj-14/login-server.vcxproj.filters b/vcproj-14/login-server.vcxproj.filters index a5beedc71..f41fcdfac 100644 --- a/vcproj-14/login-server.vcxproj.filters +++ b/vcproj-14/login-server.vcxproj.filters @@ -31,6 +31,9 @@ common + + common + common @@ -132,6 +135,9 @@ common + + common + common -- cgit v1.2.3-70-g09d2 From 92226422121a7f834fd69b0e67d41e144b2cefea Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 18:58:19 +0100 Subject: Dropped unnecessary typedefs from grfio Signed-off-by: Haru --- src/common/grfio.c | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'src/common/grfio.c') diff --git a/src/common/grfio.c b/src/common/grfio.c index fdd599ae7..4283529d6 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -38,7 +38,7 @@ //---------------------------- // file entry table struct //---------------------------- -typedef struct FILELIST { +struct grf_filelist { int srclen; ///< compressed size int srclen_aligned; int declen; ///< original size @@ -48,7 +48,7 @@ typedef struct FILELIST { char fn[128-4*5]; ///< file name char *fnd; ///< if the file was cloned, contains name of original file int8 gentry; ///< read grf file select -} FILELIST; +}; #define FILELIST_TYPE_FILE 0x01 // entry is a file #define FILELIST_TYPE_ENCRYPT_MIXED 0x02 // encryption mode 0 (header DES + periodic DES/shuffle) @@ -64,7 +64,7 @@ typedef struct FILELIST { //#define GRFIO_LOCAL // stores info about every loaded file -FILELIST* filelist = NULL; +struct grf_filelist *filelist = NULL; int filelist_entrys = 0; int filelist_maxentry = 0; @@ -298,8 +298,8 @@ static int filehash(const char* fname) return hash & 255; } -// finds a FILELIST entry with the specified file name -static FILELIST* filelist_find(const char* fname) +// finds a grf_filelist entry with the specified file name +static struct grf_filelist *filelist_find(const char *fname) { int hash, index; @@ -317,13 +317,15 @@ static FILELIST* filelist_find(const char* fname) // returns the original file name char* grfio_find_file(const char* fname) { - FILELIST *flist = filelist_find(fname); - if (!flist) return NULL; + struct grf_filelist *flist = filelist_find(fname); + if (flist == NULL) + return NULL; return (!flist->fnd ? flist->fn : flist->fnd); } -// adds a FILELIST entry into the list of loaded files -static FILELIST* filelist_add(FILELIST* entry) { +// adds a grf_filelist entry into the list of loaded files +static struct grf_filelist *filelist_add(struct grf_filelist *entry) +{ int hash; nullpo_ret(entry); #ifdef __clang_analyzer__ @@ -334,14 +336,14 @@ static FILELIST* filelist_add(FILELIST* entry) { #define FILELIST_ADDS 1024 // number increment of file lists ` if (filelist_entrys >= filelist_maxentry) { - filelist = (FILELIST *)aRealloc(filelist, (filelist_maxentry + FILELIST_ADDS) * sizeof(FILELIST)); - memset(filelist + filelist_maxentry, '\0', FILELIST_ADDS * sizeof(FILELIST)); + filelist = aRealloc(filelist, (filelist_maxentry + FILELIST_ADDS) * sizeof(struct grf_filelist)); + memset(filelist + filelist_maxentry, '\0', FILELIST_ADDS * sizeof(struct grf_filelist)); filelist_maxentry += FILELIST_ADDS; } #undef FILELIST_ADDS - memcpy(&filelist[filelist_entrys], entry, sizeof(FILELIST)); + memcpy(&filelist[filelist_entrys], entry, sizeof(struct grf_filelist)); hash = filehash(entry->fn); filelist[filelist_entrys].next = filelist_hash[hash]; @@ -352,13 +354,13 @@ static FILELIST* filelist_add(FILELIST* entry) { return &filelist[filelist_entrys - 1]; } -// adds a new FILELIST entry or overwrites an existing one -static FILELIST* filelist_modify(FILELIST* entry) +// adds a new grf_filelist entry or overwrites an existing one +static struct grf_filelist *filelist_modify(struct grf_filelist *entry) { - FILELIST* fentry = filelist_find(entry->fn); + struct grf_filelist *fentry = filelist_find(entry->fn); if (fentry != NULL) { int tmp = fentry->next; - memcpy(fentry, entry, sizeof(FILELIST)); + memcpy(fentry, entry, sizeof(struct grf_filelist)); fentry->next = tmp; } else { fentry = filelist_add(entry); @@ -373,7 +375,7 @@ static void filelist_compact(void) return; if (filelist_entrys < filelist_maxentry) { - filelist = (FILELIST *)aRealloc(filelist, filelist_entrys * sizeof(FILELIST)); + filelist = aRealloc(filelist, filelist_entrys * sizeof(struct grf_filelist)); filelist_maxentry = filelist_entrys; } } @@ -408,7 +410,7 @@ static void grfio_localpath_create(char* buffer, size_t size, const char* filena /// Reads a file into a newly allocated buffer (from grf or data directory). void *grfio_reads(const char *fname, int *size) { - FILELIST* entry = filelist_find(fname); + struct grf_filelist *entry = filelist_find(fname); if (entry == NULL || entry->gentry <= 0) { // LocalFileCheck char lfname[256]; @@ -579,7 +581,7 @@ static int grfio_entryread(const char *grfname, int gentry) // Get an entry for (entry = 0, ofs = 0; entry < entrys; ++entry) { - FILELIST aentry; + struct grf_filelist aentry; int ofs2 = ofs+getlong(grf_filelist+ofs)+4; unsigned char type = grf_filelist[ofs2+12]; if (type&FILELIST_TYPE_FILE) { @@ -650,7 +652,7 @@ static int grfio_entryread(const char *grfname, int gentry) // Get an entry for (entry = 0, ofs = 0; entry < entrys; ++entry) { - FILELIST aentry; + struct grf_filelist aentry; char *fname = (char*)(grf_filelist+ofs); int ofs2 = ofs + (int)strlen(fname)+1; int type = grf_filelist[ofs2+12]; @@ -698,7 +700,7 @@ static bool grfio_parse_restable_row(const char* row) char w1[256], w2[256]; char src[256], dst[256]; char local[256]; - FILELIST* entry; + struct grf_filelist *entry; if (sscanf(row, "%255[^#\r\n]#%255[^#\r\n]#", w1, w2) != 2) return false; @@ -712,8 +714,8 @@ static bool grfio_parse_restable_row(const char* row) entry = filelist_find(dst); if( entry != NULL ) {// alias for GRF resource - FILELIST fentry; - memcpy(&fentry, entry, sizeof(FILELIST)); + struct grf_filelist fentry; + memcpy(&fentry, entry, sizeof(struct grf_filelist)); safestrncpy(fentry.fn, src, sizeof(fentry.fn)); fentry.fnd = aStrdup(dst); filelist_modify(&fentry); @@ -723,7 +725,7 @@ static bool grfio_parse_restable_row(const char* row) grfio_localpath_create(local, sizeof(local), dst); if( exists(local) ) {// alias for local resource - FILELIST fentry; + struct grf_filelist fentry; memset(&fentry, 0, sizeof(fentry)); safestrncpy(fentry.fn, src, sizeof(fentry.fn)); fentry.fnd = aStrdup(dst); -- cgit v1.2.3-70-g09d2 From 5196ad065674b65b030d30793b611823e18f0560 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 14 Mar 2016 01:25:13 +0100 Subject: Various changes to the grfio interface Mostly stylistic changes. Cleaned up documentation. Signed-off-by: Haru --- src/common/grfio.c | 430 ++++++++++++++++++++++++++++++----------------------- src/common/grfio.h | 88 ++++++++++- src/map/map.c | 3 +- 3 files changed, 332 insertions(+), 189 deletions(-) (limited to 'src/common/grfio.c') diff --git a/src/common/grfio.c b/src/common/grfio.c index 4283529d6..0a9708f17 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -35,9 +35,11 @@ #include #include -//---------------------------- -// file entry table struct -//---------------------------- +/** @file + * Implementation of the GRF I/O interface. + */ + +/// File entry table struct. struct grf_filelist { int srclen; ///< compressed size int srclen_aligned; @@ -50,9 +52,11 @@ struct grf_filelist { int8 gentry; ///< read grf file select }; -#define FILELIST_TYPE_FILE 0x01 // entry is a file -#define FILELIST_TYPE_ENCRYPT_MIXED 0x02 // encryption mode 0 (header DES + periodic DES/shuffle) -#define FILELIST_TYPE_ENCRYPT_HEADER 0x04 // encryption mode 1 (header DES only) +enum grf_filelist_type { + FILELIST_TYPE_FILE = 0x01, ///< entry is a file + FILELIST_TYPE_ENCRYPT_MIXED = 0x02, ///< encryption mode 0 (header DES + periodic DES/shuffle) + FILELIST_TYPE_ENCRYPT_HEADER = 0x04, ///< encryption mode 1 (header DES only) +}; //gentry ... > 0 : data read from a grf file (gentry_table[gentry-1]) //gentry ... 0 : data read from a local file (data directory) @@ -80,23 +84,26 @@ struct grfio_interface grfio_s; struct grfio_interface *grfio; // little endian char array to uint conversion -static unsigned int getlong(unsigned char* p) +static unsigned int getlong(unsigned char *p) { return (p[0] << 0 | p[1] << 8 | p[2] << 16 | p[3] << 24); } -static void NibbleSwap(unsigned char* src, int len) +static void NibbleSwap(unsigned char *src, int len) { - while( len > 0 ) - { + while (len > 0) { *src = (*src >> 4) | (*src << 4); ++src; --len; } } -/// Substitutes some specific values for others, leaves rest intact. Obfuscation. -/// NOTE: Operation is symmetric (calling it twice gives back the original input). +/** + * (De)-obfuscates data. + * + * Substitutes some specific values for others, leaves rest intact. + * NOTE: Operation is symmetric (calling it twice gives back the original input). + */ static uint8_t grf_substitution(uint8_t in) { uint8_t out; @@ -157,20 +164,33 @@ static void grf_shuffle_dec(struct des_bit64 *src) *src = out; } -static void grf_decode_header(unsigned char* buf, size_t len) +/** + * Decodes header-encrypted grf data. + * + * @param[in,out] buf Data to decode (in-place). + * @param[in] len Length of the data. + */ +static void grf_decode_header(unsigned char *buf, size_t len) { struct des_bit64 *p = (struct des_bit64 *)buf; size_t nblocks = len / sizeof(struct des_bit64); size_t i; // first 20 blocks are all des-encrypted - for( i = 0; i < 20 && i < nblocks; ++i ) + for (i = 0; i < 20 && i < nblocks; ++i) des->decrypt_block(&p[i]); // the rest is plaintext, done. } -static void grf_decode_full(unsigned char* buf, size_t len, int cycle) +/** + * Decodes fully encrypted grf data + * + * @param[in,out] buf Data to decode (in-place). + * @param[in] len Length of the data. + * @param[in] cycle The current decoding cycle. + */ +static void grf_decode_full(unsigned char *buf, size_t len, int cycle) { struct des_bit64 *p = (struct des_bit64 *)buf; size_t nblocks = len / sizeof(struct des_bit64); @@ -178,7 +198,7 @@ static void grf_decode_full(unsigned char* buf, size_t len, int cycle) size_t i, j; // first 20 blocks are all des-encrypted - for( i = 0; i < 20 && i < nblocks; ++i ) + for (i = 0; i < 20 && i < nblocks; ++i) des->decrypt_block(&p[i]); // after that only one of every 'dcycle' blocks is des-encrypted @@ -189,17 +209,16 @@ static void grf_decode_full(unsigned char* buf, size_t len, int cycle) // so decrypt/de-shuffle periodically j = (size_t)-1; // 0, adjusted to fit the ++j step - for( i = 20; i < nblocks; ++i ) - { - if( i % dcycle == 0 ) - {// decrypt block + for (i = 20; i < nblocks; ++i) { + if (i % dcycle == 0) { + // decrypt block des->decrypt_block(&p[i]); continue; } ++j; - if( j % scycle == 0 && j != 0 ) - {// de-shuffle block + if (j % scycle == 0 && j != 0) { + // de-shuffle block grf_shuffle_dec(&p[i]); continue; } @@ -208,22 +227,25 @@ static void grf_decode_full(unsigned char* buf, size_t len, int cycle) } } -/// Decodes grf data. -/// @param buf data to decode (in-place) -/// @param len length of the data -/// @param entry_type flags associated with the data -/// @param entry_len true (unaligned) length of the data -static void grf_decode(unsigned char* buf, size_t len, char entry_type, int entry_len) +/** + * Decodes grf data. + * + * @param[in,out] buf Data to decode (in-place). + * @param[in] len Length of the data + * @param[in] entry_type Flags associated with the data. + * @param[in] entry_len True (unaligned) length of the data. + */ +static void grf_decode(unsigned char *buf, size_t len, char entry_type, int entry_len) { - if( entry_type & FILELIST_TYPE_ENCRYPT_MIXED ) - {// fully encrypted + if (entry_type & FILELIST_TYPE_ENCRYPT_MIXED) { + // fully encrypted int digits; int cycle; int i; // compute number of digits of the entry length digits = 1; - for( i = 10; i <= entry_len; i *= 10 ) + for (i = 10; i <= entry_len; i *= 10) ++digits; // choose size of gap between two encrypted blocks @@ -235,51 +257,47 @@ static void grf_decode(unsigned char* buf, size_t len, char entry_type, int entr : digits + 15; grf_decode_full(buf, len, cycle); - } - else - if( entry_type & FILELIST_TYPE_ENCRYPT_HEADER ) - {// header encrypted + } else if (entry_type & FILELIST_TYPE_ENCRYPT_HEADER) { + // header encrypted grf_decode_header(buf, len); } - else - {// plaintext - ; - } + /* else plaintext */ } -/****************************************************** - *** Zlib Subroutines *** - ******************************************************/ +/* Zlib Subroutines */ -/// zlib crc32 -unsigned long grfio_crc32(const unsigned char* buf, unsigned int len) +/// @copydoc grfio_interface::crc32() +unsigned long grfio_crc32(const unsigned char *buf, unsigned int len) { return crc32(crc32(0L, Z_NULL, 0), buf, len); } -/// zlib uncompress -int decode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen) +/// @copydoc grfio_interface::decode_zip +int grfio_decode_zip(void *dest, unsigned long *dest_len, const void *source, unsigned long source_len) { - return uncompress((Bytef*)dest, destLen, (const Bytef*)source, sourceLen); + return uncompress(dest, dest_len, source, source_len); } -/// zlib compress -int encode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen) { - if( *destLen == 0 ) /* [Ind/Hercules] */ - *destLen = compressBound(sourceLen); - if( dest == NULL ) { /* [Ind/Hercules] */ - CREATE(dest, unsigned char, *destLen); +/// @copydoc grfio_interface::encode_zip +int grfio_encode_zip(void *dest, unsigned long *dest_len, const void *source, unsigned long source_len) +{ + if (*dest_len == 0) /* [Ind/Hercules] */ + *dest_len = compressBound(source_len); + if (dest == NULL) { + /* [Ind/Hercules] */ + CREATE(dest, unsigned char, *dest_len); } - return compress((Bytef*)dest, destLen, (const Bytef*)source, sourceLen); + return compress(dest, dest_len, source, source_len); } -/*********************************************************** - *** File List Subroutines *** - ***********************************************************/ -// file list hash table +/* File List Subroutines */ + +/// File list hash table int filelist_hash[256]; -// initializes the table that holds the first elements of all hash chains +/** + * Initializes the table that holds the first elements of all hash chains + */ static void hashinit(void) { int i; @@ -287,26 +305,37 @@ static void hashinit(void) filelist_hash[i] = -1; } -// hashes a filename string into a number from {0..255} -static int filehash(const char* fname) +/** + * Hashes a filename string into a number from {0..255} + * + * @param fname The filename to hash. + * @return The hash. + */ +static int grf_filehash(const char *fname) { - unsigned int hash = 0; - while(*fname) { + uint32 hash = 0; + while (*fname != '\0') { hash = (hash<<1) + (hash>>7)*9 + TOLOWER(*fname); fname++; } return hash & 255; } -// finds a grf_filelist entry with the specified file name -static struct grf_filelist *filelist_find(const char *fname) +/** + * Finds a grf_filelist entry with the specified file name + * + * @param fname The file to find. + * @return The file entry. + * @retval NULL if the file wasn't found. + */ +static struct grf_filelist *grfio_filelist_find(const char *fname) { int hash, index; - if (!filelist) + if (filelist == NULL) return NULL; - hash = filelist_hash[filehash(fname)]; + hash = filelist_hash[grf_filehash(fname)]; for (index = hash; index != -1; index = filelist[index].next) if(!strcmpi(filelist[index].fn, fname)) break; @@ -314,17 +343,22 @@ static struct grf_filelist *filelist_find(const char *fname) return (index >= 0) ? &filelist[index] : NULL; } -// returns the original file name -char* grfio_find_file(const char* fname) +/// @copydoc grfio_interface::find_file() +const char *grfio_find_file(const char *fname) { - struct grf_filelist *flist = filelist_find(fname); + struct grf_filelist *flist = grfio_filelist_find(fname); if (flist == NULL) return NULL; return (!flist->fnd ? flist->fn : flist->fnd); } -// adds a grf_filelist entry into the list of loaded files -static struct grf_filelist *filelist_add(struct grf_filelist *entry) +/** + * Adds a grf_filelist entry into the list of loaded files. + * + * @param entry The entry to add. + * @return A pointer to the added entry. + */ +static struct grf_filelist *grfio_filelist_add(struct grf_filelist *entry) { int hash; nullpo_ret(entry); @@ -345,7 +379,7 @@ static struct grf_filelist *filelist_add(struct grf_filelist *entry) memcpy(&filelist[filelist_entrys], entry, sizeof(struct grf_filelist)); - hash = filehash(entry->fn); + hash = grf_filehash(entry->fn); filelist[filelist_entrys].next = filelist_hash[hash]; filelist_hash[hash] = filelist_entrys; @@ -354,22 +388,27 @@ static struct grf_filelist *filelist_add(struct grf_filelist *entry) return &filelist[filelist_entrys - 1]; } -// adds a new grf_filelist entry or overwrites an existing one -static struct grf_filelist *filelist_modify(struct grf_filelist *entry) +/** + * Adds a new grf_filelist entry or overwrites an existing one. + * + * @param entry The entry to add. + * @return A pointer to the added entry. + */ +static struct grf_filelist *grfio_filelist_modify(struct grf_filelist *entry) { - struct grf_filelist *fentry = filelist_find(entry->fn); + struct grf_filelist *fentry = grfio_filelist_find(entry->fn); if (fentry != NULL) { int tmp = fentry->next; memcpy(fentry, entry, sizeof(struct grf_filelist)); fentry->next = tmp; } else { - fentry = filelist_add(entry); + fentry = grfio_filelist_add(entry); } return fentry; } -// shrinks the file list array if too long -static void filelist_compact(void) +/// Shrinks the file list array if too long. +static void grfio_filelist_compact(void) { if (filelist == NULL) return; @@ -380,37 +419,38 @@ static void filelist_compact(void) } } -/*********************************************************** - *** Grfio Subroutines *** - ***********************************************************/ +/* GRF I/O Subroutines */ -/// Combines are resource path with the data folder location to create local resource path. -static void grfio_localpath_create(char* buffer, size_t size, const char* filename) +/** + * Combines a resource path with the data folder location to create local + * resource path. + * + * @param[out] buffer The output buffer. + * @param[in] size The size of the output buffer. + * @param[in] filename Resource path. + */ +static void grfio_localpath_create(char *buffer, size_t size, const char *filename) { - unsigned int i; + int i; size_t len; len = strlen(data_dir); - if( data_dir[0] == '\0' || data_dir[len-1] == '/' || data_dir[len-1] == '\\' ) - { + if (data_dir[0] == '\0' || data_dir[len-1] == '/' || data_dir[len-1] == '\\') safesnprintf(buffer, size, "%s%s", data_dir, filename); - } else - { safesnprintf(buffer, size, "%s/%s", data_dir, filename); - } // normalize path - for( i = 0; buffer[i] != '\0'; ++i ) - if( buffer[i] == '\\' ) + for (i = 0; buffer[i] != '\0'; ++i) + if (buffer[i] == '\\') buffer[i] = '/'; } -/// Reads a file into a newly allocated buffer (from grf or data directory). +/// @copydoc grfio_interface::reads() void *grfio_reads(const char *fname, int *size) { - struct grf_filelist *entry = filelist_find(fname); + struct grf_filelist *entry = grfio_filelist_find(fname); if (entry == NULL || entry->gentry <= 0) { // LocalFileCheck char lfname[256]; @@ -429,7 +469,7 @@ void *grfio_reads(const char *fname, int *size) return NULL; } fseek(in,0,SEEK_SET); - buf = (unsigned char *)aMalloc(declen+1); // +1 for resnametable zero-termination + buf = aMalloc(declen+1); // +1 for resnametable zero-termination buf[declen] = '\0'; if (fread(buf, 1, declen, in) != (size_t)declen) { ShowError("An error occurred in fread grfio_reads, fname=%s \n",fname); @@ -459,7 +499,7 @@ void *grfio_reads(const char *fname, int *size) if (in != NULL) { int fsize = entry->srclen_aligned; - unsigned char *buf = (unsigned char *)aMalloc(fsize); + unsigned char *buf = aMalloc(fsize); unsigned char *buf2 = NULL; if (fseek(in, entry->srcpos, SEEK_SET) != 0 || fread(buf, 1, fsize, in) != (size_t)fsize) { @@ -470,7 +510,7 @@ void *grfio_reads(const char *fname, int *size) } fclose(in); - buf2 = (unsigned char *)aMalloc(entry->declen+1); // +1 for resnametable zero-termination + buf2 = aMalloc(entry->declen+1); // +1 for resnametable zero-termination buf2[entry->declen] = '\0'; if (entry->type & FILELIST_TYPE_FILE) { // file @@ -503,35 +543,51 @@ void *grfio_reads(const char *fname, int *size) return NULL; } -/// Decodes encrypted filename from a version 01xx grf index. -static char* decode_filename(unsigned char* buf, int len) +/** + * Decodes encrypted filename from a version 01xx grf index. + * + * @param[in,out] buf The encrypted filename (decrypted in-place). + * @param[in] len The filename length. + * @return A pointer to the decrypted filename. + */ +static char *grfio_decode_filename(unsigned char *buf, int len) { - int lop; - for(lop=0;lopdecrypt(&buf[lop],8); + int i; + for (i = 0; i < len; i += 8) { + NibbleSwap(&buf[i],8); + des->decrypt(&buf[i],8); } return (char*)buf; } -/// Compares file extension against known large file types. -/// @return true if the file should undergo full mode 0 decryption, and true otherwise. -static bool isFullEncrypt(const char* fname) +/** + * Compares file extension against known large file types. + * + * @param fname The file name. + * @return true if the file should undergo full mode 0 decryption, and true otherwise. + */ +static bool grfio_is_full_encrypt(const char *fname) { - const char* ext = strrchr(fname, '.'); - if( ext != NULL ) { - static const char extensions[4][5] = { ".gnd", ".gat", ".act", ".str" }; - size_t i; - for( i = 0; i < ARRAYLENGTH(extensions); ++i ) - if( strcmpi(ext, extensions[i]) == 0 ) + const char *ext = strrchr(fname, '.'); + if (ext != NULL) { + static const char *extensions[] = { ".gnd", ".gat", ".act", ".str" }; + int i; + for (i = 0; i < ARRAYLENGTH(extensions); ++i) + if (strcmpi(ext, extensions[i]) == 0) return false; } return true; } -/// Loads all entries in the specified grf file into the filelist. -/// @param gentry index of the grf file name in the gentry_table +/** + * Loads all entries in the specified grf file into the filelist. + * + * @param grfname Name of the grf file. + * @param gentry Index of the grf file name in the gentry_table. + * @return Error code. + * @retval 0 in case of success. + */ static int grfio_entryread(const char *grfname, int gentry) { long grf_size; @@ -540,12 +596,11 @@ static int grfio_entryread(const char *grfname, int gentry) unsigned char *grf_filelist; FILE *fp = fopen(grfname, "rb"); - if( fp == NULL ) { - ShowWarning("GRF data file not found: '%s'\n",grfname); + if (fp == NULL) { + ShowWarning("GRF data file not found: '%s'\n", grfname); return 1; // 1:not found error - } else { - ShowInfo("GRF data file found: '%s'\n",grfname); } + ShowInfo("GRF data file found: '%s'\n", grfname); fseek(fp,0,SEEK_END); grf_size = ftell(fp); @@ -556,7 +611,7 @@ static int grfio_entryread(const char *grfname, int gentry) fclose(fp); return 2; // 2:file format error } - if (strcmp((const char*)grf_header,"Master of Magic") != 0 || fseek(fp,getlong(grf_header+0x1e),SEEK_CUR) != 0) { + if (strcmp((const char*)grf_header, "Master of Magic") != 0 || fseek(fp, getlong(grf_header+0x1e), SEEK_CUR) != 0) { fclose(fp); ShowError("GRF %s read error\n", grfname); return 2; // 2:file format error @@ -566,9 +621,8 @@ static int grfio_entryread(const char *grfname, int gentry) if (grf_version == 0x01) { // ****** Grf version 01xx ****** - long list_size; - list_size = grf_size - ftell(fp); - grf_filelist = (unsigned char *)aMalloc(list_size); + long list_size = grf_size - ftell(fp); + grf_filelist = aMalloc(list_size); if (fread(grf_filelist,1,list_size,fp) != (size_t)list_size) { ShowError("Couldn't read all grf_filelist element of %s \n", grfname); aFree(grf_filelist); @@ -581,11 +635,11 @@ static int grfio_entryread(const char *grfname, int gentry) // Get an entry for (entry = 0, ofs = 0; entry < entrys; ++entry) { - struct grf_filelist aentry; + struct grf_filelist aentry = { 0 }; int ofs2 = ofs+getlong(grf_filelist+ofs)+4; unsigned char type = grf_filelist[ofs2+12]; if (type&FILELIST_TYPE_FILE) { - char *fname = decode_filename(grf_filelist+ofs+6, grf_filelist[ofs]-6); + char *fname = grfio_decode_filename(grf_filelist+ofs+6, grf_filelist[ofs]-6); int srclen = getlong(grf_filelist+ofs2+0) - getlong(grf_filelist+ofs2+8) - 715; if (strlen(fname) > sizeof(aentry.fn) - 1) { @@ -594,7 +648,7 @@ static int grfio_entryread(const char *grfname, int gentry) return 5; // 5: file name too long } - type |= isFullEncrypt(fname) ? FILELIST_TYPE_ENCRYPT_MIXED : FILELIST_TYPE_ENCRYPT_HEADER; + type |= grfio_is_full_encrypt(fname) ? FILELIST_TYPE_ENCRYPT_MIXED : FILELIST_TYPE_ENCRYPT_HEADER; aentry.srclen = srclen; aentry.srclen_aligned = getlong(grf_filelist+ofs2+4)-37579; @@ -608,7 +662,7 @@ static int grfio_entryread(const char *grfname, int gentry) #else aentry.gentry = (char)(gentry+1); // With no first time LocalFileCheck #endif - filelist_modify(&aentry); + grfio_filelist_modify(&aentry); } ofs = ofs2 + 17; @@ -635,7 +689,7 @@ static int grfio_entryread(const char *grfname, int gentry) return 4; } - rBuf = (unsigned char *)aMalloc(rSize); // Get a Read Size + rBuf = aMalloc(rSize); // Get a Read Size if (fread(rBuf,1,rSize,fp) != rSize) { ShowError("An error occurred in fread \n"); fclose(fp); @@ -643,7 +697,7 @@ static int grfio_entryread(const char *grfname, int gentry) return 4; } fclose(fp); - grf_filelist = (unsigned char *)aMalloc(eSize); // Get a Extend Size + grf_filelist = aMalloc(eSize); // Get a Extend Size grfio->decode_zip(grf_filelist, &eSize, rBuf, rSize); // Decode function aFree(rBuf); @@ -677,84 +731,89 @@ static int grfio_entryread(const char *grfname, int gentry) #else aentry.gentry = (char)(gentry+1); // With no first time LocalFileCheck #endif - filelist_modify(&aentry); + grfio_filelist_modify(&aentry); } ofs = ofs2 + 17; } aFree(grf_filelist); - } else {// ****** Grf Other version ****** + } else { + // ****** Grf Other version ****** fclose(fp); ShowError("GRF version %04x not supported\n",getlong(grf_header+0x2a)); return 4; } - filelist_compact(); // Unnecessary area release of filelist + grfio_filelist_compact(); // Unnecessary area release of filelist return 0; // 0:no error } -static bool grfio_parse_restable_row(const char* row) +/** + * Parses a Resource file table row. + * + * @param row The row data. + * @return success state. + * @retval true in case of success. + */ +static bool grfio_parse_restable_row(const char *row) { char w1[256], w2[256]; char src[256], dst[256]; char local[256]; - struct grf_filelist *entry; + struct grf_filelist *entry = NULL; if (sscanf(row, "%255[^#\r\n]#%255[^#\r\n]#", w1, w2) != 2) return false; - if( strstr(w2, ".gat") == NULL && strstr(w2, ".rsw") == NULL ) + if (strstr(w2, ".gat") == NULL && strstr(w2, ".rsw") == NULL) return false; // we only need the maps' GAT and RSW files sprintf(src, "data\\%s", w1); sprintf(dst, "data\\%s", w2); - entry = filelist_find(dst); - if( entry != NULL ) - {// alias for GRF resource - struct grf_filelist fentry; - memcpy(&fentry, entry, sizeof(struct grf_filelist)); + entry = grfio_filelist_find(dst); + if (entry != NULL) { + // alias for GRF resource + struct grf_filelist fentry = *entry; safestrncpy(fentry.fn, src, sizeof(fentry.fn)); fentry.fnd = aStrdup(dst); - filelist_modify(&fentry); + grfio_filelist_modify(&fentry); return true; } grfio_localpath_create(local, sizeof(local), dst); - if( exists(local) ) - {// alias for local resource - struct grf_filelist fentry; - memset(&fentry, 0, sizeof(fentry)); + if (exists(local)) { + // alias for local resource + struct grf_filelist fentry = { 0 }; safestrncpy(fentry.fn, src, sizeof(fentry.fn)); fentry.fnd = aStrdup(dst); - filelist_modify(&fentry); + grfio_filelist_modify(&fentry); return true; } return false; } -/// Grfio Resource file check. +/** + * Grfio Resource file check. + */ static void grfio_resourcecheck(void) { char restable[256]; - char *buf; - int size; - FILE* fp; - int i = 0; + char *buf = NULL; + FILE *fp = NULL; + int size = 0, i = 0; // read resnametable from data directory and return if successful grfio_localpath_create(restable, sizeof(restable), "data\\resnametable.txt"); fp = fopen(restable, "rb"); - if( fp != NULL ) - { + if (fp != NULL) { char line[256]; - while( fgets(line, sizeof(line), fp) ) - { - if( grfio_parse_restable_row(line) ) + while (fgets(line, sizeof(line), fp)) { + if (grfio_parse_restable_row(line)) ++i; } @@ -765,19 +824,18 @@ static void grfio_resourcecheck(void) // read resnametable from loaded GRF's, only if it cannot be loaded from the data directory buf = grfio->reads("data\\resnametable.txt", &size); - if( buf != NULL ) - { - char *ptr; + if (buf != NULL) { + char *ptr = NULL; buf[size] = '\0'; ptr = buf; - while( ptr - buf < size ) - { - if( grfio_parse_restable_row(ptr) ) + while (ptr - buf < size) { + if (grfio_parse_restable_row(ptr)) ++i; ptr = strchr(ptr, '\n'); - if( ptr == NULL ) break; + if (ptr == NULL) + break; ptr++; } @@ -787,13 +845,19 @@ static void grfio_resourcecheck(void) } } -/// Reads a grf file and adds it to the list. -static int grfio_add(const char* fname) +/** + * Reads a grf file and adds it to the list. + * + * @param fname The file name to read. + * @return Error code. + * @retval 0 in case of success. + */ +static int grfio_add(const char *fname) { if (gentry_entrys >= gentry_maxentry) { #define GENTRY_ADDS 4 // The number increment of gentry_table entries gentry_maxentry += GENTRY_ADDS; - gentry_table = (char**)aRealloc(gentry_table, gentry_maxentry * sizeof(char*)); + gentry_table = aRealloc(gentry_table, gentry_maxentry * sizeof(char*)); memset(gentry_table + (gentry_maxentry - GENTRY_ADDS), 0, sizeof(char*) * GENTRY_ADDS); #undef GENTRY_ADDS } @@ -803,7 +867,7 @@ static int grfio_add(const char* fname) return grfio_entryread(fname, gentry_entrys - 1); } -/// Finalizes grfio. +/// @copydoc grfio_interface::final() void grfio_final(void) { if (filelist != NULL) { @@ -829,36 +893,33 @@ void grfio_final(void) gentry_entrys = gentry_maxentry = 0; } -/// Initializes grfio. -void grfio_init(const char* fname) +/// @copydoc grfio_interface::init() +void grfio_init(const char *fname) { - FILE* data_conf; + FILE *data_conf; int grf_num = 0; hashinit(); // hash table initialization data_conf = fopen(fname, "r"); - if( data_conf != NULL ) - { + if (data_conf != NULL) { char line[1024]; - while( fgets(line, sizeof(line), data_conf) ) - { + while (fgets(line, sizeof(line), data_conf)) { char w1[1024], w2[1024]; - if( line[0] == '/' && line[1] == '/' ) + if (line[0] == '/' && line[1] == '/') continue; // skip comments if (sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) != 2) continue; // skip unrecognized lines // Entry table reading - if( strcmp(w1, "grf") == 0 ) // GRF file - { - if( grfio_add(w2) == 0 ) + if (strcmp(w1, "grf") == 0) { + // GRF file + if (grfio_add(w2) == 0) ++grf_num; - } - else if( strcmp(w1,"data_dir") == 0 ) // Data directory - { + } else if (strcmp(w1,"data_dir") == 0) { + // Data directory safestrncpy(data_dir, w2, sizeof(data_dir)); } } @@ -867,16 +928,17 @@ void grfio_init(const char* fname) ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n", fname); } - if( grf_num == 0 ) + if (grf_num == 0) ShowInfo("No GRF loaded, using default data directory\n"); // Unnecessary area release of filelist - filelist_compact(); + grfio_filelist_compact(); // Resource check grfio_resourcecheck(); } +/// Interface base initialization. void grfio_defaults(void) { grfio = &grfio_s; @@ -885,6 +947,6 @@ void grfio_defaults(void) grfio->reads = grfio_reads; grfio->find_file = grfio_find_file; grfio->crc32 = grfio_crc32; - grfio->decode_zip = decode_zip; - grfio->encode_zip = encode_zip; + grfio->decode_zip = grfio_decode_zip; + grfio->encode_zip = grfio_encode_zip; } diff --git a/src/common/grfio.h b/src/common/grfio.h index 72afad274..857bc507e 100644 --- a/src/common/grfio.h +++ b/src/common/grfio.h @@ -23,22 +23,102 @@ #include "common/hercules.h" +/** @file + * GRF I/O library. + */ + +/// The GRF I/O interface. struct grfio_interface { + /** + * Interface initialization. + * + * @param fname Name of the configuration file. + */ void (*init) (const char *fname); + + /// Interface finalization. void (*final) (void); + + /** + * Reads a file into a newly allocated buffer (from grf or data directory). + * + * @param[in] fname Name of the file to read. + * @param[out] size Buffer to return the size of the read file (optional). + * @return The file data. + * @retval NULL in case of error. + */ void *(*reads) (const char *fname, int *size); - char *(*find_file) (const char *fname); + /** + * Finds a file in the grf or data directory + * + * @param fname The file to find. + * @return The original file name. + * @retval NULL if the file wasn't found. + */ + const char *(*find_file) (const char *fname); + + /** + * Calculates a CRC32 hash. + * + * @param buf The data to hash. + * @param len Data length. + * + * @return The CRC32 hash. + */ unsigned long (*crc32) (const unsigned char *buf, unsigned int len); - int (*decode_zip) (void *dest, unsigned long *destLen, const void *source, unsigned long sourceLen); - int (*encode_zip) (void *dest, unsigned long *destLen, const void *source, unsigned long sourceLen); + + /** + * Decompresses ZIP data. + * + * Decompresses the source buffer into the destination buffer. + * source_len is the byte length of the source buffer. Upon entry, + * dest_len is the total size of the destination buffer, which must be + * large enough to hold the entire uncompressed data. (The size of the + * uncompressed data must have been saved previously by the compressor + * and transmitted to the decompressor by some mechanism outside the + * scope of this compression library.) Upon exit, dest_len is the + * actual size of the uncompressed buffer. + * + * @param[in,out] dest The destination (uncompressed) buffer. + * @param[in,out] dest_len Max length of the destination buffer, returns length of the decompressed data. + * @param[in] source The source (compressed) buffer. + * @param[in] source_len Source data length. + * @return error code. + * @retval Z_OK in case of success. + */ + int (*decode_zip) (void *dest, unsigned long *dest_len, const void *source, unsigned long source_len); + + /** + * Compresses data to ZIP format. + * + * Compresses the source buffer into the destination buffer. + * source_len is the byte length of the source buffer. Upon entry, + * dest_len is the total size of the destination buffer, which must be + * at least the value returned by compressBound(source_len). Upon + * exit, dest_len is the actual size of the compressed buffer. + * + * @param[in,out] dest The destination (compressed) buffer (if NULL, a new buffer will be created). + * @param[in,out] dest_len Max length of the destination buffer (if 0, it will be calculated). + * @param[in] source The source (uncompressed) buffer. + * @param[in] source_len Source data length. + */ + int (*encode_zip) (void *dest, unsigned long *dest_len, const void *source, unsigned long source_len); }; +/** + * Reads a file into a newly allocated buffer (from grf or data directory) + * + * @param fn The filename to read. + * + * @see grfio_interface::reads() + * @related grfio_interface + */ #define grfio_read(fn) grfio->reads((fn), NULL) #ifdef HERCULES_CORE void grfio_defaults(void); #endif // HERCULES_CORE -HPShared struct grfio_interface *grfio; +HPShared struct grfio_interface *grfio; ///< Pointer to the grfio interface. #endif /* COMMON_GRFIO_H */ diff --git a/src/map/map.c b/src/map/map.c index 491f6fb59..c5ea7c1f3 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -3579,7 +3579,8 @@ void map_flags_init(void) { int map_waterheight(char* mapname) { char fn[256]; - char *rsw, *found; + char *rsw = NULL; + const char *found; nullpo_retr(NO_WATER, mapname); //Look up for the rsw -- cgit v1.2.3-70-g09d2