📄 input.c
字号:
input->offset = max(0, input->length - offset); else if ( whence == SEEK_SET ) input->offset = min(input->length, offset);}SWFInputnewSWFInput_buffer(unsigned char* buffer, int length){ SWFInput input = (SWFInput) malloc(sizeof(struct SWFInput_s)); /* If malloc failed, return NULL to signify this */ if (NULL == input) return NULL; input->getChar = SWFInput_buffer_getChar; input->destroy = SWFInput_dtor; input->eof = SWFInput_buffer_eof; input->read = SWFInput_buffer_read; input->seek = SWFInput_buffer_seek; input->data = buffer; input->buffer = 0; input->bufbits = 0; input->offset = 0; input->length = length;#if TRACK_ALLOCS input->gcnode = ming_gc_add_node(input, (dtorfunctype) destroySWFInput);#endif return input;}static voidSWFInput_buffer_dtor(SWFInput input){ free(input->data);#if TRACK_ALLOCS ming_gc_remove_node(input->gcnode);#endif free(input);}/* same as above but needs to be freed */SWFInputnewSWFInput_allocedBuffer(unsigned char *buffer, int length){ SWFInput input = newSWFInput_buffer(buffer, length); input->destroy = SWFInput_buffer_dtor;#if TRACK_ALLOCS input->gcnode = ming_gc_add_node(input, (dtorfunctype) destroySWFInput);#endif return input;}/* same as above but copy buffer and needs to be freed */SWFInputnewSWFInput_bufferCopy(unsigned char *buffer, int length){ unsigned char *data = (unsigned char *)malloc(length); if(data == NULL) return NULL; memcpy(data, buffer, length); SWFInput input = newSWFInput_buffer(data, length); input->destroy = SWFInput_buffer_dtor;#if TRACK_ALLOCS input->gcnode = ming_gc_add_node(input, (dtorfunctype) destroySWFInput);#endif return input;}/* SWFInput_stream */#define INPUTSTREAM_INCREMENT 32768#define MAX_INPUTSTREAM (32*1024*1024) // 32 mbstruct SWFInputStreamData{ FILE *file; unsigned char *buffer;};static voidSWFInput_stream_seek(SWFInput input, long offset, int whence){ int len, l, readOffset; struct SWFInputStreamData *data; if ( whence == SEEK_CUR ) input->offset = input->offset + offset; else if ( whence == SEEK_SET ) input->offset = offset; else if ( whence == SEEK_END ) { /* suck data until EOF */ /* XXX - might want to put a limit on how much we suck (ha) */ while ( SWFInput_getChar(input) != EOF ) if (input->length > MAX_INPUTSTREAM) break; input->offset = input->length - offset; } if (input->offset < 0) input->offset = 0; if ( input->offset < input->length ) return; /* now slurp up as much data as we need to get here */ len = (((input->offset - input->length)/INPUTSTREAM_INCREMENT)+1) * INPUTSTREAM_INCREMENT; readOffset = input->length; input->length += len; data = (struct SWFInputStreamData*) input->data; data->buffer = (unsigned char*)realloc(data->buffer, sizeof(unsigned char) * (input->length + len)); l=1; /* just to initialize to something */ while((len > 0) && (l > 0)) { l = fread(data->buffer, sizeof(unsigned char), len, data->file); len -= l; readOffset += l; }}static intSWFInput_stream_getChar(SWFInput input){ struct SWFInputStreamData *data = (struct SWFInputStreamData *) input->data; if (input->offset >= MAX_INPUTSTREAM) return EOF; if ( input->offset == input->length ) { /* fetch from stream, save in buffer */ FILE *f = data->file; int c = fgetc(f); ++input->offset; if ( c != EOF ) { if ( input->length % INPUTSTREAM_INCREMENT == 0 ) { data->buffer = (unsigned char*) realloc(data->buffer, sizeof(unsigned char) * (input->length + INPUTSTREAM_INCREMENT)); } data->buffer[input->length] = c; ++input->length; } return c; } else if ( input->offset < input->length ) { /* fetch from buffer */ return data->buffer[input->offset++]; } else return EOF;}static intSWFInput_stream_read(SWFInput input, unsigned char* buffer, int count){ struct SWFInputStreamData *data = (struct SWFInputStreamData *) input->data; int need = input->offset + count - input->length; if ( need > 0 ) { int num; data->buffer = (unsigned char*) realloc(data->buffer, sizeof(unsigned char) * INPUTSTREAM_INCREMENT * (((input->offset + count) / INPUTSTREAM_INCREMENT) + 1)); num = fread(data->buffer + input->length, sizeof(unsigned char), need, data->file); input->length += num; } if ( count > input->length - input->offset ) count = input->length - input->offset; memcpy(buffer, data->buffer + input->offset, count); return count;}static voidSWFInput_stream_dtor(SWFInput input){ free(input->data);#if TRACK_ALLOCS ming_gc_remove_node(input->gcnode);#endif free(input);}static int SWFInput_stream_eof(SWFInput input){ return ((input->offset >= input->length) ? feof((FILE *)input->data) : 0);}SWFInputnewSWFInput_stream(FILE* f){ SWFInput input = (SWFInput)malloc(sizeof(struct SWFInput_s)); /* If malloc failed, return NULL to signify this */ if (NULL == input) return NULL; struct SWFInputStreamData *data = (struct SWFInputStreamData *)malloc(sizeof(struct SWFInputStreamData)); /* If malloc failed, free memory allocated for input and return NULL to signify the failure */ if (NULL == data) { free(input); return NULL; } input->getChar = SWFInput_stream_getChar; input->destroy = SWFInput_stream_dtor; input->eof = SWFInput_stream_eof; input->read = SWFInput_stream_read; input->seek = SWFInput_stream_seek; input->offset = 0; input->length = 0; input->buffer = 0; input->bufbits = 0; data->file = f; data->buffer = NULL; input->data = (void *)data;#if TRACK_ALLOCS input->gcnode = ming_gc_add_node(input, (dtorfunctype) destroySWFInput);#endif return input;}struct SWFInputPtr{ SWFInput input; unsigned int offset;};static intSWFInput_input_getChar(SWFInput input){ struct SWFInputPtr *ptr; int old_offset, result; if ( input->offset >= input->length ) return EOF; ptr = (struct SWFInputPtr *)input->data; old_offset = SWFInput_tell(ptr->input); SWFInput_seek(ptr->input, ptr->offset + input->offset, SEEK_SET); result = SWFInput_getChar(ptr->input); input->offset++; SWFInput_seek(ptr->input, old_offset, SEEK_SET); return result;}static voidSWFInput_input_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 ) input->offset = max(0, input->length - offset); else if ( whence == SEEK_SET ) input->offset = min(input->length, offset);}static intSWFInput_input_eof(SWFInput input){ return input->offset >= input->length;}static voidSWFInput_input_dtor(SWFInput input){ free(input->data);#if TRACK_ALLOCS ming_gc_remove_node(input->gcnode);#endif free(input);}static intSWFInput_input_read(SWFInput input, unsigned char* buffer, int count){ int ret; struct SWFInputPtr *ptr; int old_offset; if ( count > input->length - input->offset ) count = input->length - input->offset; ptr = (struct SWFInputPtr *)input->data; old_offset = SWFInput_tell(ptr->input); SWFInput_seek(ptr->input, ptr->offset + input->offset, SEEK_SET); ret = SWFInput_read((SWFInput)ptr->input, buffer, count); if(ret != count) SWF_warn("SWFInput_input_read: ret %i, count %i\n", ret, count); input->offset += count; SWFInput_seek(ptr->input, old_offset, SEEK_SET); return count;}SWFInputnewSWFInput_input(SWFInput in, unsigned int length){ SWFInput input; struct SWFInputPtr *data; if (in == NULL) return NULL; input = (SWFInput)malloc(sizeof(struct SWFInput_s)); /* If malloc failed, return NULL to signify this */ if (NULL == input) return NULL; input->getChar = SWFInput_input_getChar; input->destroy = SWFInput_input_dtor; input->eof = SWFInput_input_eof; input->read = SWFInput_input_read; input->seek = SWFInput_input_seek; data = (struct SWFInputPtr *)malloc(sizeof(struct SWFInputPtr)); /* If malloc failed, free memory allocated for input and return NULL to signify the failure */ if (NULL == data) { free(input); return NULL; } data->offset = SWFInput_tell(in); data->input = in; input->offset = 0; input->length = length; input->data = (void *)data; input->buffer = 0; input->bufbits = 0;#if TRACK_ALLOCS input->gcnode = ming_gc_add_node(input, (dtorfunctype) destroySWFInput);#endif return input;}/* * Local variables: * tab-width: 2 * c-basic-offset: 2 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -