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