From a9326b175d42b9f6448d97b52108be907194854c Mon Sep 17 00:00:00 2001 From: Haru Date: Thu, 7 Jan 2016 03:24:06 +0100 Subject: Added const qualifier to the {RFIFO,RBUF}{B,W,L,Q} families A read FIFO/buffer should be, well, read-only. Signed-off-by: Haru --- src/common/socket.h | 18 +++++++++--------- src/map/clif.c | 4 +++- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/common/socket.h b/src/common/socket.h index e1ea94f5a..29f3eb3d7 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -50,13 +50,13 @@ struct config_setting_t; #define RFIFOP(fd,pos) (sockt->session[fd]->rdata + sockt->session[fd]->rdata_pos + (pos)) #define WFIFOP(fd,pos) (sockt->session[fd]->wdata + sockt->session[fd]->wdata_size + (pos)) -#define RFIFOB(fd,pos) (*(uint8*)RFIFOP((fd),(pos))) +#define RFIFOB(fd,pos) (*(const uint8*)RFIFOP((fd),(pos))) #define WFIFOB(fd,pos) (*(uint8*)WFIFOP((fd),(pos))) -#define RFIFOW(fd,pos) (*(uint16*)RFIFOP((fd),(pos))) +#define RFIFOW(fd,pos) (*(const uint16*)RFIFOP((fd),(pos))) #define WFIFOW(fd,pos) (*(uint16*)WFIFOP((fd),(pos))) -#define RFIFOL(fd,pos) (*(uint32*)RFIFOP((fd),(pos))) +#define RFIFOL(fd,pos) (*(const uint32*)RFIFOP((fd),(pos))) #define WFIFOL(fd,pos) (*(uint32*)WFIFOP((fd),(pos))) -#define RFIFOQ(fd,pos) (*(uint64*)RFIFOP((fd),(pos))) +#define RFIFOQ(fd,pos) (*(const uint64*)RFIFOP((fd),(pos))) #define WFIFOQ(fd,pos) (*(uint64*)WFIFOP((fd),(pos))) #define RFIFOSPACE(fd) (sockt->session[fd]->max_rdata - sockt->session[fd]->rdata_size) #define WFIFOSPACE(fd) (sockt->session[fd]->max_wdata - sockt->session[fd]->wdata_size) @@ -85,11 +85,11 @@ struct config_setting_t; #define WP2PTR(fd) WFIFO2PTR(fd) // buffer I/O macros -#define RBUFP(p,pos) (((const uint8*)(p)) + (pos)) -#define RBUFB(p,pos) (*(const uint8*)RBUFP((p),(pos))) -#define RBUFW(p,pos) (*(const uint16*)RBUFP((p),(pos))) -#define RBUFL(p,pos) (*(const uint32*)RBUFP((p),(pos))) -#define RBUFQ(p,pos) (*(const uint64*)RBUFP((p),(pos))) +#define RBUFP(p,pos) (((const uint8 *)(p)) + (pos)) +#define RBUFB(p,pos) (*(const uint8 *)RBUFP((p),(pos))) +#define RBUFW(p,pos) (*(const uint16 *)RBUFP((p),(pos))) +#define RBUFL(p,pos) (*(const uint32 *)RBUFP((p),(pos))) +#define RBUFQ(p,pos) (*(const uint64 *)RBUFP((p),(pos))) #define WBUFP(p,pos) (((uint8*)(p)) + (pos)) #define WBUFB(p,pos) (*(uint8*)WBUFP((p),(pos))) diff --git a/src/map/clif.c b/src/map/clif.c index a7d73d91f..a7d7dca97 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -18787,7 +18787,9 @@ int clif_parse(int fd) { return 0; // not enough data received to form the packet if( battle_config.packet_obfuscation == 2 || cmd != RFIFOW(fd, 0) || (sd && sd->parse_cmd_func == clif_parse_cmd_decrypt) ) { - RFIFOW(fd, 0) = cmd; + // Note: Overriding const qualifier to re-inject the decoded packet ID. + int16 *packet_id = (int16 *)RFIFOP(fd, 0); + *packet_id = cmd; if( sd ) { sd->cryptKey = (( sd->cryptKey * clif->cryptKey[1] ) + clif->cryptKey[2]) & 0xFFFFFFFF; // Update key for the next packet } -- cgit v1.2.3-70-g09d2 From d736b2ef87d2b331f2e8fca9c42784d481778a94 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 22 Feb 2016 00:52:53 +0100 Subject: Split mapif->elemental_save() into two functions (save and create) Signed-off-by: Haru --- src/char/int_elemental.c | 69 ++++++++++++++++++++++++++++++++---------------- src/char/mapif.c | 4 ++- src/char/mapif.h | 3 ++- 3 files changed, 51 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/char/int_elemental.c b/src/char/int_elemental.c index 82208d3f5..8d868bc06 100644 --- a/src/char/int_elemental.c +++ b/src/char/int_elemental.c @@ -40,32 +40,54 @@ struct inter_elemental_interface inter_elemental_s; struct inter_elemental_interface *inter_elemental; -bool mapif_elemental_save(struct s_elemental* ele) { - bool flag = true; - +/** + * Creates a new elemental with the given data. + * + * @remark + * The elemental ID is expected to be 0, and will be filled with the newly + * assigned ID. + * + * @param[in,out] ele The new elemental's data. + * @retval false in case of errors. + */ +bool mapif_elemental_create(struct s_elemental *ele) +{ nullpo_retr(false, ele); - if( ele->elemental_id == 0 ) { // Create new DB entry - if( SQL_ERROR == SQL->Query(inter->sql_handle, + Assert_retr(false, ele->elemental_id == 0); + + if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` (`char_id`,`class`,`mode`,`hp`,`sp`,`max_hp`,`max_sp`,`atk1`,`atk2`,`matk`,`aspd`,`def`,`mdef`,`flee`,`hit`,`life_time`)" "VALUES ('%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d')", - elemental_db, ele->char_id, ele->class_, ele->mode, ele->hp, ele->sp, ele->max_hp, ele->max_sp, ele->atk, ele->atk2, ele->matk, ele->amotion, ele->def, ele->mdef, ele->flee, ele->hit, ele->life_time) ) - { - Sql_ShowDebug(inter->sql_handle); - flag = false; - } - else - ele->elemental_id = (int)SQL->LastInsertId(inter->sql_handle); - } else if( SQL_ERROR == SQL->Query(inter->sql_handle, - "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `mode` = '%d', `hp` = '%d', `sp` = '%d'," - "`max_hp` = '%d', `max_sp` = '%d', `atk1` = '%d', `atk2` = '%d', `matk` = '%d', `aspd` = '%d', `def` = '%d'," - "`mdef` = '%d', `flee` = '%d', `hit` = '%d', `life_time` = '%d' WHERE `ele_id` = '%d'", - elemental_db, ele->char_id, ele->class_, ele->mode, ele->hp, ele->sp, ele->max_hp, ele->max_sp, ele->atk, ele->atk2, - ele->matk, ele->amotion, ele->def, ele->mdef, ele->flee, ele->hit, ele->life_time, ele->elemental_id) ) - { // Update DB entry + elemental_db, ele->char_id, ele->class_, ele->mode, ele->hp, ele->sp, ele->max_hp, ele->max_sp, ele->atk, + ele->atk2, ele->matk, ele->amotion, ele->def, ele->mdef, ele->flee, ele->hit, ele->life_time)) { + Sql_ShowDebug(inter->sql_handle); + return false; + } + ele->elemental_id = (int)SQL->LastInsertId(inter->sql_handle); + return true; +} + +/** + * Saves an existing elemental. + * + * @param ele The elemental's data. + * @retval false in case of errors. + */ +bool mapif_elemental_save(const struct s_elemental *ele) +{ + nullpo_retr(false, ele); + Assert_retr(false, ele->elemental_id > 0); + + if (SQL_ERROR == SQL->Query(inter->sql_handle, + "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `mode` = '%d', `hp` = '%d', `sp` = '%d'," + "`max_hp` = '%d', `max_sp` = '%d', `atk1` = '%d', `atk2` = '%d', `matk` = '%d', `aspd` = '%d', `def` = '%d'," + "`mdef` = '%d', `flee` = '%d', `hit` = '%d', `life_time` = '%d' WHERE `ele_id` = '%d'", + elemental_db, ele->char_id, ele->class_, ele->mode, ele->hp, ele->sp, ele->max_hp, ele->max_sp, ele->atk, ele->atk2, + ele->matk, ele->amotion, ele->def, ele->mdef, ele->flee, ele->hit, ele->life_time, ele->elemental_id)) { Sql_ShowDebug(inter->sql_handle); - flag = false; + return false; } - return flag; + return true; } bool mapif_elemental_load(int ele_id, int char_id, struct s_elemental *ele) { @@ -133,8 +155,9 @@ void mapif_elemental_send(int fd, struct s_elemental *ele, unsigned char flag) { WFIFOSET(fd,size); } -void mapif_parse_elemental_create(int fd, struct s_elemental* ele) { - bool result = mapif->elemental_save(ele); +void mapif_parse_elemental_create(int fd, struct s_elemental *ele) +{ + bool result = mapif->elemental_create(ele); mapif->elemental_send(fd, ele, result); } diff --git a/src/char/mapif.c b/src/char/mapif.c index 5ba687a77..4edcb6027 100644 --- a/src/char/mapif.c +++ b/src/char/mapif.c @@ -57,7 +57,8 @@ void mapif_auction_close(int fd, int char_id, unsigned char result); void mapif_parse_auction_close(int fd); void mapif_auction_bid(int fd, int char_id, int bid, unsigned char result); void mapif_parse_auction_bid(int fd); -bool mapif_elemental_save(struct s_elemental* ele); +bool mapif_elemental_create(struct s_elemental *ele); +bool mapif_elemental_save(const struct s_elemental *ele); bool mapif_elemental_load(int ele_id, int char_id, struct s_elemental *ele); bool mapif_elemental_delete(int ele_id); void mapif_elemental_send(int fd, struct s_elemental *ele, unsigned char flag); @@ -232,6 +233,7 @@ void mapif_defaults(void) { mapif->parse_auction_close = mapif_parse_auction_close; mapif->auction_bid = mapif_auction_bid; mapif->parse_auction_bid = mapif_parse_auction_bid; + mapif->elemental_create = mapif_elemental_create; mapif->elemental_save = mapif_elemental_save; mapif->elemental_load = mapif_elemental_load; mapif->elemental_delete = mapif_elemental_delete; diff --git a/src/char/mapif.h b/src/char/mapif.h index eb5d30b2c..e5767bf05 100644 --- a/src/char/mapif.h +++ b/src/char/mapif.h @@ -51,7 +51,8 @@ struct mapif_interface { void (*parse_auction_close) (int fd); void (*auction_bid) (int fd, int char_id, int bid, unsigned char result); void (*parse_auction_bid) (int fd); - bool (*elemental_save) (struct s_elemental* ele); + bool (*elemental_create) (struct s_elemental *ele); + bool (*elemental_save) (const struct s_elemental *ele); bool (*elemental_load) (int ele_id, int char_id, struct s_elemental *ele); bool (*elemental_delete) (int ele_id); void (*elemental_send) (int fd, struct s_elemental *ele, unsigned char flag); -- cgit v1.2.3-70-g09d2 From 95e6e3e2437dafcaa270a3891821d13a54107c94 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 22 Feb 2016 01:03:59 +0100 Subject: Split mapif->homunculus_save() into two functions (save and create) Signed-off-by: Haru --- src/char/int_homun.c | 97 ++++++++++++++++++++++++++++++---------------------- src/char/mapif.c | 4 ++- src/char/mapif.h | 3 +- 3 files changed, 61 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/char/int_homun.c b/src/char/int_homun.c index d633dc7e0..95374a978 100644 --- a/src/char/int_homun.c +++ b/src/char/int_homun.c @@ -109,64 +109,79 @@ void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char WFIFOSET(fd, NAME_LENGTH+12); } -bool mapif_homunculus_save(struct s_homunculus* hd) +/** + * Creates a new homunculus with the given data. + * + * @remark + * The homunculus ID is expected to be 0, and will be filled with the newly + * assigned ID. + * + * @param[in,out] hd The new homunculus' data. + * @retval false in case of errors. + */ +bool mapif_homunculus_create(struct s_homunculus *hd) { - bool flag = true; char esc_name[NAME_LENGTH*2+1]; - nullpo_ret(hd); + nullpo_retr(false, hd); + Assert_retr(false, hd->hom_id == 0); + SQL->EscapeStringLen(inter->sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH)); - if( hd->hom_id == 0 ) - {// new homunculus - if( SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` " + if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` " "(`char_id`, `class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`) " "VALUES ('%d', '%d', '%d', '%s', '%d', '%u', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk, - hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize) ) - { - Sql_ShowDebug(inter->sql_handle); - flag = false; - } - else - { - hd->hom_id = (int)SQL->LastInsertId(inter->sql_handle); - } + hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize)) { + Sql_ShowDebug(inter->sql_handle); + return false; } - else - { - if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'", + hd->hom_id = (int)SQL->LastInsertId(inter->sql_handle); + return true; +} + +/** + * Saves an existing homunculus. + * + * @param hd The homunculus' data. + * @retval false in case of errors. + */ +bool mapif_homunculus_save(const struct s_homunculus *hd) +{ + bool flag = true; + char esc_name[NAME_LENGTH*2+1]; + + nullpo_retr(false, hd); + Assert_retr(false, hd->hom_id > 0); + + SQL->EscapeStringLen(inter->sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH)); + + if (SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'", homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk, - hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id) ) - { - Sql_ShowDebug(inter->sql_handle); + hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id)) { + Sql_ShowDebug(inter->sql_handle); + flag = false; + } else { + int i; + SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle); + + if (SQL_ERROR == SQL->StmtPrepare(stmt, "REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", skill_homunculus_db, hd->hom_id)) { + SqlStmt_ShowDebug(stmt); flag = false; - } - else - { - SqlStmt* stmt; - int i; - - stmt = SQL->StmtMalloc(inter->sql_handle); - if( SQL_ERROR == SQL->StmtPrepare(stmt, "REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", skill_homunculus_db, hd->hom_id) ) - SqlStmt_ShowDebug(stmt); - for( i = 0; i < MAX_HOMUNSKILL; ++i ) - { - if( hd->hskill[i].id > 0 && hd->hskill[i].lv != 0 ) - { - SQL->StmtBindParam(stmt, 0, SQLDT_USHORT, &hd->hskill[i].id, 0); - SQL->StmtBindParam(stmt, 1, SQLDT_USHORT, &hd->hskill[i].lv, 0); - if( SQL_ERROR == SQL->StmtExecute(stmt) ) - { + } else { + for (i = 0; i < MAX_HOMUNSKILL; ++i) { + if (hd->hskill[i].id > 0 && hd->hskill[i].lv != 0) { + SQL->StmtBindParam(stmt, 0, SQLDT_USHORT, (void*)&hd->hskill[i].id, 0); // FIXME: StmtBindParam should take const void + SQL->StmtBindParam(stmt, 1, SQLDT_USHORT, (void*)&hd->hskill[i].lv, 0); // FIXME: StmtBindParam should take const void + if (SQL_ERROR == SQL->StmtExecute(stmt)) { SqlStmt_ShowDebug(stmt); - SQL->StmtFree(stmt); flag = false; break; } } } - SQL->StmtFree(stmt); } + SQL->StmtFree(stmt); } return flag; @@ -289,7 +304,7 @@ bool mapif_homunculus_rename(char *name) void mapif_parse_homunculus_create(int fd, int len, int account_id, struct s_homunculus* phd) { - bool result = mapif->homunculus_save(phd); + bool result = mapif->homunculus_create(phd); mapif->homunculus_created(fd, account_id, phd, result); } diff --git a/src/char/mapif.c b/src/char/mapif.c index 4edcb6027..dc8e34e36 100644 --- a/src/char/mapif.c +++ b/src/char/mapif.c @@ -108,7 +108,8 @@ void mapif_homunculus_deleted(int fd, int flag); void mapif_homunculus_loaded(int fd, int account_id, struct s_homunculus *hd); void mapif_homunculus_saved(int fd, int account_id, bool flag); void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, char* name); -bool mapif_homunculus_save(struct s_homunculus* hd); +bool mapif_homunculus_create(struct s_homunculus *hd); +bool mapif_homunculus_save(const struct s_homunculus *hd); bool mapif_homunculus_load(int homun_id, struct s_homunculus* hd); bool mapif_homunculus_delete(int homun_id); bool mapif_homunculus_rename(char *name); @@ -284,6 +285,7 @@ void mapif_defaults(void) { mapif->homunculus_loaded = mapif_homunculus_loaded; mapif->homunculus_saved = mapif_homunculus_saved; mapif->homunculus_renamed = mapif_homunculus_renamed; + mapif->homunculus_create = mapif_homunculus_create; mapif->homunculus_save = mapif_homunculus_save; mapif->homunculus_load = mapif_homunculus_load; mapif->homunculus_delete = mapif_homunculus_delete; diff --git a/src/char/mapif.h b/src/char/mapif.h index e5767bf05..05bddabb4 100644 --- a/src/char/mapif.h +++ b/src/char/mapif.h @@ -102,7 +102,8 @@ struct mapif_interface { void (*homunculus_loaded) (int fd, int account_id, struct s_homunculus *hd); void (*homunculus_saved) (int fd, int account_id, bool flag); void (*homunculus_renamed) (int fd, int account_id, int char_id, unsigned char flag, char* name); - bool (*homunculus_save) (struct s_homunculus* hd); + bool (*homunculus_create) (struct s_homunculus *hd); + bool (*homunculus_save) (const struct s_homunculus *hd); bool (*homunculus_load) (int homun_id, struct s_homunculus* hd); bool (*homunculus_delete) (int homun_id); bool (*homunculus_rename) (char *name); -- cgit v1.2.3-70-g09d2 From 1a0ad427708a010992321efbcdca90150b9e3f8b Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 22 Feb 2016 01:09:54 +0100 Subject: Split mapif->mercenary_save() into two functions (save and create) Signed-off-by: Haru --- src/char/int_mercenary.c | 59 ++++++++++++++++++++++++++++++++---------------- src/char/mapif.c | 4 +++- src/char/mapif.h | 3 ++- 3 files changed, 44 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/char/int_mercenary.c b/src/char/int_mercenary.c index 2ed45305c..133dcf565 100644 --- a/src/char/int_mercenary.c +++ b/src/char/int_mercenary.c @@ -94,32 +94,51 @@ bool inter_mercenary_owner_delete(int char_id) return true; } -bool mapif_mercenary_save(struct s_mercenary* merc) +/** + * Creates a new mercenary with the given data. + * + * @remark + * The mercenary ID is expected to be 0, and will be filled with the newly + * assigned ID. + * + * @param[in,out] merc The new mercenary's data. + * @retval false in case of errors. + */ +bool mapif_mercenary_create(struct s_mercenary *merc) { - bool flag = true; + nullpo_retr(false, merc); + Assert_retr(false, merc->mercenary_id == 0); - nullpo_ret(merc); - if( merc->mercenary_id == 0 ) - { // Create new DB entry - if( SQL_ERROR == SQL->Query(inter->sql_handle, + if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` (`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time`) VALUES ('%d','%d','%d','%d','%u','%u')", - mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time) ) - { - Sql_ShowDebug(inter->sql_handle); - flag = false; - } - else - merc->mercenary_id = (int)SQL->LastInsertId(inter->sql_handle); + mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time)) { + Sql_ShowDebug(inter->sql_handle); + return false; } - else if( SQL_ERROR == SQL->Query(inter->sql_handle, - "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%d', `sp` = '%d', `kill_counter` = '%u', `life_time` = '%u' WHERE `mer_id` = '%d'", - mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id) ) - { // Update DB entry + merc->mercenary_id = (int)SQL->LastInsertId(inter->sql_handle); + + return true; +} + +/** + * Saves an existing mercenary. + * + * @param merc The mercenary's data. + * @retval false in case of errors. + */ +bool mapif_mercenary_save(const struct s_mercenary *merc) +{ + nullpo_retr(false, merc); + Assert_retr(false, merc->mercenary_id > 0); + + if (SQL_ERROR == SQL->Query(inter->sql_handle, + "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%d', `sp` = '%d', `kill_counter` = '%u', `life_time` = '%u' WHERE `mer_id` = '%d'", + mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id)) { Sql_ShowDebug(inter->sql_handle); - flag = false; + return false; } - return flag; + return true; } bool mapif_mercenary_load(int merc_id, int char_id, struct s_mercenary *merc) @@ -181,7 +200,7 @@ void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag) void mapif_parse_mercenary_create(int fd, struct s_mercenary* merc) { - bool result = mapif->mercenary_save(merc); + bool result = mapif->mercenary_create(merc); mapif->mercenary_send(fd, merc, result); } diff --git a/src/char/mapif.c b/src/char/mapif.c index dc8e34e36..f25df2369 100644 --- a/src/char/mapif.c +++ b/src/char/mapif.c @@ -131,7 +131,8 @@ void mapif_mail_return(int fd, int char_id, int mail_id, int new_mail); void mapif_parse_mail_return(int fd); void mapif_mail_send(int fd, struct mail_message* msg); void mapif_parse_mail_send(int fd); -bool mapif_mercenary_save(struct s_mercenary* merc); +bool mapif_mercenary_create(struct s_mercenary *merc); +bool mapif_mercenary_save(const struct s_mercenary *merc); bool mapif_mercenary_load(int merc_id, int char_id, struct s_mercenary *merc); bool mapif_mercenary_delete(int merc_id); void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag); @@ -308,6 +309,7 @@ void mapif_defaults(void) { mapif->parse_mail_return = mapif_parse_mail_return; mapif->mail_send = mapif_mail_send; mapif->parse_mail_send = mapif_parse_mail_send; + mapif->mercenary_create = mapif_mercenary_create; mapif->mercenary_save = mapif_mercenary_save; mapif->mercenary_load = mapif_mercenary_load; mapif->mercenary_delete = mapif_mercenary_delete; diff --git a/src/char/mapif.h b/src/char/mapif.h index 05bddabb4..d4fbcb945 100644 --- a/src/char/mapif.h +++ b/src/char/mapif.h @@ -125,7 +125,8 @@ struct mapif_interface { void (*parse_mail_return) (int fd); void (*mail_send) (int fd, struct mail_message* msg); void (*parse_mail_send) (int fd); - bool (*mercenary_save) (struct s_mercenary* merc); + bool (*mercenary_create) (struct s_mercenary *merc); + bool (*mercenary_save) (const struct s_mercenary *merc); bool (*mercenary_load) (int merc_id, int char_id, struct s_mercenary *merc); bool (*mercenary_delete) (int merc_id); void (*mercenary_send) (int fd, struct s_mercenary *merc, unsigned char flag); -- cgit v1.2.3-70-g09d2 From 7fd9e649fbcdef4abe7e9e77c24871e485262ca3 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 22 Feb 2016 01:26:28 +0100 Subject: Updated inter_pet->tosql() and mapif->save_pet() to work with const data Signed-off-by: Haru --- src/char/int_pet.c | 71 ++++++++++++++++++++++++++++-------------------------- src/char/int_pet.h | 2 +- src/char/mapif.c | 2 +- src/char/mapif.h | 2 +- 4 files changed, 40 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index 6baf832bc..fcf71b255 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -40,44 +40,55 @@ struct inter_pet_interface inter_pet_s; struct inter_pet_interface *inter_pet; -//--------------------------------------------------------- -int inter_pet_tosql(int pet_id, struct s_pet* p) +/** + * Saves a pet to the SQL database. + * + * @remark + * In case of newly created pet, the pet ID is not updated to reflect the + * newly assigned ID. The caller must do so. + * + * @param p The pet data to save. + * @return The ID of the saved pet. + * @retval 0 in case of errors. + */ +int inter_pet_tosql(const struct s_pet *p) { //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`) char esc_name[NAME_LENGTH*2+1];// escaped pet name + int pet_id = 0, hungry = 0, intimate = 0; nullpo_ret(p); + SQL->EscapeStringLen(inter->sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH)); - p->hungry = cap_value(p->hungry, 0, 100); - p->intimate = cap_value(p->intimate, 0, 1000); - - if( pet_id == -1 ) - {// New pet. - if( SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` " - "(`class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`) " - "VALUES ('%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", - pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id, - p->equip, p->intimate, p->hungry, p->rename_flag, p->incubate) ) - { + hungry = cap_value(p->hungry, 0, 100); + intimate = cap_value(p->intimate, 0, 1000); + + if (p->pet_id == 0) { + // New pet. + if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` " + "(`class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`) " + "VALUES ('%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", + pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id, + p->equip, intimate, hungry, p->rename_flag, p->incubate)) { Sql_ShowDebug(inter->sql_handle); return 0; } - p->pet_id = (int)SQL->LastInsertId(inter->sql_handle); - } - else - {// Update pet. - if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `class`='%d',`name`='%s',`account_id`='%d',`char_id`='%d',`level`='%d',`egg_id`='%d',`equip`='%d',`intimate`='%d',`hungry`='%d',`rename_flag`='%d',`incubate`='%d' WHERE `pet_id`='%d'", - pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id, - p->equip, p->intimate, p->hungry, p->rename_flag, p->incubate, p->pet_id) ) - { + pet_id = (int)SQL->LastInsertId(inter->sql_handle); + } else { + // Update pet. + if (SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `class`='%d',`name`='%s',`account_id`='%d',`char_id`='%d',`level`='%d',`egg_id`='%d',`equip`='%d',`intimate`='%d',`hungry`='%d',`rename_flag`='%d',`incubate`='%d' WHERE `pet_id`='%d'", + pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id, + p->equip, intimate, hungry, p->rename_flag, p->incubate, p->pet_id)) { Sql_ShowDebug(inter->sql_handle); return 0; } + pet_id = p->pet_id; } if (save_log) ShowInfo("Pet saved %d - %s.\n", pet_id, p->name); - return 1; + + return pet_id; } int inter_pet_fromsql(int pet_id, struct s_pet* p) @@ -240,8 +251,8 @@ int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short else if(inter_pet->pt->intimate > 1000) inter_pet->pt->intimate = 1000; - inter_pet->pt->pet_id = -1; //Signal NEW pet. - if (inter_pet->tosql(inter_pet->pt->pet_id,inter_pet->pt)) + inter_pet->pt->pet_id = 0; //Signal NEW pet. + if ((inter_pet->pt->pet_id = inter_pet->tosql(inter_pet->pt)) != 0) mapif->pet_created(fd, account_id, inter_pet->pt); else //Failed... mapif->pet_created(fd, account_id, NULL); @@ -271,7 +282,7 @@ int mapif_load_pet(int fd, int account_id, int char_id, int pet_id) return 0; } -int mapif_save_pet(int fd, int account_id, struct s_pet *data) +int mapif_save_pet(int fd, int account_id, const struct s_pet *data) { //here process pet save request. int len; @@ -283,15 +294,7 @@ int mapif_save_pet(int fd, int account_id, struct s_pet *data) return 0; } - if (data->hungry < 0) - data->hungry = 0; - else if (data->hungry > 100) - data->hungry = 100; - if (data->intimate < 0) - data->intimate = 0; - else if (data->intimate > 1000) - data->intimate = 1000; - inter_pet->tosql(data->pet_id,data); + inter_pet->tosql(data); mapif->save_pet_ack(fd, account_id, 0); return 0; diff --git a/src/char/int_pet.h b/src/char/int_pet.h index 07a273eac..3eb19d656 100644 --- a/src/char/int_pet.h +++ b/src/char/int_pet.h @@ -30,7 +30,7 @@ struct s_pet; **/ struct inter_pet_interface { struct s_pet *pt; - int (*tosql) (int pet_id, struct s_pet* p); + int (*tosql) (const struct s_pet *p); int (*fromsql) (int pet_id, struct s_pet* p); int (*sql_init) (void); void (*sql_final) (void); diff --git a/src/char/mapif.c b/src/char/mapif.c index f25df2369..9e37ff528 100644 --- a/src/char/mapif.c +++ b/src/char/mapif.c @@ -168,7 +168,7 @@ int mapif_delete_pet_ack(int fd, int flag); int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id, short pet_equip, short intimate, short hungry, char rename_flag, char incubate, char *pet_name); int mapif_load_pet(int fd, int account_id, int char_id, int pet_id); -int mapif_save_pet(int fd, int account_id, struct s_pet *data); +int mapif_save_pet(int fd, int account_id, const struct s_pet *data); int mapif_delete_pet(int fd, int pet_id); int mapif_parse_CreatePet(int fd); int mapif_parse_LoadPet(int fd); diff --git a/src/char/mapif.h b/src/char/mapif.h index d4fbcb945..6b8afd4b2 100644 --- a/src/char/mapif.h +++ b/src/char/mapif.h @@ -162,7 +162,7 @@ struct mapif_interface { int (*create_pet) (int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id, short pet_equip, short intimate, short hungry, char rename_flag, char incubate, char *pet_name); int (*load_pet) (int fd, int account_id, int char_id, int pet_id); - int (*save_pet) (int fd, int account_id, struct s_pet *data); + int (*save_pet) (int fd, int account_id, const struct s_pet *data); int (*delete_pet) (int fd, int pet_id); int (*parse_CreatePet) (int fd); int (*parse_LoadPet) (int fd); -- cgit v1.2.3-70-g09d2 From 931d716e1000b50a66b012815a412619d16fc957 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 22 Feb 2016 20:55:20 +0100 Subject: Implemented aStrndup() - aStrndup() behaves similarly to the POSIX function strdup(). It allocates sufficient memory for a copy of the passed string, copies it, and returns a pointer to the copy. A maximum number of characters is copied (and a NUL terminator is always appended after the end). Signed-off-by: Haru --- src/common/memmgr.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common/memmgr.h | 2 ++ 2 files changed, 65 insertions(+) (limited to 'src') diff --git a/src/common/memmgr.c b/src/common/memmgr.c index 6b01eb846..15e55fbeb 100644 --- a/src/common/memmgr.c +++ b/src/common/memmgr.c @@ -184,6 +184,36 @@ char* aStrdup_(const char *p, const char *file, int line, const char *func) } return ret; } + +/** + * Copies a string to a newly allocated buffer, setting a maximum length. + * + * The string is always NULL-terminated. If the string is longer than `size`, + * then `size` bytes are copied, not including the appended NULL terminator. + * + * @warning + * If malloc is out of memory, throws a fatal error and aborts the program. + * + * @param p the source string to copy. + * @param size The maximum string length to copy. + * @param file @see ALC_MARK. + * @param line @see ALC_MARK. + * @param func @see ALC_MARK. + * @return the copied string. + */ +char *aStrndup_(const char *p, size_t size, const char *file, int line, const char *func) +{ + size_t len = strnlen(p, size); + char *ret = MALLOC(len + 1, file, line, func); + if (ret == NULL) { + ShowFatalError("%s:%d: in func %s: aStrndup error out of memory!\n", file, line, func); + exit(EXIT_FAILURE); + } + memcpy(ret, p, len); + ret[len] = '\0'; + return ret; +} + void aFree_(void *p, const char *file, int line, const char *func) { // ShowMessage("%s:%d: in func %s: aFree %p\n",file,line,func,p); @@ -478,6 +508,37 @@ char *mstrdup_(const char *p, const char *file, int line, const char *func) { } } +/** + * Copies a string to a newly allocated buffer, setting a maximum length. + * + * The string is always NULL-terminated. If the string is longer than `size`, + * then `size` bytes are copied, not including the appended NULL terminator. + * + * @warning + * If malloc is out of memory, throws a fatal error and aborts the program. + * + * @param p the source string to copy. + * @param size The maximum string length to copy. + * @param file @see ALC_MARK. + * @param line @see ALC_MARK. + * @param func @see ALC_MARK. + * @return the copied string. + * @retval NULL if the source string is NULL or in case of error. + */ +char *mstrndup_(const char *p, size_t size, const char *file, int line, const char *func) +{ + if (p == NULL) { + return NULL; + } else { + size_t len = strnlen(p, size); + char *string = iMalloc->malloc(len + 1, file, line, func); + memcpy(string, p, len); + string[len] = '\0'; + return string; + } +} + + void mfree_(void *ptr, const char *file, int line, const char *func) { struct unit_head *head; @@ -947,6 +1008,7 @@ void malloc_defaults(void) { iMalloc->realloc = mrealloc_; iMalloc->reallocz = mreallocz_; iMalloc->astrdup = mstrdup_; + iMalloc->astrndup = mstrndup_; iMalloc->free = mfree_; #else iMalloc->malloc = aMalloc_; @@ -954,6 +1016,7 @@ void malloc_defaults(void) { iMalloc->realloc = aRealloc_; iMalloc->reallocz = aReallocz_;/* not using memory manager huhum o.o perhaps we could still do something about */ iMalloc->astrdup = aStrdup_; + iMalloc->astrndup = aStrndup_; iMalloc->free = aFree_; #endif iMalloc->post_shutdown = NULL; diff --git a/src/common/memmgr.h b/src/common/memmgr.h index 5975f55c4..680947466 100644 --- a/src/common/memmgr.h +++ b/src/common/memmgr.h @@ -52,6 +52,7 @@ # define aRealloc(p,n) (iMalloc->realloc((p),(n),ALC_MARK)) # define aReallocz(p,n) (iMalloc->reallocz((p),(n),ALC_MARK)) # define aStrdup(p) (iMalloc->astrdup((p),ALC_MARK)) +# define aStrndup(p,n) (iMalloc->astrndup((p),(n),ALC_MARK)) # define aFree(p) (iMalloc->free((p),ALC_MARK)) /////////////// Buffer Creation ///////////////// @@ -85,6 +86,7 @@ struct malloc_interface { void* (*realloc)(void *p, size_t size, const char *file, int line, const char *func); void* (*reallocz)(void *p, size_t size, const char *file, int line, const char *func); char* (*astrdup)(const char *p, const char *file, int line, const char *func); + char *(*astrndup)(const char *p, size_t size, const char *file, int line, const char *func); void (*free)(void *p, const char *file, int line, const char *func); /* */ void (*memory_check)(void); -- cgit v1.2.3-70-g09d2 From 9aa8a3b09ee2c491b55259ee433af7f39308ca37 Mon Sep 17 00:00:00 2001 From: Haru Date: Thu, 7 Jan 2016 03:41:22 +0100 Subject: Added const qualifier to several variable/argument pointers - This is necessary for compatibility with a const RFIFOP. Signed-off-by: Haru --- src/char/char.c | 14 +-- src/char/char.h | 4 +- src/char/int_elemental.c | 14 ++- src/char/int_guild.c | 16 ++-- src/char/int_guild.h | 2 +- src/char/int_homun.c | 21 +++-- src/char/int_mercenary.c | 11 ++- src/char/int_party.c | 14 +-- src/char/int_pet.c | 2 +- src/char/int_quest.c | 3 +- src/char/int_storage.c | 2 +- src/char/int_storage.h | 2 +- src/char/inter.c | 6 +- src/char/mapif.c | 50 +++++------ src/char/mapif.h | 50 +++++------ src/login/login.c | 4 +- src/map/chrif.c | 4 +- src/map/clif.c | 229 ++++++++++++++++++++++++++--------------------- src/map/clif.h | 4 +- src/map/elemental.c | 3 +- src/map/elemental.h | 2 +- src/map/guild.c | 14 +-- src/map/guild.h | 12 +-- src/map/homunculus.c | 36 +++++--- src/map/homunculus.h | 8 +- src/map/intif.c | 24 ++--- src/map/intif.h | 6 +- src/map/irc-bot.c | 17 ++-- src/map/mercenary.c | 3 +- src/map/mercenary.h | 2 +- src/map/party.c | 11 +-- src/map/party.h | 8 +- src/map/pc.c | 3 +- src/map/pc.h | 2 +- src/map/pet.c | 14 +-- src/map/pet.h | 4 +- 36 files changed, 347 insertions(+), 274 deletions(-) (limited to 'src') diff --git a/src/char/char.c b/src/char/char.c index e8709c5b9..1e477ba6e 100644 --- a/src/char/char.c +++ b/src/char/char.c @@ -1657,7 +1657,8 @@ int char_check_char_name(char * name, char * esc_name) * -5: 'Symbols in Character Names are forbidden' * char_id: Success **/ -int char_make_new_char_sql(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style) { +int char_make_new_char_sql(struct char_session_data *sd, const char *name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style) +{ char name[NAME_LENGTH]; char esc_name[NAME_LENGTH*2+1]; int char_id, flag, k, l; @@ -3289,7 +3290,7 @@ void char_parse_frommap_char_select_req(int fd) } } -void char_change_map_server_ack(int fd, uint8 *data, bool ok) +void char_change_map_server_ack(int fd, const uint8 *data, bool ok) { WFIFOHEAD(fd,30); WFIFOW(fd,0) = 0x2b06; @@ -4971,11 +4972,10 @@ void char_login_map_server_ack(int fd, uint8 flag) void char_parse_char_login_map_server(int fd, uint32 ipl) { - char* l_user = (char*)RFIFOP(fd,2); - char* l_pass = (char*)RFIFOP(fd,26); + char l_user[24], l_pass[24]; int i; - l_user[23] = '\0'; - l_pass[23] = '\0'; + safestrncpy(l_user, (char *)RFIFOP(fd,2), 24); + safestrncpy(l_pass, (char *)RFIFOP(fd,26), 24); ARR_FIND( 0, ARRAYLENGTH(chr->server), i, chr->server[i].fd <= 0 ); if (core->runflag != CHARSERVER_ST_RUNNING || @@ -5286,7 +5286,7 @@ int char_parse_char(int fd) return 0; } -int mapif_sendall(unsigned char *buf, unsigned int len) +int mapif_sendall(const unsigned char *buf, unsigned int len) { int i, c; diff --git a/src/char/char.h b/src/char/char.h index ff46e4353..6d8a9815c 100644 --- a/src/char/char.h +++ b/src/char/char.h @@ -137,7 +137,7 @@ struct char_interface { bool (*char_slotchange) (struct char_session_data *sd, int fd, unsigned short from, unsigned short to); int (*rename_char_sql) (struct char_session_data *sd, int char_id); int (*check_char_name) (char * name, char * esc_name); - int (*make_new_char_sql) (struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style); + int (*make_new_char_sql) (struct char_session_data *sd, const char *name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style); int (*divorce_char_sql) (int partner_id1, int partner_id2); int (*count_users) (void); int (*mmo_char_tobuf) (uint8* buffer, struct mmo_charstatus* p); @@ -189,7 +189,7 @@ struct char_interface { void (*parse_frommap_save_character) (int fd, int id); void (*select_ack) (int fd, int account_id, uint8 flag); void (*parse_frommap_char_select_req) (int fd); - void (*change_map_server_ack) (int fd, uint8 *data, bool ok); + void (*change_map_server_ack) (int fd, const uint8 *data, bool ok); void (*parse_frommap_change_map_server) (int fd); void (*parse_frommap_remove_friend) (int fd); void (*char_name_ack) (int fd, int char_id); diff --git a/src/char/int_elemental.c b/src/char/int_elemental.c index 8d868bc06..70275725d 100644 --- a/src/char/int_elemental.c +++ b/src/char/int_elemental.c @@ -155,10 +155,15 @@ void mapif_elemental_send(int fd, struct s_elemental *ele, unsigned char flag) { WFIFOSET(fd,size); } -void mapif_parse_elemental_create(int fd, struct s_elemental *ele) +void mapif_parse_elemental_create(int fd, const struct s_elemental *ele) { - bool result = mapif->elemental_create(ele); - mapif->elemental_send(fd, ele, result); + struct s_elemental ele_; + bool result; + + memcpy(&ele_, ele, sizeof(ele_)); + + result = mapif->elemental_create(&ele_); + mapif->elemental_send(fd, &ele_, result); } void mapif_parse_elemental_load(int fd, int ele_id, int char_id) { @@ -186,7 +191,8 @@ void mapif_elemental_saved(int fd, unsigned char flag) { WFIFOSET(fd,3); } -void mapif_parse_elemental_save(int fd, struct s_elemental* ele) { +void mapif_parse_elemental_save(int fd, const struct s_elemental *ele) +{ bool result = mapif->elemental_save(ele); mapif->elemental_saved(fd, result); } diff --git a/src/char/int_guild.c b/src/char/int_guild.c index c78b6b04f..04de75a20 100644 --- a/src/char/int_guild.c +++ b/src/char/int_guild.c @@ -767,7 +767,7 @@ void inter_guild_sql_final(void) } // Get guild_id by its name. Returns 0 if not found, -1 on error. -int inter_guild_search_guildname(char *str) +int inter_guild_search_guildname(const char *str) { int guild_id; char esc_name[NAME_LENGTH*2+1]; @@ -1006,7 +1006,7 @@ int mapif_guild_broken(int guild_id, int flag) } // Send guild message -int mapif_guild_message(int guild_id, int account_id, char *mes, int len, int sfd) +int mapif_guild_message(int guild_id, int account_id, const char *mes, int len, int sfd) { unsigned char buf[512]; nullpo_ret(mes); @@ -1139,7 +1139,7 @@ int mapif_guild_master_changed(struct guild *g, int aid, int cid) return 0; } -int mapif_guild_castle_dataload(int fd, int sz, int *castle_ids) +int mapif_guild_castle_dataload(int fd, int sz, const int *castle_ids) { struct guild_castle *gc = NULL; int num = (sz - 4) / sizeof(int); @@ -1163,7 +1163,7 @@ int mapif_guild_castle_dataload(int fd, int sz, int *castle_ids) // Guild creation request -int mapif_parse_CreateGuild(int fd,int account_id,char *name,struct guild_member *master) +int mapif_parse_CreateGuild(int fd, int account_id, const char *name, const struct guild_member *master) { struct guild *g; int i=0; @@ -1259,7 +1259,7 @@ int mapif_parse_GuildInfo(int fd, int guild_id) } // Add member to guild -int mapif_parse_GuildAddMember(int fd, int guild_id, struct guild_member *m) +int mapif_parse_GuildAddMember(int fd, int guild_id, const struct guild_member *m) { struct guild * g; int i; @@ -1455,7 +1455,7 @@ int mapif_parse_BreakGuild(int fd, int guild_id) } // Forward Guild message to others map servers -int mapif_parse_GuildMessage(int fd, int guild_id, int account_id, char *mes, int len) +int mapif_parse_GuildMessage(int fd, int guild_id, int account_id, const char *mes, int len) { return mapif->guild_message(guild_id,account_id,mes,len, fd); } @@ -1670,7 +1670,7 @@ int inter_guild_charname_changed(int guild_id, int account_id, int char_id, char } // Change a position desc -int mapif_parse_GuildPosition(int fd, int guild_id, int idx, struct guild_position *p) +int mapif_parse_GuildPosition(int fd, int guild_id, int idx, const struct guild_position *p) { // Could make some improvement in speed, because only change guild_position struct guild * g; @@ -1809,7 +1809,7 @@ int mapif_parse_GuildEmblem(int fd, int len, int guild_id, int dummy, const char return mapif->guild_emblem(g); } -int mapif_parse_GuildCastleDataLoad(int fd, int len, int *castle_ids) +int mapif_parse_GuildCastleDataLoad(int fd, int len, const int *castle_ids) { return mapif->guild_castle_dataload(fd, len, castle_ids); } diff --git a/src/char/int_guild.h b/src/char/int_guild.h index 1076b3394..40728c3b2 100644 --- a/src/char/int_guild.h +++ b/src/char/int_guild.h @@ -60,7 +60,7 @@ struct inter_guild_interface { int (*sql_init) (void); int (*db_final) (DBKey key, DBData *data, va_list ap); void (*sql_final) (void); - int (*search_guildname) (char *str); + int (*search_guildname) (const char *str); bool (*check_empty) (struct guild *g); unsigned int (*nextexp) (int level); int (*checkskill) (struct guild *g, int id); diff --git a/src/char/int_homun.c b/src/char/int_homun.c index 95374a978..1377bfbd8 100644 --- a/src/char/int_homun.c +++ b/src/char/int_homun.c @@ -49,7 +49,7 @@ void inter_homunculus_sql_final(void) return; } -void mapif_homunculus_created(int fd, int account_id, struct s_homunculus *sh, unsigned char flag) +void mapif_homunculus_created(int fd, int account_id, const struct s_homunculus *sh, unsigned char flag) { nullpo_retv(sh); WFIFOHEAD(fd, sizeof(struct s_homunculus)+9); @@ -97,7 +97,7 @@ void mapif_homunculus_saved(int fd, int account_id, bool flag) WFIFOSET(fd, 7); } -void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, char* name) +void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, const char *name) { nullpo_retv(name); WFIFOHEAD(fd, NAME_LENGTH+12); @@ -279,7 +279,7 @@ bool mapif_homunculus_delete(int homun_id) return true; } -bool mapif_homunculus_rename(char *name) +bool mapif_homunculus_rename(const char *name) { int i; @@ -302,10 +302,15 @@ bool mapif_homunculus_rename(char *name) } -void mapif_parse_homunculus_create(int fd, int len, int account_id, struct s_homunculus* phd) +void mapif_parse_homunculus_create(int fd, int len, int account_id, const struct s_homunculus *phd) { - bool result = mapif->homunculus_create(phd); - mapif->homunculus_created(fd, account_id, phd, result); + struct s_homunculus shd; + bool result; + + memcpy(&shd, phd, sizeof(shd)); + + result = mapif->homunculus_create(&shd); + mapif->homunculus_created(fd, account_id, &shd, result); } void mapif_parse_homunculus_delete(int fd, int homun_id) @@ -321,13 +326,13 @@ void mapif_parse_homunculus_load(int fd, int account_id, int homun_id) mapif->homunculus_loaded(fd, account_id, ( result ? &hd : NULL )); } -void mapif_parse_homunculus_save(int fd, int len, int account_id, struct s_homunculus* phd) +void mapif_parse_homunculus_save(int fd, int len, int account_id, const struct s_homunculus *phd) { bool result = mapif->homunculus_save(phd); mapif->homunculus_saved(fd, account_id, result); } -void mapif_parse_homunculus_rename(int fd, int account_id, int char_id, char* name) +void mapif_parse_homunculus_rename(int fd, int account_id, int char_id, const char *name) { bool result = mapif->homunculus_rename(name); mapif->homunculus_renamed(fd, account_id, char_id, result, name); diff --git a/src/char/int_mercenary.c b/src/char/int_mercenary.c index 133dcf565..671c71278 100644 --- a/src/char/int_mercenary.c +++ b/src/char/int_mercenary.c @@ -198,10 +198,15 @@ void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag) WFIFOSET(fd,size); } -void mapif_parse_mercenary_create(int fd, struct s_mercenary* merc) +void mapif_parse_mercenary_create(int fd, const struct s_mercenary *merc) { - bool result = mapif->mercenary_create(merc); - mapif->mercenary_send(fd, merc, result); + struct s_mercenary merc_; + bool result; + + memcpy(&merc_, merc, sizeof(merc_)); + + result = mapif->mercenary_create(&merc_); + mapif->mercenary_send(fd, &merc_, result); } void mapif_parse_mercenary_load(int fd, int merc_id, int char_id) diff --git a/src/char/int_party.c b/src/char/int_party.c index 71034a17b..8ff9ca3a2 100644 --- a/src/char/int_party.c +++ b/src/char/int_party.c @@ -461,7 +461,7 @@ int mapif_party_broken(int party_id, int flag) } //Remarks in the party -int mapif_party_message(int party_id, int account_id, char *mes, int len, int sfd) +int mapif_party_message(int party_id, int account_id, const char *mes, int len, int sfd) { unsigned char buf[512]; nullpo_ret(mes); @@ -479,7 +479,7 @@ int mapif_party_message(int party_id, int account_id, char *mes, int len, int sf // Create Party -int mapif_parse_CreateParty(int fd, char *name, int item, int item2, struct party_member *leader) +int mapif_parse_CreateParty(int fd, const char *name, int item, int item2, const struct party_member *leader) { struct party_data *p; int i; @@ -493,9 +493,11 @@ int mapif_parse_CreateParty(int fd, char *name, int item, int item2, struct part if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorized for (i = 0; i < NAME_LENGTH && name[i]; i++) if (strchr(char_name_letters, name[i]) == NULL) { - if( name[i] == '"' ) { /* client-special-char */ - normalize_name(name,"\""); + if (name[i] == '"') { /* client-special-char */ + char *newname = aStrndup(name, NAME_LENGTH-1); + normalize_name(newname,"\""); mapif->parse_CreateParty(fd,name,item,item2,leader); + aFree(newname); return 0; } mapif->party_created(fd,leader->account_id,leader->char_id,NULL); @@ -547,7 +549,7 @@ void mapif_parse_PartyInfo(int fd, int party_id, int char_id) } // Add a player to party request -int mapif_parse_PartyAddMember(int fd, int party_id, struct party_member *member) +int mapif_parse_PartyAddMember(int fd, int party_id, const struct party_member *member) { struct party_data *p; int i; @@ -729,7 +731,7 @@ int mapif_parse_BreakParty(int fd, int party_id) } //Party sending the message -int mapif_parse_PartyMessage(int fd, int party_id, int account_id, char *mes, int len) +int mapif_parse_PartyMessage(int fd, int party_id, int account_id, const char *mes, int len) { return mapif->party_message(party_id,account_id,mes,len, fd); } diff --git a/src/char/int_pet.c b/src/char/int_pet.c index fcf71b255..676ce6ed6 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -222,7 +222,7 @@ int mapif_delete_pet_ack(int fd, int flag) } int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id, - short pet_equip, short intimate, short hungry, char rename_flag, char incubate, char *pet_name) + short pet_equip, short intimate, short hungry, char rename_flag, char incubate, const char *pet_name) { nullpo_ret(pet_name); memset(inter_pet->pt, 0, sizeof(struct s_pet)); diff --git a/src/char/int_quest.c b/src/char/int_quest.c index de3fbd000..9478b532d 100644 --- a/src/char/int_quest.c +++ b/src/char/int_quest.c @@ -218,7 +218,8 @@ int mapif_parse_quest_save(int fd) { int i, j, k, old_n, new_n = (RFIFOW(fd,2)-8)/sizeof(struct quest); int char_id = RFIFOL(fd,4); - struct quest *old_qd = NULL, *new_qd = NULL; + struct quest *old_qd = NULL; + const struct quest *new_qd = NULL; bool success = true; if (new_n > 0) diff --git a/src/char/int_storage.c b/src/char/int_storage.c index 30647233b..e98f538a2 100644 --- a/src/char/int_storage.c +++ b/src/char/int_storage.c @@ -97,7 +97,7 @@ int inter_storage_fromsql(int account_id, struct storage_data* p) } /// Save guild_storage data to sql -int inter_storage_guild_storage_tosql(int guild_id, struct guild_storage* p) +int inter_storage_guild_storage_tosql(int guild_id, const struct guild_storage *p) { nullpo_ret(p); chr->memitemdata_to_sql(p->items, MAX_GUILD_STORAGE, guild_id, TABLE_GUILD_STORAGE); diff --git a/src/char/int_storage.h b/src/char/int_storage.h index 9bf5b5fd5..3c48c00ec 100644 --- a/src/char/int_storage.h +++ b/src/char/int_storage.h @@ -32,7 +32,7 @@ struct guild_storage; struct inter_storage_interface { int (*tosql) (int account_id, struct storage_data* p); int (*fromsql) (int account_id, struct storage_data* p); - int (*guild_storage_tosql) (int guild_id, struct guild_storage* p); + int (*guild_storage_tosql) (int guild_id, const struct guild_storage *p); int (*guild_storage_fromsql) (int guild_id, struct guild_storage* p); int (*sql_init) (void); void (*sql_final) (void); diff --git a/src/char/inter.c b/src/char/inter.c index 87ecb4e6a..8dec21a77 100644 --- a/src/char/inter.c +++ b/src/char/inter.c @@ -939,7 +939,7 @@ int inter_mapif_init(int fd) //-------------------------------------------------------- // broadcast sending -int mapif_broadcast(unsigned char *mes, int len, unsigned int fontColor, short fontType, short fontSize, short fontAlign, short fontY, int sfd) +int mapif_broadcast(const unsigned char *mes, int len, unsigned int fontColor, short fontType, short fontSize, short fontAlign, short fontY, int sfd) { unsigned char *buf = (unsigned char*)aMalloc((len)*sizeof(unsigned char)); @@ -981,7 +981,7 @@ int mapif_wis_message(struct WisData *wd) return 0; } -void mapif_wis_response(int fd, unsigned char *src, int flag) +void mapif_wis_response(int fd, const unsigned char *src, int flag) { unsigned char buf[27]; nullpo_retv(src); @@ -1261,7 +1261,7 @@ void mapif_namechange_ack(int fd, int account_id, int char_id, int type, int fla int mapif_parse_NameChangeRequest(int fd) { int account_id, char_id, type; - char* name; + const char *name; int i; account_id = RFIFOL(fd,2); diff --git a/src/char/mapif.c b/src/char/mapif.c index 9e37ff528..91bfd884c 100644 --- a/src/char/mapif.c +++ b/src/char/mapif.c @@ -42,7 +42,7 @@ void mapif_server_reset(int id); void mapif_on_disconnect(int id); void mapif_on_parse_accinfo(int account_id, int u_fd, int u_aid, int u_group, int map_fd); void mapif_char_ban(int char_id, time_t timestamp); -int mapif_sendall(unsigned char *buf, unsigned int len); +int mapif_sendall(const unsigned char *buf, unsigned int len); int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len); int mapif_send(int fd, unsigned char *buf, unsigned int len); void mapif_send_users_count(int users); @@ -62,12 +62,12 @@ bool mapif_elemental_save(const struct s_elemental *ele); bool mapif_elemental_load(int ele_id, int char_id, struct s_elemental *ele); bool mapif_elemental_delete(int ele_id); void mapif_elemental_send(int fd, struct s_elemental *ele, unsigned char flag); -void mapif_parse_elemental_create(int fd, struct s_elemental* ele); +void mapif_parse_elemental_create(int fd, const struct s_elemental *ele); void mapif_parse_elemental_load(int fd, int ele_id, int char_id); void mapif_elemental_deleted(int fd, unsigned char flag); void mapif_parse_elemental_delete(int fd, int ele_id); void mapif_elemental_saved(int fd, unsigned char flag); -void mapif_parse_elemental_save(int fd, struct s_elemental* ele); +void mapif_parse_elemental_save(int fd, const struct s_elemental *ele); int mapif_guild_created(int fd, int account_id, struct guild *g); int mapif_guild_noinfo(int fd, int guild_id); int mapif_guild_info(int fd, struct guild *g); @@ -75,7 +75,7 @@ int mapif_guild_memberadded(int fd, int guild_id, int account_id, int char_id, i int mapif_guild_withdraw(int guild_id, int account_id, int char_id, int flag, const char *name, const char *mes); int mapif_guild_memberinfoshort(struct guild *g, int idx); int mapif_guild_broken(int guild_id, int flag); -int mapif_guild_message(int guild_id,int account_id,char *mes,int len, int sfd); +int mapif_guild_message(int guild_id, int account_id, const char *mes, int len, int sfd); int mapif_guild_basicinfochanged(int guild_id, int type, const void *data, int len); int mapif_guild_memberinfochanged(int guild_id, int account_id, int char_id, int type, const void *data, int len); int mapif_guild_skillupack(int guild_id, uint16 skill_id, int account_id); @@ -84,40 +84,40 @@ int mapif_guild_position(struct guild *g, int idx); int mapif_guild_notice(struct guild *g); int mapif_guild_emblem(struct guild *g); int mapif_guild_master_changed(struct guild *g, int aid, int cid); -int mapif_guild_castle_dataload(int fd, int sz, int *castle_ids); -int mapif_parse_CreateGuild(int fd,int account_id,char *name,struct guild_member *master); +int mapif_guild_castle_dataload(int fd, int sz, const int *castle_ids); +int mapif_parse_CreateGuild(int fd, int account_id, const char *name, const struct guild_member *master); int mapif_parse_GuildInfo(int fd, int guild_id); -int mapif_parse_GuildAddMember(int fd, int guild_id, struct guild_member *m); +int mapif_parse_GuildAddMember(int fd, int guild_id, const struct guild_member *m); int mapif_parse_GuildLeave(int fd, int guild_id, int account_id, int char_id, int flag, const char *mes); int mapif_parse_GuildChangeMemberInfoShort(int fd, int guild_id, int account_id, int char_id, int online, int lv, int class_); int mapif_parse_BreakGuild(int fd, int guild_id); -int mapif_parse_GuildMessage(int fd, int guild_id, int account_id, char *mes, int len); +int mapif_parse_GuildMessage(int fd, int guild_id, int account_id, const char *mes, int len); int mapif_parse_GuildBasicInfoChange(int fd, int guild_id, int type, const void *data, int len); int mapif_parse_GuildMemberInfoChange(int fd, int guild_id, int account_id, int char_id, int type, const char *data, int len); -int mapif_parse_GuildPosition(int fd, int guild_id, int idx, struct guild_position *p); +int mapif_parse_GuildPosition(int fd, int guild_id, int idx, const struct guild_position *p); int mapif_parse_GuildSkillUp(int fd, int guild_id, uint16 skill_id, int account_id, int max); int mapif_parse_GuildDeleteAlliance(struct guild *g, int guild_id, int account_id1, int account_id2, int flag); int mapif_parse_GuildAlliance(int fd, int guild_id1, int guild_id2, int account_id1, int account_id2, int flag); int mapif_parse_GuildNotice(int fd, int guild_id, const char *mes1, const char *mes2); int mapif_parse_GuildEmblem(int fd, int len, int guild_id, int dummy, const char *data); -int mapif_parse_GuildCastleDataLoad(int fd, int len, int *castle_ids); +int mapif_parse_GuildCastleDataLoad(int fd, int len, const int *castle_ids); int mapif_parse_GuildCastleDataSave(int fd, int castle_id, int index, int value); int mapif_parse_GuildMasterChange(int fd, int guild_id, const char* name, int len); -void mapif_homunculus_created(int fd, int account_id, struct s_homunculus *sh, unsigned char flag); +void mapif_homunculus_created(int fd, int account_id, const struct s_homunculus *sh, unsigned char flag); void mapif_homunculus_deleted(int fd, int flag); void mapif_homunculus_loaded(int fd, int account_id, struct s_homunculus *hd); void mapif_homunculus_saved(int fd, int account_id, bool flag); -void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, char* name); +void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, const char *name); bool mapif_homunculus_create(struct s_homunculus *hd); bool mapif_homunculus_save(const struct s_homunculus *hd); bool mapif_homunculus_load(int homun_id, struct s_homunculus* hd); bool mapif_homunculus_delete(int homun_id); -bool mapif_homunculus_rename(char *name); -void mapif_parse_homunculus_create(int fd, int len, int account_id, struct s_homunculus* phd); +bool mapif_homunculus_rename(const char *name); +void mapif_parse_homunculus_create(int fd, int len, int account_id, const struct s_homunculus *phd); void mapif_parse_homunculus_delete(int fd, int homun_id); void mapif_parse_homunculus_load(int fd, int account_id, int homun_id); -void mapif_parse_homunculus_save(int fd, int len, int account_id, struct s_homunculus* phd); -void mapif_parse_homunculus_rename(int fd, int account_id, int char_id, char* name); +void mapif_parse_homunculus_save(int fd, int len, int account_id, const struct s_homunculus *phd); +void mapif_parse_homunculus_rename(int fd, int account_id, int char_id, const char *name); void mapif_mail_sendinbox(int fd, int char_id, unsigned char flag, struct mail_data *md); void mapif_parse_mail_requestinbox(int fd); void mapif_parse_mail_read(int fd); @@ -136,12 +136,12 @@ bool mapif_mercenary_save(const struct s_mercenary *merc); bool mapif_mercenary_load(int merc_id, int char_id, struct s_mercenary *merc); bool mapif_mercenary_delete(int merc_id); void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag); -void mapif_parse_mercenary_create(int fd, struct s_mercenary* merc); +void mapif_parse_mercenary_create(int fd, const struct s_mercenary *merc); void mapif_parse_mercenary_load(int fd, int merc_id, int char_id); void mapif_mercenary_deleted(int fd, unsigned char flag); void mapif_parse_mercenary_delete(int fd, int merc_id); void mapif_mercenary_saved(int fd, unsigned char flag); -void mapif_parse_mercenary_save(int fd, struct s_mercenary* merc); +void mapif_parse_mercenary_save(int fd, const struct s_mercenary *merc); int mapif_party_created(int fd, int account_id, int char_id, struct party *p); void mapif_party_noinfo(int fd, int party_id, int char_id); void mapif_party_info(int fd, struct party* p, int char_id); @@ -150,15 +150,15 @@ int mapif_party_optionchanged(int fd, struct party *p, int account_id, int flag) int mapif_party_withdraw(int party_id,int account_id, int char_id); int mapif_party_membermoved(struct party *p, int idx); int mapif_party_broken(int party_id, int flag); -int mapif_party_message(int party_id, int account_id, char *mes, int len, int sfd); -int mapif_parse_CreateParty(int fd, char *name, int item, int item2, struct party_member *leader); +int mapif_party_message(int party_id, int account_id, const char *mes, int len, int sfd); +int mapif_parse_CreateParty(int fd, const char *name, int item, int item2, const struct party_member *leader); void mapif_parse_PartyInfo(int fd, int party_id, int char_id); -int mapif_parse_PartyAddMember(int fd, int party_id, struct party_member *member); +int mapif_parse_PartyAddMember(int fd, int party_id, const struct party_member *member); int mapif_parse_PartyChangeOption(int fd,int party_id,int account_id,int exp,int item); int mapif_parse_PartyLeave(int fd, int party_id, int account_id, int char_id); int mapif_parse_PartyChangeMap(int fd, int party_id, int account_id, int char_id, unsigned short map, int online, unsigned int lv); int mapif_parse_BreakParty(int fd, int party_id); -int mapif_parse_PartyMessage(int fd, int party_id, int account_id, char *mes, int len); +int mapif_parse_PartyMessage(int fd, int party_id, int account_id, const char *mes, int len); int mapif_parse_PartyLeaderChange(int fd, int party_id, int account_id, int char_id); int mapif_pet_created(int fd, int account_id, struct s_pet *p); int mapif_pet_info(int fd, int account_id, struct s_pet *p); @@ -166,7 +166,7 @@ int mapif_pet_noinfo(int fd, int account_id); int mapif_save_pet_ack(int fd, int account_id, int flag); int mapif_delete_pet_ack(int fd, int flag); int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id, - short pet_equip, short intimate, short hungry, char rename_flag, char incubate, char *pet_name); + short pet_equip, short intimate, short hungry, char rename_flag, char incubate, const char *pet_name); int mapif_load_pet(int fd, int account_id, int char_id, int pet_id); int mapif_save_pet(int fd, int account_id, const struct s_pet *data); int mapif_delete_pet(int fd, int pet_id); @@ -192,9 +192,9 @@ void mapif_parse_ItemBoundRetrieve(int fd); void mapif_parse_accinfo(int fd); void mapif_parse_accinfo2(bool success, int map_fd, int u_fd, int u_aid, int account_id, const char *userid, const char *user_pass, const char *email, const char *last_ip, const char *lastlogin, const char *pin_code, const char *birthdate, int group_id, int logincount, int state); -int mapif_broadcast(unsigned char *mes, int len, unsigned int fontColor, short fontType, short fontSize, short fontAlign, short fontY, int sfd); +int mapif_broadcast(const unsigned char *mes, int len, unsigned int fontColor, short fontType, short fontSize, short fontAlign, short fontY, int sfd); int mapif_wis_message(struct WisData *wd); -void mapif_wis_response(int fd, unsigned char *src, int flag); +void mapif_wis_response(int fd, const unsigned char *src, int flag); int mapif_wis_end(struct WisData *wd, int flag); int mapif_account_reg_reply(int fd,int account_id,int char_id, int type); int mapif_disconnectplayer(int fd, int account_id, int char_id, int reason); diff --git a/src/char/mapif.h b/src/char/mapif.h index 6b8afd4b2..37d9444e2 100644 --- a/src/char/mapif.h +++ b/src/char/mapif.h @@ -36,7 +36,7 @@ struct mapif_interface { void (*on_disconnect) (int id); void (*on_parse_accinfo) (int account_id, int u_fd, int u_aid, int u_group, int map_fd); void (*char_ban) (int char_id, time_t timestamp); - int (*sendall) (unsigned char *buf, unsigned int len); + int (*sendall) (const unsigned char *buf, unsigned int len); int (*sendallwos) (int sfd, unsigned char *buf, unsigned int len); int (*send) (int fd, unsigned char *buf, unsigned int len); void (*send_users_count) (int users); @@ -56,12 +56,12 @@ struct mapif_interface { bool (*elemental_load) (int ele_id, int char_id, struct s_elemental *ele); bool (*elemental_delete) (int ele_id); void (*elemental_send) (int fd, struct s_elemental *ele, unsigned char flag); - void (*parse_elemental_create) (int fd, struct s_elemental* ele); + void (*parse_elemental_create) (int fd, const struct s_elemental *ele); void (*parse_elemental_load) (int fd, int ele_id, int char_id); void (*elemental_deleted) (int fd, unsigned char flag); void (*parse_elemental_delete) (int fd, int ele_id); void (*elemental_saved) (int fd, unsigned char flag); - void (*parse_elemental_save) (int fd, struct s_elemental* ele); + void (*parse_elemental_save) (int fd, const struct s_elemental *ele); int (*guild_created) (int fd, int account_id, struct guild *g); int (*guild_noinfo) (int fd, int guild_id); int (*guild_info) (int fd, struct guild *g); @@ -69,7 +69,7 @@ struct mapif_interface { int (*guild_withdraw) (int guild_id, int account_id, int char_id, int flag, const char *name, const char *mes); int (*guild_memberinfoshort) (struct guild *g, int idx); int (*guild_broken) (int guild_id, int flag); - int (*guild_message) (int guild_id,int account_id,char *mes,int len, int sfd); + int (*guild_message) (int guild_id, int account_id, const char *mes, int len, int sfd); int (*guild_basicinfochanged) (int guild_id, int type, const void *data, int len); int (*guild_memberinfochanged) (int guild_id, int account_id, int char_id, int type, const void *data, int len); int (*guild_skillupack) (int guild_id, uint16 skill_id, int account_id); @@ -78,40 +78,40 @@ struct mapif_interface { int (*guild_notice) (struct guild *g); int (*guild_emblem) (struct guild *g); int (*guild_master_changed) (struct guild *g, int aid, int cid); - int (*guild_castle_dataload) (int fd, int sz, int *castle_ids); - int (*parse_CreateGuild) (int fd,int account_id,char *name,struct guild_member *master); + int (*guild_castle_dataload) (int fd, int sz, const int *castle_ids); + int (*parse_CreateGuild) (int fd, int account_id, const char *name, const struct guild_member *master); int (*parse_GuildInfo) (int fd, int guild_id); - int (*parse_GuildAddMember) (int fd, int guild_id, struct guild_member *m); + int (*parse_GuildAddMember) (int fd, int guild_id, const struct guild_member *m); int (*parse_GuildLeave) (int fd, int guild_id, int account_id, int char_id, int flag, const char *mes); int (*parse_GuildChangeMemberInfoShort) (int fd, int guild_id, int account_id, int char_id, int online, int lv, int class_); int (*parse_BreakGuild) (int fd, int guild_id); - int (*parse_GuildMessage) (int fd, int guild_id, int account_id, char *mes, int len); + int (*parse_GuildMessage) (int fd, int guild_id, int account_id, const char *mes, int len); int (*parse_GuildBasicInfoChange) (int fd, int guild_id, int type, const void *data, int len); int (*parse_GuildMemberInfoChange) (int fd, int guild_id, int account_id, int char_id, int type, const char *data, int len); - int (*parse_GuildPosition) (int fd, int guild_id, int idx, struct guild_position *p); + int (*parse_GuildPosition) (int fd, int guild_id, int idx, const struct guild_position *p); int (*parse_GuildSkillUp) (int fd, int guild_id, uint16 skill_id, int account_id, int max); int (*parse_GuildDeleteAlliance) (struct guild *g, int guild_id, int account_id1, int account_id2, int flag); int (*parse_GuildAlliance) (int fd, int guild_id1, int guild_id2, int account_id1, int account_id2, int flag); int (*parse_GuildNotice) (int fd, int guild_id, const char *mes1, const char *mes2); int (*parse_GuildEmblem) (int fd, int len, int guild_id, int dummy, const char *data); - int (*parse_GuildCastleDataLoad) (int fd, int len, int *castle_ids); + int (*parse_GuildCastleDataLoad) (int fd, int len, const int *castle_ids); int (*parse_GuildCastleDataSave) (int fd, int castle_id, int index, int value); int (*parse_GuildMasterChange) (int fd, int guild_id, const char* name, int len); - void (*homunculus_created) (int fd, int account_id, struct s_homunculus *sh, unsigned char flag); + void (*homunculus_created) (int fd, int account_id, const struct s_homunculus *sh, unsigned char flag); void (*homunculus_deleted) (int fd, int flag); void (*homunculus_loaded) (int fd, int account_id, struct s_homunculus *hd); void (*homunculus_saved) (int fd, int account_id, bool flag); - void (*homunculus_renamed) (int fd, int account_id, int char_id, unsigned char flag, char* name); + void (*homunculus_renamed) (int fd, int account_id, int char_id, unsigned char flag, const char *name); bool (*homunculus_create) (struct s_homunculus *hd); bool (*homunculus_save) (const struct s_homunculus *hd); bool (*homunculus_load) (int homun_id, struct s_homunculus* hd); bool (*homunculus_delete) (int homun_id); - bool (*homunculus_rename) (char *name); - void (*parse_homunculus_create) (int fd, int len, int account_id, struct s_homunculus* phd); + bool (*homunculus_rename) (const char *name); + void (*parse_homunculus_create) (int fd, int len, int account_id, const struct s_homunculus *phd); void (*parse_homunculus_delete) (int fd, int homun_id); void (*parse_homunculus_load) (int fd, int account_id, int homun_id); - void (*parse_homunculus_save) (int fd, int len, int account_id, struct s_homunculus* phd); - void (*parse_homunculus_rename) (int fd, int account_id, int char_id, char* name); + void (*parse_homunculus_save) (int fd, int len, int account_id, const struct s_homunculus *phd); + void (*parse_homunculus_rename) (int fd, int account_id, int char_id, const char *name); void (*mail_sendinbox) (int fd, int char_id, unsigned char flag, struct mail_data *md); void (*parse_mail_requestinbox) (int fd); void (*parse_mail_read) (int fd); @@ -130,12 +130,12 @@ struct mapif_interface { bool (*mercenary_load) (int merc_id, int char_id, struct s_mercenary *merc); bool (*mercenary_delete) (int merc_id); void (*mercenary_send) (int fd, struct s_mercenary *merc, unsigned char flag); - void (*parse_mercenary_create) (int fd, struct s_mercenary* merc); + void (*parse_mercenary_create) (int fd, const struct s_mercenary *merc); void (*parse_mercenary_load) (int fd, int merc_id, int char_id); void (*mercenary_deleted) (int fd, unsigned char flag); void (*parse_mercenary_delete) (int fd, int merc_id); void (*mercenary_saved) (int fd, unsigned char flag); - void (*parse_mercenary_save) (int fd, struct s_mercenary* merc); + void (*parse_mercenary_save) (int fd, const struct s_mercenary *merc); int (*party_created) (int fd, int account_id, int char_id, struct party *p); void (*party_noinfo) (int fd, int party_id, int char_id); void (*party_info) (int fd, struct party* p, int char_id); @@ -144,15 +144,15 @@ struct mapif_interface { int (*party_withdraw) (int party_id,int account_id, int char_id); int (*party_membermoved) (struct party *p, int idx); int (*party_broken) (int party_id, int flag); - int (*party_message) (int party_id, int account_id, char *mes, int len, int sfd); - int (*parse_CreateParty) (int fd, char *name, int item, int item2, struct party_member *leader); + int (*party_message) (int party_id, int account_id, const char *mes, int len, int sfd); + int (*parse_CreateParty) (int fd, const char *name, int item, int item2, const struct party_member *leader); void (*parse_PartyInfo) (int fd, int party_id, int char_id); - int (*parse_PartyAddMember) (int fd, int party_id, struct party_member *member); + int (*parse_PartyAddMember) (int fd, int party_id, const struct party_member *member); int (*parse_PartyChangeOption) (int fd,int party_id,int account_id,int exp,int item); int (*parse_PartyLeave) (int fd, int party_id, int account_id, int char_id); int (*parse_PartyChangeMap) (int fd, int party_id, int account_id, int char_id, unsigned short map, int online, unsigned int lv); int (*parse_BreakParty) (int fd, int party_id); - int (*parse_PartyMessage) (int fd, int party_id, int account_id, char *mes, int len); + int (*parse_PartyMessage) (int fd, int party_id, int account_id, const char *mes, int len); int (*parse_PartyLeaderChange) (int fd, int party_id, int account_id, int char_id); int (*pet_created) (int fd, int account_id, struct s_pet *p); int (*pet_info) (int fd, int account_id, struct s_pet *p); @@ -160,7 +160,7 @@ struct mapif_interface { int (*save_pet_ack) (int fd, int account_id, int flag); int (*delete_pet_ack) (int fd, int flag); int (*create_pet) (int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id, - short pet_equip, short intimate, short hungry, char rename_flag, char incubate, char *pet_name); + short pet_equip, short intimate, short hungry, char rename_flag, char incubate, const char *pet_name); int (*load_pet) (int fd, int account_id, int char_id, int pet_id); int (*save_pet) (int fd, int account_id, const struct s_pet *data); int (*delete_pet) (int fd, int pet_id); @@ -186,9 +186,9 @@ struct mapif_interface { void (*parse_accinfo) (int fd); void (*parse_accinfo2) (bool success, int map_fd, int u_fd, int u_aid, int account_id, const char *userid, const char *user_pass, const char *email, const char *last_ip, const char *lastlogin, const char *pin_code, const char *birthdate, int group_id, int logincount, int state); - int (*broadcast) (unsigned char *mes, int len, unsigned int fontColor, short fontType, short fontSize, short fontAlign, short fontY, int sfd); + int (*broadcast) (const unsigned char *mes, int len, unsigned int fontColor, short fontType, short fontSize, short fontAlign, short fontY, int sfd); int (*wis_message) (struct WisData *wd); - void (*wis_response) (int fd, unsigned char *src, int flag); + void (*wis_response) (int fd, const unsigned char *src, int flag); int (*wis_end) (struct WisData *wd, int flag); int (*account_reg_reply) (int fd,int account_id,int char_id, int type); int (*disconnectplayer) (int fd, int account_id, int char_id, int reason); diff --git a/src/login/login.c b/src/login/login.c index d724cccfb..8181cdae9 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -1429,8 +1429,8 @@ bool login_parse_client_login(int fd, struct login_session_data* sd, const char // Shinryo: For the time being, just use token as password. if(command == 0x0825) { - char *accname = (char *)RFIFOP(fd, 9); - char *token = (char *)RFIFOP(fd, 0x5C); + const char *accname = (char *)RFIFOP(fd, 9); + const char *token = (char *)RFIFOP(fd, 0x5C); size_t uAccLen = strlen(accname); size_t uTokenLen = RFIFOREST(fd) - 0x5C; diff --git a/src/map/chrif.c b/src/map/chrif.c index 4de82ef07..e14684e54 100644 --- a/src/map/chrif.c +++ b/src/map/chrif.c @@ -594,7 +594,7 @@ void chrif_authok(int fd) { int account_id, group_id, char_id; uint32 login_id1,login_id2; time_t expiration_time; - struct mmo_charstatus* charstatus; + const struct mmo_charstatus *charstatus; struct auth_node *node; bool changing_mapservers; struct map_session_data *sd = NULL; @@ -1221,7 +1221,7 @@ bool chrif_load_scdata(int fd) { count = RFIFOW(fd,12); //sc_count for (i = 0; i < count; i++) { - struct status_change_data *data = (struct status_change_data*)RFIFOP(fd,14 + i*sizeof(struct status_change_data)); + const struct status_change_data *data = (struct status_change_data*)RFIFOP(fd,14 + i*sizeof(struct status_change_data)); status->change_start(NULL, &sd->bl, (sc_type)data->type, 10000, data->val1, data->val2, data->val3, data->val4, data->tick, SCFLAG_NOAVOID|SCFLAG_FIXEDTICK|SCFLAG_LOADED|SCFLAG_FIXEDRATE); } diff --git a/src/map/clif.c b/src/map/clif.c index a7d7dca97..7152aaa14 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -198,22 +198,6 @@ static inline bool disguised(struct block_list* bl) return true; } -//Guarantees that the given string does not exceeds the allowed size, as well as making sure it's null terminated. [Skotlex] -static inline unsigned int mes_len_check(char* mes, unsigned int len, unsigned int max) { - nullpo_retr(0, mes); - if (len <= 0) - { - mes[0] = '\0'; - Assert_retr(0, len > 0); - } - if( len > max ) - len = max; - - mes[len-1] = '\0'; - - return len; -} - /*========================================== * Ip setting of map-server *------------------------------------------*/ @@ -8884,8 +8868,9 @@ void clif_msgtable_skill(struct map_session_data* sd, uint16 skill_id, int msg_i /// Returns true if the packet was parsed successfully. /// Formats: 0 - .w .w ( : ).?B 00 /// 1 - .w .w .24B .?B 00 -bool clif_process_message(struct map_session_data *sd, int format, char **name_, size_t *namelen_, char **message_, size_t *messagelen_) { - char *text, *name, *message; +bool clif_process_message(struct map_session_data *sd, int format, const char **name_, size_t *namelen_, const char **message_, size_t *messagelen_) +{ + const char *text, *name, *message; unsigned int packetlen, textlen; size_t namelen, messagelen; int fd = sd->fd; @@ -9733,7 +9718,8 @@ void clif_parse_GlobalMessage(int fd, struct map_session_data* sd) const char* text = (char*)RFIFOP(fd,4); size_t textlen = RFIFOW(fd,2) - 4; - char *name, *message, *fakename = NULL; + const char *name = NULL, *message = NULL; + char *fakename = NULL; size_t namelen, messagelen; bool is_fake; @@ -9870,10 +9856,9 @@ void clif_parse_MapMove(int fd, struct map_session_data *sd) __attribute__((nonn void clif_parse_MapMove(int fd, struct map_session_data *sd) { char command[MAP_NAME_LENGTH_EXT+25]; - char* map_name; + char map_name[MAP_NAME_LENGTH_EXT]; - map_name = (char*)RFIFOP(fd,2); - map_name[MAP_NAME_LENGTH_EXT-1]='\0'; + safestrncpy(map_name, (char*)RFIFOP(fd,2), MAP_NAME_LENGTH_EXT); sprintf(command, "%cmapmove %s %d %d", atcommand->at_symbol, map_name, RFIFOW(fd,18), RFIFOW(fd,20)); atcommand->exec(fd, sd, command, true); } @@ -10142,7 +10127,7 @@ void clif_parse_WisMessage(int fd, struct map_session_data* sd) struct map_session_data* dstsd; int i; - char *target, *message; + const char *target, *message; size_t namelen, messagelen; // validate packet and retrieve name and message @@ -10172,7 +10157,7 @@ void clif_parse_WisMessage(int fd, struct map_session_data* sd) // Lordalfa - Paperboy - To whisper NPC commands // //-------------------------------------------------------// if (target[0] && (strncasecmp(target,"NPC:",4) == 0) && (strlen(target) > 4)) { - char* str = target+4; //Skip the NPC: string part. + const char *str = target+4; //Skip the NPC: string part. struct npc_data *nd; if ((nd = npc->name2id(str))) { char split_data[NUM_WHISPER_VAR][CHAT_SIZE_MAX]; @@ -10207,7 +10192,7 @@ void clif_parse_WisMessage(int fd, struct map_session_data* sd) return; } } else if( target[0] == '#' ) { - char *chname = target; + const char *chname = target; struct channel_data *chan = channel->search(chname, sd); if (chan) { @@ -10271,19 +10256,26 @@ void clif_parse_WisMessage(int fd, struct map_session_data* sd) clif->wis_message(dstsd->fd, sd->status.name, message, messagelen); } -void clif_parse_Broadcast(int fd, struct map_session_data* sd) __attribute__((nonnull (2))); +void clif_parse_Broadcast(int fd, struct map_session_data *sd) __attribute__((nonnull (2))); /// /b /nb (CZ_BROADCAST). /// Request to broadcast a message on whole server. /// 0099 .W .?B 00 -void clif_parse_Broadcast(int fd, struct map_session_data* sd) { - char command[CHAT_SIZE_MAX+11]; - char* msg = (char*)RFIFOP(fd,4); - unsigned int len = RFIFOW(fd,2)-4; +void clif_parse_Broadcast(int fd, struct map_session_data *sd) +{ + const char commandname[] = "kami"; + char command[sizeof commandname + 2 + CHAT_SIZE_MAX] = ""; // '@' command + ' ' + message + NUL + int len = (int)RFIFOW(fd,2) - 4; + + if (len < 0) + return; + + sprintf(command, "%c%s ", atcommand->at_symbol, commandname); - // as the length varies depending on the command used, just block unreasonably long strings - mes_len_check(msg, len, CHAT_SIZE_MAX); + // as the length varies depending on the command used, truncate unreasonably long strings + if (len >= (int)(sizeof command - strlen(command))) + len = (int)(sizeof command - strlen(command)) - 1; - sprintf(command, "%ckami %s", atcommand->at_symbol, msg); + strncat(command, (char*)RFIFOP(fd,4), len); atcommand->exec(fd, sd, command, true); } @@ -10541,13 +10533,16 @@ void clif_parse_NpcBuyListSend(int fd, struct map_session_data* sd) __attribute_ void clif_parse_NpcBuyListSend(int fd, struct map_session_data* sd) { int n = (RFIFOW(fd,2)-4) /4; - unsigned short* item_list = (unsigned short*)RFIFOP(fd,4); int result; - if( sd->state.trading || !sd->npc_shopid || pc_has_permission(sd,PC_PERM_DISABLE_STORE) ) + if( sd->state.trading || !sd->npc_shopid || pc_has_permission(sd,PC_PERM_DISABLE_STORE) ) { result = 1; - else + } else { + unsigned short *item_list = aMalloc(sizeof(*item_list) * 2 * n); + memcpy(item_list, (unsigned short*)RFIFOP(fd,4), sizeof(*item_list) * 2 * n); result = npc->buylist(sd,n,item_list); + aFree(item_list); + } sd->npc_shopid = 0; //Clear shop data. @@ -10576,15 +10571,17 @@ void clif_parse_NpcSellListSend(int fd,struct map_session_data *sd) __attribute_ void clif_parse_NpcSellListSend(int fd,struct map_session_data *sd) { int fail=0,n; - unsigned short *item_list; n = (RFIFOW(fd,2)-4) /4; - item_list = (unsigned short*)RFIFOP(fd,4); - if (sd->state.trading || !sd->npc_shopid) + if (sd->state.trading || !sd->npc_shopid) { fail = 1; - else + } else { + unsigned short *item_list = aMalloc(sizeof(*item_list) * 2 * n); + memcpy(item_list, (unsigned short*)RFIFOP(fd,4), sizeof(*item_list) * 2 * n); fail = npc->selllist(sd,n,item_list); + aFree(item_list); + } sd->npc_shopid = 0; //Clear shop data. @@ -11595,20 +11592,26 @@ void clif_parse_ResetChar(int fd, struct map_session_data *sd) { atcommand->exec(fd, sd, cmd, true); } -void clif_parse_LocalBroadcast(int fd, struct map_session_data* sd) __attribute__((nonnull (2))); +void clif_parse_LocalBroadcast(int fd, struct map_session_data *sd) __attribute__((nonnull (2))); /// /lb /nlb (CZ_LOCALBROADCAST). /// Request to broadcast a message on current map. /// 019c .W .?B -void clif_parse_LocalBroadcast(int fd, struct map_session_data* sd) +void clif_parse_LocalBroadcast(int fd, struct map_session_data *sd) { - char command[CHAT_SIZE_MAX+16]; - char* msg = (char*)RFIFOP(fd,4); - unsigned int len = RFIFOW(fd,2)-4; + const char commandname[] = "lkami"; + char command[sizeof commandname + 2 + CHAT_SIZE_MAX] = ""; // '@' + command + ' ' + message + NUL + int len = (int)RFIFOW(fd,2) - 4; + + if (len < 0) + return; - // as the length varies depending on the command used, just block unreasonably long strings - mes_len_check(msg, len, CHAT_SIZE_MAX); + sprintf(command, "%c%s ", atcommand->at_symbol, commandname); - sprintf(command, "%clkami %s", atcommand->at_symbol, msg); + // as the length varies depending on the command used, truncate unreasonably long strings + if (len >= (int)(sizeof command - strlen(command))) + len = (int)(sizeof command - strlen(command)) - 1; + + strncat(command, (char*)RFIFOP(fd,4), len); atcommand->exec(fd, sd, command, true); } @@ -11755,9 +11758,11 @@ void clif_parse_CreateParty(int fd, struct map_session_data *sd) __attribute__(( /// Party creation request /// 00f9 .24B (CZ_MAKE_GROUP) /// 01e8 .24B .B .B (CZ_MAKE_GROUP2) -void clif_parse_CreateParty(int fd, struct map_session_data *sd) { - char* name = (char*)RFIFOP(fd,2); - name[NAME_LENGTH-1] = '\0'; +void clif_parse_CreateParty(int fd, struct map_session_data *sd) +{ + char name[NAME_LENGTH]; + + safestrncpy(name, (char*)RFIFOP(fd,2), NAME_LENGTH); if( map->list[sd->bl.m].flag.partylock ) { // Party locked. @@ -11773,11 +11778,13 @@ void clif_parse_CreateParty(int fd, struct map_session_data *sd) { } void clif_parse_CreateParty2(int fd, struct map_session_data *sd) __attribute__((nonnull (2))); -void clif_parse_CreateParty2(int fd, struct map_session_data *sd) { - char* name = (char*)RFIFOP(fd,2); +void clif_parse_CreateParty2(int fd, struct map_session_data *sd) +{ + char name[NAME_LENGTH]; int item1 = RFIFOB(fd,26); int item2 = RFIFOB(fd,27); - name[NAME_LENGTH-1] = '\0'; + + safestrncpy(name, (char*)RFIFOP(fd,2), NAME_LENGTH); if( map->list[sd->bl.m].flag.partylock ) { // Party locked. @@ -11816,10 +11823,12 @@ void clif_parse_PartyInvite(int fd, struct map_session_data *sd) { } void clif_parse_PartyInvite2(int fd, struct map_session_data *sd) __attribute__((nonnull (2))); -void clif_parse_PartyInvite2(int fd, struct map_session_data *sd) { +void clif_parse_PartyInvite2(int fd, struct map_session_data *sd) +{ struct map_session_data *t_sd; - char *name = (char*)RFIFOP(fd,2); - name[NAME_LENGTH-1] = '\0'; + char name[NAME_LENGTH]; + + safestrncpy(name, (char*)RFIFOP(fd,2), NAME_LENGTH); if(map->list[sd->bl.m].flag.partylock) { // Party locked. @@ -11918,7 +11927,7 @@ void clif_parse_PartyMessage(int fd, struct map_session_data* sd) const char* text = (char*)RFIFOP(fd,4); int textlen = RFIFOW(fd,2) - 4; - char *name, *message; + const char *name, *message; size_t namelen, messagelen; // validate packet and retrieve name and message @@ -12660,9 +12669,10 @@ void clif_parse_OpenVending(int fd, struct map_session_data* sd) { void clif_parse_CreateGuild(int fd,struct map_session_data *sd) __attribute__((nonnull (2))); /// Guild creation request (CZ_REQ_MAKE_GUILD). /// 0165 .L .24B -void clif_parse_CreateGuild(int fd,struct map_session_data *sd) { - char* name = (char*)RFIFOP(fd,6); - name[NAME_LENGTH-1] = '\0'; +void clif_parse_CreateGuild(int fd,struct map_session_data *sd) +{ + char name[NAME_LENGTH]; + safestrncpy(name, (char*)RFIFOP(fd,6), NAME_LENGTH); if(map->list[sd->bl.m].flag.guildlock) { //Guild locked. @@ -12915,18 +12925,22 @@ void clif_parse_GuildChangeNotice(int fd, struct map_session_data* sd) __attribu void clif_parse_GuildChangeNotice(int fd, struct map_session_data* sd) { int guild_id = RFIFOL(fd,2); - char* msg1 = (char*)RFIFOP(fd,6); - char* msg2 = (char*)RFIFOP(fd,66); + char *msg1 = NULL, *msg2 = NULL; - if(!sd->state.gmaster_flag) + if (!sd->state.gmaster_flag) return; + msg1 = aStrndup((char*)RFIFOP(fd,6), MAX_GUILDMES1-1); + msg2 = aStrndup((char*)RFIFOP(fd,66), MAX_GUILDMES2-1); + // compensate for some client defects when using multilingual mode if (msg1[0] == '|' && msg1[3] == '|') msg1+= 3; // skip duplicate marker if (msg2[0] == '|' && msg2[3] == '|') msg2+= 3; // skip duplicate marker if (msg2[0] == '|') msg2[strnlen(msg2, MAX_GUILDMES2)-1] = '\0'; // delete extra space at the end of string guild->change_notice(sd, guild_id, msg1, msg2); + aFree(msg1); + aFree(msg2); } // Helper function for guild invite functions @@ -12964,14 +12978,15 @@ void clif_parse_GuildInvite(int fd,struct map_session_data *sd) { void clif_parse_GuildInvite2(int fd, struct map_session_data *sd) __attribute__((nonnull (2))); /// Guild invite request (/guildinvite) (CZ_REQ_JOIN_GUILD2). /// 0916 .24B -void clif_parse_GuildInvite2(int fd, struct map_session_data *sd) { - char *nick = (char*)RFIFOP(fd, 2); - struct map_session_data *t_sd = map->nick2sd(nick); +void clif_parse_GuildInvite2(int fd, struct map_session_data *sd) +{ + char nick[NAME_LENGTH]; + struct map_session_data *t_sd = NULL; - nick[NAME_LENGTH - 1] = '\0'; + safestrncpy(nick, (char*)RFIFOP(fd, 2), NAME_LENGTH); + t_sd = map->nick2sd(nick); - if (!clif_sub_guild_invite(fd, sd, t_sd)) - return; + clif_sub_guild_invite(fd, sd, t_sd); } void clif_parse_GuildReplyInvite(int fd,struct map_session_data *sd) __attribute__((nonnull (2))); @@ -13022,7 +13037,7 @@ void clif_parse_GuildMessage(int fd, struct map_session_data* sd) const char* text = (char*)RFIFOP(fd,4); int textlen = RFIFOW(fd,2) - 4; - char *name, *message; + const char *name, *message; size_t namelen, messagelen; // validate packet and retrieve name and message @@ -13299,12 +13314,12 @@ void clif_parse_GMShift(int fd, struct map_session_data *sd) __attribute__((nonn /// Request to warp to a character with given name. /// 01bb .24B void clif_parse_GMShift(int fd, struct map_session_data *sd) -{// FIXME: remove is supposed to receive account name for clients prior 20100803RE - char *player_name; +{ + // FIXME: remove is supposed to receive account name for clients prior 20100803RE + char player_name[NAME_LENGTH]; char command[NAME_LENGTH+8]; - player_name = (char*)RFIFOP(fd,2); - player_name[NAME_LENGTH-1] = '\0'; + safestrncpy(player_name, (char*)RFIFOP(fd,2), NAME_LENGTH); sprintf(command, "%cjumpto %s", atcommand->at_symbol, player_name); atcommand->exec(fd, sd, command, true); @@ -13335,12 +13350,12 @@ void clif_parse_GMRecall(int fd, struct map_session_data *sd) __attribute__((non /// Request to summon a player with given name to own position. /// 01bd .24B void clif_parse_GMRecall(int fd, struct map_session_data *sd) -{// FIXME: recall is supposed to receive account name for clients prior 20100803RE - char *player_name; - char command [NAME_LENGTH+8]; +{ + // FIXME: recall is supposed to receive account name for clients prior 20100803RE + char player_name[NAME_LENGTH]; + char command[NAME_LENGTH+8]; - player_name = (char*)RFIFOP(fd,2); - player_name[NAME_LENGTH-1] = '\0'; + safestrncpy(player_name, (char*)RFIFOP(fd,2), NAME_LENGTH); sprintf(command, "%crecall %s", atcommand->at_symbol, player_name); atcommand->exec(fd, sd, command, true); @@ -13506,9 +13521,10 @@ void clif_parse_GMRc(int fd, struct map_session_data* sd) __attribute__((nonnull void clif_parse_GMRc(int fd, struct map_session_data* sd) { char command[NAME_LENGTH+15]; - char *name = (char*)RFIFOP(fd,2); + char name[NAME_LENGTH]; + + safestrncpy(name, (char*)RFIFOP(fd,2), NAME_LENGTH); - name[NAME_LENGTH-1] = '\0'; sprintf(command, "%cmute %d %s", atcommand->at_symbol, 60, name); atcommand->exec(fd, sd, command, true); } @@ -13567,13 +13583,14 @@ void clif_parse_PMIgnore(int fd, struct map_session_data* sd) __attribute__((non /// type: /// 0 = (/ex nick) deny speech from nick /// 1 = (/in nick) allow speech from nick -void clif_parse_PMIgnore(int fd, struct map_session_data* sd) { - char* nick; +void clif_parse_PMIgnore(int fd, struct map_session_data* sd) +{ + char nick[NAME_LENGTH]; uint8 type; int i; - nick = (char*)RFIFOP(fd,2); // speed up - nick[NAME_LENGTH-1] = '\0'; // to be sure that the player name has at most 23 characters + safestrncpy(nick, (char*)RFIFOP(fd,2), NAME_LENGTH); + type = RFIFOB(fd,26); if( type == 0 ) { // Add name to ignore list (block) @@ -13831,12 +13848,14 @@ void clif_friendlist_req(struct map_session_data* sd, int account_id, int char_i void clif_parse_FriendsListAdd(int fd, struct map_session_data *sd) __attribute__((nonnull (2))); /// Request to add a player as friend (CZ_ADD_FRIENDS). /// 0202 .24B -void clif_parse_FriendsListAdd(int fd, struct map_session_data *sd) { +void clif_parse_FriendsListAdd(int fd, struct map_session_data *sd) +{ struct map_session_data *f_sd; int i; - char *nick = (char*)RFIFOP(fd,2); + char nick[NAME_LENGTH]; + + safestrncpy(nick, (char*)RFIFOP(fd,2), NAME_LENGTH); - nick[NAME_LENGTH - 1] = '\0'; f_sd = map->nick2sd(nick); // ensure that the request player's friend list is not full @@ -14967,7 +14986,7 @@ void clif_Auction_openwindow(struct map_session_data *sd) /// Returns auction item search results (ZC_AUCTION_ITEM_REQ_SEARCH). /// 0252 .W .L .L { .L .24B .W .L .W .B .B .B .W .W .W .W .L .L .24B .L }* -void clif_Auction_results(struct map_session_data *sd, short count, short pages, uint8 *buf) +void clif_Auction_results(struct map_session_data *sd, short count, short pages, const uint8 *buf) { int i, fd, len = sizeof(struct auction_data); struct auction_data auction; @@ -15414,13 +15433,16 @@ void clif_parse_cashshop_buy(int fd, struct map_session_data *sd) int len = RFIFOW(fd,2); int points = RFIFOL(fd,4); int count = RFIFOW(fd,8); - unsigned short* item_list = (unsigned short*)RFIFOP(fd,10); + unsigned short *item_list = NULL; if( len < 10 || len != 10 + count * 4) { ShowWarning("Player %d sent incorrect cash shop buy packet (len %d:%d)!\n", sd->status.char_id, len, 10 + count * 4); return; } + item_list = aMalloc(sizeof(*item_list) * 2 * count); + memcpy(item_list, (unsigned short*)RFIFOP(fd,10), sizeof(*item_list) * 2 * count); fail = npc->cashshop_buylist(sd,points,count,item_list); + aFree(item_list); #endif } @@ -16119,7 +16141,7 @@ void clif_parse_BattleChat(int fd, struct map_session_data* sd) const char* text = (char*)RFIFOP(fd,4); int textlen = RFIFOW(fd,2) - 4; - char *name, *message; + const char *name, *message; size_t namelen, messagelen; if( !clif->process_message(sd, 0, &name, &namelen, &message, &messagelen) ) @@ -16402,27 +16424,33 @@ void clif_parse_LessEffect(int fd, struct map_session_data* sd) sd->state.lesseffect = ( isLess != 0 ); } -void clif_parse_ItemListWindowSelected(int fd, struct map_session_data* sd) __attribute__((nonnull (2))); +void clif_parse_ItemListWindowSelected(int fd, struct map_session_data *sd) __attribute__((nonnull (2))); /// S 07e4 .w