tfesupp: remove all manual string manipulations - just use std::string. (PR #1065)

This commit is contained in:
Andrea 2022-03-14 21:12:52 +00:00 committed by GitHub
parent 00668c2668
commit a4341aa808
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 164 deletions

View File

@ -29,7 +29,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "../Registry.h"
#include "../resource/resource.h"
#include "../Tfe/PCapBackend.h"
#include "../Tfe/tfesupp.h"
CPageConfigTfe* CPageConfigTfe::ms_this = 0; // reinit'd in ctor
@ -109,26 +108,23 @@ void CPageConfigTfe::DlgCANCEL(HWND window)
EndDialog(window, 0);
}
BOOL CPageConfigTfe::get_tfename(int number, char **ppname, char **ppdescription)
BOOL CPageConfigTfe::get_tfename(int number, std::string & name, std::string & description)
{
if (PCapBackend::tfe_enumadapter_open())
{
char *pname = NULL;
char *pdescription = NULL;
std::string adapterName;
std::string adapterDescription;
while (number--)
{
if (!PCapBackend::tfe_enumadapter(&pname, &pdescription))
if (!PCapBackend::tfe_enumadapter(adapterName, adapterDescription))
break;
lib_free(pname);
lib_free(pdescription);
}
if (PCapBackend::tfe_enumadapter(&pname, &pdescription))
if (PCapBackend::tfe_enumadapter(adapterName, adapterDescription))
{
*ppname = pname;
*ppdescription = pdescription;
name = adapterName;
description = adapterDescription;
PCapBackend::tfe_enumadapter_close();
return TRUE;
}
@ -166,17 +162,15 @@ int CPageConfigTfe::gray_ungray_items(HWND hwnd)
if (enable)
{
char *pname = NULL;
char *pdescription = NULL;
std::string name;
std::string description;
number = SendMessage(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE), CB_GETCURSEL, 0, 0);
if (get_tfename(number, &pname, &pdescription))
if (get_tfename(number, name, description))
{
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_NAME), pname);
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_DESC), pdescription);
lib_free(pname);
lib_free(pdescription);
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_NAME), name.c_str());
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_DESC), description.c_str());
}
}
else
@ -222,25 +216,23 @@ void CPageConfigTfe::init_tfe_dialog(HWND hwnd)
{
int cnt = 0;
char *pname;
char *pdescription;
std::string name;
std::string description;
temp_hwnd=GetDlgItem(hwnd,IDC_TFE_SETTINGS_INTERFACE);
for (cnt = 0; PCapBackend::tfe_enumadapter(&pname, &pdescription); cnt++)
for (cnt = 0; PCapBackend::tfe_enumadapter(name, description); cnt++)
{
BOOL this_entry = FALSE;
if (strcmp(pname, m_tfe_interface_name.c_str()) == 0)
if (name == m_tfe_interface_name)
{
this_entry = TRUE;
}
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_NAME), pname);
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_DESC), pdescription);
SendMessage(temp_hwnd, CB_ADDSTRING, 0, (LPARAM)pname);
lib_free(pname);
lib_free(pdescription);
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_NAME), name.c_str());
SetWindowText(GetDlgItem(hwnd, IDC_TFE_SETTINGS_INTERFACE_DESC), description.c_str());
SendMessage(temp_hwnd, CB_ADDSTRING, 0, (LPARAM)name.c_str());
if (this_entry)
{

View File

@ -28,7 +28,7 @@ protected:
virtual void DlgCANCEL(HWND window);
private:
BOOL get_tfename(int number, char **ppname, char **ppdescription);
BOOL get_tfename(int number, std::string & name, std::string & description);
int gray_ungray_items(HWND hwnd);
void init_tfe_dialog(HWND hwnd);
void save_tfe_dialog(HWND hwnd);

View File

@ -76,9 +76,9 @@ int PCapBackend::tfe_enumadapter_open(void)
return tfe_arch_enumadapter_open();
}
int PCapBackend::tfe_enumadapter(char **ppname, char **ppdescription)
int PCapBackend::tfe_enumadapter(std::string & name, std::string & description)
{
return tfe_arch_enumadapter(ppname, ppdescription);
return tfe_arch_enumadapter(name, description);
}
int PCapBackend::tfe_enumadapter_close(void)

View File

@ -54,7 +54,7 @@ public:
*/
static int tfe_enumadapter_open(void);
static int tfe_enumadapter(char **ppname, char **ppdescription);
static int tfe_enumadapter(std::string & name, std::string & description);
static int tfe_enumadapter_close(void);
static std::string tfe_interface;

View File

