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.
34 #include <sys/types.h>
38 #include "box_manager.h"
41 #include "fcgi_stdio.h"
42 #define logstream FCGI_stdout
44 #define FCGI_stdout stdout
45 #define FCGI_stderr stderr
46 #define logstream stderr
49 boxlist_param_t * gene_boxlist()
51 boxlist_param_t *boxlist;
53 boxlist = (boxlist_param_t *)malloc( sizeof(boxlist_param_t));
55 boxlist->first = NULL;
61 boxlist_param_t * get_boxstructure( int fd, Byte8_t offset, Byte8_t length)
63 boxlist_param_t *boxlist;
70 if(!(box = gene_boxbyOffset( fd, pos)))
76 boxlist = gene_boxlist();
77 insert_box_into_list( box, boxlist);
78 }while( pos < offset+length);
83 box_param_t * gene_boxbyOffset( int fd, Byte8_t offset)
86 Byte8_t boxlen, headlen;
91 if(!(data = fetch_bytes( fd, offset, 8))){
92 fprintf( FCGI_stderr, "Error: error in gene_boxbyOffset( %d, %lld)\n", fd, offset);
97 boxlen = (Byte8_t)big4(data);
98 boxtype = (char *)(data+4);
100 // box type constraint
101 if( !isalpha(boxtype[0]) || !isalpha(boxtype[1]) ||
102 (!isalnum(boxtype[2])&&!isblank(boxtype[2])) ||
103 (!isalpha(boxtype[3])&&!isblank(boxtype[3]))){
112 if((data2 = fetch_bytes( fd, offset+8, 8))){
113 boxlen = big8(data2);
117 fprintf( FCGI_stderr, "Error: error in gene_boxbyOffset( %d, %lld)\n", fd, offset);
122 box = (box_param_t *)malloc( sizeof( box_param_t));
124 box->offset = offset;
125 box->headlen = headlen;
126 box->length = boxlen;
127 strncpy( box->type, boxtype, 4);
133 box_param_t * gene_boxbyOffinStream( Byte_t *stream, Byte8_t offset)
135 Byte8_t boxlen, headlen;
139 // read LBox and TBox
141 boxlen = (Byte8_t)big4( stream);
142 boxtype = (char *)( stream+4);
144 // box type constraint
145 if( !isalpha(boxtype[0]) || !isalpha(boxtype[1]) ||
146 (!isalnum(boxtype[2])&&!isblank(boxtype[2])) ||
147 (!isalpha(boxtype[3])&&!isblank(boxtype[3]))){
153 boxlen = big8( stream+8); // read XLBox
155 box = (box_param_t *)malloc( sizeof( box_param_t));
157 box->offset = offset;
158 box->headlen = headlen;
159 box->length = boxlen;
160 strncpy( box->type, boxtype, 4);
167 box_param_t * gene_boxbyType( int fd, Byte8_t offset, Byte8_t length, char TBox[])
171 Byte8_t boxlen, headlen;
173 box_param_t *foundbox;
176 if( length==0){ // set the max length
178 if( fstat( fd, &sb) == -1){
179 fprintf( FCGI_stdout, "Reason: Target broken (fstat error)\r\n");
182 length = (Byte8_t)sb.st_size - offset;
186 while( pos < offset+length-7){ // LBox+TBox-1=7
188 // read LBox and TBox
189 if((data = fetch_bytes( fd, pos, 8))){
191 boxlen = (Byte8_t)big4(data);
192 boxtype = (char *)(data+4);
198 if((data2 = fetch_bytes( fd, pos+8, 8))){
199 boxlen = big8(data2);
203 fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %lld, %lld, %s)\n", fd, offset, length, TBox);
207 if( strncmp ( boxtype, TBox, 4) == 0){
208 foundbox = (box_param_t *)malloc( sizeof( box_param_t));
210 foundbox->offset = pos;
211 foundbox->headlen = headlen;
212 foundbox->length = boxlen;
213 strncpy( foundbox->type, TBox, 4);
214 foundbox->next = NULL;
221 fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %lld, %lld, %s)\n", fd, offset, length, TBox);
226 fprintf( FCGI_stderr, "Error: Box %s not found\n", TBox);
231 box_param_t * gene_boxbyTypeinStream( Byte_t *stream, Byte8_t offset, Byte8_t length, char TBox[])
235 Byte8_t boxlen, headlen;
237 box_param_t *foundbox;
240 if( length<=0){ // set the max length
241 fprintf( FCGI_stderr, "func gene_boxbyTypeinStream(), max length must be more than 0\n");
246 while( pos < offset+length-7){ // LBox+TBox-1=7
248 // read LBox and TBox
251 boxlen = (Byte8_t)big4(data);
252 boxtype = (char *)(data+4);
257 boxlen = big8( data+8);
260 if( strncmp ( boxtype, TBox, 4) == 0){
261 foundbox = (box_param_t *)malloc( sizeof( box_param_t));
263 foundbox->offset = pos;
264 foundbox->headlen = headlen;
265 foundbox->length = boxlen;
266 strncpy( foundbox->type, TBox, 4);
267 foundbox->next = NULL;
272 fprintf( FCGI_stderr, "Error: Box %s not found\n", TBox);
277 box_param_t * gene_childboxbyOffset( box_param_t *superbox, Byte8_t offset)
279 return gene_boxbyOffset( superbox->fd, get_DBoxoff( superbox)+offset);
282 box_param_t * gene_childboxbyType( box_param_t *superbox, Byte8_t offset, char TBox[])
284 return gene_boxbyType( superbox->fd, get_DBoxoff( superbox)+offset, get_DBoxlen( superbox)-offset, TBox);
287 Byte8_t get_DBoxoff( box_param_t *box)
289 return box->offset+box->headlen;
292 Byte8_t get_DBoxlen( box_param_t *box)
294 return box->length-box->headlen;
297 Byte_t * fetch_headbytes( box_param_t *box)
299 return fetch_bytes( box->fd, box->offset, box->headlen);
302 Byte_t * fetch_DBoxbytes( box_param_t *box, long offset, int size)
304 return fetch_bytes( box->fd, get_DBoxoff( box)+offset, size);
307 Byte_t fetch_DBox1byte( box_param_t *box, long offset)
309 return fetch_1byte( box->fd, get_DBoxoff( box)+offset);
312 Byte2_t fetch_DBox2bytebigendian( box_param_t *box, long offset)
314 return fetch_2bytebigendian( box->fd, get_DBoxoff( box)+offset);
317 Byte4_t fetch_DBox4bytebigendian( box_param_t *box, long offset)
319 return fetch_4bytebigendian( box->fd, get_DBoxoff( box)+offset);
322 Byte8_t fetch_DBox8bytebigendian( box_param_t *box, long offset)
324 return fetch_8bytebigendian( box->fd, get_DBoxoff( box)+offset);
327 box_param_t * search_box( char type[], boxlist_param_t *boxlist)
329 box_param_t *foundbox;
331 foundbox = boxlist->first;
333 while( foundbox != NULL){
335 if( strncmp( type, foundbox->type, 4) == 0)
338 foundbox = foundbox->next;
340 fprintf( FCGI_stderr, "Error: Box %s not found\n", type);
345 void print_box( box_param_t *box)
347 fprintf( logstream, "box info:\n"
349 "\t offset: %lld %#llx\n"
350 "\t header length: %d\n"
351 "\t length: %lld %#llx\n", box->type, box->offset, box->offset, box->headlen, box->length, box->length);
354 void print_allbox( boxlist_param_t *boxlist)
361 ptr = boxlist->first;
363 fprintf( logstream, "no box\n");
365 fprintf( logstream, "all box info: \n");
372 void delete_box_in_list( box_param_t **box, boxlist_param_t *boxlist)
376 if( *box == boxlist->first)
377 boxlist->first = (*box)->next;
379 ptr = boxlist->first;
380 while( ptr->next != *box){
383 ptr->next = (*box)->next;
385 if( *box == boxlist->last)
391 void delete_box_in_list_by_type( char type[], boxlist_param_t *boxlist)
395 box = search_box( type, boxlist);
396 delete_box_in_list( &box, boxlist);
399 void delete_boxlist( boxlist_param_t **boxlist)
401 box_param_t *boxPtr, *boxNext;
406 boxPtr = (*boxlist)->first;
407 while( boxPtr != NULL){
408 boxNext=boxPtr->next;
415 void insert_box_into_list( box_param_t *box, boxlist_param_t *boxlist)
418 boxlist->last->next = box;
420 boxlist->first = box;