忙忙碌碌,更新不及时诶。
C/C++代码

plist转json解析类plist2json(libplist C++封装类)


苹果配置中大量用到了plist,使用开源的C语言的库libplist可以读取解析,但是纯C的写起来非常蛋疼。


于是用C++封装了一下(c++ wrapper),但是plist的本质还是xml,读取起来还是略有繁琐,于是转为json结构,就可以直接使用了(依赖jsoncpp库)。


plist内部是有PLIST_UID、PLIST_DICT、PLIST_ARRAY等复杂的结构,支持xml和binary(bplist)两种格式,支持uid自动解析处理关联,支持NS.objects、NS.keys、NSDictionary、NSArray、NSMutableDictionary、NSMutableArray等结构自动处理,使用起来就非常方便了。

More...

blackfeather 2023/12/27 0评论

C/C++代码

几种地图经纬度坐标系转换


wgs84:GPS使用的坐标系

gcj02:中国国家测绘局制订的地理信息系统的坐标系统,是在WGS84经纬度的基础上执行加密算法而成。因为GPS得到的经纬度直接在 GCJ-02 坐标系下会定位到错误的地点,有种到了火星的感觉,因此在坊间也将 GCJ-02 戏称为火星坐标系。高德地图、腾讯地图均使用的此坐标系。

bd09:百度地图使用的,在gcj02基础上又做了一次转换。


地图大陆/港/澳台湾省海外

高德

More...

blackfeather 2022/9/8 0评论

C/C++代码

性能监控之PDH系列方法C++封装


很多应用需要监控系统资源的使用率等信息,之前零散写过很多。

近日需要读取硬盘的IO使用率,就是任务管理器中的硬盘相关信息。


读写速度很好搞定,但是这个百分比的使用率(活动时间)恶心了,最后搜索到的技术点都指向了Pdh(performance data helper)库。

More...

blackfeather 2021/6/25 0评论

C/C++代码

mongoose魔改历程(C++封装http和ws服务、多线程、优化)-2021.07.23更新


背景需求

每个工作背景都不同,需要总结一下:

1.C++编写,跨平台(windows上用的vs2013,所以是C++11标准)

2.支持http和ws的server,支持多线程,支持uri映射(类似addhandler("/hello",onrequestcallback);),支持请求中相关字段的获取(get/post参数读取、header、cookie等读取)

3.不用性能超强,并发数也不大(几十并发)

More...

blackfeather 2020/10/30 8评论

C/C++代码

C++11标准stl库的timer


timer定时器,多线程,回调为阻塞模式。支持参数绑定,非常好使。


用法:

//////////////////////////////////////////////////////////////////////////
//以下是测试代码
namespace timertester
{

	class timertestclass
	{
	public:
		timertestclass(){};
		~timertestclass(){};
		void testfn()
		{
			printf("timer callback is class func.\n");
			FTimerEvent mTimer(3000, &timertestclass::threadfn, this);

			for (int i = 0; i < 5; i++)
			{
				printf("press any key to ...\n");
				getchar();
				mTimer.SetTimerEvent();
			}

			printf("stop timer \n");
			mTimer.StopTimer();
		}
		void threadf(int i)
		{
			printf("test f i:%d\n", i);
		}
		void threadfn()
		{
			printf("test fn d\n");
		}
		void threadfn2()
		{
			printf("test fn2222 \n");
		}
	};


	static void testfn1(timertestclass *cls)
	{
		cls->threadfn();
	}
	static void testpf()
	{
		printf("test printf \n");
		printf("callback ret. \n");
	}
	static void prt(int& i)
	{
		printf("print %d\n", i);
	}
	static void timertest()
	{
		int t = 0;
		FTimerEvent timerstdref(1000, prt, std::ref(t));
		for (int i = 0; i < 10; i++)
		{
			t = i;
			Sleep(1000);
		}
		timerstdref.StopTimer();

		{
			printf("timer 0 manual to set\n");
			//如果定时为0,则每次为手动触发
			FTimerEvent timerman(0, [](){
				printf("timerman in.\n");
				Sleep(5000);
				printf("timerman out.\n");
			});
			timerman.SetTimerEvent();
			timerman.SetTimerEvent();
			timerman.StopTimer();
		}

		printf("stop timer in callback\n");
		FTimerEvent timer4;
		timer4.InitTimer(1000, [](FTimerEvent *pTimer){ printf("exit timer\n"); pTimer->StopTimer(); }, &timer4);
		Sleep(3000);
		timer4.StopTimer();

		printf("set timer in callback\n");
		FTimerEvent timer5;
		timer5.InitTimer(2000, [](FTimerEvent *pTimer){
			static bool set = false;
			printf("timer in\n");
			if (!set)
			{
				printf("set timer\n");
				pTimer->SetTimerEvent();
				set = true;
			}
			printf("timer out\n");
		}, &timer5);
		Sleep(10000);
		timer5.StopTimer();

		timertestclass test1;
		test1.testfn();

		int x = 0;
		FTimerEvent timerref(1000, [&x]()
		{
			printf("x: %d \n", x);
		});
		for (int i = 0; i < 10; i++)
		{
			x = i;
			Sleep(1000);
		}
		timerref.StopTimer();

		FTimerEvent timerx(1000, [&test1]()
		{
			test1.threadfn2();
		});
		Sleep(10000);
		timerx.StopTimer();

		FTimerEvent timer0(1000, testpf);
		Sleep(10000);
		timer0.StopTimer();

		FTimerEvent timer1(1000, testfn1, &test1);
		Sleep(10000);
		timer1.StopTimer();

		FTimerEvent timer2(1000, [](){ printf("lambada no param \n"); });
		Sleep(10000);
		timer2.StopTimer();

		int param = 0;
		FTimerEvent timer3(1000, [](int *p){ printf("lambada with param: %d \n", *p); }, &param);
		Sleep(10000);
		timer3.StopTimer();

		
	}
}


