2 * $Id: box_manager.c 44 2011-02-15 12:32:29Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
35 #include "box_manager.h"
38 #include "fcgi_stdio.h"
39 #define logstream FCGI_stdout
41 #define FCGI_stdout stdout
42 #define FCGI_stderr stderr
43 #define logstream stderr
46 boxlist_param_t * gene_boxlist()
48 boxlist_param_t *boxlist;
50 boxlist = (boxlist_param_t *)malloc( sizeof(boxlist_param_t));
52 boxlist->first = NULL;
58 boxlist_param_t * get_boxstructure( int fd, Byte8_t offset, Byte8_t length)
60 boxlist_param_t *boxlist;
67 if(!(box = gene_boxbyOffset( fd, pos)))
73 boxlist = gene_boxlist();
74 insert_box_into_list( box, boxlist);
75 }while( pos < offset+length);
80 box_param_t * gene_boxbyOffset( int fd, Byte8_t offset)
83 Byte8_t boxlen, headlen;
88 if(!(data = fetch_bytes( fd, offset, 8))){
89 fprintf( FCGI_stderr, "Error: error in gene_boxbyOffset( %d, %lld)\n", fd, offset);
94 boxlen = (Byte8_t)big4(data);
95 boxtype = (char *)(data+4);
97 // box type constraint
98 if( !isalpha(boxtype[0]) || !isalpha(boxtype[1]) ||
99 (!isalnum(boxtype[2])&&!isblank(boxtype[2])) ||
100 (!isalpha(boxtype[3])&&!isblank(boxtype[3]))){
109 if((data2 = fetch_bytes( fd, offset+8, 8))){
110 boxlen = big8(data2);
114 fprintf( FCGI_stderr, "Error: error in gene_boxbyOffset( %d, %lld)\n", fd, offset);
119 box = (box_param_t *)malloc( sizeof( box_param_t));
121 box->offset = offset;
122 box->headlen = headlen;
123 box->length = boxlen;
124 strncpy( box->type, boxtype, 4);
130 box_param_t * gene_boxbyOffinStream( Byte_t *stream, Byte8_t offset)
132 Byte8_t boxlen, headlen;
136 // read LBox and TBox
138 boxlen = (Byte8_t)big4( stream);
139 boxtype = (char *)( stream+4);
141 // box type constraint
142 if( !isalpha(boxtype[0]) || !isalpha(boxtype[1]) ||
143 (!isalnum(boxtype[2])&&!isblank(boxtype[2])) ||
144 (!isalpha(boxtype[3])&&!isblank(boxtype[3]))){
150 boxlen = big8( stream+8); // read XLBox
152 box = (box_param_t *)malloc( sizeof( box_param_t));
154 box->offset = offset;
155 box->headlen = headlen;
156 box->length = boxlen;
157 strncpy( box->type, boxtype, 4);
164 box_param_t * gene_boxbyType( int fd, Byte8_t offset, Byte8_t length, char TBox[])
168 Byte8_t boxlen, headlen;
170 box_param_t *foundbox;
173 if( length==0){ // set the max length
174 if( (length = get_filesize( fd) - offset) <= 0)
179 while( pos < offset+length-7){ // LBox+TBox-1=7
181 // read LBox and TBox
182 if((data = fetch_bytes( fd, pos, 8))){
184 boxlen = (Byte8_t)big4(data);
185 boxtype = (char *)(data+4);
191 if((data2 = fetch_bytes( fd, pos+8, 8))){
192 boxlen = big8(data2);
196 fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %lld, %lld, %s)\n", fd, offset, length, TBox);
200 if( strncmp ( boxtype, TBox, 4) == 0){
201 foundbox = (box_param_t *)malloc( sizeof( box_param_t));
203 foundbox->offset = pos;
204 foundbox->headlen = headlen;
205 foundbox->length = boxlen;
206 strncpy( foundbox->type, TBox, 4);
207 foundbox->next = NULL;
214 fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %lld, %lld, %s)\n", fd, offset, length, TBox);
219 fprintf( FCGI_stderr, "Error: Box %s not found\n", TBox);
224 box_param_t * gene_boxbyTypeinStream( Byte_t *stream, Byte8_t offset, Byte8_t length, char TBox[])
228 Byte8_t boxlen, headlen;
230 box_param_t *foundbox;
233 if( length<=0){ // set the max length
234 fprintf( FCGI_stderr, "func gene_boxbyTypeinStream(), max length must be more than 0\n");
239 while( pos < offset+length-7){ // LBox+TBox-1=7
241 // read LBox and TBox
244 boxlen = (Byte8_t)big4(data);
245 boxtype = (char *)(data+4);
250 boxlen = big8( data+8);
253 if( strncmp ( boxtype, TBox, 4) == 0){
254 foundbox = (box_param_t *)malloc( sizeof( box_param_t));
256 foundbox->offset = pos;
257 foundbox->headlen = headlen;
258 foundbox->length = boxlen;
259 strncpy( foundbox->type, TBox, 4);
260 foundbox->next = NULL;
265 fprintf( FCGI_stderr, "Error: Box %s not found\n", TBox);
270 box_param_t * gene_childboxbyOffset( box_param_t *superbox, Byte8_t offset)
272 return gene_boxbyOffset( superbox->fd, get_DBoxoff( superbox)+offset);
275 box_param_t * gene_childboxbyType( box_param_t *superbox, Byte8_t offset, char TBox[])
277 return gene_boxbyType( superbox->fd, get_DBoxoff( superbox)+offset, get_DBoxlen( superbox)-offset, TBox);
280 Byte8_t get_DBoxoff( box_param_t *box)
282 return box->offset+box->headlen;
285 Byte8_t get_DBoxlen( box_param_t *box)
287 return box->length-box->headlen;
290 Byte_t * fetch_headbytes( box_param_t *box)
292 return fetch_bytes( box->fd, box->offset, box->headlen);
295 Byte_t * fetch_DBoxbytes( box_param_t *box, long offset, int size)
297 return fetch_bytes( box->fd, get_DBoxoff( box)+offset, size);
300 Byte_t fetch_DBox1byte( box_param_t *box, long offset)
302 return fetch_1byte( box->fd, get_DBoxoff( box)+offset);
305 Byte2_t fetch_DBox2bytebigendian( box_param_t *box, long offset)
307 return fetch_2bytebigendian( box->fd, get_DBoxoff( box)+offset);
310 Byte4_t fetch_DBox4bytebigendian( box_param_t *box, long offset)
312 return fetch_4bytebigendian( box->fd, get_DBoxoff( box)+offset);
315 Byte8_t fetch_DBox8bytebigendian( box_param_t *box, long offset)
317 return fetch_8bytebigendian( box->fd, get_DBoxoff( box)+offset);
320 box_param_t * search_box( char type[], boxlist_param_t *boxlist)
322 box_param_t *foundbox;
324 foundbox = boxlist->first;
326 while( foundbox != NULL){
328 if( strncmp( type, foundbox->type, 4) == 0)
331 foundbox = foundbox->next;
333 fprintf( FCGI_stderr, "Error: Box %s not found\n", type);
338 void print_box( box_param_t *box)
340 fprintf( logstream, "box info:\n"
342 "\t offset: %lld %#llx\n"
343 "\t header length: %d\n"
344 "\t length: %lld %#llx\n", box->type, box->offset, box->offset, box->headlen, box->length, box->length);
347 void print_allbox( boxlist_param_t *boxlist)
354 ptr = boxlist->first;
356 fprintf( logstream, "no box\n");
358 fprintf( logstream, "all box info: \n");
365 void delete_box_in_list( box_param_t **box, boxlist_param_t *boxlist)
369 if( *box == boxlist->first)
370 boxlist->first = (*box)->next;
372 ptr = boxlist->first;
373 while( ptr->next != *box){
376 ptr->next = (*box)->next;
378 if( *box == boxlist->last)
384 void delete_box_in_list_by_type( char type[], boxlist_param_t *boxlist)
388 box = search_box( type, boxlist);
389 delete_box_in_list( &box, boxlist);
392 void delete_boxlist( boxlist_param_t **boxlist)
394 box_param_t *boxPtr, *boxNext;
399 boxPtr = (*boxlist)->first;
400 while( boxPtr != NULL){
401 boxNext=boxPtr->next;
408 void insert_box_into_list( box_param_t *box, boxlist_param_t *boxlist)
411 boxlist->last->next = box;
413 boxlist->first = box;