Skip to content

Instantly share code, notes, and snippets.

View pps83's full-sized avatar

Pavel P pps83

View GitHub Profile
@pps83
pps83 / rand.h
Created November 22, 2019 05:53
pcg-based rand32() and rand64()
// pcg-based rand32() and rand64()
#include <stdint.h>
struct pcg32_random_t
{
uint64_t state;
};
static const uint64_t inc = 997;
static struct pcg32_random_t rndState = {99991};
// check TCP connect to port/host
var sock = require('net').connect({ port: 443, host: '127.0.0.1' }).on('connect', () => { console.log('connect OK'); sock.end(); }).on('error', (e) => { console.log('connect error (code:'+e.code+')'); })
// listen for TCP connect on port/host
var server = require('net').createServer((sock) => { console.log('client connected'); sock.end(); }).listen({ port: 443, host: '127.0.0.1' });
run node server.js
then build and run curl-test.cpp
Everything will be ok:
```
CURLINFO_ACTIVESOCKET, ret:0 sockWs:276
curl_easy_send ret:0 sentSize:194
curl_easy_recv ret:0 recvSize:167
CURLINFO_ACTIVESOCKET, ret:0 sockWs:296
curl_easy_send ret:0 sentSize:194
curl_easy_recv ret:0 recvSize:167
@pps83
pps83 / main.cpp
Created June 10, 2019 02:01
Test simdfastpfor128
#undef NDEBUG
#include "compositecodec.h"
#include "simdfastpfor.h"
#include "variablebyte.h"
#include <stdlib.h>
#include <vector>
// typedef for simdfastpfor128 codec from FastPFor
typedef FastPForLib::CompositeCodec<FastPForLib::SIMDFastPFor<4>, FastPForLib::VariableByte> IntCodec;
@pps83
pps83 / computeHash
Created July 11, 2018 03:21
Compute sha1/md5/hmac-sha1/hmac-md5 using windows API
#ifdef _WIN32
#include <windows.h>
#if !defined(PLATFORM_XBOX) && !defined(PLATFORM_UWP)
#include <wincrypt.h>
#else
#include <bcrypt.h>
#pragma comment(lib, "bcrypt.lib")
#endif
int computeHash(const void* data, unsigned dataSize, unsigned char hashBuf[64], const void *hmacSecret, unsigned hmacSecretSize, bool isMD5)
@pps83
pps83 / ctz_clz.cpp
Last active June 17, 2024 10:45
__builtin_ctz (ctzl, ctzll) and __builtin_clz (clzl, clzll) for Visual Studio
// Note, bsf/bsr are used by default.
// Enable /arch:AVX2 compilation for better optimizations
#if defined(_MSC_VER) && !defined(__clang__)
#include <intrin.h>
static __forceinline int __builtin_ctz(unsigned x)
{
#if defined(_M_ARM) || defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC)
return (int)_CountTrailingZeros(x);
#define _WS2DEF_
#include "inaddr.h"
#include "MSTcpIP.h"
void startServer();
void startClient_6_to_4(short xxport);
void startClient_4_to_4(short xxport);
void startClient_6_to_6(short xxport);
short xxport = 8112;
#ifdef _WIN32 // inet_pton is available from Vista only (based on inet_pton.c from wireshark)
#define NS_INADDRSZ_ 4
#define NS_IN6ADDRSZ_ 16
#define NS_INT16SZ_ 2
static int inet_pton4(const char *src, unsigned char *dst)
{
char saw_digit = 0, octets = 0, ch;
unsigned char tmp[NS_INADDRSZ_], *tp = tmp;
@pps83
pps83 / DoSuspendThread.cpp
Created February 15, 2018 21:31
suspend threads in the current process
#include <windows.h>
#include <tlhelp32.h>
// Pass 0 as the targetProcessId to suspend threads in the current process
void DoSuspendThread(DWORD targetProcessId, DWORD targetThreadId)
{
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (h != INVALID_HANDLE_VALUE)
{
THREADENTRY32 te;
@pps83
pps83 / WriteMinidumpForException.cpp
Last active February 7, 2018 07:11
WriteMinidumpForException
#include <Windows.h>
#include <stdio.h>
// fullDump enables MiniDumpWithFullMemory, and results in dmp files to be over 100MB in size (as opposed to default which is only 100KB).
// With fullDump Visual Studio is able to display proper backtrace for unhandled c++ exception. Without fullDump WinDBG has to be used.
static int WriteMinidumpForException(EXCEPTION_POINTERS* e, bool fullDump)
{
HMODULE dbghelp = LoadLibraryA("DbgHelp.dll"); // Note: no matching FreeLibrary
if (!dbghelp)
return EXCEPTION_CONTINUE_SEARCH;