📄 input.c
字号:
/* Ming, an SWF output library Copyright (C) 2002 Opaque Industries - http://www.opaque.net/ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//* $Id: input.c,v 1.29 2008/06/22 14:13:09 krechert Exp $ */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <errno.h>#include <sys/stat.h>#ifndef WIN32 #include <unistd.h>#endif#include "libming.h"#include "input.h"#include "method.h"#include "error.h"struct SWFInput_s{ void (*destroy)(SWFInput This); int (*getChar)(SWFInput This); int (*read)(SWFInput This, unsigned char* buffer, int count); void (*seek)(SWFInput This, long offset, int whence); int (*eof)(SWFInput This); int offset; int length; void *data;#if TRACK_ALLOCS /* memory node for garbage collection */ mem_node *gcnode;#endif int buffer; int bufbits;};voidSWFInput_byteAlign(SWFInput input){ if(input->bufbits > 0) { input->bufbits = 0; input->buffer = 0; }}intSWFInput_readBits(SWFInput input, int number){ int ret = input->buffer; if ( number == input->bufbits ) { input->bufbits = 0; input->buffer = 0; return ret; } if ( number > input->bufbits ) { number -= input->bufbits; while( number > 8 ) { ret <<= 8; ret += SWFInput_getChar(input); number -= 8; } input->buffer = SWFInput_getChar(input); if ( number > 0 ) { ret <<= number; input->bufbits = 8-number; ret += input->buffer >> (8-number); input->buffer &= (1<<input->bufbits)-1; } return ret; } ret = input->buffer >> (input->bufbits-number); input->bufbits -= number; input->buffer &= (1<<input->bufbits)-1;// printf("done: readBits(%i) numer < bufbits %i\n", number, ret); return ret;}int SWFInput_readSBits(SWFInput input, int number){ int num = SWFInput_readBits(input, number); if ( num & (1<<(number-1)) ) return num - (1<<number); else return num;}intSWFInput_getChar(SWFInput input){ return input->getChar(input);}intSWFInput_getUInt16(SWFInput input){ int num = SWFInput_getChar(input); num += SWFInput_getChar(input) << 8; return num;}intSWFInput_getUInt16_BE(SWFInput input){ int num = SWFInput_getChar(input) << 8; num += SWFInput_getChar(input); return num;}intSWFInput_getSInt16(SWFInput input){ int num = SWFInput_getChar(input); num += SWFInput_getChar(input) * 256; return num;}unsigned long SWFInput_getUInt24_BE(SWFInput input){ unsigned long num = SWFInput_getChar(input) << 16; num += SWFInput_getChar(input) << 8; num += SWFInput_getChar(input); return num;}unsigned longSWFInput_getUInt32(SWFInput input){ unsigned long num = SWFInput_getChar(input); num += SWFInput_getChar(input) << 8; num += SWFInput_getChar(input) << 16; num += SWFInput_getChar(input) << 24; return num;}unsigned longSWFInput_getUInt32_BE(SWFInput input){ unsigned long num = SWFInput_getChar(input) << 24; num += SWFInput_getChar(input) << 16; num += SWFInput_getChar(input) << 8; num += SWFInput_getChar(input); return num;}intSWFInput_read(SWFInput input, unsigned char* buffer, int count){ return input->read(input, buffer, count);}voidSWFInput_seek(SWFInput input, long offset, int whence){ input->seek(input, offset, whence);}intSWFInput_length(SWFInput input){ int pos = SWFInput_tell(input); SWFInput_seek(input, 0, SEEK_END); SWFInput_seek(input, pos, SEEK_SET); return input->length;}intSWFInput_eof(SWFInput input){ return input->eof(input);}intSWFInput_tell(SWFInput input){ return input->offset;}voidSWFInput_rewind(SWFInput input){ SWFInput_seek(input, 0, SEEK_SET);}voiddestroySWFInput(SWFInput input){ input->destroy(input);}static voidSWFInput_dtor(SWFInput input){#if TRACK_ALLOCS ming_gc_remove_node(input->gcnode);#endif free(input);}/* SWFInput_file */static voidSWFInput_file_seek(SWFInput input, long offset, int whence){ if ( fseek((FILE *)input->data, offset, whence) == -1 ) { if ( errno == EBADF ) SWF_error("This is not a seekable stream- use newSWFInput_stream instead"); else if ( errno == EINVAL ) SWF_error("Invalid whence argument"); else SWF_error("Unknown error"); } if ( whence == SEEK_SET ) input->offset = offset; else if ( whence == SEEK_END ) input->offset = input->length - offset; else if ( whence == SEEK_CUR ) input->offset += offset;}static intSWFInput_file_eof(SWFInput input){ return feof((FILE *)input->data);}static intSWFInput_file_getChar(SWFInput input){ int c = fgetc((FILE *)input->data); if ( c == EOF ) input->length = input->offset; else ++input->offset; return c;}static int SWFInput_file_read(SWFInput input, unsigned char *buffer, int count){ int len = fread(buffer, 1, count, (FILE *)input->data); input->offset += len; return len;}SWFInputnewSWFInput_file(FILE *f){ SWFInput input; struct stat buf; /* XXX - doesn't check for validity of f.. */ if ( fseek(f, 0, SEEK_CUR) == -1 ) return newSWFInput_stream(f); if ( fstat(fileno(f), &buf) == -1 ) SWF_error("Couldn't fstat filehandle in newSWFInput_file");; input = (SWFInput) malloc(sizeof(struct SWFInput_s)); /* If malloc failed, return NULL to signify this */ if (NULL == input) return NULL; input->getChar = SWFInput_file_getChar; input->destroy = SWFInput_dtor; input->eof = SWFInput_file_eof; input->seek = SWFInput_file_seek; input->read = SWFInput_file_read; input->data = f; input->bufbits = 0; input->buffer = 0; input->offset = 0; input->length = buf.st_size;#if TRACK_ALLOCS input->gcnode = ming_gc_add_node(input, (dtorfunctype) destroySWFInput);#endif return input;}static void SWFInput_dtor_close(SWFInput input){ fclose((FILE *)input->data); SWFInput_dtor(input);}SWFInputnewSWFInput_filename(const char *filename){ FILE *file; SWFInput input; file = fopen(filename, "rb"); if(file == NULL) { SWF_warn("newSWFInput_filename: fopen failed\n"); return NULL; } input = newSWFInput_file(file); if(input == NULL) return NULL; input->destroy = SWFInput_dtor_close; return input;}/* SWFInput_buffer */static intSWFInput_buffer_getChar(SWFInput input){ if ( input->offset >= input->length ) return EOF; else return ((unsigned char *)input->data)[input->offset++];}static intSWFInput_buffer_eof(SWFInput input){ return input->offset >= input->length;}static intSWFInput_buffer_read(SWFInput input, unsigned char* buffer, int count){ if ( count > input->length - input->offset ) count = input->length - input->offset; memcpy(buffer, (unsigned char*)input->data + input->offset, count); input->offset += count; return count;}static voidSWFInput_buffer_seek(SWFInput input, long offset, int whence){ if ( whence == SEEK_CUR ) { if ( offset >= 0 ) input->offset = min(input->length, input->offset + offset); else input->offset = max(0, input->offset + offset); } else if ( whence == SEEK_END )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -