Skip to content

Commit

Permalink
move some common macro or functions to commdef.h
Browse files Browse the repository at this point in the history
  • Loading branch information
devseed committed Apr 7, 2024
1 parent 72c9906 commit b923372
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 332 deletions.
7 changes: 4 additions & 3 deletions project/windll_winhook/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# build example
# build example, tested in linux 10.0.0-3, gcc 12, wine-9.0
# make libwinhook helloexe hellodll libwinhook_test CC=i686-w64-mingw32-gcc BUILD_TYPE=32d
# make libwinhook helloexe hellodll libwinhook_test CC=x86_64-w64-mingw32-gcc BUILD_TYPE=64d
# wine build/libwinhook_test32d.exe && wine build/libwinhook_test64d.exe
# cd build; wine libwinhook_test32d.exe; cd -
# cd build; wine libwinhook_test64d.exe; cd -

# general config
CC:=gcc # clang (llvm-mingw), gcc (mingw-w64), tcc (x86 stdcall name has problem)
Expand Down Expand Up @@ -42,7 +43,7 @@ clean:
@rm -rf $(BUILD_DIR)/*test*

prepare:
@if ! [ -d $(BUILD_DIR) ]; then mkdir -p $(BUILD_DIR); fi
@mkdir -p $(BUILD_DIR)

libwinhook: src/libwinhook.c
@echo "## $@"
Expand Down
9 changes: 5 additions & 4 deletions project/windll_winpe/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# build example
# build example, tested in linux 10.0.0-3, gcc 12, wine-9.0
# make libwinpe libwinpe_test CC=i686-w64-mingw32-gcc BUILD_TYPE=32d
# make libwinpe libwinpe_test CC=x86_64-w64-mingw32-gcc BUILD_TYPE=64d
# wine build/libwinpe_test32d.exe && wine build/libwinpe_test64d.exe
# wine build/libwinpe_test32d.exe
# wine build/libwinpe_test64d.exe

# general config
CC:=gcc # clang (llvm-mingw), gcc (mingw-w64), tcc (x86 stdcall name has problem)
Expand Down Expand Up @@ -40,11 +41,11 @@ clean:
@rm -rf $(BUILD_DIR)/*libwinpe*

prepare:
@if ! [ -d $(BUILD_DIR) ]; then mkdir -p $(BUILD_DIR); fi
@mkdir -p $(BUILD_DIR)

libwinpe: src/libwinpe.c
@echo "## $@"
$(CC) -shared $^ -o $(BUILD_DIR)/$@$(BUILD_TYPE).dll \
$(CC) -shared $< -o $(BUILD_DIR)/$@$(BUILD_TYPE).dll \
$(INCS) $(LIBS) \
$(CFLAGS) $(LDFLAGS)

Expand Down
2 changes: 1 addition & 1 deletion project/winexe_winloader/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# build example
# build example, tested in linux 10.0.0-3, gcc 12, wine-9.0
# make winloader CC=i686-w64-mingw32-gcc WINDRES=i686-w64-mingw32-windres BUILD_TYPE=32d
# make winloader CC=x86_64-w64-mingw32-gcc WINDRES=x86_64-w64-mingw32-windres BUILD_TYPE=64d

Expand Down
2 changes: 1 addition & 1 deletion project/winexe_winloader/src/winloader.rc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "resource.h"
#include "winres.h"
IDI_ICON1 ICON ".\\..\\..\\..\\asset\\default.ico"
IDI_ICON1 ICON "../../../asset/default.ico"
219 changes: 219 additions & 0 deletions src/commdef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/**
* common macro define
* v0.1, developed by devseed
*/

#ifndef _COMMDEF_H
#define _COMMDEF_H
#define COMMDEF_VERSION 100
#include <stdio.h>
#include <stdint.h>

// function declear macro
#if defined(_MSC_VER) || defined(__TINYC__)
#ifndef STDCALL
#define STDCALL __stdcall
#endif
#ifndef NAKED
#define NAKED __declspec(naked)
#endif
#ifndef INLINE
#define INLINE __forceinline
#endif
#ifndef EXPORT
#define EXPORT __declspec(dllexport)
#endif
#else
#ifndef STDCALL
#define STDCALL __attribute__((stdcall))
#endif
#ifndef NAKED
#define NAKED __attribute__((naked))
#endif
#ifndef INLINE
#define INLINE __attribute__((always_inline)) inline
#endif
#ifndef EXPORT
#define EXPORT __attribute__((visibility("default")))
#endif
#endif // _MSC_VER
#if defined(__TINYC__) // fix tcc not support inline
#ifdef INLINE
#undef INLINE
#endif
#define INLINE
#endif // __TINYC__
#ifndef IN
#define IN
#endif // IN
#ifndef OUT
#define OUT
#endif // OUT
#ifndef OPTIONAL
#define OPTIONAL
#endif // OPTIONAL

