コード例 #1
0
ファイル: ArkFile.cpp プロジェクト: mtolly/onyxite-customs
// gets the size of the file without changing the current file position
// 
// args:	handle of file to get size of
// returns:	size of file if successful
//			< 0 if error
s64 ArkFile::GetFilesize(FILE* fd) const
{
	s64 curr_offset = FTELL64(fd);
	if( FSEEK64(fd, 0, SEEK_END) ) return -1;
	s64 size = FTELL64(fd);
	if( FSEEK64(fd, curr_offset, SEEK_SET) ) return -1;
	return size;
}
コード例 #2
0
ファイル: fileUtil.c プロジェクト: glshort/MapReady
long long fileSize(const char *name)
{
  FILE *fp = FOPEN(name,"rb");
  FSEEK64(fp,0,SEEK_END);
  long long file_size = FTELL64(fp);
  fclose(fp);
  return file_size;
}
コード例 #3
0
ファイル: ArkHdrPair.cpp プロジェクト: mtolly/onyxite-customs
s64 ArkHdrPair::GetFilesize(const char* filename) const
{
	FILE* fd = fopen(filename, "rb");
	if(fd == NULL)
		return -1;
	if( FSEEK64(fd, 0, SEEK_END) ) { fclose(fd); return -1; }
	s64 size = FTELL64(fd);
	fclose(fd);
	return size;
}
コード例 #4
0
ファイル: bacon_io.cpp プロジェクト: JeppeSRC/Bacon
	void bcLoadRawFile64(const char* filename, unsigned char** data, unsigned __int64* size) {
		FILE* file;
		fopen_s(&file, filename, "rb");

		if (file) {

			FSEEK64(file, 0, SEEK_END);
			*size = FTELL64(file);
			FSEEK64(file, 0, SEEK_SET);

			*data = new unsigned char[(unsigned int)*size];
			FREAD(*data, (unsigned int)*size, file);

			fclose(file);
		} else {
			LOG("Failed To Open File: %s\n", filename);
		}
	}
コード例 #5
0
ファイル: bacon_io.cpp プロジェクト: JeppeSRC/Bacon
	unsigned char* bcGetRawFile64(const char* filename) {
		FILE* file;
		fopen_s(&file, filename, "rb");
		unsigned __int64 size = 0;
		unsigned char* data;
		if (file) {

			FSEEK64(file, 0, SEEK_END);
			size = FTELL64(file);
			FSEEK64(file, 0, SEEK_SET);

			data = new unsigned char[(unsigned int)size];
			FREAD(data, (unsigned int)size, file);

			fclose(file);
		} else {
			LOG("Failed To Open File: %s\n", filename);
		}

		return data;
	}
