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().
This commit is contained in:
Kevin Easton
2017-06-28 16:54:11 +10:00
parent c62e099d80
commit 9b0d3e4ab5
6 changed files with 61 additions and 76 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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])

View File

@@ -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] =
{

View File

@@ -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 *buffer = new_malloc(strlen(str) + 1);
char *output_ptr = buffer;
char c;
int i,
new_size = 0;
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++])
{
case CTCP_QUOTE_CHAR:
*ptr++ = CTCP_QUOTE_CHAR;
if (!(c = *str++))
break;
switch (c)
{
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)

View File

@@ -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 *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);