00001 /************************************************************************** 00002 Lightspark, a free flash player implementation 00003 00004 Copyright (C) 2009,2010 Alessandro Pignotti (a.pignotti@sssup.it) 00005 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 **************************************************************************/ 00019 00020 #ifndef _STREAMS_H 00021 #define _STREAMS_H 00022 00023 #include "compat.h" 00024 #include <streambuf> 00025 #include <fstream> 00026 #include <semaphore.h> 00027 #include <inttypes.h> 00028 #include "zlib.h" 00029 00030 class zlib_filter: public std::streambuf 00031 { 00032 private: 00033 bool compressed; 00034 int consumed; 00035 z_stream strm; 00036 char buffer[4096]; 00037 int available; 00038 virtual int provideBuffer(int limit)=0; 00039 bool initialize(); 00040 protected: 00041 char in_buf[4096]; 00042 virtual int_type underflow(); 00043 virtual pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode); 00044 public: 00045 zlib_filter() DLL_PUBLIC; 00046 ~zlib_filter() DLL_PUBLIC; 00047 }; 00048 00049 class DLL_PUBLIC sync_stream: public zlib_filter 00050 { 00051 public: 00052 sync_stream(); 00053 void destroy(); 00054 ~sync_stream(); 00055 uint32_t write(char* buf, int len); 00056 uint32_t getFree(); 00057 private: 00058 int provideBuffer(int limit); 00059 char* buffer; 00060 int head; 00061 int tail; 00062 sem_t mutex; 00063 sem_t notfull; 00064 sem_t notempty; 00065 bool wait_notfull; 00066 bool wait_notempty; 00067 const int buf_size; 00068 bool failed; 00069 }; 00070 00071 class DLL_PUBLIC zlib_file_filter:public zlib_filter 00072 { 00073 private: 00074 FILE* f; 00075 int provideBuffer(int limit); 00076 public: 00077 zlib_file_filter(const char* file_name); 00078 }; 00079 00080 class zlib_bytes_filter:public zlib_filter 00081 { 00082 private: 00083 const uint8_t* buf; 00084 int offset; 00085 int len; 00086 int provideBuffer(int limit); 00087 public: 00088 zlib_bytes_filter(const uint8_t* b, int l); 00089 }; 00090 00091 #endif
1.6.3