[PR]

『新妻LOVELY×CATION』を応援しています!
水無瀬の部屋 > Programming > sample > exe2ico > exe2riff.cpp
最終更新日: 2007/03/24

   1: //*********************************************************
   2: // プロジェクト: exe2ico
   3: //  ファイル名: exe2riff.cpp
   4: //*********************************************************
   5: #include "exe2riff.h"
   6: #include "cmdline.h"
   7: #include "util.h"
   8: 
   9: 
  10: //---------------------------------------------------------
  11: // マクロ の 定義
  12: //---------------------------------------------------------
  13: #define DWORDALIGN(n) (((n) + 3) & (~3))
  14: #define EXT_AVI    ".avi"
  15: #define EXT_WAV    ".wav"
  16: #define EXT_ANI    ".ani"
  17: #define TAG_RIFF   "RIFF"
  18: #define TAG_AVI    "AVI "
  19: #define TAG_WAV    "WAVE"
  20: #define TAG_ANI    "ACON"
  21: #define TYPE_AVI   ( 1 )
  22: #define TYPE_WAV   ( 2 )
  23: #define TYPE_ANI   ( 3 )
  24: 
  25: 
  26: //---------------------------------------------------------
  27: // ファイルスコープ変数 の 定義
  28: //---------------------------------------------------------
  29: static const struct { char *tag; int type; char *ext; DWORD flag; } g_pair[] =
  30: 	{
  31: 		{ TAG_AVI, TYPE_AVI, EXT_AVI, FLAG_NOAVI },
  32: 		{ TAG_WAV, TYPE_WAV, EXT_WAV, FLAG_NOWAV },
  33: 		{ TAG_ANI, TYPE_ANI, EXT_ANI, FLAG_NOANI },
  34: 	};
  35: 
  36: 
  37: //---------------------------------------------------------
  38: // ファイルスコープ関数 の 宣言
  39: //---------------------------------------------------------
  40: static bool FindRiff( FILE *fr, int *read_byte );
  41: static void *LoadRiff( FILE *fr, int *size, int *type, int max_size );
  42: static bool IsValidRiffType( int rifftype );
  43: 
  44: 
  45: //*********************************************************
  46: // exe2riff
  47: //*********************************************************
  48: bool 
  49: exe2riff
  50: 	(
  51: 		const char  *filename,
  52: 		      DWORD  flags
  53: 	)
  54: {
  55: 	// パラメタの仮定
  56: 	ASSERT( IsPathExist( filename ) );
  57: 
  58: 	const DWORD filesize = fsize( filename, null );
  59: 	FILE *fr = fopen( filename, "rb" );
  60: 	if ( !fr )
  61: 		return false;
  62: 
  63: 	int read_byte = 0;
  64: 	int count[numof(g_pair)];
  65: 	memzero( count, sizeof(count) );
  66: 	while( FindRiff( fr, &read_byte ) )
  67: 	{
  68: 		const long pos = ftell( fr );
  69: 		
  70: 		int type, size; 
  71: 		void *riff = LoadRiff( fr, &size, &type, filesize-read_byte );
  72: 		if ( riff )
  73: 		{
  74: 			for( int i = 0; i < numof(g_pair); i++ )
  75: 			{
  76: 				if ( (type == g_pair[i].type) && (0 == (flags & g_pair[i].flag)) )
  77: 				{
  78: 					if ( OutputResourceFile( riff, size, filename, g_pair[i].ext, count[i] ) )
  79: 						count[i]++;
  80: 				}
  81: 			}
  82: 			free( riff );
  83: 		}
  84: 
  85: 		VERIFY( 0 == fseek( fr, pos, SEEK_SET ) );
  86: 	}
  87: 	VERIFY( 0 == fclose( fr ) );
  88: 	return true;
  89: }//exe2riff
  90: 
  91: 
  92: //******************************************************************************************************************
  93: // private
  94: //******************************************************************************************************************
  95: static int GetRiffType( const BYTE *type );
  96: 
  97: 
  98: //*********************************************************
  99: // FindRiff
 100: //*********************************************************
 101: static
 102: bool
 103: FindRiff
 104: 	(
 105: 		FILE *fr,
 106: 		int *read_byte
 107: 	)
 108: {
 109: 	// パラメタの仮定
 110: 	ASSERT( IsValidFilePtr( fr ) );
 111: 
 112: 	int c;
 113: 	int iMatch = 0;
 114: 	while( EOF != (c = fgetc(fr)) )
 115: 	{
 116: 		//
 117: 		(*read_byte)++;
 118: 
 119: 		//
 120: 		if ( c == TAG_RIFF[iMatch] )
 121: 		{
 122: 			if ( 4 <= ++iMatch )
 123: 			{
 124: 				return true;
 125: 			}
 126: 		}
 127: 		else
 128: 		{
 129: 			iMatch = 0;
 130: 			if ( c == TAG_RIFF[iMatch] )
 131: 				iMatch++;
 132: 		}
 133: 	}
 134: 	return false;
 135: }//FindRiff
 136: 
 137: //*********************************************************
 138: // LoadRiff
 139: //*********************************************************
 140: static
 141: void *
 142: LoadRiff
 143: 	(
 144: 		FILE *fr,
 145: 		int *size,
 146: 		int *type,
 147: 		int max_size
 148: 	)
 149: {
 150: 	// パラメタの仮定
 151: 	ASSERT( (0 < max_size) && (max_size < INT_MAX) );
 152: 	ASSERT( IsValidFilePtr( fr ) );
 153: 	ASSERT( IsValidPtr( size, sizeof(int) ) );
 154: 	ASSERT( IsValidPtr( type, sizeof(int) ) );
 155: 
 156: 	//
 157: 	*size = 0;
 158: 	*type = -1;
 159: 
 160: 	//
 161: 	BYTE buffer[8];
 162: 	if ( 8 != fread( buffer, 1, 8, fr ) )
 163: 		return null;
 164: 
 165: 	//
 166: 	int datasize = ( buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24) );
 167: 	datasize = DWORDALIGN( datasize );
 168: 	if ( (datasize <= 0) || (max_size < datasize) || (INT_MAX <= datasize) )
 169: 		return null;
 170: 
 171: 	//
 172: 	const int rifftype = GetRiffType( 4 + buffer );
 173: 	if ( !IsValidRiffType( rifftype ) )
 174: 		return null; 
 175: 
 176: 	//
 177: 	const int filesize = /*4 + */8 + datasize;
 178: 	BYTE *data = (BYTE *)malloc( filesize );
 179: 	if ( !data ) return null;
 180: 	memzero( data, filesize );
 181: 
 182: 	//
 183: 	memmove(     data,     "RIFF", 4 ); //
 184: 	memmove( 4 + data,     buffer, 4 ); // size
 185: 	memmove( 8 + data, 4 + buffer, 4 ); // TYPE
 186: //	if ( datasize != (int)fread( 12 + data, 1, datasize, fr ) ) // 計算が微妙
 187: 	if ( datasize-8 >  (int)fread( 12 + data, 1, datasize-4, fr ) ) // 計算が微妙
 188: //	if ( datasize-8 != (int)fread( 12 + data, 1, datasize-8, fr ) ) // 計算が微妙
 189: 	{
 190: 		free( data );
 191: 		return null;
 192: 	}
 193: 	
 194: 	//
 195: 	*type = rifftype;
 196: 	*size = filesize;
 197: 	return data;
 198: }//LoadRiff
 199: 
 200: //*********************************************************
 201: // GetRiffType
 202: //*********************************************************
 203: static
 204: int
 205: GetRiffType
 206: 	(
 207: 		const BYTE *tag
 208: 	)
 209: {
 210: 	// パラメタの仮定
 211: 	ASSERT( IsValidReadPtr( tag, 4 ) );
 212: 
 213: 	{for( int i = 0; i < numof(g_pair); i++ )
 214: 	{
 215: 		if ( 0 == memcmp(tag, g_pair[i].tag, 4) )
 216: 			return g_pair[i].type;
 217: 	}}
 218: 
 219: 	return -1;
 220: }//GetRiffType
 221: 
 222: //*********************************************************
 223: // IsValidRiffType
 224: //*********************************************************
 225: static
 226: bool
 227: IsValidRiffType
 228: 	(
 229: 		int rifftype
 230: 	)
 231: {
 232: 	{for( int i = 0; i < numof(g_pair); i++ )
 233: 	{
 234: 		if ( rifftype == g_pair[i].type )
 235: 			return true;
 236: 	}}
 237: 
 238: 	return false;
 239: }//IsValidRiffType
 240: 
 241: 
 242: //** end **

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > exe2ico > exe2riff.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/exe2ico/exe2riff_cpp.shtml
>> Amazon.co.jp 『たまゆら童子』 へ
>> 楽天ブックス 『たまゆら童子』 へ