// log macro
#ifndef LOG_LEVEL_
#define LOG_LEVEL_
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_WARNING 2
#define LOG_LEVEL_INFO 3
#define LOG_LEVEL_DEBUG 4
#define LOG_LEVEL_VERBOSE 5
#define LogTagPrintf(format, tag, ...) \
printf("[%s,%d,%s,%s] ", __FILE__, __LINE__, __func__, tag);\
printf(format, ##__VA_ARGS__);
#define LogTagWprintf(format, tag, ...) \
wprintf(L"[%ls,%d,%ls,%ls] ", __FILE__, __LINE__, __func__, tag);\
wprintf(format, ##__VA_ARGS__);
#define DummyPrintf(format, ...)
#define LOG(format, ...) LogTagPrintf(format, "I", ##__VA_ARGS__)
#define LOGL(format, ...) LogTagWprintf(format, L"I", ##__VA_ARGS__)
#define LOGe(format, ...) LogTagPrintf(format, "E", ##__VA_ARGS__)
#define LOGLe(format, ...) LogTagWprintf(format, L"E", ##__VA_ARGS__)
#define LOGw(format, ...) LogTagPrintf(format, "W", ##__VA_ARGS__)
#define LOGLw(format, ...) LogTagWprintf(format, L"W", ##__VA_ARGS__)
#define LOGi(format, ...) LogTagPrintf(format, "I", ##__VA_ARGS__)
#define LOGLi(format, ...) LogTagWprintf(format, L"I", ##__VA_ARGS__)
#define LOGd(format, ...) LogTagPrintf(format, "D", ##__VA_ARGS__)
#define LOGLd(format, ...) LogTagWprintf(format, L"D", ##__VA_ARGS__)
#define LOGv(format, ...) LogTagPrintf(format, "V", ##__VA_ARGS__)
#define LOGLv(format, ...) LogTagWprintf(format, L"V", ##__VA_ARGS__)
#endif // LOG_LEVEL_
#ifndef LOG_LEVEL
#define LOG_LEVEL LOG_LEVEL_INFO
#endif // LOG_LEVEL
#if LOG_LEVEL < LOG_LEVEL_WARNING
#undef LOGw
#undef LOGLw
#define LOGw DummyPrintf
#define LOGLw DummyPrintf
#endif // LOG_LEVEL_WARNING
#if LOG_LEVEL < LOG_LEVEL_INFO
#undef LOGi
#undef LOGLi
#define LOGi DummyPrintf
#define LOGLi DummyPrintf
#endif // LOG_LEVEL_INFO
#if LOG_LEVEL < LOG_LEVEL_DEBUG
#undef LOGd
#undef LOGLd
#define LOGd DummyPrintf
#define LOGLd DummyPrintf
#endif // LOG_LEVEL_DEBUG
#if LOG_LEVEL < LOG_LEVEL_VERBOSE
#undef LOGv
#undef LOGLv
#define LOGv DummyPrintf
#define LOGLv DummyPrintf
#endif // LOG_LEVEL_VERBOSE

// util macro
#define DUMP(path, addr, size) \
FILE *fp = fopen(path, "wb"); \
fwrite(addr, 1, size, fp); \
fclose(fp);

// inline functions
static INLINE size_t inl_strlen(const char *str1)
{
const char* p = str1;
while(*p) p++;
return p - str1;
}

static INLINE int inl_stricmp(const char *str1, const char *str2)
{
int i=0;
while(str1[i]!=0 && str2[i]!=0)
{
if (str1[i] == str2[i]
|| str1[i] + 0x20 == str2[i]
|| str2[i] + 0x20 == str1[i])
{
i++;
}
else
{
return (int)str1[i] - (int)str2[i];
}
}
return (int)str1[i] - (int)str2[i];
}

static INLINE int inl_stricmp2(const char *str1, const wchar_t *str2)
{
int i=0;
while(str1[i]!=0 && str2[i]!=0)
{
if ((wchar_t)str1[i] == str2[i]
|| (wchar_t)str1[i] + 0x20 == str2[i]
|| str2[i] + 0x20 == (wchar_t)str1[i])
{
i++;
}
else
{
return (int)str1[i] - (int)str2[i];
}
}
return (int)str1[i] - (int)str2[i];
}

static INLINE int inl_wcsicmp(const wchar_t *str1, const wchar_t *str2)
{
int i = 0;
while (str1[i] != 0 && str2[i] != 0)
{
if (str1[i] == str2[i]
|| str1[i] + 0x20 == str2[i]
|| str2[i] + 0x20 == str1[i])
{
i++;
}
else
{
return (int)str1[i] - (int)str2[i];
}
}
return (int)str1[i] - (int)str2[i];
}

static INLINE uint32_t inl_crc32(const void *buf, size_t n)
{
uint32_t crc32 = ~0;
for(size_t i=0; i< n; i++)
{
crc32 ^= *(const uint8_t*)((uint8_t*)buf+i);

for(int i = 0; i < 8; i++)
{
uint32_t t = ~((crc32&1) - 1);
crc32 = (crc32>>1) ^ (0xEDB88320 & t);
}
}
return ~crc32;
}

static INLINE void* inl_memset(void *buf, int ch, size_t n)
{
char *p = (char *)buf;
for(size_t i=0;i<n;i++) p[i] = (char)ch;
return buf;
}

static INLINE void* inl_memcpy(void *dst, const void *src, size_t n)
{
char *p1 = (char*)dst;
char *p2 = (char*)src;
for(size_t i=0;i<n;i++) p1[i] = p2[i];
return dst;
}

#endif // _COMMDEF_H

/**
* history
* v0.1, initial version
*/
Loading

0 comments on commit b923372

Please sign in to comment.