Merge other top-level C code
[platform/upstream/libvpx.git] / libmkv / EbmlWriter.c
1 // Copyright (c) 2010 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8
9
10 #include "EbmlWriter.h"
11 #include <stdlib.h>
12 #include <wchar.h>
13 #include <string.h>
14 #include <limits.h>
15 #if defined(_MSC_VER)
16 #define LITERALU64(n) n
17 #else
18 #define LITERALU64(n) n##LLU
19 #endif
20
21 void Ebml_WriteLen(EbmlGlobal *glob, long long val) {
22   // TODO check and make sure we are not > than 0x0100000000000000LLU
23   unsigned char size = 8; // size in bytes to output
24   unsigned long long minVal = LITERALU64(0x00000000000000ff); // mask to compare for byte size
25
26   for (size = 1; size < 8; size ++) {
27     if (val < minVal)
28       break;
29
30     minVal = (minVal << 7);
31   }
32
33   val |= (LITERALU64(0x000000000000080) << ((size - 1) * 7));
34
35   Ebml_Serialize(glob, (void *) &val, sizeof(val), size);
36 }
37
38 void Ebml_WriteString(EbmlGlobal *glob, const char *str) {
39   const size_t size_ = strlen(str);
40   const unsigned long long  size = size_;
41   Ebml_WriteLen(glob, size);
42   // TODO: it's not clear from the spec whether the nul terminator
43   // should be serialized too.  For now we omit the null terminator.
44   Ebml_Write(glob, str, size);
45 }
46
47 void Ebml_WriteUTF8(EbmlGlobal *glob, const wchar_t *wstr) {
48   const size_t strlen = wcslen(wstr);
49
50   // TODO: it's not clear from the spec whether the nul terminator
51   // should be serialized too.  For now we include it.
52   const unsigned long long  size = strlen;
53
54   Ebml_WriteLen(glob, size);
55   Ebml_Write(glob, wstr, size);
56 }
57
58 void Ebml_WriteID(EbmlGlobal *glob, unsigned long class_id) {
59   int len;
60
61   if (class_id >= 0x01000000)
62     len = 4;
63   else if (class_id >= 0x00010000)
64     len = 3;
65   else if (class_id >= 0x00000100)
66     len = 2;
67   else
68     len = 1;
69
70   Ebml_Serialize(glob, (void *)&class_id, sizeof(class_id), len);
71 }
72
73 void Ebml_SerializeUnsigned64(EbmlGlobal *glob, unsigned long class_id, uint64_t ui) {
74   unsigned char sizeSerialized = 8 | 0x80;
75   Ebml_WriteID(glob, class_id);
76   Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
77   Ebml_Serialize(glob, &ui, sizeof(ui), 8);
78 }
79
80 void Ebml_SerializeUnsigned(EbmlGlobal *glob, unsigned long class_id, unsigned long ui) {
81   unsigned char size = 8; // size in bytes to output
82   unsigned char sizeSerialized = 0;
83   unsigned long minVal;
84
85   Ebml_WriteID(glob, class_id);
86   minVal = 0x7fLU; // mask to compare for byte size
87
88   for (size = 1; size < 4; size ++) {
89     if (ui < minVal) {
90       break;
91     }
92
93     minVal <<= 7;
94   }
95
96   sizeSerialized = 0x80 | size;
97   Ebml_Serialize(glob, &sizeSerialized, sizeof(sizeSerialized), 1);
98   Ebml_Serialize(glob, &ui, sizeof(ui), size);
99 }
100 // TODO: perhaps this is a poor name for this id serializer helper function
101 void Ebml_SerializeBinary(EbmlGlobal *glob, unsigned long class_id, unsigned long bin) {
102   int size;
103   for (size = 4; size > 1; size--) {
104     if (bin & 0x000000ff << ((size - 1) * 8))
105       break;
106   }
107   Ebml_WriteID(glob, class_id);
108   Ebml_WriteLen(glob, size);
109   Ebml_WriteID(glob, bin);
110 }
111
112 void Ebml_SerializeFloat(EbmlGlobal *glob, unsigned long class_id, double d) {
113   unsigned char len = 0x88;
114
115   Ebml_WriteID(glob, class_id);
116   Ebml_Serialize(glob, &len, sizeof(len), 1);
117   Ebml_Serialize(glob,  &d, sizeof(d), 8);
118 }
119
120 void Ebml_WriteSigned16(EbmlGlobal *glob, short val) {
121   signed long out = ((val & 0x003FFFFF) | 0x00200000) << 8;
122   Ebml_Serialize(glob, &out, sizeof(out), 3);
123 }
124
125 void Ebml_SerializeString(EbmlGlobal *glob, unsigned long class_id, const char *s) {
126   Ebml_WriteID(glob, class_id);
127   Ebml_WriteString(glob, s);
128 }
129
130 void Ebml_SerializeUTF8(EbmlGlobal *glob, unsigned long class_id, wchar_t *s) {
131   Ebml_WriteID(glob,  class_id);
132   Ebml_WriteUTF8(glob,  s);
133 }
134
135 void Ebml_SerializeData(EbmlGlobal *glob, unsigned long class_id, unsigned char *data, unsigned long data_length) {
136   Ebml_WriteID(glob, class_id);
137   Ebml_WriteLen(glob, data_length);
138   Ebml_Write(glob,  data, data_length);
139 }
140
141 void Ebml_WriteVoid(EbmlGlobal *glob, unsigned long vSize) {
142   unsigned char tmp = 0;
143   unsigned long i = 0;
144
145   Ebml_WriteID(glob, 0xEC);
146   Ebml_WriteLen(glob, vSize);
147
148   for (i = 0; i < vSize; i++) {
149     Ebml_Write(glob, &tmp, 1);
150   }
151 }
152
153 // TODO Serialize Date