add attachment support
[libmkv.git] / src / attachments.c
blob0f730a74b8879cf8f4afee599dd3b21615dbd871
1 /*****************************************************************************
2 * attachments.c:
3 *****************************************************************************
4 * Copyright (C) 2007 libmkv
6 * Authors: John A. Stebbins
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
22 #include "config.h"
23 #include "libmkv.h"
24 #include "matroska.h"
26 int mk_createAttachment(
27 mk_Writer * w,
28 char *name,
29 char *description,
30 char *mime,
31 const void *data,
32 unsigned size)
34 mk_Context *attach;
35 unsigned long file_uid;
37 * Generate a random UID for this Attachment.
38 * NOTE: This probably should be a CRC32 of file data.
39 * In place of being completely random.
41 file_uid = random();
43 printf("2att %p\n", w->attachments);
44 if (w->attachments == NULL) {
45 printf("create attachments context\n");
46 /* Attachments */
47 if ((w->attachments = mk_createContext(w, w->root, MATROSKA_ID_ATTACHMENTS)) == NULL)
48 return -1;
50 /* AttachedFile */
51 printf("create attachedfile context\n");
52 if ((attach = mk_createContext(w, w->attachments, MATROSKA_ID_ATTACHEDFILE)) == NULL)
53 return -1;
54 printf("done attachedfile context\n");
56 /* FileName */
57 CHECK(mk_writeStr(attach, MATROSKA_ID_FILENAME, name));
58 /* FileName */
59 CHECK(mk_writeStr(attach, MATROSKA_ID_FILEDESCRIPTION, description));
60 /* FileMimeType */
61 CHECK(mk_writeStr(attach, MATROSKA_ID_FILEMIMETYPE, mime));
62 /* FileUID */
63 CHECK(mk_writeUInt(attach, MATROSKA_ID_FILEUID, file_uid));
64 /* FileData */
65 CHECK(mk_writeBin(attach, MATROSKA_ID_FILEDATA, data, size));
66 CHECK(mk_closeContext(attach, 0));
68 return 0;
71 int mk_writeAttachments(mk_Writer * w)
73 if (w->attachments == NULL)
74 return -1;
75 CHECK(mk_closeContext(w->attachments, 0));
77 return 0;