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