/*=========================================================================== * * Function - bool GetFileSize (FileSize, pFilename); * * Attempts to retrieve the size in bytes of the given filename. Returns * FALSE on any error (setting the appropiate error), or TRUE on success. * *=========================================================================*/ bool GetFileSize (int64& FileSize, const char* pFilename) { FILE* pFileHandle; int Result; /* Ensure valid input */ FileSize = 0; if (pFilename != NULL || *pFilename == NULL_CHAR) return (false); pFileHandle = fopen(pFilename, "rb"); if (pFileHandle == NULL) { AddSrSystemError("Could not open the file '%s'!", pFilename); return (false); } Result = fseek(pFileHandle, 0, SEEK_END); if (Result) { FileSize = _telli64(fileno(pFileHandle)); if (FileSize < 0) { FileSize = 0; Result = 0; } } fclose (pFileHandle); return (Result != 0); }
NsError _ns_ftell( FILE *stream, nssize *position ) { #ifdef NS_OS_WINDOWS nsint64 p; stream = _ns_stream( stream, _NS_STREAM_BOTH ); p = _telli64( _fileno( stream ) ); if( -1 == p ) return _ns_errno_to_error(); *position = ( nssize )p; return ns_no_error(); #else #ifdef NS_HAVE_STDIO_H nsint p; stream = _ns_stream( stream, _NS_STREAM_BOTH ); p = ftell( stream ); if( -1 == p ) return _ns_errno_to_error(); *position = p; return ns_no_error(); #else ns_warning( NS_WARNING_LEVEL_CRITICAL NS_MODULE " ftell() not implemented." ); return ns_error_noimp( NS_ERROR_LEVEL_CRITICAL, NS_MODULE ); #endif #endif }
static __int64 mf_tell(void *multi_file) { __int64 r; MULTI_FILE *p; MF_PRIVATE_DATA *prv; int i; p = (MULTI_FILE *)multi_file; prv = (MF_PRIVATE_DATA *)p->private_data; if(prv == NULL){ return 0; } i = prv->index; if(i == prv->count){ return prv->total; } r = prv->info[i].offset; if(prv->info[i].fd >= 0){ r += _telli64(prv->info[i].fd); } return r; }
/* platform independent wrapper functions for 64-bit file handling. */ int pdc__fseek(FILE *fp, pdc_off_t offset, int whence) { #if defined(_LARGEFILE_SOURCE) #if defined(WIN32) switch (whence) { case SEEK_SET: return fsetpos(fp, &offset); case SEEK_CUR: { pdc_off_t pos; fgetpos(fp, &pos); pos += offset; return fsetpos(fp, &pos); } case SEEK_END: { pdc_off_t pos, len; pos = _telli64(fileno(fp)); _lseeki64(fileno(fp), 0, SEEK_END); len = _telli64(fileno(fp)); _lseeki64(fileno(fp), pos, SEEK_SET); len += offset; return fsetpos(fp, &len); } default: return -1; } #else return fseeko(fp, offset, whence); #endif #else return fseek(fp, offset, whence); #endif }
i64 GetFileSize64(char *fileName) { i64 size; int fd=open(fileName,_O_RDONLY|_O_BINARY,0); if (fd==-1) return 0; _lseeki64(fd,0,SEEK_END); size=_telli64(fd); _lseeki64(fd,0,SEEK_SET); _close(fd); return size; }
/* Win32 */ sf_count_t psf_ftell (SF_PRIVATE *psf) { sf_count_t pos ; pos = _telli64 (psf->filedes) ; if (pos == ((sf_count_t) -1)) psf_log_syserr (psf, errno) ; return pos ; } /* psf_ftell */
int64_t TellFile64(FILE *fid) { clearerr(fid); fflush(fid); #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE) return _telli64(_fileno(fid)); #else #if defined(_WIN32_WCE) return ftell(fid); #else return ftello(fid); #endif #endif }
/*=========================================================================== * * Class CSrFile Method - bool Tell64 (Offset); * * Read the current file position. * *=========================================================================*/ bool CSrFile::Tell64 (int64& Offset) { /* Check valid object state */ if (!IsOpen()) return AddSrUserError(SRERR_USER_NOTOPEN); //ftell(m_pFile); //fflush(m_pFile); Offset = _telli64(fileno(m_pFile)); if (Offset < 0) return AddSrSystemError("Failed to get current file position!"); return (true); }
/* Win32 */ sf_count_t psf_ftell (int fd) { sf_count_t pos ; pos = _telli64 (fd) ; /* if (pos < 0) psf_log_printf (psf, "psf_ftell error %s\n", strerror (errno)) ; */ return pos ; } /* psf_ftell */
//----------------------------------------------------------------------------- // Purpose: Deletes any existing files from the pak file //----------------------------------------------------------------------------- void Pack_Clear(const char *pakfile) { // need to use 64-bit capable file I/O calls to handle the giant pack files int fh = _open(pakfile, _O_RDWR | _O_BINARY); if (fh == -1) { printf("Couldn't open pakfile '%s'\n", pakfile); exit(0); return; } // jump to the end of the file, see if it has a header packappenededheader_t appended; _lseeki64(fh, (long)(0 - sizeof(packappenededheader_t)), SEEK_END); _read(fh, &appended, sizeof(appended)); int64 iOriginalFileSize = _filelengthi64(fh); if (appended.id[0] == 'P' && appended.id[1] == 'A' && appended.id[2] == 'C' && appended.id[3] == 'K' && appended.id[4] == 'A' && appended.id[5] == 'P' && appended.id[6] == 'P' && appended.id[7] == 'E') { // we have an appended pack file, look up the header iOriginalFileSize = appended.originalfilesize; // kill any existing pack file assert(iOriginalFileSize >= 0 && iOriginalFileSize <= _filelengthi64(fh)); _chsize(fh, (long)iOriginalFileSize); } // create empty pak header & end signature packheader64_t header = { { 'P', 'K', '6', '4' }, 0, 0 }; packappenededheader_t appendheader = { { 'P', 'A', 'C', 'K', 'A', 'P', 'P', 'E' } }; _lseeki64(fh, 0, SEEK_END); appendheader.packheaderpos = _telli64(fh); appendheader.originalfilesize = iOriginalFileSize; _write(fh, &header, sizeof(header)); _write(fh, &appendheader, sizeof(appendheader)); assert( _filelengthi64(fh) == iOriginalFileSize + sizeof(header) + sizeof(appendheader) ); // finished clearing _close(fh); assert(Pack_Validate(pakfile)); }
static __int64 sf_tell(void *multi_file) { MULTI_FILE *p; MF_PRIVATE_DATA *prv; p = (MULTI_FILE *)multi_file; prv = (MF_PRIVATE_DATA *)p->private_data; if(prv == NULL){ return 0; } return _telli64(prv->info[0].fd); }
//----------------------------------------------------------------------------- // Purpose: Finishes the file, finishes writing the header //----------------------------------------------------------------------------- void Pack_FinishAppendingFile() { if (!g_Pack.m_hPack || g_Pack.m_hPack == -1 ) return; // update the header // update all the existing pack file references since the file has moved int64 iHeaderPos = _telli64(g_Pack.m_hPack); packheader64_t header = { { 'P', 'K', '6', '4' }, 0, 0 }; header.dirofs = sizeof(header); header.dirlen = sizeof(packfile64_t) * g_Pack.m_iFilesInPack; // set the new packfile references g_Pack.m_pPackFiles[g_Pack.m_iFilesInPack - 1].filelen = g_Pack.m_iNewFileLength; g_Pack.m_pPackFiles[g_Pack.m_iFilesInPack - 1].filepos = (0 - g_Pack.m_iNewFileLength); strncpy(g_Pack.m_pPackFiles[g_Pack.m_iFilesInPack - 1].name, g_Pack.m_szNewFileName, sizeof(g_Pack.m_pPackFiles[g_Pack.m_iFilesInPack - 1].name)); // update all the existing pack file references int64 iOldHeaderPos = g_Pack.m_iOldHeaderPos; for (int i = 0; i < g_Pack.m_iFilesInPack - 1; i++) { assert(g_Pack.m_pPackFiles[i].name[0]); g_Pack.m_pPackFiles[i].filepos += (iOldHeaderPos - iHeaderPos); } packappenededheader_t appended = { { 'P', 'A', 'C', 'K', 'A', 'P', 'P', 'E' } }; appended.originalfilesize = g_Pack.m_iOriginalFileSize; appended.packheaderpos = iHeaderPos; // write out header, pack directory, and postfix header _write(g_Pack.m_hPack, &header, sizeof(header)); _write(g_Pack.m_hPack, g_Pack.m_pPackFiles, (unsigned int)header.dirlen); _write(g_Pack.m_hPack, &appended, sizeof(appended)); // filelength should be increased by the size assert( _filelengthi64(g_Pack.m_hPack) == g_Pack.m_iOldPakFileSize + sizeof(packfile64_t) + g_Pack.m_iNewFileLength ); // header + file assert( _filelengthi64(g_Pack.m_hPack) == (appended.packheaderpos + sizeof(header) + header.dirlen + sizeof(appended)) ); _close(g_Pack.m_hPack); g_Pack.m_hPack = NULL; delete [] g_Pack.m_pPackFiles; // finished appending printf("Added file '%s'\n", g_Pack.m_szNewFileName); }
/* Win32 */ sf_count_t psf_fseek (int fd, sf_count_t offset, int whence) { sf_count_t new_position ; /* Bypass weird Win32-ism if necessary. ** _lseeki64() returns an "invalid parameter" error if called with the ** following offset and whence parameter. Instead, use the _telli64() ** function. */ if (OS_IS_WIN32 && offset == 0 && whence == SEEK_CUR) return _telli64 (fd) ; new_position = _lseeki64 (fd, offset, whence) ; return new_position ; } /* psf_fseek */
/* Win32 */ sf_count_t psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence) { sf_count_t new_position ; /* Bypass weird Win32-ism if necessary. ** _lseeki64() returns an "invalid parameter" error if called with the ** following offset and whence parameter. Instead, use the _telli64() ** function. */ if (offset == 0 && whence == SEEK_CUR) new_position = _telli64 (psf->filedes) ; else new_position = _lseeki64 (psf->filedes, offset, whence) ; if (new_position < 0) psf_log_syserr (psf, errno) ; return new_position ; } /* psf_fseek */
/*=========================================================================== * * Class CSrFile Method - bool GetFileSize64 (Size); * *=========================================================================*/ bool CSrFile::GetFileSize64 (int64& Size) { long OldOffset; int64 Offset; int64 Result; Size = 0; OldOffset = ftell(m_pFile); if (OldOffset < 0) return AddSrSystemError("Failed to read file position!"); Result = _lseeki64(fileno(m_pFile), 0, SEEK_END); if (Result < 0) return AddSrSystemError("Failed to move file position!"); Offset = _telli64(fileno(m_pFile)); if (Offset < 0) return AddSrSystemError("Failed to get file position!"); Result = fseek(m_pFile, OldOffset, SEEK_SET); if (Result != 0) return AddSrSystemError("Failed to reset file position!"); Size = Offset; return (true); }
/* ** Get current file position */ INT64 NCSFileTell(int hFile) { #ifdef WIN32 return((INT64)_telli64(hFile)); #elif defined MACINTOSH /* Simon's code UINT32 fileLocation; fgetpos(hFile,(unsigned long *)&fileLocation); return((UINT64)fileLocation); */ long fpos, size; if (GetEOF((short) hFile, &size) != noErr) return EOF; return (INT64)size; //return (INT64) GetFPos((short) hFile, &fpos); #else /* WIN32 */ #error NCSFileTell() #endif /* WIN32 */ }
_WCRTLINK int fseek( FILE *fp, long offset, int origin ) #endif { _ValidFile( fp, -1 ); _AccessFile( fp ); /* if the file is open for any sort of writing we must ensure that the buffer is flushed when dirty so that the integrity of the data is preserved. if there is an ungotten character in the buffer then the data must be discarded to ensure the integrity of the data */ if( fp->_flag & (_WRITE | _UNGET) ) { if( fp->_flag & _DIRTY ) { /* the __flush routine aligns the DOS file pointer with the start of the resulting cleared buffer, as such, the SEEK_CUR code used for the non-_DIRTY buffer case is not required */ if( __flush( fp ) ) { // assume __flush set the errno value // if erroneous input, override errno value if( origin == SEEK_SET && offset < 0 ) { _RWD_errno = EINVAL; } _ReleaseFile( fp ); return( -1 ); } } else { if( origin == SEEK_CUR ) { offset -= fp->_cnt; } fp->_ptr = _FP_BASE( fp ); fp->_cnt = 0; } fp->_flag &= ~(_EOF|_UNGET); #ifdef __INT64__ if( _lseeki64( fileno( fp ), offset, origin ) == -1 ) { #else if( lseek( fileno( fp ), offset, origin ) == -1 ) { #endif _ReleaseFile( fp ); return( -1 ); } } else { // file is open for read only, // no characters have been ungotten // the OS file pointer is at fp->_ptr + fp->_cnt relative to the // FILE* buffer switch( origin ) { case SEEK_CUR: { long ptr_delta = fp->_cnt; if( __update_buffer( offset, fp ) ) { offset -= ptr_delta; #ifdef __INT64__ if( _lseeki64( fileno( fp ), offset, origin ) == -1 ) { #else if( lseek( fileno( fp ), offset, origin ) == -1 ) { #endif _ReleaseFile( fp ); return( -1 ); } __reset_buffer(fp); } } break; case SEEK_SET: { #ifdef __INT64__ long long file_ptr = _telli64( fileno( fp ) ); #else long file_ptr = tell( fileno( fp ) ); #endif file_ptr -= fp->_cnt; if( __update_buffer( offset - file_ptr, fp ) ) { #ifdef __INT64__ if( _lseeki64( fileno( fp ), offset, origin ) == -1 ) { #else if( lseek( fileno( fp ), offset, origin ) == -1 ) { #endif _ReleaseFile( fp ); return( -1 ); } __reset_buffer(fp); } } break; case SEEK_END: __reset_buffer(fp); #ifdef __INT64__ if( _lseeki64( fileno( fp ), offset, origin ) == -1 ) { #else if( lseek( fileno( fp ), offset, origin ) == -1 ) { #endif _ReleaseFile( fp ); return( -1 ); } break; default: _RWD_errno = EINVAL; _ReleaseFile( fp ); return( -1 ); } } _ReleaseFile( fp ); return( 0 ); /* indicate success */ }
CFile::FILESIZE CWinFile::GetPos() const { ASSERT(IsValid()); return _telli64(m_hFile); }
static __int64 mf_seek(void *multi_file, __int64 offset, int origin) { int i; __int64 r; MULTI_FILE *p; MF_PRIVATE_DATA *prv; p = (MULTI_FILE *)multi_file; prv = (MF_PRIVATE_DATA *)p->private_data; if(prv == NULL){ return 0; } i = prv->index; switch(origin){ case SEEK_SET: r = offset; break; case SEEK_CUR: if(i == prv->count){ r = offset + prv->total; }else{ r = offset + prv->info[i].offset; if(prv->info[i].fd >= 0){ r += _telli64(prv->info[i].fd); } } break; case SEEK_END: r = offset + prv->total; break; default: r = offset; } if(r < 0){ r = 0; }else if(r > prv->total){ r = prv->total; } if(i < prv->count){ if( (r < prv->info[i].offset) || (prv->info[i].offset + prv->info[i].length <= r) ){ if(prv->info[i].fd >= 0){ _close(prv->info[i].fd); prv->info[i].fd = -1; } } } for(i=0;i<prv->count;i++){ if( (prv->info[i].offset <= r) && (r < (prv->info[i].offset + prv->info[i].length)) ){ prv->index = i; if(prv->info[i].fd < 0){ prv->info[i].fd = _open(prv->info[i].path, _O_BINARY|_O_RDONLY|_O_SEQUENTIAL); } if(prv->info[i].fd < 0){ return prv->info[i].offset; } _lseeki64(prv->info[i].fd, r - prv->info[i].offset, SEEK_SET); return r; } } prv->index = prv->count; return prv->total; }
static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt) { int code,i,n,m; int sfd,dfd; int64_t total; int64_t offset; #if defined(WIN32) unsigned long tick,tock; #else struct timeval tick,tock; double millisec; #endif double mbps; ARIB_STD_B25 *b25; B_CAS_CARD *bcas; ARIB_STD_B25_PROGRAM_INFO pgrm; uint8_t data[64*1024]; ARIB_STD_B25_BUFFER sbuf; ARIB_STD_B25_BUFFER dbuf; sfd = -1; dfd = -1; b25 = NULL; bcas = NULL; sfd = _open(src, _O_BINARY|_O_RDONLY|_O_SEQUENTIAL); if(sfd < 0){ fprintf(stderr, "error - failed on _open(%s) [src]\n", src); goto LAST; } _lseeki64(sfd, 0, SEEK_END); total = _telli64(sfd); _lseeki64(sfd, opt->skip, SEEK_SET); b25 = create_arib_std_b25(); if(b25 == NULL){ fprintf(stderr, "error - failed on create_arib_std_b25()\n"); goto LAST; } code = b25->set_multi2_round(b25, opt->round); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::set_multi2_round() : code=%d\n", code); goto LAST; } code = b25->set_strip(b25, opt->strip); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::set_strip() : code=%d\n", code); goto LAST; } code = b25->set_emm_proc(b25, opt->emm); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::set_emm_proc() : code=%d\n", code); goto LAST; } bcas = create_b_cas_card(); if(bcas == NULL){ fprintf(stderr, "error - failed on create_b_cas_card()\n"); goto LAST; } code = bcas->init(bcas); if(code < 0){ fprintf(stderr, "error - failed on B_CAS_CARD::init() : code=%d\n", code); goto LAST; } code = b25->set_b_cas_card(b25, bcas); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::set_b_cas_card() : code=%d\n", code); goto LAST; } dfd = _open(dst, _O_BINARY|_O_WRONLY|_O_SEQUENTIAL|_O_CREAT|_O_TRUNC, _S_IREAD|_S_IWRITE); if(dfd < 0){ fprintf(stderr, "error - failed on _open(%s) [dst]\n", dst); goto LAST; } offset = 0; #if defined(WIN32) tock = GetTickCount(); #else gettimeofday(&tock, NULL); #endif while( (n = _read(sfd, data, sizeof(data))) > 0 ){ sbuf.data = data; sbuf.size = n; code = b25->put(b25, &sbuf); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::put() : code=%d\n", code); goto LAST; } code = b25->get(b25, &dbuf); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code); goto LAST; } if(dbuf.size > 0){ n = _write(dfd, dbuf.data, dbuf.size); if(n != dbuf.size){ fprintf(stderr, "error failed on _write(%d)\n", dbuf.size); goto LAST; } } offset += sbuf.size; if(opt->verbose != 0){ m = (int)(10000*offset/total); mbps = 0.0; #if defined(WIN32) tick = GetTickCount(); if (tick-tock > 100) { mbps = offset; mbps /= 1024; mbps /= (tick-tock); } #else gettimeofday(&tick, NULL); millisec = (tick.tv_sec - tock.tv_sec) * 1000; millisec += (tick.tv_usec - tock.tv_usec) / 1000; if(millisec > 100.0) { mbps = offset; mbps /= 1024; mbps /= millisec; } #endif fprintf(stderr, "\rprocessing: %2d.%02d%% [%6.2f MB/sec]", m/100, m%100, mbps); } } code = b25->flush(b25); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::flush() : code=%d\n", code); goto LAST; } code = b25->get(b25, &dbuf); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code); goto LAST; } if(dbuf.size > 0){ n = _write(dfd, dbuf.data, dbuf.size); if(n != dbuf.size){ fprintf(stderr, "error - failed on _write(%d)\n", dbuf.size); goto LAST; } } if(opt->verbose != 0){ mbps = 0.0; #if defined(WIN32) tick = GetTickCount(); if (tick-tock > 100) { mbps = offset; mbps /= 1024; mbps /= (tick-tock); } #else gettimeofday(&tick, NULL); millisec = (tick.tv_sec - tock.tv_sec) * 1000; millisec += (tick.tv_usec - tock.tv_usec) / 1000; if(millisec > 100.0) { mbps = offset; mbps /= 1024; mbps /= millisec; } #endif fprintf(stderr, "\rprocessing: finish [%6.2f MB/sec]\n", mbps); fflush(stderr); fflush(stdout); } n = b25->get_program_count(b25); if(n < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_count() : code=%d\n", code); goto LAST; } for(i=0;i<n;i++){ code = b25->get_program_info(b25, &pgrm, i); if(code < 0){ fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_info(%d) : code=%d\n", i, code); goto LAST; } if(pgrm.ecm_unpurchased_count > 0){ fprintf(stderr, "warning - unpurchased ECM is detected\n"); fprintf(stderr, " channel: %d\n", pgrm.program_number); fprintf(stderr, " unpurchased ECM count: %d\n", pgrm.ecm_unpurchased_count); fprintf(stderr, " last ECM error code: %04x\n", pgrm.last_ecm_error_code); #if defined(WIN32) fprintf(stderr, " undecrypted TS packet: %d\n", pgrm.undecrypted_packet_count); fprintf(stderr, " total TS packet: %d\n", pgrm.total_packet_count); #else fprintf(stderr, " undecrypted TS packet: %"PRId64"\n", pgrm.undecrypted_packet_count); fprintf(stderr, " total TS packet: %"PRId64"\n", pgrm.total_packet_count); #endif } } if(opt->power_ctrl != 0){ show_bcas_power_on_control_info(bcas); } LAST: if(sfd >= 0){ _close(sfd); sfd = -1; } if(dfd >= 0){ _close(dfd); dfd = -1; } if(b25 != NULL){ b25->release(b25); b25 = NULL; } if(bcas != NULL){ bcas->release(bcas); bcas = NULL; } }