Skip to content

Commit

Permalink
fix free of wrong thing, misc cleanup (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhinakg authored Jun 10, 2023
1 parent 0fd73ae commit 51adc70
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 80 deletions.
22 changes: 8 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# CMakeList.txt : CMake project for Win32Test, include source and define
# CMakeList.txt : CMake project for usbdump, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.16)
cmake_policy(SET CMP0091 NEW)
project ("Win32Test" LANGUAGES C CXX VERSION 0.1)
# cmake_policy(SET CMP0149 NEW)
project ("usbdump" LANGUAGES C CXX VERSION 0.1)
include(FetchContent)

# find static libraries (so MT can work ??)
Expand All @@ -19,9 +20,8 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")

FetchContent_Declare(json
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
GIT_TAG v3.9.1)
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz)
FetchContent_MakeAvailable(json)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_compile_options(/ZH:SHA_256 /W4 /permissive- /Zc:__cplusplus)
Expand All @@ -39,21 +39,15 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
add_compile_options(-O3 -fsanitize=undefined,address)
add_link_options(-fsanitize=undefined,address)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options(-O3 -fsanitize=undefined,address -fsanitize-blacklist=C:/Users/dhina/Documents/GitHub/Win32Testv2/fsanitize-blacklist.txt)
add_link_options(-fsanitize=undefined,address -fsanitize-blacklist=C:/Users/dhina/Documents/GitHub/Win32Testv2/fsanitize-blacklist.txt)
add_compile_options(-O3 -fsanitize=undefined,address -fsanitize-blacklist=C:/Users/dhina/Documents/GitHub/usbdump/fsanitize-blacklist.txt)
add_link_options(-fsanitize=undefined,address -fsanitize-blacklist=C:/Users/dhina/Documents/GitHub/usbdump/fsanitize-blacklist.txt)
endif()
endif()

FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

add_compile_definitions(UNICODE)