More...

blackfeather 2020/9/7 0评论

C/C++代码

大坑的aes GCM解密算法

基于openssl库写的aes_gcm解密方法

兼容gcm 128、192、256位,兼容变长iv算法,与python和java的gcmdecrypt方法结果保持一致。

最恶心的地方在iv长度,要手动设置,否则某些情况下会与python或者java的结果不一致(代码的24行)。


std::string AesGCMDecrypt(const std::string &strKey, const std::string &strIV, const std::string &strBuffer, int nPadding/* = 1*/)
{
	std::string strRet;
	if (strBuffer.empty())
		return strRet;

	const EVP_CIPHER *cipher;
	if (strKey.length() == 16)
		cipher = EVP_aes_128_gcm();
	else if (strKey.length() == 24)
		cipher = EVP_aes_192_gcm();
	else if (strKey.length() == 32)
		cipher = EVP_aes_256_gcm();
	else
		return strRet;

	unsigned char *pszDecode = new unsigned char[strBuffer.length() + 32];
	ZeroMemory(pszDecode, strBuffer.length() + 32);
	unsigned char *pszOut = pszDecode;

	int nDecodeLen, nTotalLen = 0;
	EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
	EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL);
	if (strIV.length() != 12)
		EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, strIV.length(), NULL);
	EVP_DecryptInit_ex(ctx, NULL, NULL, (const unsigned char *)strKey.c_str(), (const unsigned char *)strIV.c_str());
	EVP_CIPHER_CTX_set_padding(ctx, nPadding);
	EVP_DecryptUpdate(ctx, pszOut, &nDecodeLen, (const unsigned char *)strBuffer.c_str(), strBuffer.length());
	nTotalLen = nDecodeLen;
	pszOut += nDecodeLen;

	EVP_DecryptFinal(ctx, pszOut, &nDecodeLen);
	nTotalLen += nDecodeLen;

	strRet.assign((const char *)pszDecode, nTotalLen);
	delete[] pszDecode;

	EVP_CIPHER_CTX_free(ctx);

	return strRet;
}


blackfeather 2020/9/1 1评论

C/C++代码

C++的defer

在C/C++编码中,open对close,new对delete,malloc对free等等是严格要求的,否则会有各种泄露的奇葩情况。

但是实际编码中,各种异常捕捉和错误判断又让编码变得繁琐臃肿。

后来在使用golang中,defer方法使用非常方便,C++虽然没有自带的,但是可以写嘛!

借助C++类的析构方法和lambada表达式,还有std::function容器,很容易写出来一个自己的defer(以下代码是复制来的,感觉非常的干练,自己写的不好看不贴了,后来也用了这个.h)

More...

blackfeather 2020/9/1 0评论

C/C++代码

AESUnwrap纯C函数


有缘人拿去。。。RFC 3394,AESUnwrap。。。某些情况下使用的。


uint8_t AES_unwrap(uint8_t *kek, uint16_t key_len, uint8_t *cipher_text, uint16_t cipher_len, uint8_t *output)
{
    uint8_t a[8], b[16];
    uint8_t *r;
    uint8_t *c;
    uint16_t i, j, n;
    AES_KEY ctx;

    if (! kek || cipher_len < 16 || ! cipher_text || ! output)
    {
        /* We don't do anything with the return value */
        return 1;
    }

    /* Initialize variables */

    n = (cipher_len/8)-1;  /* the algorithm works on 64-bits at a time */
    memcpy(a, cipher_text, 8);
    r = output;
    c = cipher_text;
    memcpy(r, c+8, cipher_len - 8);

    /* Compute intermediate values */
    for (j=5; j >= 0; --j)
    {
        r = output + (n - 1) * 8;
        /* DEBUG_DUMP("r1", (r-8), 8); */
        /* DEBUG_DUMP("r2", r, 8); */
        for (i = n; i >= 1; --i)
        {
            uint16_t t = (n*j) + i;
            /* DEBUG_DUMP("a", a, 8); */
            memcpy(b, a, 8);
            b[7] ^= t;
            /* DEBUG_DUMP("a plus t", b, 8); */
            memcpy(b+8, r, 8);
            AES_set_decrypt_key(kek, 128, &ctx);
            AES_decrypt(b, b, &ctx);  /* NOTE: we are using the same src and dst buffer. It's ok. */
            /* DEBUG_DUMP("aes decrypt", b, 16) */
            memcpy(a,b,8);
            memcpy(r, b+8, 8);
            r -= 8;
        }
    }

    /* DEBUG_DUMP("a", a, 8); */
    /* DEBUG_DUMP("output", output, cipher_len - 8); */

    return 0;
}


blackfeather 2019/6/25 0评论