1 | #ifndef __EBML_H |
---|
2 | #define __EBML_H |
---|
3 | |
---|
4 | #ifdef _WIN32 |
---|
5 | #include <windows.h> |
---|
6 | #endif |
---|
7 | |
---|
8 | #include <sys/types.h> |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #ifndef _WIN32 |
---|
12 | #include <unistd.h> |
---|
13 | #else |
---|
14 | #include <io.h> |
---|
15 | #include <fcntl.h> |
---|
16 | #endif |
---|
17 | #include <string.h> |
---|
18 | #include <sys/stat.h> |
---|
19 | #include <fcntl.h> |
---|
20 | #include <stdexcept> |
---|
21 | #include <map> |
---|
22 | #include <string> |
---|
23 | |
---|
24 | #ifndef _WIN32 |
---|
25 | #define O_BINARY 0 |
---|
26 | #else |
---|
27 | typedef unsigned char u_int8_t; |
---|
28 | typedef unsigned short u_int16_t; |
---|
29 | typedef unsigned long u_int32_t; |
---|
30 | typedef unsigned long long u_int64_t; |
---|
31 | #endif |
---|
32 | |
---|
33 | #ifndef O_LARGEFILE |
---|
34 | #define O_LARGEFILE 0 |
---|
35 | #endif |
---|
36 | |
---|
37 | //#ifdef _WIN32 |
---|
38 | #ifdef _MSC_VER |
---|
39 | inline int strcasecmp(const char* s1,const char* s2) { return lstrcmpiA(s1,s2); } |
---|
40 | #endif |
---|
41 | |
---|
42 | |
---|
43 | namespace ebml |
---|
44 | { |
---|
45 | class exception : public std::exception |
---|
46 | { |
---|
47 | protected: |
---|
48 | std::string _what; |
---|
49 | public: |
---|
50 | explicit exception(const std::string& s):_what(s) {} |
---|
51 | virtual ~exception(void) throw() {} |
---|
52 | virtual const char* what(void) const throw() {return _what.c_str();} |
---|
53 | }; |
---|
54 | |
---|
55 | |
---|
56 | class track |
---|
57 | { |
---|
58 | public: |
---|
59 | u_int32_t id; |
---|
60 | std::string codec; |
---|
61 | std::string lang; |
---|
62 | u_int32_t timecode; |
---|
63 | u_int32_t duration; |
---|
64 | |
---|
65 | track(void):id(0),timecode(-1),duration(0) {} |
---|
66 | }; |
---|
67 | |
---|
68 | class doc |
---|
69 | { |
---|
70 | public: |
---|
71 | std::string doc_type; |
---|
72 | u_int8_t version; |
---|
73 | u_int8_t read_version; |
---|
74 | |
---|
75 | std::map<u_int32_t,ebml::track> tracks; |
---|
76 | u_int32_t blocks; |
---|
77 | |
---|
78 | doc(void):version(0),read_version(0),blocks(0) {} |
---|
79 | }; |
---|
80 | |
---|
81 | |
---|
82 | class stream_base |
---|
83 | { |
---|
84 | public: |
---|
85 | virtual int read(char* p,int l)=0; |
---|
86 | virtual int skip(u_int64_t l)=0; |
---|
87 | int getTag(u_int32_t& tag); |
---|
88 | int getLen(u_int64_t& len); |
---|
89 | }; |
---|
90 | |
---|
91 | |
---|
92 | class stream : public stream_base |
---|
93 | { |
---|
94 | protected: |
---|
95 | int fd; |
---|
96 | |
---|
97 | enum { max_buf_len=2048 }; |
---|
98 | |
---|
99 | char buf[max_buf_len]; |
---|
100 | |
---|
101 | int len,offset; |
---|
102 | public: |
---|
103 | stream(void):fd(-1),len(0),offset(0) {} |
---|
104 | stream(const char* path):fd(-1),len(0),offset(0) { open(path); } |
---|
105 | ~stream(void) { close(); } |
---|
106 | |
---|
107 | bool open(const char* name); |
---|
108 | void close(void); |
---|
109 | int isOpened(void) { return fd==-1?0:1; } |
---|
110 | |
---|
111 | virtual int read(char* p,int l); |
---|
112 | virtual int skip(u_int64_t l); |
---|
113 | }; |
---|
114 | |
---|
115 | |
---|
116 | class segment : public stream_base |
---|
117 | { |
---|
118 | protected: |
---|
119 | stream_base* source; |
---|
120 | u_int64_t length; |
---|
121 | public: |
---|
122 | segment(stream_base* s,u_int64_t len):source(s),length(len) {} |
---|
123 | |
---|
124 | virtual int read(char* p,int l) |
---|
125 | { |
---|
126 | if(l>length) |
---|
127 | l=length; |
---|
128 | |
---|
129 | int n=source->read(p,l); |
---|
130 | |
---|
131 | length-=n; |
---|
132 | |
---|
133 | return n; |
---|
134 | } |
---|
135 | |
---|
136 | virtual int skip(u_int64_t l) |
---|
137 | { |
---|
138 | if(l>length) |
---|
139 | l=length; |
---|
140 | |
---|
141 | if(source->skip(l)) |
---|
142 | return -1; |
---|
143 | |
---|
144 | length-=l; |
---|
145 | |
---|
146 | return 0; |
---|
147 | } |
---|
148 | }; |
---|
149 | |
---|
150 | void init(void); |
---|
151 | void parse(ebml::stream_base* s,doc& m) throw(std::exception); |
---|
152 | } |
---|
153 | |
---|
154 | #endif |
---|
155 | |
---|