Skip to content

Instantly share code, notes, and snippets.

View voidbar's full-sized avatar

Tomer Lev voidbar

View GitHub Profile
@voidbar
voidbar / to_string_generic_c++17.cpp
Last active January 30, 2022 08:42
Number to string function using constexpr lamba with auto deductible parameters
#include <charconv>
#include <iostream>
#include <array>
constexpr auto ToString = [](auto number) constexpr -> const std::string {
std::array<char, 20 /* Max chars in 64 bit number*/> buffer;
if (auto [ptr, ec] = std::to_chars(buffer.data(), buffer.data() + buffer.size(), number); ec == std::errc())
{
return std::string{buffer.data(), static_cast<size_t>(ptr - buffer.data())};
}
@voidbar
voidbar / windows_short_to_long_path.cpp
Last active August 25, 2022 20:31
Using std::filesystem trick to convert windows short path into long path
#include <filesystem>
#include <iostream>
#include <format> // C++20 Required
namespace fs = std::filesystem;
std::string to_long_path(const std::string& short_path) {
return fs::canonical(short_path).string();
}
@voidbar
voidbar / teams_bot_with_markdown.py
Last active January 30, 2022 08:41
Send a Teams message with proper markdown support using python
import logging
import requests
import sys
from os import path
# https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook
TEAMS_WEBHOOK = "{YOUR_WEBHOOK_LINK}"
LOG_FILE = path.join(path.dirname(__file__), "log.txt")
logger = logging.getLogger(__name__)
@voidbar
voidbar / get_null_terminator_templated.cpp
Created February 6, 2022 13:49
A small utility function to get the null terminator of some string type. Can be useful when dealing with systems where sizeof(wchar_t) can change
#include <string>
#include <iostream>
#include <string>
template <typename T>
constexpr auto NullTerminator() {
return std::string(sizeof(typename T::value_type), '\0');
}
int main() {
@voidbar
voidbar / meyers_singleton.cpp
Last active August 25, 2022 20:58
Simple Meyers singleton example C++17
#include <string>
#include <iostream>
#include <chrono>
#include <thread>
#include <format> // Requires C++20
using system_clock = std::chrono::system_clock;
using namespace std::chrono_literals;
const std::string& get_static_message() {
#include <tuple>
template <typename... Args>
constexpr void ignore0(Args... args)
{
constexpr auto dummy = [](auto&& a) { std::ignore = a;};
(dummy(std::forward<Args>(args)), ...);
}
template <typename... Args>
@voidbar
voidbar / Set-DevShell.ps1
Created April 14, 2022 07:41
Modern MSVC open developer shell script using Powershell
# Require `Install-Module VSSetup -Scope CurrentUser`
function vsdev
{
param(
[int] $Version = 16 # VS 2019
)
$vspath = Get-VSSetupInstance | where { $_.InstallationVersion.Major -eq $Version } | select -first 1 -ExpandProperty InstallationPath
Import-Module "$vspath\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" && Enter-VsDevShell -VsInstallPath $vspath -SkipAutomaticLocation -DevCmdArguments '-arch=x64'
}
@voidbar
voidbar / bytes-to-pcap.py
Last active May 25, 2022 12:27
Creating a valid pcap from raw packet ethernet bytes using scapy
import sys
import scapy.all as s
pkt_bytes = [0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb, 0x48, 0x4d, 0x7e, 0xb9, 0x4d, 0x34, 0x08, 0x00, 0x45, 0x00, 0x00, 0x6a,
0xae, 0x07, 0x40, 0x00, 0xff, 0x11, 0x8e, 0x1a, 0x0a, 0xa6, 0x53, 0xbf, 0xe0, 0x00, 0x00, 0xfb, 0x14, 0xe9,
0x14, 0xe9, 0x00, 0x56, 0xf6, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, 0xd7, 0x94, 0xd7, 0x99, 0x21, 0x00, 0x00, 0x0c, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x0c, 0x00, 0x01, 0x00,
0x00, 0x11, 0x94, 0x00, 0x20, 0x1d, 0x48, 0x50, 0x20, 0x4c, 0x61, 0x73, 0x65, 0x72, 0x4a, 0x65, 0x74, 0x20,
0x4d, 0x46, 0x50, 0x20, 0x4d, 0x36, 0x33, 0x30, 0x20, 0x5b, 0x43, 0x41, 0x43, 0x39, 0x31]
@voidbar
voidbar / spot-the-bug-lambda.cpp
Created June 26, 2022 10:23
C++ Spot the Bug
template <typename Func>
auto decorator(Func func) {
return [&]{
std::cout << "Calling f():\n";
func();
};
}
auto foo(fs::path filepath) {
auto func = [filepath] {
std::cout << filepath << "\n";
@voidbar
voidbar / run.ps1
Created January 12, 2023 16:08
Why is it so hard to mount a windows directory to a Windows container?
# Mounting current directory to C:/tmp inside Server 2016 docker image
docker run --mount src="$pwd",dst=C:/tmp/,type=bind -it mcr.microsoft.com/windows/servercore:ltsc2016 powershell