@ -41,7 +41,6 @@
#include <StdAfx.h> // this is necessary in linux, but in MSVC windows.h MUST come after winsock2.h (from pcap.h above)
#include "tfearch.h"
#include "tfesupp.h"
#include "../Log.h"
@ -258,16 +257,16 @@ int tfe_arch_enumadapter_open(void)
return 1;
}
int tfe_arch_enumadapter(char **ppname, char **ppdescription)
int tfe_arch_enumadapter(std::string & name, std::string & description)
{
if (!TfePcapNextDev)
return 0;
*ppname = lib_stralloc(TfePcapNextDev->name);
name = TfePcapNextDev->name;
if (TfePcapNextDev->description)
*ppdescription = lib_stralloc(TfePcapNextDev->description);
description = TfePcapNextDev->description;
else
*ppdescription = lib_stralloc(TfePcapNextDev->name);
description = TfePcapNextDev->name;
TfePcapNextDev = TfePcapNextDev->next;
@ -293,20 +292,18 @@ pcap_t * TfePcapOpenAdapter(const std::string & interface_name)
}
else {
/* look if we can find the specified adapter */
char *pname;
char *pdescription;
std::string name;
std::string description;
BOOL found = FALSE;
if (!interface_name.empty()) {
/* we have an interface name, try it */
TfePcapDevice = TfePcapAlldevs;
while (tfe_arch_enumadapter(&pname, &pdescription)) {
if (strcmp(pname, interface_name.c_str())==0) {
while (tfe_arch_enumadapter(name, description)) {
if (name == interface_name) {
found = TRUE;
}
lib_free(pname);
lib_free(pdescription);
if (found) break;
TfePcapDevice = TfePcapNextDev;
}

View File

@ -68,7 +68,7 @@ int tfe_arch_receive(pcap_t * TfePcapFP,
);
extern int tfe_arch_enumadapter_open(void);
extern int tfe_arch_enumadapter(char **ppname, char **ppdescription);
extern int tfe_arch_enumadapter(std::string & name, std::string & description);
extern int tfe_arch_enumadapter_close(void);
#endif

View File

@ -33,122 +33,12 @@
*
*/
#include <stdlib.h>
#include <string.h>
// Lib Stuff
/* #define LIB_DEBUG*/
#ifdef LIB_DEBUG
#define LIB_DEBUG_SIZE 0x10000
#define LIB_DEBUG_GUARD 0x1000
#endif
#include "tfesupp.h"
#define CRC32_POLY 0xedb88320
static unsigned long crc32_table[256];
static int crc32_is_initialized = 0;
void lib_free(void *ptr)
{
#ifdef LIB_DEBUG
lib_debug_free(ptr, 1, 1);
#endif
#ifdef LIB_DEBUG
lib_debug_libc_free(ptr);
#else
free(ptr);
#endif
}
void *lib_malloc(size_t size)
{
#ifdef LIB_DEBUG
void *ptr = lib_debug_libc_malloc(size);
#else
void *ptr = malloc(size);
#endif
#ifndef __OS2__
if (ptr == NULL && size > 0)
exit(-1);
#endif
#ifdef LIB_DEBUG
lib_debug_alloc(ptr, size, 3);
#endif
return ptr;
}
/*-----------------------------------------------------------------------*/
/* Malloc enough space for `str', copy `str' into it and return its
address. */
char *lib_stralloc(const char *str)
{
size_t size;
char *ptr;
if (str == NULL)
exit(-1);
size = strlen(str) + 1;
ptr = (char *)lib_malloc(size);
memcpy(ptr, str, size);
return ptr;
}
/* Like realloc, but abort if not enough memory is available. */
void *lib_realloc(void *ptr, size_t size)
{
#ifdef LIB_DEBUG
void *new_ptr = lib_debug_libc_realloc(ptr, size);
#else
void *new_ptr = realloc(ptr, size);
#endif
#ifndef __OS2__
if (new_ptr == NULL)
exit(-1);
#endif
#ifdef LIB_DEBUG
lib_debug_free(ptr, 1, 0);
lib_debug_alloc(new_ptr, size, 1);
#endif
return new_ptr;
}
// Util Stuff
/* Set a new value to the dynamically allocated string *str.
Returns `-1' if nothing has to be done. */
int util_string_set(char **str, const char *new_value)
{
if (*str == NULL) {
if (new_value != NULL)
*str = lib_stralloc(new_value);
} else {
if (new_value == NULL) {
lib_free(*str);
*str = NULL;
} else {
/* Skip copy if src and dest are already the same. */
if (strcmp(*str, new_value) == 0)
return -1;
*str = (char *)lib_realloc(*str, strlen(new_value) + 1);
strcpy(*str, new_value);
}
}
return 0;
}
// crc32 Stuff
unsigned long crc32_buf(const char *buffer, unsigned int len)
@ -173,4 +63,3 @@ unsigned long crc32_buf(const char *buffer, unsigned int len)
return ~crc;
}

View File

@ -39,15 +39,6 @@
#ifndef _TFESUPP_H
#define _TFESUPP_H
extern FILE* g_fh; // Filehandle for log file
extern void *lib_malloc(size_t size);
extern void *lib_realloc(void *p, size_t size);
extern void lib_free(void *ptr);
extern char *lib_stralloc(const char *str);
extern int util_string_set(char **str, const char *new_value);
extern unsigned long crc32_buf(const char *buffer, unsigned int len);
#endif