Skip to content

Instantly share code, notes, and snippets.

@lihnux
lihnux / check_special_ip.c
Last active September 5, 2023 00:26
一些网络相关的代码
// 判断一个IP地址,是否属于以下网段:
// 10.0.0.0/8, 169.254.0.0/16, 172.16.0.0/12, 192.168.0.0/16, 224.0.0.0/4, 255.255.255.255/32, fc00::/7, fe80::/10, ff00::/8
#include <stdint.h>
#include <arpa/inet.h>
int is_special_ip(uint32_t ipv4) {
uint8_t first_octet = ipv4 >> 24;
if (first_octet == 10) return 1;
if (first_octet == 169 && (ipv4 & 0xFFFF0000) == 0xA9FE0000) return 1;
@lihnux
lihnux / AliyunSMS.py
Created January 19, 2018 02:53
2018.01.19 -- 可用的Python3的阿里云短信发送的代码
#coding: utf-8
import uuid
import datetime
import hmac
import base64
import requests
from urllib.parse import urlencode, quote
class AliSMS():
@lihnux
lihnux / 老王装货.cpp
Last active April 21, 2017 09:32
动态规划-简易背包问题
#include <stdlib.h>
#include <stdio.h>
// 千里码刷题: http://www.qlcoder.com/task/7566
static const int goods[16] = {0, 509, 838, 924, 650, 604, 793, 564, 651, 697, 649, 747, 787, 701, 605, 644};
#define MAX(x, y) ((x) > (y)? (x) : (y))
int main(int argc, char * argv[])
@lihnux
lihnux / swift-sec-trust.swift
Created July 2, 2015 09:48
Swift NSURLAuthenticationMethodServerTrust
//
// SwiftHttp.swift
//
// Created by Lihnux on 7/2/15.
//
import Foundation
class SwitfHttp: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {
@lihnux
lihnux / redirectNSLog.m
Created August 28, 2014 07:59
Redirecting NSLog to a file
- (BOOL)redirectNSLog {
// Create log file
[@"" writeToFile:@"/NSLog.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
id fileHandle = [NSFileHandle fileHandleForWritingAtPath:@"/NSLog.txt"];
if (!fileHandle) {
NSLog(@"Opening log failed");
return NO;
}
[fileHandle retain];
// Redirect stderr
@lihnux
lihnux / base64.js
Created August 11, 2014 09:43
Javascript Base64 encode and decode
// Create Base64 Object
var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r
@lihnux
lihnux / StringToByteArray.js
Created August 6, 2014 06:05
Javascript Convert String to Byte Array
var url = "Hello World";
var data = [];
for (var i = 0; i < url.length; i++){
data.push(url.charCodeAt(i));
}
@lihnux
lihnux / Base64.m
Last active August 29, 2015 14:04
Cooca Base64 Encode & Decode
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
static NSData *base64helper(NSData *input, SecTransformRef transform)
{
NSData *output = nil;
if (!transform)
return nil;
//Decodes Base64
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <stdint.h>
#include <assert.h>
size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded string
size_t len = strlen(b64input),
padding = 0;