"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/atom.c" (9 Jan 2007, 5526 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <ctype.h>
    2 #include <stdio.h>
    3 #include "funcprotos.h"
    4 #include "quicktime.h"
    5 #include "workarounds.h"
    6 
    7 
    8 
    9 static int read_type(char *data, char *type)
   10 {
   11     type[0] = data[4];
   12     type[1] = data[5];
   13     type[2] = data[6];
   14     type[3] = data[7];
   15     type[4] = 0;
   16 
   17 /*printf("%c%c%c%c ", type[0], type[1], type[2], type[3]); */
   18 /* need this for quicktime_check_sig */
   19     if(isalpha(type[0]) && isalpha(type[1]) && isalpha(type[2]) && isalpha(type[3]))
   20     return 0;
   21     else
   22     return 1;
   23 }
   24 
   25 
   26 static unsigned long read_size(char *data)
   27 {
   28     unsigned long result;
   29     unsigned long a, b, c, d;
   30     
   31     a = (unsigned char)data[0];
   32     b = (unsigned char)data[1];
   33     c = (unsigned char)data[2];
   34     d = (unsigned char)data[3];
   35 
   36     result = (a << 24) | (b << 16) | (c << 8) | d;
   37 
   38 // extended header is size 1
   39 //  if(result < HEADER_LENGTH) result = HEADER_LENGTH;
   40     return result;
   41 }
   42 
   43 static int64_t read_size64(char *data)
   44 {
   45     uint64_t result, a, b, c, d, e, f, g, h;
   46 
   47     a = (unsigned char)data[0];
   48     b = (unsigned char)data[1];
   49     c = (unsigned char)data[2];
   50     d = (unsigned char)data[3];
   51     e = (unsigned char)data[4];
   52     f = (unsigned char)data[5];
   53     g = (unsigned char)data[6];
   54     h = (unsigned char)data[7];
   55 
   56     result = (a << 56) | 
   57         (b << 48) | 
   58         (c << 40) | 
   59         (d << 32) | 
   60         (e << 24) | 
   61         (f << 16) | 
   62         (g << 8) | 
   63         h;
   64 
   65     if(result < HEADER_LENGTH) result = HEADER_LENGTH;
   66     return (int64_t)result;
   67 }
   68 
   69 static int reset(quicktime_atom_t *atom)
   70 {
   71     atom->end = 0;
   72     atom->type[0] = atom->type[1] = atom->type[2] = atom->type[3] = atom->type[4] = 0;
   73     return 0;
   74 }
   75 
   76 int quicktime_atom_read_header(quicktime_t *file, quicktime_atom_t *atom)
   77 {
   78     int result = 0;
   79     char header[10];
   80     int debug = 0;
   81 
   82     if(file->use_avi)
   83     {
   84         reset(atom);
   85         atom->start = quicktime_position(file);
   86         if(!quicktime_read_data(file, header, HEADER_LENGTH)) return 1;
   87         atom->type[0] = header[0];
   88         atom->type[1] = header[1];
   89         atom->type[2] = header[2];
   90         atom->type[3] = header[3];
   91         atom->type[4] = 0;
   92         atom->size = 
   93             (((unsigned char)header[4])      ) |
   94             (((unsigned char)header[5]) << 8 ) |
   95             (((unsigned char)header[6]) << 16) |
   96             (((unsigned char)header[7]) << 24);
   97         atom->end = quicktime_add3(atom->start, atom->size, 8);
   98     }
   99     else
  100     {
  101         int64_t size2;
  102 
  103         reset(atom);
  104 
  105         atom->start = quicktime_position(file);
  106 
  107         if(!quicktime_read_data(file, header, HEADER_LENGTH)) return 1;
  108         result = read_type(header, atom->type);
  109         atom->size = read_size(header);
  110         atom->end = atom->start + atom->size;
  111         if(debug)
  112             printf("quicktime_atom_read_header 1 %c%c%c%c start=0x%llx size=0x%llx end=0x%llx ftell %llx %llx\n", 
  113                 atom->type[0], atom->type[1], atom->type[2], atom->type[3],
  114                 atom->start, atom->size, atom->end,
  115                 file->file_position,
  116                 (int64_t)FTELL(file->stream));
  117 
  118 /* Skip placeholder atom */
  119         if(quicktime_match_32(atom->type, "wide"))
  120         {
  121             atom->start = quicktime_position(file);
  122             reset(atom);
  123             if(!quicktime_read_data(file, header, HEADER_LENGTH)) return 1;
  124             result = read_type(header, atom->type);
  125             atom->size -= 8;
  126             if(atom->size <= 0)
  127             {
  128 /* Wrapper ended.  Get new atom size */
  129                 atom->size = read_size(header);
  130             }
  131             atom->end = atom->start + atom->size;
  132         }
  133         else
  134 /* Get extended size */
  135         if(atom->size == 1)
  136         {
  137             if(!quicktime_read_data(file, header, HEADER_LENGTH)) return 1;
  138             atom->size = read_size64(header);
  139             atom->end = atom->start + atom->size;
  140 /*
  141  * printf("quicktime_atom_read_header 2 %c%c%c%c start %llx size %llx end %llx ftell %llx\n", 
  142  *  atom->type[0], atom->type[1], atom->type[2], atom->type[3],
  143  *  atom->start, atom->size, atom->end,
  144  *  file->file_position);
  145  */
  146         }
  147     }
  148 
  149 
  150     return result;
  151 }
  152 
  153 int quicktime_atom_write_header64(quicktime_t *file, quicktime_atom_t *atom, char *text)
  154 {
  155     int result = 0;
  156     atom->start = quicktime_position(file);
  157 
  158     result = !quicktime_write_int32(file, 1);
  159     if(!result) result = !quicktime_write_char32(file, text);
  160     if(!result) result = !quicktime_write_int64(file, 0);
  161 
  162     atom->use_64 = 1;
  163     return result;
  164 }
  165 
  166 int quicktime_atom_write_header(quicktime_t *file, 
  167     quicktime_atom_t *atom, 
  168     char *text)
  169 {
  170     int result = 0;
  171 
  172     if(file->use_avi)
  173     {
  174         reset(atom);
  175         atom->start = quicktime_position(file) + 8;
  176         result = !quicktime_write_char32(file, text);
  177         if(!result) result = !quicktime_write_int32_le(file, 0);
  178         atom->use_64 = 0;
  179     }
  180     else
  181     {
  182         atom->start = quicktime_position(file);
  183         result = !quicktime_write_int32(file, 0);
  184         if(!result) result = !quicktime_write_char32(file, text);
  185         atom->use_64 = 0;
  186     }
  187 
  188     return result;
  189 }
  190 
  191 void quicktime_atom_write_footer(quicktime_t *file, quicktime_atom_t *atom)
  192 {
  193     atom->end = quicktime_position(file);
  194     if(file->use_avi)
  195     {
  196         quicktime_set_position(file, atom->start - 4);
  197         quicktime_write_int32_le(file, atom->end - atom->start);
  198     }
  199     else
  200     {
  201         if(atom->use_64)
  202         {
  203             quicktime_set_position(file, atom->start + 8);
  204 //printf("quicktime_atom_write_footer %llx %llx %llx %llx\n", file->total_length, file->file_position, atom->start, atom->end);
  205             quicktime_write_int64(file, atom->end - atom->start);
  206         }
  207         else
  208         {
  209             quicktime_set_position(file, atom->start);
  210             quicktime_write_int32(file, atom->end - atom->start);
  211         }
  212     }
  213 
  214     quicktime_set_position(file, atom->end);
  215 }
  216 
  217 int quicktime_atom_is(quicktime_atom_t *atom, unsigned char *type)
  218 {
  219     if(atom->type[0] == type[0] &&
  220         atom->type[1] == type[1] &&
  221         atom->type[2] == type[2] &&
  222         atom->type[3] == type[3])
  223     return 1;
  224     else
  225     return 0;
  226 }
  227 
  228 int quicktime_atom_skip(quicktime_t *file, quicktime_atom_t *atom)
  229 {
  230     if(atom->start == atom->end) atom->end++;
  231     return quicktime_set_position(file, atom->end);
  232 }