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:
@@ -64,8 +64,8 @@ extern int in_ctcp_flag;
|
|||||||
#define CTCP_QUOTE_EM "\r\n\001\\"
|
#define CTCP_QUOTE_EM "\r\n\001\\"
|
||||||
|
|
||||||
|
|
||||||
extern char * ctcp_quote_it (char *, int);
|
extern char *ctcp_quote_it(const char *, size_t);
|
||||||
extern char * ctcp_unquote_it (char *, int *);
|
extern char *ctcp_unquote_it(const char *, size_t *);
|
||||||
extern char * do_ctcp (char *, char *, char *);
|
extern char * do_ctcp (char *, char *, char *);
|
||||||
extern char * do_notice_ctcp (char *, char *, char *);
|
extern char * do_notice_ctcp (char *, char *, char *);
|
||||||
extern int in_ctcp (void);
|
extern int in_ctcp (void);
|
||||||
|
|||||||
@@ -12,12 +12,12 @@
|
|||||||
#ifndef ENCRYPT_H_
|
#ifndef ENCRYPT_H_
|
||||||
#define ENCRYPT_H_
|
#define ENCRYPT_H_
|
||||||
|
|
||||||
char *crypt_msg (char *, char *);
|
char *crypt_msg(char *, const char *);
|
||||||
char *decrypt_msg (char *, char *);
|
char *decrypt_msg(const char *, const char *);
|
||||||
void encrypt_cmd(char *, char *, char *, char *);
|
void encrypt_cmd(char *, char *, char *, char *);
|
||||||
char *is_crypted (char *);
|
const char *is_crypted(char *);
|
||||||
void BX_my_decrypt (char *, int, char *);
|
void BX_my_decrypt(char *, int, const char *);
|
||||||
void BX_my_encrypt (char *, int, char *);
|
void BX_my_encrypt(char *, int, const char *);
|
||||||
|
|
||||||
#define CRYPT_HEADER ""
|
#define CRYPT_HEADER ""
|
||||||
#define CRYPT_HEADER_LEN 5
|
#define CRYPT_HEADER_LEN 5
|
||||||
|
|||||||
@@ -249,8 +249,8 @@ extern Function_ptr *global;
|
|||||||
#define bsd_globfree (*(void (*)(glob_t *))global[BSD_GLOBFREE])
|
#define bsd_globfree (*(void (*)(glob_t *))global[BSD_GLOBFREE])
|
||||||
|
|
||||||
/* misc commands */
|
/* misc commands */
|
||||||
#define my_encrypt (*(void (*)(char *, int , char *))global[MY_ENCRYPT])
|
#define my_encrypt (*(void (*)(char *, int , const char *))global[MY_ENCRYPT])
|
||||||
#define my_decrypt (*(void (*)(char *, int , char *))global[MY_DECRYPT])
|
#define my_decrypt (*(void (*)(char *, int , const char *))global[MY_DECRYPT])
|
||||||
#define prepare_command (*(ChannelList *(*)(int *, char *, int))global[PREPARE_COMMAND])
|
#define prepare_command (*(ChannelList *(*)(int *, char *, int))global[PREPARE_COMMAND])
|
||||||
#define convert_output_format (*(char *(*)(const char *, const char *, ...))global[CONVERT_OUTPUT_FORMAT])
|
#define convert_output_format (*(char *(*)(const char *, const char *, ...))global[CONVERT_OUTPUT_FORMAT])
|
||||||
#define userage (*(void (*)(char *, char *))global[USERAGE])
|
#define userage (*(void (*)(char *, char *))global[USERAGE])
|
||||||
|
|||||||
@@ -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)
|
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,
|
int i,
|
||||||
old_server,
|
old_server,
|
||||||
not_done = 1,
|
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,
|
char *current_nick,
|
||||||
*next_nick,
|
*next_nick,
|
||||||
*free_nick,
|
*free_nick,
|
||||||
*line,
|
*line;
|
||||||
*key = NULL;
|
const char *key = NULL;
|
||||||
static int sent_text_recursion = 0;
|
|
||||||
|
|
||||||
struct target_type target[4] =
|
struct target_type target[4] =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -603,9 +603,9 @@ int server;
|
|||||||
return NULL;
|
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;
|
char *crypt_who;
|
||||||
|
|
||||||
if (*from == '=' || !my_stricmp(to, get_server_nickname(from_server)))
|
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
|
* null terminated (it can contain nulls). Returned is a malloced, null
|
||||||
* terminated string.
|
* 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 *buffer = new_malloc(2 * len + 1);
|
||||||
char *ptr;
|
char *ptr = buffer;
|
||||||
int i;
|
size_t i;
|
||||||
|
|
||||||
ptr = buffer;
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
switch (str[i])
|
switch (str[i])
|
||||||
@@ -1567,7 +1566,8 @@ extern char *ctcp_quote_it (char *str, int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*ptr = '\0';
|
*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
|
* convenied, but the returned data may contain nulls!. The len is modified
|
||||||
* to contain the size of the data returned.
|
* 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 *buffer = new_malloc(strlen(str) + 1);
|
||||||
char *ptr;
|
char *output_ptr = buffer;
|
||||||
char c;
|
char c;
|
||||||
int i,
|
|
||||||
new_size = 0;
|
|
||||||
|
|
||||||
buffer = (char *) new_malloc((sizeof(char) * *len) + 1);
|
while ((c = *str++))
|
||||||
ptr = buffer;
|
|
||||||
i = 0;
|
|
||||||
while (i < *len)
|
|
||||||
{
|
{
|
||||||
if ((c = str[i++]) == CTCP_QUOTE_CHAR)
|
if (c == CTCP_QUOTE_CHAR)
|
||||||
{
|
{
|
||||||
switch (c = str[i++])
|
if (!(c = *str++))
|
||||||
{
|
|
||||||
case CTCP_QUOTE_CHAR:
|
|
||||||
*ptr++ = CTCP_QUOTE_CHAR;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
switch (c)
|
||||||
|
{
|
||||||
case 'a':
|
case 'a':
|
||||||
*ptr++ = CTCP_DELIM_CHAR;
|
c = CTCP_DELIM_CHAR;
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
*ptr++ = '\n';
|
c = '\n';
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
*ptr++ = '\r';
|
c = '\r';
|
||||||
break;
|
break;
|
||||||
case '0':
|
case '0':
|
||||||
*ptr++ = '\0';
|
c = '\0';
|
||||||
break;
|
break;
|
||||||
|
case CTCP_QUOTE_CHAR:
|
||||||
default:
|
default:
|
||||||
*ptr++ = c;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
*ptr++ = c;
|
*output_ptr++ = c;
|
||||||
new_size++;
|
|
||||||
}
|
}
|
||||||
*ptr = '\0';
|
|
||||||
*len = new_size;
|
*output_ptr = '\0';
|
||||||
return (buffer);
|
*output_len = output_ptr - buffer;
|
||||||
|
|
||||||
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_ctcp_val (char *str)
|
int get_ctcp_val (char *str)
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ CVS_REVISION(encrypt_c)
|
|||||||
|
|
||||||
static void add_to_crypt (char *, char *);
|
static void add_to_crypt (char *, char *);
|
||||||
static int remove_crypt (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
|
#define CRYPT_BUFFER_SIZE (IRCD_BUFFER_SIZE - 50) /* Make this less than
|
||||||
* the trasmittable
|
* 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
|
* 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.
|
* 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;
|
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,
|
int key_len,
|
||||||
key_pos,
|
key_pos,
|
||||||
@@ -162,7 +161,7 @@ extern void BX_my_encrypt (char *str, int len, char *key)
|
|||||||
str[i] = (char) 0;
|
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,
|
int key_len,
|
||||||
key_pos,
|
key_pos,
|
||||||
@@ -187,38 +186,23 @@ extern void BX_my_decrypt(char *str, int len, char *key)
|
|||||||
str[i] = (char) 0;
|
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
|
* 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
|
* 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
|
* string is ready to be sent over irc. If flag is false, the string is
|
||||||
* decrypted and the returned string should be readable
|
* 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 };
|
static const char sed_prefix[] = { CTCP_DELIM_CHAR, 'S', 'E', 'D', ' ', 0 };
|
||||||
|
const size_t len = strlen(str);
|
||||||
char buffer[CRYPT_BUFFER_SIZE];
|
char buffer[CRYPT_BUFFER_SIZE];
|
||||||
char *ptr;
|
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
|
/* The - 1 terms here are to ensure that the trailing CTCP_DELIM_CHAR
|
||||||
* always gets added. */
|
* 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
|
* a big buffer to scratch around (The decrypted text could be a CTCP UTC
|
||||||
* which could expand to a larger string of text.)
|
* 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;
|
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);
|
strlcpy(buffer, ptr, CRYPT_BUFFER_SIZE);
|
||||||
new_free(&ptr);
|
new_free(&ptr);
|
||||||
|
|||||||
Reference in New Issue
Block a user