コード例 #6
0
ファイル: ArkFile.cpp プロジェクト: mtolly/onyxite-customs
// from a given offset into an ark, the correct file handle is returned
// which is pointing at the correct offset in that file
// (will seek to a new offset outside of current bounds)
// 
// args:	offset into ark
//			flag or whether to seek outside existing file sizes
// returns:	file handle if successful
//			NULL if error
FILE* ArkFile::GetHandleFromOffset(s64 offset, bool offsetMustExist)
{
	s64 filesize;
	for(int i=0; i<(int)mArkHandles.size(); i++)
	{
		if((filesize=GetFilesize(mArkHandles[i])) < 0)
			return NULL;
		if(offset >= filesize)
		{
			// if there is still another ark file after this one
			// then update the offset to be in that ark file
			if(i+1 < (int)mArkHandles.size())
				offset -= filesize;
			else
				break;
		}
		else
		{
			FSEEK64(mArkHandles[i], offset, SEEK_SET);
			return mArkHandles[i];
		}
	}
	
	// offset is outside of the current file space
	if(offsetMustExist)
		return NULL;
	
	// check if we need to open a new ark file
	char zero[1] = {0};
	FILE* fd = NULL;
	if(offset >= mMaxArkSize &&
		mMaxArkSize != 0)
	{
		// pad the current ark file up to the max ark size limit,
		// then create a new ark file and seek to the required offset in it
		fd = mArkHandles[mArkHandles.size()-1];
		FSEEK64(fd, 0, SEEK_END);
		if(FTELL64(fd) < mMaxArkSize)
		{
			FSEEK64(fd, mMaxArkSize-1, SEEK_SET);
			fwrite(zero, 1, 1, fd);
		}
		offset -= mMaxArkSize;
		char new_filename[260];
		sprintf(new_filename, "%s%cMAIN_%d.ARK", mDirname, DIRSEPCHAR, (int) mArkHandles.size());
		FILE* fd = fopen(new_filename, "w+b");
		if(fd == NULL)
			return NULL;
		mArkHandles.push_back(fd);
		if(offset > 0)
		{
			FSEEK64(fd, offset-1, SEEK_SET);
			fwrite(zero, 1, 1, fd);
		}
		return fd;
	}
	else
	{
		// according to posix specifications, an fseek followed by an fwrite
		// should make a file grow to the new end of file mark.
		// (check the file needs to grow first though)
		fd = mArkHandles[mArkHandles.size()-1];
		FSEEK64(fd, 0, SEEK_END);
		if(FTELL64(fd) < offset)
		{
			FSEEK64(fd, offset-1, SEEK_SET);
			fwrite(zero, 1, 1, fd);
		}
		else
		{
			FSEEK64(fd, offset, SEEK_SET);
		}
		return fd;
	}
}
コード例 #7
0
ファイル: ArkFile.cpp プロジェクト: mtolly/onyxite-customs
// reads a file from ark into currently open file descriptor "destFd"
bool ArkFile::ReadFile(FILE* destFd, const FileEntry& entry, bool performDecrypts)
{
	if( !IsOpen() )
		return false;
	
	if(destFd == NULL)
		return false;
	
	// test for possible encrypted song files
	bool is_mogg_file	= performDecrypts && (strstr(entry.Arkname(), ".mogg") != 0);
	bool is_pss_file	= performDecrypts && (strstr(entry.Arkname(), ".pss") != 0);
	bool is_vgs_file	= performDecrypts && (strstr(entry.Arkname(), ".vgs") != 0);
	
	if( entry.IsExternal() )
	{
		// read out file from external file on pc
		FILE* src_fd = fopen(entry.Filename(), "rb");
		if(src_fd == NULL)
			return false;

		s64 filesize;
		if( !performDecrypts && entry.Encrypted() )
		{
			// external file is in decrypted form on pc
			// but is required to be read out in encrypted form
			// this occurs when an external file is added to an ark then the ark is "saved as"
			unsigned char* temp = new unsigned char[(int)entry.Arksize()];
			memcpy(temp, &G_CRYPT_KEY, 4);
			if( fread( temp+4, 1, (int)entry.Filesize(), src_fd) != entry.Filesize() )
			{
				delete[] temp;
				return false;
			}
			if( mNewEncryption )
				dtb_crypt_new(temp, (int)entry.Arksize());
			else
				dtb_crypt_old(temp, (int)entry.Arksize());
			if( fwrite(temp, 1, (int)entry.Arksize(), destFd) != entry.Arksize() )
			{
				delete[] temp;
				return false;
			}
			delete[] temp;
			return true;
		}
		else if( performDecrypts && entry.Encrypted() )
		{
			// external file is in decrypted form on pc
			// but is required to be read out in decrypted form
			// so just read it straight out
			filesize = entry.Filesize();
		}
		else if( !entry.Encrypted() )
		{
			// external file either isnt encrypted
			filesize = entry.Arksize();
		}
		for(s64 i=0; i<filesize; i+=MS_WORK_BUFFER_SIZE)
		{
			int read_size = (int)(min(filesize-i, MS_WORK_BUFFER_SIZE));
			if( fread( mpWorkBuff, 1, read_size, src_fd) != read_size ||
				fwrite(mpWorkBuff, 1, read_size, destFd) != read_size )
			{
				fclose(src_fd);
				return false;
			}
		}
		
		bool read_result = true;
		
		// decrypt output song files if they need to be
		if(is_mogg_file)
		{
			s64 end_file_loc = FTELL64(destFd);
			FSEEK64(destFd, -entry.Arksize(), SEEK_CUR);
			if( IsMoggEncrypted(destFd) )
				read_result = DecryptMogg(destFd);
			else
				FSEEK64(destFd, end_file_loc, SEEK_SET);
		}
		else if(is_pss_file)
		{
			s64 end_file_loc = FTELL64(destFd);
			FSEEK64(destFd, -entry.Arksize(), SEEK_CUR);
			if( IsPssEncrypted(destFd) )
				read_result = DecryptPss(destFd);
			else
				FSEEK64(destFd, end_file_loc, SEEK_SET);
		}
		else if(is_vgs_file)
		{
			s64 end_file_loc = FTELL64(destFd);
			FSEEK64(destFd, -entry.Arksize(), SEEK_CUR);
			if( IsVgsEncrypted(destFd) )
				read_result = DecryptVgs(destFd);
			else
				FSEEK64(destFd, end_file_loc, SEEK_SET);
		}
		
		// close external source file
		fclose(src_fd);
		return read_result;
	}
	else
	{
		// read out file from inside ark file
		FILE* src_fd = GetHandleFromOffset(entry.Offset(), true);
		if(src_fd == NULL)
			return false;
		
		if( performDecrypts && entry.Encrypted() )
		{
			// file needs to be decrypted
			unsigned char* temp = new unsigned char[(int)entry.Arksize()];
			if( fread( temp, 1, (int)entry.Arksize(), src_fd) != entry.Arksize() )
			{
				delete[] temp;
				return false;
			}
			if( mNewEncryption )
				dtb_crypt_new(temp, (int)entry.Arksize());
			else
				dtb_crypt_old(temp, (int)entry.Arksize());
			if( fwrite(temp+4, 1, (int)entry.Filesize(), destFd) != entry.Filesize() )
			{
				delete[] temp;
				return false;
			}
			delete[] temp;
			return true;
		}
		else
		{
			for(s64 i=0; i<entry.Arksize(); i+=MS_WORK_BUFFER_SIZE)
			{
				int read_size = (int)(min(entry.Arksize()-i, MS_WORK_BUFFER_SIZE));
				if( fread( mpWorkBuff, 1, read_size, src_fd) != read_size ||
					fwrite(mpWorkBuff, 1, read_size, destFd) != read_size )
				{
					return false;
				}
			}
			bool read_result = true;
			if(is_mogg_file)
			{
				s64 end_file_loc = FTELL64(destFd);
				FSEEK64(destFd, -entry.Arksize(), SEEK_CUR);
				if( IsMoggEncrypted(destFd) )
					read_result = DecryptMogg(destFd);
				else
					FSEEK64(destFd, end_file_loc, SEEK_SET);
			}
			else if(is_pss_file)
			{
				s64 end_file_loc = FTELL64(destFd);
				FSEEK64(destFd, -entry.Arksize(), SEEK_CUR);
				if( IsPssEncrypted(destFd) )
					read_result = DecryptPss(destFd);
				else
					FSEEK64(destFd, end_file_loc, SEEK_SET);
			}
			else if(is_vgs_file)
			{
				s64 end_file_loc = FTELL64(destFd);
				FSEEK64(destFd, -entry.Arksize(), SEEK_CUR);
				if( IsVgsEncrypted(destFd) )
					read_result = DecryptVgs(destFd);
				else
					FSEEK64(destFd, end_file_loc, SEEK_SET);
			}
			return read_result;
		}
	}
}
コード例 #8
0
/******************************************************************************
fillOutGetRec:
    Given a file name, returns a "getRec", which you can pass to
getSignalLine to fetch a line of signal data.
*/
getRec * fillOutGetRec(char file[])
{
    getRec *r=(getRec *)MALLOC (sizeof(getRec));
    char name_ASF[1024],name_RAW[1024];
    FILE *fp_ASF, *fp_RAW;
    int skip_second_if=0;

    create_name(name_ASF,file,".D");
    /* Lower case fopen used to control if statement */
    fp_ASF=fopen(name_ASF,"rb");

    create_name(name_RAW,file,".img");
    /* Lower case fopen used to control if statement */
    fp_RAW=fopen(name_RAW,"rb");

    r->lines=NULL;
    if (fp_ASF!=NULL)
    {
        struct        IOF_VFDR ifiledr;
        r->flipIQ='y';
        r->sampleSize=2;
        r->fp_in=fp_ASF;
        get_ifiledr(file,&ifiledr);
        if(!quietflag)printf("Looking for %s...\n",name_ASF);

    /**Make sure input file is a CCSD file and not a CEOS**/
        if (strcmp(ifiledr.formcode,"CI*2")!=0)
        {
          fprintf(stderr, "  %s is not a CCSD file.\n", name_ASF);
          if (!quietflag)printf("Looking for %s...\n",name_RAW);
        }
         else
        {
        if (!quietflag)
          printf("   Found ASF CCSD file '%s'...\n",name_ASF);
        skip_second_if++;
        r->lineSize=ifiledr.reclen;
        r->nSamples=ifiledr.datgroup;
        FSEEK64(r->fp_in,0,SEEK_END);
        r->nLines=FTELL64(r->fp_in)/r->lineSize;/*Automatically figure out number of lines in file.*/
        r->header=ifiledr.reclen+(ifiledr.reclen - ifiledr.sardata);
        r->dcOffsetI=r->dcOffsetQ=15.5;
        }
    }
    if ((fp_RAW!=NULL)&&(!skip_second_if))
    {
        long long bytesInFile;

        if (!quietflag) printf("   Found Raw, manual file '%s'...\n",name_RAW);

        r->header=0;/*Assume a zero-byte header*/
        r->dcOffsetI=r->dcOffsetQ=15.5;
        r->flipIQ='n';
        r->sampleSize=2;
        r->fp_in=fp_RAW;
        FSEEK64(r->fp_in,0,SEEK_END);
        bytesInFile=FTELL64(r->fp_in);

        getSignalFormat(file,bytesInFile,r);
    }
    r->inputArr=(unsigned char *)MALLOC(r->sampleSize*r->nSamples);
    return r;
}