Skip to content

Instantly share code, notes, and snippets.

View shawnfeng0's full-sized avatar
💭
Keep thinking

Shawn Feng shawnfeng0

💭
Keep thinking
View GitHub Profile
@shawnfeng0
shawnfeng0 / mutex_queue.h
Last active March 22, 2022 04:09
simple queue with mutex
//
// Created by shawnfeng on 2021-08-12.
// https://gist.github.com/ShawnFeng0/429b6caef85ec0d32f34081374dcdb8a
//
#pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
@shawnfeng0
shawnfeng0 / create_gitkeep_to_empty_directory.sh
Created April 13, 2021 07:32
Create .gitkeep file to empty directory
find -type d -empty | xargs -t -L1 -I _ touch _/.gitkeep
@shawnfeng0
shawnfeng0 / change_git_commit_message.sh
Last active August 4, 2021 13:44
change git commit message
#!/bin/sh
if [ $# -lt 3 ] ; then
echo "Usage: $0 old_email new_email new_name"
echo "Example: $0 [email protected] [email protected] new_name"
exit 1;
fi
echo "old_email: $1"
echo "new_email: $2"
@shawnfeng0
shawnfeng0 / dd_read_write_test.sh
Last active April 7, 2021 09:14
dd read and write test
sync && dd if=/dev/zero of=/var/avi/mmc0/tmp.bin bs=1M count=100 && sync && dd if=/var/avi/mmc0/tmp.bin of=/dev/null bs=1M
@shawnfeng0
shawnfeng0 / openssl_crypt.cc
Last active April 15, 2021 09:03
Use openssl crypt
#include <openssl/evp.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
// From https://www.openssl.org/docs/man1.1.1/man3/EVP_EncryptInit.html
static bool CryptAes256Cfb(const std::vector<uint8_t> &key,
const std::vector<uint8_t> &iv,
@shawnfeng0
shawnfeng0 / wpa_ssid_convert_utf8.cc
Last active March 29, 2021 07:53
wpa_ssid_convert_utf8
unsigned char HexNumber(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else {
return 0;
}
@shawnfeng0
shawnfeng0 / split.h
Created March 2, 2021 05:59
string split function for C++
#include <vector>
#include <string>
inline std::vector<std::string> Split(const std::string &s, const std::string &delimiters = " ") {
std::vector<std::string> tokens;
std::string::size_type start = s.find_first_not_of(delimiters, 0);
std::string::size_type end = s.find_first_of(delimiters, start);
while (std::string::npos != end || std::string::npos != start) {
tokens.emplace_back(s.substr(start, end - start));
start = s.find_first_not_of(delimiters, end);
@shawnfeng0
shawnfeng0 / sockaddr_tostr.h
Created December 23, 2020 04:56 — forked from jkomyno/sockaddr_tostr.h
Convert a struct sockaddr address to a string, IPv4 and IPv6
// Convert a struct sockaddr address to a string, IPv4 and IPv6:
char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
{
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
s, maxlen);
break;
template<typename T>
static inline auto RoundPowOfTwo(T n) -> decltype(n) {
uint64_t value = n;
// Fill 1
value |= value >> 1U;
value |= value >> 2U;
value |= value >> 4U;
value |= value >> 8U;
value |= value >> 16U;
void *memcpy(void *dst, void *src, unsigned n) {
if (!dst || !src) {
return nullptr;
}
auto dst_c = (char *) dst;
auto src_c = (char *) src;
if (dst_c < src || dst_c > (src_c + n)) {
while (n--) {