/*!
@file signatuan.cpp
@auther d金魚
@note
This source code licence is Boost Licence 1.0
(See at http://www.boost.org/LICENSE_1_0.txt)
@version 0.002
@todo
もうこのソースは駄目だ。
scoped_file_bufferは超でかいファイルを読み込むことが出来ない。(100MBクラスのファイルとか)
メモリ上に展開なんてやってられねぇぜ!
でも、ちっこいファイルだったら使えるみたいね。
*/
#define DKUTIL_MSVC_IMPORT_LIB
#include <dkutil/include/dkutilAllInclude.h>
#include <dkutil/dkutilUtility.h>
using namespace dkutil;
///ファイルが見つからない時用printf
void not_found_file_printf(const char *filename){
printf("%sは見つかりません。\n",filename);
}
///ハッシュ値をprintf
void hash_printf(const char *hash_name,const unsigned char *str){
printf("%s:%s\n",hash_name,str);
}
///CRCをpritnf
void crc_printf(const char *crc_name,unsigned long crc){
printf("%s:%08lX\n",crc_name,crc);
}
///dkutilに実装されているハッシュ関数をすべて通す。
void hash_print(const char *filename,int flag){
SHA1_Adapter sha1;
SHA256_Adapter sha256;
SHA512_Adapter sha512;
MD5_Adapter md5;
//テーブルを作る。
Hash_Adapter_Interface *table[]={
&md5,&sha1,&sha256,&sha512,NULL//番兵としてNULLを最後に入れておく
};
char *title[]={
"MD5","SHA1","SHA256","SHA512",NULL
};
///ヘナチョコユーティリティ
dkutilUtility ope;
///tableが終端になるまでループ
for(int i=0;table[i] != NULL;i++)
{
BYTE hashvalue[1024]="";
if(false==ope.FileToHashValue(table[i],filename,hashvalue,sizeof(hashvalue)))
{
not_found_file_printf(filename);
break;
}
switch(flag){
case 1:
dMB(title[i],"%s:\n%s:%s",filename,title[i],hashvalue);
break;
case 0:
default:
hash_printf(title[i],hashvalue);
}
}
}
///dkutilに実装されているすべてのcrcを通す。
void crc_print(const char *filename){
///ファイルをメモリ上に展開するバッファ
scoped_file_buffer<BYTE> buff(filename);
if(false==buff.isValid()){
not_found_file_printf(filename);
return;
}
using namespace dkutil::policy;
CRC_Adapter<policy::crc32_left> cl32;
CRC_Adapter<policy::crc32_right> cr32;
CRC_Adapter<policy::crc16_left> cl16;
CRC_Adapter<policy::crc16_right> cr16;
CRC_Adapter_Interface *table[]={
&cl32,&cr32,&cl16,&cr16,NULL
};
char *title[]={
"crc32_left","crc32_right","crc16_left","crc16_right",NULL
};
/*{
scoped_file_buffer<BYTE> buf(filename);
DKUTIL_NOT_ASSERT(false==buf.isValid());
crc1 ccc;
printf("crc1() = %08lX\n", ccc.crc(buf.size(),buf.get()));
}*/
///tableが終端になるまでループ
for(int i=0;table[i] != NULL;i++){
UINT crc = 0;
bool r = table[i]->FileToSignature(filename,&crc);
if(false==r)
{//見つからないとき〜〜
not_found_file_printf(filename);
return;
}
crc_printf(title[i],crc);
}
}
int main(int argc,char *argv[])
{
int i,result = -1;
if(1 >= argc)
{//なんかファイルが指定されていないみたいですねぇ。
for(i=0;i<1;i++)
{
printf("%s",argv[i]);
}
printf("ファイル名を実行ファイル名の後に半角スペースを空けて入れてください。\n");
printf("ファイル名に半角スペースが混じっている場合は\" \"(ダブルクォーテーション)でくくってください。\n");
goto End;
}
for(i=1;i<argc;i++)
{//よっしゃ!ハッシュとCRCをしらべたろかい!!ってね^^
/*if(0==i){
continue;
}*/
size_t len = strlen(argv[i]);
if(FALSE==dkcIsNativePathString(argv[i],len))
{//uneffective
printf("OPERATION:%s\n",argv[i]);
file_listupper up;
if(false==up.reset(argv[i],SafeGetCurrentDirectory().c_str(),false))
{
printf("Error:%s",argv[i]);
continue;
}
if(false==up.find()){
printf("Error:%s",argv[i]);
continue;
}
file_listupper::iterator it = up.begin();
for(;it != up.end();it++)
{
printf("FILENAME:%s\n",(*it).c_str());
hash_print((*it).c_str(),0);
crc_print((*it).c_str());
printf("//**********************************************************\n");
}
}else{//effective
printf("FILENAME:%s\n",argv[i]);
hash_print(argv[i],0);
crc_print(argv[i]);
printf("//**********************************************************\n");
}
}
//見事にエラー無しで終了!!
result = 0;
End:
#ifdef DEBUG
Sleep(5000);
#endif
return result;
}