This source file includes following definitions.
- collectDigits
- collectFraction
- invalidTimeValue
- m_endTime
- startTime
- endTime
- parseFragments
- parseTimeFragment
- parseNPTFragment
- parseNPTTime
#include "config.h"
#include "core/html/MediaFragmentURIParser.h"
#include "platform/graphics/media/MediaPlayer.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/text/WTFString.h"
namespace WebCore {
const int secondsPerHour = 3600;
const int secondsPerMinute = 60;
const unsigned nptIdentiferLength = 4;
static String collectDigits(const LChar* input, unsigned length, unsigned& position)
{
StringBuilder digits;
while (position < length && isASCIIDigit(input[position]))
digits.append(input[position++]);
return digits.toString();
}
static String collectFraction(const LChar* input, unsigned length, unsigned& position)
{
StringBuilder digits;
if (input[position] != '.')
return String();
digits.append(input[position++]);
while (position < length && isASCIIDigit(input[position]))
digits.append(input[position++]);
return digits.toString();
}
double MediaFragmentURIParser::invalidTimeValue()
{
return MediaPlayer::invalidTime();
}
MediaFragmentURIParser::MediaFragmentURIParser(const KURL& url)
: m_url(url)
, m_timeFormat(None)
, m_startTime(MediaPlayer::invalidTime())
, m_endTime(MediaPlayer::invalidTime())
{
}
double MediaFragmentURIParser::startTime()
{
if (!m_url.isValid())
return MediaPlayer::invalidTime();
if (m_timeFormat == None)
parseTimeFragment();
return m_startTime;
}
double MediaFragmentURIParser::endTime()
{
if (!m_url.isValid())
return MediaPlayer::invalidTime();
if (m_timeFormat == None)
parseTimeFragment();
return m_endTime;
}
void MediaFragmentURIParser::parseFragments()
{
if (!m_url.hasFragmentIdentifier())
return;
String fragmentString = m_url.fragmentIdentifier();
if (fragmentString.isEmpty())
return;
unsigned offset = 0;
unsigned end = fragmentString.length();
while (offset < end) {
size_t parameterStart = offset;
size_t parameterEnd = fragmentString.find('&', offset);
if (parameterEnd == kNotFound)
parameterEnd = end;
size_t equalOffset = fragmentString.find('=', offset);
if (equalOffset == kNotFound || equalOffset > parameterEnd) {
offset = parameterEnd + 1;
continue;
}
String name = decodeURLEscapeSequences(fragmentString.substring(parameterStart, equalOffset - parameterStart));
String value;
if (equalOffset != parameterEnd)
value = decodeURLEscapeSequences(fragmentString.substring(equalOffset + 1, parameterEnd - equalOffset - 1));
bool validUTF8 = true;
if (!name.isEmpty()) {
name = name.utf8(StrictUTF8Conversion).data();
validUTF8 = !name.isEmpty();
}
if (validUTF8 && !value.isEmpty()) {
value = value.utf8(StrictUTF8Conversion).data();
validUTF8 = !value.isEmpty();
}
if (validUTF8)
m_fragments.append(std::make_pair(name, value));
offset = parameterEnd + 1;
}
}
void MediaFragmentURIParser::parseTimeFragment()
{
ASSERT(m_timeFormat == None);
if (m_fragments.isEmpty())
parseFragments();
m_timeFormat = Invalid;
for (unsigned i = 0; i < m_fragments.size(); ++i) {
pair<String, String>& fragment = m_fragments[i];
ASSERT(fragment.first.is8Bit());
ASSERT(fragment.second.is8Bit());
if (fragment.first != "t")
continue;
double start = MediaPlayer::invalidTime();
double end = MediaPlayer::invalidTime();
if (parseNPTFragment(fragment.second.characters8(), fragment.second.length(), start, end)) {
m_startTime = start;
m_endTime = end;
m_timeFormat = NormalPlayTime;
}
}
m_fragments.clear();
}
bool MediaFragmentURIParser::parseNPTFragment(const LChar* timeString, unsigned length, double& startTime, double& endTime)
{
unsigned offset = 0;
if (length >= nptIdentiferLength && timeString[0] == 'n' && timeString[1] == 'p' && timeString[2] == 't' && timeString[3] == ':')
offset += nptIdentiferLength;
if (offset == length)
return false;
if (timeString[offset] == ',')
startTime = 0;
else {
if (!parseNPTTime(timeString, length, offset, startTime))
return false;
}
if (offset == length)
return true;
if (timeString[offset] != ',')
return false;
if (++offset == length)
return false;
if (!parseNPTTime(timeString, length, offset, endTime))
return false;
if (offset != length)
return false;
if (startTime >= endTime)
return false;
return true;
}
bool MediaFragmentURIParser::parseNPTTime(const LChar* timeString, unsigned length, unsigned& offset, double& time)
{
enum Mode { minutes, hours };
Mode mode = minutes;
if (offset >= length || !isASCIIDigit(timeString[offset]))
return false;
String digits1 = collectDigits(timeString, length, offset);
int value1 = digits1.toInt();
if (offset >= length || timeString[offset] == ',') {
time = value1;
return true;
}
double fraction = 0;
if (timeString[offset] == '.') {
if (offset == length)
return true;
String digits = collectFraction(timeString, length, offset);
fraction = digits.toDouble();
time = value1 + fraction;
return true;
}
if (digits1.length() < 2)
return false;
if (digits1.length() > 2)
mode = hours;
if (offset >= length || timeString[offset++] != ':')
return false;
if (offset >= length || !isASCIIDigit(timeString[(offset)]))
return false;
String digits2 = collectDigits(timeString, length, offset);
int value2 = digits2.toInt();
if (digits2.length() != 2)
return false;
int value3;
if (mode == hours || (offset < length && timeString[offset] == ':')) {
if (offset >= length || timeString[offset++] != ':')
return false;
if (offset >= length || !isASCIIDigit(timeString[offset]))
return false;
String digits3 = collectDigits(timeString, length, offset);
if (digits3.length() != 2)
return false;
value3 = digits3.toInt();
} else {
value3 = value2;
value2 = value1;
value1 = 0;
}
if (offset < length && timeString[offset] == '.')
fraction = collectFraction(timeString, length, offset).toDouble();
time = (value1 * secondsPerHour) + (value2 * secondsPerMinute) + value3 + fraction;
return true;
}
}