From 9b0d3e4ab57ecfe921e101519be6272e404fa504 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Wed, 28 Jun 2017 16:54:11 +1000 Subject: [PATCH] Type improvements to the internal encrypt/decrypt and CTCP quoting API Use size_t for passing buffer lengths, and const char * for encryption keys and other non-modified buffer arguments. Remove pointless helper function do_crypt(). --- include/ctcp.h | 4 +-- include/encrypt.h | 12 ++++----- include/modval.h | 4 +-- source/commands.c | 7 +++--- source/ctcp.c | 64 ++++++++++++++++++++++------------------------- source/encrypt.c | 46 +++++++++++++--------------------- 6 files changed, 61 insertions(+), 76 deletions(-) diff --git a/include/ctcp.h b/include/ctcp.h index 40d2575..9a1f9d0 100644 --- a/include/ctcp.h +++ b/include/ctcp.h @@ -64,8 +64,8 @@ extern int in_ctcp_flag; #define CTCP_QUOTE_EM "\r\n\001\\" -extern char * ctcp_quote_it (char *, int); -extern char * ctcp_unquote_it (char *, int *); +extern char *ctcp_quote_it(const char *, size_t); +extern char *ctcp_unquote_it(const char *, size_t *); extern char * do_ctcp (char *, char *, char *); extern char * do_notice_ctcp (char *, char *, char *); extern int in_ctcp (void); diff --git a/include/encrypt.h b/include/encrypt.h index 41c8d58..81b536c 100644 --- a/include/encrypt.h +++ b/include/encrypt.h @@ -12,12 +12,12 @@ #ifndef ENCRYPT_H_ #define ENCRYPT_H_ - char *crypt_msg (char *, char *); - char *decrypt_msg (char *, char *); - void encrypt_cmd (char *, char *, char *, char *); - char *is_crypted (char *); - void BX_my_decrypt (char *, int, char *); - void BX_my_encrypt (char *, int, char *); +char *crypt_msg(char *, const char *); +char *decrypt_msg(const char *, const char *); +void encrypt_cmd(char *, char *, char *, char *); +const char *is_crypted(char *); +void BX_my_decrypt(char *, int, const char *); +void BX_my_encrypt(char *, int, const char *); #define CRYPT_HEADER "" #define CRYPT_HEADER_LEN 5 diff --git a/include/modval.h b/include/modval.h index 740ee42..25e43a8 100644 --- a/include/modval.h +++ b/include/modval.h @@ -249,8 +249,8 @@ extern Function_ptr *global; #define bsd_globfree (*(void (*)(glob_t *))global[BSD_GLOBFREE]) /* misc commands */ -#define my_encrypt (*(void (*)(char *, int , char *))global[MY_ENCRYPT]) -#define my_decrypt (*(void (*)(char *, int , char *))global[MY_DECRYPT]) +#define my_encrypt (*(void (*)(char *, int , const char *))global[MY_ENCRYPT]) +#define my_decrypt (*(void (*)(char *, int , const char *))global[MY_DECRYPT]) #define prepare_command (*(ChannelList *(*)(int *, char *, int))global[PREPARE_COMMAND]) #define convert_output_format (*(char *(*)(const char *, const char *, ...))global[CONVERT_OUTPUT_FORMAT]) #define userage (*(void (*)(char *, char *))global[USERAGE]) diff --git a/source/commands.c b/source/commands.c index fbd4b30..056ae7a 100644 --- a/source/commands.c +++ b/source/commands.c @@ -3789,6 +3789,8 @@ int current_target = 0; */ void BX_send_text(const char *nick_list, const char *text, char *command, int hook, int log) { + static int sent_text_recursion = 0; + int i, old_server, not_done = 1, @@ -3797,9 +3799,8 @@ void BX_send_text(const char *nick_list, const char *text, char *command, int h char *current_nick, *next_nick, *free_nick, - *line, - *key = NULL; -static int sent_text_recursion = 0; + *line; + const char *key = NULL; struct target_type target[4] = { diff --git a/source/ctcp.c b/source/ctcp.c index 0ea3a9a..d6ca007 100644 --- a/source/ctcp.c +++ b/source/ctcp.c @@ -603,9 +603,9 @@ int server; return NULL; } -static char *try_decrypt(char *from, char *to, char *msg) +static char *try_decrypt(char *from, char *to, const char *msg) { - char *key; + const char *key; char *crypt_who; if (*from == '=' || !my_stricmp(to, get_server_nickname(from_server))) @@ -1536,13 +1536,12 @@ extern void send_ctcp (int type, char *to, int datatag, char *format, ...) * null terminated (it can contain nulls). Returned is a malloced, null * terminated string. */ -extern char *ctcp_quote_it (char *str, int len) +extern char *ctcp_quote_it(const char *str, size_t len) { - char buffer[BIG_BUFFER_SIZE + 1]; - char *ptr; - int i; + char *buffer = new_malloc(2 * len + 1); + char *ptr = buffer; + size_t i; - ptr = buffer; for (i = 0; i < len; i++) { switch (str[i]) @@ -1567,7 +1566,8 @@ extern char *ctcp_quote_it (char *str, int len) } } *ptr = '\0'; - return m_strdup(buffer); + + return buffer; } /* @@ -1577,50 +1577,46 @@ extern char *ctcp_quote_it (char *str, int len) * convenied, but the returned data may contain nulls!. The len is modified * to contain the size of the data returned. */ -extern char *ctcp_unquote_it (char *str, int *len) +extern char *ctcp_unquote_it(const char *str, size_t *output_len) { - char *buffer; - char *ptr; - char c; - int i, - new_size = 0; + char *buffer = new_malloc(strlen(str) + 1); + char *output_ptr = buffer; + char c; - buffer = (char *) new_malloc((sizeof(char) * *len) + 1); - ptr = buffer; - i = 0; - while (i < *len) + while ((c = *str++)) { - if ((c = str[i++]) == CTCP_QUOTE_CHAR) + if (c == CTCP_QUOTE_CHAR) { - switch (c = str[i++]) + if (!(c = *str++)) + break; + + switch (c) { - case CTCP_QUOTE_CHAR: - *ptr++ = CTCP_QUOTE_CHAR; - break; case 'a': - *ptr++ = CTCP_DELIM_CHAR; + c = CTCP_DELIM_CHAR; break; case 'n': - *ptr++ = '\n'; + c = '\n'; break; case 'r': - *ptr++ = '\r'; + c = '\r'; break; case '0': - *ptr++ = '\0'; + c = '\0'; break; + case CTCP_QUOTE_CHAR: default: - *ptr++ = c; break; } } - else - *ptr++ = c; - new_size++; + + *output_ptr++ = c; } - *ptr = '\0'; - *len = new_size; - return (buffer); + + *output_ptr = '\0'; + *output_len = output_ptr - buffer; + + return buffer; } int get_ctcp_val (char *str) diff --git a/source/encrypt.c b/source/encrypt.c index 2c39b30..2cf6dbf 100644 --- a/source/encrypt.c +++ b/source/encrypt.c @@ -26,7 +26,6 @@ CVS_REVISION(encrypt_c) static void add_to_crypt (char *, char *); static int remove_crypt (char *); -static char *do_crypt (char *, char *, int); #define CRYPT_BUFFER_SIZE (IRCD_BUFFER_SIZE - 50) /* Make this less than * the trasmittable @@ -88,7 +87,7 @@ static int remove_crypt(char *nick) * is_crypted: looks up nick in the crypt_list and returns the encryption key * if found in the list. If not found in the crypt_list, null is returned. */ -char * is_crypted(char *nick) +const char *is_crypted(char *nick) { Crypt *tmp; @@ -138,7 +137,7 @@ BUILT_IN_COMMAND(encrypt_cmd) } } -extern void BX_my_encrypt (char *str, int len, char *key) +extern void BX_my_encrypt (char *str, int len, const char *key) { int key_len, key_pos, @@ -162,7 +161,7 @@ extern void BX_my_encrypt (char *str, int len, char *key) str[i] = (char) 0; } -extern void BX_my_decrypt(char *str, int len, char *key) +extern void BX_my_decrypt(char *str, int len, const char *key) { int key_len, key_pos, @@ -187,38 +186,23 @@ extern void BX_my_decrypt(char *str, int len, char *key) str[i] = (char) 0; } -static char *do_crypt(char *str, char *key, int flag) -{ - int c; - char *ptr = NULL; - - c = strlen(str); - if (flag) - { - my_encrypt(str, c, key); - ptr = ctcp_quote_it(str, c); - } - else - { - ptr = ctcp_unquote_it(str, &c); - my_decrypt(ptr, c, key); - } - return (ptr); -} - /* * crypt_msg: Executes the encryption program on the given string with the * given key. If flag is true, the string is encrypted and the returned * string is ready to be sent over irc. If flag is false, the string is * decrypted and the returned string should be readable */ -char *crypt_msg(char *str, char *key) +char *crypt_msg(char *str, const char *key) { static const char sed_prefix[] = { CTCP_DELIM_CHAR, 'S', 'E', 'D', ' ', 0 }; + const size_t len = strlen(str); char buffer[CRYPT_BUFFER_SIZE]; char *ptr; - if ((ptr = do_crypt(str, key, 1))) + my_encrypt(str, len, key); + ptr = ctcp_quote_it(str, len); + + if (ptr) { /* The - 1 terms here are to ensure that the trailing CTCP_DELIM_CHAR * always gets added. */ @@ -243,12 +227,16 @@ char *crypt_msg(char *str, char *key) * a big buffer to scratch around (The decrypted text could be a CTCP UTC * which could expand to a larger string of text.) */ -char *decrypt_msg (char *str, char *key) +char *decrypt_msg(const char *str, const char *key) { - char *buffer = (char *)new_malloc(BIG_BUFFER_SIZE + 1); - char *ptr; + char *buffer = new_malloc(BIG_BUFFER_SIZE + 1); + char *ptr; + size_t len; - if ((ptr = do_crypt(str, key, 0)) != NULL) + ptr = ctcp_unquote_it(str, &len); + my_decrypt(ptr, len, key); + + if (ptr) { strlcpy(buffer, ptr, CRYPT_BUFFER_SIZE); new_free(&ptr);