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/core.c | 4 +++- src/common/des.c | 14 ++++++++++++-- src/common/des.h | 14 ++++++++++---- src/common/grfio.c | 8 ++++---- src/plugins/HPMHooking.c | 1 + 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/common/core.c b/src/common/core.c index e6b522926..ce92a77e3 100644 --- a/src/common/core.c +++ b/src/common/core.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 @@ -25,6 +25,7 @@ #include "common/cbasetypes.h" #include "common/console.h" +#include "common/des.h" #include "common/db.h" #include "common/memmgr.h" #include "common/mmo.h" @@ -253,6 +254,7 @@ void core_defaults(void) { malloc_defaults(); showmsg_defaults(); cmdline_defaults(); + des_defaults(); #ifndef MINICORE libconfig_defaults(); sql_defaults(); diff --git a/src/common/des.c b/src/common/des.c index ce64309f3..c811dd96c 100644 --- a/src/common/des.c +++ b/src/common/des.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 @@ -24,6 +24,9 @@ #include "common/cbasetypes.h" +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 @@ -232,5 +235,12 @@ void des_decrypt(unsigned char* data, size_t size) size_t i; for( i = 0; i*8 < size; i += 8 ) - des_decrypt_block(p); + des->decrypt_block(p); +} + +void des_defaults(void) +{ + des = &des_s; + des->decrypt = des_decrypt; + des->decrypt_block = des_decrypt_block; } diff --git a/src/common/des.h b/src/common/des.h index d62b5cc49..e7460e9fd 100644 --- a/src/common/des.h +++ b/src/common/des.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,14 +21,20 @@ #ifndef COMMON_DES_H #define COMMON_DES_H -#include "common/cbasetypes.h" +#include "common/hercules.h" /// One 64-bit block. typedef struct BIT64 { uint8_t b[8]; } BIT64; +struct des_interface { + void (*decrypt_block) (BIT64* block); + void (*decrypt) (unsigned char* data, size_t size); +}; + #ifdef HERCULES_CORE -void des_decrypt_block(BIT64* block); -void des_decrypt(unsigned char* data, size_t size); +void des_defaults(void); #endif // HERCULES_CORE +HPShared struct des_interface *des; + #endif // COMMON_DES_H 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; } diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index 4fb7911c2..b383d30a5 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -109,6 +109,7 @@ #include "common/conf.h" #include "common/console.h" #include "common/db.h" +#include "common/des.h" #include "common/memmgr.h" #include "common/nullpo.h" #include "common/showmsg.h" -- 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(-) 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(-) 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 ad05eb4f2e531f5a9aec6f27d2f69fd53b6525ac Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 19:57:54 +0100 Subject: HPM Hooks Update Signed-off-by: Haru --- src/common/HPMDataCheck.h | 3 +- src/common/HPMSymbols.inc.h | 6 +++ src/plugins/HPMHooking/HPMHooking.Defs.inc | 6 +++ .../HPMHooking/HPMHooking_char.HPMHooksCore.inc | 9 ++++ .../HPMHooking/HPMHooking_char.HookingPoints.inc | 3 ++ src/plugins/HPMHooking/HPMHooking_char.Hooks.inc | 53 ++++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_char.sources.inc | 1 + .../HPMHooking/HPMHooking_login.HPMHooksCore.inc | 9 ++++ .../HPMHooking/HPMHooking_login.HookingPoints.inc | 3 ++ src/plugins/HPMHooking/HPMHooking_login.Hooks.inc | 53 ++++++++++++++++++++++ .../HPMHooking/HPMHooking_login.sources.inc | 1 + .../HPMHooking/HPMHooking_map.HPMHooksCore.inc | 9 ++++ .../HPMHooking/HPMHooking_map.HookingPoints.inc | 3 ++ src/plugins/HPMHooking/HPMHooking_map.Hooks.inc | 53 ++++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_map.sources.inc | 1 + 15 files changed, 212 insertions(+), 1 deletion(-) diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h index 332857a90..c4eedadbf 100644 --- a/src/common/HPMDataCheck.h +++ b/src/common/HPMDataCheck.h @@ -150,7 +150,8 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = { #define COMMON_DB_H #endif // COMMON_DB_H #ifdef COMMON_DES_H - { "BIT64", sizeof(struct BIT64), SERVER_TYPE_ALL }, + { "des_bit64", sizeof(struct des_bit64), SERVER_TYPE_ALL }, + { "des_interface", sizeof(struct des_interface), SERVER_TYPE_ALL }, #else #define COMMON_DES_H #endif // COMMON_DES_H diff --git a/src/common/HPMSymbols.inc.h b/src/common/HPMSymbols.inc.h index 516222e8e..0e535f54c 100644 --- a/src/common/HPMSymbols.inc.h +++ b/src/common/HPMSymbols.inc.h @@ -68,6 +68,9 @@ struct core_interface *core; #ifdef COMMON_DB_H /* DB */ struct db_interface *DB; #endif // COMMON_DB_H +#ifdef COMMON_DES_H /* des */ +struct des_interface *des; +#endif // COMMON_DES_H #ifdef MAP_DUEL_H /* duel */ struct duel_interface *duel; #endif // MAP_DUEL_H @@ -300,6 +303,9 @@ if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("core", core)) return "core"; #ifdef COMMON_DB_H /* DB */ if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("DB", DB)) return "DB"; #endif // COMMON_DB_H +#ifdef COMMON_DES_H /* des */ +if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("des", des)) return "des"; +#endif // COMMON_DES_H #ifdef MAP_DUEL_H /* duel */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("duel", duel)) return "duel"; #endif // MAP_DUEL_H diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index b6a0a28c0..3ee1f1b3d 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -2271,6 +2271,12 @@ typedef void (*HPMHOOK_post_DB_init) (void); typedef void (*HPMHOOK_pre_DB_final) (void); typedef void (*HPMHOOK_post_DB_final) (void); #endif // COMMON_DB_H +#ifdef COMMON_DES_H /* des */ +typedef void (*HPMHOOK_pre_des_decrypt_block) (struct des_bit64 **block); +typedef void (*HPMHOOK_post_des_decrypt_block) (struct des_bit64 *block); +typedef void (*HPMHOOK_pre_des_decrypt) (unsigned char **data, size_t *size); +typedef void (*HPMHOOK_post_des_decrypt) (unsigned char *data, size_t size); +#endif // COMMON_DES_H #ifdef MAP_DUEL_H /* duel */ typedef int (*HPMHOOK_pre_duel_create) (struct map_session_data **sd, const unsigned int *maxpl); typedef int (*HPMHOOK_post_duel_create) (int retVal___, struct map_session_data *sd, const unsigned int maxpl); diff --git a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc index 3477a58a7..38dd2a7a0 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc @@ -408,6 +408,10 @@ struct { struct HPMHookPoint *HP_DB_init_post; struct HPMHookPoint *HP_DB_final_pre; struct HPMHookPoint *HP_DB_final_post; + struct HPMHookPoint *HP_des_decrypt_block_pre; + struct HPMHookPoint *HP_des_decrypt_block_post; + struct HPMHookPoint *HP_des_decrypt_pre; + struct HPMHookPoint *HP_des_decrypt_post; struct HPMHookPoint *HP_geoip_getcountry_pre; struct HPMHookPoint *HP_geoip_getcountry_post; struct HPMHookPoint *HP_geoip_final_pre; @@ -1813,6 +1817,10 @@ struct { int HP_DB_init_post; int HP_DB_final_pre; int HP_DB_final_post; + int HP_des_decrypt_block_pre; + int HP_des_decrypt_block_post; + int HP_des_decrypt_pre; + int HP_des_decrypt_post; int HP_geoip_getcountry_pre; int HP_geoip_getcountry_post; int HP_geoip_final_pre; @@ -2842,6 +2850,7 @@ struct { struct console_interface console; struct core_interface core; struct db_interface DB; + struct des_interface des; struct geoip_interface geoip; struct inter_auction_interface inter_auction; struct inter_elemental_interface inter_elemental; diff --git a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc index 9844e61b6..a50d1e847 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc @@ -223,6 +223,9 @@ struct HookingPointData HookingPoints[] = { { HP_POP(DB->data2ptr, HP_DB_data2ptr) }, { HP_POP(DB->init, HP_DB_init) }, { HP_POP(DB->final, HP_DB_final) }, +/* des_interface */ + { HP_POP(des->decrypt_block, HP_des_decrypt_block) }, + { HP_POP(des->decrypt, HP_des_decrypt) }, /* geoip_interface */ { HP_POP(geoip->getcountry, HP_geoip_getcountry) }, { HP_POP(geoip->final, HP_geoip_final) }, diff --git a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc index b1f2f58b7..df995eee9 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc @@ -5100,6 +5100,59 @@ void HP_DB_final(void) { } return; } +/* des_interface */ +void HP_des_decrypt_block(struct des_bit64 *block) { + int hIndex = 0; + if( HPMHooks.count.HP_des_decrypt_block_pre ) { + void (*preHookFunc) (struct des_bit64 **block); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_block_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_des_decrypt_block_pre[hIndex].func; + preHookFunc(&block); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.des.decrypt_block(block); + } + if( HPMHooks.count.HP_des_decrypt_block_post ) { + void (*postHookFunc) (struct des_bit64 *block); + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_block_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_des_decrypt_block_post[hIndex].func; + postHookFunc(block); + } + } + return; +} +void HP_des_decrypt(unsigned char *data, size_t size) { + int hIndex = 0; + if( HPMHooks.count.HP_des_decrypt_pre ) { + void (*preHookFunc) (unsigned char **data, size_t *size); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_des_decrypt_pre[hIndex].func; + preHookFunc(&data, &size); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.des.decrypt(data, size); + } + if( HPMHooks.count.HP_des_decrypt_post ) { + void (*postHookFunc) (unsigned char *data, size_t size); + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_des_decrypt_post[hIndex].func; + postHookFunc(data, size); + } + } + return; +} /* geoip_interface */ const char* HP_geoip_getcountry(uint32 ipnum) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_char.sources.inc b/src/plugins/HPMHooking/HPMHooking_char.sources.inc index 21d1f6531..569936c97 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.sources.inc @@ -31,6 +31,7 @@ memcpy(&HPMHooks.source.cmdline, cmdline, sizeof(struct cmdline_interface)); memcpy(&HPMHooks.source.console, console, sizeof(struct console_interface)); memcpy(&HPMHooks.source.core, core, sizeof(struct core_interface)); memcpy(&HPMHooks.source.DB, DB, sizeof(struct db_interface)); +memcpy(&HPMHooks.source.des, des, sizeof(struct des_interface)); memcpy(&HPMHooks.source.geoip, geoip, sizeof(struct geoip_interface)); memcpy(&HPMHooks.source.inter_auction, inter_auction, sizeof(struct inter_auction_interface)); memcpy(&HPMHooks.source.inter_elemental, inter_elemental, sizeof(struct inter_elemental_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc index 3e63de124..393794c71 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc @@ -92,6 +92,10 @@ struct { struct HPMHookPoint *HP_DB_init_post; struct HPMHookPoint *HP_DB_final_pre; struct HPMHookPoint *HP_DB_final_post; + struct HPMHookPoint *HP_des_decrypt_block_pre; + struct HPMHookPoint *HP_des_decrypt_block_post; + struct HPMHookPoint *HP_des_decrypt_pre; + struct HPMHookPoint *HP_des_decrypt_post; struct HPMHookPoint *HP_lclif_init_pre; struct HPMHookPoint *HP_lclif_init_post; struct HPMHookPoint *HP_lclif_final_pre; @@ -725,6 +729,10 @@ struct { int HP_DB_init_post; int HP_DB_final_pre; int HP_DB_final_post; + int HP_des_decrypt_block_pre; + int HP_des_decrypt_block_post; + int HP_des_decrypt_pre; + int HP_des_decrypt_post; int HP_lclif_init_pre; int HP_lclif_init_post; int HP_lclif_final_pre; @@ -1297,6 +1305,7 @@ struct { struct console_interface console; struct core_interface core; struct db_interface DB; + struct des_interface des; struct lclif_interface lclif; struct lclif_interface_private PRIV__lclif; struct libconfig_interface libconfig; diff --git a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc index cd846840b..93c838c46 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc @@ -64,6 +64,9 @@ struct HookingPointData HookingPoints[] = { { HP_POP(DB->data2ptr, HP_DB_data2ptr) }, { HP_POP(DB->init, HP_DB_init) }, { HP_POP(DB->final, HP_DB_final) }, +/* des_interface */ + { HP_POP(des->decrypt_block, HP_des_decrypt_block) }, + { HP_POP(des->decrypt, HP_des_decrypt) }, /* lclif_interface */ { HP_POP(lclif->init, HP_lclif_init) }, { HP_POP(lclif->final, HP_lclif_final) }, diff --git a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc index 23e2e2c90..7771ca05d 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc @@ -911,6 +911,59 @@ void HP_DB_final(void) { } return; } +/* des_interface */ +void HP_des_decrypt_block(struct des_bit64 *block) { + int hIndex = 0; + if( HPMHooks.count.HP_des_decrypt_block_pre ) { + void (*preHookFunc) (struct des_bit64 **block); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_block_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_des_decrypt_block_pre[hIndex].func; + preHookFunc(&block); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.des.decrypt_block(block); + } + if( HPMHooks.count.HP_des_decrypt_block_post ) { + void (*postHookFunc) (struct des_bit64 *block); + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_block_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_des_decrypt_block_post[hIndex].func; + postHookFunc(block); + } + } + return; +} +void HP_des_decrypt(unsigned char *data, size_t size) { + int hIndex = 0; + if( HPMHooks.count.HP_des_decrypt_pre ) { + void (*preHookFunc) (unsigned char **data, size_t *size); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_des_decrypt_pre[hIndex].func; + preHookFunc(&data, &size); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.des.decrypt(data, size); + } + if( HPMHooks.count.HP_des_decrypt_post ) { + void (*postHookFunc) (unsigned char *data, size_t size); + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_des_decrypt_post[hIndex].func; + postHookFunc(data, size); + } + } + return; +} /* lclif_interface */ void HP_lclif_init(void) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_login.sources.inc b/src/plugins/HPMHooking/HPMHooking_login.sources.inc index 10558043c..df0092691 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.sources.inc @@ -30,6 +30,7 @@ memcpy(&HPMHooks.source.cmdline, cmdline, sizeof(struct cmdline_interface)); memcpy(&HPMHooks.source.console, console, sizeof(struct console_interface)); memcpy(&HPMHooks.source.core, core, sizeof(struct core_interface)); memcpy(&HPMHooks.source.DB, DB, sizeof(struct db_interface)); +memcpy(&HPMHooks.source.des, des, sizeof(struct des_interface)); memcpy(&HPMHooks.source.lclif, lclif, sizeof(struct lclif_interface)); memcpy(&HPMHooks.source.PRIV__lclif, lclif->p, sizeof(struct lclif_interface_private)); memcpy(&HPMHooks.source.libconfig, libconfig, sizeof(struct libconfig_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index 591c6b147..383517175 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -1928,6 +1928,10 @@ struct { struct HPMHookPoint *HP_DB_init_post; struct HPMHookPoint *HP_DB_final_pre; struct HPMHookPoint *HP_DB_final_post; + struct HPMHookPoint *HP_des_decrypt_block_pre; + struct HPMHookPoint *HP_des_decrypt_block_post; + struct HPMHookPoint *HP_des_decrypt_pre; + struct HPMHookPoint *HP_des_decrypt_post; struct HPMHookPoint *HP_duel_create_pre; struct HPMHookPoint *HP_duel_create_post; struct HPMHookPoint *HP_duel_invite_pre; @@ -7885,6 +7889,10 @@ struct { int HP_DB_init_post; int HP_DB_final_pre; int HP_DB_final_post; + int HP_des_decrypt_block_pre; + int HP_des_decrypt_block_post; + int HP_des_decrypt_pre; + int HP_des_decrypt_post; int HP_duel_create_pre; int HP_duel_create_post; int HP_duel_invite_pre; @@ -11953,6 +11961,7 @@ struct { struct console_interface console; struct core_interface core; struct db_interface DB; + struct des_interface des; struct duel_interface duel; struct elemental_interface elemental; struct guild_interface guild; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index 014497ae2..aeb76be5b 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -990,6 +990,9 @@ struct HookingPointData HookingPoints[] = { { HP_POP(DB->data2ptr, HP_DB_data2ptr) }, { HP_POP(DB->init, HP_DB_init) }, { HP_POP(DB->final, HP_DB_final) }, +/* des_interface */ + { HP_POP(des->decrypt_block, HP_des_decrypt_block) }, + { HP_POP(des->decrypt, HP_des_decrypt) }, /* duel_interface */ { HP_POP(duel->create, HP_duel_create) }, { HP_POP(duel->invite, HP_duel_invite) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index 850c54f80..62ce7d41f 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -25109,6 +25109,59 @@ void HP_DB_final(void) { } return; } +/* des_interface */ +void HP_des_decrypt_block(struct des_bit64 *block) { + int hIndex = 0; + if( HPMHooks.count.HP_des_decrypt_block_pre ) { + void (*preHookFunc) (struct des_bit64 **block); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_block_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_des_decrypt_block_pre[hIndex].func; + preHookFunc(&block); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.des.decrypt_block(block); + } + if( HPMHooks.count.HP_des_decrypt_block_post ) { + void (*postHookFunc) (struct des_bit64 *block); + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_block_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_des_decrypt_block_post[hIndex].func; + postHookFunc(block); + } + } + return; +} +void HP_des_decrypt(unsigned char *data, size_t size) { + int hIndex = 0; + if( HPMHooks.count.HP_des_decrypt_pre ) { + void (*preHookFunc) (unsigned char **data, size_t *size); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_des_decrypt_pre[hIndex].func; + preHookFunc(&data, &size); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.des.decrypt(data, size); + } + if( HPMHooks.count.HP_des_decrypt_post ) { + void (*postHookFunc) (unsigned char *data, size_t size); + for(hIndex = 0; hIndex < HPMHooks.count.HP_des_decrypt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_des_decrypt_post[hIndex].func; + postHookFunc(data, size); + } + } + return; +} /* duel_interface */ int HP_duel_create(struct map_session_data *sd, const unsigned int maxpl) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_map.sources.inc b/src/plugins/HPMHooking/HPMHooking_map.sources.inc index f98330b88..50e83206a 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.sources.inc @@ -38,6 +38,7 @@ memcpy(&HPMHooks.source.cmdline, cmdline, sizeof(struct cmdline_interface)); memcpy(&HPMHooks.source.console, console, sizeof(struct console_interface)); memcpy(&HPMHooks.source.core, core, sizeof(struct core_interface)); memcpy(&HPMHooks.source.DB, DB, sizeof(struct db_interface)); +memcpy(&HPMHooks.source.des, des, sizeof(struct des_interface)); memcpy(&HPMHooks.source.duel, duel, sizeof(struct duel_interface)); memcpy(&HPMHooks.source.elemental, elemental, sizeof(struct elemental_interface)); memcpy(&HPMHooks.source.guild, guild, sizeof(struct guild_interface)); -- cgit v1.2.3-70-g09d2 From 732c6d6019d608f9f0e151ec75c48fcc498d4f51 Mon Sep 17 00:00:00 2001 From: hemagx Date: Wed, 17 Feb 2016 14:26:01 +0200 Subject: Interface md5calc.c --- src/common/core.c | 2 ++ src/common/md5calc.c | 21 +++++++++++++-------- src/common/md5calc.h | 16 ++++++++++++---- src/login/lclif.c | 10 +++++----- src/login/login.c | 14 +++++++------- src/map/script.c | 4 ++-- src/plugins/HPMHooking.c | 1 + vcproj-11/char-server.vcxproj | 2 ++ vcproj-11/char-server.vcxproj.filters | 6 ++++++ vcproj-12/char-server.vcxproj | 2 ++ vcproj-12/char-server.vcxproj.filters | 6 ++++++ vcproj-14/char-server.vcxproj | 2 ++ vcproj-14/char-server.vcxproj.filters | 6 ++++++ 13 files changed, 66 insertions(+), 26 deletions(-) diff --git a/src/common/core.c b/src/common/core.c index ce92a77e3..19f2ef0c7 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -40,6 +40,7 @@ # include "common/HPM.h" # include "common/conf.h" # include "common/ers.h" +# include "common/md5calc.h" # include "common/socket.h" # include "common/sql.h" # include "common/thread.h" @@ -261,6 +262,7 @@ void core_defaults(void) { timer_defaults(); db_defaults(); socket_defaults(); + md5_defaults(); #endif } /** diff --git a/src/common/md5calc.c b/src/common/md5calc.c index d346c8aa4..ad0f7c2d8 100644 --- a/src/common/md5calc.c +++ b/src/common/md5calc.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 @@ -37,6 +37,9 @@ #include #include +struct md5_interface md5_s; +struct md5_interface *md5; + // Global variable static unsigned int *pX; @@ -232,18 +235,12 @@ static void MD5_String2binary(const char * string, unsigned char * output) //------------------------------------------------------------------- // The function for the exteriors -/** output is the coded binary in the character sequence which wants to code string. */ -void MD5_Binary(const char * string, unsigned char * output) -{ - MD5_String2binary(string,output); -} - /** output is the coded character sequence in the character sequence which wants to code string. */ void MD5_String(const char *string, char *output) { unsigned char digest[16]; - MD5_String2binary(string,digest); + md5->binary(string,digest); snprintf(output, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[ 0], digest[ 1], digest[ 2], digest[ 3], digest[ 4], digest[ 5], digest[ 6], digest[ 7], @@ -259,3 +256,11 @@ void MD5_Salt(unsigned int len, char * output) output[i] = (char)(1 + rnd() % 255); } + +void md5_defaults(void) +{ + md5 = &md5_s; + md5->binary = MD5_String2binary; + md5->string = MD5_String; + md5->salt = MD5_Salt; +} diff --git a/src/common/md5calc.h b/src/common/md5calc.h index 0294c3ca1..1611b6fee 100644 --- a/src/common/md5calc.h +++ b/src/common/md5calc.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,10 +21,18 @@ #ifndef COMMON_MD5CALC_H #define COMMON_MD5CALC_H +#include "common/hercules.h" + +struct md5_interface { + void (*string) (const char *string, char *output); + void (*binary) (const char *string, unsigned char *output); + void (*salt) (unsigned int len, char *output); +}; + #ifdef HERCULES_CORE -void MD5_String(const char * string, char * output); -void MD5_Binary(const char * string, unsigned char * output); -void MD5_Salt(unsigned int len, char * output); +void md5_defaults(void); #endif // HERCULES_CORE +HPShared struct md5_interface *md5; + #endif /* COMMON_MD5CALC_H */ diff --git a/src/login/lclif.c b/src/login/lclif.c index 9515c5940..3ed257e85 100644 --- a/src/login/lclif.c +++ b/src/login/lclif.c @@ -86,7 +86,7 @@ enum parsefunc_rcode lclif_parse_CA_LOGIN(int fd, struct login_session_data *sd) safestrncpy(sd->passwd, packet->password, PASSWD_LEN); if (login->config->use_md5_passwds) - MD5_String(sd->passwd, sd->passwd); + md5->string(sd->passwd, sd->passwd); sd->passwdenc = PWENC_NONE; login->client_login(fd, sd); @@ -160,7 +160,7 @@ enum parsefunc_rcode lclif_parse_CA_LOGIN_PCBANG(int fd, struct login_session_da safestrncpy(sd->passwd, packet->password, PASSWD_LEN); if (login->config->use_md5_passwds) - MD5_String(sd->passwd, sd->passwd); + md5->string(sd->passwd, sd->passwd); sd->passwdenc = PWENC_NONE; login->client_login(fd, sd); @@ -183,7 +183,7 @@ enum parsefunc_rcode lclif_parse_CA_LOGIN_HAN(int fd, struct login_session_data safestrncpy(sd->passwd, packet->password, PASSWD_LEN); if (login->config->use_md5_passwds) - MD5_String(sd->passwd, sd->passwd); + md5->string(sd->passwd, sd->passwd); sd->passwdenc = PWENC_NONE; login->client_login(fd, sd); @@ -209,7 +209,7 @@ enum parsefunc_rcode lclif_parse_CA_SSO_LOGIN_REQ(int fd, struct login_session_d safestrncpy(sd->passwd, packet->t1, min(tokenlen + 1, PASSWD_LEN)); // Variable-length field, don't copy more than necessary if (login->config->use_md5_passwds) - MD5_String(sd->passwd, sd->passwd); + md5->string(sd->passwd, sd->passwd); sd->passwdenc = PWENC_NONE; login->client_login(fd, sd); @@ -222,7 +222,7 @@ enum parsefunc_rcode lclif_parse_CA_REQ_HASH(int fd, struct login_session_data * { memset(sd->md5key, '\0', sizeof(sd->md5key)); sd->md5keylen = (uint16)(12 + rnd() % 4); - MD5_Salt(sd->md5keylen, sd->md5key); + md5->salt(sd->md5keylen, sd->md5key); lclif->coding_key(fd, sd); return PACKET_VALID; diff --git a/src/login/login.c b/src/login/login.c index a8ba3d9b7..7f5f78a4d 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -239,7 +239,7 @@ bool login_check_encrypted(const char* str1, const char* str2, const char* passw nullpo_ret(str2); nullpo_ret(passwd); safesnprintf(tmpstr, sizeof(tmpstr), "%s%s", str1, str2); - MD5_String(tmpstr, md5str); + md5->string(tmpstr, md5str); return (0==strcmp(passwd, md5str)); } @@ -1378,7 +1378,7 @@ void login_parse_request_connection(int fd, struct login_session_data* sd, const safestrncpy(sd->userid, RFIFOP(fd,2), NAME_LENGTH); safestrncpy(sd->passwd, RFIFOP(fd,26), NAME_LENGTH); if (login->config->use_md5_passwds) - MD5_String(sd->passwd, sd->passwd); + md5->string(sd->passwd, sd->passwd); sd->passwdenc = PWENC_NONE; sd->version = login->config->client_version_to_connect; // hack to skip version check server_ip = ntohl(RFIFOL(fd,54)); @@ -1529,14 +1529,14 @@ int login_config_read(const char *cfgName) login->config->client_hash_check = config_switch(w2); else if(!strcmpi(w1, "client_hash")) { int group = 0; - char md5[33]; - memset(md5, '\0', 33); + char md5hash[33]; + memset(md5hash, '\0', 33); - if (sscanf(w2, "%d, %32s", &group, md5) == 2) { + if (sscanf(w2, "%d, %32s", &group, md5hash) == 2) { struct client_hash_node *nnode; CREATE(nnode, struct client_hash_node, 1); - if (strcmpi(md5, "disabled") == 0) { + if (strcmpi(md5hash, "disabled") == 0) { nnode->hash[0] = '\0'; } else { int i; @@ -1544,7 +1544,7 @@ int login_config_read(const char *cfgName) char buf[3]; unsigned int byte; - memcpy(buf, &md5[i], 2); + memcpy(buf, &md5hash[i], 2); buf[2] = 0; sscanf(buf, "%x", &byte); diff --git a/src/map/script.c b/src/map/script.c index 90a70e48d..effe40ea5 100644 --- a/src/map/script.c +++ b/src/map/script.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 @@ -16049,7 +16049,7 @@ BUILDIN(md5) tmpstr = script_getstr(st,2); md5str = (char *)aMalloc((32+1)*sizeof(char)); - MD5_String(tmpstr, md5str); + md5->string(tmpstr, md5str); script_pushstr(st, md5str); return true; } diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index b383d30a5..be6947754 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -110,6 +110,7 @@ #include "common/console.h" #include "common/db.h" #include "common/des.h" +#include "common/md5calc.h" #include "common/memmgr.h" #include "common/nullpo.h" #include "common/showmsg.h" diff --git a/vcproj-11/char-server.vcxproj b/vcproj-11/char-server.vcxproj index 764fb3378..abd954904 100644 --- a/vcproj-11/char-server.vcxproj +++ b/vcproj-11/char-server.vcxproj @@ -155,6 +155,7 @@ + @@ -208,6 +209,7 @@ + diff --git a/vcproj-11/char-server.vcxproj.filters b/vcproj-11/char-server.vcxproj.filters index 751afc993..8cd2d592f 100644 --- a/vcproj-11/char-server.vcxproj.filters +++ b/vcproj-11/char-server.vcxproj.filters @@ -25,6 +25,9 @@ common + + common + common @@ -162,6 +165,9 @@ common + + common + common diff --git a/vcproj-12/char-server.vcxproj b/vcproj-12/char-server.vcxproj index 2077f08ee..fe4706d14 100644 --- a/vcproj-12/char-server.vcxproj +++ b/vcproj-12/char-server.vcxproj @@ -154,6 +154,7 @@ + @@ -207,6 +208,7 @@ + diff --git a/vcproj-12/char-server.vcxproj.filters b/vcproj-12/char-server.vcxproj.filters index 751afc993..8cd2d592f 100644 --- a/vcproj-12/char-server.vcxproj.filters +++ b/vcproj-12/char-server.vcxproj.filters @@ -25,6 +25,9 @@ common + + common + common @@ -162,6 +165,9 @@ common + + common + common diff --git a/vcproj-14/char-server.vcxproj b/vcproj-14/char-server.vcxproj index 4e30a1f74..0868d56ee 100644 --- a/vcproj-14/char-server.vcxproj +++ b/vcproj-14/char-server.vcxproj @@ -153,6 +153,7 @@ + @@ -206,6 +207,7 @@ + diff --git a/vcproj-14/char-server.vcxproj.filters b/vcproj-14/char-server.vcxproj.filters index 751afc993..8cd2d592f 100644 --- a/vcproj-14/char-server.vcxproj.filters +++ b/vcproj-14/char-server.vcxproj.filters @@ -25,6 +25,9 @@ common + + common + common @@ -162,6 +165,9 @@ common + + common + common -- cgit v1.2.3-70-g09d2 From b1ad3f26233d8a2e79d88802eb75ba39be88af95 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 20:19:26 +0100 Subject: Various changes to the md5 interface Mostly stylistic changes. Cleaned up documentation. Signed-off-by: Haru --- src/common/md5calc.c | 141 +++++++++++++++++++++++++++------------------------ src/common/md5calc.h | 32 +++++++++++- 2 files changed, 104 insertions(+), 69 deletions(-) diff --git a/src/common/md5calc.c b/src/common/md5calc.c index ad0f7c2d8..bd6b48f10 100644 --- a/src/common/md5calc.c +++ b/src/common/md5calc.c @@ -20,30 +20,27 @@ */ #define HERCULES_CORE -/*********************************************************** - * md5 calculation algorithm - * - * The source code referred to the following URL. - * http://www.geocities.co.jp/SiliconValley-Oakland/8878/lab17/lab17.html - * - ***********************************************************/ - #include "md5calc.h" #include "common/cbasetypes.h" +#include "common/nullpo.h" #include "common/random.h" #include #include #include +/** @file + * Implementation of the md5 interface. + */ + struct md5_interface md5_s; struct md5_interface *md5; -// Global variable +/// Global variable static unsigned int *pX; -// String Table +/// String Table static const unsigned int T[] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, //0 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, //4 @@ -63,99 +60,102 @@ static const unsigned int T[] = { 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 //60 }; -// ROTATE_LEFT The left is made to rotate x [ n-bit ]. This is diverted as it is from RFC. +/// The left is made to rotate x [ n-bit ]. This is diverted as it is from RFC. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) // The function used for other calculation -static unsigned int F(unsigned int X, unsigned int Y, unsigned int Z) +static unsigned int md5_F(unsigned int X, unsigned int Y, unsigned int Z) { return (X & Y) | (~X & Z); } -static unsigned int G(unsigned int X, unsigned int Y, unsigned int Z) + +static unsigned int md5_G(unsigned int X, unsigned int Y, unsigned int Z) { return (X & Z) | (Y & ~Z); } -static unsigned int H(unsigned int X, unsigned int Y, unsigned int Z) + +static unsigned int md5_H(unsigned int X, unsigned int Y, unsigned int Z) { return X ^ Y ^ Z; } -static unsigned int I(unsigned int X, unsigned int Y, unsigned int Z) + +static unsigned int md5_I(unsigned int X, unsigned int Y, unsigned int Z) { return Y ^ (X | ~Z); } -static unsigned int Round(unsigned int a, unsigned int b, unsigned int FGHI, +static unsigned int md5_Round(unsigned int a, unsigned int b, unsigned int FGHI, unsigned int k, unsigned int s, unsigned int i) { return b + ROTATE_LEFT(a + FGHI + pX[k] + T[i], s); } -static void Round1(unsigned int *a, unsigned int b, unsigned int c, +static void md5_Round1(unsigned int *a, unsigned int b, unsigned int c, unsigned int d,unsigned int k, unsigned int s, unsigned int i) { - *a = Round(*a, b, F(b,c,d), k, s, i); + *a = md5_Round(*a, b, md5_F(b,c,d), k, s, i); } -static void Round2(unsigned int *a, unsigned int b, unsigned int c, +static void md5_Round2(unsigned int *a, unsigned int b, unsigned int c, unsigned int d,unsigned int k, unsigned int s, unsigned int i) { - *a = Round(*a, b, G(b,c,d), k, s, i); + *a = md5_Round(*a, b, md5_G(b,c,d), k, s, i); } -static void Round3(unsigned int *a, unsigned int b, unsigned int c, +static void md5_Round3(unsigned int *a, unsigned int b, unsigned int c, unsigned int d,unsigned int k, unsigned int s, unsigned int i) { - *a = Round(*a, b, H(b,c,d), k, s, i); + *a = md5_Round(*a, b, md5_H(b,c,d), k, s, i); } -static void Round4(unsigned int *a, unsigned int b, unsigned int c, +static void md5_Round4(unsigned int *a, unsigned int b, unsigned int c, unsigned int d,unsigned int k, unsigned int s, unsigned int i) { - *a = Round(*a, b, I(b,c,d), k, s, i); + *a = md5_Round(*a, b, md5_I(b,c,d), k, s, i); } -static void MD5_Round_Calculate(const unsigned char *block, +static void md5_Round_Calculate(const unsigned char *block, unsigned int *A2, unsigned int *B2, unsigned int *C2, unsigned int *D2) { //create X It is since it is required. unsigned int X[16]; //512bit 64byte - int j,k; + int j, k; //Save A as AA, B as BB, C as CC, and and D as DD (saving of A, B, C, and D) - unsigned int A=*A2, B=*B2, C=*C2, D=*D2; - unsigned int AA = A,BB = B,CC = C,DD = D; + unsigned int A = *A2, B = *B2, C = *C2, D = *D2; + unsigned int AA = A, BB = B, CC = C, DD = D; //It is a large region variable reluctantly because of calculation of a round. . . for Round1...4 pX = X; //Copy block(padding_message) i into X - for (j=0,k=0; j<64; j+=4,k++) - X[k] = ( (unsigned int )block[j] ) // 8byte*4 -> 32byte conversion - | ( ((unsigned int )block[j+1]) << 8 ) // A function called Decode as used in the field of RFC - | ( ((unsigned int )block[j+2]) << 16 ) - | ( ((unsigned int )block[j+3]) << 24 ); - + for (j = 0, k = 0; j < 64; j += 4, k++) { + X[k] = ((unsigned int)block[j]) // 8byte*4 -> 32byte conversion + | (((unsigned int)block[j+1]) << 8) // A function called Decode as used in the field of RFC + | (((unsigned int)block[j+2]) << 16) + | (((unsigned int)block[j+3]) << 24); + } //Round 1 - Round1(&A,B,C,D, 0, 7, 0); Round1(&D,A,B,C, 1, 12, 1); Round1(&C,D,A,B, 2, 17, 2); Round1(&B,C,D,A, 3, 22, 3); - Round1(&A,B,C,D, 4, 7, 4); Round1(&D,A,B,C, 5, 12, 5); Round1(&C,D,A,B, 6, 17, 6); Round1(&B,C,D,A, 7, 22, 7); - Round1(&A,B,C,D, 8, 7, 8); Round1(&D,A,B,C, 9, 12, 9); Round1(&C,D,A,B, 10, 17, 10); Round1(&B,C,D,A, 11, 22, 11); - Round1(&A,B,C,D, 12, 7, 12); Round1(&D,A,B,C, 13, 12, 13); Round1(&C,D,A,B, 14, 17, 14); Round1(&B,C,D,A, 15, 22, 15); + md5_Round1(&A,B,C,D, 0, 7, 0); md5_Round1(&D,A,B,C, 1, 12, 1); md5_Round1(&C,D,A,B, 2, 17, 2); md5_Round1(&B,C,D,A, 3, 22, 3); + md5_Round1(&A,B,C,D, 4, 7, 4); md5_Round1(&D,A,B,C, 5, 12, 5); md5_Round1(&C,D,A,B, 6, 17, 6); md5_Round1(&B,C,D,A, 7, 22, 7); + md5_Round1(&A,B,C,D, 8, 7, 8); md5_Round1(&D,A,B,C, 9, 12, 9); md5_Round1(&C,D,A,B, 10, 17, 10); md5_Round1(&B,C,D,A, 11, 22, 11); + md5_Round1(&A,B,C,D, 12, 7, 12); md5_Round1(&D,A,B,C, 13, 12, 13); md5_Round1(&C,D,A,B, 14, 17, 14); md5_Round1(&B,C,D,A, 15, 22, 15); //Round 2 - Round2(&A,B,C,D, 1, 5, 16); Round2(&D,A,B,C, 6, 9, 17); Round2(&C,D,A,B, 11, 14, 18); Round2(&B,C,D,A, 0, 20, 19); - Round2(&A,B,C,D, 5, 5, 20); Round2(&D,A,B,C, 10, 9, 21); Round2(&C,D,A,B, 15, 14, 22); Round2(&B,C,D,A, 4, 20, 23); - Round2(&A,B,C,D, 9, 5, 24); Round2(&D,A,B,C, 14, 9, 25); Round2(&C,D,A,B, 3, 14, 26); Round2(&B,C,D,A, 8, 20, 27); - Round2(&A,B,C,D, 13, 5, 28); Round2(&D,A,B,C, 2, 9, 29); Round2(&C,D,A,B, 7, 14, 30); Round2(&B,C,D,A, 12, 20, 31); + md5_Round2(&A,B,C,D, 1, 5, 16); md5_Round2(&D,A,B,C, 6, 9, 17); md5_Round2(&C,D,A,B, 11, 14, 18); md5_Round2(&B,C,D,A, 0, 20, 19); + md5_Round2(&A,B,C,D, 5, 5, 20); md5_Round2(&D,A,B,C, 10, 9, 21); md5_Round2(&C,D,A,B, 15, 14, 22); md5_Round2(&B,C,D,A, 4, 20, 23); + md5_Round2(&A,B,C,D, 9, 5, 24); md5_Round2(&D,A,B,C, 14, 9, 25); md5_Round2(&C,D,A,B, 3, 14, 26); md5_Round2(&B,C,D,A, 8, 20, 27); + md5_Round2(&A,B,C,D, 13, 5, 28); md5_Round2(&D,A,B,C, 2, 9, 29); md5_Round2(&C,D,A,B, 7, 14, 30); md5_Round2(&B,C,D,A, 12, 20, 31); //Round 3 - Round3(&A,B,C,D, 5, 4, 32); Round3(&D,A,B,C, 8, 11, 33); Round3(&C,D,A,B, 11, 16, 34); Round3(&B,C,D,A, 14, 23, 35); - Round3(&A,B,C,D, 1, 4, 36); Round3(&D,A,B,C, 4, 11, 37); Round3(&C,D,A,B, 7, 16, 38); Round3(&B,C,D,A, 10, 23, 39); - Round3(&A,B,C,D, 13, 4, 40); Round3(&D,A,B,C, 0, 11, 41); Round3(&C,D,A,B, 3, 16, 42); Round3(&B,C,D,A, 6, 23, 43); - Round3(&A,B,C,D, 9, 4, 44); Round3(&D,A,B,C, 12, 11, 45); Round3(&C,D,A,B, 15, 16, 46); Round3(&B,C,D,A, 2, 23, 47); + md5_Round3(&A,B,C,D, 5, 4, 32); md5_Round3(&D,A,B,C, 8, 11, 33); md5_Round3(&C,D,A,B, 11, 16, 34); md5_Round3(&B,C,D,A, 14, 23, 35); + md5_Round3(&A,B,C,D, 1, 4, 36); md5_Round3(&D,A,B,C, 4, 11, 37); md5_Round3(&C,D,A,B, 7, 16, 38); md5_Round3(&B,C,D,A, 10, 23, 39); + md5_Round3(&A,B,C,D, 13, 4, 40); md5_Round3(&D,A,B,C, 0, 11, 41); md5_Round3(&C,D,A,B, 3, 16, 42); md5_Round3(&B,C,D,A, 6, 23, 43); + md5_Round3(&A,B,C,D, 9, 4, 44); md5_Round3(&D,A,B,C, 12, 11, 45); md5_Round3(&C,D,A,B, 15, 16, 46); md5_Round3(&B,C,D,A, 2, 23, 47); //Round 4 - Round4(&A,B,C,D, 0, 6, 48); Round4(&D,A,B,C, 7, 10, 49); Round4(&C,D,A,B, 14, 15, 50); Round4(&B,C,D,A, 5, 21, 51); - Round4(&A,B,C,D, 12, 6, 52); Round4(&D,A,B,C, 3, 10, 53); Round4(&C,D,A,B, 10, 15, 54); Round4(&B,C,D,A, 1, 21, 55); - Round4(&A,B,C,D, 8, 6, 56); Round4(&D,A,B,C, 15, 10, 57); Round4(&C,D,A,B, 6, 15, 58); Round4(&B,C,D,A, 13, 21, 59); - Round4(&A,B,C,D, 4, 6, 60); Round4(&D,A,B,C, 11, 10, 61); Round4(&C,D,A,B, 2, 15, 62); Round4(&B,C,D,A, 9, 21, 63); + md5_Round4(&A,B,C,D, 0, 6, 48); md5_Round4(&D,A,B,C, 7, 10, 49); md5_Round4(&C,D,A,B, 14, 15, 50); md5_Round4(&B,C,D,A, 5, 21, 51); + md5_Round4(&A,B,C,D, 12, 6, 52); md5_Round4(&D,A,B,C, 3, 10, 53); md5_Round4(&C,D,A,B, 10, 15, 54); md5_Round4(&B,C,D,A, 1, 21, 55); + md5_Round4(&A,B,C,D, 8, 6, 56); md5_Round4(&D,A,B,C, 15, 10, 57); md5_Round4(&C,D,A,B, 6, 15, 58); md5_Round4(&B,C,D,A, 13, 21, 59); + md5_Round4(&A,B,C,D, 4, 6, 60); md5_Round4(&D,A,B,C, 11, 10, 61); md5_Round4(&C,D,A,B, 2, 15, 62); md5_Round4(&B,C,D,A, 9, 21, 63); // Then perform the following additions. (let's add) *A2 = A + AA; @@ -167,7 +167,8 @@ static void MD5_Round_Calculate(const unsigned char *block, memset(pX, 0, sizeof(X)); } -static void MD5_String2binary(const char * string, unsigned char * output) +/// @copydoc md5_interface::binary() +static void md5_string2binary(const char *string, unsigned char *output) { //var /*8bit*/ @@ -199,7 +200,7 @@ static void MD5_String2binary(const char * string, unsigned char * output) //1-2 Repeat calculation until length becomes less than 64 bytes. for (i=string_byte_len; 64<=i; i-=64,pstring+=64) - MD5_Round_Calculate(pstring, A,B,C,D); + md5_Round_Calculate(pstring, A,B,C,D); //1-3 copy_len = string_byte_len % 64; //The number of bytes which remained is computed. @@ -210,7 +211,7 @@ static void MD5_String2binary(const char * string, unsigned char * output) //1-4 //If 56 bytes or more (less than 64 bytes) of remainder becomes, it will calculate by extending to 64 bytes. if (56 <= copy_len) { - MD5_Round_Calculate(padding_message, A,B,C,D); + md5_Round_Calculate(padding_message, A,B,C,D); memset(padding_message, 0, 56); //56 bytes is newly fill uped with 0. } @@ -222,24 +223,25 @@ static void MD5_String2binary(const char * string, unsigned char * output) if (UINT_MAX / 8 < string_byte_len) { unsigned int high = (string_byte_len - UINT_MAX / 8) * 8; memcpy(&padding_message[60], &high, 4); - } else + } else { memset(&padding_message[60], 0, 4); //In this case, it is good for a higher rank at 0. + } //Step 4.Process Message in 16-Word Blocks (calculation of MD5) - MD5_Round_Calculate(padding_message, A,B,C,D); + md5_Round_Calculate(padding_message, A,B,C,D); //Step 5.Output (output) memcpy(output,msg_digest,16); } -//------------------------------------------------------------------- -// The function for the exteriors - -/** output is the coded character sequence in the character sequence which wants to code string. */ -void MD5_String(const char *string, char *output) +/// @copydoc md5_interface::string() +void md5_string(const char *string, char *output) { unsigned char digest[16]; + nullpo_retv(string); + nullpo_retv(output); + md5->binary(string,digest); snprintf(output, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[ 0], digest[ 1], digest[ 2], digest[ 3], @@ -248,19 +250,24 @@ void MD5_String(const char *string, char *output) digest[12], digest[13], digest[14], digest[15]); } -/** output is a sequence of non-zero characters to be used as password salt. */ -void MD5_Salt(unsigned int len, char * output) +/// @copydoc md5_interface::salt(); +void md5_salt(int len, char *output) { - unsigned int i; - for( i = 0; i < len; ++i ) + int i; + Assert_retv(len > 0); + + for (i = 0; i < len; ++i) output[i] = (char)(1 + rnd() % 255); } +/** + * Interface base initialization. + */ void md5_defaults(void) { md5 = &md5_s; - md5->binary = MD5_String2binary; - md5->string = MD5_String; - md5->salt = MD5_Salt; + md5->binary = md5_string2binary; + md5->string = md5_string; + md5->salt = md5_salt; } diff --git a/src/common/md5calc.h b/src/common/md5calc.h index 1611b6fee..b4d4995f9 100644 --- a/src/common/md5calc.h +++ b/src/common/md5calc.h @@ -23,16 +23,44 @@ #include "common/hercules.h" +/** @file + * md5 calculation algorithm. + * + * The source code referred to the following URL. + * http://www.geocities.co.jp/SiliconValley-Oakland/8878/lab17/lab17.html + */ + +/// The md5 interface struct md5_interface { + /** + * Hashes a string, returning the hash in string format. + * + * @param[in] string The source string (NUL terminated). + * @param[out] output Output buffer (at least 33 bytes available). + */ void (*string) (const char *string, char *output); + + /** + * Hashes a string, returning the buffer in binary format. + * + * @param[in] string The source string. + * @param[out] output Output buffer (at least 16 bytes available). + */ void (*binary) (const char *string, unsigned char *output); - void (*salt) (unsigned int len, char *output); + + /** + * Generates a random salt. + * + * @param[in] len The desired salt length. + * @param[out] output The output buffer (at least len bytes available). + */ + void (*salt) (int len, char *output); }; #ifdef HERCULES_CORE void md5_defaults(void); #endif // HERCULES_CORE -HPShared struct md5_interface *md5; +HPShared struct md5_interface *md5; ///< Pointer to the md5 interface. #endif /* COMMON_MD5CALC_H */ -- cgit v1.2.3-70-g09d2 From 591e877c7f30d4c9d34b996f245b0ad0ee81c46d Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 20:21:07 +0100 Subject: HPM Hooks Update Signed-off-by: Haru --- src/common/HPMDataCheck.h | 5 ++ src/common/HPMSymbols.inc.h | 6 ++ src/plugins/HPMHooking/HPMHooking.Defs.inc | 8 +++ .../HPMHooking/HPMHooking_char.HPMHooksCore.inc | 13 ++++ .../HPMHooking/HPMHooking_char.HookingPoints.inc | 4 ++ src/plugins/HPMHooking/HPMHooking_char.Hooks.inc | 79 ++++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_char.sources.inc | 1 + .../HPMHooking/HPMHooking_login.HPMHooksCore.inc | 13 ++++ .../HPMHooking/HPMHooking_login.HookingPoints.inc | 4 ++ src/plugins/HPMHooking/HPMHooking_login.Hooks.inc | 79 ++++++++++++++++++++++ .../HPMHooking/HPMHooking_login.sources.inc | 1 + .../HPMHooking/HPMHooking_map.HPMHooksCore.inc | 13 ++++ .../HPMHooking/HPMHooking_map.HookingPoints.inc | 4 ++ src/plugins/HPMHooking/HPMHooking_map.Hooks.inc | 79 ++++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_map.sources.inc | 1 + 15 files changed, 310 insertions(+) diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h index c4eedadbf..4d99115f2 100644 --- a/src/common/HPMDataCheck.h +++ b/src/common/HPMDataCheck.h @@ -172,6 +172,11 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = { #else #define COMMON_MAPINDEX_H #endif // COMMON_MAPINDEX_H + #ifdef COMMON_MD5CALC_H + { "md5_interface", sizeof(struct md5_interface), SERVER_TYPE_ALL }, + #else + #define COMMON_MD5CALC_H + #endif // COMMON_MD5CALC_H #ifdef COMMON_MEMMGR_H { "malloc_interface", sizeof(struct malloc_interface), SERVER_TYPE_ALL }, #else diff --git a/src/common/HPMSymbols.inc.h b/src/common/HPMSymbols.inc.h index 0e535f54c..2bd20aa8e 100644 --- a/src/common/HPMSymbols.inc.h +++ b/src/common/HPMSymbols.inc.h @@ -170,6 +170,9 @@ struct mapit_interface *mapit; #ifdef MAP_MAPREG_H /* mapreg */ struct mapreg_interface *mapreg; #endif // MAP_MAPREG_H +#ifdef COMMON_MD5CALC_H /* md5 */ +struct md5_interface *md5; +#endif // COMMON_MD5CALC_H #ifdef MAP_MERCENARY_H /* mercenary */ struct mercenary_interface *mercenary; #endif // MAP_MERCENARY_H @@ -405,6 +408,9 @@ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("mapit", mapit)) return "mapi #ifdef MAP_MAPREG_H /* mapreg */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("mapreg", mapreg)) return "mapreg"; #endif // MAP_MAPREG_H +#ifdef COMMON_MD5CALC_H /* md5 */ +if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("md5", md5)) return "md5"; +#endif // COMMON_MD5CALC_H #ifdef MAP_MERCENARY_H /* mercenary */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("mercenary", mercenary)) return "mercenary"; #endif // MAP_MERCENARY_H diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index 3ee1f1b3d..6e8c74174 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -4433,6 +4433,14 @@ typedef void (*HPMHOOK_post_mapreg_reload) (void); typedef bool (*HPMHOOK_pre_mapreg_config_read) (const char **w1, const char **w2); typedef bool (*HPMHOOK_post_mapreg_config_read) (bool retVal___, const char *w1, const char *w2); #endif // MAP_MAPREG_H +#ifdef COMMON_MD5CALC_H /* md5 */ +typedef void (*HPMHOOK_pre_md5_string) (const char **string, char **output); +typedef void (*HPMHOOK_post_md5_string) (const char *string, char *output); +typedef void (*HPMHOOK_pre_md5_binary) (const char **string, unsigned char **output); +typedef void (*HPMHOOK_post_md5_binary) (const char *string, unsigned char *output); +typedef void (*HPMHOOK_pre_md5_salt) (int *len, char **output); +typedef void (*HPMHOOK_post_md5_salt) (int len, char *output); +#endif // COMMON_MD5CALC_H #ifdef MAP_MERCENARY_H /* mercenary */ typedef void (*HPMHOOK_pre_mercenary_init) (bool *minimal); typedef void (*HPMHOOK_post_mercenary_init) (bool minimal); diff --git a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc index 38dd2a7a0..da3990cf3 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc @@ -1156,6 +1156,12 @@ struct { struct HPMHookPoint *HP_mapindex_id2name_post; struct HPMHookPoint *HP_mapindex_check_default_pre; struct HPMHookPoint *HP_mapindex_check_default_post; + struct HPMHookPoint *HP_md5_string_pre; + struct HPMHookPoint *HP_md5_string_post; + struct HPMHookPoint *HP_md5_binary_pre; + struct HPMHookPoint *HP_md5_binary_post; + struct HPMHookPoint *HP_md5_salt_pre; + struct HPMHookPoint *HP_md5_salt_post; struct HPMHookPoint *HP_nullpo_assert_report_pre; struct HPMHookPoint *HP_nullpo_assert_report_post; struct HPMHookPoint *HP_pincode_handle_pre; @@ -2565,6 +2571,12 @@ struct { int HP_mapindex_id2name_post; int HP_mapindex_check_default_pre; int HP_mapindex_check_default_post; + int HP_md5_string_pre; + int HP_md5_string_post; + int HP_md5_binary_pre; + int HP_md5_binary_post; + int HP_md5_salt_pre; + int HP_md5_salt_post; int HP_nullpo_assert_report_pre; int HP_nullpo_assert_report_post; int HP_pincode_handle_pre; @@ -2868,6 +2880,7 @@ struct { struct malloc_interface iMalloc; struct mapif_interface mapif; struct mapindex_interface mapindex; + struct md5_interface md5; struct nullpo_interface nullpo; struct pincode_interface pincode; struct showmsg_interface showmsg; diff --git a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc index a50d1e847..15db8625a 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc @@ -615,6 +615,10 @@ struct HookingPointData HookingPoints[] = { { HP_POP(mapindex->name2id, HP_mapindex_name2id) }, { HP_POP(mapindex->id2name, HP_mapindex_id2name) }, { HP_POP(mapindex->check_default, HP_mapindex_check_default) }, +/* md5_interface */ + { HP_POP(md5->string, HP_md5_string) }, + { HP_POP(md5->binary, HP_md5_binary) }, + { HP_POP(md5->salt, HP_md5_salt) }, /* nullpo_interface */ { HP_POP(nullpo->assert_report, HP_nullpo_assert_report) }, /* pincode_interface */ diff --git a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc index df995eee9..fd69a2bd9 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc @@ -15120,6 +15120,85 @@ bool HP_mapindex_check_default(void) { } return retVal___; } +/* md5_interface */ +void HP_md5_string(const char *string, char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_string_pre ) { + void (*preHookFunc) (const char **string, char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_string_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_string_pre[hIndex].func; + preHookFunc(&string, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.string(string, output); + } + if( HPMHooks.count.HP_md5_string_post ) { + void (*postHookFunc) (const char *string, char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_string_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_string_post[hIndex].func; + postHookFunc(string, output); + } + } + return; +} +void HP_md5_binary(const char *string, unsigned char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_binary_pre ) { + void (*preHookFunc) (const char **string, unsigned char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_binary_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_binary_pre[hIndex].func; + preHookFunc(&string, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.binary(string, output); + } + if( HPMHooks.count.HP_md5_binary_post ) { + void (*postHookFunc) (const char *string, unsigned char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_binary_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_binary_post[hIndex].func; + postHookFunc(string, output); + } + } + return; +} +void HP_md5_salt(int len, char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_salt_pre ) { + void (*preHookFunc) (int *len, char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_salt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_salt_pre[hIndex].func; + preHookFunc(&len, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.salt(len, output); + } + if( HPMHooks.count.HP_md5_salt_post ) { + void (*postHookFunc) (int len, char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_salt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_salt_post[hIndex].func; + postHookFunc(len, output); + } + } + return; +} /* nullpo_interface */ void HP_nullpo_assert_report(const char *file, int line, const char *func, const char *targetname, const char *title) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_char.sources.inc b/src/plugins/HPMHooking/HPMHooking_char.sources.inc index 569936c97..925fefd93 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.sources.inc @@ -49,6 +49,7 @@ memcpy(&HPMHooks.source.loginif, loginif, sizeof(struct loginif_interface)); memcpy(&HPMHooks.source.iMalloc, iMalloc, sizeof(struct malloc_interface)); memcpy(&HPMHooks.source.mapif, mapif, sizeof(struct mapif_interface)); memcpy(&HPMHooks.source.mapindex, mapindex, sizeof(struct mapindex_interface)); +memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); memcpy(&HPMHooks.source.nullpo, nullpo, sizeof(struct nullpo_interface)); memcpy(&HPMHooks.source.pincode, pincode, sizeof(struct pincode_interface)); memcpy(&HPMHooks.source.showmsg, showmsg, sizeof(struct showmsg_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc index 393794c71..daf7d35b0 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc @@ -404,6 +404,12 @@ struct { struct HPMHookPoint *HP_iMalloc_post_shutdown_post; struct HPMHookPoint *HP_iMalloc_init_messages_pre; struct HPMHookPoint *HP_iMalloc_init_messages_post; + struct HPMHookPoint *HP_md5_string_pre; + struct HPMHookPoint *HP_md5_string_post; + struct HPMHookPoint *HP_md5_binary_pre; + struct HPMHookPoint *HP_md5_binary_post; + struct HPMHookPoint *HP_md5_salt_pre; + struct HPMHookPoint *HP_md5_salt_post; struct HPMHookPoint *HP_nullpo_assert_report_pre; struct HPMHookPoint *HP_nullpo_assert_report_post; struct HPMHookPoint *HP_showmsg_init_pre; @@ -1041,6 +1047,12 @@ struct { int HP_iMalloc_post_shutdown_post; int HP_iMalloc_init_messages_pre; int HP_iMalloc_init_messages_post; + int HP_md5_string_pre; + int HP_md5_string_post; + int HP_md5_binary_pre; + int HP_md5_binary_post; + int HP_md5_salt_pre; + int HP_md5_salt_post; int HP_nullpo_assert_report_pre; int HP_nullpo_assert_report_post; int HP_showmsg_init_pre; @@ -1311,6 +1323,7 @@ struct { struct libconfig_interface libconfig; struct login_interface login; struct malloc_interface iMalloc; + struct md5_interface md5; struct nullpo_interface nullpo; struct showmsg_interface showmsg; struct socket_interface sockt; diff --git a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc index 93c838c46..6de163d9f 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc @@ -226,6 +226,10 @@ struct HookingPointData HookingPoints[] = { { HP_POP(iMalloc->usage, HP_iMalloc_usage) }, { HP_POP(iMalloc->post_shutdown, HP_iMalloc_post_shutdown) }, { HP_POP(iMalloc->init_messages, HP_iMalloc_init_messages) }, +/* md5_interface */ + { HP_POP(md5->string, HP_md5_string) }, + { HP_POP(md5->binary, HP_md5_binary) }, + { HP_POP(md5->salt, HP_md5_salt) }, /* nullpo_interface */ { HP_POP(nullpo->assert_report, HP_nullpo_assert_report) }, /* showmsg_interface */ diff --git a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc index 7771ca05d..a90ed233b 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc @@ -5089,6 +5089,85 @@ void HP_iMalloc_init_messages(void) { } return; } +/* md5_interface */ +void HP_md5_string(const char *string, char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_string_pre ) { + void (*preHookFunc) (const char **string, char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_string_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_string_pre[hIndex].func; + preHookFunc(&string, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.string(string, output); + } + if( HPMHooks.count.HP_md5_string_post ) { + void (*postHookFunc) (const char *string, char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_string_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_string_post[hIndex].func; + postHookFunc(string, output); + } + } + return; +} +void HP_md5_binary(const char *string, unsigned char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_binary_pre ) { + void (*preHookFunc) (const char **string, unsigned char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_binary_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_binary_pre[hIndex].func; + preHookFunc(&string, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.binary(string, output); + } + if( HPMHooks.count.HP_md5_binary_post ) { + void (*postHookFunc) (const char *string, unsigned char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_binary_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_binary_post[hIndex].func; + postHookFunc(string, output); + } + } + return; +} +void HP_md5_salt(int len, char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_salt_pre ) { + void (*preHookFunc) (int *len, char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_salt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_salt_pre[hIndex].func; + preHookFunc(&len, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.salt(len, output); + } + if( HPMHooks.count.HP_md5_salt_post ) { + void (*postHookFunc) (int len, char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_salt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_salt_post[hIndex].func; + postHookFunc(len, output); + } + } + return; +} /* nullpo_interface */ void HP_nullpo_assert_report(const char *file, int line, const char *func, const char *targetname, const char *title) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_login.sources.inc b/src/plugins/HPMHooking/HPMHooking_login.sources.inc index df0092691..cbaf06854 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.sources.inc @@ -36,6 +36,7 @@ memcpy(&HPMHooks.source.PRIV__lclif, lclif->p, sizeof(struct lclif_interface_pri memcpy(&HPMHooks.source.libconfig, libconfig, sizeof(struct libconfig_interface)); memcpy(&HPMHooks.source.login, login, sizeof(struct login_interface)); memcpy(&HPMHooks.source.iMalloc, iMalloc, sizeof(struct malloc_interface)); +memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); memcpy(&HPMHooks.source.nullpo, nullpo, sizeof(struct nullpo_interface)); memcpy(&HPMHooks.source.showmsg, showmsg, sizeof(struct showmsg_interface)); memcpy(&HPMHooks.source.sockt, sockt, sizeof(struct socket_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index 383517175..574496f00 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -3314,6 +3314,12 @@ struct { struct HPMHookPoint *HP_mapreg_reload_post; struct HPMHookPoint *HP_mapreg_config_read_pre; struct HPMHookPoint *HP_mapreg_config_read_post; + struct HPMHookPoint *HP_md5_string_pre; + struct HPMHookPoint *HP_md5_string_post; + struct HPMHookPoint *HP_md5_binary_pre; + struct HPMHookPoint *HP_md5_binary_post; + struct HPMHookPoint *HP_md5_salt_pre; + struct HPMHookPoint *HP_md5_salt_post; struct HPMHookPoint *HP_mercenary_init_pre; struct HPMHookPoint *HP_mercenary_init_post; struct HPMHookPoint *HP_mercenary_class_pre; @@ -9275,6 +9281,12 @@ struct { int HP_mapreg_reload_post; int HP_mapreg_config_read_pre; int HP_mapreg_config_read_post; + int HP_md5_string_pre; + int HP_md5_string_post; + int HP_md5_binary_pre; + int HP_md5_binary_post; + int HP_md5_salt_pre; + int HP_md5_salt_post; int HP_mercenary_init_pre; int HP_mercenary_init_post; int HP_mercenary_class_pre; @@ -11979,6 +11991,7 @@ struct { struct mapindex_interface mapindex; struct mapit_interface mapit; struct mapreg_interface mapreg; + struct md5_interface md5; struct mercenary_interface mercenary; struct mob_interface mob; struct npc_chat_interface npc_chat; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index aeb76be5b..3b6582b18 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -1701,6 +1701,10 @@ struct HookingPointData HookingPoints[] = { { HP_POP(mapreg->destroyreg, HP_mapreg_destroyreg) }, { HP_POP(mapreg->reload, HP_mapreg_reload) }, { HP_POP(mapreg->config_read, HP_mapreg_config_read) }, +/* md5_interface */ + { HP_POP(md5->string, HP_md5_string) }, + { HP_POP(md5->binary, HP_md5_binary) }, + { HP_POP(md5->salt, HP_md5_salt) }, /* mercenary_interface */ { HP_POP(mercenary->init, HP_mercenary_init) }, { HP_POP(mercenary->class, HP_mercenary_class) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index 62ce7d41f..849008661 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -43862,6 +43862,85 @@ bool HP_mapreg_config_read(const char *w1, const char *w2) { } return retVal___; } +/* md5_interface */ +void HP_md5_string(const char *string, char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_string_pre ) { + void (*preHookFunc) (const char **string, char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_string_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_string_pre[hIndex].func; + preHookFunc(&string, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.string(string, output); + } + if( HPMHooks.count.HP_md5_string_post ) { + void (*postHookFunc) (const char *string, char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_string_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_string_post[hIndex].func; + postHookFunc(string, output); + } + } + return; +} +void HP_md5_binary(const char *string, unsigned char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_binary_pre ) { + void (*preHookFunc) (const char **string, unsigned char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_binary_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_binary_pre[hIndex].func; + preHookFunc(&string, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.binary(string, output); + } + if( HPMHooks.count.HP_md5_binary_post ) { + void (*postHookFunc) (const char *string, unsigned char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_binary_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_binary_post[hIndex].func; + postHookFunc(string, output); + } + } + return; +} +void HP_md5_salt(int len, char *output) { + int hIndex = 0; + if( HPMHooks.count.HP_md5_salt_pre ) { + void (*preHookFunc) (int *len, char **output); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_salt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_md5_salt_pre[hIndex].func; + preHookFunc(&len, &output); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.md5.salt(len, output); + } + if( HPMHooks.count.HP_md5_salt_post ) { + void (*postHookFunc) (int len, char *output); + for(hIndex = 0; hIndex < HPMHooks.count.HP_md5_salt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_md5_salt_post[hIndex].func; + postHookFunc(len, output); + } + } + return; +} /* mercenary_interface */ void HP_mercenary_init(bool minimal) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_map.sources.inc b/src/plugins/HPMHooking/HPMHooking_map.sources.inc index 50e83206a..5d556c065 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.sources.inc @@ -56,6 +56,7 @@ memcpy(&HPMHooks.source.map, map, sizeof(struct map_interface)); memcpy(&HPMHooks.source.mapindex, mapindex, sizeof(struct mapindex_interface)); memcpy(&HPMHooks.source.mapit, mapit, sizeof(struct mapit_interface)); memcpy(&HPMHooks.source.mapreg, mapreg, sizeof(struct mapreg_interface)); +memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); memcpy(&HPMHooks.source.mercenary, mercenary, sizeof(struct mercenary_interface)); memcpy(&HPMHooks.source.mob, mob, sizeof(struct mob_interface)); memcpy(&HPMHooks.source.npc_chat, npc_chat, sizeof(struct npc_chat_interface)); -- cgit v1.2.3-70-g09d2 From 909b9d35ff9bdaaac784cf88f669eea0982fdd58 Mon Sep 17 00:00:00 2001 From: hemagx Date: Wed, 17 Feb 2016 15:29:36 +0200 Subject: Interface mutex.c --- src/common/console.c | 20 +++---- src/common/core.c | 2 + src/common/mutex.c | 25 +++++++- src/common/mutex.h | 152 +++++++++++++++++++++++++---------------------- src/plugins/HPMHooking.c | 1 + 5 files changed, 115 insertions(+), 85 deletions(-) diff --git a/src/common/console.c b/src/common/console.c index 0be33e5c3..bca9f36aa 100644 --- a/src/common/console.c +++ b/src/common/console.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 @@ -475,9 +475,9 @@ void *cThread_main(void *x) { LeaveSpinLock(&console->input->ptlock); } } - ramutex_lock( console->input->ptmutex ); - racond_wait( console->input->ptcond, console->input->ptmutex, -1 ); - ramutex_unlock( console->input->ptmutex ); + mutex->lock(console->input->ptmutex); + mutex->cond_wait(console->input->ptcond, console->input->ptmutex, -1); + mutex->unlock(console->input->ptmutex); } return NULL; @@ -490,19 +490,19 @@ int console_parse_timer(int tid, int64 tick, int id, intptr_t data) { } cinput.count = 0; LeaveSpinLock(&console->input->ptlock); - racond_signal(console->input->ptcond); + mutex->cond_signal(console->input->ptcond); return 0; } void console_parse_final(void) { if( console->input->ptstate ) { InterlockedDecrement(&console->input->ptstate); - racond_signal(console->input->ptcond); + mutex->cond_signal(console->input->ptcond); /* wait for thread to close */ rathread_wait(console->input->pthread, NULL); - racond_destroy(console->input->ptcond); - ramutex_destroy(console->input->ptmutex); + mutex->cond_destroy(console->input->ptcond); + mutex->destroy(console->input->ptmutex); } } void console_parse_init(void) { @@ -512,8 +512,8 @@ void console_parse_init(void) { InitializeSpinLock(&console->input->ptlock); - console->input->ptmutex = ramutex_create(); - console->input->ptcond = racond_create(); + console->input->ptmutex = mutex->create(); + console->input->ptcond = mutex->cond_create(); if( (console->input->pthread = rathread_create(console->input->pthread_main, NULL)) == NULL ){ ShowFatalError("console_parse_init: failed to spawn console_parse thread.\n"); diff --git a/src/common/core.c b/src/common/core.c index 19f2ef0c7..314bdf947 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -41,6 +41,7 @@ # include "common/conf.h" # include "common/ers.h" # include "common/md5calc.h" +# include "common/mutex.h" # include "common/socket.h" # include "common/sql.h" # include "common/thread.h" @@ -257,6 +258,7 @@ void core_defaults(void) { cmdline_defaults(); des_defaults(); #ifndef MINICORE + mutex_defaults(); libconfig_defaults(); sql_defaults(); timer_defaults(); diff --git a/src/common/mutex.c b/src/common/mutex.c index 0f02b153f..15c0c5101 100644 --- a/src/common/mutex.c +++ b/src/common/mutex.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) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify @@ -34,6 +34,9 @@ #include #endif +struct mutex_interface mutex_s; +struct mutex_interface *mutex; + struct ramutex{ #ifdef WIN32 CRITICAL_SECTION hMutex; @@ -181,7 +184,7 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { // we can release the mutex (m) here, cause win's // manual reset events maintain state when used with // SetEvent() - ramutex_unlock(m); + mutex->unlock(m); result = WaitForMultipleObjects(2, c->events, FALSE, ms); @@ -196,7 +199,7 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { if(is_last == true) ResetEvent( c->events[EVENT_COND_BROADCAST] ); - ramutex_lock(m); + mutex->lock(m); #else if(timeout_ticks < 0){ @@ -247,3 +250,19 @@ void racond_broadcast(racond *c) { pthread_cond_broadcast(&c->hCond); #endif }//end: racond_broadcast() + +void mutex_defaults(void) +{ + mutex = &mutex_s; + mutex->create = ramutex_create; + mutex->destroy = ramutex_destroy; + mutex->lock = ramutex_lock; + mutex->trylock = ramutex_trylock; + mutex->unlock = ramutex_unlock; + + mutex->cond_create = racond_create; + mutex->cond_destroy = racond_destroy; + mutex->cond_wait = racond_wait; + mutex->cond_signal = racond_signal; + mutex->cond_broadcast = racond_broadcast; +} diff --git a/src/common/mutex.h b/src/common/mutex.h index e49791493..54e304add 100644 --- a/src/common/mutex.h +++ b/src/common/mutex.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) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify @@ -21,91 +21,99 @@ #ifndef COMMON_MUTEX_H #define COMMON_MUTEX_H -#include "common/cbasetypes.h" +#include "common/hercules.h" typedef struct ramutex ramutex; // Mutex typedef struct racond racond; // Condition Var -#ifdef HERCULES_CORE -/** - * Creates a Mutex - * - * @return not NULL - */ -ramutex *ramutex_create(void); +struct mutex_interface { + /** + * Creates a Mutex + * + * @return The created mutex. + */ + ramutex *(*create) (void); -/** - * Destroys a Mutex - * - * @param m - the mutex to destroy - */ -void ramutex_destroy(ramutex *m); + /** + * Destroys a Mutex. + * + * @param m the mutex to destroy. + */ + void (*destroy) (ramutex *m); -/** - * Gets a lock - * - * @param m - the mutex to lock - */ -void ramutex_lock(ramutex *m); + /** + * Gets a lock. + * + * @param m The mutex to lock. + */ + void (*lock) (ramutex *m); -/** - * Trys to get the Lock - * - * @param m - the mutex try to lock - * - * @return boolean (true = got the lock) - */ -bool ramutex_trylock(ramutex *m); + /** + * Tries to get a lock. + * + * @param m The mutex to try to lock. + * @return success status. + * @retval true if the lock was acquired. + * @retval false if the mutex couldn't be locked. + */ + bool (*trylock) (ramutex *m); -/** - * Unlocks a mutex - * - * @param m - the mutex to unlock - */ -void ramutex_unlock(ramutex *m); + /** + * Unlocks a mutex. + * + * @param m The mutex to unlock. + */ + void (*unlock) (ramutex *m); -/** - * Creates a Condition variable - * - * @return not NULL - */ -racond *racond_create(void); + /** + * Creates a Condition variable. + * + * @return the created condition variable. + */ + racond *(*cond_create) (void); -/** - * Destroy a Condition variable - * - * @param c - the condition variable to destroy - */ -void racond_destroy(racond *c); + /** + * Destroys a Condition variable. + * + * @param c the condition variable to destroy. + */ + void (*cond_destroy) (racond *c); -/** - * Waits Until state is signaled - * - * @param c - the condition var to wait for signaled state - * @param m - the mutex used for synchronization - * @param timeout_ticks - timeout in ticks ( -1 = INFINITE ) - */ -void racond_wait(racond *c, ramutex *m, sysint timeout_ticks); + /** + * Waits Until state is signaled. + * + * @param c The condition var to wait for signaled state. + * @param m The mutex used for synchronization. + * @param timeout_ticks Timeout in ticks (-1 = INFINITE) + */ + void (*cond_wait) (racond *c, ramutex *m, sysint timeout_ticks); -/** - * Sets the given condition var to signaled state - * - * @param c - condition var to set in signaled state. - * - * @note: - * Only one waiter gets notified. - */ -void racond_signal(racond *c); + /** + * Sets the given condition var to signaled state. + * + * @remark + * Only one waiter gets notified. + * + * @param c Condition var to set in signaled state. + */ + void (*cond_signal) (racond *c); -/** - * Sets notifies all waiting threads thats signaled. - * @param c - condition var to set in signaled state - * - * @note: - * All Waiters getting notified. - */ -void racond_broadcast(racond *c); + /** + * Sets notifies all waiting threads thats signaled. + * + * @remark + * All Waiters getting notified. + * + * @param c Condition var to set in signaled state. + */ + void (*cond_broadcast) (racond *c); +}; + +#ifdef HERCULES_CORE +void mutex_defaults(void); #endif // HERCULES_CORE +HPShared struct mutex_interface *mutex; + #endif /* COMMON_MUTEX_H */ diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index be6947754..0b1b67e61 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -112,6 +112,7 @@ #include "common/des.h" #include "common/md5calc.h" #include "common/memmgr.h" +#include "common/mutex.h" #include "common/nullpo.h" #include "common/showmsg.h" #include "common/socket.h" -- cgit v1.2.3-70-g09d2 From 02983c41e397060cc2215db44ec43f9cc38c7ace Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 01:58:52 +0100 Subject: Removed unnecessary typedefs from mutex.h - ramutex -> struct mutex_data - racond -> struct cond_data Signed-off-by: Haru --- src/common/console.h | 7 +++-- src/common/mutex.c | 80 +++++++++++++++++++++++++--------------------------- src/common/mutex.h | 26 +++++++++-------- 3 files changed, 56 insertions(+), 57 deletions(-) diff --git a/src/common/console.h b/src/common/console.h index 57c750a7d..f97e36456 100644 --- a/src/common/console.h +++ b/src/common/console.h @@ -22,12 +22,13 @@ #include "common/hercules.h" #include "common/db.h" -#include "common/mutex.h" #include "common/spinlock.h" #include "common/thread.h" /* Forward Declarations */ struct Sql; // common/sql.h +struct mutex_data; +struct cond_data; /** * Queue Max @@ -74,8 +75,8 @@ struct console_input_interface { SPIN_LOCK ptlock;/* parse thread lock */ rAthread *pthread;/* parse thread */ volatile int32 ptstate;/* parse thread state */ - ramutex *ptmutex;/* parse thread mutex */ - racond *ptcond;/* parse thread cond */ + struct mutex_data *ptmutex; ///< parse thread mutex. + struct cond_data *ptcond; ///< parse thread conditional variable. /* */ VECTOR_DECL(struct CParseEntry *) command_list; VECTOR_DECL(struct CParseEntry *) commands; diff --git a/src/common/mutex.c b/src/common/mutex.c index 15c0c5101..a3f0fd4e5 100644 --- a/src/common/mutex.c +++ b/src/common/mutex.c @@ -37,7 +37,7 @@ struct mutex_interface mutex_s; struct mutex_interface *mutex; -struct ramutex{ +struct mutex_data { #ifdef WIN32 CRITICAL_SECTION hMutex; #else @@ -45,15 +45,13 @@ struct ramutex{ #endif }; -struct racond{ +struct cond_data { #ifdef WIN32 HANDLE events[2]; ra_align(8) volatile LONG nWaiters; CRITICAL_SECTION waiters_lock; - #define EVENT_COND_SIGNAL 0 #define EVENT_COND_BROADCAST 1 - #else pthread_cond_t hCond; #endif @@ -65,12 +63,11 @@ struct racond{ // Implementation: // -ramutex *ramutex_create(void) { - struct ramutex *m; - - m = (struct ramutex*)aMalloc( sizeof(struct ramutex) ); +struct mutex_data *ramutex_create(void) +{ + struct mutex_data *m = aMalloc(sizeof(struct mutex_data)); if (m == NULL) { - ShowFatalError("ramutex_create: OOM while allocating %"PRIuS" bytes.\n", sizeof(struct ramutex)); + ShowFatalError("ramutex_create: OOM while allocating %"PRIuS" bytes.\n", sizeof(struct mutex_data)); return NULL; } @@ -83,8 +80,8 @@ ramutex *ramutex_create(void) { return m; }//end: ramutex_create() -void ramutex_destroy(ramutex *m) { - +void ramutex_destroy(struct mutex_data *m) +{ #ifdef WIN32 DeleteCriticalSection(&m->hMutex); #else @@ -92,11 +89,10 @@ void ramutex_destroy(ramutex *m) { #endif aFree(m); - }//end: ramutex_destroy() -void ramutex_lock(ramutex *m) { - +void ramutex_lock(struct mutex_data *m) +{ #ifdef WIN32 EnterCriticalSection(&m->hMutex); #else @@ -104,27 +100,25 @@ void ramutex_lock(ramutex *m) { #endif }//end: ramutex_lock -bool ramutex_trylock(ramutex *m) { +bool ramutex_trylock(struct mutex_data *m) +{ #ifdef WIN32 - if(TryEnterCriticalSection(&m->hMutex) != FALSE) + if (TryEnterCriticalSection(&m->hMutex) != FALSE) return true; - - return false; #else - if(pthread_mutex_trylock(&m->hMutex) == 0) + if (pthread_mutex_trylock(&m->hMutex) == 0) return true; - - return false; #endif + return false; }//end: ramutex_trylock() -void ramutex_unlock(ramutex *m) { +void ramutex_unlock(struct mutex_data *m) +{ #ifdef WIN32 LeaveCriticalSection(&m->hMutex); #else pthread_mutex_unlock(&m->hMutex); #endif - }//end: ramutex_unlock() /////////////// @@ -133,12 +127,11 @@ void ramutex_unlock(ramutex *m) { // Implementation: // -racond *racond_create(void) { - struct racond *c; - - c = (struct racond*)aMalloc( sizeof(struct racond) ); +struct cond_data *racond_create(void) +{ + struct cond_data *c = aMalloc(sizeof(struct cond_data)); if (c == NULL) { - ShowFatalError("racond_create: OOM while allocating %"PRIuS" bytes\n", sizeof(struct racond)); + ShowFatalError("racond_create: OOM while allocating %"PRIuS" bytes\n", sizeof(struct cond_data)); return NULL; } @@ -154,11 +147,12 @@ racond *racond_create(void) { return c; }//end: racond_create() -void racond_destroy(racond *c) { +void racond_destroy(struct cond_data *c) +{ #ifdef WIN32 - CloseHandle( c->events[ EVENT_COND_SIGNAL ] ); - CloseHandle( c->events[ EVENT_COND_BROADCAST ] ); - DeleteCriticalSection( &c->waiters_lock ); + CloseHandle(c->events[EVENT_COND_SIGNAL]); + CloseHandle(c->events[EVENT_COND_BROADCAST]); + DeleteCriticalSection(&c->waiters_lock); #else pthread_cond_destroy(&c->hCond); #endif @@ -166,7 +160,8 @@ void racond_destroy(racond *c) { aFree(c); }//end: racond_destroy() -void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { +void racond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) +{ #ifdef WIN32 register DWORD ms; int result; @@ -176,7 +171,7 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { c->nWaiters++; LeaveCriticalSection(&c->waiters_lock); - if(timeout_ticks < 0) + if (timeout_ticks < 0) ms = INFINITE; else ms = (timeout_ticks > MAXDWORD) ? (MAXDWORD - 1) : (DWORD)timeout_ticks; @@ -190,21 +185,21 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { EnterCriticalSection(&c->waiters_lock); c->nWaiters--; - if( (result == WAIT_OBJECT_0 + EVENT_COND_BROADCAST) && (c->nWaiters == 0) ) + if ((result == WAIT_OBJECT_0 + EVENT_COND_BROADCAST) && (c->nWaiters == 0)) is_last = true; // Broadcast called! LeaveCriticalSection(&c->waiters_lock); // we are the last waiter that has to be notified, or to stop waiting // so we have to do a manual reset - if(is_last == true) + if (is_last == true) ResetEvent( c->events[EVENT_COND_BROADCAST] ); mutex->lock(m); #else - if(timeout_ticks < 0){ - pthread_cond_wait( &c->hCond, &m->hMutex ); - }else{ + if (timeout_ticks < 0) { + pthread_cond_wait(&c->hCond, &m->hMutex); + } else { struct timespec wtime; int64 exact_timeout = timer->gettick() + timeout_ticks; @@ -213,11 +208,11 @@ void racond_wait(racond *c, ramutex *m, sysint timeout_ticks) { pthread_cond_timedwait( &c->hCond, &m->hMutex, &wtime); } - #endif }//end: racond_wait() -void racond_signal(racond *c) { +void racond_signal(struct cond_data *c) +{ #ifdef WIN32 # if 0 bool has_waiters = false; @@ -234,7 +229,8 @@ void racond_signal(racond *c) { #endif }//end: racond_signal() -void racond_broadcast(racond *c) { +void racond_broadcast(struct cond_data *c) +{ #ifdef WIN32 # if 0 bool has_waiters = false; diff --git a/src/common/mutex.h b/src/common/mutex.h index 54e304add..2365cf4e9 100644 --- a/src/common/mutex.h +++ b/src/common/mutex.h @@ -23,30 +23,32 @@ #include "common/hercules.h" -typedef struct ramutex ramutex; // Mutex -typedef struct racond racond; // Condition Var +/* Opaque types */ +struct mutex_data; ///< Mutex +struct cond_data; ///< Conditional variable +/* Interface */ struct mutex_interface { /** * Creates a Mutex * * @return The created mutex. */ - ramutex *(*create) (void); + struct mutex_data *(*create) (void); /** * Destroys a Mutex. * * @param m the mutex to destroy. */ - void (*destroy) (ramutex *m); + void (*destroy) (struct mutex_data *m); /** * Gets a lock. * * @param m The mutex to lock. */ - void (*lock) (ramutex *m); + void (*lock) (struct mutex_data *m); /** * Tries to get a lock. @@ -56,14 +58,14 @@ struct mutex_interface { * @retval true if the lock was acquired. * @retval false if the mutex couldn't be locked. */ - bool (*trylock) (ramutex *m); + bool (*trylock) (struct mutex_data *m); /** * Unlocks a mutex. * * @param m The mutex to unlock. */ - void (*unlock) (ramutex *m); + void (*unlock) (struct mutex_data *m); /** @@ -71,14 +73,14 @@ struct mutex_interface { * * @return the created condition variable. */ - racond *(*cond_create) (void); + struct cond_data *(*cond_create) (void); /** * Destroys a Condition variable. * * @param c the condition variable to destroy. */ - void (*cond_destroy) (racond *c); + void (*cond_destroy) (struct cond_data *c); /** * Waits Until state is signaled. @@ -87,7 +89,7 @@ struct mutex_interface { * @param m The mutex used for synchronization. * @param timeout_ticks Timeout in ticks (-1 = INFINITE) */ - void (*cond_wait) (racond *c, ramutex *m, sysint timeout_ticks); + void (*cond_wait) (struct cond_data *c, struct mutex_data *m, sysint timeout_ticks); /** * Sets the given condition var to signaled state. @@ -97,7 +99,7 @@ struct mutex_interface { * * @param c Condition var to set in signaled state. */ - void (*cond_signal) (racond *c); + void (*cond_signal) (struct cond_data *c); /** * Sets notifies all waiting threads thats signaled. @@ -107,7 +109,7 @@ struct mutex_interface { * * @param c Condition var to set in signaled state. */ - void (*cond_broadcast) (racond *c); + void (*cond_broadcast) (struct cond_data *c); }; #ifdef HERCULES_CORE -- cgit v1.2.3-70-g09d2 From 14d410fc8695049691bfb5e70e881020625f7a85 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 20:50:19 +0100 Subject: Various changes to the mutex interface Mostly stylistic changes. Cleaned up documentation. Signed-off-by: Haru --- src/common/mutex.c | 95 ++++++++++++++++++++++++++++++------------------------ src/common/mutex.h | 26 ++++++++++----- 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/common/mutex.c b/src/common/mutex.c index a3f0fd4e5..bdc2fb4dc 100644 --- a/src/common/mutex.c +++ b/src/common/mutex.c @@ -34,6 +34,10 @@ #include #endif +/** @file + * Implementation of the mutex interface. + */ + struct mutex_interface mutex_s; struct mutex_interface *mutex; @@ -57,13 +61,10 @@ struct cond_data { #endif }; -//////////////////// -// Mutex -// -// Implementation: -// +/* Mutex */ -struct mutex_data *ramutex_create(void) +/// @copydoc mutex_interface::create() +struct mutex_data *mutex_create(void) { struct mutex_data *m = aMalloc(sizeof(struct mutex_data)); if (m == NULL) { @@ -78,9 +79,10 @@ struct mutex_data *ramutex_create(void) #endif return m; -}//end: ramutex_create() +} -void ramutex_destroy(struct mutex_data *m) +/// @copydoc mutex_interface::destroy() +void mutex_destroy(struct mutex_data *m) { #ifdef WIN32 DeleteCriticalSection(&m->hMutex); @@ -89,18 +91,20 @@ void ramutex_destroy(struct mutex_data *m) #endif aFree(m); -}//end: ramutex_destroy() +} -void ramutex_lock(struct mutex_data *m) +/// @copydoc mutex_interface::lock() +void mutex_lock(struct mutex_data *m) { #ifdef WIN32 EnterCriticalSection(&m->hMutex); #else pthread_mutex_lock(&m->hMutex); #endif -}//end: ramutex_lock +} -bool ramutex_trylock(struct mutex_data *m) +/// @copydoc mutex_interface::trylock() +bool mutex_trylock(struct mutex_data *m) { #ifdef WIN32 if (TryEnterCriticalSection(&m->hMutex) != FALSE) @@ -110,24 +114,22 @@ bool ramutex_trylock(struct mutex_data *m) return true; #endif return false; -}//end: ramutex_trylock() +} -void ramutex_unlock(struct mutex_data *m) +/// @copydoc mutex_interface::unlock() +void mutex_unlock(struct mutex_data *m) { #ifdef WIN32 LeaveCriticalSection(&m->hMutex); #else pthread_mutex_unlock(&m->hMutex); #endif -}//end: ramutex_unlock() +} -/////////////// -// Condition Variables -// -// Implementation: -// +/* Conditional variable */ -struct cond_data *racond_create(void) +/// @copydoc mutex_interface::cond_create() +struct cond_data *cond_create(void) { struct cond_data *c = aMalloc(sizeof(struct cond_data)); if (c == NULL) { @@ -145,9 +147,10 @@ struct cond_data *racond_create(void) #endif return c; -}//end: racond_create() +} -void racond_destroy(struct cond_data *c) +/// @copydoc mutex_interface::cond_destroy() +void cond_destroy(struct cond_data *c) { #ifdef WIN32 CloseHandle(c->events[EVENT_COND_SIGNAL]); @@ -158,9 +161,10 @@ void racond_destroy(struct cond_data *c) #endif aFree(c); -}//end: racond_destroy() +} -void racond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) +/// @copydoc mutex_interface::cond_wait() +void cond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) { #ifdef WIN32 register DWORD ms; @@ -209,9 +213,10 @@ void racond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks pthread_cond_timedwait( &c->hCond, &m->hMutex, &wtime); } #endif -}//end: racond_wait() +} -void racond_signal(struct cond_data *c) +/// @copydoc mutex_interface::cond_signal() +void cond_signal(struct cond_data *c) { #ifdef WIN32 # if 0 @@ -223,13 +228,14 @@ void racond_signal(struct cond_data *c) if(has_waiters == true) # endif // 0 - SetEvent( c->events[ EVENT_COND_SIGNAL ] ); + SetEvent(c->events[EVENT_COND_SIGNAL]); #else pthread_cond_signal(&c->hCond); #endif -}//end: racond_signal() +} -void racond_broadcast(struct cond_data *c) +/// @copydoc mutex_interface::cond_broadcast() +void cond_broadcast(struct cond_data *c) { #ifdef WIN32 # if 0 @@ -241,24 +247,27 @@ void racond_broadcast(struct cond_data *c) if(has_waiters == true) # endif // 0 - SetEvent( c->events[ EVENT_COND_BROADCAST ] ); + SetEvent(c->events[EVENT_COND_BROADCAST]); #else pthread_cond_broadcast(&c->hCond); #endif -}//end: racond_broadcast() +} +/** + * Interface base initialization. + */ void mutex_defaults(void) { mutex = &mutex_s; - mutex->create = ramutex_create; - mutex->destroy = ramutex_destroy; - mutex->lock = ramutex_lock; - mutex->trylock = ramutex_trylock; - mutex->unlock = ramutex_unlock; - - mutex->cond_create = racond_create; - mutex->cond_destroy = racond_destroy; - mutex->cond_wait = racond_wait; - mutex->cond_signal = racond_signal; - mutex->cond_broadcast = racond_broadcast; + mutex->create = mutex_create; + mutex->destroy = mutex_destroy; + mutex->lock = mutex_lock; + mutex->trylock = mutex_trylock; + mutex->unlock = mutex_unlock; + + mutex->cond_create = cond_create; + mutex->cond_destroy = cond_destroy; + mutex->cond_wait = cond_wait; + mutex->cond_signal = cond_signal; + mutex->cond_broadcast = cond_broadcast; } diff --git a/src/common/mutex.h b/src/common/mutex.h index 2365cf4e9..0569fb0da 100644 --- a/src/common/mutex.h +++ b/src/common/mutex.h @@ -23,21 +23,28 @@ #include "common/hercules.h" +/** @file + * Mutex and conditional variables implementation for Hercules. + */ + /* Opaque types */ + struct mutex_data; ///< Mutex struct cond_data; ///< Conditional variable /* Interface */ + +/// The mutex interface. struct mutex_interface { /** - * Creates a Mutex + * Creates a mutex. * * @return The created mutex. */ struct mutex_data *(*create) (void); /** - * Destroys a Mutex. + * Destroys a mutex. * * @param m the mutex to destroy. */ @@ -46,6 +53,8 @@ struct mutex_interface { /** * Gets a lock. * + * This function blocks until the lock can be acquired. + * * @param m The mutex to lock. */ void (*lock) (struct mutex_data *m); @@ -53,6 +62,8 @@ struct mutex_interface { /** * Tries to get a lock. * + * This function returns immediately. + * * @param m The mutex to try to lock. * @return success status. * @retval true if the lock was acquired. @@ -67,18 +78,17 @@ struct mutex_interface { */ void (*unlock) (struct mutex_data *m); - /** - * Creates a Condition variable. + * Creates a conditional variable. * - * @return the created condition variable. + * @return the created conditional variable. */ struct cond_data *(*cond_create) (void); /** - * Destroys a Condition variable. + * Destroys a conditional variable. * - * @param c the condition variable to destroy. + * @param c the conditional variable to destroy. */ void (*cond_destroy) (struct cond_data *c); @@ -116,6 +126,6 @@ struct mutex_interface { void mutex_defaults(void); #endif // HERCULES_CORE -HPShared struct mutex_interface *mutex; +HPShared struct mutex_interface *mutex; ///< Pointer to the mutex interface. #endif /* COMMON_MUTEX_H */ -- cgit v1.2.3-70-g09d2 From 2af9259a48ef9d7ec864fa80d1cb0392f2f2ee7a Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 20:51:23 +0100 Subject: HPM Hooks Update Signed-off-by: Haru --- src/common/HPMDataCheck.h | 5 + src/common/HPMSymbols.inc.h | 6 + src/plugins/HPMHooking/HPMHooking.Defs.inc | 22 ++ .../HPMHooking/HPMHooking_char.HPMHooksCore.inc | 41 ++++ .../HPMHooking/HPMHooking_char.HookingPoints.inc | 11 + src/plugins/HPMHooking/HPMHooking_char.Hooks.inc | 264 +++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_char.sources.inc | 1 + .../HPMHooking/HPMHooking_login.HPMHooksCore.inc | 41 ++++ .../HPMHooking/HPMHooking_login.HookingPoints.inc | 11 + src/plugins/HPMHooking/HPMHooking_login.Hooks.inc | 264 +++++++++++++++++++++ .../HPMHooking/HPMHooking_login.sources.inc | 1 + .../HPMHooking/HPMHooking_map.HPMHooksCore.inc | 41 ++++ .../HPMHooking/HPMHooking_map.HookingPoints.inc | 11 + src/plugins/HPMHooking/HPMHooking_map.Hooks.inc | 264 +++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_map.sources.inc | 1 + 15 files changed, 984 insertions(+) diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h index 4d99115f2..426c94ba2 100644 --- a/src/common/HPMDataCheck.h +++ b/src/common/HPMDataCheck.h @@ -216,6 +216,11 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = { #else #define COMMON_MMO_H #endif // COMMON_MMO_H + #ifdef COMMON_MUTEX_H + { "mutex_interface", sizeof(struct mutex_interface), SERVER_TYPE_ALL }, + #else + #define COMMON_MUTEX_H + #endif // COMMON_MUTEX_H #ifdef COMMON_NULLPO_H { "nullpo_interface", sizeof(struct nullpo_interface), SERVER_TYPE_ALL }, #else diff --git a/src/common/HPMSymbols.inc.h b/src/common/HPMSymbols.inc.h index 2bd20aa8e..24aefe06a 100644 --- a/src/common/HPMSymbols.inc.h +++ b/src/common/HPMSymbols.inc.h @@ -179,6 +179,9 @@ struct mercenary_interface *mercenary; #ifdef MAP_MOB_H /* mob */ struct mob_interface *mob; #endif // MAP_MOB_H +#ifdef COMMON_MUTEX_H /* mutex */ +struct mutex_interface *mutex; +#endif // COMMON_MUTEX_H #ifdef MAP_NPC_H /* npc_chat */ struct npc_chat_interface *npc_chat; #endif // MAP_NPC_H @@ -417,6 +420,9 @@ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("mercenary", mercenary)) retu #ifdef MAP_MOB_H /* mob */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("mob", mob)) return "mob"; #endif // MAP_MOB_H +#ifdef COMMON_MUTEX_H /* mutex */ +if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("mutex", mutex)) return "mutex"; +#endif // COMMON_MUTEX_H #ifdef MAP_NPC_H /* npc_chat */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("npc_chat", npc_chat)) return "npc_chat"; #endif // MAP_NPC_H diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index 6e8c74174..852a2c122 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -4701,6 +4701,28 @@ typedef void (*HPMHOOK_post_mob_clear_spawninfo) (void); typedef void (*HPMHOOK_pre_mob_destroy_mob_db) (int *index); typedef void (*HPMHOOK_post_mob_destroy_mob_db) (int index); #endif // MAP_MOB_H +#ifdef COMMON_MUTEX_H /* mutex */ +typedef struct mutex_data* (*HPMHOOK_pre_mutex_create) (void); +typedef struct mutex_data* (*HPMHOOK_post_mutex_create) (struct mutex_data* retVal___); +typedef void (*HPMHOOK_pre_mutex_destroy) (struct mutex_data **m); +typedef void (*HPMHOOK_post_mutex_destroy) (struct mutex_data *m); +typedef void (*HPMHOOK_pre_mutex_lock) (struct mutex_data **m); +typedef void (*HPMHOOK_post_mutex_lock) (struct mutex_data *m); +typedef bool (*HPMHOOK_pre_mutex_trylock) (struct mutex_data **m); +typedef bool (*HPMHOOK_post_mutex_trylock) (bool retVal___, struct mutex_data *m); +typedef void (*HPMHOOK_pre_mutex_unlock) (struct mutex_data **m); +typedef void (*HPMHOOK_post_mutex_unlock) (struct mutex_data *m); +typedef struct cond_data* (*HPMHOOK_pre_mutex_cond_create) (void); +typedef struct cond_data* (*HPMHOOK_post_mutex_cond_create) (struct cond_data* retVal___); +typedef void (*HPMHOOK_pre_mutex_cond_destroy) (struct cond_data **c); +typedef void (*HPMHOOK_post_mutex_cond_destroy) (struct cond_data *c); +typedef void (*HPMHOOK_pre_mutex_cond_wait) (struct cond_data **c, struct mutex_data **m, sysint *timeout_ticks); +typedef void (*HPMHOOK_post_mutex_cond_wait) (struct cond_data *c, struct mutex_data *m, sysint timeout_ticks); +typedef void (*HPMHOOK_pre_mutex_cond_signal) (struct cond_data **c); +typedef void (*HPMHOOK_post_mutex_cond_signal) (struct cond_data *c); +typedef void (*HPMHOOK_pre_mutex_cond_broadcast) (struct cond_data **c); +typedef void (*HPMHOOK_post_mutex_cond_broadcast) (struct cond_data *c); +#endif // COMMON_MUTEX_H #ifdef MAP_NPC_H /* npc_chat */ typedef int (*HPMHOOK_pre_npc_chat_sub) (struct block_list **bl, va_list ap); typedef int (*HPMHOOK_post_npc_chat_sub) (int retVal___, struct block_list *bl, va_list ap); diff --git a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc index da3990cf3..78066d26e 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc @@ -1162,6 +1162,26 @@ struct { struct HPMHookPoint *HP_md5_binary_post; struct HPMHookPoint *HP_md5_salt_pre; struct HPMHookPoint *HP_md5_salt_post; + struct HPMHookPoint *HP_mutex_create_pre; + struct HPMHookPoint *HP_mutex_create_post; + struct HPMHookPoint *HP_mutex_destroy_pre; + struct HPMHookPoint *HP_mutex_destroy_post; + struct HPMHookPoint *HP_mutex_lock_pre; + struct HPMHookPoint *HP_mutex_lock_post; + struct HPMHookPoint *HP_mutex_trylock_pre; + struct HPMHookPoint *HP_mutex_trylock_post; + struct HPMHookPoint *HP_mutex_unlock_pre; + struct HPMHookPoint *HP_mutex_unlock_post; + struct HPMHookPoint *HP_mutex_cond_create_pre; + struct HPMHookPoint *HP_mutex_cond_create_post; + struct HPMHookPoint *HP_mutex_cond_destroy_pre; + struct HPMHookPoint *HP_mutex_cond_destroy_post; + struct HPMHookPoint *HP_mutex_cond_wait_pre; + struct HPMHookPoint *HP_mutex_cond_wait_post; + struct HPMHookPoint *HP_mutex_cond_signal_pre; + struct HPMHookPoint *HP_mutex_cond_signal_post; + struct HPMHookPoint *HP_mutex_cond_broadcast_pre; + struct HPMHookPoint *HP_mutex_cond_broadcast_post; struct HPMHookPoint *HP_nullpo_assert_report_pre; struct HPMHookPoint *HP_nullpo_assert_report_post; struct HPMHookPoint *HP_pincode_handle_pre; @@ -2577,6 +2597,26 @@ struct { int HP_md5_binary_post; int HP_md5_salt_pre; int HP_md5_salt_post; + int HP_mutex_create_pre; + int HP_mutex_create_post; + int HP_mutex_destroy_pre; + int HP_mutex_destroy_post; + int HP_mutex_lock_pre; + int HP_mutex_lock_post; + int HP_mutex_trylock_pre; + int HP_mutex_trylock_post; + int HP_mutex_unlock_pre; + int HP_mutex_unlock_post; + int HP_mutex_cond_create_pre; + int HP_mutex_cond_create_post; + int HP_mutex_cond_destroy_pre; + int HP_mutex_cond_destroy_post; + int HP_mutex_cond_wait_pre; + int HP_mutex_cond_wait_post; + int HP_mutex_cond_signal_pre; + int HP_mutex_cond_signal_post; + int HP_mutex_cond_broadcast_pre; + int HP_mutex_cond_broadcast_post; int HP_nullpo_assert_report_pre; int HP_nullpo_assert_report_post; int HP_pincode_handle_pre; @@ -2881,6 +2921,7 @@ struct { struct mapif_interface mapif; struct mapindex_interface mapindex; struct md5_interface md5; + struct mutex_interface mutex; struct nullpo_interface nullpo; struct pincode_interface pincode; struct showmsg_interface showmsg; diff --git a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc index 15db8625a..405dd4a91 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc @@ -619,6 +619,17 @@ struct HookingPointData HookingPoints[] = { { HP_POP(md5->string, HP_md5_string) }, { HP_POP(md5->binary, HP_md5_binary) }, { HP_POP(md5->salt, HP_md5_salt) }, +/* mutex_interface */ + { HP_POP(mutex->create, HP_mutex_create) }, + { HP_POP(mutex->destroy, HP_mutex_destroy) }, + { HP_POP(mutex->lock, HP_mutex_lock) }, + { HP_POP(mutex->trylock, HP_mutex_trylock) }, + { HP_POP(mutex->unlock, HP_mutex_unlock) }, + { HP_POP(mutex->cond_create, HP_mutex_cond_create) }, + { HP_POP(mutex->cond_destroy, HP_mutex_cond_destroy) }, + { HP_POP(mutex->cond_wait, HP_mutex_cond_wait) }, + { HP_POP(mutex->cond_signal, HP_mutex_cond_signal) }, + { HP_POP(mutex->cond_broadcast, HP_mutex_cond_broadcast) }, /* nullpo_interface */ { HP_POP(nullpo->assert_report, HP_nullpo_assert_report) }, /* pincode_interface */ diff --git a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc index fd69a2bd9..4ec1abcd1 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc @@ -15199,6 +15199,270 @@ void HP_md5_salt(int len, char *output) { } return; } +/* mutex_interface */ +struct mutex_data* HP_mutex_create(void) { + int hIndex = 0; + struct mutex_data* retVal___ = NULL; + if( HPMHooks.count.HP_mutex_create_pre ) { + struct mutex_data* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_create_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.create(); + } + if( HPMHooks.count.HP_mutex_create_post ) { + struct mutex_data* (*postHookFunc) (struct mutex_data* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +void HP_mutex_destroy(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_destroy_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_destroy_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.destroy(m); + } + if( HPMHooks.count.HP_mutex_destroy_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_destroy_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +void HP_mutex_lock(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_lock_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_lock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_lock_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.lock(m); + } + if( HPMHooks.count.HP_mutex_lock_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_lock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_lock_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +bool HP_mutex_trylock(struct mutex_data *m) { + int hIndex = 0; + bool retVal___ = false; + if( HPMHooks.count.HP_mutex_trylock_pre ) { + bool (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_trylock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_trylock_pre[hIndex].func; + retVal___ = preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.trylock(m); + } + if( HPMHooks.count.HP_mutex_trylock_post ) { + bool (*postHookFunc) (bool retVal___, struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_trylock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_trylock_post[hIndex].func; + retVal___ = postHookFunc(retVal___, m); + } + } + return retVal___; +} +void HP_mutex_unlock(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_unlock_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_unlock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_unlock_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.unlock(m); + } + if( HPMHooks.count.HP_mutex_unlock_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_unlock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_unlock_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +struct cond_data* HP_mutex_cond_create(void) { + int hIndex = 0; + struct cond_data* retVal___ = NULL; + if( HPMHooks.count.HP_mutex_cond_create_pre ) { + struct cond_data* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_create_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.cond_create(); + } + if( HPMHooks.count.HP_mutex_cond_create_post ) { + struct cond_data* (*postHookFunc) (struct cond_data* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +void HP_mutex_cond_destroy(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_destroy_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_destroy_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_destroy(c); + } + if( HPMHooks.count.HP_mutex_cond_destroy_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_destroy_post[hIndex].func; + postHookFunc(c); + } + } + return; +} +void HP_mutex_cond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_wait_pre ) { + void (*preHookFunc) (struct cond_data **c, struct mutex_data **m, sysint *timeout_ticks); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_wait_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_wait_pre[hIndex].func; + preHookFunc(&c, &m, &timeout_ticks); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_wait(c, m, timeout_ticks); + } + if( HPMHooks.count.HP_mutex_cond_wait_post ) { + void (*postHookFunc) (struct cond_data *c, struct mutex_data *m, sysint timeout_ticks); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_wait_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_wait_post[hIndex].func; + postHookFunc(c, m, timeout_ticks); + } + } + return; +} +void HP_mutex_cond_signal(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_signal_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_signal_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_signal_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_signal(c); + } + if( HPMHooks.count.HP_mutex_cond_signal_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_signal_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_signal_post[hIndex].func; + postHookFunc(c); + } + } + return; +} +void HP_mutex_cond_broadcast(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_broadcast_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_broadcast_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_broadcast_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_broadcast(c); + } + if( HPMHooks.count.HP_mutex_cond_broadcast_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_broadcast_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_broadcast_post[hIndex].func; + postHookFunc(c); + } + } + return; +} /* nullpo_interface */ void HP_nullpo_assert_report(const char *file, int line, const char *func, const char *targetname, const char *title) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_char.sources.inc b/src/plugins/HPMHooking/HPMHooking_char.sources.inc index 925fefd93..c17c0313d 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.sources.inc @@ -50,6 +50,7 @@ memcpy(&HPMHooks.source.iMalloc, iMalloc, sizeof(struct malloc_interface)); memcpy(&HPMHooks.source.mapif, mapif, sizeof(struct mapif_interface)); memcpy(&HPMHooks.source.mapindex, mapindex, sizeof(struct mapindex_interface)); memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); +memcpy(&HPMHooks.source.mutex, mutex, sizeof(struct mutex_interface)); memcpy(&HPMHooks.source.nullpo, nullpo, sizeof(struct nullpo_interface)); memcpy(&HPMHooks.source.pincode, pincode, sizeof(struct pincode_interface)); memcpy(&HPMHooks.source.showmsg, showmsg, sizeof(struct showmsg_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc index daf7d35b0..e6d43c833 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc @@ -410,6 +410,26 @@ struct { struct HPMHookPoint *HP_md5_binary_post; struct HPMHookPoint *HP_md5_salt_pre; struct HPMHookPoint *HP_md5_salt_post; + struct HPMHookPoint *HP_mutex_create_pre; + struct HPMHookPoint *HP_mutex_create_post; + struct HPMHookPoint *HP_mutex_destroy_pre; + struct HPMHookPoint *HP_mutex_destroy_post; + struct HPMHookPoint *HP_mutex_lock_pre; + struct HPMHookPoint *HP_mutex_lock_post; + struct HPMHookPoint *HP_mutex_trylock_pre; + struct HPMHookPoint *HP_mutex_trylock_post; + struct HPMHookPoint *HP_mutex_unlock_pre; + struct HPMHookPoint *HP_mutex_unlock_post; + struct HPMHookPoint *HP_mutex_cond_create_pre; + struct HPMHookPoint *HP_mutex_cond_create_post; + struct HPMHookPoint *HP_mutex_cond_destroy_pre; + struct HPMHookPoint *HP_mutex_cond_destroy_post; + struct HPMHookPoint *HP_mutex_cond_wait_pre; + struct HPMHookPoint *HP_mutex_cond_wait_post; + struct HPMHookPoint *HP_mutex_cond_signal_pre; + struct HPMHookPoint *HP_mutex_cond_signal_post; + struct HPMHookPoint *HP_mutex_cond_broadcast_pre; + struct HPMHookPoint *HP_mutex_cond_broadcast_post; struct HPMHookPoint *HP_nullpo_assert_report_pre; struct HPMHookPoint *HP_nullpo_assert_report_post; struct HPMHookPoint *HP_showmsg_init_pre; @@ -1053,6 +1073,26 @@ struct { int HP_md5_binary_post; int HP_md5_salt_pre; int HP_md5_salt_post; + int HP_mutex_create_pre; + int HP_mutex_create_post; + int HP_mutex_destroy_pre; + int HP_mutex_destroy_post; + int HP_mutex_lock_pre; + int HP_mutex_lock_post; + int HP_mutex_trylock_pre; + int HP_mutex_trylock_post; + int HP_mutex_unlock_pre; + int HP_mutex_unlock_post; + int HP_mutex_cond_create_pre; + int HP_mutex_cond_create_post; + int HP_mutex_cond_destroy_pre; + int HP_mutex_cond_destroy_post; + int HP_mutex_cond_wait_pre; + int HP_mutex_cond_wait_post; + int HP_mutex_cond_signal_pre; + int HP_mutex_cond_signal_post; + int HP_mutex_cond_broadcast_pre; + int HP_mutex_cond_broadcast_post; int HP_nullpo_assert_report_pre; int HP_nullpo_assert_report_post; int HP_showmsg_init_pre; @@ -1324,6 +1364,7 @@ struct { struct login_interface login; struct malloc_interface iMalloc; struct md5_interface md5; + struct mutex_interface mutex; struct nullpo_interface nullpo; struct showmsg_interface showmsg; struct socket_interface sockt; diff --git a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc index 6de163d9f..ce9a112f7 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc @@ -230,6 +230,17 @@ struct HookingPointData HookingPoints[] = { { HP_POP(md5->string, HP_md5_string) }, { HP_POP(md5->binary, HP_md5_binary) }, { HP_POP(md5->salt, HP_md5_salt) }, +/* mutex_interface */ + { HP_POP(mutex->create, HP_mutex_create) }, + { HP_POP(mutex->destroy, HP_mutex_destroy) }, + { HP_POP(mutex->lock, HP_mutex_lock) }, + { HP_POP(mutex->trylock, HP_mutex_trylock) }, + { HP_POP(mutex->unlock, HP_mutex_unlock) }, + { HP_POP(mutex->cond_create, HP_mutex_cond_create) }, + { HP_POP(mutex->cond_destroy, HP_mutex_cond_destroy) }, + { HP_POP(mutex->cond_wait, HP_mutex_cond_wait) }, + { HP_POP(mutex->cond_signal, HP_mutex_cond_signal) }, + { HP_POP(mutex->cond_broadcast, HP_mutex_cond_broadcast) }, /* nullpo_interface */ { HP_POP(nullpo->assert_report, HP_nullpo_assert_report) }, /* showmsg_interface */ diff --git a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc index a90ed233b..dc0ed4a17 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc @@ -5168,6 +5168,270 @@ void HP_md5_salt(int len, char *output) { } return; } +/* mutex_interface */ +struct mutex_data* HP_mutex_create(void) { + int hIndex = 0; + struct mutex_data* retVal___ = NULL; + if( HPMHooks.count.HP_mutex_create_pre ) { + struct mutex_data* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_create_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.create(); + } + if( HPMHooks.count.HP_mutex_create_post ) { + struct mutex_data* (*postHookFunc) (struct mutex_data* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +void HP_mutex_destroy(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_destroy_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_destroy_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.destroy(m); + } + if( HPMHooks.count.HP_mutex_destroy_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_destroy_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +void HP_mutex_lock(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_lock_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_lock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_lock_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.lock(m); + } + if( HPMHooks.count.HP_mutex_lock_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_lock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_lock_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +bool HP_mutex_trylock(struct mutex_data *m) { + int hIndex = 0; + bool retVal___ = false; + if( HPMHooks.count.HP_mutex_trylock_pre ) { + bool (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_trylock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_trylock_pre[hIndex].func; + retVal___ = preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.trylock(m); + } + if( HPMHooks.count.HP_mutex_trylock_post ) { + bool (*postHookFunc) (bool retVal___, struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_trylock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_trylock_post[hIndex].func; + retVal___ = postHookFunc(retVal___, m); + } + } + return retVal___; +} +void HP_mutex_unlock(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_unlock_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_unlock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_unlock_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.unlock(m); + } + if( HPMHooks.count.HP_mutex_unlock_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_unlock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_unlock_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +struct cond_data* HP_mutex_cond_create(void) { + int hIndex = 0; + struct cond_data* retVal___ = NULL; + if( HPMHooks.count.HP_mutex_cond_create_pre ) { + struct cond_data* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_create_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.cond_create(); + } + if( HPMHooks.count.HP_mutex_cond_create_post ) { + struct cond_data* (*postHookFunc) (struct cond_data* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +void HP_mutex_cond_destroy(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_destroy_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_destroy_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_destroy(c); + } + if( HPMHooks.count.HP_mutex_cond_destroy_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_destroy_post[hIndex].func; + postHookFunc(c); + } + } + return; +} +void HP_mutex_cond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_wait_pre ) { + void (*preHookFunc) (struct cond_data **c, struct mutex_data **m, sysint *timeout_ticks); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_wait_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_wait_pre[hIndex].func; + preHookFunc(&c, &m, &timeout_ticks); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_wait(c, m, timeout_ticks); + } + if( HPMHooks.count.HP_mutex_cond_wait_post ) { + void (*postHookFunc) (struct cond_data *c, struct mutex_data *m, sysint timeout_ticks); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_wait_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_wait_post[hIndex].func; + postHookFunc(c, m, timeout_ticks); + } + } + return; +} +void HP_mutex_cond_signal(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_signal_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_signal_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_signal_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_signal(c); + } + if( HPMHooks.count.HP_mutex_cond_signal_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_signal_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_signal_post[hIndex].func; + postHookFunc(c); + } + } + return; +} +void HP_mutex_cond_broadcast(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_broadcast_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_broadcast_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_broadcast_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_broadcast(c); + } + if( HPMHooks.count.HP_mutex_cond_broadcast_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_broadcast_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_broadcast_post[hIndex].func; + postHookFunc(c); + } + } + return; +} /* nullpo_interface */ void HP_nullpo_assert_report(const char *file, int line, const char *func, const char *targetname, const char *title) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_login.sources.inc b/src/plugins/HPMHooking/HPMHooking_login.sources.inc index cbaf06854..fc530bcbf 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.sources.inc @@ -37,6 +37,7 @@ memcpy(&HPMHooks.source.libconfig, libconfig, sizeof(struct libconfig_interface) memcpy(&HPMHooks.source.login, login, sizeof(struct login_interface)); memcpy(&HPMHooks.source.iMalloc, iMalloc, sizeof(struct malloc_interface)); memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); +memcpy(&HPMHooks.source.mutex, mutex, sizeof(struct mutex_interface)); memcpy(&HPMHooks.source.nullpo, nullpo, sizeof(struct nullpo_interface)); memcpy(&HPMHooks.source.showmsg, showmsg, sizeof(struct showmsg_interface)); memcpy(&HPMHooks.source.sockt, sockt, sizeof(struct socket_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index 574496f00..d85e64eb2 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -3576,6 +3576,26 @@ struct { struct HPMHookPoint *HP_mob_clear_spawninfo_post; struct HPMHookPoint *HP_mob_destroy_mob_db_pre; struct HPMHookPoint *HP_mob_destroy_mob_db_post; + struct HPMHookPoint *HP_mutex_create_pre; + struct HPMHookPoint *HP_mutex_create_post; + struct HPMHookPoint *HP_mutex_destroy_pre; + struct HPMHookPoint *HP_mutex_destroy_post; + struct HPMHookPoint *HP_mutex_lock_pre; + struct HPMHookPoint *HP_mutex_lock_post; + struct HPMHookPoint *HP_mutex_trylock_pre; + struct HPMHookPoint *HP_mutex_trylock_post; + struct HPMHookPoint *HP_mutex_unlock_pre; + struct HPMHookPoint *HP_mutex_unlock_post; + struct HPMHookPoint *HP_mutex_cond_create_pre; + struct HPMHookPoint *HP_mutex_cond_create_post; + struct HPMHookPoint *HP_mutex_cond_destroy_pre; + struct HPMHookPoint *HP_mutex_cond_destroy_post; + struct HPMHookPoint *HP_mutex_cond_wait_pre; + struct HPMHookPoint *HP_mutex_cond_wait_post; + struct HPMHookPoint *HP_mutex_cond_signal_pre; + struct HPMHookPoint *HP_mutex_cond_signal_post; + struct HPMHookPoint *HP_mutex_cond_broadcast_pre; + struct HPMHookPoint *HP_mutex_cond_broadcast_post; struct HPMHookPoint *HP_npc_chat_sub_pre; struct HPMHookPoint *HP_npc_chat_sub_post; struct HPMHookPoint *HP_npc_chat_finalize_pre; @@ -9543,6 +9563,26 @@ struct { int HP_mob_clear_spawninfo_post; int HP_mob_destroy_mob_db_pre; int HP_mob_destroy_mob_db_post; + int HP_mutex_create_pre; + int HP_mutex_create_post; + int HP_mutex_destroy_pre; + int HP_mutex_destroy_post; + int HP_mutex_lock_pre; + int HP_mutex_lock_post; + int HP_mutex_trylock_pre; + int HP_mutex_trylock_post; + int HP_mutex_unlock_pre; + int HP_mutex_unlock_post; + int HP_mutex_cond_create_pre; + int HP_mutex_cond_create_post; + int HP_mutex_cond_destroy_pre; + int HP_mutex_cond_destroy_post; + int HP_mutex_cond_wait_pre; + int HP_mutex_cond_wait_post; + int HP_mutex_cond_signal_pre; + int HP_mutex_cond_signal_post; + int HP_mutex_cond_broadcast_pre; + int HP_mutex_cond_broadcast_post; int HP_npc_chat_sub_pre; int HP_npc_chat_sub_post; int HP_npc_chat_finalize_pre; @@ -11994,6 +12034,7 @@ struct { struct md5_interface md5; struct mercenary_interface mercenary; struct mob_interface mob; + struct mutex_interface mutex; struct npc_chat_interface npc_chat; struct npc_interface npc; struct nullpo_interface nullpo; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index 3b6582b18..59949c536 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -1835,6 +1835,17 @@ struct HookingPointData HookingPoints[] = { { HP_POP(mob->load, HP_mob_load) }, { HP_POP(mob->clear_spawninfo, HP_mob_clear_spawninfo) }, { HP_POP(mob->destroy_mob_db, HP_mob_destroy_mob_db) }, +/* mutex_interface */ + { HP_POP(mutex->create, HP_mutex_create) }, + { HP_POP(mutex->destroy, HP_mutex_destroy) }, + { HP_POP(mutex->lock, HP_mutex_lock) }, + { HP_POP(mutex->trylock, HP_mutex_trylock) }, + { HP_POP(mutex->unlock, HP_mutex_unlock) }, + { HP_POP(mutex->cond_create, HP_mutex_cond_create) }, + { HP_POP(mutex->cond_destroy, HP_mutex_cond_destroy) }, + { HP_POP(mutex->cond_wait, HP_mutex_cond_wait) }, + { HP_POP(mutex->cond_signal, HP_mutex_cond_signal) }, + { HP_POP(mutex->cond_broadcast, HP_mutex_cond_broadcast) }, /* npc_chat_interface */ { HP_POP(npc_chat->sub, HP_npc_chat_sub) }, { HP_POP(npc_chat->finalize, HP_npc_chat_finalize) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index 849008661..f46a5e7d0 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -47466,6 +47466,270 @@ void HP_mob_destroy_mob_db(int index) { } return; } +/* mutex_interface */ +struct mutex_data* HP_mutex_create(void) { + int hIndex = 0; + struct mutex_data* retVal___ = NULL; + if( HPMHooks.count.HP_mutex_create_pre ) { + struct mutex_data* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_create_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.create(); + } + if( HPMHooks.count.HP_mutex_create_post ) { + struct mutex_data* (*postHookFunc) (struct mutex_data* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +void HP_mutex_destroy(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_destroy_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_destroy_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.destroy(m); + } + if( HPMHooks.count.HP_mutex_destroy_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_destroy_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +void HP_mutex_lock(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_lock_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_lock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_lock_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.lock(m); + } + if( HPMHooks.count.HP_mutex_lock_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_lock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_lock_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +bool HP_mutex_trylock(struct mutex_data *m) { + int hIndex = 0; + bool retVal___ = false; + if( HPMHooks.count.HP_mutex_trylock_pre ) { + bool (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_trylock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_trylock_pre[hIndex].func; + retVal___ = preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.trylock(m); + } + if( HPMHooks.count.HP_mutex_trylock_post ) { + bool (*postHookFunc) (bool retVal___, struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_trylock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_trylock_post[hIndex].func; + retVal___ = postHookFunc(retVal___, m); + } + } + return retVal___; +} +void HP_mutex_unlock(struct mutex_data *m) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_unlock_pre ) { + void (*preHookFunc) (struct mutex_data **m); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_unlock_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_unlock_pre[hIndex].func; + preHookFunc(&m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.unlock(m); + } + if( HPMHooks.count.HP_mutex_unlock_post ) { + void (*postHookFunc) (struct mutex_data *m); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_unlock_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_unlock_post[hIndex].func; + postHookFunc(m); + } + } + return; +} +struct cond_data* HP_mutex_cond_create(void) { + int hIndex = 0; + struct cond_data* retVal___ = NULL; + if( HPMHooks.count.HP_mutex_cond_create_pre ) { + struct cond_data* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_create_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.mutex.cond_create(); + } + if( HPMHooks.count.HP_mutex_cond_create_post ) { + struct cond_data* (*postHookFunc) (struct cond_data* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +void HP_mutex_cond_destroy(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_destroy_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_destroy_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_destroy(c); + } + if( HPMHooks.count.HP_mutex_cond_destroy_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_destroy_post[hIndex].func; + postHookFunc(c); + } + } + return; +} +void HP_mutex_cond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_wait_pre ) { + void (*preHookFunc) (struct cond_data **c, struct mutex_data **m, sysint *timeout_ticks); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_wait_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_wait_pre[hIndex].func; + preHookFunc(&c, &m, &timeout_ticks); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_wait(c, m, timeout_ticks); + } + if( HPMHooks.count.HP_mutex_cond_wait_post ) { + void (*postHookFunc) (struct cond_data *c, struct mutex_data *m, sysint timeout_ticks); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_wait_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_wait_post[hIndex].func; + postHookFunc(c, m, timeout_ticks); + } + } + return; +} +void HP_mutex_cond_signal(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_signal_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_signal_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_signal_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_signal(c); + } + if( HPMHooks.count.HP_mutex_cond_signal_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_signal_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_signal_post[hIndex].func; + postHookFunc(c); + } + } + return; +} +void HP_mutex_cond_broadcast(struct cond_data *c) { + int hIndex = 0; + if( HPMHooks.count.HP_mutex_cond_broadcast_pre ) { + void (*preHookFunc) (struct cond_data **c); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_broadcast_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_mutex_cond_broadcast_pre[hIndex].func; + preHookFunc(&c); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.mutex.cond_broadcast(c); + } + if( HPMHooks.count.HP_mutex_cond_broadcast_post ) { + void (*postHookFunc) (struct cond_data *c); + for(hIndex = 0; hIndex < HPMHooks.count.HP_mutex_cond_broadcast_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_mutex_cond_broadcast_post[hIndex].func; + postHookFunc(c); + } + } + return; +} /* npc_chat_interface */ int HP_npc_chat_sub(struct block_list *bl, va_list ap) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_map.sources.inc b/src/plugins/HPMHooking/HPMHooking_map.sources.inc index 5d556c065..a835e0a03 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.sources.inc @@ -59,6 +59,7 @@ memcpy(&HPMHooks.source.mapreg, mapreg, sizeof(struct mapreg_interface)); memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); memcpy(&HPMHooks.source.mercenary, mercenary, sizeof(struct mercenary_interface)); memcpy(&HPMHooks.source.mob, mob, sizeof(struct mob_interface)); +memcpy(&HPMHooks.source.mutex, mutex, sizeof(struct mutex_interface)); memcpy(&HPMHooks.source.npc_chat, npc_chat, sizeof(struct npc_chat_interface)); memcpy(&HPMHooks.source.npc, npc, sizeof(struct npc_interface)); memcpy(&HPMHooks.source.nullpo, nullpo, sizeof(struct nullpo_interface)); -- cgit v1.2.3-70-g09d2 From 527cf1b812a117285957be820ba1dc31533e9073 Mon Sep 17 00:00:00 2001 From: hemagx Date: Wed, 17 Feb 2016 21:43:45 +0200 Subject: Interface random.c --- src/common/core.c | 13 +++++++------ src/common/random.c | 30 +++++++++++++++++++++++++---- src/common/random.h | 30 +++++++++++++++++++---------- src/map/battle.c | 2 +- src/map/homunculus.c | 50 ++++++++++++++++++++++++------------------------ src/map/skill.c | 4 ++-- src/plugins/HPMHooking.c | 1 + 7 files changed, 82 insertions(+), 48 deletions(-) diff --git a/src/common/core.c b/src/common/core.c index 314bdf947..f5860e992 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -25,15 +25,15 @@ #include "common/cbasetypes.h" #include "common/console.h" -#include "common/des.h" #include "common/db.h" +#include "common/des.h" #include "common/memmgr.h" #include "common/mmo.h" -#include "common/random.h" +#include "common/nullpo.h" #include "common/showmsg.h" #include "common/strlib.h" #include "common/sysinfo.h" -#include "common/nullpo.h" +#include "common/timer.h" #include "common/utils.h" #ifndef MINICORE @@ -42,10 +42,10 @@ # include "common/ers.h" # include "common/md5calc.h" # include "common/mutex.h" +# include "common/random.h" # include "common/socket.h" # include "common/sql.h" # include "common/thread.h" -# include "common/timer.h" #endif #ifndef _WIN32 @@ -264,6 +264,7 @@ void core_defaults(void) { timer_defaults(); db_defaults(); socket_defaults(); + rnd_defaults(); md5_defaults(); #endif } @@ -515,8 +516,7 @@ int main (int argc, char **argv) { timer->init(); /* timer first */ - rnd_init(); - srand((unsigned int)timer->gettick()); + rnd->init(); console->init(); @@ -543,6 +543,7 @@ int main (int argc, char **argv) { DB->final(); rathread_final(); ers_final(); + rnd->final(); #endif cmdline->final(); //sysinfo->final(); Called by iMalloc->final() diff --git a/src/common/random.c b/src/common/random.c index ba70a5c1d..dbb2ca830 100644 --- a/src/common/random.c +++ b/src/common/random.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 @@ -27,6 +27,7 @@ #include "common/timer.h" // gettick #include // init_genrand, genrand_int32, genrand_res53 +#include #if defined(WIN32) # include "common/winapi.h" @@ -35,6 +36,8 @@ # include #endif +struct rnd_interface rnd_s; +struct rnd_interface *rnd; /// Initializes the random number generator with an appropriate seed. void rnd_init(void) @@ -53,8 +56,14 @@ void rnd_init(void) #endif // HAVE_GETTID #endif init_genrand(seed); + + // Also initialize the stdlib rng, just in case it's used somewhere. + srand((unsigned int)timer->gettick()); } +void rnd_final(void) +{ +} /// Initializes the random number generator. void rnd_seed(uint32 seed) @@ -64,7 +73,7 @@ void rnd_seed(uint32 seed) /// Generates a random number in the interval [0, SINT32_MAX] -int32 rnd(void) +int32 rnd_random(void) { return (int32)genrand_int31(); } @@ -74,7 +83,7 @@ int32 rnd(void) /// NOTE: interval is open ended, so dice_faces is excluded (unless it's 0) uint32 rnd_roll(uint32 dice_faces) { - return (uint32)(rnd_uniform()*dice_faces); + return (uint32)(rnd->uniform()*dice_faces); } @@ -84,7 +93,7 @@ int32 rnd_value(int32 min, int32 max) { if( min >= max ) return min; - return min + (int32)(rnd_uniform()*(max-min+1)); + return min + (int32)(rnd->uniform()*(max-min+1)); } @@ -103,3 +112,16 @@ double rnd_uniform53(void) { return genrand_res53(); } + +void rnd_defaults(void) +{ + rnd = &rnd_s; + rnd->init = rnd_init; + rnd->final = rnd_final; + rnd->seed = rnd_seed; + rnd->random = rnd_random; + rnd->roll = rnd_roll; + rnd->value = rnd_value; + rnd->uniform = rnd_uniform; + rnd->uniform53 = rnd_uniform53; +} diff --git a/src/common/random.h b/src/common/random.h index 0b5abbcdc..41bdb4083 100644 --- a/src/common/random.h +++ b/src/common/random.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,17 +21,27 @@ #ifndef COMMON_RANDOM_H #define COMMON_RANDOM_H -#include "common/cbasetypes.h" +#include "common/hercules.h" + +struct rnd_interface { + void (*init) (void); + void (*final) (void); + + void (*seed) (uint32); + + int32 (*random) (void);// [0, SINT32_MAX] + uint32 (*roll) (uint32 dice_faces);// [0, dice_faces) + int32 (*value) (int32 min, int32 max);// [min, max] + double (*uniform) (void);// [0.0, 1.0) + double (*uniform53) (void);// [0.0, 1.0) +}; + +#define rnd() rnd->random() #ifdef HERCULES_CORE -void rnd_init(void); -void rnd_seed(uint32); - -int32 rnd(void);// [0, SINT32_MAX] -uint32 rnd_roll(uint32 dice_faces);// [0, dice_faces) -int32 rnd_value(int32 min, int32 max);// [min, max] -double rnd_uniform(void);// [0.0, 1.0) -double rnd_uniform53(void);// [0.0, 1.0) +void rnd_defaults(void); #endif // HERCULES_CORE +HPShared struct rnd_interface *rnd; + #endif /* COMMON_RANDOM_H */ diff --git a/src/map/battle.c b/src/map/battle.c index 944271efa..76b74a766 100644 --- a/src/map/battle.c +++ b/src/map/battle.c @@ -2431,7 +2431,7 @@ int battle_calc_skillratio(int attack_type, struct block_list *src, struct block RE_LVL_DMOD(100); break; case LG_OVERBRAND_PLUSATK: - skillratio = 200 * skill_lv + rnd_value( 10, 100); + skillratio = 200 * skill_lv + rnd->value(10, 100); RE_LVL_DMOD(100); break; case LG_RAYOFGENESIS: diff --git a/src/map/homunculus.c b/src/map/homunculus.c index 66cce23e6..080a314b8 100644 --- a/src/map/homunculus.c +++ b/src/map/homunculus.c @@ -356,14 +356,14 @@ bool homunculus_levelup(struct homun_data *hd) { max = &hd->homunculusDB->gmax; min = &hd->homunculusDB->gmin; - growth_max_hp = rnd_value(min->HP, max->HP); - growth_max_sp = rnd_value(min->SP, max->SP); - growth_str = rnd_value(min->str, max->str); - growth_agi = rnd_value(min->agi, max->agi); - growth_vit = rnd_value(min->vit, max->vit); - growth_dex = rnd_value(min->dex, max->dex); - growth_int = rnd_value(min->int_,max->int_); - growth_luk = rnd_value(min->luk, max->luk); + growth_max_hp = rnd->value(min->HP, max->HP); + growth_max_sp = rnd->value(min->SP, max->SP); + growth_str = rnd->value(min->str, max->str); + growth_agi = rnd->value(min->agi, max->agi); + growth_vit = rnd->value(min->vit, max->vit); + growth_dex = rnd->value(min->dex, max->dex); + growth_int = rnd->value(min->int_,max->int_); + growth_luk = rnd->value(min->luk, max->luk); //Aegis discards the decimals in the stat growth values! growth_str-=growth_str%10; @@ -432,14 +432,14 @@ bool homunculus_evolve(struct homun_data *hd) { hom = &hd->homunculus; max = &hd->homunculusDB->emax; min = &hd->homunculusDB->emin; - hom->max_hp += rnd_value(min->HP, max->HP); - hom->max_sp += rnd_value(min->SP, max->SP); - hom->str += 10*rnd_value(min->str, max->str); - hom->agi += 10*rnd_value(min->agi, max->agi); - hom->vit += 10*rnd_value(min->vit, max->vit); - hom->int_+= 10*rnd_value(min->int_,max->int_); - hom->dex += 10*rnd_value(min->dex, max->dex); - hom->luk += 10*rnd_value(min->luk, max->luk); + hom->max_hp += rnd->value(min->HP, max->HP); + hom->max_sp += rnd->value(min->SP, max->SP); + hom->str += 10*rnd->value(min->str, max->str); + hom->agi += 10*rnd->value(min->agi, max->agi); + hom->vit += 10*rnd->value(min->vit, max->vit); + hom->int_+= 10*rnd->value(min->int_,max->int_); + hom->dex += 10*rnd->value(min->dex, max->dex); + hom->luk += 10*rnd->value(min->luk, max->luk); hom->intimacy = 500; unit->remove_map(&hd->bl, CLR_OUTSIGHT, ALC_MARK); @@ -848,7 +848,7 @@ bool homunculus_call(struct map_session_data *sd) { nullpo_retr(false, sd); if (!sd->status.hom_id) //Create a new homun. - return homun->creation_request(sd, HM_CLASS_BASE + rnd_value(0, 7)); + return homun->creation_request(sd, HM_CLASS_BASE + rnd->value(0, 7)); // If homunc not yet loaded, load it if (!sd->hd) @@ -1075,14 +1075,14 @@ bool homunculus_shuffle(struct homun_data *hd) { //Evolved bonuses struct s_homunculus *hom = &hd->homunculus; struct h_stats *max = &hd->homunculusDB->emax, *min = &hd->homunculusDB->emin; - hom->max_hp += rnd_value(min->HP, max->HP); - hom->max_sp += rnd_value(min->SP, max->SP); - hom->str += 10*rnd_value(min->str, max->str); - hom->agi += 10*rnd_value(min->agi, max->agi); - hom->vit += 10*rnd_value(min->vit, max->vit); - hom->int_+= 10*rnd_value(min->int_,max->int_); - hom->dex += 10*rnd_value(min->dex, max->dex); - hom->luk += 10*rnd_value(min->luk, max->luk); + hom->max_hp += rnd->value(min->HP, max->HP); + hom->max_sp += rnd->value(min->SP, max->SP); + hom->str += 10*rnd->value(min->str, max->str); + hom->agi += 10*rnd->value(min->agi, max->agi); + hom->vit += 10*rnd->value(min->vit, max->vit); + hom->int_+= 10*rnd->value(min->int_,max->int_); + hom->dex += 10*rnd->value(min->dex, max->dex); + hom->luk += 10*rnd->value(min->luk, max->luk); } hd->homunculus.exp = exp; diff --git a/src/map/skill.c b/src/map/skill.c index 76ee3d3fd..0b66c1ded 100644 --- a/src/map/skill.c +++ b/src/map/skill.c @@ -8827,9 +8827,9 @@ int skill_castend_nodamage_id(struct block_list *src, struct block_list *bl, uin if (is_boss(bl)) break; joblvbonus = ( sd ? sd->status.job_level : 50 ); //First we set the success chance based on the caster's build which increases the chance. - rate = 10 * skill_lv + rnd_value( sstatus->dex / 12, sstatus->dex / 4 ) + joblvbonus + status->get_lv(src) / 10; + rate = 10 * skill_lv + rnd->value( sstatus->dex / 12, sstatus->dex / 4 ) + joblvbonus + status->get_lv(src) / 10; // We then reduce the success chance based on the target's build. - rate -= rnd_value( tstatus->agi / 6, tstatus->agi / 3 ) + tstatus->luk / 10 + ( dstsd ? (dstsd->max_weight / 10 - dstsd->weight / 10 ) / 100 : 0 ) + status->get_lv(bl) / 10; + rate -= rnd->value( tstatus->agi / 6, tstatus->agi / 3 ) + tstatus->luk / 10 + ( dstsd ? (dstsd->max_weight / 10 - dstsd->weight / 10 ) / 100 : 0 ) + status->get_lv(bl) / 10; //Finally we set the minimum success chance cap based on the caster's skill level and DEX. rate = cap_value( rate, skill_lv + sstatus->dex / 20, 100); clif->skill_nodamage(src,bl,skill_id,0,sc_start(src,bl,type,rate,skill_lv,skill->get_time(skill_id,skill_lv))); diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index 0b1b67e61..1c0cec51d 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -114,6 +114,7 @@ #include "common/memmgr.h" #include "common/mutex.h" #include "common/nullpo.h" +#include "common/random.h" #include "common/showmsg.h" #include "common/socket.h" #include "common/sql.h" -- cgit v1.2.3-70-g09d2 From 7214eb1ef48352911a84ada089bbebdb0d5678f2 Mon Sep 17 00:00:00 2001 From: Haru Date: Sat, 16 Apr 2016 23:51:57 +0200 Subject: Changed the HPMHookGen script to correctly handle macros associated with an interface Signed-off-by: Haru --- tools/HPMHookGen/HPMHookGen.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/HPMHookGen/HPMHookGen.pl b/tools/HPMHookGen/HPMHookGen.pl index 1d6a3e122..75dd51736 100755 --- a/tools/HPMHookGen/HPMHookGen.pl +++ b/tools/HPMHookGen/HPMHookGen.pl @@ -359,6 +359,7 @@ foreach my $file (@files) { # Loop through the xml files my $bstart = $b->{location}->[0]->{bodystart} || $b->{location}->[0]->{line}; $astart <=> $bstart } @$memberdef) { # Loop through the members + next unless $f->{kind} eq 'variable'; # Skip macros my $t = $f->{argsstring}->[0]; my $def = $f->{definition}->[0]; if ($f->{type}->[0] =~ /^\s*LoginParseFunc\s*\*\s*$/) { -- cgit v1.2.3-70-g09d2 From bdfa016ce40fb7382206775d656ba5f751ac3b49 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 21:12:42 +0100 Subject: Various changes to the random interface Mostly stylistic changes. Cleaned up documentation. Signed-off-by: Haru --- src/common/random.c | 34 ++++++++++++--------------- src/common/random.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/common/random.c b/src/common/random.c index dbb2ca830..b3a13e054 100644 --- a/src/common/random.c +++ b/src/common/random.c @@ -36,10 +36,14 @@ # include #endif +/** @file + * Implementation of the random number generator interface. + */ + struct rnd_interface rnd_s; struct rnd_interface *rnd; -/// Initializes the random number generator with an appropriate seed. +/// @copydoc rnd_interface::init() void rnd_init(void) { unsigned long seed = (unsigned long)timer->gettick(); @@ -58,61 +62,53 @@ void rnd_init(void) init_genrand(seed); // Also initialize the stdlib rng, just in case it's used somewhere. - srand((unsigned int)timer->gettick()); + srand((unsigned int)seed); } +/// @copydoc rnd_interface::final() void rnd_final(void) { } -/// Initializes the random number generator. +/// @copydoc rnd_interface::seed() void rnd_seed(uint32 seed) { init_genrand(seed); } - -/// Generates a random number in the interval [0, SINT32_MAX] +/// @copydoc rnd_interface::random() int32 rnd_random(void) { return (int32)genrand_int31(); } - -/// Generates a random number in the interval [0, dice_faces) -/// NOTE: interval is open ended, so dice_faces is excluded (unless it's 0) +/// @copydoc rnd_interface::roll() uint32 rnd_roll(uint32 dice_faces) { return (uint32)(rnd->uniform()*dice_faces); } - -/// Generates a random number in the interval [min, max] -/// Returns min if range is invalid. +/// @copydoc rnd_interface::value() int32 rnd_value(int32 min, int32 max) { - if( min >= max ) + if (min >= max) return min; return min + (int32)(rnd->uniform()*(max-min+1)); } - -/// Generates a random number in the interval [0.0, 1.0) -/// NOTE: interval is open ended, so 1.0 is excluded +/// @copydoc rnd_interface::uniform() double rnd_uniform(void) { return ((uint32)genrand_int32())*(1.0/4294967296.0);// divided by 2^32 } - -/// Generates a random number in the interval [0.0, 1.0) with 53-bit resolution -/// NOTE: interval is open ended, so 1.0 is excluded -/// NOTE: 53 bits is the maximum precision of a double +/// @copydoc rnd_interface::uniform53() double rnd_uniform53(void) { return genrand_res53(); } +/// Interface base initialization. void rnd_defaults(void) { rnd = &rnd_s; diff --git a/src/common/random.h b/src/common/random.h index 41bdb4083..1b249ef19 100644 --- a/src/common/random.h +++ b/src/common/random.h @@ -23,25 +23,79 @@ #include "common/hercules.h" +/** @file + * The random number generator interface. + */ + +/// Random interface. struct rnd_interface { + /** + * Interface initialization. + * + * During initialization, the RNG is seeded with a random seed. + */ void (*init) (void); + + /// Interface finalization. void (*final) (void); - void (*seed) (uint32); + /** + * Re-seeds the random number generator. + * + * @param seed The new seed. + */ + void (*seed) (uint32 seed); + + /** + * Generates a random number in the interval [0, SINT32_MAX]. + */ + int32 (*random) (void); + + /** + * Generates a random number in the interval [0, dice_faces). + * + * @remark + * interval is open ended, so dice_faces is excluded (unless it's 0) + */ + uint32 (*roll) (uint32 dice_faces); - int32 (*random) (void);// [0, SINT32_MAX] - uint32 (*roll) (uint32 dice_faces);// [0, dice_faces) - int32 (*value) (int32 min, int32 max);// [min, max] - double (*uniform) (void);// [0.0, 1.0) - double (*uniform53) (void);// [0.0, 1.0) + /** + * Generates a random number in the interval [min, max]. + * + * @retval min if range is invalid. + */ + int32 (*value) (int32 min, int32 max); + + /** + * Generates a random number in the interval [0.0, 1.0) + * + * @remark + * interval is open ended, so 1.0 is excluded + */ + double (*uniform) (void); + + /** + * Generates a random number in the interval [0.0, 1.0) with 53-bit resolution. + * + * 53 bits is the maximum precision of a double. + * + * @remark + * interval is open ended, so 1.0 is excluded + */ + double (*uniform53) (void); }; +/** + * Utlity macro to call the frequently used rnd_interface#random(). + * + * @related rnd_interface. + */ #define rnd() rnd->random() #ifdef HERCULES_CORE void rnd_defaults(void); #endif // HERCULES_CORE -HPShared struct rnd_interface *rnd; +HPShared struct rnd_interface *rnd; ///< Pointer to the random interface. #endif /* COMMON_RANDOM_H */ -- cgit v1.2.3-70-g09d2 From d0355d1df812f75e22f2d0538a1850d4e1274078 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 21:13:32 +0100 Subject: HPM Hooks Update Signed-off-by: Haru --- src/common/HPMDataCheck.h | 5 + src/common/HPMSymbols.inc.h | 6 + src/plugins/HPMHooking/HPMHooking.Defs.inc | 18 ++ .../HPMHooking/HPMHooking_char.HPMHooksCore.inc | 33 ++++ .../HPMHooking/HPMHooking_char.HookingPoints.inc | 9 + src/plugins/HPMHooking/HPMHooking_char.Hooks.inc | 214 +++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_char.sources.inc | 1 + .../HPMHooking/HPMHooking_login.HPMHooksCore.inc | 33 ++++ .../HPMHooking/HPMHooking_login.HookingPoints.inc | 9 + src/plugins/HPMHooking/HPMHooking_login.Hooks.inc | 214 +++++++++++++++++++++ .../HPMHooking/HPMHooking_login.sources.inc | 1 + .../HPMHooking/HPMHooking_map.HPMHooksCore.inc | 33 ++++ .../HPMHooking/HPMHooking_map.HookingPoints.inc | 9 + src/plugins/HPMHooking/HPMHooking_map.Hooks.inc | 214 +++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_map.sources.inc | 1 + 15 files changed, 800 insertions(+) diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h index 426c94ba2..8045b745f 100644 --- a/src/common/HPMDataCheck.h +++ b/src/common/HPMDataCheck.h @@ -226,6 +226,11 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = { #else #define COMMON_NULLPO_H #endif // COMMON_NULLPO_H + #ifdef COMMON_RANDOM_H + { "rnd_interface", sizeof(struct rnd_interface), SERVER_TYPE_ALL }, + #else + #define COMMON_RANDOM_H + #endif // COMMON_RANDOM_H #ifdef COMMON_SHOWMSG_H { "showmsg_interface", sizeof(struct showmsg_interface), SERVER_TYPE_ALL }, #else diff --git a/src/common/HPMSymbols.inc.h b/src/common/HPMSymbols.inc.h index 24aefe06a..379c04b14 100644 --- a/src/common/HPMSymbols.inc.h +++ b/src/common/HPMSymbols.inc.h @@ -215,6 +215,9 @@ struct pincode_interface *pincode; #ifdef MAP_QUEST_H /* quest */ struct quest_interface *quest; #endif // MAP_QUEST_H +#ifdef COMMON_RANDOM_H /* rnd */ +struct rnd_interface *rnd; +#endif // COMMON_RANDOM_H #ifdef MAP_SCRIPT_H /* script */ struct script_interface *script; #endif // MAP_SCRIPT_H @@ -456,6 +459,9 @@ if ((server_type&(SERVER_TYPE_CHAR)) && !HPM_SYMBOL("pincode", pincode)) return #ifdef MAP_QUEST_H /* quest */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("quest", quest)) return "quest"; #endif // MAP_QUEST_H +#ifdef COMMON_RANDOM_H /* rnd */ +if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("rnd", rnd)) return "rnd"; +#endif // COMMON_RANDOM_H #ifdef MAP_SCRIPT_H /* script */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("script", script)) return "script"; #endif // MAP_SCRIPT_H diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index 852a2c122..cbf15fa7e 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -5699,6 +5699,24 @@ typedef int (*HPMHOOK_post_quest_read_db) (int retVal___); typedef struct quest_db* (*HPMHOOK_pre_quest_read_db_sub) (struct config_setting_t **cs, int *n, const char **source); typedef struct quest_db* (*HPMHOOK_post_quest_read_db_sub) (struct quest_db* retVal___, struct config_setting_t *cs, int n, const char *source); #endif // MAP_QUEST_H +#ifdef COMMON_RANDOM_H /* rnd */ +typedef void (*HPMHOOK_pre_rnd_init) (void); +typedef void (*HPMHOOK_post_rnd_init) (void); +typedef void (*HPMHOOK_pre_rnd_final) (void); +typedef void (*HPMHOOK_post_rnd_final) (void); +typedef void (*HPMHOOK_pre_rnd_seed) (uint32 *seed); +typedef void (*HPMHOOK_post_rnd_seed) (uint32 seed); +typedef int32 (*HPMHOOK_pre_rnd_random) (void); +typedef int32 (*HPMHOOK_post_rnd_random) (int32 retVal___); +typedef uint32 (*HPMHOOK_pre_rnd_roll) (uint32 *dice_faces); +typedef uint32 (*HPMHOOK_post_rnd_roll) (uint32 retVal___, uint32 dice_faces); +typedef int32 (*HPMHOOK_pre_rnd_value) (int32 *min, int32 *max); +typedef int32 (*HPMHOOK_post_rnd_value) (int32 retVal___, int32 min, int32 max); +typedef double (*HPMHOOK_pre_rnd_uniform) (void); +typedef double (*HPMHOOK_post_rnd_uniform) (double retVal___); +typedef double (*HPMHOOK_pre_rnd_uniform53) (void); +typedef double (*HPMHOOK_post_rnd_uniform53) (double retVal___); +#endif // COMMON_RANDOM_H #ifdef MAP_SCRIPT_H /* script */ typedef void (*HPMHOOK_pre_script_init) (bool *minimal); typedef void (*HPMHOOK_post_script_init) (bool minimal); diff --git a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc index 78066d26e..2162fad57 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc @@ -1204,6 +1204,22 @@ struct { struct HPMHookPoint *HP_pincode_check_post; struct HPMHookPoint *HP_pincode_config_read_pre; struct HPMHookPoint *HP_pincode_config_read_post; + struct HPMHookPoint *HP_rnd_init_pre; + struct HPMHookPoint *HP_rnd_init_post; + struct HPMHookPoint *HP_rnd_final_pre; + struct HPMHookPoint *HP_rnd_final_post; + struct HPMHookPoint *HP_rnd_seed_pre; + struct HPMHookPoint *HP_rnd_seed_post; + struct HPMHookPoint *HP_rnd_random_pre; + struct HPMHookPoint *HP_rnd_random_post; + struct HPMHookPoint *HP_rnd_roll_pre; + struct HPMHookPoint *HP_rnd_roll_post; + struct HPMHookPoint *HP_rnd_value_pre; + struct HPMHookPoint *HP_rnd_value_post; + struct HPMHookPoint *HP_rnd_uniform_pre; + struct HPMHookPoint *HP_rnd_uniform_post; + struct HPMHookPoint *HP_rnd_uniform53_pre; + struct HPMHookPoint *HP_rnd_uniform53_post; struct HPMHookPoint *HP_showmsg_init_pre; struct HPMHookPoint *HP_showmsg_init_post; struct HPMHookPoint *HP_showmsg_final_pre; @@ -2639,6 +2655,22 @@ struct { int HP_pincode_check_post; int HP_pincode_config_read_pre; int HP_pincode_config_read_post; + int HP_rnd_init_pre; + int HP_rnd_init_post; + int HP_rnd_final_pre; + int HP_rnd_final_post; + int HP_rnd_seed_pre; + int HP_rnd_seed_post; + int HP_rnd_random_pre; + int HP_rnd_random_post; + int HP_rnd_roll_pre; + int HP_rnd_roll_post; + int HP_rnd_value_pre; + int HP_rnd_value_post; + int HP_rnd_uniform_pre; + int HP_rnd_uniform_post; + int HP_rnd_uniform53_pre; + int HP_rnd_uniform53_post; int HP_showmsg_init_pre; int HP_showmsg_init_post; int HP_showmsg_final_pre; @@ -2924,6 +2956,7 @@ struct { struct mutex_interface mutex; struct nullpo_interface nullpo; struct pincode_interface pincode; + struct rnd_interface rnd; struct showmsg_interface showmsg; struct socket_interface sockt; struct sql_interface SQL; diff --git a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc index 405dd4a91..d9021e906 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc @@ -643,6 +643,15 @@ struct HookingPointData HookingPoints[] = { { HP_POP(pincode->compare, HP_pincode_compare) }, { HP_POP(pincode->check, HP_pincode_check) }, { HP_POP(pincode->config_read, HP_pincode_config_read) }, +/* rnd_interface */ + { HP_POP(rnd->init, HP_rnd_init) }, + { HP_POP(rnd->final, HP_rnd_final) }, + { HP_POP(rnd->seed, HP_rnd_seed) }, + { HP_POP(rnd->random, HP_rnd_random) }, + { HP_POP(rnd->roll, HP_rnd_roll) }, + { HP_POP(rnd->value, HP_rnd_value) }, + { HP_POP(rnd->uniform, HP_rnd_uniform) }, + { HP_POP(rnd->uniform53, HP_rnd_uniform53) }, /* showmsg_interface */ { HP_POP(showmsg->init, HP_showmsg_init) }, { HP_POP(showmsg->final, HP_showmsg_final) }, diff --git a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc index 4ec1abcd1..958724065 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc @@ -15753,6 +15753,220 @@ bool HP_pincode_config_read(char *w1, char *w2) { } return retVal___; } +/* rnd_interface */ +void HP_rnd_init(void) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_init_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_init_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_init_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.init(); + } + if( HPMHooks.count.HP_rnd_init_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_init_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_init_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_rnd_final(void) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_final_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_final_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_final_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.final(); + } + if( HPMHooks.count.HP_rnd_final_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_final_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_final_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_rnd_seed(uint32 seed) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_seed_pre ) { + void (*preHookFunc) (uint32 *seed); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_seed_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_seed_pre[hIndex].func; + preHookFunc(&seed); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.seed(seed); + } + if( HPMHooks.count.HP_rnd_seed_post ) { + void (*postHookFunc) (uint32 seed); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_seed_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_seed_post[hIndex].func; + postHookFunc(seed); + } + } + return; +} +int32 HP_rnd_random(void) { + int hIndex = 0; + int32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_random_pre ) { + int32 (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_random_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_random_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.random(); + } + if( HPMHooks.count.HP_rnd_random_post ) { + int32 (*postHookFunc) (int32 retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_random_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_random_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +uint32 HP_rnd_roll(uint32 dice_faces) { + int hIndex = 0; + uint32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_roll_pre ) { + uint32 (*preHookFunc) (uint32 *dice_faces); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_roll_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_roll_pre[hIndex].func; + retVal___ = preHookFunc(&dice_faces); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.roll(dice_faces); + } + if( HPMHooks.count.HP_rnd_roll_post ) { + uint32 (*postHookFunc) (uint32 retVal___, uint32 dice_faces); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_roll_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_roll_post[hIndex].func; + retVal___ = postHookFunc(retVal___, dice_faces); + } + } + return retVal___; +} +int32 HP_rnd_value(int32 min, int32 max) { + int hIndex = 0; + int32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_value_pre ) { + int32 (*preHookFunc) (int32 *min, int32 *max); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_value_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_value_pre[hIndex].func; + retVal___ = preHookFunc(&min, &max); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.value(min, max); + } + if( HPMHooks.count.HP_rnd_value_post ) { + int32 (*postHookFunc) (int32 retVal___, int32 min, int32 max); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_value_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_value_post[hIndex].func; + retVal___ = postHookFunc(retVal___, min, max); + } + } + return retVal___; +} +double HP_rnd_uniform(void) { + int hIndex = 0; + double retVal___ = 0.; + if( HPMHooks.count.HP_rnd_uniform_pre ) { + double (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_uniform_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.uniform(); + } + if( HPMHooks.count.HP_rnd_uniform_post ) { + double (*postHookFunc) (double retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_uniform_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +double HP_rnd_uniform53(void) { + int hIndex = 0; + double retVal___ = 0.; + if( HPMHooks.count.HP_rnd_uniform53_pre ) { + double (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform53_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_uniform53_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.uniform53(); + } + if( HPMHooks.count.HP_rnd_uniform53_post ) { + double (*postHookFunc) (double retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform53_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_uniform53_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} /* showmsg_interface */ void HP_showmsg_init(void) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_char.sources.inc b/src/plugins/HPMHooking/HPMHooking_char.sources.inc index c17c0313d..f1180dea8 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.sources.inc @@ -53,6 +53,7 @@ memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); memcpy(&HPMHooks.source.mutex, mutex, sizeof(struct mutex_interface)); memcpy(&HPMHooks.source.nullpo, nullpo, sizeof(struct nullpo_interface)); memcpy(&HPMHooks.source.pincode, pincode, sizeof(struct pincode_interface)); +memcpy(&HPMHooks.source.rnd, rnd, sizeof(struct rnd_interface)); memcpy(&HPMHooks.source.showmsg, showmsg, sizeof(struct showmsg_interface)); memcpy(&HPMHooks.source.sockt, sockt, sizeof(struct socket_interface)); memcpy(&HPMHooks.source.SQL, SQL, sizeof(struct sql_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc index e6d43c833..91d2304d8 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc @@ -432,6 +432,22 @@ struct { struct HPMHookPoint *HP_mutex_cond_broadcast_post; struct HPMHookPoint *HP_nullpo_assert_report_pre; struct HPMHookPoint *HP_nullpo_assert_report_post; + struct HPMHookPoint *HP_rnd_init_pre; + struct HPMHookPoint *HP_rnd_init_post; + struct HPMHookPoint *HP_rnd_final_pre; + struct HPMHookPoint *HP_rnd_final_post; + struct HPMHookPoint *HP_rnd_seed_pre; + struct HPMHookPoint *HP_rnd_seed_post; + struct HPMHookPoint *HP_rnd_random_pre; + struct HPMHookPoint *HP_rnd_random_post; + struct HPMHookPoint *HP_rnd_roll_pre; + struct HPMHookPoint *HP_rnd_roll_post; + struct HPMHookPoint *HP_rnd_value_pre; + struct HPMHookPoint *HP_rnd_value_post; + struct HPMHookPoint *HP_rnd_uniform_pre; + struct HPMHookPoint *HP_rnd_uniform_post; + struct HPMHookPoint *HP_rnd_uniform53_pre; + struct HPMHookPoint *HP_rnd_uniform53_post; struct HPMHookPoint *HP_showmsg_init_pre; struct HPMHookPoint *HP_showmsg_init_post; struct HPMHookPoint *HP_showmsg_final_pre; @@ -1095,6 +1111,22 @@ struct { int HP_mutex_cond_broadcast_post; int HP_nullpo_assert_report_pre; int HP_nullpo_assert_report_post; + int HP_rnd_init_pre; + int HP_rnd_init_post; + int HP_rnd_final_pre; + int HP_rnd_final_post; + int HP_rnd_seed_pre; + int HP_rnd_seed_post; + int HP_rnd_random_pre; + int HP_rnd_random_post; + int HP_rnd_roll_pre; + int HP_rnd_roll_post; + int HP_rnd_value_pre; + int HP_rnd_value_post; + int HP_rnd_uniform_pre; + int HP_rnd_uniform_post; + int HP_rnd_uniform53_pre; + int HP_rnd_uniform53_post; int HP_showmsg_init_pre; int HP_showmsg_init_post; int HP_showmsg_final_pre; @@ -1366,6 +1398,7 @@ struct { struct md5_interface md5; struct mutex_interface mutex; struct nullpo_interface nullpo; + struct rnd_interface rnd; struct showmsg_interface showmsg; struct socket_interface sockt; struct sql_interface SQL; diff --git a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc index ce9a112f7..48e376287 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc @@ -243,6 +243,15 @@ struct HookingPointData HookingPoints[] = { { HP_POP(mutex->cond_broadcast, HP_mutex_cond_broadcast) }, /* nullpo_interface */ { HP_POP(nullpo->assert_report, HP_nullpo_assert_report) }, +/* rnd_interface */ + { HP_POP(rnd->init, HP_rnd_init) }, + { HP_POP(rnd->final, HP_rnd_final) }, + { HP_POP(rnd->seed, HP_rnd_seed) }, + { HP_POP(rnd->random, HP_rnd_random) }, + { HP_POP(rnd->roll, HP_rnd_roll) }, + { HP_POP(rnd->value, HP_rnd_value) }, + { HP_POP(rnd->uniform, HP_rnd_uniform) }, + { HP_POP(rnd->uniform53, HP_rnd_uniform53) }, /* showmsg_interface */ { HP_POP(showmsg->init, HP_showmsg_init) }, { HP_POP(showmsg->final, HP_showmsg_final) }, diff --git a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc index dc0ed4a17..e176c8488 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc @@ -5459,6 +5459,220 @@ void HP_nullpo_assert_report(const char *file, int line, const char *func, const } return; } +/* rnd_interface */ +void HP_rnd_init(void) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_init_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_init_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_init_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.init(); + } + if( HPMHooks.count.HP_rnd_init_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_init_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_init_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_rnd_final(void) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_final_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_final_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_final_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.final(); + } + if( HPMHooks.count.HP_rnd_final_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_final_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_final_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_rnd_seed(uint32 seed) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_seed_pre ) { + void (*preHookFunc) (uint32 *seed); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_seed_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_seed_pre[hIndex].func; + preHookFunc(&seed); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.seed(seed); + } + if( HPMHooks.count.HP_rnd_seed_post ) { + void (*postHookFunc) (uint32 seed); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_seed_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_seed_post[hIndex].func; + postHookFunc(seed); + } + } + return; +} +int32 HP_rnd_random(void) { + int hIndex = 0; + int32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_random_pre ) { + int32 (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_random_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_random_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.random(); + } + if( HPMHooks.count.HP_rnd_random_post ) { + int32 (*postHookFunc) (int32 retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_random_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_random_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +uint32 HP_rnd_roll(uint32 dice_faces) { + int hIndex = 0; + uint32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_roll_pre ) { + uint32 (*preHookFunc) (uint32 *dice_faces); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_roll_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_roll_pre[hIndex].func; + retVal___ = preHookFunc(&dice_faces); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.roll(dice_faces); + } + if( HPMHooks.count.HP_rnd_roll_post ) { + uint32 (*postHookFunc) (uint32 retVal___, uint32 dice_faces); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_roll_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_roll_post[hIndex].func; + retVal___ = postHookFunc(retVal___, dice_faces); + } + } + return retVal___; +} +int32 HP_rnd_value(int32 min, int32 max) { + int hIndex = 0; + int32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_value_pre ) { + int32 (*preHookFunc) (int32 *min, int32 *max); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_value_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_value_pre[hIndex].func; + retVal___ = preHookFunc(&min, &max); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.value(min, max); + } + if( HPMHooks.count.HP_rnd_value_post ) { + int32 (*postHookFunc) (int32 retVal___, int32 min, int32 max); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_value_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_value_post[hIndex].func; + retVal___ = postHookFunc(retVal___, min, max); + } + } + return retVal___; +} +double HP_rnd_uniform(void) { + int hIndex = 0; + double retVal___ = 0.; + if( HPMHooks.count.HP_rnd_uniform_pre ) { + double (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_uniform_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.uniform(); + } + if( HPMHooks.count.HP_rnd_uniform_post ) { + double (*postHookFunc) (double retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_uniform_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +double HP_rnd_uniform53(void) { + int hIndex = 0; + double retVal___ = 0.; + if( HPMHooks.count.HP_rnd_uniform53_pre ) { + double (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform53_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_uniform53_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.uniform53(); + } + if( HPMHooks.count.HP_rnd_uniform53_post ) { + double (*postHookFunc) (double retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform53_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_uniform53_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} /* showmsg_interface */ void HP_showmsg_init(void) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_login.sources.inc b/src/plugins/HPMHooking/HPMHooking_login.sources.inc index fc530bcbf..ddd1206ca 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.sources.inc @@ -39,6 +39,7 @@ memcpy(&HPMHooks.source.iMalloc, iMalloc, sizeof(struct malloc_interface)); memcpy(&HPMHooks.source.md5, md5, sizeof(struct md5_interface)); memcpy(&HPMHooks.source.mutex, mutex, sizeof(struct mutex_interface)); memcpy(&HPMHooks.source.nullpo, nullpo, sizeof(struct nullpo_interface)); +memcpy(&HPMHooks.source.rnd, rnd, sizeof(struct rnd_interface)); memcpy(&HPMHooks.source.showmsg, showmsg, sizeof(struct showmsg_interface)); memcpy(&HPMHooks.source.sockt, sockt, sizeof(struct socket_interface)); memcpy(&HPMHooks.source.SQL, SQL, sizeof(struct sql_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index d85e64eb2..e3276fdf6 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -4530,6 +4530,22 @@ struct { struct HPMHookPoint *HP_quest_read_db_post; struct HPMHookPoint *HP_quest_read_db_sub_pre; struct HPMHookPoint *HP_quest_read_db_sub_post; + struct HPMHookPoint *HP_rnd_init_pre; + struct HPMHookPoint *HP_rnd_init_post; + struct HPMHookPoint *HP_rnd_final_pre; + struct HPMHookPoint *HP_rnd_final_post; + struct HPMHookPoint *HP_rnd_seed_pre; + struct HPMHookPoint *HP_rnd_seed_post; + struct HPMHookPoint *HP_rnd_random_pre; + struct HPMHookPoint *HP_rnd_random_post; + struct HPMHookPoint *HP_rnd_roll_pre; + struct HPMHookPoint *HP_rnd_roll_post; + struct HPMHookPoint *HP_rnd_value_pre; + struct HPMHookPoint *HP_rnd_value_post; + struct HPMHookPoint *HP_rnd_uniform_pre; + struct HPMHookPoint *HP_rnd_uniform_post; + struct HPMHookPoint *HP_rnd_uniform53_pre; + struct HPMHookPoint *HP_rnd_uniform53_post; struct HPMHookPoint *HP_script_init_pre; struct HPMHookPoint *HP_script_init_post; struct HPMHookPoint *HP_script_final_pre; @@ -10517,6 +10533,22 @@ struct { int HP_quest_read_db_post; int HP_quest_read_db_sub_pre; int HP_quest_read_db_sub_post; + int HP_rnd_init_pre; + int HP_rnd_init_post; + int HP_rnd_final_pre; + int HP_rnd_final_post; + int HP_rnd_seed_pre; + int HP_rnd_seed_post; + int HP_rnd_random_pre; + int HP_rnd_random_post; + int HP_rnd_roll_pre; + int HP_rnd_roll_post; + int HP_rnd_value_pre; + int HP_rnd_value_post; + int HP_rnd_uniform_pre; + int HP_rnd_uniform_post; + int HP_rnd_uniform53_pre; + int HP_rnd_uniform53_post; int HP_script_init_pre; int HP_script_init_post; int HP_script_final_pre; @@ -12045,6 +12077,7 @@ struct { struct pcre_interface libpcre; struct pet_interface pet; struct quest_interface quest; + struct rnd_interface rnd; struct script_interface script; struct searchstore_interface searchstore; struct showmsg_interface showmsg; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index 59949c536..4f103df81 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -2323,6 +2323,15 @@ struct HookingPointData HookingPoints[] = { { HP_POP(quest->clear, HP_quest_clear) }, { HP_POP(quest->read_db, HP_quest_read_db) }, { HP_POP(quest->read_db_sub, HP_quest_read_db_sub) }, +/* rnd_interface */ + { HP_POP(rnd->init, HP_rnd_init) }, + { HP_POP(rnd->final, HP_rnd_final) }, + { HP_POP(rnd->seed, HP_rnd_seed) }, + { HP_POP(rnd->random, HP_rnd_random) }, + { HP_POP(rnd->roll, HP_rnd_roll) }, + { HP_POP(rnd->value, HP_rnd_value) }, + { HP_POP(rnd->uniform, HP_rnd_uniform) }, + { HP_POP(rnd->uniform53, HP_rnd_uniform53) }, /* script_interface */ { HP_POP(script->init, HP_script_init) }, { HP_POP(script->final, HP_script_final) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index f46a5e7d0..949928058 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -60384,6 +60384,220 @@ struct quest_db* HP_quest_read_db_sub(struct config_setting_t *cs, int n, const } return retVal___; } +/* rnd_interface */ +void HP_rnd_init(void) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_init_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_init_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_init_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.init(); + } + if( HPMHooks.count.HP_rnd_init_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_init_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_init_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_rnd_final(void) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_final_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_final_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_final_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.final(); + } + if( HPMHooks.count.HP_rnd_final_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_final_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_final_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_rnd_seed(uint32 seed) { + int hIndex = 0; + if( HPMHooks.count.HP_rnd_seed_pre ) { + void (*preHookFunc) (uint32 *seed); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_seed_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_seed_pre[hIndex].func; + preHookFunc(&seed); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.rnd.seed(seed); + } + if( HPMHooks.count.HP_rnd_seed_post ) { + void (*postHookFunc) (uint32 seed); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_seed_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_seed_post[hIndex].func; + postHookFunc(seed); + } + } + return; +} +int32 HP_rnd_random(void) { + int hIndex = 0; + int32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_random_pre ) { + int32 (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_random_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_random_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.random(); + } + if( HPMHooks.count.HP_rnd_random_post ) { + int32 (*postHookFunc) (int32 retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_random_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_random_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +uint32 HP_rnd_roll(uint32 dice_faces) { + int hIndex = 0; + uint32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_roll_pre ) { + uint32 (*preHookFunc) (uint32 *dice_faces); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_roll_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_roll_pre[hIndex].func; + retVal___ = preHookFunc(&dice_faces); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.roll(dice_faces); + } + if( HPMHooks.count.HP_rnd_roll_post ) { + uint32 (*postHookFunc) (uint32 retVal___, uint32 dice_faces); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_roll_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_roll_post[hIndex].func; + retVal___ = postHookFunc(retVal___, dice_faces); + } + } + return retVal___; +} +int32 HP_rnd_value(int32 min, int32 max) { + int hIndex = 0; + int32 retVal___ = 0; + if( HPMHooks.count.HP_rnd_value_pre ) { + int32 (*preHookFunc) (int32 *min, int32 *max); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_value_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_value_pre[hIndex].func; + retVal___ = preHookFunc(&min, &max); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.value(min, max); + } + if( HPMHooks.count.HP_rnd_value_post ) { + int32 (*postHookFunc) (int32 retVal___, int32 min, int32 max); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_value_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_value_post[hIndex].func; + retVal___ = postHookFunc(retVal___, min, max); + } + } + return retVal___; +} +double HP_rnd_uniform(void) { + int hIndex = 0; + double retVal___ = 0.; + if( HPMHooks.count.HP_rnd_uniform_pre ) { + double (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_uniform_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.uniform(); + } + if( HPMHooks.count.HP_rnd_uniform_post ) { + double (*postHookFunc) (double retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_uniform_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +double HP_rnd_uniform53(void) { + int hIndex = 0; + double retVal___ = 0.; + if( HPMHooks.count.HP_rnd_uniform53_pre ) { + double (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform53_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_rnd_uniform53_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.rnd.uniform53(); + } + if( HPMHooks.count.HP_rnd_uniform53_post ) { + double (*postHookFunc) (double retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_rnd_uniform53_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_rnd_uniform53_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} /* script_interface */ void HP_script_init(bool minimal) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_map.sources.inc b/src/plugins/HPMHooking/HPMHooking_map.sources.inc index a835e0a03..87cf0c3ed 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.sources.inc @@ -70,6 +70,7 @@ memcpy(&HPMHooks.source.pc, pc, sizeof(struct pc_interface)); memcpy(&HPMHooks.source.libpcre, libpcre, sizeof(struct pcre_interface)); memcpy(&HPMHooks.source.pet, pet, sizeof(struct pet_interface)); memcpy(&HPMHooks.source.quest, quest, sizeof(struct quest_interface)); +memcpy(&HPMHooks.source.rnd, rnd, sizeof(struct rnd_interface)); memcpy(&HPMHooks.source.script, script, sizeof(struct script_interface)); memcpy(&HPMHooks.source.searchstore, searchstore, sizeof(struct searchstore_interface)); memcpy(&HPMHooks.source.showmsg, showmsg, sizeof(struct showmsg_interface)); -- cgit v1.2.3-70-g09d2 From 257b9e11ab6cf503d0b9582eb855ea0b50ec8877 Mon Sep 17 00:00:00 2001 From: hemagx Date: Tue, 1 Mar 2016 07:48:59 +0200 Subject: Interface thread.c --- src/common/console.c | 4 +- src/common/core.c | 5 +- src/common/spinlock.h | 72 +++++++++-------- src/common/thread.c | 27 ++++++- src/common/thread.h | 204 +++++++++++++++++++++++++---------------------- src/plugins/HPMHooking.c | 1 + src/test/test_spinlock.c | 4 +- 7 files changed, 176 insertions(+), 141 deletions(-) diff --git a/src/common/console.c b/src/common/console.c index bca9f36aa..4a12ba24b 100644 --- a/src/common/console.c +++ b/src/common/console.c @@ -499,7 +499,7 @@ void console_parse_final(void) { mutex->cond_signal(console->input->ptcond); /* wait for thread to close */ - rathread_wait(console->input->pthread, NULL); + thread->wait(console->input->pthread, NULL); mutex->cond_destroy(console->input->ptcond); mutex->destroy(console->input->ptmutex); @@ -515,7 +515,7 @@ void console_parse_init(void) { console->input->ptmutex = mutex->create(); console->input->ptcond = mutex->cond_create(); - if( (console->input->pthread = rathread_create(console->input->pthread_main, NULL)) == NULL ){ + if( (console->input->pthread = thread->create(console->input->pthread_main, NULL)) == NULL ){ ShowFatalError("console_parse_init: failed to spawn console_parse thread.\n"); exit(EXIT_FAILURE); } diff --git a/src/common/core.c b/src/common/core.c index f5860e992..508a728f0 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -266,6 +266,7 @@ void core_defaults(void) { socket_defaults(); rnd_defaults(); md5_defaults(); + thread_defaults(); #endif } /** @@ -505,7 +506,7 @@ int main (int argc, char **argv) { set_server_type(); Sql_Init(); - rathread_init(); + thread->init(); DB->init(); signals_init(); @@ -541,7 +542,7 @@ int main (int argc, char **argv) { timer->final(); sockt->final(); DB->final(); - rathread_final(); + thread->final(); ers_final(); rnd->final(); #endif diff --git a/src/common/spinlock.h b/src/common/spinlock.h index 4d9c4c668..f1df6ef34 100644 --- a/src/common/spinlock.h +++ b/src/common/spinlock.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) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify @@ -54,57 +54,61 @@ typedef struct SPIN_LOCK{ #ifdef HERCULES_CORE -static forceinline void InitializeSpinLock(SPIN_LOCK *lck){ - lck->lock = 0; - lck->nest = 0; - lck->sync_lock = 0; +static forceinline void InitializeSpinLock(SPIN_LOCK *lck) +{ + lck->lock = 0; + lck->nest = 0; + lck->sync_lock = 0; } -static forceinline void FinalizeSpinLock(SPIN_LOCK *lck){ +static forceinline void FinalizeSpinLock(SPIN_LOCK *lck) +{ return; } -#define getsynclock(l) do { if(InterlockedCompareExchange((l), 1, 0) == 0) break; rathread_yield(); } while(/*always*/1) +#define getsynclock(l) do { if(InterlockedCompareExchange((l), 1, 0) == 0) break; thread->yield(); } while(/*always*/1) #define dropsynclock(l) do { InterlockedExchange((l), 0); } while(0) -static forceinline void EnterSpinLock(SPIN_LOCK *lck){ - int tid = rathread_get_tid(); +static forceinline void EnterSpinLock(SPIN_LOCK *lck) +{ + int tid = thread->get_tid(); - // Get Sync Lock && Check if the requester thread already owns the lock. - // if it owns, increase nesting level - getsynclock(&lck->sync_lock); - if(InterlockedCompareExchange(&lck->lock, tid, tid) == tid){ - InterlockedIncrement(&lck->nest); - dropsynclock(&lck->sync_lock); - return; // Got Lock - } - // drop sync lock + // Get Sync Lock && Check if the requester thread already owns the lock. + // if it owns, increase nesting level + getsynclock(&lck->sync_lock); + if (InterlockedCompareExchange(&lck->lock, tid, tid) == tid) { + InterlockedIncrement(&lck->nest); dropsynclock(&lck->sync_lock); - - // Spin until we've got it ! - while(1){ - if(InterlockedCompareExchange(&lck->lock, tid, 0) == 0){ - InterlockedIncrement(&lck->nest); - return; // Got Lock - } - rathread_yield(); // Force ctxswitch to another thread. + return; // Got Lock + } + // drop sync lock + dropsynclock(&lck->sync_lock); + + // Spin until we've got it ! + while (true) { + if (InterlockedCompareExchange(&lck->lock, tid, 0) == 0) { + InterlockedIncrement(&lck->nest); + return; // Got Lock } + thread->yield(); // Force ctxswitch to another thread. + } } -static forceinline void LeaveSpinLock(SPIN_LOCK *lck){ - int tid = rathread_get_tid(); +static forceinline void LeaveSpinLock(SPIN_LOCK *lck) +{ + int tid = thread->get_tid(); - getsynclock(&lck->sync_lock); + getsynclock(&lck->sync_lock); - if(InterlockedCompareExchange(&lck->lock, tid, tid) == tid){ // this thread owns the lock. - if(InterlockedDecrement(&lck->nest) == 0) - InterlockedExchange(&lck->lock, 0); // Unlock! - } + if (InterlockedCompareExchange(&lck->lock, tid, tid) == tid) { // this thread owns the lock. + if (InterlockedDecrement(&lck->nest) == 0) + InterlockedExchange(&lck->lock, 0); // Unlock! + } - dropsynclock(&lck->sync_lock); + dropsynclock(&lck->sync_lock); } #endif // HERCULES_CORE diff --git a/src/common/thread.c b/src/common/thread.c index b724344e6..7bcffaf86 100644 --- a/src/common/thread.c +++ b/src/common/thread.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) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify @@ -68,6 +68,9 @@ struct rAthread { __thread int g_rathread_ID = -1; #endif +struct thread_interface thread_s; +struct thread_interface *thread; + /// /// Subystem Code /// @@ -100,7 +103,7 @@ void rathread_final(void) { for(i = 1; i < RA_THREADS_MAX; i++){ if(l_threads[i].proc != NULL){ ShowWarning("rAthread_final: unterminated Thread (tid %u entryPoint %p) - forcing to terminate (kill)\n", i, l_threads[i].proc); - rathread_destroy(&l_threads[i]); + thread->destroy(&l_threads[i]); } } @@ -159,7 +162,7 @@ static void *raThreadMainRedirector( void *p ){ /// API Level /// rAthread *rathread_create(rAthreadProc entryPoint, void *param) { - return rathread_createEx( entryPoint, param, (1<<23) /*8MB*/, RAT_PRIO_NORMAL ); + return thread->createEx(entryPoint, param, (1<<23) /*8MB*/, RAT_PRIO_NORMAL); }//end: rathread_create() rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack, RATHREAD_PRIO prio) { @@ -205,7 +208,7 @@ rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack pthread_attr_destroy(&attr); #endif - rathread_prio_set( handle, prio ); + thread->prio_set( handle, prio ); return handle; }//end: rathread_createEx @@ -301,3 +304,19 @@ void rathread_yield(void) { sched_yield(); #endif }//end: rathread_yield() + +void thread_defaults(void) +{ + thread = &thread_s; + thread->create = rathread_create; + thread->createEx = rathread_createEx; + thread->destroy = rathread_destroy; + thread->self = rathread_self; + thread->get_tid = rathread_get_tid; + thread->wait = rathread_wait; + thread->prio_set = rathread_prio_set; + thread->prio_get = rathread_prio_get; + thread->yield = rathread_yield; + thread->init = rathread_init; + thread->final = rathread_final; +} diff --git a/src/common/thread.h b/src/common/thread.h index 261735306..be34ea830 100644 --- a/src/common/thread.h +++ b/src/common/thread.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) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify @@ -21,7 +21,7 @@ #ifndef COMMON_THREAD_H #define COMMON_THREAD_H -#include "common/cbasetypes.h" +#include "common/hercules.h" typedef struct rAthread rAthread; typedef void* (*rAthreadProc)(void*); @@ -32,103 +32,113 @@ typedef enum RATHREAD_PRIO { RAT_PRIO_HIGH } RATHREAD_PRIO; +struct thread_interface { + /** + * Creates a new Thread. + * + * @param entyPoint Thread's entry point. + * @param param General purpose parameter, would be given as + * parameter to the thread's entry point. + * + * @return The created thread object. + * @retval NULL in vase of failure. + */ + rAthread *(*create) (rAthreadProc entryPoint, void *param); + + /** + * Creates a new Thread (with more creation options). + * + * @param entyPoint Thread's entry point. + * @param param General purpose parameter, would be given as + * parameter to the thread's entry point. + * @param szStack Stack Size in bytes. + * @param prio Priority of the Thread in the OS scheduler. + * + * @return The created thread object. + * @retval NULL in case of failure. + */ + rAthread *(*createEx) (rAthreadProc entryPoint, void *param, size_t szStack, RATHREAD_PRIO prio); + + /** + * Destroys the given Thread immediately. + * + * @remark + * The Handle gets invalid after call! don't use it afterwards. + * + * @param handle The thread to destroy. + */ + void (*destroy) (rAthread *handle); + + /** + * Returns the thread handle of the thread calling this function. + * + * @remark + * This won't work in the program's main thread. + * + * @warning + * The underlying implementation might not perform very well, cache + * the value received! + * + * @return the thread handle. + * @retval NULL in case of failure. + */ + rAthread *(*self) (void); + + /** + * Returns own thread id (TID). + * + * @remark + * This is an unique identifier for the calling thread, and depends + * on platform/ compiler, and may not be the systems Thread ID! + * + * @return the thread ID. + * @retval -1 in case of failure. + */ + int (*get_tid) (void); + + /** + * Waits for the given thread to terminate. + * + * @param[in] handle The thread to wait (join) for. + * @param[out] out_Exitcode Pointer to return the exit code of the + * given thread after termination (optional). + * + * @retval true if the given thread has been terminated. + */ + bool (*wait) (rAthread *handle, void **out_exitCode); + + /** + * Sets the given priority in the OS scheduler. + * + * @param handle The thread to set the priority for. + * @param prio The priority to set (@see enum RATHREAD_PRIO). + */ + void (*prio_set) (rAthread *handle, RATHREAD_PRIO prio); + + /** + * Gets the current priority of the given thread. + * + * @param handle The thread to get the priority for. + */ + RATHREAD_PRIO (*prio_get) (rAthread *handle); + + /** + * Tells the OS scheduler to yield the execution of the calling thread. + * + * @remark + * This will not "pause" the thread, it just allows the OS to spend + * the remaining time of the slice to another thread. + */ + void (*yield) (void); + + void (*init) (void); + void (*final) (void); +}; #ifdef HERCULES_CORE -/** - * Creates a new Thread - * - * @param entyPoint - entryProc, - * @param param - general purpose parameter, would be given as parameter to the thread's entry point. - * - * @return not NULL if success - */ -rAthread *rathread_create(rAthreadProc entryPoint, void *param); - - -/** - * Creates a new Thread (with more creation options) - * - * @param entyPoint - entryProc, - * @param param - general purpose parameter, would be given as parameter to the thread's entry point - * @param szStack - stack Size in bytes - * @param prio - Priority of the Thread @ OS Scheduler.. - * - * @return not NULL if success - */ -rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack, RATHREAD_PRIO prio); - - -/** - * Destroys the given Thread immediately - * - * @note The Handle gets invalid after call! don't use it afterwards. - * - * @param handle - thread to destroy. - */ -void rathread_destroy(rAthread *handle); - - -/** - * Returns the thread handle of the thread calling this function - * - * @note this wont work @ programs main thread - * @note the underlying implementation might not perform very well, cache the value received! - * - * @return not NULL if success - */ -rAthread *rathread_self(void); - - -/** - * Returns own thread id (TID) - * - * @note this is an unique identifier for the calling thread, and - * depends on platform/ compiler, and may not be the systems Thread ID! - * - * @return -1 when fails, otherwise >= 0 - */ -int rathread_get_tid(void); - - -/** - * Waits for the given thread to terminate - * - * @param handle - thread to wait (join) for - * @param out_Exitcode - [OPTIONAL] - if given => Exitcode (value) of the given thread - if it's terminated - * - * @return true - if the given thread has been terminated. - */ -bool rathread_wait(rAthread *handle, void **out_exitCode); - - -/** - * Sets the given PRIORITY @ OS Task Scheduler - * - * @param handle - thread to set prio for - * @param rio - the priority (RAT_PRIO_LOW ... ) - */ -void rathread_prio_set(rAthread *handle, RATHREAD_PRIO prio); - - -/** - * Gets the current Prio of the given thread - * - * @param handle - the thread to get the prio for. - */ -RATHREAD_PRIO rathread_prio_get(rAthread *handle); - - -/** - * Tells the OS scheduler to yield the execution of the calling thread - * - * @note: this will not "pause" the thread, - * it just allows the OS to spend the remaining time - * of the slice to another thread. - */ -void rathread_yield(void); - -void rathread_init(void); -void rathread_final(void); +void thread_defaults(void); #endif // HERCULES_CORE +HPShared struct thread_interface *thread; + #endif /* COMMON_THREAD_H */ diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index 1c0cec51d..97a36f4ce 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -120,6 +120,7 @@ #include "common/sql.h" #include "common/strlib.h" #include "common/sysinfo.h" +#include "common/thread.h" #include "common/timer.h" #include "common/utils.h" diff --git a/src/test/test_spinlock.c b/src/test/test_spinlock.c index 6ffc4eafc..a19887f8d 100644 --- a/src/test/test_spinlock.c +++ b/src/test/test_spinlock.c @@ -78,13 +78,13 @@ int do_init(int argc, char **argv){ InitializeSpinLock(&lock); for(i =0; i < THRC; i++){ - t[i] = rathread_createEx( worker, NULL, 1024*512, RAT_PRIO_NORMAL); + t[i] = thread->createEx(worker, NULL, 1024*512, RAT_PRIO_NORMAL); } while(1){ if(InterlockedCompareExchange(&done_threads, THRC, THRC) == THRC) break; - rathread_yield(); + thread->yield(); } FinalizeSpinLock(&lock); -- cgit v1.2.3-70-g09d2 From 8c66f639fdd495f3d28aac905acda823fc6a0ae6 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 23:51:26 +0100 Subject: Corrected some compiler warnings in test_spinlock --- src/test/test_spinlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/test_spinlock.c b/src/test/test_spinlock.c index a19887f8d..cb46b239c 100644 --- a/src/test/test_spinlock.c +++ b/src/test/test_spinlock.c @@ -66,7 +66,7 @@ int do_init(int argc, char **argv){ int ok; ShowStatus("==========\n"); - ShowStatus("TEST: %u Runs, (%u Threads)\n", LOOPS, THRC); + ShowStatus("TEST: %d Runs, (%d Threads)\n", LOOPS, THRC); ShowStatus("This can take a while\n"); ShowStatus("\n\n"); -- cgit v1.2.3-70-g09d2 From b9578c1d8ce54b48363e297b1c56cdea0ed72821 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 13 Mar 2016 15:48:14 +0100 Subject: Removed unnecessary typedefs from thread and spinlock - SPIN_LOCK -> struct spin_lock - rAthread -> struct thread_handle - rAthreadProc -> threadFunc - RATHREAD_PRIO -> enum thread_priority - RAT_PRIO_LOW -> THREADPRIO_LOW - RAT_PRIO_NORMAL -> THREADPRIO_NORMAL - RAT_PRIO_HIGH -> THREADPRIO_HIGH - RA_THREADS_MAX -> THREADS_MAX Signed-off-by: Haru fixupthread --- src/common/console.c | 14 ++++---- src/common/console.h | 15 +++++---- src/common/spinlock.h | 25 +++++++-------- src/common/thread.c | 73 ++++++++++++++++++++++++------------------ src/common/thread.h | 33 ++++++++++--------- src/test/test_spinlock.c | 9 +++--- tools/HPMHookGen/HPMHookGen.pl | 2 ++ 7 files changed, 93 insertions(+), 78 deletions(-) diff --git a/src/common/console.c b/src/common/console.c index 4a12ba24b..f1b4523e2 100644 --- a/src/common/console.c +++ b/src/common/console.c @@ -57,6 +57,7 @@ struct console_interface console_s; struct console_interface *console; #ifdef CONSOLE_INPUT struct console_input_interface console_input_s; +struct spin_lock console_ptlock_s; struct { char queue[CONSOLE_PARSE_SIZE][MAX_CONSOLE_INPUT]; @@ -464,15 +465,15 @@ void *cThread_main(void *x) { console->input->parse(input); if( input[0] != '\0' ) {/* did we get something? */ - EnterSpinLock(&console->input->ptlock); + EnterSpinLock(console->input->ptlock); if( cinput.count == CONSOLE_PARSE_SIZE ) { - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); continue;/* drop */ } safestrncpy(cinput.queue[cinput.count++],input,MAX_CONSOLE_INPUT); - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); } } mutex->lock(console->input->ptmutex); @@ -484,12 +485,12 @@ void *cThread_main(void *x) { } int console_parse_timer(int tid, int64 tick, int id, intptr_t data) { int i; - EnterSpinLock(&console->input->ptlock); + EnterSpinLock(console->input->ptlock); for(i = 0; i < cinput.count; i++) { console->input->parse_sub(cinput.queue[i]); } cinput.count = 0; - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); mutex->cond_signal(console->input->ptcond); return 0; } @@ -510,7 +511,7 @@ void console_parse_init(void) { console->input->ptstate = 1; - InitializeSpinLock(&console->input->ptlock); + InitializeSpinLock(console->input->ptlock); console->input->ptmutex = mutex->create(); console->input->ptcond = mutex->cond_create(); @@ -563,6 +564,7 @@ void console_defaults(void) console->display_gplnotice = display_gplnotice; #ifdef CONSOLE_INPUT console->input = &console_input_s; + console->input->ptlock = &console_ptlock_s; console->input->parse_init = console_parse_init; console->input->parse_final = console_parse_final; console->input->parse_timer = console_parse_timer; diff --git a/src/common/console.h b/src/common/console.h index f97e36456..dd3a9cf2e 100644 --- a/src/common/console.h +++ b/src/common/console.h @@ -23,12 +23,13 @@ #include "common/hercules.h" #include "common/db.h" #include "common/spinlock.h" -#include "common/thread.h" /* Forward Declarations */ struct Sql; // common/sql.h -struct mutex_data; struct cond_data; +struct mutex_data; +struct spin_lock; +struct thread_handle; /** * Queue Max @@ -72,11 +73,11 @@ struct CParseEntry { struct console_input_interface { #ifdef CONSOLE_INPUT /* vars */ - SPIN_LOCK ptlock;/* parse thread lock */ - rAthread *pthread;/* parse thread */ - volatile int32 ptstate;/* parse thread state */ - struct mutex_data *ptmutex; ///< parse thread mutex. - struct cond_data *ptcond; ///< parse thread conditional variable. + struct spin_lock *ptlock; ///< parse thread lock. + struct thread_handle *pthread; ///< parse thread. + volatile int32 ptstate; ///< parse thread state. + struct mutex_data *ptmutex; ///< parse thread mutex. + struct cond_data *ptcond; ///< parse thread conditional variable. /* */ VECTOR_DECL(struct CParseEntry *) command_list; VECTOR_DECL(struct CParseEntry *) commands; diff --git a/src/common/spinlock.h b/src/common/spinlock.h index f1df6ef34..c04416285 100644 --- a/src/common/spinlock.h +++ b/src/common/spinlock.h @@ -37,31 +37,28 @@ #endif #ifdef WIN32 - -typedef struct __declspec( align(64) ) SPIN_LOCK{ +struct __declspec(align(64)) spin_lock { volatile LONG lock; volatile LONG nest; volatile LONG sync_lock; -} SPIN_LOCK; +}; #else -typedef struct SPIN_LOCK{ - volatile int32 lock; - volatile int32 nest; // nesting level. - - volatile int32 sync_lock; -} __attribute__((aligned(64))) SPIN_LOCK; +struct spin_lock { + volatile int32 lock; + volatile int32 nest; // nesting level. + volatile int32 sync_lock; +} __attribute__((aligned(64))); #endif - #ifdef HERCULES_CORE -static forceinline void InitializeSpinLock(SPIN_LOCK *lck) +static forceinline void InitializeSpinLock(struct spin_lock *lck) { lck->lock = 0; lck->nest = 0; lck->sync_lock = 0; } -static forceinline void FinalizeSpinLock(SPIN_LOCK *lck) +static forceinline void FinalizeSpinLock(struct spin_lock *lck) { return; } @@ -70,7 +67,7 @@ static forceinline void FinalizeSpinLock(SPIN_LOCK *lck) #define getsynclock(l) do { if(InterlockedCompareExchange((l), 1, 0) == 0) break; thread->yield(); } while(/*always*/1) #define dropsynclock(l) do { InterlockedExchange((l), 0); } while(0) -static forceinline void EnterSpinLock(SPIN_LOCK *lck) +static forceinline void EnterSpinLock(struct spin_lock *lck) { int tid = thread->get_tid(); @@ -97,7 +94,7 @@ static forceinline void EnterSpinLock(SPIN_LOCK *lck) } -static forceinline void LeaveSpinLock(SPIN_LOCK *lck) +static forceinline void LeaveSpinLock(struct spin_lock *lck) { int tid = thread->get_tid(); diff --git a/src/common/thread.c b/src/common/thread.c index 7bcffaf86..8d862bbfc 100644 --- a/src/common/thread.c +++ b/src/common/thread.c @@ -48,13 +48,13 @@ #define HAS_TLS #endif -#define RA_THREADS_MAX 64 +#define THREADS_MAX 64 -struct rAthread { +struct thread_handle { unsigned int myID; - RATHREAD_PRIO prio; - rAthreadProc proc; + enum thread_priority prio; + threadFunc proc; void *param; #ifdef WIN32 @@ -74,13 +74,13 @@ struct thread_interface *thread; /// /// Subystem Code /// -static struct rAthread l_threads[RA_THREADS_MAX]; +static struct thread_handle l_threads[THREADS_MAX]; void rathread_init(void) { register unsigned int i; - memset(&l_threads, 0x00, RA_THREADS_MAX * sizeof(struct rAthread) ); + memset(&l_threads, 0x00, THREADS_MAX * sizeof(struct thread_handle)); - for(i = 0; i < RA_THREADS_MAX; i++){ + for (i = 0; i < THREADS_MAX; i++) { l_threads[i].myID = i; } @@ -88,8 +88,8 @@ void rathread_init(void) { #ifdef HAS_TLS g_rathread_ID = 0; #endif - l_threads[0].prio = RAT_PRIO_NORMAL; - l_threads[0].proc = (rAthreadProc)0xDEADCAFE; + l_threads[0].prio = THREADPRIO_NORMAL; + l_threads[0].proc = (threadFunc)0xDEADCAFE; }//end: rathread_init() @@ -100,7 +100,7 @@ void rathread_final(void) { // Shouldn't happen .. // Kill 'em all! // - for(i = 1; i < RA_THREADS_MAX; i++){ + for (i = 1; i < THREADS_MAX; i++) { if(l_threads[i].proc != NULL){ ShowWarning("rAthread_final: unterminated Thread (tid %u entryPoint %p) - forcing to terminate (kill)\n", i, l_threads[i].proc); thread->destroy(&l_threads[i]); @@ -110,24 +110,28 @@ void rathread_final(void) { }//end: rathread_final() // gets called whenever a thread terminated .. -static void rat_thread_terminated(rAthread *handle) { +static void rat_thread_terminated(struct thread_handle *handle) +{ // Preserve handle->myID and handle->hThread, set everything else to its default value handle->param = NULL; handle->proc = NULL; - handle->prio = RAT_PRIO_NORMAL; + handle->prio = THREADPRIO_NORMAL; }//end: rat_thread_terminated() #ifdef WIN32 -DWORD WINAPI raThreadMainRedirector(LPVOID p){ +DWORD WINAPI raThreadMainRedirector(LPVOID p) +{ #else -static void *raThreadMainRedirector( void *p ){ +static void *raThreadMainRedirector(void *p) +{ sigset_t set; // on Posix Thread platforms #endif void *ret; + struct thread_handle *self = p; // Update myID @ TLS to right id. #ifdef HAS_TLS - g_rathread_ID = ((rAthread*)p)->myID; + g_rathread_ID = self->myID; #endif #ifndef WIN32 @@ -144,13 +148,13 @@ static void *raThreadMainRedirector( void *p ){ #endif - ret = ((rAthread*)p)->proc( ((rAthread*)p)->param ) ; + ret = self->proc(self->param); #ifdef WIN32 - CloseHandle( ((rAthread*)p)->hThread ); + CloseHandle(self->hThread); #endif - rat_thread_terminated( (rAthread*)p ); + rat_thread_terminated(self); #ifdef WIN32 return (DWORD)ret; #else @@ -161,17 +165,19 @@ static void *raThreadMainRedirector( void *p ){ /// /// API Level /// -rAthread *rathread_create(rAthreadProc entryPoint, void *param) { - return thread->createEx(entryPoint, param, (1<<23) /*8MB*/, RAT_PRIO_NORMAL); +struct thread_handle *rathread_create(threadFunc entryPoint, void *param) +{ + return thread->createEx(entryPoint, param, (1<<23) /*8MB*/, THREADPRIO_NORMAL); }//end: rathread_create() -rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack, RATHREAD_PRIO prio) { +struct thread_handle *rathread_createEx(threadFunc entryPoint, void *param, size_t szStack, enum thread_priority prio) +{ #ifndef WIN32 pthread_attr_t attr; #endif size_t tmp; unsigned int i; - rAthread *handle = NULL; + struct thread_handle *handle = NULL; // given stacksize aligned to systems pagesize? tmp = szStack % sysinfo->getpagesize(); @@ -179,7 +185,7 @@ rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack szStack += tmp; // Get a free Thread Slot. - for(i = 0; i < RA_THREADS_MAX; i++){ + for (i = 0; i < THREADS_MAX; i++) { if(l_threads[i].proc == NULL){ handle = &l_threads[i]; break; @@ -213,7 +219,8 @@ rAthread *rathread_createEx(rAthreadProc entryPoint, void *param, size_t szStack return handle; }//end: rathread_createEx -void rathread_destroy(rAthread *handle) { +void rathread_destroy(struct thread_handle *handle) +{ #ifdef WIN32 if( TerminateThread(handle->hThread, 0) != FALSE){ CloseHandle(handle->hThread); @@ -230,9 +237,10 @@ void rathread_destroy(rAthread *handle) { #endif }//end: rathread_destroy() -rAthread *rathread_self(void) { +struct thread_handle *rathread_self(void) +{ #ifdef HAS_TLS - rAthread *handle = &l_threads[g_rathread_ID]; + struct thread_handle *handle = &l_threads[g_rathread_ID]; if(handle->proc != NULL) // entry point set, so its used! return handle; @@ -248,7 +256,7 @@ rAthread *rathread_self(void) { hSelf = pthread_self(); #endif - for(i = 0; i < RA_THREADS_MAX; i++){ + for (i = 0; i < THREADS_MAX; i++) { if(l_threads[i].hThread == hSelf && l_threads[i].proc != NULL) return &l_threads[i]; } @@ -272,7 +280,8 @@ int rathread_get_tid(void) { }//end: rathread_get_tid() -bool rathread_wait(rAthread *handle, void **out_exitCode) { +bool rathread_wait(struct thread_handle *handle, void **out_exitCode) +{ // Hint: // no thread data cleanup routine call here! // its managed by the callProxy itself.. @@ -288,12 +297,14 @@ bool rathread_wait(rAthread *handle, void **out_exitCode) { }//end: rathread_wait() -void rathread_prio_set(rAthread *handle, RATHREAD_PRIO prio) { - handle->prio = RAT_PRIO_NORMAL; +void rathread_prio_set(struct thread_handle *handle, enum thread_priority prio) +{ + handle->prio = THREADPRIO_NORMAL; //@TODO }//end: rathread_prio_set() -RATHREAD_PRIO rathread_prio_get(rAthread *handle) { +enum thread_priority rathread_prio_get(struct thread_handle *handle) +{ return handle->prio; }//end: rathread_prio_get() diff --git a/src/common/thread.h b/src/common/thread.h index be34ea830..bde35f738 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -23,14 +23,15 @@ #include "common/hercules.h" -typedef struct rAthread rAthread; -typedef void* (*rAthreadProc)(void*); - -typedef enum RATHREAD_PRIO { - RAT_PRIO_LOW = 0, - RAT_PRIO_NORMAL, - RAT_PRIO_HIGH -} RATHREAD_PRIO; +/* Opaque Types */ +struct thread_handle; ///< Thread handle. +typedef void *(*threadFunc)(void *); ///< Thread entry point function. + +enum thread_priority { + THREADPRIO_LOW = 0, + THREADPRIO_NORMAL, + THREADPRIO_HIGH +}; struct thread_interface { /** @@ -43,7 +44,7 @@ struct thread_interface { * @return The created thread object. * @retval NULL in vase of failure. */ - rAthread *(*create) (rAthreadProc entryPoint, void *param); + struct thread_handle *(*create) (threadFunc entryPoint, void *param); /** * Creates a new Thread (with more creation options). @@ -57,7 +58,7 @@ struct thread_interface { * @return The created thread object. * @retval NULL in case of failure. */ - rAthread *(*createEx) (rAthreadProc entryPoint, void *param, size_t szStack, RATHREAD_PRIO prio); + struct thread_handle *(*createEx) (threadFunc entryPoint, void *param, size_t szStack, enum thread_priority prio); /** * Destroys the given Thread immediately. @@ -67,7 +68,7 @@ struct thread_interface { * * @param handle The thread to destroy. */ - void (*destroy) (rAthread *handle); + void (*destroy) (struct thread_handle *handle); /** * Returns the thread handle of the thread calling this function. @@ -82,7 +83,7 @@ struct thread_interface { * @return the thread handle. * @retval NULL in case of failure. */ - rAthread *(*self) (void); + struct thread_handle *(*self) (void); /** * Returns own thread id (TID). @@ -105,22 +106,22 @@ struct thread_interface { * * @retval true if the given thread has been terminated. */ - bool (*wait) (rAthread *handle, void **out_exitCode); + bool (*wait) (struct thread_handle *handle, void **out_exitCode); /** * Sets the given priority in the OS scheduler. * * @param handle The thread to set the priority for. - * @param prio The priority to set (@see enum RATHREAD_PRIO). + * @param prio The priority to set (@see enum thread_priority). */ - void (*prio_set) (rAthread *handle, RATHREAD_PRIO prio); + void (*prio_set) (struct thread_handle *handle, enum thread_priority prio); /** * Gets the current priority of the given thread. * * @param handle The thread to get the priority for. */ - RATHREAD_PRIO (*prio_get) (rAthread *handle); + enum thread_priority (*prio_get) (struct thread_handle *handle); /** * Tells the OS scheduler to yield the execution of the calling thread. diff --git a/src/test/test_spinlock.c b/src/test/test_spinlock.c index cb46b239c..5ea3ee94d 100644 --- a/src/test/test_spinlock.c +++ b/src/test/test_spinlock.c @@ -38,7 +38,7 @@ #define PERINC 100000 #define LOOPS 47 -static SPIN_LOCK lock; +static struct spin_lock lock; static unsigned int val = 0; static volatile int32 done_threads = 0; @@ -60,8 +60,9 @@ static void *worker(void *p){ return NULL; }//end: worker() -int do_init(int argc, char **argv){ - rAthread *t[THRC]; +int do_init(int argc, char **argv) +{ + struct thread_handle *t[THRC]; int j, i; int ok; @@ -78,7 +79,7 @@ int do_init(int argc, char **argv){ InitializeSpinLock(&lock); for(i =0; i < THRC; i++){ - t[i] = thread->createEx(worker, NULL, 1024*512, RAT_PRIO_NORMAL); + t[i] = thread->createEx(worker, NULL, 1024*512, THREADPRIO_NORMAL); } while(1){ diff --git a/tools/HPMHookGen/HPMHookGen.pl b/tools/HPMHookGen/HPMHookGen.pl index 75dd51736..2f22193f0 100755 --- a/tools/HPMHookGen/HPMHookGen.pl +++ b/tools/HPMHookGen/HPMHookGen.pl @@ -242,6 +242,8 @@ sub parse($$) { $rtinit = ' = PACKET_UNKNOWN'; } elsif ($x =~ /^(?:enum\s+)?DBOptions$/) { # Known enum DBOptions $rtinit = ' = DB_OPT_BASE'; + } elsif ($x =~ /^enum\s+thread_priority$/) { # Known enum DBOptions + $rtinit = ' = THREADPRIO_NORMAL'; } elsif ($x eq 'DBComparator' or $x eq 'DBHasher' or $x eq 'DBReleaser') { # DB function pointers $rtinit = ' = NULL'; } elsif ($x =~ /^(?:struct|union)\s+.*$/) { # Structs and unions -- cgit v1.2.3-70-g09d2 From c4a3a3f59e5720b300077c3d34aa7f4ecdd76400 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 14 Mar 2016 00:19:22 +0100 Subject: Various changes to the thread interface Mostly stylistic changes. Cleaned up documentation. Signed-off-by: Haru --- src/common/thread.c | 204 ++++++++++++++++++++++++----------------------- src/common/thread.h | 47 ++++++----- src/test/test_spinlock.c | 2 +- 3 files changed, 136 insertions(+), 117 deletions(-) diff --git a/src/common/thread.c b/src/common/thread.c index 8d862bbfc..aaafab943 100644 --- a/src/common/thread.c +++ b/src/common/thread.c @@ -20,10 +20,6 @@ */ #define HERCULES_CORE -// Basic Threading abstraction (for pthread / win32 based systems) -// -// Author: Florian Wilkemeyer - #include "thread.h" #include "common/cbasetypes.h" @@ -48,6 +44,15 @@ #define HAS_TLS #endif +/** @file + * Thread interface implementation. + * @author Florian Wilkemeyer + */ + +struct thread_interface thread_s; +struct thread_interface *thread; + +/// The maximum amount of threads. #define THREADS_MAX 64 struct thread_handle { @@ -68,16 +73,14 @@ struct thread_handle { __thread int g_rathread_ID = -1; #endif -struct thread_interface thread_s; -struct thread_interface *thread; +// Subystem Code -/// -/// Subystem Code -/// static struct thread_handle l_threads[THREADS_MAX]; -void rathread_init(void) { - register unsigned int i; +/// @copydoc thread_interface::init() +void thread_init(void) +{ + register int i; memset(&l_threads, 0x00, THREADS_MAX * sizeof(struct thread_handle)); for (i = 0; i < THREADS_MAX; i++) { @@ -91,38 +94,41 @@ void rathread_init(void) { l_threads[0].prio = THREADPRIO_NORMAL; l_threads[0].proc = (threadFunc)0xDEADCAFE; -}//end: rathread_init() +} -void rathread_final(void) { - register unsigned int i; +/// @copydoc thread_interface::final() +void thread_final(void) +{ + register int i; // Unterminated Threads Left? - // Shouldn't happen .. - // Kill 'em all! - // + // Shouldn't happen ... Kill 'em all! for (i = 1; i < THREADS_MAX; i++) { - if(l_threads[i].proc != NULL){ - ShowWarning("rAthread_final: unterminated Thread (tid %u entryPoint %p) - forcing to terminate (kill)\n", i, l_threads[i].proc); + if (l_threads[i].proc != NULL){ + ShowWarning("thread_final: unterminated Thread (tid %d entry_point %p) - forcing to terminate (kill)\n", i, l_threads[i].proc); thread->destroy(&l_threads[i]); } } +} -}//end: rathread_final() - -// gets called whenever a thread terminated .. -static void rat_thread_terminated(struct thread_handle *handle) +/** + * Gets called whenever a thread terminated. + * + * @param handle The terminated thread's handle. + */ +static void thread_terminated(struct thread_handle *handle) { // Preserve handle->myID and handle->hThread, set everything else to its default value handle->param = NULL; handle->proc = NULL; handle->prio = THREADPRIO_NORMAL; -}//end: rat_thread_terminated() +} #ifdef WIN32 -DWORD WINAPI raThreadMainRedirector(LPVOID p) +DWORD WINAPI thread_main_redirector(LPVOID p) { #else -static void *raThreadMainRedirector(void *p) +static void *thread_main_redirector(void *p) { sigset_t set; // on Posix Thread platforms #endif @@ -145,7 +151,6 @@ static void *raThreadMainRedirector(void *p) (void)sigaddset(&set, SIGPIPE); pthread_sigmask(SIG_BLOCK, &set, NULL); - #endif ret = self->proc(self->param); @@ -154,35 +159,36 @@ static void *raThreadMainRedirector(void *p) CloseHandle(self->hThread); #endif - rat_thread_terminated(self); + thread_terminated(self); #ifdef WIN32 return (DWORD)ret; #else return ret; #endif -}//end: raThreadMainRedirector() +} + +// API Level -/// -/// API Level -/// -struct thread_handle *rathread_create(threadFunc entryPoint, void *param) +/// @copydoc thread_interface::create() +struct thread_handle *thread_create(threadFunc entry_point, void *param) { - return thread->createEx(entryPoint, param, (1<<23) /*8MB*/, THREADPRIO_NORMAL); -}//end: rathread_create() + return thread->create_opt(entry_point, param, (1<<23) /*8MB*/, THREADPRIO_NORMAL); +} -struct thread_handle *rathread_createEx(threadFunc entryPoint, void *param, size_t szStack, enum thread_priority prio) +/// @copydoc thread_interface::create_opt() +struct thread_handle *thread_create_opt(threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio) { #ifndef WIN32 pthread_attr_t attr; #endif size_t tmp; - unsigned int i; + int i; struct thread_handle *handle = NULL; // given stacksize aligned to systems pagesize? - tmp = szStack % sysinfo->getpagesize(); - if(tmp != 0) - szStack += tmp; + tmp = stack_size % sysinfo->getpagesize(); + if (tmp != 0) + stack_size += tmp; // Get a free Thread Slot. for (i = 0; i < THREADS_MAX; i++) { @@ -192,21 +198,21 @@ struct thread_handle *rathread_createEx(threadFunc entryPoint, void *param, size } } - if(handle == NULL){ - ShowError("rAthread: cannot create new thread (entryPoint: %p) - no free thread slot found!", entryPoint); + if (handle == NULL) { + ShowError("thread_create_opt: cannot create new thread (entry_point: %p) - no free thread slot found!", entry_point); return NULL; } - handle->proc = entryPoint; + handle->proc = entry_point; handle->param = param; #ifdef WIN32 - handle->hThread = CreateThread(NULL, szStack, raThreadMainRedirector, (void*)handle, 0, NULL); + handle->hThread = CreateThread(NULL, stack_size, thread_main_redirector, handle, 0, NULL); #else pthread_attr_init(&attr); - pthread_attr_setstacksize(&attr, szStack); + pthread_attr_setstacksize(&attr, stack_size); - if(pthread_create(&handle->hThread, &attr, raThreadMainRedirector, (void*)handle) != 0){ + if (pthread_create(&handle->hThread, &attr, thread_main_redirector, handle) != 0) { handle->proc = NULL; handle->param = NULL; return NULL; @@ -214,120 +220,122 @@ struct thread_handle *rathread_createEx(threadFunc entryPoint, void *param, size pthread_attr_destroy(&attr); #endif - thread->prio_set( handle, prio ); + thread->prio_set(handle, prio); return handle; -}//end: rathread_createEx +} -void rathread_destroy(struct thread_handle *handle) +/// @copydoc thread_interface::destroy() +void thread_destroy(struct thread_handle *handle) { #ifdef WIN32 - if( TerminateThread(handle->hThread, 0) != FALSE){ + if (TerminateThread(handle->hThread, 0) != FALSE) { CloseHandle(handle->hThread); - rat_thread_terminated(handle); + thread_terminated(handle); } #else - if( pthread_cancel( handle->hThread ) == 0){ + if (pthread_cancel(handle->hThread) == 0) { // We have to join it, otherwise pthread wont re-cycle its internal resources assoc. with this thread. - pthread_join( handle->hThread, NULL ); + pthread_join(handle->hThread, NULL); // Tell our manager to release resources ;) - rat_thread_terminated(handle); + thread_terminated(handle); } #endif -}//end: rathread_destroy() +} -struct thread_handle *rathread_self(void) +struct thread_handle *thread_self(void) { #ifdef HAS_TLS struct thread_handle *handle = &l_threads[g_rathread_ID]; - if(handle->proc != NULL) // entry point set, so its used! + if (handle->proc != NULL) // entry point set, so its used! return handle; #else // .. so no tls means we have to search the thread by its api-handle .. int i; - #ifdef WIN32 - HANDLE hSelf; - hSelf = GetCurrent = GetCurrentThread(); - #else - pthread_t hSelf; - hSelf = pthread_self(); - #endif +#ifdef WIN32 + HANDLE hSelf; + hSelf = GetCurrent = GetCurrentThread(); +#else + pthread_t hSelf; + hSelf = pthread_self(); +#endif for (i = 0; i < THREADS_MAX; i++) { - if(l_threads[i].hThread == hSelf && l_threads[i].proc != NULL) + if (l_threads[i].hThread == hSelf && l_threads[i].proc != NULL) return &l_threads[i]; } #endif return NULL; -}//end: rathread_self() - -int rathread_get_tid(void) { +} -#ifdef HAS_TLS +/// @copydoc thread_interface::get_tid() +int thread_get_tid(void) +{ +#if defined(HAS_TLS) return g_rathread_ID; +#elif defined(WIN32) + return (int)GetCurrentThreadId(); #else - // TODO - #ifdef WIN32 - return (int)GetCurrentThreadId(); - #else - return (int)pthread_self(); - #endif + return (int)pthread_self(); #endif +} -}//end: rathread_get_tid() - -bool rathread_wait(struct thread_handle *handle, void **out_exitCode) +/// @copydoc thread_interface::wait() +bool thread_wait(struct thread_handle *handle, void **out_exit_code) { // Hint: // no thread data cleanup routine call here! // its managed by the callProxy itself.. - // #ifdef WIN32 WaitForSingleObject(handle->hThread, INFINITE); return true; #else - if(pthread_join(handle->hThread, out_exitCode) == 0) + if (pthread_join(handle->hThread, out_exit_code) == 0) return true; return false; #endif -}//end: rathread_wait() +} -void rathread_prio_set(struct thread_handle *handle, enum thread_priority prio) +/// @copydoc thread_interface::prio_set() +void thread_prio_set(struct thread_handle *handle, enum thread_priority prio) { handle->prio = THREADPRIO_NORMAL; //@TODO -}//end: rathread_prio_set() +} -enum thread_priority rathread_prio_get(struct thread_handle *handle) +/// @copydoc thread_interface::prio_get() +enum thread_priority thread_prio_get(struct thread_handle *handle) { return handle->prio; -}//end: rathread_prio_get() +} -void rathread_yield(void) { +/// @copydoc thread_interface::yield() +void thread_yield(void) { #ifdef WIN32 SwitchToThread(); #else sched_yield(); #endif -}//end: rathread_yield() +} +/// Interface base initialization. void thread_defaults(void) { thread = &thread_s; - thread->create = rathread_create; - thread->createEx = rathread_createEx; - thread->destroy = rathread_destroy; - thread->self = rathread_self; - thread->get_tid = rathread_get_tid; - thread->wait = rathread_wait; - thread->prio_set = rathread_prio_set; - thread->prio_get = rathread_prio_get; - thread->yield = rathread_yield; - thread->init = rathread_init; - thread->final = rathread_final; + thread->init = thread_init; + thread->final = thread_final; + thread->create = thread_create; + thread->create_opt = thread_create_opt; + thread->destroy = thread_destroy; + thread->self = thread_self; + thread->get_tid = thread_get_tid; + thread->wait = thread_wait; + thread->prio_set = thread_prio_set; + thread->prio_get = thread_prio_get; + thread->yield = thread_yield; } diff --git a/src/common/thread.h b/src/common/thread.h index bde35f738..c668afbb4 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -23,42 +23,56 @@ #include "common/hercules.h" +/** @file + * Basic Threading abstraction (for pthread / win32 based systems). + */ + /* Opaque Types */ struct thread_handle; ///< Thread handle. typedef void *(*threadFunc)(void *); ///< Thread entry point function. +/* Enums */ + +/// Thread priority enum thread_priority { THREADPRIO_LOW = 0, THREADPRIO_NORMAL, THREADPRIO_HIGH }; +/// The thread interface struct thread_interface { + /// Interface initialization. + void (*init) (void); + + /// Interface finalization. + void (*final) (void); + /** * Creates a new Thread. * - * @param entyPoint Thread's entry point. - * @param param General purpose parameter, would be given as - * parameter to the thread's entry point. + * @param enty_point Thread's entry point. + * @param param General purpose parameter, would be given as + * parameter to the thread's entry point. * * @return The created thread object. * @retval NULL in vase of failure. */ - struct thread_handle *(*create) (threadFunc entryPoint, void *param); + struct thread_handle *(*create) (threadFunc entry_point, void *param); /** * Creates a new Thread (with more creation options). * - * @param entyPoint Thread's entry point. - * @param param General purpose parameter, would be given as - * parameter to the thread's entry point. - * @param szStack Stack Size in bytes. - * @param prio Priority of the Thread in the OS scheduler. + * @param enty_point Thread's entry point. + * @param param General purpose parameter, would be given as + * parameter to the thread's entry point. + * @param stack_size Stack Size in bytes. + * @param prio Priority of the Thread in the OS scheduler. * * @return The created thread object. * @retval NULL in case of failure. */ - struct thread_handle *(*createEx) (threadFunc entryPoint, void *param, size_t szStack, enum thread_priority prio); + struct thread_handle *(*create_opt) (threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio); /** * Destroys the given Thread immediately. @@ -100,13 +114,13 @@ struct thread_interface { /** * Waits for the given thread to terminate. * - * @param[in] handle The thread to wait (join) for. - * @param[out] out_Exitcode Pointer to return the exit code of the - * given thread after termination (optional). + * @param[in] handle The thread to wait (join) for. + * @param[out] out_exit_code Pointer to return the exit code of the + * given thread after termination (optional). * * @retval true if the given thread has been terminated. */ - bool (*wait) (struct thread_handle *handle, void **out_exitCode); + bool (*wait) (struct thread_handle *handle, void **out_exit_code); /** * Sets the given priority in the OS scheduler. @@ -131,15 +145,12 @@ struct thread_interface { * the remaining time of the slice to another thread. */ void (*yield) (void); - - void (*init) (void); - void (*final) (void); }; #ifdef HERCULES_CORE void thread_defaults(void); #endif // HERCULES_CORE -HPShared struct thread_interface *thread; +HPShared struct thread_interface *thread; ///< Pointer to the thread interface. #endif /* COMMON_THREAD_H */ diff --git a/src/test/test_spinlock.c b/src/test/test_spinlock.c index 5ea3ee94d..34102a845 100644 --- a/src/test/test_spinlock.c +++ b/src/test/test_spinlock.c @@ -79,7 +79,7 @@ int do_init(int argc, char **argv) InitializeSpinLock(&lock); for(i =0; i < THRC; i++){ - t[i] = thread->createEx(worker, NULL, 1024*512, THREADPRIO_NORMAL); + t[i] = thread->create_opt(worker, NULL, 1024*512, THREADPRIO_NORMAL); } while(1){ -- cgit v1.2.3-70-g09d2 From b0a4a441f582810c91aeec458fdbe7cc6cc78db9 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 14 Mar 2016 01:26:28 +0100 Subject: HPM Hooks Update Signed-off-by: Haru --- src/common/HPMDataCheck.h | 7 +- src/common/HPMSymbols.inc.h | 6 + src/plugins/HPMHooking/HPMHooking.Defs.inc | 24 ++ .../HPMHooking/HPMHooking_char.HPMHooksCore.inc | 45 ++++ .../HPMHooking/HPMHooking_char.HookingPoints.inc | 12 + src/plugins/HPMHooking/HPMHooking_char.Hooks.inc | 293 +++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_char.sources.inc | 1 + .../HPMHooking/HPMHooking_login.HPMHooksCore.inc | 45 ++++ .../HPMHooking/HPMHooking_login.HookingPoints.inc | 12 + src/plugins/HPMHooking/HPMHooking_login.Hooks.inc | 293 +++++++++++++++++++++ .../HPMHooking/HPMHooking_login.sources.inc | 1 + .../HPMHooking/HPMHooking_map.HPMHooksCore.inc | 45 ++++ .../HPMHooking/HPMHooking_map.HookingPoints.inc | 12 + src/plugins/HPMHooking/HPMHooking_map.Hooks.inc | 293 +++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_map.sources.inc | 1 + 15 files changed, 1089 insertions(+), 1 deletion(-) diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h index 8045b745f..55ea1e1b9 100644 --- a/src/common/HPMDataCheck.h +++ b/src/common/HPMDataCheck.h @@ -246,7 +246,7 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = { #define COMMON_SOCKET_H #endif // COMMON_SOCKET_H #ifdef COMMON_SPINLOCK_H - { "SPIN_LOCK", sizeof(struct SPIN_LOCK), SERVER_TYPE_ALL }, + { "spin_lock", sizeof(struct spin_lock), SERVER_TYPE_ALL }, #else #define COMMON_SPINLOCK_H #endif // COMMON_SPINLOCK_H @@ -269,6 +269,11 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = { #else #define COMMON_SYSINFO_H #endif // COMMON_SYSINFO_H + #ifdef COMMON_THREAD_H + { "thread_interface", sizeof(struct thread_interface), SERVER_TYPE_ALL }, + #else + #define COMMON_THREAD_H + #endif // COMMON_THREAD_H #ifdef COMMON_TIMER_H { "TimerData", sizeof(struct TimerData), SERVER_TYPE_ALL }, { "timer_interface", sizeof(struct timer_interface), SERVER_TYPE_ALL }, diff --git a/src/common/HPMSymbols.inc.h b/src/common/HPMSymbols.inc.h index 379c04b14..0a2e64cb4 100644 --- a/src/common/HPMSymbols.inc.h +++ b/src/common/HPMSymbols.inc.h @@ -254,6 +254,9 @@ struct sv_interface *sv; #ifdef COMMON_SYSINFO_H /* sysinfo */ struct sysinfo_interface *sysinfo; #endif // COMMON_SYSINFO_H +#ifdef COMMON_THREAD_H /* thread */ +struct thread_interface *thread; +#endif // COMMON_THREAD_H #ifdef COMMON_TIMER_H /* timer */ struct timer_interface *timer; #endif // COMMON_TIMER_H @@ -498,6 +501,9 @@ if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("sv", sv)) return "sv"; #ifdef COMMON_SYSINFO_H /* sysinfo */ if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("sysinfo", sysinfo)) return "sysinfo"; #endif // COMMON_SYSINFO_H +#ifdef COMMON_THREAD_H /* thread */ +if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("thread", thread)) return "thread"; +#endif // COMMON_THREAD_H #ifdef COMMON_TIMER_H /* timer */ if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("timer", timer)) return "timer"; #endif // COMMON_TIMER_H diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index cbf15fa7e..2ac33f6f7 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -7063,6 +7063,30 @@ typedef void (*HPMHOOK_post_sysinfo_init) (void); typedef void (*HPMHOOK_pre_sysinfo_final) (void); typedef void (*HPMHOOK_post_sysinfo_final) (void); #endif // COMMON_SYSINFO_H +#ifdef COMMON_THREAD_H /* thread */ +typedef void (*HPMHOOK_pre_thread_init) (void); +typedef void (*HPMHOOK_post_thread_init) (void); +typedef void (*HPMHOOK_pre_thread_final) (void); +typedef void (*HPMHOOK_post_thread_final) (void); +typedef struct thread_handle* (*HPMHOOK_pre_thread_create) (threadFunc *entry_point, void **param); +typedef struct thread_handle* (*HPMHOOK_post_thread_create) (struct thread_handle* retVal___, threadFunc entry_point, void *param); +typedef struct thread_handle* (*HPMHOOK_pre_thread_create_opt) (threadFunc *entry_point, void **param, size_t *stack_size, enum thread_priority *prio); +typedef struct thread_handle* (*HPMHOOK_post_thread_create_opt) (struct thread_handle* retVal___, threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio); +typedef void (*HPMHOOK_pre_thread_destroy) (struct thread_handle **handle); +typedef void (*HPMHOOK_post_thread_destroy) (struct thread_handle *handle); +typedef struct thread_handle* (*HPMHOOK_pre_thread_self) (void); +typedef struct thread_handle* (*HPMHOOK_post_thread_self) (struct thread_handle* retVal___); +typedef int (*HPMHOOK_pre_thread_get_tid) (void); +typedef int (*HPMHOOK_post_thread_get_tid) (int retVal___); +typedef bool (*HPMHOOK_pre_thread_wait) (struct thread_handle **handle, void ***out_exit_code); +typedef bool (*HPMHOOK_post_thread_wait) (bool retVal___, struct thread_handle *handle, void **out_exit_code); +typedef void (*HPMHOOK_pre_thread_prio_set) (struct thread_handle **handle, enum thread_priority *prio); +typedef void (*HPMHOOK_post_thread_prio_set) (struct thread_handle *handle, enum thread_priority prio); +typedef enum thread_priority (*HPMHOOK_pre_thread_prio_get) (struct thread_handle **handle); +typedef enum thread_priority (*HPMHOOK_post_thread_prio_get) (enum thread_priority retVal___, struct thread_handle *handle); +typedef void (*HPMHOOK_pre_thread_yield) (void); +typedef void (*HPMHOOK_post_thread_yield) (void); +#endif // COMMON_THREAD_H #ifdef COMMON_TIMER_H /* timer */ typedef int64 (*HPMHOOK_pre_timer_gettick) (void); typedef int64 (*HPMHOOK_post_timer_gettick) (int64 retVal___); diff --git a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc index 2162fad57..f51ddcffc 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HPMHooksCore.inc @@ -1448,6 +1448,28 @@ struct { struct HPMHookPoint *HP_sysinfo_init_post; struct HPMHookPoint *HP_sysinfo_final_pre; struct HPMHookPoint *HP_sysinfo_final_post; + struct HPMHookPoint *HP_thread_init_pre; + struct HPMHookPoint *HP_thread_init_post; + struct HPMHookPoint *HP_thread_final_pre; + struct HPMHookPoint *HP_thread_final_post; + struct HPMHookPoint *HP_thread_create_pre; + struct HPMHookPoint *HP_thread_create_post; + struct HPMHookPoint *HP_thread_create_opt_pre; + struct HPMHookPoint *HP_thread_create_opt_post; + struct HPMHookPoint *HP_thread_destroy_pre; + struct HPMHookPoint *HP_thread_destroy_post; + struct HPMHookPoint *HP_thread_self_pre; + struct HPMHookPoint *HP_thread_self_post; + struct HPMHookPoint *HP_thread_get_tid_pre; + struct HPMHookPoint *HP_thread_get_tid_post; + struct HPMHookPoint *HP_thread_wait_pre; + struct HPMHookPoint *HP_thread_wait_post; + struct HPMHookPoint *HP_thread_prio_set_pre; + struct HPMHookPoint *HP_thread_prio_set_post; + struct HPMHookPoint *HP_thread_prio_get_pre; + struct HPMHookPoint *HP_thread_prio_get_post; + struct HPMHookPoint *HP_thread_yield_pre; + struct HPMHookPoint *HP_thread_yield_post; struct HPMHookPoint *HP_timer_gettick_pre; struct HPMHookPoint *HP_timer_gettick_post; struct HPMHookPoint *HP_timer_gettick_nocache_pre; @@ -2899,6 +2921,28 @@ struct { int HP_sysinfo_init_post; int HP_sysinfo_final_pre; int HP_sysinfo_final_post; + int HP_thread_init_pre; + int HP_thread_init_post; + int HP_thread_final_pre; + int HP_thread_final_post; + int HP_thread_create_pre; + int HP_thread_create_post; + int HP_thread_create_opt_pre; + int HP_thread_create_opt_post; + int HP_thread_destroy_pre; + int HP_thread_destroy_post; + int HP_thread_self_pre; + int HP_thread_self_post; + int HP_thread_get_tid_pre; + int HP_thread_get_tid_post; + int HP_thread_wait_pre; + int HP_thread_wait_post; + int HP_thread_prio_set_pre; + int HP_thread_prio_set_post; + int HP_thread_prio_get_pre; + int HP_thread_prio_get_post; + int HP_thread_yield_pre; + int HP_thread_yield_post; int HP_timer_gettick_pre; int HP_timer_gettick_post; int HP_timer_gettick_nocache_pre; @@ -2964,5 +3008,6 @@ struct { struct strlib_interface strlib; struct sv_interface sv; struct sysinfo_interface sysinfo; + struct thread_interface thread; struct timer_interface timer; } source; diff --git a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc index d9021e906..4371cf81a 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.HookingPoints.inc @@ -773,6 +773,18 @@ struct HookingPointData HookingPoints[] = { { HP_POP(sysinfo->is_superuser, HP_sysinfo_is_superuser) }, { HP_POP(sysinfo->init, HP_sysinfo_init) }, { HP_POP(sysinfo->final, HP_sysinfo_final) }, +/* thread_interface */ + { HP_POP(thread->init, HP_thread_init) }, + { HP_POP(thread->final, HP_thread_final) }, + { HP_POP(thread->create, HP_thread_create) }, + { HP_POP(thread->create_opt, HP_thread_create_opt) }, + { HP_POP(thread->destroy, HP_thread_destroy) }, + { HP_POP(thread->self, HP_thread_self) }, + { HP_POP(thread->get_tid, HP_thread_get_tid) }, + { HP_POP(thread->wait, HP_thread_wait) }, + { HP_POP(thread->prio_set, HP_thread_prio_set) }, + { HP_POP(thread->prio_get, HP_thread_prio_get) }, + { HP_POP(thread->yield, HP_thread_yield) }, /* timer_interface */ { HP_POP(timer->gettick, HP_timer_gettick) }, { HP_POP(timer->gettick_nocache, HP_timer_gettick_nocache) }, diff --git a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc index 958724065..aecffbecd 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.Hooks.inc @@ -19050,6 +19050,299 @@ void HP_sysinfo_final(void) { } return; } +/* thread_interface */ +void HP_thread_init(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_init_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_init_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_init_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.init(); + } + if( HPMHooks.count.HP_thread_init_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_init_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_init_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_thread_final(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_final_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_final_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_final_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.final(); + } + if( HPMHooks.count.HP_thread_final_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_final_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_final_post[hIndex].func; + postHookFunc(); + } + } + return; +} +struct thread_handle* HP_thread_create(threadFunc entry_point, void *param) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_create_pre ) { + struct thread_handle* (*preHookFunc) (threadFunc *entry_point, void **param); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_create_pre[hIndex].func; + retVal___ = preHookFunc(&entry_point, ¶m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.create(entry_point, param); + } + if( HPMHooks.count.HP_thread_create_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___, threadFunc entry_point, void *param); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___, entry_point, param); + } + } + return retVal___; +} +struct thread_handle* HP_thread_create_opt(threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_create_opt_pre ) { + struct thread_handle* (*preHookFunc) (threadFunc *entry_point, void **param, size_t *stack_size, enum thread_priority *prio); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_opt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_create_opt_pre[hIndex].func; + retVal___ = preHookFunc(&entry_point, ¶m, &stack_size, &prio); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.create_opt(entry_point, param, stack_size, prio); + } + if( HPMHooks.count.HP_thread_create_opt_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___, threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_opt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_create_opt_post[hIndex].func; + retVal___ = postHookFunc(retVal___, entry_point, param, stack_size, prio); + } + } + return retVal___; +} +void HP_thread_destroy(struct thread_handle *handle) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_destroy_pre ) { + void (*preHookFunc) (struct thread_handle **handle); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_destroy_pre[hIndex].func; + preHookFunc(&handle); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.destroy(handle); + } + if( HPMHooks.count.HP_thread_destroy_post ) { + void (*postHookFunc) (struct thread_handle *handle); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_destroy_post[hIndex].func; + postHookFunc(handle); + } + } + return; +} +struct thread_handle* HP_thread_self(void) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_self_pre ) { + struct thread_handle* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_self_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_self_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.self(); + } + if( HPMHooks.count.HP_thread_self_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_self_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_self_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +int HP_thread_get_tid(void) { + int hIndex = 0; + int retVal___ = 0; + if( HPMHooks.count.HP_thread_get_tid_pre ) { + int (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_get_tid_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_get_tid_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.get_tid(); + } + if( HPMHooks.count.HP_thread_get_tid_post ) { + int (*postHookFunc) (int retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_get_tid_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_get_tid_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +bool HP_thread_wait(struct thread_handle *handle, void **out_exit_code) { + int hIndex = 0; + bool retVal___ = false; + if( HPMHooks.count.HP_thread_wait_pre ) { + bool (*preHookFunc) (struct thread_handle **handle, void ***out_exit_code); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_wait_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_wait_pre[hIndex].func; + retVal___ = preHookFunc(&handle, &out_exit_code); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.wait(handle, out_exit_code); + } + if( HPMHooks.count.HP_thread_wait_post ) { + bool (*postHookFunc) (bool retVal___, struct thread_handle *handle, void **out_exit_code); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_wait_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_wait_post[hIndex].func; + retVal___ = postHookFunc(retVal___, handle, out_exit_code); + } + } + return retVal___; +} +void HP_thread_prio_set(struct thread_handle *handle, enum thread_priority prio) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_prio_set_pre ) { + void (*preHookFunc) (struct thread_handle **handle, enum thread_priority *prio); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_set_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_prio_set_pre[hIndex].func; + preHookFunc(&handle, &prio); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.prio_set(handle, prio); + } + if( HPMHooks.count.HP_thread_prio_set_post ) { + void (*postHookFunc) (struct thread_handle *handle, enum thread_priority prio); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_set_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_prio_set_post[hIndex].func; + postHookFunc(handle, prio); + } + } + return; +} +enum thread_priority HP_thread_prio_get(struct thread_handle *handle) { + int hIndex = 0; + enum thread_priority retVal___ = THREADPRIO_NORMAL; + if( HPMHooks.count.HP_thread_prio_get_pre ) { + enum thread_priority (*preHookFunc) (struct thread_handle **handle); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_get_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_prio_get_pre[hIndex].func; + retVal___ = preHookFunc(&handle); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.prio_get(handle); + } + if( HPMHooks.count.HP_thread_prio_get_post ) { + enum thread_priority (*postHookFunc) (enum thread_priority retVal___, struct thread_handle *handle); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_get_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_prio_get_post[hIndex].func; + retVal___ = postHookFunc(retVal___, handle); + } + } + return retVal___; +} +void HP_thread_yield(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_yield_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_yield_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_yield_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.yield(); + } + if( HPMHooks.count.HP_thread_yield_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_yield_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_yield_post[hIndex].func; + postHookFunc(); + } + } + return; +} /* timer_interface */ int64 HP_timer_gettick(void) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_char.sources.inc b/src/plugins/HPMHooking/HPMHooking_char.sources.inc index f1180dea8..54ae8f030 100644 --- a/src/plugins/HPMHooking/HPMHooking_char.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_char.sources.inc @@ -61,4 +61,5 @@ memcpy(&HPMHooks.source.StrBuf, StrBuf, sizeof(struct stringbuf_interface)); memcpy(&HPMHooks.source.strlib, strlib, sizeof(struct strlib_interface)); memcpy(&HPMHooks.source.sv, sv, sizeof(struct sv_interface)); memcpy(&HPMHooks.source.sysinfo, sysinfo, sizeof(struct sysinfo_interface)); +memcpy(&HPMHooks.source.thread, thread, sizeof(struct thread_interface)); memcpy(&HPMHooks.source.timer, timer, sizeof(struct timer_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc index 91d2304d8..b97b6ea7f 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HPMHooksCore.inc @@ -676,6 +676,28 @@ struct { struct HPMHookPoint *HP_sysinfo_init_post; struct HPMHookPoint *HP_sysinfo_final_pre; struct HPMHookPoint *HP_sysinfo_final_post; + struct HPMHookPoint *HP_thread_init_pre; + struct HPMHookPoint *HP_thread_init_post; + struct HPMHookPoint *HP_thread_final_pre; + struct HPMHookPoint *HP_thread_final_post; + struct HPMHookPoint *HP_thread_create_pre; + struct HPMHookPoint *HP_thread_create_post; + struct HPMHookPoint *HP_thread_create_opt_pre; + struct HPMHookPoint *HP_thread_create_opt_post; + struct HPMHookPoint *HP_thread_destroy_pre; + struct HPMHookPoint *HP_thread_destroy_post; + struct HPMHookPoint *HP_thread_self_pre; + struct HPMHookPoint *HP_thread_self_post; + struct HPMHookPoint *HP_thread_get_tid_pre; + struct HPMHookPoint *HP_thread_get_tid_post; + struct HPMHookPoint *HP_thread_wait_pre; + struct HPMHookPoint *HP_thread_wait_post; + struct HPMHookPoint *HP_thread_prio_set_pre; + struct HPMHookPoint *HP_thread_prio_set_post; + struct HPMHookPoint *HP_thread_prio_get_pre; + struct HPMHookPoint *HP_thread_prio_get_post; + struct HPMHookPoint *HP_thread_yield_pre; + struct HPMHookPoint *HP_thread_yield_post; struct HPMHookPoint *HP_timer_gettick_pre; struct HPMHookPoint *HP_timer_gettick_post; struct HPMHookPoint *HP_timer_gettick_nocache_pre; @@ -1355,6 +1377,28 @@ struct { int HP_sysinfo_init_post; int HP_sysinfo_final_pre; int HP_sysinfo_final_post; + int HP_thread_init_pre; + int HP_thread_init_post; + int HP_thread_final_pre; + int HP_thread_final_post; + int HP_thread_create_pre; + int HP_thread_create_post; + int HP_thread_create_opt_pre; + int HP_thread_create_opt_post; + int HP_thread_destroy_pre; + int HP_thread_destroy_post; + int HP_thread_self_pre; + int HP_thread_self_post; + int HP_thread_get_tid_pre; + int HP_thread_get_tid_post; + int HP_thread_wait_pre; + int HP_thread_wait_post; + int HP_thread_prio_set_pre; + int HP_thread_prio_set_post; + int HP_thread_prio_get_pre; + int HP_thread_prio_get_post; + int HP_thread_yield_pre; + int HP_thread_yield_post; int HP_timer_gettick_pre; int HP_timer_gettick_post; int HP_timer_gettick_nocache_pre; @@ -1406,5 +1450,6 @@ struct { struct strlib_interface strlib; struct sv_interface sv; struct sysinfo_interface sysinfo; + struct thread_interface thread; struct timer_interface timer; } source; diff --git a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc index 48e376287..2d3e37bc3 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.HookingPoints.inc @@ -373,6 +373,18 @@ struct HookingPointData HookingPoints[] = { { HP_POP(sysinfo->is_superuser, HP_sysinfo_is_superuser) }, { HP_POP(sysinfo->init, HP_sysinfo_init) }, { HP_POP(sysinfo->final, HP_sysinfo_final) }, +/* thread_interface */ + { HP_POP(thread->init, HP_thread_init) }, + { HP_POP(thread->final, HP_thread_final) }, + { HP_POP(thread->create, HP_thread_create) }, + { HP_POP(thread->create_opt, HP_thread_create_opt) }, + { HP_POP(thread->destroy, HP_thread_destroy) }, + { HP_POP(thread->self, HP_thread_self) }, + { HP_POP(thread->get_tid, HP_thread_get_tid) }, + { HP_POP(thread->wait, HP_thread_wait) }, + { HP_POP(thread->prio_set, HP_thread_prio_set) }, + { HP_POP(thread->prio_get, HP_thread_prio_get) }, + { HP_POP(thread->yield, HP_thread_yield) }, /* timer_interface */ { HP_POP(timer->gettick, HP_timer_gettick) }, { HP_POP(timer->gettick_nocache, HP_timer_gettick_nocache) }, diff --git a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc index e176c8488..013a56104 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.Hooks.inc @@ -8756,6 +8756,299 @@ void HP_sysinfo_final(void) { } return; } +/* thread_interface */ +void HP_thread_init(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_init_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_init_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_init_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.init(); + } + if( HPMHooks.count.HP_thread_init_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_init_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_init_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_thread_final(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_final_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_final_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_final_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.final(); + } + if( HPMHooks.count.HP_thread_final_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_final_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_final_post[hIndex].func; + postHookFunc(); + } + } + return; +} +struct thread_handle* HP_thread_create(threadFunc entry_point, void *param) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_create_pre ) { + struct thread_handle* (*preHookFunc) (threadFunc *entry_point, void **param); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_create_pre[hIndex].func; + retVal___ = preHookFunc(&entry_point, ¶m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.create(entry_point, param); + } + if( HPMHooks.count.HP_thread_create_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___, threadFunc entry_point, void *param); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___, entry_point, param); + } + } + return retVal___; +} +struct thread_handle* HP_thread_create_opt(threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_create_opt_pre ) { + struct thread_handle* (*preHookFunc) (threadFunc *entry_point, void **param, size_t *stack_size, enum thread_priority *prio); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_opt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_create_opt_pre[hIndex].func; + retVal___ = preHookFunc(&entry_point, ¶m, &stack_size, &prio); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.create_opt(entry_point, param, stack_size, prio); + } + if( HPMHooks.count.HP_thread_create_opt_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___, threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_opt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_create_opt_post[hIndex].func; + retVal___ = postHookFunc(retVal___, entry_point, param, stack_size, prio); + } + } + return retVal___; +} +void HP_thread_destroy(struct thread_handle *handle) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_destroy_pre ) { + void (*preHookFunc) (struct thread_handle **handle); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_destroy_pre[hIndex].func; + preHookFunc(&handle); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.destroy(handle); + } + if( HPMHooks.count.HP_thread_destroy_post ) { + void (*postHookFunc) (struct thread_handle *handle); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_destroy_post[hIndex].func; + postHookFunc(handle); + } + } + return; +} +struct thread_handle* HP_thread_self(void) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_self_pre ) { + struct thread_handle* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_self_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_self_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.self(); + } + if( HPMHooks.count.HP_thread_self_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_self_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_self_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +int HP_thread_get_tid(void) { + int hIndex = 0; + int retVal___ = 0; + if( HPMHooks.count.HP_thread_get_tid_pre ) { + int (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_get_tid_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_get_tid_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.get_tid(); + } + if( HPMHooks.count.HP_thread_get_tid_post ) { + int (*postHookFunc) (int retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_get_tid_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_get_tid_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +bool HP_thread_wait(struct thread_handle *handle, void **out_exit_code) { + int hIndex = 0; + bool retVal___ = false; + if( HPMHooks.count.HP_thread_wait_pre ) { + bool (*preHookFunc) (struct thread_handle **handle, void ***out_exit_code); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_wait_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_wait_pre[hIndex].func; + retVal___ = preHookFunc(&handle, &out_exit_code); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.wait(handle, out_exit_code); + } + if( HPMHooks.count.HP_thread_wait_post ) { + bool (*postHookFunc) (bool retVal___, struct thread_handle *handle, void **out_exit_code); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_wait_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_wait_post[hIndex].func; + retVal___ = postHookFunc(retVal___, handle, out_exit_code); + } + } + return retVal___; +} +void HP_thread_prio_set(struct thread_handle *handle, enum thread_priority prio) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_prio_set_pre ) { + void (*preHookFunc) (struct thread_handle **handle, enum thread_priority *prio); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_set_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_prio_set_pre[hIndex].func; + preHookFunc(&handle, &prio); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.prio_set(handle, prio); + } + if( HPMHooks.count.HP_thread_prio_set_post ) { + void (*postHookFunc) (struct thread_handle *handle, enum thread_priority prio); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_set_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_prio_set_post[hIndex].func; + postHookFunc(handle, prio); + } + } + return; +} +enum thread_priority HP_thread_prio_get(struct thread_handle *handle) { + int hIndex = 0; + enum thread_priority retVal___ = THREADPRIO_NORMAL; + if( HPMHooks.count.HP_thread_prio_get_pre ) { + enum thread_priority (*preHookFunc) (struct thread_handle **handle); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_get_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_prio_get_pre[hIndex].func; + retVal___ = preHookFunc(&handle); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.prio_get(handle); + } + if( HPMHooks.count.HP_thread_prio_get_post ) { + enum thread_priority (*postHookFunc) (enum thread_priority retVal___, struct thread_handle *handle); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_get_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_prio_get_post[hIndex].func; + retVal___ = postHookFunc(retVal___, handle); + } + } + return retVal___; +} +void HP_thread_yield(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_yield_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_yield_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_yield_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.yield(); + } + if( HPMHooks.count.HP_thread_yield_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_yield_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_yield_post[hIndex].func; + postHookFunc(); + } + } + return; +} /* timer_interface */ int64 HP_timer_gettick(void) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_login.sources.inc b/src/plugins/HPMHooking/HPMHooking_login.sources.inc index ddd1206ca..6677a0c55 100644 --- a/src/plugins/HPMHooking/HPMHooking_login.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_login.sources.inc @@ -47,4 +47,5 @@ memcpy(&HPMHooks.source.StrBuf, StrBuf, sizeof(struct stringbuf_interface)); memcpy(&HPMHooks.source.strlib, strlib, sizeof(struct strlib_interface)); memcpy(&HPMHooks.source.sv, sv, sizeof(struct sv_interface)); memcpy(&HPMHooks.source.sysinfo, sysinfo, sizeof(struct sysinfo_interface)); +memcpy(&HPMHooks.source.thread, thread, sizeof(struct thread_interface)); memcpy(&HPMHooks.source.timer, timer, sizeof(struct timer_interface)); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index e3276fdf6..1f46a1c0f 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -5868,6 +5868,28 @@ struct { struct HPMHookPoint *HP_sysinfo_init_post; struct HPMHookPoint *HP_sysinfo_final_pre; struct HPMHookPoint *HP_sysinfo_final_post; + struct HPMHookPoint *HP_thread_init_pre; + struct HPMHookPoint *HP_thread_init_post; + struct HPMHookPoint *HP_thread_final_pre; + struct HPMHookPoint *HP_thread_final_post; + struct HPMHookPoint *HP_thread_create_pre; + struct HPMHookPoint *HP_thread_create_post; + struct HPMHookPoint *HP_thread_create_opt_pre; + struct HPMHookPoint *HP_thread_create_opt_post; + struct HPMHookPoint *HP_thread_destroy_pre; + struct HPMHookPoint *HP_thread_destroy_post; + struct HPMHookPoint *HP_thread_self_pre; + struct HPMHookPoint *HP_thread_self_post; + struct HPMHookPoint *HP_thread_get_tid_pre; + struct HPMHookPoint *HP_thread_get_tid_post; + struct HPMHookPoint *HP_thread_wait_pre; + struct HPMHookPoint *HP_thread_wait_post; + struct HPMHookPoint *HP_thread_prio_set_pre; + struct HPMHookPoint *HP_thread_prio_set_post; + struct HPMHookPoint *HP_thread_prio_get_pre; + struct HPMHookPoint *HP_thread_prio_get_post; + struct HPMHookPoint *HP_thread_yield_pre; + struct HPMHookPoint *HP_thread_yield_post; struct HPMHookPoint *HP_timer_gettick_pre; struct HPMHookPoint *HP_timer_gettick_post; struct HPMHookPoint *HP_timer_gettick_nocache_pre; @@ -11871,6 +11893,28 @@ struct { int HP_sysinfo_init_post; int HP_sysinfo_final_pre; int HP_sysinfo_final_post; + int HP_thread_init_pre; + int HP_thread_init_post; + int HP_thread_final_pre; + int HP_thread_final_post; + int HP_thread_create_pre; + int HP_thread_create_post; + int HP_thread_create_opt_pre; + int HP_thread_create_opt_post; + int HP_thread_destroy_pre; + int HP_thread_destroy_post; + int HP_thread_self_pre; + int HP_thread_self_post; + int HP_thread_get_tid_pre; + int HP_thread_get_tid_post; + int HP_thread_wait_pre; + int HP_thread_wait_post; + int HP_thread_prio_set_pre; + int HP_thread_prio_set_post; + int HP_thread_prio_get_pre; + int HP_thread_prio_get_post; + int HP_thread_yield_pre; + int HP_thread_yield_post; int HP_timer_gettick_pre; int HP_timer_gettick_post; int HP_timer_gettick_nocache_pre; @@ -12090,6 +12134,7 @@ struct { struct strlib_interface strlib; struct sv_interface sv; struct sysinfo_interface sysinfo; + struct thread_interface thread; struct timer_interface timer; struct trade_interface trade; struct unit_interface unit; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index 4f103df81..6d7b62cff 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -3005,6 +3005,18 @@ struct HookingPointData HookingPoints[] = { { HP_POP(sysinfo->is_superuser, HP_sysinfo_is_superuser) }, { HP_POP(sysinfo->init, HP_sysinfo_init) }, { HP_POP(sysinfo->final, HP_sysinfo_final) }, +/* thread_interface */ + { HP_POP(thread->init, HP_thread_init) }, + { HP_POP(thread->final, HP_thread_final) }, + { HP_POP(thread->create, HP_thread_create) }, + { HP_POP(thread->create_opt, HP_thread_create_opt) }, + { HP_POP(thread->destroy, HP_thread_destroy) }, + { HP_POP(thread->self, HP_thread_self) }, + { HP_POP(thread->get_tid, HP_thread_get_tid) }, + { HP_POP(thread->wait, HP_thread_wait) }, + { HP_POP(thread->prio_set, HP_thread_prio_set) }, + { HP_POP(thread->prio_get, HP_thread_prio_get) }, + { HP_POP(thread->yield, HP_thread_yield) }, /* timer_interface */ { HP_POP(timer->gettick, HP_timer_gettick) }, { HP_POP(timer->gettick_nocache, HP_timer_gettick_nocache) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index 949928058..ae813a9cb 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -78627,6 +78627,299 @@ void HP_sysinfo_final(void) { } return; } +/* thread_interface */ +void HP_thread_init(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_init_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_init_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_init_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.init(); + } + if( HPMHooks.count.HP_thread_init_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_init_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_init_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void HP_thread_final(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_final_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_final_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_final_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.final(); + } + if( HPMHooks.count.HP_thread_final_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_final_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_final_post[hIndex].func; + postHookFunc(); + } + } + return; +} +struct thread_handle* HP_thread_create(threadFunc entry_point, void *param) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_create_pre ) { + struct thread_handle* (*preHookFunc) (threadFunc *entry_point, void **param); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_create_pre[hIndex].func; + retVal___ = preHookFunc(&entry_point, ¶m); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.create(entry_point, param); + } + if( HPMHooks.count.HP_thread_create_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___, threadFunc entry_point, void *param); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_create_post[hIndex].func; + retVal___ = postHookFunc(retVal___, entry_point, param); + } + } + return retVal___; +} +struct thread_handle* HP_thread_create_opt(threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_create_opt_pre ) { + struct thread_handle* (*preHookFunc) (threadFunc *entry_point, void **param, size_t *stack_size, enum thread_priority *prio); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_opt_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_create_opt_pre[hIndex].func; + retVal___ = preHookFunc(&entry_point, ¶m, &stack_size, &prio); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.create_opt(entry_point, param, stack_size, prio); + } + if( HPMHooks.count.HP_thread_create_opt_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___, threadFunc entry_point, void *param, size_t stack_size, enum thread_priority prio); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_create_opt_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_create_opt_post[hIndex].func; + retVal___ = postHookFunc(retVal___, entry_point, param, stack_size, prio); + } + } + return retVal___; +} +void HP_thread_destroy(struct thread_handle *handle) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_destroy_pre ) { + void (*preHookFunc) (struct thread_handle **handle); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_destroy_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_destroy_pre[hIndex].func; + preHookFunc(&handle); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.destroy(handle); + } + if( HPMHooks.count.HP_thread_destroy_post ) { + void (*postHookFunc) (struct thread_handle *handle); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_destroy_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_destroy_post[hIndex].func; + postHookFunc(handle); + } + } + return; +} +struct thread_handle* HP_thread_self(void) { + int hIndex = 0; + struct thread_handle* retVal___ = NULL; + if( HPMHooks.count.HP_thread_self_pre ) { + struct thread_handle* (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_self_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_self_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.self(); + } + if( HPMHooks.count.HP_thread_self_post ) { + struct thread_handle* (*postHookFunc) (struct thread_handle* retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_self_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_self_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +int HP_thread_get_tid(void) { + int hIndex = 0; + int retVal___ = 0; + if( HPMHooks.count.HP_thread_get_tid_pre ) { + int (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_get_tid_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_get_tid_pre[hIndex].func; + retVal___ = preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.get_tid(); + } + if( HPMHooks.count.HP_thread_get_tid_post ) { + int (*postHookFunc) (int retVal___); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_get_tid_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_get_tid_post[hIndex].func; + retVal___ = postHookFunc(retVal___); + } + } + return retVal___; +} +bool HP_thread_wait(struct thread_handle *handle, void **out_exit_code) { + int hIndex = 0; + bool retVal___ = false; + if( HPMHooks.count.HP_thread_wait_pre ) { + bool (*preHookFunc) (struct thread_handle **handle, void ***out_exit_code); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_wait_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_wait_pre[hIndex].func; + retVal___ = preHookFunc(&handle, &out_exit_code); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.wait(handle, out_exit_code); + } + if( HPMHooks.count.HP_thread_wait_post ) { + bool (*postHookFunc) (bool retVal___, struct thread_handle *handle, void **out_exit_code); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_wait_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_wait_post[hIndex].func; + retVal___ = postHookFunc(retVal___, handle, out_exit_code); + } + } + return retVal___; +} +void HP_thread_prio_set(struct thread_handle *handle, enum thread_priority prio) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_prio_set_pre ) { + void (*preHookFunc) (struct thread_handle **handle, enum thread_priority *prio); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_set_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_prio_set_pre[hIndex].func; + preHookFunc(&handle, &prio); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.prio_set(handle, prio); + } + if( HPMHooks.count.HP_thread_prio_set_post ) { + void (*postHookFunc) (struct thread_handle *handle, enum thread_priority prio); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_set_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_prio_set_post[hIndex].func; + postHookFunc(handle, prio); + } + } + return; +} +enum thread_priority HP_thread_prio_get(struct thread_handle *handle) { + int hIndex = 0; + enum thread_priority retVal___ = THREADPRIO_NORMAL; + if( HPMHooks.count.HP_thread_prio_get_pre ) { + enum thread_priority (*preHookFunc) (struct thread_handle **handle); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_get_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_prio_get_pre[hIndex].func; + retVal___ = preHookFunc(&handle); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.thread.prio_get(handle); + } + if( HPMHooks.count.HP_thread_prio_get_post ) { + enum thread_priority (*postHookFunc) (enum thread_priority retVal___, struct thread_handle *handle); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_prio_get_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_prio_get_post[hIndex].func; + retVal___ = postHookFunc(retVal___, handle); + } + } + return retVal___; +} +void HP_thread_yield(void) { + int hIndex = 0; + if( HPMHooks.count.HP_thread_yield_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_yield_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_thread_yield_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.thread.yield(); + } + if( HPMHooks.count.HP_thread_yield_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_thread_yield_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_thread_yield_post[hIndex].func; + postHookFunc(); + } + } + return; +} /* timer_interface */ int64 HP_timer_gettick(void) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_map.sources.inc b/src/plugins/HPMHooking/HPMHooking_map.sources.inc index 87cf0c3ed..0df5aee53 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.sources.inc @@ -83,6 +83,7 @@ memcpy(&HPMHooks.source.StrBuf, StrBuf, sizeof(struct stringbuf_interface)); memcpy(&HPMHooks.source.strlib, strlib, sizeof(struct strlib_interface)); memcpy(&HPMHooks.source.sv, sv, sizeof(struct sv_interface)); memcpy(&HPMHooks.source.sysinfo, sysinfo, sizeof(struct sysinfo_interface)); +memcpy(&HPMHooks.source.thread, thread, sizeof(struct thread_interface)); memcpy(&HPMHooks.source.timer, timer, sizeof(struct timer_interface)); memcpy(&HPMHooks.source.trade, trade, sizeof(struct trade_interface)); memcpy(&HPMHooks.source.unit, unit, sizeof(struct unit_interface)); -- 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(-) 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(-) 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(-) 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 From 14c9f5228fec75fc4e9bc8552e3fbae16f356103 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 14 Mar 2016 01:30:43 +0100 Subject: HPM Hooks Update Signed-off-by: Haru --- src/common/HPMDataCheck.h | 5 + src/common/HPMSymbols.inc.h | 6 + src/plugins/HPMHooking/HPMHooking.Defs.inc | 16 ++ .../HPMHooking/HPMHooking_map.HPMHooksCore.inc | 29 ++++ .../HPMHooking/HPMHooking_map.HookingPoints.inc | 8 + src/plugins/HPMHooking/HPMHooking_map.Hooks.inc | 188 +++++++++++++++++++++ src/plugins/HPMHooking/HPMHooking_map.sources.inc | 1 + 7 files changed, 253 insertions(+) diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h index 55ea1e1b9..d0e23811c 100644 --- a/src/common/HPMDataCheck.h +++ b/src/common/HPMDataCheck.h @@ -160,6 +160,11 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = { #else #define COMMON_ERS_H #endif // COMMON_ERS_H + #ifdef COMMON_GRFIO_H + { "grfio_interface", sizeof(struct grfio_interface), SERVER_TYPE_MAP }, + #else + #define COMMON_GRFIO_H + #endif // COMMON_GRFIO_H #ifdef COMMON_HPMI_H { "HPMi_interface", sizeof(struct HPMi_interface), SERVER_TYPE_ALL }, { "hplugin_info", sizeof(struct hplugin_info), SERVER_TYPE_ALL }, diff --git a/src/common/HPMSymbols.inc.h b/src/common/HPMSymbols.inc.h index 0a2e64cb4..7ccb960de 100644 --- a/src/common/HPMSymbols.inc.h +++ b/src/common/HPMSymbols.inc.h @@ -80,6 +80,9 @@ struct elemental_interface *elemental; #ifdef CHAR_GEOIP_H /* geoip */ struct geoip_interface *geoip; #endif // CHAR_GEOIP_H +#ifdef COMMON_GRFIO_H /* grfio */ +struct grfio_interface *grfio; +#endif // COMMON_GRFIO_H #ifdef MAP_GUILD_H /* guild */ struct guild_interface *guild; #endif // MAP_GUILD_H @@ -327,6 +330,9 @@ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("elemental", elemental)) retu #ifdef CHAR_GEOIP_H /* geoip */ if ((server_type&(SERVER_TYPE_CHAR)) && !HPM_SYMBOL("geoip", geoip)) return "geoip"; #endif // CHAR_GEOIP_H +#ifdef COMMON_GRFIO_H /* grfio */ +if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("grfio", grfio)) return "grfio"; +#endif // COMMON_GRFIO_H #ifdef MAP_GUILD_H /* guild */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("guild", guild)) return "guild"; #endif // MAP_GUILD_H diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index 2ac33f6f7..4f033ccaf 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -2371,6 +2371,22 @@ typedef void (*HPMHOOK_post_geoip_final) (bool shutdown); typedef void (*HPMHOOK_pre_geoip_init) (void); typedef void (*HPMHOOK_post_geoip_init) (void); #endif // CHAR_GEOIP_H +#ifdef COMMON_GRFIO_H /* grfio */ +typedef void (*HPMHOOK_pre_grfio_init) (const char **fname); +typedef void (*HPMHOOK_post_grfio_init) (const char *fname); +typedef void (*HPMHOOK_pre_grfio_final) (void); +typedef void (*HPMHOOK_post_grfio_final) (void); +typedef void* (*HPMHOOK_pre_grfio_reads) (const char **fname, int **size); +typedef void* (*HPMHOOK_post_grfio_reads) (void* retVal___, const char *fname, int *size); +typedef const char* (*HPMHOOK_pre_grfio_find_file) (const char **fname); +typedef const char* (*HPMHOOK_post_grfio_find_file) (const char* retVal___, const char *fname); +typedef unsigned long (*HPMHOOK_pre_grfio_crc32) (const unsigned char **buf, unsigned int *len); +typedef unsigned long (*HPMHOOK_post_grfio_crc32) (unsigned long retVal___, const unsigned char *buf, unsigned int len); +typedef int (*HPMHOOK_pre_grfio_decode_zip) (void **dest, unsigned long **dest_len, const void **source, unsigned long *source_len); +typedef int (*HPMHOOK_post_grfio_decode_zip) (int retVal___, void *dest, unsigned long *dest_len, const void *source, unsigned long source_len); +typedef int (*HPMHOOK_pre_grfio_encode_zip) (void **dest, unsigned long **dest_len, const void **source, unsigned long *source_len); +typedef int (*HPMHOOK_post_grfio_encode_zip) (int retVal___, void *dest, unsigned long *dest_len, const void *source, unsigned long source_len); +#endif // COMMON_GRFIO_H #ifdef MAP_GUILD_H /* guild */ typedef void (*HPMHOOK_pre_guild_init) (bool *minimal); typedef void (*HPMHOOK_post_guild_init) (bool minimal); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index 1f46a1c0f..7131f486c 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -2014,6 +2014,20 @@ struct { struct HPMHookPoint *HP_elemental_ai_timer_post; struct HPMHookPoint *HP_elemental_read_db_pre; struct HPMHookPoint *HP_elemental_read_db_post; + struct HPMHookPoint *HP_grfio_init_pre; + struct HPMHookPoint *HP_grfio_init_post; + struct HPMHookPoint *HP_grfio_final_pre; + struct HPMHookPoint *HP_grfio_final_post; + struct HPMHookPoint *HP_grfio_reads_pre; + struct HPMHookPoint *HP_grfio_reads_post; + struct HPMHookPoint *HP_grfio_find_file_pre; + struct HPMHookPoint *HP_grfio_find_file_post; + struct HPMHookPoint *HP_grfio_crc32_pre; + struct HPMHookPoint *HP_grfio_crc32_post; + struct HPMHookPoint *HP_grfio_decode_zip_pre; + struct HPMHookPoint *HP_grfio_decode_zip_post; + struct HPMHookPoint *HP_grfio_encode_zip_pre; + struct HPMHookPoint *HP_grfio_encode_zip_post; struct HPMHookPoint *HP_guild_init_pre; struct HPMHookPoint *HP_guild_init_post; struct HPMHookPoint *HP_guild_final_pre; @@ -8039,6 +8053,20 @@ struct { int HP_elemental_ai_timer_post; int HP_elemental_read_db_pre; int HP_elemental_read_db_post; + int HP_grfio_init_pre; + int HP_grfio_init_post; + int HP_grfio_final_pre; + int HP_grfio_final_post; + int HP_grfio_reads_pre; + int HP_grfio_reads_post; + int HP_grfio_find_file_pre; + int HP_grfio_find_file_post; + int HP_grfio_crc32_pre; + int HP_grfio_crc32_post; + int HP_grfio_decode_zip_pre; + int HP_grfio_decode_zip_post; + int HP_grfio_encode_zip_pre; + int HP_grfio_encode_zip_post; int HP_guild_init_pre; int HP_guild_init_post; int HP_guild_final_pre; @@ -12092,6 +12120,7 @@ struct { struct des_interface des; struct duel_interface duel; struct elemental_interface elemental; + struct grfio_interface grfio; struct guild_interface guild; struct guild_storage_interface gstorage; struct homunculus_interface homun; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index 6d7b62cff..cbf7ba79f 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -1036,6 +1036,14 @@ struct HookingPointData HookingPoints[] = { { HP_POP(elemental->ai_sub_foreachclient, HP_elemental_ai_sub_foreachclient) }, { HP_POP(elemental->ai_timer, HP_elemental_ai_timer) }, { HP_POP(elemental->read_db, HP_elemental_read_db) }, +/* grfio_interface */ + { HP_POP(grfio->init, HP_grfio_init) }, + { HP_POP(grfio->final, HP_grfio_final) }, + { HP_POP(grfio->reads, HP_grfio_reads) }, + { HP_POP(grfio->find_file, HP_grfio_find_file) }, + { HP_POP(grfio->crc32, HP_grfio_crc32) }, + { HP_POP(grfio->decode_zip, HP_grfio_decode_zip) }, + { HP_POP(grfio->encode_zip, HP_grfio_encode_zip) }, /* guild_interface */ { HP_POP(guild->init, HP_guild_init) }, { HP_POP(guild->final, HP_guild_final) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index ae813a9cb..2317f9392 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -26270,6 +26270,194 @@ int HP_elemental_read_db(void) { } return retVal___; } +/* grfio_interface */ +void HP_grfio_init(const char *fname) { + int hIndex = 0; + if( HPMHooks.count.HP_grfio_init_pre ) { + void (*preHookFunc) (const char **fname); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_init_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_grfio_init_pre[hIndex].func; + preHookFunc(&fname); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.grfio.init(fname); + } + if( HPMHooks.count.HP_grfio_init_post ) { + void (*postHookFunc) (const char *fname); + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_init_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_grfio_init_post[hIndex].func; + postHookFunc(fname); + } + } + return; +} +void HP_grfio_final(void) { + int hIndex = 0; + if( HPMHooks.count.HP_grfio_final_pre ) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_final_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_grfio_final_pre[hIndex].func; + preHookFunc(); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.grfio.final(); + } + if( HPMHooks.count.HP_grfio_final_post ) { + void (*postHookFunc) (void); + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_final_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_grfio_final_post[hIndex].func; + postHookFunc(); + } + } + return; +} +void* HP_grfio_reads(const char *fname, int *size) { + int hIndex = 0; + void* retVal___ = NULL; + if( HPMHooks.count.HP_grfio_reads_pre ) { + void* (*preHookFunc) (const char **fname, int **size); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_reads_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_grfio_reads_pre[hIndex].func; + retVal___ = preHookFunc(&fname, &size); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.grfio.reads(fname, size); + } + if( HPMHooks.count.HP_grfio_reads_post ) { + void* (*postHookFunc) (void* retVal___, const char *fname, int *size); + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_reads_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_grfio_reads_post[hIndex].func; + retVal___ = postHookFunc(retVal___, fname, size); + } + } + return retVal___; +} +const char* HP_grfio_find_file(const char *fname) { + int hIndex = 0; + const char* retVal___ = NULL; + if( HPMHooks.count.HP_grfio_find_file_pre ) { + const char* (*preHookFunc) (const char **fname); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_find_file_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_grfio_find_file_pre[hIndex].func; + retVal___ = preHookFunc(&fname); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.grfio.find_file(fname); + } + if( HPMHooks.count.HP_grfio_find_file_post ) { + const char* (*postHookFunc) (const char* retVal___, const char *fname); + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_find_file_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_grfio_find_file_post[hIndex].func; + retVal___ = postHookFunc(retVal___, fname); + } + } + return retVal___; +} +unsigned long HP_grfio_crc32(const unsigned char *buf, unsigned int len) { + int hIndex = 0; + unsigned long retVal___ = 0; + if( HPMHooks.count.HP_grfio_crc32_pre ) { + unsigned long (*preHookFunc) (const unsigned char **buf, unsigned int *len); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_crc32_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_grfio_crc32_pre[hIndex].func; + retVal___ = preHookFunc(&buf, &len); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.grfio.crc32(buf, len); + } + if( HPMHooks.count.HP_grfio_crc32_post ) { + unsigned long (*postHookFunc) (unsigned long retVal___, const unsigned char *buf, unsigned int len); + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_crc32_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_grfio_crc32_post[hIndex].func; + retVal___ = postHookFunc(retVal___, buf, len); + } + } + return retVal___; +} +int HP_grfio_decode_zip(void *dest, unsigned long *dest_len, const void *source, unsigned long source_len) { + int hIndex = 0; + int retVal___ = 0; + if( HPMHooks.count.HP_grfio_decode_zip_pre ) { + int (*preHookFunc) (void **dest, unsigned long **dest_len, const void **source, unsigned long *source_len); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_decode_zip_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_grfio_decode_zip_pre[hIndex].func; + retVal___ = preHookFunc(&dest, &dest_len, &source, &source_len); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.grfio.decode_zip(dest, dest_len, source, source_len); + } + if( HPMHooks.count.HP_grfio_decode_zip_post ) { + int (*postHookFunc) (int retVal___, void *dest, unsigned long *dest_len, const void *source, unsigned long source_len); + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_decode_zip_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_grfio_decode_zip_post[hIndex].func; + retVal___ = postHookFunc(retVal___, dest, dest_len, source, source_len); + } + } + return retVal___; +} +int HP_grfio_encode_zip(void *dest, unsigned long *dest_len, const void *source, unsigned long source_len) { + int hIndex = 0; + int retVal___ = 0; + if( HPMHooks.count.HP_grfio_encode_zip_pre ) { + int (*preHookFunc) (void **dest, unsigned long **dest_len, const void **source, unsigned long *source_len); + *HPMforce_return = false; + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_encode_zip_pre; hIndex++ ) { + preHookFunc = HPMHooks.list.HP_grfio_encode_zip_pre[hIndex].func; + retVal___ = preHookFunc(&dest, &dest_len, &source, &source_len); + } + if( *HPMforce_return ) { + *HPMforce_return = false; + return retVal___; + } + } + { + retVal___ = HPMHooks.source.grfio.encode_zip(dest, dest_len, source, source_len); + } + if( HPMHooks.count.HP_grfio_encode_zip_post ) { + int (*postHookFunc) (int retVal___, void *dest, unsigned long *dest_len, const void *source, unsigned long source_len); + for(hIndex = 0; hIndex < HPMHooks.count.HP_grfio_encode_zip_post; hIndex++ ) { + postHookFunc = HPMHooks.list.HP_grfio_encode_zip_post[hIndex].func; + retVal___ = postHookFunc(retVal___, dest, dest_len, source, source_len); + } + } + return retVal___; +} /* guild_interface */ void HP_guild_init(bool minimal) { int hIndex = 0; diff --git a/src/plugins/HPMHooking/HPMHooking_map.sources.inc b/src/plugins/HPMHooking/HPMHooking_map.sources.inc index 0df5aee53..54861ed6f 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.sources.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.sources.inc @@ -41,6 +41,7 @@ memcpy(&HPMHooks.source.DB, DB, sizeof(struct db_interface)); memcpy(&HPMHooks.source.des, des, sizeof(struct des_interface)); memcpy(&HPMHooks.source.duel, duel, sizeof(struct duel_interface)); memcpy(&HPMHooks.source.elemental, elemental, sizeof(struct elemental_interface)); +memcpy(&HPMHooks.source.grfio, grfio, sizeof(struct grfio_interface)); memcpy(&HPMHooks.source.guild, guild, sizeof(struct guild_interface)); memcpy(&HPMHooks.source.gstorage, gstorage, sizeof(struct guild_storage_interface)); memcpy(&HPMHooks.source.homun, homun, sizeof(struct homunculus_interface)); -- cgit v1.2.3-70-g09d2