📄 ttmmap.c
字号:
}/******************************************************************* * * Function : TT_Flush_Stream * * Description : Flushes a stream, i.e., closes its file handle. * * Input : stream address of target TT_Stream structure * * Output : Error code * * NOTE : Never flush the current opened stream. This means that * you should _never_ call this function between a * TT_Use_Stream() and a TT_Done_Stream()! * ******************************************************************/ EXPORT_FUNC TT_Error TT_Flush_Stream( TT_Stream* stream ) { /* XXX - DUMMY IMPLEMENTATION */ return TT_Err_Ok; }/******************************************************************* * * Function : TT_Stream_Size * * Description : Returns the length of a given stream, even if it * is flushed. * * Input : stream the stream * * Output : Length of stream in bytes. * ******************************************************************/ EXPORT_FUNC Long TT_Stream_Size( TT_Stream stream ) { PStream_Rec rec = STREAM2REC( stream ); if ( rec ) return rec->map->size; else return 0; /* invalid stream - return 0 */ }/******************************************************************* * * Function : TT_Seek_File * * Description : Seeks the file cursor to a different position. * * Input : position new position in file * * Output : SUCCESS on success. FAILURE if out of range. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Seek_File( STREAM_ARGS Long position ) { if ( position > CUR_Stream->map->size ) return TT_Err_Invalid_File_Offset; CUR_Stream->pos = position; return TT_Err_Ok; }/******************************************************************* * * Function : TT_Skip_File * * Description : Skips forward the file cursor. * * Input : distance number of bytes to skip * * Output : see TT_Seek_File * ******************************************************************/ EXPORT_FUNC TT_Error TT_Skip_File( STREAM_ARGS Long distance ) { return TT_Seek_File( STREAM_VARS CUR_Stream->pos + distance ); }/******************************************************************* * * Function : TT_Read_File * * Description : Reads a chunk of the file and copies it to memory. * * Input : buffer target buffer * count length in bytes to read * * Output : SUCCESS on success. FAILURE if out of range. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Read_File( STREAM_ARGS void* buffer, Long count ) { if ( CUR_Stream->pos + count > CUR_Stream->map->size ) return TT_Err_Invalid_File_Read; MEM_Copy( buffer, MAP_Address( CUR_Stream->map ) + CUR_Stream->pos, count ); CUR_Stream->pos += count; return TT_Err_Ok; }/******************************************************************* * * Function : TT_Read_At_File * * Description : Reads file at a specified position. * * Input : position position to seek to before read * buffer target buffer * count number of bytes to read * * Output : SUCCESS on success. FAILURE if error. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Read_At_File( STREAM_ARGS Long position, void* buffer, Long count ) { TT_Error error; if ( (error = TT_Seek_File( STREAM_VARS position )) || (error = TT_Read_File( STREAM_VARS buffer, count )) ) return error; return TT_Err_Ok; }/******************************************************************* * * Function : TT_File_Pos * * Description : Returns current file seek pointer. * * Input : none * * Output : current file position * ******************************************************************/ EXPORT_FUNC Long TT_File_Pos( STREAM_ARG ) { return CUR_Stream->pos; }/******************************************************************* * * Function : TT_Access_Frame * * Description : Notifies the component that we're going to read * 'size' bytes from the current file position. * This function should load/cache/map these bytes * so that they will be addressed by the GET_xxx() * functions easily. * * Input : size number of bytes to access. * * Output : Error code * * Notes: The function fails if the byte range is not within the * the file, or if there is not enough memory to cache * the bytes properly (which usually means that aSize is * too big in both cases). * ******************************************************************/ EXPORT_FUNC TT_Error TT_Access_Frame( STREAM_ARGS FRAME_ARGS Long size ) { if ( CUR_Frame.address != NULL ) return TT_Err_Nested_Frame_Access; if ( CUR_Stream->pos + size > CUR_Stream->map->size ) return TT_Err_Invalid_Frame_Access; CUR_Frame.size = size; CUR_Frame.address = MAP_Address( CUR_Stream->map ) + CUR_Stream->pos; CUR_Frame.cursor = CUR_Frame.address; CUR_Stream->pos += size; return TT_Err_Ok; }/******************************************************************* * * Function : TT_Check_And_Access_Frame * * Description : Notifies the component that we're going to read * 'size' bytes from the current file position. * This function should load/cache/map these bytes * so that they will be addressed by the GET_xxx() * functions easily. * * Input : size number of bytes to access. * * Output : Error code * * Notes: The function truncates 'size' if the byte range is not * within the file. * * It will fail if there is not enough memory to cache * the bytes properly (which usually means that aSize is * too big). * * It will fail if you make two consecutive calls * to TT_Access_Frame(), without a TT_Forget_Frame() between * them. * * The only difference with TT_Access_Frame() is that we * check that the frame is within the current file. We * otherwise truncate it. The 'overflow' is set to zero. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Check_And_Access_Frame( STREAM_ARGS FRAME_ARGS Long size ) { TT_Error error; Long readBytes; if ( CUR_Frame.address != NULL ) return TT_Err_Nested_Frame_Access; readBytes = CUR_Stream->map->size - CUR_Stream->pos; if ( size > readBytes ) { /* There is overflow, we allocate a new block then */ if ( ALLOC( CUR_Frame.address, size ) ) return error; CUR_Frame.size = size; /* copy the valid part */ MEM_Copy( CUR_Frame.address, MAP_Address( CUR_Stream->map ) + CUR_Stream->pos, readBytes ); } else { CUR_Frame.size = size; CUR_Frame.address = MAP_Address( CUR_Stream->map ) + CUR_Stream->pos; } CUR_Frame.cursor = CUR_Frame.address; return TT_Err_Ok; }/******************************************************************* * * Function : TT_Forget_Frame * * Description : Releases a cached frame after reading. * * Input : None * * Output : SUCCESS on success. FAILURE on error. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Forget_Frame( FRAME_ARG ) { if ( CUR_Frame.address == NULL ) return TT_Err_Nested_Frame_Access; /* If we were using a duplicate in case of overflow, free it now */ if ( CUR_Frame.address < (Byte*)CUR_Stream->map->base || CUR_Frame.address >= (Byte*)CUR_Stream->map->base + CUR_Stream->map->size ) FREE( CUR_Frame.address ); ZERO_Frame( files.frame ); return TT_Err_Ok; }/******************************************************************* * * Function : GET_Byte * * Description : Extracts a byte from the current file frame. * * Input : None or current frame * * Output : Extracted Byte * * NOTES : We consider that the programmer is intelligent enough * not to try to get a byte that is out of the frame. Hence, * we provide no bounds check here. (A misbehaving client * could easily page fault using this call.) * ******************************************************************/#if 0 EXPORT_FUNC Byte TT_Get_Byte( FRAME_ARG ) { CHECK_FRAME( CUR_Frame, 1 ); return (Byte)(*CUR_Frame.cursor++); }#endif/******************************************************************* * * Function : GET_Char * * Description : Extracts a signed byte from the current file frame. * * Input : None or current frame * * Output : Extracted char * * NOTES : We consider that the programmer is intelligent enough * not to try to get a byte that is out of the frame. Hence, * we provide no bounds check here. (A misbehaving client * could easily page fault using this call.) * ******************************************************************/ EXPORT_FUNC Char TT_Get_Char( FRAME_ARG ) { CHECK_FRAME( CUR_Frame, 1 ); return (Char)(*CUR_Frame.cursor++); }/******************************************************************* * * Function : GET_Short * * Description : Extracts a short from the current file frame. * * Input : None or current frame * * Output : Extracted short * * NOTES : We consider that the programmer is intelligent enough * not to try to get a byte that is out of the frame. Hence, * we provide no bounds check here. (A misbehaving client * could easily page fault using this call.) * ******************************************************************/ EXPORT_FUNC Short TT_Get_Short( FRAME_ARG ) { Short getshort; CHECK_FRAME( CUR_Frame, 2 ); getshort = ((Short)CUR_Frame.cursor[0] << 8) | (Short)CUR_Frame.cursor[1]; CUR_Frame.cursor += 2; return getshort; }/******************************************************************* * * Function : GET_UShort * * Description : Extracts an unsigned short from the frame. * * Input : None or current frame * * Output : Extracted ushort * * NOTES : We consider that the programmer is intelligent enough * not to try to get a byte that is out of the frame. Hence, * we provide no bounds check here. (A misbehaving client * could easily page fault using this call.) * ******************************************************************/#if 0 EXPORT_FUNC UShort TT_Get_UShort( FRAME_ARG ) { UShort getshort; CHECK_FRAME( CUR_Frame, 2 ); getshort = ((UShort)CUR_Frame.cursor[0] << 8) | (UShort)CUR_Frame.cursor[1]; CUR_Frame.cursor += 2; return getshort; }#endif/******************************************************************* * * Function : GET_Long * * Description : Extracts a long from the frame. * * Input : None or current frame * * Output : Extracted long * * NOTES : We consider that the programmer is intelligent enough * not to try to get a byte that is out of the frame. Hence, * we provide no bounds check here. (A misbehaving client * could easily page fault using this call.) * ******************************************************************/ EXPORT_FUNC Long TT_Get_Long( FRAME_ARG ) { Long getlong; CHECK_FRAME( CUR_Frame, 4 ); getlong = ((Long)CUR_Frame.cursor[0] << 24) | ((Long)CUR_Frame.cursor[1] << 16) | ((Long)CUR_Frame.cursor[2] << 8 ) | (Long)CUR_Frame.cursor[3]; CUR_Frame.cursor += 4; return getlong; }/******************************************************************* * * Function : GET_ULong * * Description : Extracts an unsigned long from the frame. * * Input : None * * Output : Extracted ulong * * NOTES : We consider that the programmer is intelligent enough * not to try to get a byte that is out of the frame. Hence, * we provide no bounds check here. (A misbehaving client * could easily page fault using this call.) * ******************************************************************/#if 0 EXPORT_FUNC ULong TT_Get_ULong( FRAME_ARG ) { ULong getlong; CHECK_FRAME( CUR_Frame, 4 ); getlong = ( ((ULong)CUR_Frame.cursor[0] << 24) | ((ULong)CUR_Frame.cursor[1] << 16) | ((ULong)CUR_Frame.cursor[2] << 8 ) | (ULong)CUR_Frame.cursor[3] ); CUR_Frame.cursor += 4; return getlong; }#endif/* END */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -