1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogg/ogg.h>
#ifdef NEED_FSEEKO
#include <sys/stat.h>
#include <errno.h>
#endif
#include "common.h"
void _die(const char *s, const char *file, int line) {
fprintf(stderr, "die @ %s/%d : %s\n", file, line, s);
exit(1);
}
ogg_packet *duplicate_ogg_packet(ogg_packet *src) {
ogg_packet *dst;
dst = (ogg_packet *)malloc(sizeof(ogg_packet));
if (dst == NULL)
die("malloc");
memcpy(dst, src, sizeof(ogg_packet));
dst->packet = (unsigned char *)malloc(src->bytes);
if (dst->packet == NULL)
die("malloc");
memcpy(dst->packet, src->packet, src->bytes);
return dst;
}
char **dup_comments(char **comments) {
char **new_comments;
int nc;
if (comments == NULL)
return NULL;
for (nc = 0; comments[nc] != NULL; nc++)
;
new_comments = (char **)malloc(sizeof(char *) * nc + 1);
if (new_comments == NULL)
die("malloc");
for (nc = 0; comments[nc] != NULL; nc++) {
new_comments[nc] = strdup(comments[nc]);
if (new_comments[nc] == NULL)
die("strdup");
}
new_comments[nc] = NULL;
return new_comments;
}
void free_comments(char **comments) {
int i;
if (comments != NULL) {
for (i = 0; comments[i] != NULL; i++)
free(comments[i]);
free(comments);
}
}
u_int16_t get_uint16(const void *buf) {
u_int16_t ret;
unsigned char *tmp;
tmp = (unsigned char *)buf;
ret = tmp[1] & 0xff;
ret = (ret << 8) + (tmp[0] & 0xff);
return ret;
}
u_int32_t get_uint32(const void *buf) {
u_int32_t ret;
unsigned char *tmp;
tmp = (unsigned char *)buf;
ret = tmp[3] & 0xff;
ret = (ret << 8) + (tmp[2] & 0xff);
ret = (ret << 8) + (tmp[1] & 0xff);
ret = (ret << 8) + (tmp[0] & 0xff);
return ret;
}
u_int64_t get_uint64(const void *buf) {
u_int64_t ret;
unsigned char *tmp;
tmp = (unsigned char *) buf;
ret = tmp[7] & 0xff;
ret = (ret << 8) + (tmp[6] & 0xff);
ret = (ret << 8) + (tmp[5] & 0xff);
ret = (ret << 8) + (tmp[4] & 0xff);
ret = (ret << 8) + (tmp[3] & 0xff);
ret = (ret << 8) + (tmp[2] & 0xff);
ret = (ret << 8) + (tmp[1] & 0xff);
ret = (ret << 8) + (tmp[0] & 0xff);
return ret;
}
void put_uint16(void *buf, u_int16_t val) {
unsigned char *tmp;
tmp = (unsigned char *) buf;
tmp[0] = val & 0xff;
tmp[1] = (val >>= 8) & 0xff;
}
void put_uint32(void *buf, u_int32_t val) {
unsigned char *tmp;
tmp = (unsigned char *) buf;
tmp[0] = val & 0xff;
tmp[1] = (val >>= 8) & 0xff;
tmp[2] = (val >>= 8) & 0xff;
tmp[3] = (val >>= 8) & 0xff;
}
void put_uint64(void *buf, u_int64_t val) {
unsigned char *tmp;
tmp = (unsigned char *) buf;
tmp[0] = val & 0xff;
tmp[1] = (val >>= 8) & 0xff;
tmp[2] = (val >>= 8) & 0xff;
tmp[3] = (val >>= 8) & 0xff;
tmp[4] = (val >>= 8) & 0xff;
tmp[5] = (val >>= 8) & 0xff;
tmp[6] = (val >>= 8) & 0xff;
tmp[7] = (val >>= 8) & 0xff;
}
#ifdef NEED_FSEEKO
/*
* On BSD/OS and NetBSD, off_t and fpos_t are the same. Standards
* say off_t is an arithmetic type, but not necessarily integral,
* while fpos_t might be neither.
*/
int fseeko(FILE *stream, off_t offset, int whence) {
off_t floc;
struct stat filestat;
switch (whence) {
case SEEK_CUR:
flockfile(stream);
if (fgetpos(stream, &floc) != 0)
goto failure;
floc += offset;
if (fsetpos(stream, &floc) != 0)
goto failure;
funlockfile(stream);
return 0;
break;
case SEEK_SET:
if (fsetpos(stream, &offset) != 0)
return -1;
return 0;
break;
case SEEK_END:
flockfile(stream);
if (fstat(fileno(stream), &filestat) != 0)
goto failure;
floc = filestat.st_size;
if (fsetpos(stream, &floc) != 0)
goto failure;
funlockfile(stream);
return 0;
break;
default:
errno = EINVAL;
return -1;
}
failure:
funlockfile(stream);
return -1;
}
off_t ftello(FILE *stream) {
off_t floc;
if (fgetpos(stream, &floc) != 0)
return -1;
return floc;
}
#endif /* NEED_FSEEKO */
#define COPY(m, s) memcpy(&dst->m, &src->m, s)
void copy_headers(stream_header *dst, old_stream_header *src, int size) {
if (size == sizeof(old_stream_header)) {
COPY(streamtype[0], 8);
COPY(subtype[0], 4);
COPY(size, 4);
COPY(time_unit, 8);
COPY(samples_per_unit, 8);
COPY(default_len, 4);
COPY(buffersize, 4);
COPY(bits_per_sample, 2);
COPY(sh, sizeof(stream_header_video));
} else
memcpy(dst, src, size);
}
|