# Add source to this project's executable.
add_executable (usbdump "Win32Test.cpp" "enum.cpp" "devnode.cpp" "Win32Test.h" "usbdesc.h" "uvcdesc.h" "uvcview.h")
add_executable (usbdump "usbdump.cpp" "enum.cpp" "devnode.cpp" "usbdump.h" "usbdesc.h" "uvcdesc.h" "uvcview.h")
set_property(TARGET usbdump PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

Expand Down
54 changes: 21 additions & 33 deletions enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Revision History:
//*****************************************************************************

#include "uvcview.h"
#include "utils.hpp"

//*****************************************************************************
// D E F I N E S
Expand Down Expand Up @@ -218,11 +219,8 @@ FindMatchingDeviceNodeForDriverName(

// List of enumerated host controllers.
//
LIST_ENTRY EnumeratedHCListHead =
{
&EnumeratedHCListHead,
&EnumeratedHCListHead
};

std::vector<PUSBHOSTCONTROLLERINFO> EnumeratedHCList;

DEVICE_GUID_LIST gHubList;
DEVICE_GUID_LIST gDeviceList;
Expand Down Expand Up @@ -348,7 +346,9 @@ EnumerateHostControllers (
deviceDetailData->DevicePath,
deviceInfo,
&deviceInfoData);
hostControllers.push_back(info);
if (info) {
hostControllers.push_back(info);
}

CloseHandle(hHCDev);
}
Expand All @@ -359,7 +359,7 @@ EnumerateHostControllers (
SetupDiDestroyDeviceInfoList(deviceInfo);

*DevicesConnected = TotalDevicesConnected;

return hostControllers;
}

Expand All @@ -383,9 +383,7 @@ EnumerateHostController (
PWCHAR driverKeyName = NULL;
HTREEITEM hHCItem = NULL;
PWCHAR rootHubName = NULL;
PLIST_ENTRY listEntry = NULL;
PUSBHOSTCONTROLLERINFO hcInfo = NULL;
PUSBHOSTCONTROLLERINFO hcInfoInList = NULL;
DWORD dwSuccess;
BOOL success = FALSE;
ULONG deviceAndFunction = 0;
Expand Down Expand Up @@ -417,25 +415,16 @@ EnumerateHostController (
// Don't enumerate this host controller again if it already
// on the list of enumerated host controllers.
//
listEntry = EnumeratedHCListHead.Flink;

if (!listEntry) return NULL;
while (listEntry != &EnumeratedHCListHead)
for (PUSBHOSTCONTROLLERINFO hcInfoInList : EnumeratedHCList)
{
hcInfoInList = CONTAINING_RECORD(listEntry,
USBHOSTCONTROLLERINFO,
ListEntry);

if (hcInfoInList && wcscmp(driverKeyName, hcInfoInList->DriverKey) == 0)
if (wcscmp(driverKeyName, hcInfoInList->DriverKey) == 0)
{
// Already on the list, exit
//
FREE(driverKeyName);
FREE(hcInfo);
return NULL;
}

listEntry = listEntry->Flink;
}

// Obtain host controller device properties
Expand Down Expand Up @@ -554,8 +543,7 @@ EnumerateHostController (
// Add this host controller to the list of enumerated
// host controllers.
//
InsertTailList(&EnumeratedHCListHead,
&hcInfo->ListEntry);
EnumeratedHCList.push_back(hcInfo);

// Get the name of the root hub for this host
// controller and then enumerate the root hub.
Expand Down Expand Up @@ -1826,7 +1814,7 @@ PWCHAR GetHCDDriverKeyName (
ULONG nBytes = 0;
USB_HCD_DRIVERKEY_NAME driverKeyName = {0};
PUSB_HCD_DRIVERKEY_NAME driverKeyNameW = NULL;
// PWCHAR pWideStr = NULL;
PWCHAR pWideStr = NULL;

ZeroMemory(&driverKeyName, sizeof(driverKeyName));

Expand Down Expand Up @@ -1888,19 +1876,19 @@ PWCHAR GetHCDDriverKeyName (


// Use local string to guarantee zero termination
// pWideStr = (PWCHAR) ALLOC((DWORD) nBytes - sizeof(USB_HCD_DRIVERKEY_NAME) + sizeof(WCHAR) + sizeof(WCHAR));
// if (NULL == pWideStr)
// {
// return NULL;
// }
// memset(pWideStr, 0, nBytes - sizeof(USB_HCD_DRIVERKEY_NAME) + sizeof(WCHAR) + sizeof(WCHAR));
// memcpy(pWideStr, driverKeyNameW->DriverKeyName, nBytes - sizeof(USB_HCD_DRIVERKEY_NAME) + sizeof(WCHAR));
pWideStr = (PWCHAR) ALLOC((DWORD) nBytes - sizeof(USB_HCD_DRIVERKEY_NAME) + sizeof(WCHAR) + sizeof(WCHAR));
if (NULL == pWideStr)
{
return NULL;
}
memset(pWideStr, 0, nBytes - sizeof(USB_HCD_DRIVERKEY_NAME) + sizeof(WCHAR) + sizeof(WCHAR));
memcpy(pWideStr, driverKeyNameW->DriverKeyName, nBytes - sizeof(USB_HCD_DRIVERKEY_NAME) + sizeof(WCHAR));
//
// FREE(driverKeyNameW);
FREE(driverKeyNameW);
//
// return pWideStr;
return pWideStr;

return driverKeyNameW->DriverKeyName;
// return driverKeyNameW->DriverKeyName;

GetHCDDriverKeyNameError:
// There was an error, free anything that was allocated
Expand Down
30 changes: 2 additions & 28 deletions specializations.hpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
#pragma once

#include "uvcview.h"
#include "nlohmann/json.hpp"
#include <nlohmann/json.hpp>
#include "debugapi.h"
#include "utils.hpp"

#include <locale>
#include <codecvt>


using json = nlohmann::json;

// https://stackoverflow.com/a/3999597
// Thank you tfinniga!

// Convert a wide Unicode string to an UTF8 string
static std::string utf8_encode(const std::wstring &wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
// Convert an UTF8 string to a wide Unicode String
static std::wstring utf8_decode(const std::string &str)
{
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
#pragma clang diagnostic pop


#define VA_LIST(...) __VA_ARGS__

#define ADD_JSON_STRUCT(a, unsafe, safe) \
Expand Down
7 changes: 4 additions & 3 deletions Win32Test.cpp → usbdump.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Win32Test.cpp : Defines the entry point for the application.
// usbdump.cpp : Defines the entry point for the application.
//

#include "Win32Test.h"
#include "usbdump.h"

using namespace std;
using json = nlohmann::json;
Expand All @@ -16,7 +16,8 @@ void initGlobals() {
int main(int argc, char* argv[]) {
if (argc >= 2) {
if (std::string(argv[1]) == "--version" || std::string(argv[1]) == "-v") {
std::cout << TOOL_VERSION << std::endl;
std::cout << "usbdump " TOOL_VERSION << std::endl;
std::cout << "Built on " __DATE__ " " __TIME__ << std::endl;
return 0;
} else {
return 1;
Expand Down
4 changes: 2 additions & 2 deletions Win32Test.h → usbdump.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Win32Test.h : Include file for standard system include files,
// usbdump.h : Include file for standard system include files,
// or project specific include files.

#pragma once
Expand All @@ -21,7 +21,7 @@
#include "specializations.hpp"
#include "nlohmann/json.hpp"

#define TOOL_VERSION "0.1.1"
#define TOOL_VERSION "0.2"


#define QUICKCOUT(...) std::cout << __VA_ARGS__ << std::endl
Expand Down
33 changes: 33 additions & 0 deletions utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <locale>
#include <codecvt>
#include <stringapiset.h>

// https://stackoverflow.com/a/3999597
// Thank you tfinniga!

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"

// Convert a wide Unicode string to an UTF8 string
static inline std::string utf8_encode(const std::wstring &wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}

// Convert an UTF8 string to a wide Unicode String
static inline std::wstring utf8_decode(const std::string &str)
{
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}

#pragma clang diagnostic pop

0 comments on commit 51adc70

Please sign in to comment.