forked from McZonk/CoreMediaPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMPBlockBuffer+Text.c
177 lines (143 loc) · 4.23 KB
/
CMPBlockBuffer+Text.c
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
#include "CMPBlockBuffer+Text.h"
#include <TargetConditionals.h>
#if defined(TARGET_OS_IPHONE) && (TARGET_OS_IPHONE > 0)
#include <MacErrors.h>
#endif
#include "CMPAtoms.h"
#include "CMPAtoms+Iterate.h"
OSStatus CMPBlockBufferCreateWithText(CFAllocatorRef allocator, CFStringRef text, CMBlockBufferRef* outBlockBuffer) {
CFIndex textLengthUTF16 = CFStringGetLength(text);
if(textLengthUTF16 > USHRT_MAX)
{
// text is too long in utf16
return paramErr;
}
const char *textCharacters = CFStringGetCStringPtr(text, kCFStringEncodingUTF8);
Boolean textCharactersNeedsFree = false;
CFIndex textLengthUTF8 = textLengthUTF16;
if(textCharacters == NULL)
{
// if there are umlauts or other special characters CFStringGetCStringPtr returns NULL
CFRange range = CFRangeMake(0, textLengthUTF16);
CFIndex length = CFStringGetBytes(text, range, kCFStringEncodingUTF8, 0, false, NULL, 0, &textLengthUTF8);
if(length != textLengthUTF16)
{
// unable to encode the string
return paramErr;
}
if(textLengthUTF8 > USHRT_MAX)
{
// text is too long in utf8
return paramErr;
}
// allocate one byte more than needed to append \0 for debugging
textCharacters = calloc(textLengthUTF8 + 1, 1);
textCharactersNeedsFree = true;
length = CFStringGetBytes(text, CFRangeMake(0, textLengthUTF16), kCFStringEncodingUTF8, 0, false, (UInt8 *)textCharacters, textLengthUTF8 + 1, &textLengthUTF8);
if(length != textLengthUTF16)
{
// unable to encode the string, this should never happen
free((void *)textCharacters);
return paramErr;
}
}
size_t bufferLength = 2 + textLengthUTF8;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(allocator, NULL, bufferLength, allocator, NULL, 0, bufferLength, kCMBlockBufferAssureMemoryNowFlag, outBlockBuffer);
if(status != noErr)
{
if(textCharactersNeedsFree)
{
free((void *)textCharacters);
}
return status;
}
char *buffer = NULL;
status = CMBlockBufferGetDataPointer(*outBlockBuffer, 0, NULL, NULL, &buffer);
if(status != noErr)
{
if(textCharactersNeedsFree)
{
free((void *)textCharacters);
}
return status;
}
*(uint16_t *)buffer = OSSwapHostToBigInt16(textLengthUTF8);
buffer += sizeof(uint16_t);
memcpy(buffer, textCharacters, textLengthUTF8);
buffer += textLengthUTF8;
// TODO: write an explicit encoding atom here?
if(textCharactersNeedsFree)
{
free((void *)textCharacters);
}
return noErr;
}
CFStringRef CMPBlockBufferCopyText(CFAllocatorRef allocator, CMBlockBufferRef blockBuffer)
{
char *buffer = NULL;
size_t size = 0;
OSStatus status = CMBlockBufferGetDataPointer(blockBuffer, 0, &size, NULL, &buffer);
if(status != noErr)
{
return NULL;
}
// check if we can read the size
if(size < 2)
{
return NULL;
}
CFIndex textLength = OSSwapBigToHostInt16(*(UInt16 *)buffer);
buffer += 2;
size -= 2;
// check if the size is big enough for all values
if(size < textLength)
{
return NULL;
}
const UInt8 *textCharacters = (UInt8 *)buffer;
buffer += textLength;
size -= textLength;
CFStringEncoding textEncoding = kCFStringEncodingUTF8;
// read appended atoms
if(size > 0)
{
size_t offset = 0;
CMPAtom *atom = NULL;
while(CMPAtomIterate(buffer, size, &offset, &atom))
{
FourCharCode atomType = OSSwapBigToHostInt32(atom->type);
UInt32 atomSize = OSSwapBigToHostInt32(atom->size);
if(atomType == CMPAtomTypeEncoding)
{
CMPEncodingAtom *encodingAtom = (CMPEncodingAtom *)atom;
// atom does not fit
if(atomSize > sizeof(*encodingAtom))
{
continue;
}
textEncoding = OSSwapBigToHostInt32(encodingAtom->encoding);
}
}
}
// search "User Data Text Strings and Language Codes"
// https://developer.apple.com/library/mac/documentation/QuickTime/qtff/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-BBCCFFGD
if(textLength > 2)
{
UInt16 bom = *(UInt16 *)textCharacters;
if(textEncoding == kCFStringEncodingUTF8)
{
if(bom == 0xfeff || bom == 0xfffe)
{
textEncoding = kCFStringEncodingUTF16;
}
}
else if(textEncoding == kCFStringEncodingUTF16)
{
if(bom != 0xfeff && bom != 0xfffe)
{
textEncoding = kCFStringEncodingUTF8;
}
}
}
return CFStringCreateWithBytes(allocator, textCharacters, textLength, textEncoding, false);
}