- fixed malloc and used g_new or g_malloc in all places

- i double bumped the version accidentally
- undo formatting of not touched  code (the other is that different, that it wouldn't be recognizeable anyway)
- removed the limit option completely, also resulting in removing the /checksum command
- use g_strdup instead of some manual stuff + strcpy
This commit is contained in:
Totto16 2022-10-24 22:55:53 +02:00
parent 1b88946a4e
commit c87a7c63ed
No known key found for this signature in database
GPG Key ID: 40285387F8ABE31B

View File

@ -22,67 +22,22 @@
#include "config.h"
#include <gio/gio.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gerror.h>
#include <glib/gstdio.h>
#include <stdlib.h>
#include <gio/gio.h>
#include "hexchat-plugin.h"
#define BUFSIZE 32768
#define DEFAULT_LIMIT 256 /* default size is 256 MiB */
#define SHA256_DIGEST_LENGTH 32
#define SHA256_BUFFER_LENGTH 65
static hexchat_plugin *ph; /* plugin handle */
static char name[] = "Checksum";
static char desc[] = "Calculate checksum for DCC file transfers";
static char version[] = "3.3";
static void set_limit(char *size) {
int limit = atoi(size);
if (limit > 0 && limit < INT_MAX) {
if (hexchat_pluginpref_set_int(ph, "limit", limit))
hexchat_printf(
ph,
"Checksum: File size limit has successfully been set to: %d MiB\n",
limit);
else
hexchat_printf(ph, "Checksum: File access error while saving!\n");
} else {
hexchat_printf(ph, "Checksum: Invalid input!\n");
}
}
static int get_limit(void) {
int size = hexchat_pluginpref_get_int(ph, "limit");
if (size <= 0 || size >= INT_MAX)
return DEFAULT_LIMIT;
else
return size;
}
static gboolean check_limit(GFile *file) {
GFileInfo *file_info;
goffset file_size;
file_info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_SIZE,
G_FILE_QUERY_INFO_NONE, NULL, NULL);
if (!file_info)
return FALSE;
file_size = g_file_info_get_size(file_info);
g_object_unref(file_info);
if (file_size > get_limit() * 1048576ll)
return FALSE;
return TRUE;
}
static char version[] = "3.2";
typedef void async_result_cb(gboolean, gpointer);
@ -96,10 +51,10 @@ typedef struct {
} stream_async_cb_data;
static void free_user_data(stream_async_cb_data *user_data) {
free(user_data->buffer_size);
free(user_data->buffer);
g_free(user_data->buffer_size);
g_free(user_data->buffer);
g_checksum_free(user_data->checksum);
free(user_data);
g_free(user_data);
}
static void stream_async_cb(GObject *source_object, GAsyncResult *res,
@ -147,32 +102,13 @@ static void sha256_from_stream_async(GFileInputStream *file_stream,
char out_buf[], async_result_cb *result_cb,
gpointer bound_data) {
size_t *buffer_size = malloc(sizeof(size_t));
if (buffer_size == NULL) {
return result_cb(FALSE, bound_data);
}
size_t *buffer_size = g_new(size_t, 1);
*buffer_size = BUFSIZ * sizeof(guchar);
guchar *buffer = malloc(*buffer_size);
if (buffer == NULL) {
free(buffer_size);
return result_cb(FALSE, bound_data);
}
guchar *buffer = g_malloc(*buffer_size);
GChecksum *checksum = g_checksum_new(G_CHECKSUM_SHA256);
if (checksum == NULL) {
free(buffer_size);
free(buffer);
return result_cb(FALSE, bound_data);
}
stream_async_cb_data *user_data = malloc(sizeof(stream_async_cb_data));
if (user_data == NULL) {
free(buffer_size);
free(buffer);
g_checksum_free(checksum);
return result_cb(FALSE, bound_data);
}
stream_async_cb_data *user_data = g_new(stream_async_cb_data, 1);
user_data->checksum = checksum;
user_data->result_callback = result_cb;
@ -207,7 +143,7 @@ static void file_async_cb(gboolean result, gpointer user_data) {
g_object_unref(bound_vars->file);
async_result_cb *cb = bound_vars->result_cb;
gpointer bound_data = bound_vars->bound_data;
free(bound_vars);
g_free(bound_vars);
return cb(result, bound_data);
}
@ -232,14 +168,6 @@ static void sha256_from_file_async(char *filename, char out_buf[],
return result_cb(FALSE, bound_data);
}
if (!check_limit(file)) {
hexchat_printf(ph,
"Checksum: %s is larger than size limit. You can increase "
"it with /CHECKSUM SET.\n",
filename);
g_object_unref(file);
return result_cb(FALSE, bound_data);
}
file_stream = g_file_read(file, NULL, NULL);
if (!file_stream) {
@ -248,12 +176,8 @@ static void sha256_from_file_async(char *filename, char out_buf[],
return result_cb(FALSE, bound_data);
}
file_async_cb_data *user_data = malloc(sizeof(file_async_cb_data));
if (user_data == NULL) {
hexchat_printf(ph, "Checksum: Failed to malloc needed memory\n");
g_object_unref(file);
return result_cb(FALSE, bound_data);
}
file_async_cb_data *user_data = g_new(file_async_cb_data, 1);
user_data->result_cb = result_cb;
user_data->file_stream = file_stream;
@ -280,21 +204,17 @@ void dccrecv_async_cb(gboolean result, gpointer user_data) {
bound_vars->word_1, bound_vars->checksum);
}
free(bound_vars->word_1);
free(bound_vars->checksum);
g_free(bound_vars->word_1);
g_free(bound_vars->checksum);
g_free(bound_vars->filename);
free(bound_vars);
g_free(bound_vars);
}
static int dccrecv_cb(char *word[], void *userdata) {
const char *dcc_completed_dir;
char *filename;
char *checksum = malloc(SHA256_BUFFER_LENGTH);
if (checksum == NULL) {
hexchat_commandf(ph, "Checksum: Failed to malloc needed memory\n");
return HEXCHAT_EAT_NONE;
}
char *checksum = g_malloc(SHA256_BUFFER_LENGTH);
/* Print in the privmsg tab of the sender */
hexchat_set_context(ph, hexchat_find_context(ph, NULL, word[3]));
@ -307,24 +227,11 @@ static int dccrecv_cb(char *word[], void *userdata) {
filename = g_strdup(word[2]);
}
dccrecv_async_cb_data *user_data = malloc(sizeof(dccrecv_async_cb_data));
if (user_data == NULL) {
hexchat_printf(ph, "Checksum: Failed to malloc needed memory\n");
free(checksum);
return HEXCHAT_EAT_NONE;
}
dccrecv_async_cb_data *user_data = g_new(dccrecv_async_cb_data,1);
user_data->checksum = checksum;
char *word_1 = malloc(strlen(word[1]) + 1);
if (word_1 == NULL) {
hexchat_printf(ph, "Checksum: Failed to malloc needed memory\n");
free(checksum);
free(user_data);
return HEXCHAT_EAT_NONE;
}
strcpy(word_1, word[1]);
char *word_1 = g_strdup(word[1]);
user_data->word_1 = word_1;
user_data->filename = filename;
@ -351,11 +258,11 @@ void dccoffer_async_cb(gboolean result, gpointer user_data) {
bound_vars->word_2, bound_vars->word_1, bound_vars->checksum);
}
free(bound_vars->word_2);
free(bound_vars->word_1);
free(bound_vars->checksum);
g_free(bound_vars->word_2);
g_free(bound_vars->word_1);
g_free(bound_vars->checksum);
free(bound_vars);
g_free(bound_vars);
}
static int dccoffer_cb(char *word[], void *userdata) {
@ -363,40 +270,15 @@ static int dccoffer_cb(char *word[], void *userdata) {
/* Print in the privmsg tab of the receiver */
hexchat_set_context(ph, hexchat_find_context(ph, NULL, word[3]));
char *checksum = malloc(SHA256_BUFFER_LENGTH);
if (checksum == NULL) {
hexchat_commandf(ph, "Checksum: Failed to malloc needed memory\n");
return HEXCHAT_EAT_NONE;
}
char *checksum = g_malloc(SHA256_BUFFER_LENGTH);
dccoffer_async_cb_data *user_data = g_new(dccoffer_async_cb_data, 1);
dccoffer_async_cb_data *user_data = malloc(sizeof(dccoffer_async_cb_data));
if (user_data == NULL) {
hexchat_printf(ph, "Checksum: Failed to malloc needed memory\n");
free(checksum);
return HEXCHAT_EAT_NONE;
}
user_data->checksum = checksum;
char *word_1 = malloc(strlen(word[1]) + 1);
if (word_1 == NULL) {
hexchat_printf(ph, "Checksum: Failed to malloc needed memory\n");
free(checksum);
free(user_data);
return HEXCHAT_EAT_NONE;
}
char *word_2 = malloc(strlen(word[2]) + 1);
if (word_2 == NULL) {
hexchat_printf(ph, "Checksum: Failed to malloc needed memory\n");
free(checksum);
free(user_data);
free(word_1);
return HEXCHAT_EAT_NONE;
}
strcpy(word_1, word[1]);
strcpy(word_2, word[2]);
char *word_1 = g_strdup(word[1]);
char *word_2 = g_strdup(word[2]);
user_data->word_1 = word_1;
user_data->word_2 = word_2;
@ -406,47 +288,25 @@ static int dccoffer_cb(char *word[], void *userdata) {
return HEXCHAT_EAT_NONE;
}
static int checksum(char *word[], char *word_eol[], void *userdata) {
if (!g_ascii_strcasecmp("GET", word[2])) {
hexchat_printf(ph, "File size limit for checksums: %d MiB", get_limit());
} else if (!g_ascii_strcasecmp("SET", word[2])) {
set_limit(word[3]);
} else {
hexchat_printf(ph, "Usage: /CHECKSUM GET|SET\n");
hexchat_printf(
ph, " GET - print the maximum file size (in MiB) to be hashed\n");
hexchat_printf(
ph,
" SET <filesize> - set the maximum file size (in MiB) to be hashed\n");
}
return HEXCHAT_EAT_ALL;
}
int hexchat_plugin_init(hexchat_plugin *plugin_handle, char **plugin_name,
char **plugin_desc, char **plugin_version, char *arg) {
int
hexchat_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
{
ph = plugin_handle;
*plugin_name = name;
*plugin_desc = desc;
*plugin_version = version;
/* this is required for the very first run */
if (hexchat_pluginpref_get_int(ph, "limit") == -1) {
hexchat_pluginpref_set_int(ph, "limit", DEFAULT_LIMIT);
}
hexchat_hook_command(ph, "CHECKSUM", HEXCHAT_PRI_NORM, checksum,
"Usage: /CHECKSUM GET|SET", NULL);
hexchat_hook_print(ph, "DCC RECV Complete", HEXCHAT_PRI_NORM, dccrecv_cb,
NULL);
hexchat_hook_print (ph, "DCC RECV Complete", HEXCHAT_PRI_NORM, dccrecv_cb, NULL);
hexchat_hook_print (ph, "DCC Offer", HEXCHAT_PRI_NORM, dccoffer_cb, NULL);
hexchat_printf (ph, "%s plugin loaded\n", name);
return 1;
}
int hexchat_plugin_deinit(void) {
int
hexchat_plugin_deinit (void)
{
hexchat_printf (ph, "%s plugin unloaded\n", name);
return 1;
}