1 | int ShowPIC(CDC *pDC, CString strPath, CRect rect, int ShowType) |
(原创)sem_wait实现毫秒级延时
1 | static int gettimeofday(struct timeval *tp, void *t) |
(原创)hexo next主题中使用文章计数计时功能
一定要先安装hexo-wordcount,不然就算配置了next主题,网页中显示的也是空的。
安装方法:
在hexo目录上右键选择”Git Bash Here”,执行1
npm install hexo-wordcount --save
在next目录下,找到_config.yml,找到post_wordcount,修改如下1
2
3
4
5
6
7
8# Post wordcount display settings
# Dependencies: https://github.com/willin/hexo-wordcount
post_wordcount:
item_text: true
wordcount: true
min2read: true
totalcount: true
separated_meta: true
到这边的话,已经初步实现了文章计数计时功能,剩下的就是汉化了,在主题目录下,打开languages\zh-Hans.yml,找打totalcount,修改成总字数或则你任意喜欢的文字,这边是统计博客所有文章字符数的
再打开layout_macro\post.swig,找到{ __(‘post.wordcount’) },修改如下1
2
3<span title="{{ __('post.wordcount') }}">
{{ wordcount(post.content) }}字
</span>
找到{ __(‘post.min2read’) },修改如下1
2
3<span title="{{ __('post.min2read') }}">
{{ min2read(post.content) }}分钟
</span>
大功告成!!!
(转载)error LNK2005 ...already defined in MSVCRTD
编译错误:
Error1error LNK2005: _CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(dbgheap.obj)
Error2error LNK2005: _calloc_dbg already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(dbgheap.obj)
Error3error LNK2005: _free_dbg already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(dbgheap.obj)
Error4error LNK2005: _malloc_dbg already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(dbgheap.obj)
Error5error LNK2005: _amsg_exit already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(crt0dat.obj)
Error6error LNK2005: _initterm_e already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(crt0dat.obj)
Error7error LNK2005: __CppXcptFilter already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(winxfltr.obj)
Error8error LNK2005: __crtCaptureCurrentContext already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(winapisupp.obj)
Error9error LNK2005: __crtCapturePreviousContext already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(winapisupp.obj)
Error10error LNK2005: __crtTerminateProcess already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(winapisupp.obj)
Error11error LNK2005: __crtUnhandledException already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(winapisupp.obj)
Error12error LNK2005: _lock already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(mlock.obj)
Error13error LNK2005: _unlock already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(mlock.obj)
Error14error LNK2005: __crt_debugger_hook already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(dbghook.obj)
Error15error LNK2005: “void __cdecl terminate(void)” (?terminate@@YAXXZ) already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(hooks.obj)
Error16error LNK2005: __xi_a already defined in MSVCRTD.lib(cinitexe.obj)\libcmtd.lib(crt0init.obj)
Error17error LNK2005: __xi_z already defined in MSVCRTD.lib(cinitexe.obj)\libcmtd.lib(crt0init.obj)
Error18error LNK2005: __xc_a already defined in MSVCRTD.lib(cinitexe.obj)\libcmtd.lib(crt0init.obj)
Error19error LNK2005: __xc_z already defined in MSVCRTD.lib(cinitexe.obj)\libcmtd.lib(crt0init.obj)
Error20error LNK2005: __iob_func already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(_file.obj)
Error21error LNK2005: _CrtDbgReportW already defined in MSVCRTD.lib(MSVCR110D.dll)\libcmtd.lib(dbgrptw.obj)
原因:静态库或动态库编译时的设置的Runtime Library和当前工程不一样
解决办法:工程Property -> C/C++ -> Code Generation -> Runtime Library设置为统一的,例如Debug模式下,统一为MTd,Release模式下统一为MT
(转载)socket:10038错误{winSock的一个bug:当closesocket多次错误使用时会导致问题}
这几天想在一个开源的代码上进行修改,以期研发出一个产品出来。
程序原来是单线程网络程序,需要修改为多线程,修改之后,总是出问题,辅助线程中的recv函数总是运行一阵子之后收到长度为-1的数据报,导致程序运行不正确甚至崩溃。
由于是多线程,只好打日志进行调试,发现一个奇怪的问题。在A线程与B线程中,均使用了socket这个函数来产生socket,竟然会产生两个相同返回值的socket!也就是说,A线程与B线程能同时获得socket值为360的socket,这样当A正在使用360进行数据接收时,B去连接一把,A自然就出错了。
这个问题实在是诡异,因为socket又不是COM组件,是可以直接在线程中共享的,究竟是出了什么问题呢,MSDN和网络上没有见到任何与此问题相关的内容。活脱脱地就是API出错了!按道理这是不可能的事情啊。
实在没辙了,只好review代码,发现程序中有很多这样的语句:
if( mSocket != INVALID_SOCKET )
{
closesocket( mSocket );
}
这句有什么问题没有?呵呵看起来没什么问题,但是,不容置疑的是,肯定会导致一个socket被关闭多次。本着死马当着活马医的原则,进行修改,每次closesocket之后,将socket的值置为INVALID_SOCKET,运行程序,问题解决。
这个问题也太ft了,windows竟然连这个容错都没有做,按道理这是很好做的,socket实际上只是一个索引值,系统内核在关闭时如果发现已经关闭就不要做操作就可以了。现在看起来,内核里面貌似是一个socket缓冲池,程序在使用的时候使用计数来管理生命周期。这样当一个socket被关闭多次后,再创建改socket之后,系统可能会认为该socket是关闭的(因为计数小于等于0)。所以下次分配的时候,会将该索引重新分配出去。
该问题在vista home版上出现,其他平台又没有问题不得而知,由此可以看出成对编码的好处以及面向对象封装的好处。
转自:http://blog.csdn.net/chen495810242/article/details/42029825
个人注释:在win7上的测试结果,当一个工程调用多个dll的时候,不同的dll之间使用socket这个函数来产生socket,返回值是可能一样的,就会发生连接无缘无故被断开的现象,查了两三天的说
(原创)解决ssh登录linux速度慢的问题(以centos7为例)
解决步骤:
先备份/etc/ssh/sshd_config,备份命令为
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
1.su (以root用户登录)
2.vi /etc/ssh/sshd_config (编辑配置文件)
3.输入 / ,查找GSSAPIAuthentication 赋值为no
4.输入 /,查找UseDNS,赋值为 no(该项默认不启用的,要把前面的#删除掉)
5.最后输入!wq保存文件并退出
6.重启systemctl restart sshd
(原创)随机串的产生,可以用于盐值
之前要加密密码的时候,要加入盐值以加强强度,找了部分,又在网上的基础上修改了下(参考网页:http://blog.csdn.net/wangchangshuai0010/article/details/17188417)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36//产生长度为length的随机字符串
char pSymbol[] = {'~','`','!','@','#','$','%','^','&',
'*','(',')','_','+','-','=','{','}','|','[',']',
':',';','<','>','?',
',','.','\"','\\','\'','/'};
char* getRandomString(int length)
{
int nRand, i;
char* string;
srand((unsigned) time(NULL ));
if ((string = (char*) malloc(length + 1)) == NULL )
return NULL ;
for (i = 0; i < length - 1; i++)
{
nRand = rand() % (sizeof(pSymbol) + 26 * 2 + 10);
if (nRand >= 0)
{
if (nRand < 26)
string[i] = 'A' + nRand;
else if (nRand < 52)
string[i] = 'a' + nRand - 26;
else if (nRand < 62)
string[i] = '1' + nRand - 52;
else if(nRand - 62 < sizeof(pSymbol))
string[i] = pSymbol[nRand - 62];
else
string[i] = 'x';
}
else
string[i] = 'x';
}
string[length] = '\0';
return string;
}
(原创)linux下Microsoftcpprestsdk支持https(server)
原创,转载请标明源地址
之前看网上一堆的资料说Microsoft/cpprestsdk不支持https或者说只支持window下的https,差点就被误导了,没办法,只好自己去翻了下源代码
先说明下linux版本把,centos7
创建ssl证书啥的,网上一堆的资料,这边就不再说了,直接贴源代码了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178#include <stdio.h>
#include <cpprest/uri.h>
#include <cpprest/http_listener.h>
#include <cpprest/http_client.h>
#include <cpprest/asyncrt_utils.h>
#include <cpprest/filestream.h>
#pragma comment(lib, "cpprest_2_7.lib")
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "winhttp.lib")
#pragma comment(lib, "httpapi.lib")
using namespace web;
using namespace http;
using namespace utility;
using namespace http::experimental::listener;
using namespace web::http;
using namespace web::http::client;
//using namespace concurrency;
class CommandHandler
{
public:
CommandHandler() {}
CommandHandler(utility::string_t url,http_listener_config server_config);
pplx::task<void> open() { return m_listener.open(); }
pplx::task<void> close() { return m_listener.close(); }
private:
void handle_get(http_request message);
void handle_post(http_request message);
void handle_put(http_request message);
void handle_del(http_request message);
http_listener m_listener;
};
CommandHandler::CommandHandler(utility::string_t url,http_listener_config server_config) : m_listener(url,server_config)
{
m_listener.support(methods::GET, std::bind(&CommandHandler::handle_get, this, std::placeholders::_1));
m_listener.support(methods::POST, std::bind(&CommandHandler::handle_post, this, std::placeholders::_1));
m_listener.support(methods::PUT, std::bind(&CommandHandler::handle_put, this, std::placeholders::_1));
m_listener.support(methods::DEL, std::bind(&CommandHandler::handle_del, this, std::placeholders::_1));
}
void CommandHandler::handle_get(http_request message)
{
ucout << "Method: " << message.method() << std::endl;
ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl;
ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << std::endl;
message.reply(status_codes::OK, "ACCEPTED");
}
}
void CommandHandler::handle_post(http_request message)
{
//std::unordered_map<std::string, std::unordered_map<std::string, std::vector<unsigned char>>> multipart_form_data = granada::httpex::parserex::ParseMultipartFormData(message);
ucout << "Method: " << message.method() << std::endl;
ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl;
ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << std::endl;
message.reply(status_codes::OK, "POST");
}
void CommandHandler::handle_put(http_request message)
{
ucout << "Method: " << message.method() << std::endl;
ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl;
ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << std::endl;
message.reply(status_codes::OK, "PUT");
}
void CommandHandler::handle_del(http_request message)
{
ucout << "Method: " << message.method() << std::endl;
ucout << "URI: " << http::uri::decode(message.relative_uri().path()) << std::endl;
ucout << "Query: " << http::uri::decode(message.relative_uri().query()) << std::endl << std::endl;
message.reply(status_codes::OK, "DEL");
}
int main(int argc, char *argv[])
{
try
{
//用C++ REST SDK编写HTTP服务端,需要在所有的网络接口上监听时,
//Windows下应使用地址"http://*:8080",
//Linux下应使用地址"http://0.0.0.0:8080"。
const char * self_signed_cert = R"(
-----BEGIN CERTIFICATE-----
MIIDlzCCAn+gAwIBAgIJAP9ZV+1X94UjMA0GCSqGSIb3DQEBBQUAMGIxCzAJBgNV
BAYTAkNOMQswCQYDVQQIDAJTSDELMAkGA1UEBwwCU0gxEjAQBgNVBAoMCU1JQ1JP
U09GVDERMA8GA1UECwwISFBDLVBBQ0sxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0x
NTA4MTkwOTA0MjhaFw00MzAxMDMwOTA0MjhaMGIxCzAJBgNVBAYTAkNOMQswCQYD
VQQIDAJTSDELMAkGA1UEBwwCU0gxEjAQBgNVBAoMCU1JQ1JPU09GVDERMA8GA1UE
CwwISFBDLVBBQ0sxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEB
BQADggEPADCCAQoCggEBALLv7AAPa+4wYpa+3tqc9HIHhh8kv/MpV2Dm+oKG27iH
zOugMNAPqLzMAaWCzDRyw27I+jPS3pzAAu6rQ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h79u1fmrd2SV085P1WAYRqE+Y4qMvUg/em
JVjmEeBnT+HI7fmdGpOvRyjxt92BDI5w8WVTU2lI1fqEHTpNZ9Te5WbWgfCpf9ax
H74VzCCtT74Bq7l1kFdp0IqOKpcpJu8VtETHcG5LtQKBgQC4Tx7El1Xb4hsI4dvE
h43j3KBb3evlz6vaqgz0BArahYAz2UkkOYDSOPs4K6aOxxXjO0BjqQqCx/tCPcU5
AvLsTlswO+wDLXM1DoKxzFBZL5o8927niqW+vZpzyGc1uPmC1MG7+MDKdZsR+e+9
XzJTD4slrGSJrcpLt/g/Jqqdjg==
-----END PRIVATE KEY-----
)";
boost::asio::const_buffer cert(self_signed_cert, std::strlen(self_signed_cert));
boost::asio::const_buffer key(private_key, std::strlen(private_key));
http_listener_config server_config;
server_config.set_ssl_context_callback(
[&](boost::asio::ssl::context& ctx)
{
ctx.set_options(boost::asio::ssl::context::default_workarounds);
ctx.use_certificate_chain(cert);
ctx.use_private_key(key, boost::asio::ssl::context::pem);
});
int nPort = 8080;
char pAddr[32];
memset(pAddr,0,sizeof(pAddr));
sprintf(pAddr,"https://0.0.0.0:%d",nPort);
utility::string_t address = pAddr;//U("http://0.0.0.0:8080");
uri_builder uri(address);
auto addr = uri.to_uri().to_string();
CommandHandler handler(addr,server_config);
handler.open().wait();
ucout << utility::string_t(U("Listening for requests at: ")) << addr << std::endl;
ucout << U("Press ENTER key to quit...") << std::endl;
std::string line;
std::getline(std::cin, line);
handler.close().wait();
}
catch (std::exception& ex)
{
ucout << U("Exception: ") << ex.what() << std::endl;
ucout << U("Press ENTER key to quit...") << std::endl;
std::string line;
std::getline(std::cin, line);
}
return 0;
}
下面是访问结果
(原创)foef注意事项
原创内容,转载请标明原文地址
我们先来看下下面的代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15//获取文件大小
DWORD Get_File_Size(const char* m_FilePath)
{
unsigned long size;
if (_access(m_FilePath, 0) == -1)//文件不存在 2013.09.24
return 0;
FILE* fp = fopen( m_FilePath, "rb" );
if(fp==NULL)
return 0;
fseek( fp, SEEK_SET, SEEK_END );
size=ftell(fp);
fclose(fp);
return size;
}
1 | BYTE bReadBuf[1024*100]; |
其中pub.txt的文件属性为
弹出的内容分别为64,1,0,2
获取出来的文件大小是正确的,但是如果按照这个大小去读取的话,是会发现,feof返回的是true,但是继续去读取的话,又读取不出来数据,但是这个时候再次判断feof则返回false了,
原因在于,feof只有只有在读取超过的情况下,才会返回false
(转载)SHA-256算法 C语言实现
1.如果出现 _ftelli64未定义 的错误,将_ftelli64替换为ftello64。visual c应该不会有这种错误。
2.函数的第三个参数是计算后的字符串形式,不需要再转换,详情看示例代码。
3.默认计算结果是大写形式,如需小写,将函数中末尾处所有 %08X 替换为 %08x 即可。1
2
3
4
5
6
7
8
9
10
11
12
13
14/*用法示例*/
#include <stdio.h>
#include <stdlib.h>
extern char* StrSHA256(const char* str, long long length, char* sha256);
int main(void){
char text[] = "blackkitty";
char sha256[65];
StrSHA256(text,sizeof(text)-1,sha256); // sizeof()计算的结果包含了末尾的'\0'应减1
puts(sha256);
puts(StrSHA256(text,sizeof(text)-1,sha256)); // 函数返回值即sha256,直接输出也可以
return 0;
}
使用在线加密验证
http://tool.oschina.net/encrypt?type=2
代码如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121#include <stdio.h>
#include <stdlib.h>
#define SHA256_ROTL(a,b) (((a>>(32-b))&(0x7fffffff>>(31-b)))|(a<<b))
#define SHA256_SR(a,b) ((a>>b)&(0x7fffffff>>(b-1)))
#define SHA256_Ch(x,y,z) ((x&y)^((~x)&z))
#define SHA256_Maj(x,y,z) ((x&y)^(x&z)^(y&z))
#define SHA256_E0(x) (SHA256_ROTL(x,30)^SHA256_ROTL(x,19)^SHA256_ROTL(x,10))
#define SHA256_E1(x) (SHA256_ROTL(x,26)^SHA256_ROTL(x,21)^SHA256_ROTL(x,7))
#define SHA256_O0(x) (SHA256_ROTL(x,25)^SHA256_ROTL(x,14)^SHA256_SR(x,3))
#define SHA256_O1(x) (SHA256_ROTL(x,15)^SHA256_ROTL(x,13)^SHA256_SR(x,10))
extern char* StrSHA256(const char* str, long long length, char* sha256){
/*
计算字符串SHA-256
参数说明:
str 字符串指针
length 字符串长度
sha256 用于保存SHA-256的字符串指针
返回值为参数sha256
*/
char *pp, *ppend;
long l, i, W[64], T1, T2, A, B, C, D, E, F, G, H, H0, H1, H2, H3, H4, H5, H6, H7;
H0 = 0x6a09e667, H1 = 0xbb67ae85, H2 = 0x3c6ef372, H3 = 0xa54ff53a;
H4 = 0x510e527f, H5 = 0x9b05688c, H6 = 0x1f83d9ab, H7 = 0x5be0cd19;
long K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
};
l = length + ((length % 64 >= 56) ? (128 - length % 64) : (64 - length % 64));
if (!(pp = (char*)malloc((unsigned long)l))) return 0;
for (i = 0; i < length; pp[i + 3 - 2 * (i % 4)] = str[i], i++);
for (pp[i + 3 - 2 * (i % 4)] = 128, i++; i < l; pp[i + 3 - 2 * (i % 4)] = 0, i++);
*((long*)(pp + l - 4)) = length << 3;
*((long*)(pp + l - 8)) = length >> 29;
for (ppend = pp + l; pp < ppend; pp += 64){
for (i = 0; i < 16; W[i] = ((long*)pp)[i], i++);
for (i = 16; i < 64; W[i] = (SHA256_O1(W[i - 2]) + W[i - 7] + SHA256_O0(W[i - 15]) + W[i - 16]), i++);
A = H0, B = H1, C = H2, D = H3, E = H4, F = H5, G = H6, H = H7;
for (i = 0; i < 64; i++){
T1 = H + SHA256_E1(E) + SHA256_Ch(E, F, G) + K[i] + W[i];
T2 = SHA256_E0(A) + SHA256_Maj(A, B, C);
H = G, G = F, F = E, E = D + T1, D = C, C = B, B = A, A = T1 + T2;
}
H0 += A, H1 += B, H2 += C, H3 += D, H4 += E, H5 += F, H6 += G, H7 += H;
}
free(pp - l);
sprintf(sha256, "%08X%08X%08X%08X%08X%08X%08X%08X", H0, H1, H2, H3, H4, H5, H6, H7);
return sha256;
}
extern char* FileSHA256(const char* file, char* sha256){
/*
计算文件SHA-256
参数说明:
file 文件路径字符串指针
sha256 用于保存SHA-256的字符串指针
返回值为参数sha256
*/
FILE* fh;
char* addlp, T[64];
long addlsize, j, W[64], T1, T2, A, B, C, D, E, F, G, H, H0, H1, H2, H3, H4, H5, H6, H7;
long long length, i, cpys;
void *pp, *ppend;
H0 = 0x6a09e667, H1 = 0xbb67ae85, H2 = 0x3c6ef372, H3 = 0xa54ff53a;
H4 = 0x510e527f, H5 = 0x9b05688c, H6 = 0x1f83d9ab, H7 = 0x5be0cd19;
long K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
};
fh = fopen(file, "rb");
fseek(fh, 0, SEEK_END);
length = _ftelli64(fh);
addlsize = (56 - length % 64 > 0) ? (64) : (128);
if (!(addlp = (char*)malloc(addlsize))) return 0;
cpys = ((length - (56 - length % 64)) > 0) ? (length - length % 64) : (0);
j = (long)(length - cpys);
if (!(pp = (char*)malloc(j))) return 0;
fseek(fh, -j, SEEK_END);
fread(pp, 1, j, fh);
for (i = 0; i < j; addlp[i + 3 - 2 * (i % 4)] = ((char*)pp)[i], i++);
free(pp);
for (addlp[i + 3 - 2 * (i % 4)] = 128, i++; i < addlsize; addlp[i + 3 - 2 * (i % 4)] = 0, i++);
*((long*)(addlp + addlsize - 4)) = length << 3;
*((long*)(addlp + addlsize - 8)) = length >> 29;
for (rewind(fh); 64 == fread(W, 1, 64, fh);){
for (i = 0; i < 64; T[i + 3 - 2 * (i % 4)] = ((char*)W)[i], i++);
for (i = 0; i < 16; W[i] = ((long*)T)[i], i++);
for (i = 16; i < 64; W[i] = (SHA256_O1(W[i - 2]) + W[i - 7] + SHA256_O0(W[i - 15]) + W[i - 16]), i++);
A = H0, B = H1, C = H2, D = H3, E = H4, F = H5, G = H6, H = H7;
for (i = 0; i < 64; i++){
T1 = H + SHA256_E1(E) + SHA256_Ch(E, F, G) + K[i] + W[i];
T2 = SHA256_E0(A) + SHA256_Maj(A, B, C);
H = G, G = F, F = E, E = D + T1, D = C, C = B, B = A, A = T1 + T2;
}
H0 += A, H1 += B, H2 += C, H3 += D, H4 += E, H5 += F, H6 += G, H7 += H;
}
for (pp = addlp, ppend = addlp + addlsize; pp < ppend; pp = (long*)pp + 16){
for (i = 0; i < 16; W[i] = ((long*)pp)[i], i++);
for (i = 16; i < 64; W[i] = (SHA256_O1(W[i - 2]) + W[i - 7] + SHA256_O0(W[i - 15]) + W[i - 16]), i++);
A = H0, B = H1, C = H2, D = H3, E = H4, F = H5, G = H6, H = H7;
for (i = 0; i < 64; i++){
T1 = H + SHA256_E1(E) + SHA256_Ch(E, F, G) + K[i] + W[i];
T2 = SHA256_E0(A) + SHA256_Maj(A, B, C);
H = G, G = F, F = E, E = D + T1, D = C, C = B, B = A, A = T1 + T2;
}
H0 += A, H1 += B, H2 += C, H3 += D, H4 += E, H5 += F, H6 += G, H7 += H;
}
free(addlp); fclose(fh);
sprintf(sha256, "%08X%08X%08X%08X%08X%08X%08X%08X", H0, H1, H2, H3, H4, H5, H6, H7);
return sha256;
}