📄 rsc_files.c
字号:
/***************************************************************************** * rsc_files.c: resources files manipulation functions * This library describe a general format used to store 'resources'. Resources * can be anything, including pictures, audio streams, and so on. ***************************************************************************** * Copyright (C) 1999, 2000 VideoLAN * * Authors: * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************//***************************************************************************** * Format of a resource file: * offset type meaning * 0 char[2] "RF" (magic number) * 2 char[2] "VL" (minor magic number, ignored) * 4 u16 i_type: resource file type. This is to allow * different versions of the resources types constants. * 6 u16 i_size: number of entries in the resources table. * { * +0 char[32] resource name (ASCIIZ or ASCII[32]) * +32 u16 resource type * +34 u64 data offset in bytes, from beginning of file * +42 u64 data size in bytes * } * i_size *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include "defs.h"#include <errno.h> /* errno */#include <fcntl.h> /* open() */#include <stdlib.h> /* free() */#include <string.h> /* strerror(), strncopy() */#include <unistd.h> /* read(), close(), lseek(), write() */#include "config.h"#include "common.h"#include "mtime.h"#include "rsc_files.h"#include "intf_msg.h"/***************************************************************************** * CreateResourceFile: create a new resource file ***************************************************************************** * Creates a new resource file. The file is opened read/write and erased if * it already exists. ***************************************************************************** * Messages type: rsc, major code 101 *****************************************************************************/resource_file_t *CreateResourceFile( char *psz_filename, int i_type, int i_size, int i_mode ){ resource_file_t * p_file; /* new descriptor */ int i_index; /* resource index */ /* Create descriptor and tables */ p_file = malloc( sizeof(resource_file_t) ); if( p_file == NULL ) { intf_ErrMsg("rsc error 101-1: %s\n", strerror(errno)); return( NULL ); } p_file->p_resource = malloc( sizeof(resource_descriptor_t) * i_size ); if( p_file->p_resource == NULL ) { intf_ErrMsg("rsc error 101-2: %s\n", strerror(errno)); free( p_file ); return( NULL ); } /* Open file */ p_file->i_file = open( psz_filename, O_CREAT | O_RDWR, i_mode ); if( p_file->i_file == -1 ) /* error */ { intf_ErrMsg("rsc error 101-3: %s: %s\n", psz_filename, strerror(errno) ); free( p_file->p_resource ); free( p_file ); } /* Initialize tables */ p_file->i_type = i_type; p_file->i_size = i_size; p_file->b_read_only = 0; for( i_index = 0; i_index < i_size; i_index++ ) { p_file->p_resource[i_index].i_type = EMPTY_RESOURCE; } return( p_file );}/***************************************************************************** * OpenResourceFile: open an existing resource file read-only ***************************************************************************** * Open an existing resource file. i_flags should be O_RDONLY or O_RDWR. An * error will occurs if the file does not exists. ***************************************************************************** * Messages type: rsc, major code 102 *****************************************************************************/resource_file_t *OpenResourceFile( char *psz_filename, int i_type, int i_flags ){ resource_file_t * p_file; /* new descriptor */ int i_index; /* resource index */ byte_t p_buffer[50]; /* buffer */ /* Create descriptor and tables */ p_file = malloc( sizeof(resource_file_t) ); if( p_file == NULL ) { intf_ErrMsg("rsc error 102-1: %s\n", strerror(errno)); return( NULL ); } /* Open file */ p_file->i_file = open( psz_filename, i_flags ); if( p_file->i_file == -1 ) /* error */ { intf_ErrMsg("rsc error 102-2: %s: %s\n", psz_filename, strerror(errno) ); free( p_file ); return( NULL ); } /* Read header */ if( read( p_file->i_file, p_buffer, 8 ) != 8) { intf_ErrMsg("rsc error 102-3: %s: unexpected end of file (not a resource file ?)\n"); close( p_file->i_file ); free( p_file); return( NULL ); } if( (p_buffer[0] != 'R') || (p_buffer[0] != 'F') || (*(u16 *)(p_buffer + 4) != i_type) ) { intf_ErrMsg("rsc error 102-4: %s is not a valid resource file or has incorrect type\n", psz_filename); close( p_file->i_file ); free( p_file ); return( NULL ); } p_file->i_type = i_type; p_file->i_size = *(u16 *)(p_buffer + 6); intf_DbgMsg("rsc debug 102-1: %s opened, %d resources\n", psz_filename, p_file->i_size); /* Allocate tables */ p_file->p_resource = malloc( sizeof(resource_descriptor_t) * p_file->i_size ); if( p_file->p_resource == NULL ) { intf_ErrMsg("rsc error 102-5: %s\n", strerror(errno)); close( p_file->i_file ); free( p_file ); return( NULL ); } /* Initialize table */ p_file->b_up_to_date = 1; p_file->b_read_only = ( i_flags & O_RDWR ) ? 0 : 1; for( i_index = 0; i_index < p_file->i_size; i_index++ ) { if( read( p_file->i_file, p_buffer, 50 ) != 50 ) { intf_ErrMsg("rsc error 102-6: %s: unexpected end of file\n", psz_filename); close( p_file->i_file ); free( p_file->p_resource ); free( p_file ); return( NULL ); } memcpy( p_file->p_resource[i_index].psz_name, p_buffer, 32 ); p_file->p_resource[i_index].psz_name[RESOURCE_MAX_NAME] = '\0'; p_file->p_resource[i_index].i_type = *(u16 *)(p_buffer + 32 ); p_file->p_resource[i_index].i_offset = *(u64 *)(p_buffer + 34 ); p_file->p_resource[i_index].i_size = *(u64 *)(p_buffer + 42 ); } return( p_file );}/***************************************************************************** * UpdateResourceFile: write the resources table in a resource file ***************************************************************************** * This function writes resources table in the resource file. This is * automatically done when the file is closed, but can also be done manually. ***************************************************************************** * Messages type: rsc, major code 103 *****************************************************************************/int UpdateResourceFile( resource_file_t *p_file ){ byte_t p_buffer[50]; /* buffer */ int i_index; /* resource index */#ifdef DEBUG if( p_file->b_read_only ) { intf_DbgMsg("rsc debug 103-1: can't update a read-only file\n"); return( -1 ); }#endif /* Seek beginning of file */ if( lseek( p_file->i_file, 0, SEEK_SET ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -