From f0a87e4f7f5377498960429e96be5dff183c8326 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Wed, 9 Jan 2013 00:18:53 -0800 Subject: Make incoming packets read-only --- src/char/char.cpp | 268 ++++++++++++------------------ src/char/inter.cpp | 18 +-- src/common/socket.cpp | 19 +++ src/common/socket.hpp | 150 ++++++++++++----- src/common/timer.hpp | 8 +- src/ladmin/ladmin.cpp | 151 ++++++++--------- src/login/login.cpp | 236 +++++++++++---------------- src/login/login.hpp | 4 +- src/map/atcommand.cpp | 21 +-- src/map/battle.cpp | 2 +- src/map/chrif.cpp | 4 +- src/map/clif.cpp | 441 +++++++++++++++++++++++++------------------------- src/map/intif.cpp | 12 +- src/map/map.cpp | 18 +-- src/map/npc.cpp | 10 +- src/map/npc.hpp | 4 +- src/map/party.cpp | 13 +- src/map/party.hpp | 2 +- src/map/pc.cpp | 8 +- src/map/pc.hpp | 2 +- src/map/script.cpp | 7 +- src/warnings.hpp | 4 +- 22 files changed, 684 insertions(+), 718 deletions(-) diff --git a/src/char/char.cpp b/src/char/char.cpp index 05c217d..5185309 100644 --- a/src/char/char.cpp +++ b/src/char/char.cpp @@ -634,10 +634,10 @@ void remove_prefix_blanks(char *name) // Function to create a new character //----------------------------------- static -int make_new_char(int fd, unsigned char *dat) +int make_new_char(int fd, const uint8_t *dat) { // ugh - char *cdat = (char *)dat; + char *cdat = reinterpret_cast(const_cast(dat)); int i, j; struct char_session_data *sd = (struct char_session_data *)session[fd]->session_data; @@ -2637,6 +2637,100 @@ int lan_ip_check(unsigned char *p) return lancheck; } +static +void handle_x0066(int fd, struct char_session_data *sd, uint8_t rfifob_2, uint8_t *p) +{ + const char *ip = ip2str(session[fd]->client_addr.sin_addr); + + // if we activated email creation and email is default email + if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 + && login_fd > 0) + { // to modify an e-mail, login-server must be online + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; // 00 = Incorrect Email address + WFIFOSET(fd, 3); + + // otherwise, load the character + } + else + { + int ch; + for (ch = 0; ch < 9; ch++) + if (sd->found_char[ch] >= 0 + && char_dat[sd->found_char[ch]].char_num == rfifob_2) + break; + if (ch != 9) + { + CHAR_LOG("Character Selected, Account ID: %d, Character Slot: %d, Character Name: %s [%s]\n", + sd->account_id, rfifob_2, + char_dat[sd->found_char[ch]].name, ip); + // searching map server + int i = search_mapserver(char_dat[sd->found_char[ch]].last_point.map); + // if map is not found, we check major cities + if (i < 0) + { + int j; + // get first online server (with a map) + i = 0; + for (j = 0; j < MAX_MAP_SERVERS; j++) + if (server_fd[j] >= 0 + && server[j].map[0][0]) + { // change save point to one of map found on the server (the first) + i = j; + memcpy(char_dat[sd->found_char[ch]].last_point.map, + server[j].map[0], 16); + PRINTF("Map-server #%d found with a map: '%s'.\n", + j, server[j].map[0]); + // coordonates are unknown + break; + } + // if no map-server is connected, we send: server closed + if (j == MAX_MAP_SERVERS) + { + WFIFOW(fd, 0) = 0x81; + WFIFOL(fd, 2) = 1; // 01 = Server closed + WFIFOSET(fd, 3); + return; + } + } + WFIFOW(fd, 0) = 0x71; + WFIFOL(fd, 2) = char_dat[sd->found_char[ch]].char_id; + memcpy(WFIFOP(fd, 6), + char_dat[sd->found_char[ch]].last_point.map, + 16); + PRINTF("Character selection '%s' (account: %d, slot: %d) [%s]\n", + char_dat[sd->found_char[ch]].name, + sd->account_id, ch, ip); + PRINTF("--Send IP of map-server. "); + if (lan_ip_check(p)) + WFIFOL(fd, 22) = inet_addr(lan_map_ip); + else + WFIFOL(fd, 22) = server[i].ip; + WFIFOW(fd, 26) = server[i].port; + WFIFOSET(fd, 28); + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; + //PRINTF("auth_fifo set #%d - account %d, char: %d, secure: %08x-%08x\n", auth_fifo_pos, sd->account_id, char_dat[sd->found_char[ch]].char_id, sd->login_id1, sd->login_id2); + auth_fifo[auth_fifo_pos].account_id = sd->account_id; + auth_fifo[auth_fifo_pos].char_id = + char_dat[sd->found_char[ch]].char_id; + auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1; + auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2; + auth_fifo[auth_fifo_pos].delflag = 0; + auth_fifo[auth_fifo_pos].char_pos = + sd->found_char[ch]; + auth_fifo[auth_fifo_pos].sex = sd->sex; + auth_fifo[auth_fifo_pos].connect_until_time = + sd->connect_until_time; + auth_fifo[auth_fifo_pos].ip = + session[fd]->client_addr.sin_addr.s_addr; + auth_fifo[auth_fifo_pos].packet_tmw_version = + sd->packet_tmw_version; + auth_fifo_pos++; + } + } +} + static void parse_char(int fd) { @@ -2776,171 +2870,14 @@ void parse_char(int fd) case 0x66: // キャラ選択 if (!sd || RFIFOREST(fd) < 3) return; - { - const char *ip = ip2str(session[fd]->client_addr.sin_addr); - - // if we activated email creation and email is default email - if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 - && login_fd > 0) - { // to modify an e-mail, login-server must be online - WFIFOW(fd, 0) = 0x70; - WFIFOB(fd, 2) = 0; // 00 = Incorrect Email address - WFIFOSET(fd, 3); - - // otherwise, load the character - } - else - { - for (ch = 0; ch < 9; ch++) - if (sd->found_char[ch] >= 0 - && char_dat[sd->found_char[ch]].char_num == - RFIFOB(fd, 2)) - break; - if (ch != 9) - { - CHAR_LOG("Character Selected, Account ID: %d, Character Slot: %d, Character Name: %s [%s]\n", - sd->account_id, RFIFOB(fd, 2), - char_dat[sd->found_char[ch]].name, ip); - // searching map server - i = search_mapserver(char_dat - [sd->found_char[ch]].last_point. - map); - // if map is not found, we check major cities - if (i < 0) - { - if ((i = search_mapserver("prontera.gat")) >= 0) - { // check is done without 'gat'. - memcpy(char_dat - [sd->found_char[ch]].last_point.map, - "prontera.gat", 16); - char_dat[sd->found_char[ch]].last_point.x = 273; // savepoint coordonates - char_dat[sd->found_char[ch]].last_point.y = - 354; - } - else if ((i = - search_mapserver("geffen.gat")) >= 0) - { // check is done without 'gat'. - memcpy(char_dat - [sd->found_char[ch]].last_point.map, - "geffen.gat", 16); - char_dat[sd->found_char[ch]].last_point.x = 120; // savepoint coordonates - char_dat[sd->found_char[ch]].last_point.y = - 100; - } - else if ((i = - search_mapserver("morocc.gat")) >= 0) - { // check is done without 'gat'. - memcpy(char_dat - [sd->found_char[ch]].last_point.map, - "morocc.gat", 16); - char_dat[sd->found_char[ch]].last_point.x = 160; // savepoint coordonates - char_dat[sd->found_char[ch]].last_point.y = - 94; - } - else if ((i = - search_mapserver("alberta.gat")) >= 0) - { // check is done without 'gat'. - memcpy(char_dat - [sd->found_char[ch]].last_point.map, - "alberta.gat", 16); - char_dat[sd->found_char[ch]].last_point.x = 116; // savepoint coordonates - char_dat[sd->found_char[ch]].last_point.y = - 57; - } - else if ((i = - search_mapserver("payon.gat")) >= 0) - { // check is done without 'gat'. - memcpy(char_dat - [sd->found_char[ch]].last_point.map, - "payon.gat", 16); - char_dat[sd->found_char[ch]].last_point.x = 87; // savepoint coordonates - char_dat[sd->found_char[ch]].last_point.y = - 117; - } - else if ((i = - search_mapserver("izlude.gat")) >= 0) - { // check is done without 'gat'. - memcpy(char_dat - [sd->found_char[ch]].last_point.map, - "izlude.gat", 16); - char_dat[sd->found_char[ch]].last_point.x = 94; // savepoint coordonates - char_dat[sd->found_char[ch]].last_point.y = - 103; - } - else - { - int j; - // get first online server (with a map) - i = 0; - for (j = 0; j < MAX_MAP_SERVERS; j++) - if (server_fd[j] >= 0 - && server[j].map[0][0]) - { // change save point to one of map found on the server (the first) - i = j; - memcpy(char_dat - [sd-> - found_char[ch]].last_point. - map, server[j].map[0], 16); - PRINTF("Map-server #%d found with a map: '%s'.\n", - j, server[j].map[0]); - // coordonates are unknown - break; - } - // if no map-server is connected, we send: server closed - if (j == MAX_MAP_SERVERS) - { - WFIFOW(fd, 0) = 0x81; - WFIFOL(fd, 2) = 1; // 01 = Server closed - WFIFOSET(fd, 3); - RFIFOSKIP(fd, 3); - break; - } - } - } - WFIFOW(fd, 0) = 0x71; - WFIFOL(fd, 2) = char_dat[sd->found_char[ch]].char_id; - memcpy(WFIFOP(fd, 6), - char_dat[sd->found_char[ch]].last_point.map, - 16); - PRINTF("Character selection '%s' (account: %d, slot: %d) [%s]\n", - char_dat[sd->found_char[ch]].name, - sd->account_id, ch, ip); - PRINTF("--Send IP of map-server. "); - if (lan_ip_check(p)) - WFIFOL(fd, 22) = inet_addr(lan_map_ip); - else - WFIFOL(fd, 22) = server[i].ip; - WFIFOW(fd, 26) = server[i].port; - WFIFOSET(fd, 28); - if (auth_fifo_pos >= AUTH_FIFO_SIZE) - auth_fifo_pos = 0; - //PRINTF("auth_fifo set #%d - account %d, char: %d, secure: %08x-%08x\n", auth_fifo_pos, sd->account_id, char_dat[sd->found_char[ch]].char_id, sd->login_id1, sd->login_id2); - auth_fifo[auth_fifo_pos].account_id = sd->account_id; - auth_fifo[auth_fifo_pos].char_id = - char_dat[sd->found_char[ch]].char_id; - auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1; - auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2; - auth_fifo[auth_fifo_pos].delflag = 0; - auth_fifo[auth_fifo_pos].char_pos = - sd->found_char[ch]; - auth_fifo[auth_fifo_pos].sex = sd->sex; - auth_fifo[auth_fifo_pos].connect_until_time = - sd->connect_until_time; - auth_fifo[auth_fifo_pos].ip = - session[fd]->client_addr.sin_addr.s_addr; - auth_fifo[auth_fifo_pos].packet_tmw_version = - sd->packet_tmw_version; - auth_fifo_pos++; - } - } - } + handle_x0066(fd, sd, RFIFOB(fd, 2), p); RFIFOSKIP(fd, 3); break; case 0x67: // 作成 if (!sd || RFIFOREST(fd) < 37) return; - i = make_new_char(fd, RFIFOP(fd, 2)); + i = make_new_char(fd, static_cast(RFIFOP(fd, 2))); if (i < 0) { WFIFOW(fd, 0) = 0x6e; @@ -3041,13 +2978,8 @@ void parse_char(int fd) WFIFOL(login_fd, 2) = sd->account_id; memcpy(WFIFOP(login_fd, 6), email, 40); WFIFOSET(login_fd, 46); - // skip part of the packet! (46, but leave the size of select packet: 3) - RFIFOSKIP(fd, 43); - // change value to put new packet (char selection) - RFIFOW(fd, 0) = 0x66; - RFIFOB(fd, 2) = - char_dat[sd->found_char[i]].char_num; - // not send packet, it's modify of actual packet + RFIFOSKIP(fd, 46); + handle_x0066(fd, sd, char_dat[sd->found_char[i]].char_num, p); break; } if (i == 9) diff --git a/src/char/inter.cpp b/src/char/inter.cpp index 5262cca..f04500c 100644 --- a/src/char/inter.cpp +++ b/src/char/inter.cpp @@ -245,7 +245,7 @@ int inter_init(const char *file) // GMメッセージ送信 static -int mapif_GMmessage(unsigned char *mes, int len) +void mapif_GMmessage(const uint8_t *mes, int len) { unsigned char buf[len]; @@ -253,8 +253,6 @@ int mapif_GMmessage(unsigned char *mes, int len) WBUFW(buf, 2) = len; memcpy(WBUFP(buf, 4), mes, len - 4); mapif_sendall(buf, len); - - return 0; } // Wisp/page transmission to all map-server @@ -290,11 +288,11 @@ int mapif_wis_end(struct WisData *wd, int flag) // アカウント変数送信 static -int mapif_account_reg(int fd, unsigned char *src) +int mapif_account_reg(int fd, const uint8_t *src) { - unsigned char buf[WBUFW(src, 2)]; + unsigned char buf[RBUFW(src, 2)]; - memcpy(WBUFP(buf, 0), src, WBUFW(src, 2)); + memcpy(WBUFP(buf, 0), src, RBUFW(src, 2)); WBUFW(buf, 0) = 0x3804; mapif_sendallwos(fd, buf, WBUFW(buf, 2)); @@ -374,7 +372,7 @@ int check_ttl_wisdata(void) static int mapif_parse_GMmessage(int fd) { - mapif_GMmessage(RFIFOP(fd, 4), RFIFOW(fd, 2)); + mapif_GMmessage(static_cast(RFIFOP(fd, 4)), RFIFOW(fd, 2)); return 0; } @@ -411,8 +409,7 @@ int mapif_parse_WisRequest(int fd) else { // to be sure of the correct name, rewrite it - memset(RFIFOP(fd, 28), 0, 24); - strncpy((char *)RFIFOP(fd, 28), search_character_name(index), 24); + strzcpy(static_cast(const_cast(RFIFOP(fd, 28))), search_character_name(index), 24); // if source is destination, don't ask other servers. if (strcmp((const char *)RFIFOP(fd, 4), (const char *)RFIFOP(fd, 28)) == 0) { @@ -499,7 +496,8 @@ int mapif_parse_AccReg(int fd) } reg->reg_num = j; - mapif_account_reg(fd, RFIFOP(fd, 0)); // 他のMAPサーバーに送信 + // 他のMAPサーバーに送信 + mapif_account_reg(fd, static_cast(RFIFOP(fd, 0))); return 0; } diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 023a856..f2e6df6 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -24,11 +24,30 @@ int fd_max; static int currentuse; +static const uint32_t RFIFO_SIZE = 65536; +static const uint32_t WFIFO_SIZE = 65536; struct socket_data *session[FD_SETSIZE]; +/// clean up by discarding handled bytes +inline +void RFIFOFLUSH(int fd) +{ + memmove(session[fd]->rdata, RFIFOP(fd, 0), RFIFOREST(fd)); + session[fd]->rdata_size = RFIFOREST(fd); + session[fd]->rdata_pos = 0; +} + +/// how much room there is to read more data +inline +size_t RFIFOSPACE(int fd) +{ + return session[fd]->max_rdata - session[fd]->rdata_size; +} + + /// Discard all input static void null_parse(int fd); diff --git a/src/common/socket.hpp b/src/common/socket.hpp index ac7fbae..975b7f4 100644 --- a/src/common/socket.hpp +++ b/src/common/socket.hpp @@ -10,47 +10,6 @@ # include # include -/// Check how much can be read -# define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos) -/// Read from the queue -# define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos)) -# define RFIFOB(fd,pos) (*(uint8_t*)(RFIFOP(fd, pos))) -# define RFIFOW(fd,pos) (*(uint16_t*)(RFIFOP(fd, pos))) -# define RFIFOL(fd,pos) (*(uint32_t*)(RFIFOP(fd, pos))) -/// Done reading -void RFIFOSKIP(int fd, size_t len); -/// Internal - clean up by discarding handled bytes -# define RFIFOFLUSH(fd) (memmove(session[fd]->rdata,RFIFOP(fd,0),RFIFOREST(fd)),\ -session[fd]->rdata_size=RFIFOREST(fd),\ -session[fd]->rdata_pos=0) - -/// Used internally - how much room there is to read more data -# define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size) - -/// Read from an arbitrary buffer -# define RBUFP(p,pos) (((uint8_t*)(p))+(pos)) -# define RBUFB(p,pos) (*(uint8_t*)RBUFP((p),(pos))) -# define RBUFW(p,pos) (*(uint16_t*)RBUFP((p),(pos))) -# define RBUFL(p,pos) (*(uint32_t*)RBUFP((p),(pos))) - - - -/// Unused - check how much data can be written -# define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) -/// Write to the queue -# define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos)) -# define WFIFOB(fd,pos) (*(uint8_t*)(WFIFOP(fd,pos))) -# define WFIFOW(fd,pos) (*(uint16_t*)(WFIFOP(fd,pos))) -# define WFIFOL(fd,pos) (*(uint32_t*)(WFIFOP(fd,pos))) -/// Finish writing -void WFIFOSET(int fd, size_t len); - -/// Write to an arbitrary buffer -#define WBUFP(p,pos) (((uint8_t*)(p))+(pos)) -#define WBUFB(p,pos) (*(uint8_t*)WBUFP((p),(pos))) -#define WBUFW(p,pos) (*(uint16_t*)WBUFP((p),(pos))) -#define WBUFL(p,pos) (*(uint32_t*)WBUFP((p),(pos))) - // Struct declaration struct socket_data @@ -128,6 +87,115 @@ void set_defaultparse(void(*defaultparse)(int)); /// Wrappers to track number of free FDs void fclose_(FILE * fp); FILE *fopen_(const char *path, const char *mode); + bool free_fds(void); + + +/// Check how much can be read +inline +size_t RFIFOREST(int fd) +{ + return session[fd]->rdata_size - session[fd]->rdata_pos; +} +/// Read from the queue +inline +const void *RFIFOP(int fd, size_t pos) +{ + return session[fd]->rdata + session[fd]->rdata_pos + pos; +} +inline +uint8_t RFIFOB(int fd, size_t pos) +{ + return *static_cast(RFIFOP(fd, pos)); +} +inline +uint16_t RFIFOW(int fd, size_t pos) +{ + return *static_cast(RFIFOP(fd, pos)); +} +inline +uint32_t RFIFOL(int fd, size_t pos) +{ + return *static_cast(RFIFOP(fd, pos)); +} + +/// Done reading +void RFIFOSKIP(int fd, size_t len); + +/// Read from an arbitrary buffer +inline +const void *RBUFP(const uint8_t *p, size_t pos) +{ + return p + pos; +} +inline +uint8_t RBUFB(const uint8_t *p, size_t pos) +{ + return *static_cast(RBUFP(p, pos)); +} +inline +uint16_t RBUFW(const uint8_t *p, size_t pos) +{ + return *static_cast(RBUFP(p, pos)); +} +inline +uint32_t RBUFL(const uint8_t *p, size_t pos) +{ + return *static_cast(RBUFP(p, pos)); +} + + +/// Unused - check how much data can be written +inline +size_t WFIFOSPACE(int fd) +{ + return session[fd]->max_wdata - session[fd]->wdata_size; +} +/// Write to the queue +inline +void *WFIFOP(int fd, size_t pos) +{ + return session[fd]->wdata + session[fd]->wdata_size + pos; +} +inline +uint8_t& WFIFOB(int fd, size_t pos) +{ + return *static_cast(WFIFOP(fd, pos)); +} +inline +uint16_t& WFIFOW(int fd, size_t pos) +{ + return *static_cast(WFIFOP(fd, pos)); +} +inline +uint32_t& WFIFOL(int fd, size_t pos) +{ + return *static_cast(WFIFOP(fd, pos)); +} +/// Finish writing +void WFIFOSET(int fd, size_t len); + +/// Write to an arbitrary buffer +inline +void *WBUFP(uint8_t *p, size_t pos) +{ + return p + pos; +} +inline +uint8_t& WBUFB(uint8_t *p, size_t pos) +{ + return *static_cast(WBUFP(p, pos)); +} +inline +uint16_t& WBUFW(uint8_t *p, size_t pos) +{ + return *static_cast(WBUFP(p, pos)); +} +inline +uint32_t& WBUFL(uint8_t *p, size_t pos) +{ + return *static_cast(WBUFP(p, pos)); +} + #endif // SOCKET_HPP diff --git a/src/common/timer.hpp b/src/common/timer.hpp index fc4f8cb..db4dedd 100644 --- a/src/common/timer.hpp +++ b/src/common/timer.hpp @@ -10,9 +10,13 @@ enum TIMER_TYPE TIMER_INTERVAL, }; /// This is needed to produce a signed result when 2 ticks are subtracted -# define DIFF_TICK(a,b) ((int32_t)((a)-(b))) +inline +int32_t DIFF_TICK(int32_t a, int32_t b) +{ + return a - b; +} -// TODO replace with signed 64-bit to make code more clear and protect from the future +// TODO replace with std::chrono::time_point and std::chrono::duration typedef uint32_t tick_t; typedef uint32_t interval_t; typedef uint32_t timer_id; diff --git a/src/ladmin/ladmin.cpp b/src/ladmin/ladmin.cpp index 1a57cbb..966d290 100644 --- a/src/ladmin/ladmin.cpp +++ b/src/ladmin/ladmin.cpp @@ -2932,16 +2932,16 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] creation failed. Same account already exists.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] creation failed. Same account already exists.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("Account [%s] is successfully created [id: %d].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Account [%s] is successfully created [id: %d].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -2953,16 +2953,16 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] deletion failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] deletion failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("Account [%s][id: %d] is successfully DELETED.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Account [%s][id: %d] is successfully DELETED.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -2974,18 +2974,18 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] password changing failed.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); PRINTF("Account [%s] doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account password changing failed. The compte [%s] doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("Account [%s][id: %d] password successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Account [%s][id: %d] password successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -2997,15 +2997,15 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] state changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] state changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { std::string tmpstr = STRPRINTF( "Account [%s] state successfully changed in [", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); switch (RFIFOL(fd, 30)) { case 0: @@ -3086,16 +3086,16 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("The account [%s] doesn't exist or the password is incorrect.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("The account [%s] doesn't exist or the password is incorrect.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("The proposed password is correct for the account [%s][id: %d].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("The proposed password is correct for the account [%s][id: %d].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -3107,18 +3107,18 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] sex changing failed.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); PRINTF("Account [%s] doesn't exist or the sex is already the good sex.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account sex changing failed. The compte [%s] doesn't exist or the sex is already the good sex.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("Account [%s][id: %d] sex successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Account [%s][id: %d] sex successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -3130,19 +3130,19 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] GM level changing failed.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); PRINTF("Account [%s] doesn't exist, the GM level is already the good GM level\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); PRINTF("or it's impossible to modify the GM accounts file.\n"); LADMIN_LOG("Account GM level changing failed. The compte [%s] doesn't exist, the GM level is already the good sex or it's impossible to modify the GM accounts file.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("Account [%s][id: %d] GM level successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Account [%s][id: %d] GM level successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -3154,18 +3154,18 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] e-mail changing failed.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); PRINTF("Account [%s] doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account e-mail changing failed. The compte [%s] doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("Account [%s][id: %d] e-mail successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Account [%s][id: %d] e-mail successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -3177,16 +3177,17 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] memo changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] memo changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + + static_cast(RFIFOP(fd, 6))); } else { PRINTF("Account [%s][id: %d] memo successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Account [%s][id: %d] memo successfully changed.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -3198,16 +3199,16 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Unable to find the account [%s] id. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Unable to find the account [%s] id. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { PRINTF("The account [%s] have the id: %d.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("The account [%s] have the id: %d.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -3226,9 +3227,9 @@ void parse_fromlogin(int fd) else { PRINTF("The account [id: %d] have the name: %s.\n", - RFIFOL(fd, 2), RFIFOP(fd, 6)); + RFIFOL(fd, 2), static_cast(RFIFOP(fd, 6))); LADMIN_LOG("The account [id: %d] have the name: %s.\n", - RFIFOL(fd, 2), RFIFOP(fd, 6)); + RFIFOL(fd, 2), static_cast(RFIFOP(fd, 6))); } bytes_to_read = 0; RFIFOSKIP(fd, 30); @@ -3240,9 +3241,9 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] validity limit changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] validity limit changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { @@ -3250,19 +3251,19 @@ void parse_fromlogin(int fd) if (timestamp == 0) { PRINTF("Validity Limit of the account [%s][id: %d] successfully changed to [unlimited].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Validity Limit of the account [%s][id: %d] successfully changed to [unlimited].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } else { timestamp_seconds_buffer tmpstr; stamp_time(tmpstr, ×tamp); PRINTF("Validity Limit of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), tmpstr); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2), tmpstr); LADMIN_LOG("Validity Limit of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), - tmpstr); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2), + tmpstr); } } bytes_to_read = 0; @@ -3275,9 +3276,9 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] final date of banishment changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] final date of banishment changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { @@ -3285,19 +3286,19 @@ void parse_fromlogin(int fd) if (timestamp == 0) { PRINTF("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } else { timestamp_seconds_buffer tmpstr; stamp_time(tmpstr, ×tamp); PRINTF("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), tmpstr); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2), tmpstr); LADMIN_LOG("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), - tmpstr); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2), + tmpstr); } } bytes_to_read = 0; @@ -3310,9 +3311,9 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] final date of banishment changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] final date of banishment changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { @@ -3320,19 +3321,20 @@ void parse_fromlogin(int fd) if (timestamp == 0) { PRINTF("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); LADMIN_LOG("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished].\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } else { timestamp_seconds_buffer tmpstr; stamp_time(tmpstr, ×tamp); PRINTF("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), tmpstr); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2), + tmpstr); LADMIN_LOG("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), - tmpstr); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2), + tmpstr); } } bytes_to_read = 0; @@ -3362,9 +3364,9 @@ void parse_fromlogin(int fd) if (RFIFOL(fd, 2) == -1) { PRINTF("Account [%s] validity limit changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); LADMIN_LOG("Account [%s] validity limit changing failed. Account doesn't exist.\n", - RFIFOP(fd, 6)); + static_cast(RFIFOP(fd, 6))); } else { @@ -3372,21 +3374,22 @@ void parse_fromlogin(int fd) if (timestamp == 0) { PRINTF("Validity limit of the account [%s][id: %d] unchanged.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); PRINTF("The account have an unlimited validity limit or\n"); PRINTF("the changing is impossible with the proposed adjustments.\n"); LADMIN_LOG("Validity limit of the account [%s][id: %d] unchanged. The account have an unlimited validity limit or the changing is impossible with the proposed adjustments.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2)); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2)); } else { timestamp_seconds_buffer tmpstr; stamp_time(tmpstr, ×tamp); PRINTF("Validity limit of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), tmpstr); + static_cast(RFIFOP(fd, 6)), RFIFOL(fd, 2), + tmpstr); LADMIN_LOG("Validity limit of the account [%s][id: %d] successfully changed to be until %s.\n", - RFIFOP(fd, 6), RFIFOL(fd, 2), - tmpstr); + static_cast( RFIFOP(fd, 6)), RFIFOL(fd, 2), + tmpstr); } } bytes_to_read = 0; @@ -3395,7 +3398,7 @@ void parse_fromlogin(int fd) case 0x7953: // answer of a request about informations of an account (by account name/id) if (RFIFOREST(fd) < 150 - || RFIFOREST(fd) <(150 + RFIFOW(fd, 148))) + || RFIFOREST(fd) < (150 + RFIFOW(fd, 148))) return; { char userid[24], error_message[20], lastlogin[24], diff --git a/src/login/login.cpp b/src/login/login.cpp index 1d01f72..b4d488d 100644 --- a/src/login/login.cpp +++ b/src/login/login.cpp @@ -748,21 +748,17 @@ void check_auth_sync(timer_id, tick_t, custom_id_t, custom_data_t) // Packet send to all char-servers, except one (wos: without our self) //-------------------------------------------------------------------- static -int charif_sendallwos(int sfd, unsigned char *buf, unsigned int len) +void charif_sendallwos(int sfd, const uint8_t *buf, size_t len) { - int i, c; - - for (i = 0, c = 0; i < MAX_SERVERS; i++) + for (int i = 0; i < MAX_SERVERS; i++) { - int fd; - if ((fd = server_fd[i]) >= 0 && fd != sfd) + int fd = server_fd[i]; + if (fd >= 0 && fd != sfd) { memcpy(WFIFOP(fd, 0), buf, len); WFIFOSET(fd, len); - c++; } } - return c; } //----------------------------------------------------- @@ -1769,7 +1765,7 @@ static void parse_admin(int fd) { int i, j; - char *account_name; + char account_name[24]; const char *ip = ip2str(session[fd]->client_addr.sin_addr); @@ -1878,7 +1874,10 @@ void parse_admin(int fd) { // [Fate] Itemfrob package: change item IDs if (RFIFOREST(fd) < 10) return; - charif_sendallwos(-1, RFIFOP(fd, 0), 10); // forward package to char servers + uint8_t buf[10]; + memcpy(buf, RFIFOP(fd, 0), 10); + // forward package to char servers + charif_sendallwos(-1, buf, 10); RFIFOSKIP(fd, 10); WFIFOW(fd, 0) = 0x7925; WFIFOSET(fd, 2); @@ -1890,19 +1889,14 @@ void parse_admin(int fd) return; { struct mmo_account ma; - ma.userid = (char *)RFIFOP(fd, 2); - ma.passwd = (char *)RFIFOP(fd, 26); + strzcpy(ma.userid, static_cast(RFIFOP(fd, 2)), 24); + strzcpy(ma.passwd, static_cast(RFIFOP(fd, 26)), 24); memcpy(ma.lastlogin, "-", 2); ma.sex = RFIFOB(fd, 50); WFIFOW(fd, 0) = 0x7931; WFIFOL(fd, 2) = -1; - memcpy(WFIFOP(fd, 6), RFIFOP(fd, 2), 24); - if (strlen(ma.userid) > 23 || strlen(ma.passwd) > 23) - { - LOGIN_LOG("'ladmin': Attempt to create an invalid account (account or pass is too long, ip: %s)\n", - ip); - } - else if (strlen(ma.userid) < 4 || strlen(ma.passwd) < 4) + memcpy(WFIFOP(fd, 6), ma.userid, 24); + if (strlen(ma.userid) < 4 || strlen(ma.passwd) < 4) { LOGIN_LOG("'ladmin': Attempt to create an invalid account (account or pass is too short, ip: %s)\n", ip); @@ -1935,8 +1929,7 @@ void parse_admin(int fd) { int new_id; char email[40]; - memcpy(email, RFIFOP(fd, 51), 40); - email[39] = '\0'; + strzcpy(email, static_cast(RFIFOP(fd, 51)), 40); remove_control_chars(email); new_id = mmo_auth_new(&ma, ma.sex, email); LOGIN_LOG("'ladmin': Account creation (account: %s (id: %d), sex: %c, email: %s, ip: %s)\n", @@ -1955,15 +1948,14 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x7933; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) { { // Char-server is notified of deletion (for characters deletion). - unsigned char buf[65535]; + uint8_t buf[6]; WBUFW(buf, 0) = 0x2730; WBUFL(buf, 2) = auth_dat[i].account_id; charif_sendallwos(-1, buf, 6); @@ -1999,15 +1991,13 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x7935; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) { memcpy(WFIFOP(fd, 6), auth_dat[i].userid, 24); - strcpy(auth_dat[i].pass, MD5_saltcrypt((char *)RFIFOP(fd, 26), make_salt())); - auth_dat[i].pass[39] = '\0'; + strzcpy(auth_dat[i].pass, MD5_saltcrypt(static_cast(RFIFOP(fd, 26)), make_salt()), 40); WFIFOL(fd, 2) = auth_dat[i].account_id; LOGIN_LOG("'ladmin': Modification of a password (account: %s, new password: %s, ip: %s)\n", auth_dat[i].userid, auth_dat[i].pass, ip); @@ -2030,12 +2020,10 @@ void parse_admin(int fd) int statut; WFIFOW(fd, 0) = 0x7937; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); statut = RFIFOL(fd, 26); - memcpy(error_message, RFIFOP(fd, 30), 20); - error_message[19] = '\0'; + strzcpy(error_message, static_cast(RFIFOP(fd, 30)), 20); remove_control_chars(error_message); if (statut != 7 || error_message[0] == '\0') { // 7: // 6 = Your are Prohibited to log in until %s @@ -2120,14 +2108,15 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x793b; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) { memcpy(WFIFOP(fd, 6), auth_dat[i].userid, 24); - if ( pass_ok((char *)RFIFOP(fd, 26), auth_dat[i].pass) ) + char pass[24]; + strzcpy(pass, static_cast(RFIFOP(fd, 26)), 24); + if (pass_ok(pass, auth_dat[i].pass)) { WFIFOL(fd, 2) = auth_dat[i].account_id; LOGIN_LOG("'ladmin': Check of password OK (account: %s, password: %s, ip: %s)\n", @@ -2136,9 +2125,6 @@ void parse_admin(int fd) } else { - char pass[24]; - memcpy(pass, RFIFOP(fd, 26), 24); - pass[23] = '\0'; remove_control_chars(pass); LOGIN_LOG("'ladmin': Failure of password check (account: %s, proposed pass: %s, ip: %s)\n", auth_dat[i].userid, pass, ip); @@ -2159,8 +2145,7 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x793d; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); memcpy(WFIFOP(fd, 6), account_name, 24); { @@ -2226,8 +2211,7 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x793f; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); memcpy(WFIFOP(fd, 6), account_name, 24); { @@ -2373,13 +2357,12 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x7941; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); memcpy(WFIFOP(fd, 6), account_name, 24); { char email[40]; - memcpy(email, RFIFOP(fd, 26), 40); + strzcpy(email, static_cast(RFIFOP(fd, 26)), 40); if (e_mail_check(email) == 0) { LOGIN_LOG("'ladmin': Attempt to give an invalid e-mail (account: %s, ip: %s)\n", @@ -2414,8 +2397,7 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x7943; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) @@ -2458,8 +2440,7 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x7945; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) @@ -2512,8 +2493,7 @@ void parse_admin(int fd) { WFIFOW(fd, 0) = 0x7949; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); time_t timestamp = static_cast(RFIFOL(fd, 26)); timestamp_seconds_buffer tmpstr; @@ -2548,8 +2528,7 @@ void parse_admin(int fd) { WFIFOW(fd, 0) = 0x794b; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); time_t timestamp = static_cast(RFIFOL(fd, 26)); if (timestamp <= time(NULL)) @@ -2605,8 +2584,7 @@ void parse_admin(int fd) struct tm *tmtime; WFIFOW(fd, 0) = 0x794d; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) @@ -2756,8 +2734,7 @@ void parse_admin(int fd) struct tm *tmtime; WFIFOW(fd, 0) = 0x7951; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) @@ -2849,8 +2826,7 @@ void parse_admin(int fd) return; WFIFOW(fd, 0) = 0x7953; WFIFOL(fd, 2) = -1; - account_name = (char *)RFIFOP(fd, 2); - account_name[23] = '\0'; + strzcpy(account_name, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account_name); i = search_account_index(account_name); if (i != -1) @@ -3064,13 +3040,15 @@ void parse_login(int fd) { if (RFIFOREST(fd) >= ((RFIFOW(fd, 0) == 0x64) ? 55 : 47)) PRINTF("parse_login: connection #%d, packet: 0x%x (with being read: %d), account: %s.\n", - fd, RFIFOW(fd, 0), RFIFOREST(fd), RFIFOP(fd, 6)); + fd, RFIFOW(fd, 0), RFIFOREST(fd), + static_cast(RFIFOP(fd, 6))); } else if (RFIFOW(fd, 0) == 0x2710) { if (RFIFOREST(fd) >= 86) PRINTF("parse_login: connection #%d, packet: 0x%x (with being read: %d), server: %s.\n", - fd, RFIFOW(fd, 0), RFIFOREST(fd), RFIFOP(fd, 60)); + fd, RFIFOW(fd, 0), RFIFOREST(fd), + static_cast(RFIFOP(fd, 60))); } else PRINTF("parse_login: connection #%d, packet: 0x%x (with being read: %d).\n", @@ -3092,31 +3070,17 @@ void parse_login(int fd) break; case 0x64: // Ask connection of a client - case 0x01dd: // Ask connection of a client (encryption mode) - if (RFIFOREST(fd) < ((RFIFOW(fd, 0) == 0x64) ? 55 : 47)) + if (RFIFOREST(fd) < 55) return; - account.userid = (char *)RFIFOP(fd, 6); - account.userid[23] = '\0'; + strzcpy(account.userid, static_cast(RFIFOP(fd, 6)), 24); remove_control_chars(account.userid); - account.passwd = (char *)RFIFOP(fd, 30); - if (RFIFOW(fd, 0) == 0x64) - { - account.passwd[23] = '\0'; - remove_control_chars(account.passwd); - } + strzcpy(account.passwd, static_cast(RFIFOP(fd, 30)), 24); + remove_control_chars(account.passwd); account.passwdenc = 0; - if (RFIFOW(fd, 0) == 0x64) - { - LOGIN_LOG("Request for connection (non encryption mode) of %s (ip: %s).\n", - account.userid, ip); - } - else - { - LOGIN_LOG("Request for connection (encryption mode) of %s (ip: %s).\n", - account.userid, ip); - } + LOGIN_LOG("Request for connection (non encryption mode) of %s (ip: %s).\n", + account.userid, ip); if (!check_ip(session[fd]->client_addr.sin_addr)) { @@ -3125,12 +3089,19 @@ void parse_login(int fd) WFIFOW(fd, 0) = 0x6a; WFIFOB(fd, 2) = 0x03; WFIFOSET(fd, 3); - RFIFOSKIP(fd, (RFIFOW(fd, 0) == 0x64) ? 55 : 47); + RFIFOSKIP(fd, 55); break; } result = mmo_auth(&account, fd); if (result == -1) + { + int version_2 = RFIFOB(fd, 54); + if (!(version_2 & VERSION_2_UPDATEHOST) + || !(version_2 & VERSION_2_SERVERORDER)) + result = 5; // client too old + } + if (result == -1) { int gm_level = isGM(account.account_id); if (min_level_to_connect > gm_level) @@ -3144,7 +3115,7 @@ void parse_login(int fd) } else { - int version_2 = RFIFOB(fd, 54); // version 2 + // int version_2 = RFIFOB(fd, 54); // version 2 if (gm_level) PRINTF("Connection of the GM (level:%d) account '%s' accepted.\n", @@ -3161,9 +3132,10 @@ void parse_login(int fd) * from the incoming 0x64 packet (the byte at offset 54). If bit 0 of this is set, * then the client can safely accept the 0x63 packet. The "version 2" value is not * otherwise used by eAthena. + * + * All supported clients now send both, so the check is removed. */ - if ((RFIFOW(fd, 0) == 0x64) - && (version_2 & VERSION_2_UPDATEHOST)) + // if (version_2 & VERSION_2_UPDATEHOST) { host_len = (int) strlen(update_host); if (host_len > 0) @@ -3178,54 +3150,30 @@ void parse_login(int fd) // Load list of char servers into outbound packet server_num = 0; - if (version_2 & VERSION_2_SERVERORDER) - for (int i = 0; i < MAX_SERVERS; i++) - { - if (server_fd[i] >= 0) - { - if (lan_ip_check(p)) - WFIFOL(fd, 47 + server_num * 32) = - inet_addr(lan_char_ip); - else - WFIFOL(fd, 47 + server_num * 32) = - server[i].ip; - WFIFOW(fd, 47 + server_num * 32 + 4) = - server[i].port; - memcpy(WFIFOP(fd, 47 + server_num * 32 + 6), - server[i].name, 20); - WFIFOW(fd, 47 + server_num * 32 + 26) = - server[i].users; - WFIFOW(fd, 47 + server_num * 32 + 28) = - server[i].maintenance; - WFIFOW(fd, 47 + server_num * 32 + 30) = - server[i].is_new; - server_num++; - } - } - else // Send them in reverse, as the client defaults to the second (!) one - for (int i = MAX_SERVERS - 1; i >= 0; i--) + // if (version_2 & VERSION_2_SERVERORDER) + for (int i = 0; i < MAX_SERVERS; i++) + { + if (server_fd[i] >= 0) { - if (server_fd[i] >= 0) - { - if (lan_ip_check(p)) - WFIFOL(fd, 47 + server_num * 32) = - inet_addr(lan_char_ip); - else - WFIFOL(fd, 47 + server_num * 32) = - server[i].ip; - WFIFOW(fd, 47 + server_num * 32 + 4) = - server[i].port; - memcpy(WFIFOP(fd, 47 + server_num * 32 + 6), - server[i].name, 20); - WFIFOW(fd, 47 + server_num * 32 + 26) = - server[i].users; - WFIFOW(fd, 47 + server_num * 32 + 28) = - server[i].maintenance; - WFIFOW(fd, 47 + server_num * 32 + 30) = - server[i].is_new; - server_num++; - } + if (lan_ip_check(p)) + WFIFOL(fd, 47 + server_num * 32) = + inet_addr(lan_char_ip); + else + WFIFOL(fd, 47 + server_num * 32) = + server[i].ip; + WFIFOW(fd, 47 + server_num * 32 + 4) = + server[i].port; + memcpy(WFIFOP(fd, 47 + server_num * 32 + 6), + server[i].name, 20); + WFIFOW(fd, 47 + server_num * 32 + 26) = + server[i].users; + WFIFOW(fd, 47 + server_num * 32 + 28) = + server[i].maintenance; + WFIFOW(fd, 47 + server_num * 32 + 30) = + server[i].is_new; + server_num++; } + } // if at least 1 char-server if (server_num > 0) { @@ -3340,16 +3288,13 @@ void parse_login(int fd) return; { int GM_value, len; - char *server_name; - account.userid = (char *)RFIFOP(fd, 2); - account.userid[23] = '\0'; + char server_name[20]; + strzcpy(account.userid, static_cast(RFIFOP(fd, 2)), 24); remove_control_chars(account.userid); - account.passwd = (char *)RFIFOP(fd, 26); - account.passwd[23] = '\0'; + strzcpy(account.passwd, static_cast(RFIFOP(fd, 26)), 24); remove_control_chars(account.passwd); account.passwdenc = 0; - server_name = (char *)RFIFOP(fd, 60); - server_name[19] = '\0'; + strzcpy(server_name, static_cast(RFIFOP(fd, 60)), 20); remove_control_chars(server_name); LOGIN_LOG("Connection request of the char-server '%s' @ %d.%d.%d.%d:%d (ip: %s)\n", server_name, RFIFOB(fd, 54), RFIFOB(fd, 55), @@ -3470,9 +3415,8 @@ void parse_login(int fd) struct login_session_data *ld = (struct login_session_data *)session[fd]->session_data; if (RFIFOW(fd, 2) == 0) { // non encrypted password - char *password; - password = (char *)RFIFOP(fd, 4); - password[23] = '\0'; + char password[24]; + strzcpy(password, static_cast(RFIFOP(fd, 4)), 24); remove_control_chars(password); // If remote administration is enabled and password sent by client matches password read from login server configuration file if ((admin_state == 1) @@ -4156,7 +4100,7 @@ void save_config_in_log(void) LOGIN_LOG("- to accept following IP for remote administration:\n"); for (i = 0; i < access_ladmin_allownum; i++) LOGIN_LOG(" %s\n", - (char *)(access_ladmin_allow + i * ACO_STRSIZE)); + access_ladmin_allow + i * ACO_STRSIZE); } if (gm_pass[0] == '\0') @@ -4249,7 +4193,7 @@ void save_config_in_log(void) LOGIN_LOG("- with the IP security order: 'deny,allow' (allow if not deny). Refused IP are:\n"); for (i = 0; i < access_denynum; i++) LOGIN_LOG(" %s\n", - (char *)(access_deny + i * ACO_STRSIZE)); + access_deny + i * ACO_STRSIZE); } } else if (access_order == ACO_ALLOW_DENY) @@ -4267,7 +4211,7 @@ void save_config_in_log(void) LOGIN_LOG("- with the IP security order: 'allow,deny' (deny if not allow). Authorised IP are:\n"); for (i = 0; i < access_allownum; i++) LOGIN_LOG(" %s\n", - (char *)(access_allow + i * ACO_STRSIZE)); + access_allow + i * ACO_STRSIZE); } } else @@ -4292,12 +4236,12 @@ void save_config_in_log(void) LOGIN_LOG(" Authorised IP are:\n"); for (i = 0; i < access_allownum; i++) LOGIN_LOG(" %s\n", - (char *)(access_allow + i * ACO_STRSIZE)); + access_allow + i * ACO_STRSIZE); } LOGIN_LOG(" Refused IP are:\n"); for (i = 0; i < access_denynum; i++) LOGIN_LOG(" %s\n", - (char *)(access_deny + i * ACO_STRSIZE)); + access_deny + i * ACO_STRSIZE); } } } diff --git a/src/login/login.hpp b/src/login/login.hpp index d6f8b2f..4d018de 100644 --- a/src/login/login.hpp +++ b/src/login/login.hpp @@ -11,8 +11,8 @@ struct mmo_account { - char *userid; - char *passwd; + char userid[24]; + char passwd[24]; int passwdenc; long account_id; diff --git a/src/map/atcommand.cpp b/src/map/atcommand.cpp index 46c0e9e..3216f91 100644 --- a/src/map/atcommand.cpp +++ b/src/map/atcommand.cpp @@ -37,9 +37,6 @@ #include "tmw.hpp" #include "trade.hpp" -static -char command_symbol = '@'; // first char of the commands (by [Yor]) - #define ATCOMMAND_FUNC(x) static \ int atcommand_##x(const int fd, struct map_session_data* sd, const char* command, const char* message) ATCOMMAND_FUNC(setup); @@ -682,7 +679,7 @@ bool is_atcommand(const int fd, struct map_session_data *sd, AtCommandType atcommand(const int level, const char *message, struct AtCommandInfo * info) { - char *p = (char *) message; // it's 'char' and not 'const char' to have possibility to modify the first character if necessary + const char *p = message; if (!info) return AtCommand_None; @@ -694,7 +691,7 @@ AtCommandType atcommand(const int level, const char *message, return AtCommand_None; } - if (*p == command_symbol) + if (*p == '@') { // check first char. char command[101]; int i = 0; @@ -704,10 +701,9 @@ AtCommandType atcommand(const int level, const char *message, while (atcommand_info[i].type != AtCommand_Unknown) { - if (strcasecmp(command + 1, atcommand_info[i].command + 1) == 0 + if (strcasecmp(command, atcommand_info[i].command) == 0 && level >= atcommand_info[i].level) { - p[0] = atcommand_info[i].command[0]; // set correct first symbol for after. break; } i++; @@ -798,9 +794,6 @@ int atcommand_config_read(const char *cfgName) if (strcasecmp(w1, "import") == 0) atcommand_config_read(w2); - else if (strcasecmp(w1, "command_symbol") == 0 && w2[0] > 31 && w2[0] != '/' && // symbol of standard ragnarok GM commands - w2[0] != '%') // symbol of party chat speaking - command_symbol = w2[0]; } fclose_(fp); @@ -1888,10 +1881,9 @@ int atcommand_item(const int fd, struct map_session_data *sd, item_tmp.nameid = item_id; item_tmp.identify = 1; PickupFail flag; - if ((flag = pc_additem((struct map_session_data *) sd, - &item_tmp, get_count)) + if ((flag = pc_additem(sd, &item_tmp, get_count)) != PickupFail::OKAY) - clif_additem((struct map_session_data *) sd, 0, 0, flag); + clif_additem(sd, 0, 0, flag); } clif_displaymessage(fd, "Item created."); } @@ -2476,8 +2468,7 @@ int atcommand_spawn(const int fd, struct map_session_data *sd, my = sd->bl.y + (MRAND(range) - (range / 2)); else my = y; - k = mob_once_spawn((struct map_session_data *) sd, "this", mx, - my, "", mob_id, 1, ""); + k = mob_once_spawn(sd, "this", mx, my, "", mob_id, 1, ""); } count += (k != 0) ? 1 : 0; } diff --git a/src/map/battle.cpp b/src/map/battle.cpp index 7da685f..93f6fe6 100644 --- a/src/map/battle.cpp +++ b/src/map/battle.cpp @@ -3628,7 +3628,7 @@ struct Damage battle_calc_pc_weapon_attack(struct block_list *src, && sd->inventory_data[index]->type == ItemType::WEAPON) { damage += - (int)(double)(sd->inventory_data[index]->weight * + (int)(sd->inventory_data[index]->weight * (0.8 * skill_lv * 4 / 10)); damage += sd->status.inventory[index].refine * pc_getrefinebonus(0, diff --git a/src/map/chrif.cpp b/src/map/chrif.cpp index 6ab786c..ee8113a 100644 --- a/src/map/chrif.cpp +++ b/src/map/chrif.cpp @@ -667,7 +667,7 @@ int chrif_changedsex(int fd) { if (sd->status.inventory[i].nameid && bool(sd->status.inventory[i].equip)) - pc_unequipitem((struct map_session_data *) sd, i, CalcStatus::NOW); + pc_unequipitem(sd, i, CalcStatus::NOW); } // reset skill of some job if (s_class.job == 19 || s_class.job == 4020 @@ -1129,7 +1129,7 @@ void chrif_parse(int fd) case 0x2afd: pc_authok(RFIFOL(fd, 4), RFIFOL(fd, 8), (time_t) RFIFOL(fd, 12), RFIFOW(fd, 16), - (struct mmo_charstatus *) RFIFOP(fd, 18)); + (const struct mmo_charstatus *) RFIFOP(fd, 18)); break; case 0x2afe: pc_authfail(RFIFOL(fd, 2)); diff --git a/src/map/clif.cpp b/src/map/clif.cpp index edae508..88b3839 100644 --- a/src/map/clif.cpp +++ b/src/map/clif.cpp @@ -128,11 +128,35 @@ enum class SendWho #define SELF SendWho::SELF }; -#define WBUFPOS(p,pos,x,y) { unsigned char *__p = (p); __p+= (pos); __p[0] = (x)>>2; __p[1] = ((x)<<6) | (((y)>>4)&0x3f); __p[2] = (y)<<4; } -#define WBUFPOS2(p,pos,x0,y0,x1,y1) { unsigned char *__p = (p); __p+= (pos); __p[0] = (x0)>>2; __p[1] = ((x0)<<6) | (((y0)>>4)&0x3f); __p[2] = ((y0)<<4) | (((x1)>>6)&0x0f); __p[3]= ((x1)<<2) | (((y1)>>8)&0x03); __p[4]= (y1); } +inline +void WBUFPOS(uint8_t *p, size_t pos, uint16_t x, uint16_t y) +{ + p += pos; + p[0] = x >> 2; + p[1] = (x << 6) | ((y >> 4) & 0x3f); + p[2] = y << 4; +} +inline +void WBUFPOS2(uint8_t *p, size_t pos, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) +{ + p += pos; + p[0] = x0 >> 2; + p[1] = (x0 << 6) | ((y0 >> 4) & 0x3f); + p[2] = (y0 << 4) | ((x1 >> 6) & 0x0f); + p[3] = (x1 << 2) | ((y1 >> 8) & 0x03); + p[4] = y1; +} -#define WFIFOPOS(fd,pos,x,y) { WBUFPOS(WFIFOP(fd,pos),0,x,y); } -#define WFIFOPOS2(fd,pos,x0,y0,x1,y1) { WBUFPOS2(WFIFOP(fd,pos),0,x0,y0,x1,y1); } +inline +void WFIFOPOS(int fd, size_t pos, uint16_t x, uint16_t y) +{ + WBUFPOS(static_cast(WFIFOP(fd, pos)), 0, x, y); +} +inline +void WFIFOPOS2(int fd, size_t pos, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) +{ + WBUFPOS2(static_cast(WFIFOP(fd, pos)), 0, x0, y0, x1, y1); +} static char map_ip_str[16]; @@ -235,7 +259,7 @@ void clif_emotion_towards(struct block_list *bl, static char *clif_validate_chat(struct map_session_data *sd, int type, - char **message, size_t *message_len); + const char **message, size_t *message_len); /*========================================== * clif_sendでAREA*指定時用 @@ -282,7 +306,7 @@ void clif_send_sub(struct block_list *bl, const unsigned char *buf, int len, { PRINTF("WARNING: Invalid use of clif_send function\n"); PRINTF(" Packet x%4x use a WFIFO of a player instead of to use a buffer.\n", - WBUFW(buf, 0)); + RBUFW(buf, 0)); PRINTF(" Please correct your code.\n"); // don't send to not move the pointer of the packet for next sessions in the loop } @@ -1603,29 +1627,25 @@ int clif_cutin(struct map_session_data *sd, const char *image, int type) */ int clif_additem(struct map_session_data *sd, int n, int amount, PickupFail fail) { - int fd, j; - unsigned char *buf; - nullpo_ret(sd); - fd = sd->fd; - buf = WFIFOP(fd, 0); + int fd = sd->fd; if (fail != PickupFail::OKAY) { - WBUFW(buf, 0) = 0xa0; - WBUFW(buf, 2) = n + 2; - WBUFW(buf, 4) = amount; - WBUFW(buf, 6) = 0; - WBUFB(buf, 8) = 0; - WBUFB(buf, 9) = 0; - WBUFB(buf, 10) = 0; - WBUFW(buf, 11) = 0; - WBUFW(buf, 13) = 0; - WBUFW(buf, 15) = 0; - WBUFW(buf, 17) = 0; - WBUFW(buf, 19) = 0; - WBUFB(buf, 21) = 0; - WBUFB(buf, 22) = uint8_t(fail); + WFIFOW(fd, 0) = 0xa0; + WFIFOW(fd, 2) = n + 2; + WFIFOW(fd, 4) = amount; + WFIFOW(fd, 6) = 0; + WFIFOB(fd, 8) = 0; + WFIFOB(fd, 9) = 0; + WFIFOB(fd, 10) = 0; + WFIFOW(fd, 11) = 0; + WFIFOW(fd, 13) = 0; + WFIFOW(fd, 15) = 0; + WFIFOW(fd, 17) = 0; + WFIFOW(fd, 19) = 0; + WFIFOB(fd, 21) = 0; + WFIFOB(fd, 22) = uint8_t(fail); } else { @@ -1633,56 +1653,57 @@ int clif_additem(struct map_session_data *sd, int n, int amount, PickupFail fail || sd->inventory_data[n] == NULL) return 1; - WBUFW(buf, 0) = 0xa0; - WBUFW(buf, 2) = n + 2; - WBUFW(buf, 4) = amount; + WFIFOW(fd, 0) = 0xa0; + WFIFOW(fd, 2) = n + 2; + WFIFOW(fd, 4) = amount; if (sd->inventory_data[n]->view_id > 0) - WBUFW(buf, 6) = sd->inventory_data[n]->view_id; + WFIFOW(fd, 6) = sd->inventory_data[n]->view_id; else - WBUFW(buf, 6) = sd->status.inventory[n].nameid; - WBUFB(buf, 8) = sd->status.inventory[n].identify; + WFIFOW(fd, 6) = sd->status.inventory[n].nameid; + WFIFOB(fd, 8) = sd->status.inventory[n].identify; if (sd->status.inventory[n].broken == 1) - WBUFB(buf, 9) = 1; // is weapon broken [Valaris] + WFIFOB(fd, 9) = 1; // is weapon broken [Valaris] else - WBUFB(buf, 9) = sd->status.inventory[n].attribute; - WBUFB(buf, 10) = sd->status.inventory[n].refine; + WFIFOB(fd, 9) = sd->status.inventory[n].attribute; + WFIFOB(fd, 10) = sd->status.inventory[n].refine; if (sd->status.inventory[n].card[0] == 0x00ff || sd->status.inventory[n].card[0] == 0x00fe || sd->status.inventory[n].card[0] == (short) 0xff00) { - WBUFW(buf, 11) = sd->status.inventory[n].card[0]; - WBUFW(buf, 13) = sd->status.inventory[n].card[1]; - WBUFW(buf, 15) = sd->status.inventory[n].card[2]; - WBUFW(buf, 17) = sd->status.inventory[n].card[3]; + WFIFOW(fd, 11) = sd->status.inventory[n].card[0]; + WFIFOW(fd, 13) = sd->status.inventory[n].card[1]; + WFIFOW(fd, 15) = sd->status.inventory[n].card[2]; + WFIFOW(fd, 17) = sd->status.inventory[n].card[3]; } else { + int j; if (sd->status.inventory[n].card[0] > 0 && (j = itemdb_viewid(sd->status.inventory[n].card[0])) > 0) - WBUFW(buf, 11) = j; + WFIFOW(fd, 11) = j; else - WBUFW(buf, 11) = sd->status.inventory[n].card[0]; + WFIFOW(fd, 11) = sd->status.inventory[n].card[0]; if (sd->status.inventory[n].card[1] > 0 && (j = itemdb_viewid(sd->status.inventory[n].card[1])) > 0) - WBUFW(buf, 13) = j; + WFIFOW(fd, 13) = j; else - WBUFW(buf, 13) = sd->status.inventory[n].card[1]; + WFIFOW(fd, 13) = sd->status.inventory[n].card[1]; if (sd->status.inventory[n].card[2] > 0 && (j = itemdb_viewid(sd->status.inventory[n].card[2])) > 0) - WBUFW(buf, 15) = j; + WFIFOW(fd, 15) = j; else - WBUFW(buf, 15) = sd->status.inventory[n].card[2]; + WFIFOW(fd, 15) = sd->status.inventory[n].card[2]; if (sd->status.inventory[n].card[3] > 0 && (j = itemdb_viewid(sd->status.inventory[n].card[3])) > 0) - WBUFW(buf, 17) = j; + WFIFOW(fd, 17) = j; else - WBUFW(buf, 17) = sd->status.inventory[n].card[3]; + WFIFOW(fd, 17) = sd->status.inventory[n].card[3]; } - WBUFW(buf, 19) = uint16_t(pc_equippoint(sd, n)); - WBUFB(buf, 21) = uint8_t(sd->inventory_data[n]->type == ItemType::_7 + WFIFOW(fd, 19) = uint16_t(pc_equippoint(sd, n)); + WFIFOB(fd, 21) = uint8_t(sd->inventory_data[n]->type == ItemType::_7 ? ItemType::WEAPON : sd->inventory_data[n]->type); - WBUFB(buf, 22) = uint8_t(fail); + WFIFOB(fd, 22) = uint8_t(fail); } WFIFOSET(fd, packet_len_table[0xa0]); @@ -1715,45 +1736,43 @@ int clif_delitem(struct map_session_data *sd, int n, int amount) */ int clif_itemlist(struct map_session_data *sd) { - int i, n, fd, arrow = -1; - unsigned char *buf; - nullpo_ret(sd); - fd = sd->fd; - buf = WFIFOP(fd, 0); - WBUFW(buf, 0) = 0x1ee; - for (i = 0, n = 0; i < MAX_INVENTORY; i++) + int n = 0; + int arrow = -1; + int fd = sd->fd; + WFIFOW(fd, 0) = 0x1ee; + for (int i = 0; i < MAX_INVENTORY; i++) { if (sd->status.inventory[i].nameid <= 0 || sd->inventory_data[i] == NULL || itemdb_isequip2(sd->inventory_data[i])) continue; - WBUFW(buf, n * 18 + 4) = i + 2; + WFIFOW(fd, n * 18 + 4) = i + 2; if (sd->inventory_data[i]->view_id > 0) - WBUFW(buf, n * 18 + 6) = sd->inventory_data[i]->view_id; + WFIFOW(fd, n * 18 + 6) = sd->inventory_data[i]->view_id; else - WBUFW(buf, n * 18 + 6) = sd->status.inventory[i].nameid; - WBUFB(buf, n * 18 + 8) = uint8_t(sd->inventory_data[i]->type); - WBUFB(buf, n * 18 + 9) = sd->status.inventory[i].identify; - WBUFW(buf, n * 18 + 10) = sd->status.inventory[i].amount; + WFIFOW(fd, n * 18 + 6) = sd->status.inventory[i].nameid; + WFIFOB(fd, n * 18 + 8) = uint8_t(sd->inventory_data[i]->type); + WFIFOB(fd, n * 18 + 9) = sd->status.inventory[i].identify; + WFIFOW(fd, n * 18 + 10) = sd->status.inventory[i].amount; if (sd->inventory_data[i]->equip == EPOS::ARROW) { - WBUFW(buf, n * 18 + 12) = uint16_t(EPOS::ARROW); + WFIFOW(fd, n * 18 + 12) = uint16_t(EPOS::ARROW); if (bool(sd->status.inventory[i].equip)) arrow = i; // ついでに矢装備チェック } else - WBUFW(buf, n * 18 + 12) = uint16_t(EPOS::ZERO); - WBUFW(buf, n * 18 + 14) = sd->status.inventory[i].card[0]; - WBUFW(buf, n * 18 + 16) = sd->status.inventory[i].card[1]; - WBUFW(buf, n * 18 + 18) = sd->status.inventory[i].card[2]; - WBUFW(buf, n * 18 + 20) = sd->status.inventory[i].card[3]; + WFIFOW(fd, n * 18 + 12) = uint16_t(EPOS::ZERO); + WFIFOW(fd, n * 18 + 14) = sd->status.inventory[i].card[0]; + WFIFOW(fd, n * 18 + 16) = sd->status.inventory[i].card[1]; + WFIFOW(fd, n * 18 + 18) = sd->status.inventory[i].card[2]; + WFIFOW(fd, n * 18 + 20) = sd->status.inventory[i].card[3]; n++; } if (n) { - WBUFW(buf, 2) = 4 + n * 18; + WFIFOW(fd, 2) = 4 + n * 18; WFIFOSET(fd, WFIFOW(fd, 2)); } if (arrow >= 0) @@ -1767,74 +1786,72 @@ int clif_itemlist(struct map_session_data *sd) */ int clif_equiplist(struct map_session_data *sd) { - int i, j, n, fd; - unsigned char *buf; - nullpo_ret(sd); - fd = sd->fd; - buf = WFIFOP(fd, 0); - WBUFW(buf, 0) = 0xa4; - for (i = 0, n = 0; i < MAX_INVENTORY; i++) + int fd = sd->fd; + WFIFOW(fd, 0) = 0xa4; + int n = 0; + for (int i = 0; i < MAX_INVENTORY; i++) { if (sd->status.inventory[i].nameid <= 0 || sd->inventory_data[i] == NULL || !itemdb_isequip2(sd->inventory_data[i])) continue; - WBUFW(buf, n * 20 + 4) = i + 2; + WFIFOW(fd, n * 20 + 4) = i + 2; if (sd->inventory_data[i]->view_id > 0) - WBUFW(buf, n * 20 + 6) = sd->inventory_data[i]->view_id; + WFIFOW(fd, n * 20 + 6) = sd->inventory_data[i]->view_id; else - WBUFW(buf, n * 20 + 6) = sd->status.inventory[i].nameid; - WBUFB(buf, n * 20 + 8) = uint8_t( + WFIFOW(fd, n * 20 + 6) = sd->status.inventory[i].nameid; + WFIFOB(fd, n * 20 + 8) = uint8_t( sd->inventory_data[i]->type == ItemType::_7 ? ItemType::WEAPON : sd->inventory_data[i]->type); - WBUFB(buf, n * 20 + 9) = sd->status.inventory[i].identify; - WBUFW(buf, n * 20 + 10) = uint16_t(pc_equippoint(sd, i)); - WBUFW(buf, n * 20 + 12) = uint16_t(sd->status.inventory[i].equip); + WFIFOB(fd, n * 20 + 9) = sd->status.inventory[i].identify; + WFIFOW(fd, n * 20 + 10) = uint16_t(pc_equippoint(sd, i)); + WFIFOW(fd, n * 20 + 12) = uint16_t(sd->status.inventory[i].equip); if (sd->status.inventory[i].broken == 1) - WBUFB(buf, n * 20 + 14) = 1; // is weapon broken [Valaris] + WFIFOB(fd, n * 20 + 14) = 1; // is weapon broken [Valaris] else - WBUFB(buf, n * 20 + 14) = sd->status.inventory[i].attribute; - WBUFB(buf, n * 20 + 15) = sd->status.inventory[i].refine; + WFIFOB(fd, n * 20 + 14) = sd->status.inventory[i].attribute; + WFIFOB(fd, n * 20 + 15) = sd->status.inventory[i].refine; if (sd->status.inventory[i].card[0] == 0x00ff || sd->status.inventory[i].card[0] == 0x00fe || sd->status.inventory[i].card[0] == (short) 0xff00) { - WBUFW(buf, n * 20 + 16) = sd->status.inventory[i].card[0]; - WBUFW(buf, n * 20 + 18) = sd->status.inventory[i].card[1]; - WBUFW(buf, n * 20 + 20) = sd->status.inventory[i].card[2]; - WBUFW(buf, n * 20 + 22) = sd->status.inventory[i].card[3]; + WFIFOW(fd, n * 20 + 16) = sd->status.inventory[i].card[0]; + WFIFOW(fd, n * 20 + 18) = sd->status.inventory[i].card[1]; + WFIFOW(fd, n * 20 + 20) = sd->status.inventory[i].card[2]; + WFIFOW(fd, n * 20 + 22) = sd->status.inventory[i].card[3]; } else { + int j; if (sd->status.inventory[i].card[0] > 0 && (j = itemdb_viewid(sd->status.inventory[i].card[0])) > 0) - WBUFW(buf, n * 20 + 16) = j; + WFIFOW(fd, n * 20 + 16) = j; else - WBUFW(buf, n * 20 + 16) = sd->status.inventory[i].card[0]; + WFIFOW(fd, n * 20 + 16) = sd->status.inventory[i].card[0]; if (sd->status.inventory[i].card[1] > 0 && (j = itemdb_viewid(sd->status.inventory[i].card[1])) > 0) - WBUFW(buf, n * 20 + 18) = j; + WFIFOW(fd, n * 20 + 18) = j; else - WBUFW(buf, n * 20 + 18) = sd->status.inventory[i].card[1]; + WFIFOW(fd, n * 20 + 18) = sd->status.inventory[i].card[1]; if (sd->status.inventory[i].card[2] > 0 && (j = itemdb_viewid(sd->status.inventory[i].card[2])) > 0) - WBUFW(buf, n * 20 + 20) = j; + WFIFOW(fd, n * 20 + 20) = j; else - WBUFW(buf, n * 20 + 20) = sd->status.inventory[i].card[2]; + WFIFOW(fd, n * 20 + 20) = sd->status.inventory[i].card[2]; if (sd->status.inventory[i].card[3] > 0 && (j = itemdb_viewid(sd->status.inventory[i].card[3])) > 0) - WBUFW(buf, n * 20 + 22) = j; + WFIFOW(fd, n * 20 + 22) = j; else - WBUFW(buf, n * 20 + 22) = sd->status.inventory[i].card[3]; + WFIFOW(fd, n * 20 + 22) = sd->status.inventory[i].card[3]; } n++; } if (n) { - WBUFW(buf, 2) = 4 + n * 20; + WFIFOW(fd, 2) = 4 + n * 20; WFIFOSET(fd, WFIFOW(fd, 2)); } return 0; @@ -1846,42 +1863,40 @@ int clif_equiplist(struct map_session_data *sd) */ int clif_storageitemlist(struct map_session_data *sd, struct storage *stor) { - struct item_data *id; - int i, n, fd; - unsigned char *buf; - nullpo_ret(sd); nullpo_ret(stor); - fd = sd->fd; - buf = WFIFOP(fd, 0); - WBUFW(buf, 0) = 0x1f0; - for (i = 0, n = 0; i < MAX_STORAGE; i++) + int fd = sd->fd; + WFIFOW(fd, 0) = 0x1f0; + int n = 0; + for (int i = 0; i < MAX_STORAGE; i++) { if (stor->storage_[i].nameid <= 0) continue; + + struct item_data *id; nullpo_ret(id = itemdb_search(stor->storage_[i].nameid)); if (itemdb_isequip2(id)) continue; - WBUFW(buf, n * 18 + 4) = i + 1; + WFIFOW(fd, n * 18 + 4) = i + 1; if (id->view_id > 0) - WBUFW(buf, n * 18 + 6) = id->view_id; + WFIFOW(fd, n * 18 + 6) = id->view_id; else - WBUFW(buf, n * 18 + 6) = stor->storage_[i].nameid; - WBUFB(buf, n * 18 + 8) = uint8_t(id->type); - WBUFB(buf, n * 18 + 9) = stor->storage_[i].identify; - WBUFW(buf, n * 18 + 10) = stor->storage_[i].amount; - WBUFW(buf, n * 18 + 12) = 0; - WBUFW(buf, n * 18 + 14) = stor->storage_[i].card[0]; - WBUFW(buf, n * 18 + 16) = stor->storage_[i].card[1]; - WBUFW(buf, n * 18 + 18) = stor->storage_[i].card[2]; - WBUFW(buf, n * 18 + 20) = stor->storage_[i].card[3]; + WFIFOW(fd, n * 18 + 6) = stor->storage_[i].nameid; + WFIFOB(fd, n * 18 + 8) = uint8_t(id->type); + WFIFOB(fd, n * 18 + 9) = stor->storage_[i].identify; + WFIFOW(fd, n * 18 + 10) = stor->storage_[i].amount; + WFIFOW(fd, n * 18 + 12) = 0; + WFIFOW(fd, n * 18 + 14) = stor->storage_[i].card[0]; + WFIFOW(fd, n * 18 + 16) = stor->storage_[i].card[1]; + WFIFOW(fd, n * 18 + 18) = stor->storage_[i].card[2]; + WFIFOW(fd, n * 18 + 20) = stor->storage_[i].card[3]; n++; } if (n) { - WBUFW(buf, 2) = 4 + n * 18; + WFIFOW(fd, 2) = 4 + n * 18; WFIFOSET(fd, WFIFOW(fd, 2)); } return 0; @@ -1893,74 +1908,73 @@ int clif_storageitemlist(struct map_session_data *sd, struct storage *stor) */ int clif_storageequiplist(struct map_session_data *sd, struct storage *stor) { - struct item_data *id; - int i, j, n, fd; - unsigned char *buf; - nullpo_ret(sd); nullpo_ret(stor); - fd = sd->fd; - buf = WFIFOP(fd, 0); - WBUFW(buf, 0) = 0xa6; - for (i = 0, n = 0; i < MAX_STORAGE; i++) + int fd = sd->fd; + WFIFOW(fd, 0) = 0xa6; + int n = 0; + for (int i = 0; i < MAX_STORAGE; i++) { if (stor->storage_[i].nameid <= 0) continue; + + struct item_data *id; nullpo_ret(id = itemdb_search(stor->storage_[i].nameid)); if (!itemdb_isequip2(id)) continue; - WBUFW(buf, n * 20 + 4) = i + 1; + WFIFOW(fd, n * 20 + 4) = i + 1; if (id->view_id > 0) - WBUFW(buf, n * 20 + 6) = id->view_id; + WFIFOW(fd, n * 20 + 6) = id->view_id; else - WBUFW(buf, n * 20 + 6) = stor->storage_[i].nameid; - WBUFB(buf, n * 20 + 8) = uint8_t(id->type); - WBUFB(buf, n * 20 + 9) = stor->storage_[i].identify; - WBUFW(buf, n * 20 + 10) = uint16_t(id->equip); - WBUFW(buf, n * 20 + 12) = uint16_t(stor->storage_[i].equip); + WFIFOW(fd, n * 20 + 6) = stor->storage_[i].nameid; + WFIFOB(fd, n * 20 + 8) = uint8_t(id->type); + WFIFOB(fd, n * 20 + 9) = stor->storage_[i].identify; + WFIFOW(fd, n * 20 + 10) = uint16_t(id->equip); + WFIFOW(fd, n * 20 + 12) = uint16_t(stor->storage_[i].equip); if (stor->storage_[i].broken == 1) - WBUFB(buf, n * 20 + 14) = 1; //is weapon broken [Valaris] + WFIFOB(fd, n * 20 + 14) = 1; //is weapon broken [Valaris] else - WBUFB(buf, n * 20 + 14) = stor->storage_[i].attribute; - WBUFB(buf, n * 20 + 15) = stor->storage_[i].refine; + WFIFOB(fd, n * 20 + 14) = stor->storage_[i].attribute; + WFIFOB(fd, n * 20 + 15) = stor->storage_[i].refine; if (stor->storage_[i].card[0] == 0x00ff || stor->storage_[i].card[0] == 0x00fe || stor->storage_[i].card[0] == (short) 0xff00) { - WBUFW(buf, n * 20 + 16) = stor->storage_[i].card[0]; - WBUFW(buf, n * 20 + 18) = stor->storage_[i].card[1]; - WBUFW(buf, n * 20 + 20) = stor->storage_[i].card[2]; - WBUFW(buf, n * 20 + 22) = stor->storage_[i].card[3]; + WFIFOW(fd, n * 20 + 16) = stor->storage_[i].card[0]; + WFIFOW(fd, n * 20 + 18) = stor->storage_[i].card[1]; + WFIFOW(fd, n * 20 + 20) = stor->storage_[i].card[2]; + WFIFOW(fd, n * 20 + 22) = stor->storage_[i].card[3]; } else { + int j; if (stor->storage_[i].card[0] > 0 && (j = itemdb_viewid(stor->storage_[i].card[0])) > 0) - WBUFW(buf, n * 20 + 16) = j; + WFIFOW(fd, n * 20 + 16) = j; else - WBUFW(buf, n * 20 + 16) = stor->storage_[i].card[0]; + WFIFOW(fd, n * 20 + 16) = stor->storage_[i].card[0]; if (stor->storage_[i].card[1] > 0 && (j = itemdb_viewid(stor->storage_[i].card[1])) > 0) - WBUFW(buf, n * 20 + 18) = j; + WFIFOW(fd, n * 20 + 18) = j; else - WBUFW(buf, n * 20 + 18) = stor->storage_[i].card[1]; + WFIFOW(fd, n * 20 + 18) = stor->storage_[i].card[1]; if (stor->storage_[i].card[2] > 0 && (j = itemdb_viewid(stor->storage_[i].card[2])) > 0) - WBUFW(buf, n * 20 + 20) = j; + WFIFOW(fd, n * 20 + 20) = j; else - WBUFW(buf, n * 20 + 20) = stor->storage_[i].card[2]; + WFIFOW(fd, n * 20 + 20) = stor->storage_[i].card[2]; if (stor->storage_[i].card[3] > 0 && (j = itemdb_viewid(stor->storage_[i].card[3])) > 0) - WBUFW(buf, n * 20 + 22) = j; + WFIFOW(fd, n * 20 + 22) = j; else - WBUFW(buf, n * 20 + 22) = stor->storage_[i].card[3]; + WFIFOW(fd, n * 20 + 22) = stor->storage_[i].card[3]; } n++; } if (n) { - WBUFW(buf, 2) = 4 + n * 20; + WFIFOW(fd, 2) = 4 + n * 20; WFIFOSET(fd, WFIFOW(fd, 2)); } return 0; @@ -2149,8 +2163,7 @@ int clif_changelook(struct block_list *bl, LOOK type, int val) int clif_changelook_towards(struct block_list *bl, LOOK type, int val, struct map_session_data *dstsd) { - unsigned char rbuf[32]; - unsigned char *buf = dstsd ? WFIFOP(dstsd->fd, 0) : rbuf; // pick target buffer or general-purpose one + unsigned char buf[32]; struct map_session_data *sd = NULL; nullpo_ret(bl); @@ -2240,7 +2253,7 @@ int clif_changelook_towards(struct block_list *bl, LOOK type, int val, WBUFW(buf, 7) = val; WBUFW(buf, 9) = 0; if (dstsd) - WFIFOSET(dstsd->fd, packet_len_table[0x1d7]); + clif_send(buf, packet_len_table[0x1d7], &dstsd->bl, SELF); else clif_send(buf, packet_len_table[0x1d7], bl, AREA); } @@ -2254,44 +2267,40 @@ int clif_changelook_towards(struct block_list *bl, LOOK type, int val, static int clif_initialstatus(struct map_session_data *sd) { - int fd; - unsigned char *buf; - nullpo_ret(sd); - fd = sd->fd; - buf = WFIFOP(fd, 0); - - WBUFW(buf, 0) = 0xbd; - WBUFW(buf, 2) = sd->status.status_point; - - WBUFB(buf, 4) = min(sd->status.attrs[ATTR::STR], 255); - WBUFB(buf, 5) = pc_need_status_point(sd, SP_STR); - WBUFB(buf, 6) = min(sd->status.attrs[ATTR::AGI], 255); - WBUFB(buf, 7) = pc_need_status_point(sd, SP_AGI); - WBUFB(buf, 8) = min(sd->status.attrs[ATTR::VIT], 255); - WBUFB(buf, 9) = pc_need_status_point(sd, SP_VIT); - WBUFB(buf, 10) = min(sd->status.attrs[ATTR::INT], 255); - WBUFB(buf, 11) = pc_need_status_point(sd, SP_INT); - WBUFB(buf, 12) = min(sd->status.attrs[ATTR::DEX], 255); - WBUFB(buf, 13) = pc_need_status_point(sd, SP_DEX); - WBUFB(buf, 14) = min(sd->status.attrs[ATTR::LUK], 255); - WBUFB(buf, 15) = pc_need_status_point(sd, SP_LUK); - - WBUFW(buf, 16) = sd->base_atk + sd->watk; - WBUFW(buf, 18) = sd->watk2; //atk bonus - WBUFW(buf, 20) = sd->matk1; - WBUFW(buf, 22) = sd->matk2; - WBUFW(buf, 24) = sd->def; // def - WBUFW(buf, 26) = sd->def2; - WBUFW(buf, 28) = sd->mdef; // mdef - WBUFW(buf, 30) = sd->mdef2; - WBUFW(buf, 32) = sd->hit; - WBUFW(buf, 34) = sd->flee; - WBUFW(buf, 36) = sd->flee2 / 10; - WBUFW(buf, 38) = sd->critical / 10; - WBUFW(buf, 40) = sd->status.karma; - WBUFW(buf, 42) = sd->status.manner; + int fd = sd->fd; + + WFIFOW(fd, 0) = 0xbd; + WFIFOW(fd, 2) = sd->status.status_point; + + WFIFOB(fd, 4) = min(sd->status.attrs[ATTR::STR], 255); + WFIFOB(fd, 5) = pc_need_status_point(sd, SP_STR); + WFIFOB(fd, 6) = min(sd->status.attrs[ATTR::AGI], 255); + WFIFOB(fd, 7) = pc_need_status_point(sd, SP_AGI); + WFIFOB(fd, 8) = min(sd->status.attrs[ATTR::VIT], 255); + WFIFOB(fd, 9) = pc_need_status_point(sd, SP_VIT); + WFIFOB(fd, 10) = min(sd->status.attrs[ATTR::INT], 255); + WFIFOB(fd, 11) = pc_need_status_point(sd, SP_INT); + WFIFOB(fd, 12) = min(sd->status.attrs[ATTR::DEX], 255); + WFIFOB(fd, 13) = pc_need_status_point(sd, SP_DEX); + WFIFOB(fd, 14) = min(sd->status.attrs[ATTR::LUK], 255); + WFIFOB(fd, 15) = pc_need_status_point(sd, SP_LUK); + + WFIFOW(fd, 16) = sd->base_atk + sd->watk; + WFIFOW(fd, 18) = sd->watk2; //atk bonus + WFIFOW(fd, 20) = sd->matk1; + WFIFOW(fd, 22) = sd->matk2; + WFIFOW(fd, 24) = sd->def; // def + WFIFOW(fd, 26) = sd->def2; + WFIFOW(fd, 28) = sd->mdef; // mdef + WFIFOW(fd, 30) = sd->mdef2; + WFIFOW(fd, 32) = sd->hit; + WFIFOW(fd, 34) = sd->flee; + WFIFOW(fd, 36) = sd->flee2 / 10; + WFIFOW(fd, 38) = sd->critical / 10; + WFIFOW(fd, 40) = sd->status.karma; + WFIFOW(fd, 42) = sd->status.manner; WFIFOSET(fd, packet_len_table[0xbd]); @@ -2862,12 +2871,12 @@ void clif_getareachar_pc(struct map_session_data *sd, if (dstsd->walktimer != -1) { - len = clif_set007b(dstsd, WFIFOP(sd->fd, 0)); + len = clif_set007b(dstsd, static_cast(WFIFOP(sd->fd, 0))); WFIFOSET(sd->fd, len); } else { - len = clif_set0078(dstsd, WFIFOP(sd->fd, 0)); + len = clif_set0078(dstsd, static_cast(WFIFOP(sd->fd, 0))); WFIFOSET(sd->fd, len); } @@ -2894,7 +2903,7 @@ void clif_getareachar_npc(struct map_session_data *sd, struct npc_data *nd) if (nd->npc_class < 0 || nd->flag & 1 || nd->npc_class == INVISIBLE_CLASS) return; - len = clif_npc0078(nd, WFIFOP(sd->fd, 0)); + len = clif_npc0078(nd, static_cast(WFIFOP(sd->fd, 0))); WFIFOSET(sd->fd, len); } @@ -3030,12 +3039,12 @@ void clif_getareachar_mob(struct map_session_data *sd, struct mob_data *md) if (md->state.state == MS_WALK) { - len = clif_mob007b(md, WFIFOP(sd->fd, 0)); + len = clif_mob007b(md, static_cast(WFIFOP(sd->fd, 0))); WFIFOSET(sd->fd, len); } else { - len = clif_mob0078(md, WFIFOP(sd->fd, 0)); + len = clif_mob0078(md, static_cast(WFIFOP(sd->fd, 0))); WFIFOSET(sd->fd, len); } } @@ -4357,7 +4366,7 @@ void clif_parse_GlobalMessage(int fd, struct map_session_data *sd) size_t message_len = 0; // sometimes uint8_t char *buf = NULL; - char *message = NULL; /* The message text only. */ + const char *message = NULL; /* The message text only. */ nullpo_retv(sd); @@ -4386,9 +4395,9 @@ void clif_parse_GlobalMessage(int fd, struct map_session_data *sd) } /* It's not a spell/magic message, so send the message to others. */ - WBUFW(buf, 0) = 0x8d; - WBUFW(buf, 2) = msg_len + 8; /* Header(2) + length(2) + ID(4). */ - WBUFL(buf, 4) = sd->bl.id; + WBUFW(reinterpret_cast(buf), 0) = 0x8d; + WBUFW(reinterpret_cast(buf), 2) = msg_len + 8; /* Header(2) + length(2) + ID(4). */ + WBUFL(reinterpret_cast(buf), 4) = sd->bl.id; // evil multiuse buffer! clif_send((const uint8_t *)buf, msg_len + 8, &sd->bl, @@ -4622,7 +4631,7 @@ void clif_parse_Wis(int fd, struct map_session_data *sd) { size_t message_len = 0; char *buf = NULL; - char *message = NULL; /* The message text only. */ + const char *message = NULL; /* The message text only. */ struct map_session_data *dstsd = NULL; nullpo_retv(sd); @@ -4844,12 +4853,13 @@ void clif_parse_EquipItem(int fd, struct map_session_data *sd) //ペット用装備であるかないか if (sd->inventory_data[index]) { + EPOS epos = EPOS(RFIFOW(fd, 4)); if (sd->inventory_data[index]->type == ItemType::ARROW) // 矢を無理やり装備できるように(−−; - RFIFOW(fd, 4) = uint16_t(EPOS::ARROW); + epos = EPOS::ARROW; // Note: the EPOS argument to pc_equipitem is actually ignored - pc_equipitem(sd, index, EPOS(RFIFOW(fd, 4))); + pc_equipitem(sd, index, epos); } } @@ -4920,13 +4930,11 @@ void clif_parse_NpcBuySellSelected(int fd, struct map_session_data *sd) static void clif_parse_NpcBuyListSend(int fd, struct map_session_data *sd) { - int fail = 0, n; - unsigned short *item_list; + int n = (RFIFOW(fd, 2) - 4) / 4; + // really an array of pairs of uint16_t + const uint16_t *item_list = static_cast(RFIFOP(fd, 4)); - n = (RFIFOW(fd, 2) - 4) / 4; - item_list = (unsigned short *) RFIFOP(fd, 4); - - fail = npc_buylist(sd, n, item_list); + int fail = npc_buylist(sd, n, item_list); WFIFOW(fd, 0) = 0xca; WFIFOB(fd, 2) = fail; @@ -4940,13 +4948,11 @@ void clif_parse_NpcBuyListSend(int fd, struct map_session_data *sd) static 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); + int n = (RFIFOW(fd, 2) - 4) / 4; + // really an array of pairs of uint16_t + const uint16_t *item_list = static_cast(RFIFOP(fd, 4)); - fail = npc_selllist(sd, n, item_list); + int fail = npc_selllist(sd, n, item_list); WFIFOW(fd, 0) = 0xcb; WFIFOB(fd, 2) = fail; @@ -5469,7 +5475,7 @@ void clif_parse_PartyMessage(int fd, struct map_session_data *sd) { size_t message_len = 0; char *buf = NULL; - char *message = NULL; /* The message text only. */ + const char *message = NULL; /* The message text only. */ nullpo_retv(sd); @@ -6205,7 +6211,7 @@ int clif_check_packet_flood(int fd, int cmd) */ static char *clif_validate_chat(struct map_session_data *sd, int type, - char **message, size_t *message_len) + const char **message, size_t *message_len) { int fd; unsigned int buf_len; /* Actual message length. */ @@ -6213,7 +6219,6 @@ char *clif_validate_chat(struct map_session_data *sd, int type, unsigned int min_len; /* Minimum message length. */ size_t name_len; /* Sender's name length. */ char *buf = NULL; /* Copy of actual message data. */ - char *p = NULL; /* Temporary pointer to message. */ *message = NULL; *message_len = 0; @@ -6264,7 +6269,7 @@ char *clif_validate_chat(struct map_session_data *sd, int type, return NULL; } - p = (char *)((type != 1) ? RFIFOP(fd, 4) : RFIFOP(fd, 28)); + const char *p = static_cast((type != 1) ? RFIFOP(fd, 4) : RFIFOP(fd, 28)); buf_len = (type == 1) ? msg_len - min_len: msg_len; /* @@ -6297,7 +6302,7 @@ char *clif_validate_chat(struct map_session_data *sd, int type, } else { - char *pos = NULL; + const char *pos = NULL; if (!(pos = strstr(p, " : ")) || strncmp(p, sd->status.name, name_len) || pos - p != name_len) diff --git a/src/map/intif.cpp b/src/map/intif.cpp index bb27643..9d44200 100644 --- a/src/map/intif.cpp +++ b/src/map/intif.cpp @@ -288,8 +288,10 @@ int intif_parse_WisMessage(int fd) if (battle_config.etc_log) PRINTF("intif_parse_wismessage: id: %d, from: %s, to: %s, message: '%s'\n", - RFIFOL(fd, 4), RFIFOP(fd, 8), RFIFOP(fd, 32), RFIFOP(fd, - 56)); + RFIFOL(fd, 4), + static_cast(RFIFOP(fd, 8)), + static_cast(RFIFOP(fd, 32)), + static_cast(RFIFOP(fd, 56))); sd = map_nick2sd((const char *)RFIFOP(fd, 32)); // Searching destination player if (sd != NULL && strcmp(sd->status.name, (const char *)RFIFOP(fd, 32)) == 0) { // exactly same name (inter-server have checked the name before) @@ -328,7 +330,9 @@ int intif_parse_WisEnd(int fd) struct map_session_data *sd; if (battle_config.etc_log) - PRINTF("intif_parse_wisend: player: %s, flag: %d\n", RFIFOP(fd, 2), RFIFOB(fd, 26)); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target + // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target + PRINTF("intif_parse_wisend: player: %s, flag: %d\n", + static_cast(RFIFOP(fd, 2)), RFIFOB(fd, 26)); sd = map_nick2sd((const char *)RFIFOP(fd, 2)); if (sd != NULL) clif_wis_end(sd->fd, RFIFOB(fd, 26)); @@ -484,7 +488,7 @@ int intif_parse_PartyInfo(int fd) RFIFOL(fd, 4), RFIFOW(fd, 2), sizeof(struct party) + 4); } - party_recv_info((struct party *) RFIFOP(fd, 4)); + party_recv_info(static_cast(RFIFOP(fd, 4))); return 0; } diff --git a/src/map/map.cpp b/src/map/map.cpp index 8732186..4e6cb66 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -1795,25 +1795,15 @@ void map_log(const_string line) if (!map_logfile) return; - struct timeval tv; - gettimeofday(&tv, NULL); + time_t t = time(NULL); - if ((tv.tv_sec >> LOGFILE_SECONDS_PER_CHUNK_SHIFT) != map_logfile_index) + if ((t >> LOGFILE_SECONDS_PER_CHUNK_SHIFT) != map_logfile_index) { map_close_logfile(); - map_start_logfile(tv.tv_sec); - } - - if (!line) - { - fputc('\n', map_logfile); - return; + map_start_logfile(t); } - FPRINTF(map_logfile, "%ld.%06ld ", (long) tv.tv_sec, (long) tv.tv_usec); - fwrite(line.data(), 1, line.size(), map_logfile); - if (line.back() != '\n') - fputc('\n', map_logfile); + log_with_timestamp(map_logfile, line); } /*========================================== diff --git a/src/map/npc.cpp b/src/map/npc.cpp index 0e406c2..df75216 100644 --- a/src/map/npc.cpp +++ b/src/map/npc.cpp @@ -363,7 +363,7 @@ void npc_do_ontimer_sub(db_key_t key, db_val_t data, int *c, int option) char temp[10]; char event[50]; - if (ev->nd->bl.id == (int) *c && (p = strchr(p, ':')) && p + if (ev->nd->bl.id == *c && (p = strchr(p, ':')) && p && strncasecmp("::OnTimer", p, 8) == 0) { sscanf(&p[9], "%s", temp); @@ -851,7 +851,7 @@ int npc_buysellsel(struct map_session_data *sd, int id, int type) *------------------------------------------ */ int npc_buylist(struct map_session_data *sd, int n, - unsigned short *item_list) + const uint16_t *item_list) { struct npc_data *nd; double z; @@ -969,7 +969,7 @@ int npc_buylist(struct map_session_data *sd, int n, *------------------------------------------ */ int npc_selllist(struct map_session_data *sd, int n, - unsigned short *item_list) + const uint16_t *item_list) { double z; int i, skill, itemamount = 0; @@ -1700,7 +1700,9 @@ int npc_parse_function(char *, char *, char *w3, char *, p = (char *) calloc(50, sizeof(char)); strncpy(p, w3, 49); - strdb_insert(script_get_userfunc_db(), p, script); + // db_val_t takes a void *, we do restore safely ... + ScriptCode *script_ = const_cast(script); + strdb_insert(script_get_userfunc_db(), p, script_); // label_db=script_get_label_db(); diff --git a/src/map/npc.hpp b/src/map/npc.hpp index fba0ec0..52758d4 100644 --- a/src/map/npc.hpp +++ b/src/map/npc.hpp @@ -17,8 +17,8 @@ int npc_touch_areanpc(struct map_session_data *, int, int, int); int npc_click(struct map_session_data *, int); int npc_scriptcont(struct map_session_data *, int); int npc_buysellsel(struct map_session_data *, int, int); -int npc_buylist(struct map_session_data *, int, unsigned short *); -int npc_selllist(struct map_session_data *, int, unsigned short *); +int npc_buylist(struct map_session_data *, int, const uint16_t *); +int npc_selllist(struct map_session_data *, int, const uint16_t *); int npc_parse_warp(const char *w1, const char *w2, const char *w3, const char *w4); int npc_enable(const char *name, int flag); diff --git a/src/map/party.cpp b/src/map/party.cpp index 9b9470c..98bb2be 100644 --- a/src/map/party.cpp +++ b/src/map/party.cpp @@ -145,7 +145,10 @@ int party_check_member(struct party *p) if (strcmp(p->member[j].name, sd->status.name) == 0) f = 0; // データがある else - p->member[j].sd = NULL; // 同垢別キャラだった + { + // I can prove it was already zeroed + // p->member[j].sd = NULL; // 同垢別キャラだった + } } } if (f) @@ -178,7 +181,7 @@ int party_recv_noinfo(int party_id) } // 情報所得 -int party_recv_info(struct party *sp) +int party_recv_info(const struct party *sp) { struct party *p; int i; @@ -191,9 +194,11 @@ int party_recv_info(struct party *sp) numdb_insert(party_db, sp->party_id, p); // 最初のロードなのでユーザーのチェックを行う - party_check_member(sp); + *p = *sp; + party_check_member(p); } - memcpy(p, sp, sizeof(struct party)); + else + *p = *sp; for (i = 0; i < MAX_PARTY; i++) { // sdの設定 diff --git a/src/map/party.hpp b/src/map/party.hpp index ecb0cc4..8dfd9b5 100644 --- a/src/map/party.hpp +++ b/src/map/party.hpp @@ -23,7 +23,7 @@ int party_member_leaved(int party_id, int account_id, const char *name); int party_reply_invite(struct map_session_data *sd, int account_id, int flag); int party_recv_noinfo(int party_id); -int party_recv_info(struct party *sp); +int party_recv_info(const struct party *sp); int party_recv_movemap(int party_id, int account_id, const char *map, int online, int lv); int party_broken(int party_id); diff --git a/src/map/pc.cpp b/src/map/pc.cpp index 2c8a4e7..68dc50b 100644 --- a/src/map/pc.cpp +++ b/src/map/pc.cpp @@ -217,7 +217,7 @@ void pc_invincible_timer(timer_id tid, tick_t, custom_id_t id, custom_data_t) { struct map_session_data *sd; - if ((sd = (struct map_session_data *) map_id2sd(id)) == NULL + if ((sd = map_id2sd(id)) == NULL || sd->bl.type != BL_PC) return; @@ -259,7 +259,7 @@ void pc_spiritball_timer(timer_id tid, tick_t, custom_id_t id, custom_data_t) struct map_session_data *sd; int i; - if ((sd = (struct map_session_data *) map_id2sd(id)) == NULL + if ((sd = map_id2sd(id)) == NULL || sd->bl.type != BL_PC) return; @@ -781,7 +781,7 @@ int pc_breakarmor(struct map_session_data *sd) *------------------------------------------ */ int pc_authok(int id, int login_id2, time_t connect_until_time, - short tmw_version, struct mmo_charstatus *st) + short tmw_version, const struct mmo_charstatus *st) { struct map_session_data *sd = NULL; @@ -6883,7 +6883,7 @@ int pc_equipitem(struct map_session_data *sd, int n, EPOS) return 0; } - if (bool(pos == (EPOS::MISC2 | EPOS::CAPE))) + if (pos == (EPOS::MISC2 | EPOS::CAPE)) { // アクセサリ用例外処理 EPOS epor = EPOS::ZERO; diff --git a/src/map/pc.hpp b/src/map/pc.hpp index 97163b1..6f0c4e8 100644 --- a/src/map/pc.hpp +++ b/src/map/pc.hpp @@ -75,7 +75,7 @@ int pc_counttargeted(struct map_session_data *sd, struct block_list *src, int pc_setrestartvalue(struct map_session_data *sd, int type); int pc_makesavestatus(struct map_session_data *); int pc_setnewpc(struct map_session_data *, int, int, int, int, int, int); -int pc_authok(int, int, time_t, short tmw_version, struct mmo_charstatus *); +int pc_authok(int, int, time_t, short tmw_version, const struct mmo_charstatus *); int pc_authfail(int); EPOS pc_equippoint(struct map_session_data *sd, int n); diff --git a/src/map/script.cpp b/src/map/script.cpp index eca91e6..a82cd66 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -941,7 +941,7 @@ const ScriptCode *parse_script(const char *src, int line) exit(1); } set_label(l, script_pos); - strdb_insert(scriptlabel_db, (const char*)p, script_pos); // 外部用label db登録 + strdb_insert(scriptlabel_db, p, script_pos); // 外部用label db登録 *tmpp = c; p = tmpp + 1; continue; @@ -1373,7 +1373,8 @@ void builtin_callfunc(ScriptState *st) const ScriptCode *scr; const char *str = conv_str(st, &(st->stack->stack_data[st->start + 2])); - if ((scr = (const ScriptCode *)strdb_search(script_get_userfunc_db(), str))) + // note: strdb_search returns a void *; but ScriptCode is really const + if ((scr = static_cast(strdb_search(script_get_userfunc_db(), str)))) { int j = 0; #if 0 @@ -5082,7 +5083,7 @@ void mapregstr_db_final(db_key_t, db_val_t data) static void userfunc_db_final(db_key_t key, db_val_t data) { - free((char*)key.s); + free(key.ms); free(data); } diff --git a/src/warnings.hpp b/src/warnings.hpp index f280014..4248155 100644 --- a/src/warnings.hpp +++ b/src/warnings.hpp @@ -198,7 +198,7 @@ E(-Wc++0x-compat) X(-Wcast-align) /// Warn about casts which discard qualifiers -W(-Wcast-qual) +E(-Wcast-qual) /// Warn about subscripts whose type is "char" E(-Wchar-subscripts) @@ -599,7 +599,7 @@ E(-Wunused-value) E(-Wunused-variable) /// Warn about useless casts -XG48(-Wuseless-cast) +EG48(-Wuseless-cast) /// Warn about questionable usage of the macros used /// to retrieve variable arguments -- cgit v1.2.3-70-g09d2