Skip to content

Instantly share code, notes, and snippets.

@shangdawei
shangdawei / tea.pas
Created July 4, 2016 11:51
TEA Pascal
type
TTEA_InOut = array[0..7] of byte;
TTEA_Key = array[0..15] of byte;
var
TEA_InitKey : TTEA_Key = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
TEA_InitData : TTEA_InOut = ($0a,$3a,$ea,$41,$40,$a9,$ba,$94);
type
TTeaArray = array [0 .. 1] of Cardinal;
@shangdawei
shangdawei / injector.cpp
Created July 8, 2016 20:55 — forked from Zoxc/injector.cpp
DLL Injector for x86/x64
#pragma once
#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <tchar.h>
#include <shellapi.h>
#include <string>
#include <sstream>
#include <functional>
@shangdawei
shangdawei / Cortex debug condition assertion.c
Created October 9, 2017 11:42
Cortex debug condition assertion
Cortex debug condition assertion
Used to popular ASSERT(condition) macro? Code below stops on faulty condition when debugging Cortex CPU. BE CAREFUL: use code build with _DEBUG or DEBUG define when debugging BUT DON'T use same code without debugger.
#if defined ( _DEBUG ) || defined ( DEBUG )
#define ASSERT(expr) { if ( !(expr) ) { __ASM__(" bkpt #0"); } }
#else
#define ASSERT(...) { }
#endif
Usage:
@shangdawei
shangdawei / _CDCDSerialDriverConfigurationDescriptors.c
Last active October 9, 2017 12:04
_CDCDSerialDriverConfigurationDescriptors struct
/**
* \typedef CDCDSerialDriverConfigurationDescriptors
* \brief Configuration descriptor list for a device implementing a
* CDC serial driver.
*/
typedef struct _CDCDSerialDriverConfigurationDescriptors {
/** Standard configuration descriptor. */
USBConfigurationDescriptor configuration;
/** Communication interface descriptor. */
@shangdawei
shangdawei / gist:599fdc2c7aa693d26973954a3882b581
Created October 16, 2017 12:08
RegEx Convert Upper to Case
TextPad will allow you to perform this operation.
example:
test this sentence
Find what: \([^ ]*\) \(.*\) Replace with: \U\1\E \2
the \U will cause all following chars to be upper
the \E will turn off the \U
@shangdawei
shangdawei / RTOS functions Malloc Free.c
Created October 17, 2017 09:23
RTOS functions Malloc Free.c
/**********************************************************************
*
* OS_malloc
*
***********************************************************************
Purpose:
*/
void* OS_malloc(unsigned int Size) {
@shangdawei
shangdawei / MSB LSB.h
Created October 18, 2017 01:18
MSB LSB
#define LSB(hword) (((uint8_t *)&(hword))[1]) // Least significant byte of hword.
#define MSB(hword) (((uint8_t *)&(hword))[0]) // Most significant byte of hword.
#define LSH(word) (((uint16_t *)&(word))[1]) // Least significant half-hword of word.
#define MSH(word) (((uint16_t *)&(word))[0]) // Most significant half-hword of word.
#define LSW(dword) (((uint32_t *)&(dword))[1]) // Least significant hword of dword.
#define MSW(dword) (((uint32_t *)&(dword))[0]) // Most significant hword of dword.
#define MSB0W(word) (((uint8_t *)&(word))[0]) // Most significant byte of 1st rank of word.
@shangdawei
shangdawei / BE LE.h
Last active December 20, 2023 10:02
Big Endian and Little Endian Convert
#if BYTE_ORDER == __ORDER_BIG_ENDIAN__
//Host byte order to network byte order (at compile time)
#define HTONS(value) (value)
#define HTONL(value) (value)
//Network byte order to host byte order (at compile time)
#define NTOHS(value) (value)
#define NTOHL(value) (value)
@shangdawei
shangdawei / Unnamed struct union fields within structs unions.c
Created October 18, 2017 06:54
Unnamed struct/union fields within structs/unions
// https://gcc.gnu.org/onlinedocs/gcc-4.8.4/gcc/Unnamed-Fields.html
#include <stdint.h>
typedef union
{
uint8_t s8;
struct {
uint8_t b0 : 1;
uint8_t b1 : 1;
@shangdawei
shangdawei / BitBLT ROP.c
Last active October 20, 2017 03:40
BitBLT ROP
BF_XFER_0 = BF_XFER_BLACKNESS, // BLACKNESS ---------- Dst < 0 : BitFieldClr
BF_XFER_1 = BF_XFER_WHITENESS, // WHITENESS ---------- Dst < 1 : BitFieldSet
BF_XFER_BLACKNESS = 0UL, // BLACKNESS ---------- Dst < 0 : BitFieldClr
BF_XFER_DN = BF_XFER_DSTINVERT, // DSTINVERT ---------- Dst ^ 1 : BitFieldInv
BF_XFER_DSA = BF_XFER_SRCAND, // SRCAND ------------- Src & Dst
BF_XFER_DSNO = BF_XFER_MERGEPAINT, // MERGEPAINT --------- ~Src | Dst
BF_XFER_DSO = BF_XFER_SRCPAINT, // SRCPAINT ----------- Src | Dst
BF_XFER_DSON = BF_XFER_NOTSRCERASE, // NOTSRCERASE -------- ~(Src | Dst)
BF_XFER_DSTINVERT, // DSTINVERT ---------- Dst ^ 1 : BitFieldInv
BF_XFER_DSX = BF_XFER_SRCINVERT, // SRCINVERT ---------- Src ^ Dst