3 #include "funcprotos.h"
5 #include "workarounds.h"
9 static int read_type(char *data, unsigned char *type)
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]))
26 static unsigned long read_size(char *data)
29 unsigned long a, b, c, d;
31 a = (unsigned char)data[0];
32 b = (unsigned char)data[1];
33 c = (unsigned char)data[2];
34 d = (unsigned char)data[3];
36 result = (a << 24) | (b << 16) | (c << 8) | d;
38 // extended header is size 1
39 // if(result < HEADER_LENGTH) result = HEADER_LENGTH;
43 static int64_t read_size64(char *data)
45 uint64_t result, a, b, c, d, e, f, g, h;
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];
65 if(result < HEADER_LENGTH) result = HEADER_LENGTH;
66 return (int64_t)result;
69 static int reset(quicktime_atom_t *atom)
72 atom->type[0] = atom->type[1] = atom->type[2] = atom->type[3] = atom->type[4] = 0;
76 int quicktime_atom_read_header(quicktime_t *file, quicktime_atom_t *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];
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);
103 atom->start = quicktime_position(file);
105 if(!quicktime_read_data(file, header, HEADER_LENGTH)) return 1;
106 result = read_type(header, atom->type);
107 atom->size = read_size(header);
108 atom->end = atom->start + atom->size;
110 printf("quicktime_atom_read_header 1 %c%c%c%c start=0x%jx"
111 " size=0x%jx end=0x%jx ftell %jx %jx\n",
112 atom->type[0], atom->type[1], atom->type[2], atom->type[3],
113 atom->start, atom->size, atom->end,
115 (int64_t)FTELL(file->stream));
117 /* Skip placeholder atom */
118 if(quicktime_match_32((char*)atom->type, "wide"))
120 atom->start = quicktime_position(file);
122 if(!quicktime_read_data(file, header, HEADER_LENGTH)) return 1;
123 result = read_type(header, atom->type);
127 /* Wrapper ended. Get new atom size */
128 atom->size = read_size(header);
130 atom->end = atom->start + atom->size;
133 /* Get extended size */
136 if(!quicktime_read_data(file, header, HEADER_LENGTH)) return 1;
137 atom->size = read_size64(header);
138 atom->end = atom->start + atom->size;
140 * printf("quicktime_atom_read_header 2 %c%c%c%c start %llx size %llx end %llx ftell %llx\n",
141 * atom->type[0], atom->type[1], atom->type[2], atom->type[3],
142 * atom->start, atom->size, atom->end,
143 * file->file_position);
152 int quicktime_atom_write_header64(quicktime_t *file, quicktime_atom_t *atom, char *text)
155 atom->start = quicktime_position(file);
157 result = !quicktime_write_int32(file, 1);
158 if(!result) result = !quicktime_write_char32(file, text);
159 if(!result) result = !quicktime_write_int64(file, 0);
165 int quicktime_atom_write_header(quicktime_t *file,
166 quicktime_atom_t *atom,
174 atom->start = quicktime_position(file) + 8;
175 result = !quicktime_write_char32(file, text);
176 if(!result) result = !quicktime_write_int32_le(file, 0);
181 atom->start = quicktime_position(file);
182 result = !quicktime_write_int32(file, 0);
183 if(!result) result = !quicktime_write_char32(file, text);
190 void quicktime_atom_write_footer(quicktime_t *file, quicktime_atom_t *atom)
192 atom->end = quicktime_position(file);
195 quicktime_set_position(file, atom->start - 4);
196 quicktime_write_int32_le(file, atom->end - atom->start);
197 atom->size = atom->end - atom->start;
201 // We should calculate atom->size here also... it is used in trak.c FIXME
202 // I don't know internals of the quicktime to know what is proper calculation - with or wirhout header?
205 quicktime_set_position(file, atom->start + 8);
206 //printf("quicktime_atom_write_footer %llx %llx %llx %llx\n", file->total_length, file->file_position, atom->start, atom->end);
207 quicktime_write_int64(file, atom->end - atom->start);
211 quicktime_set_position(file, atom->start);
212 quicktime_write_int32(file, atom->end - atom->start);
215 quicktime_set_position(file, atom->end);
218 int quicktime_atom_is(quicktime_atom_t *atom, char *typ)
220 unsigned char *type = (unsigned char *)typ;
221 return atom->type[0] == type[0] && atom->type[1] == type[1] &&
222 atom->type[2] == type[2] && atom->type[3] == type[3] ? 1 : 0;
225 int quicktime_atom_skip(quicktime_t *file, quicktime_atom_t *atom)
227 if(atom->start == atom->end) atom->end++;
228 return quicktime_set_position(file, atom->end);