Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: Source/core/html/track/WebVTTParser.cpp

Issue 64303004: Handle buffer boundaries in the WebVTT parser (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix compilation warning Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/html/track/WebVTTParser.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 128
129 void WebVTTParser::getNewRegions(Vector<RefPtr<VTTRegion> >& outputRegions) 129 void WebVTTParser::getNewRegions(Vector<RefPtr<VTTRegion> >& outputRegions)
130 { 130 {
131 outputRegions = m_regionList; 131 outputRegions = m_regionList;
132 m_regionList.clear(); 132 m_regionList.clear();
133 } 133 }
134 134
135 void WebVTTParser::parseBytes(const char* data, unsigned length) 135 void WebVTTParser::parseBytes(const char* data, unsigned length)
136 { 136 {
137 String textData = m_decoder->decode(data, length); 137 String textData = m_decoder->decode(data, length);
138 parse(textData); 138 m_lineReader.append(textData);
139 parse();
139 } 140 }
140 141
141 void WebVTTParser::flush() 142 void WebVTTParser::flush()
142 { 143 {
143 String textData = m_decoder->flush(); 144 String textData = m_decoder->flush();
144 parse(textData); 145 m_lineReader.append(textData);
146 m_lineReader.setEndOfStream();
147 parse();
145 flushPendingCue(); 148 flushPendingCue();
146 } 149 }
147 150
148 void WebVTTParser::parse(const String& textData) 151 void WebVTTParser::parse()
149 { 152 {
150 // 4.8.10.13.3 WHATWG WebVTT Parser algorithm. 153 // 4.8.10.13.3 WHATWG WebVTT Parser algorithm.
151 // 1-3 - Initial setup. 154 // 1-3 - Initial setup.
152 unsigned position = 0;
153 155
154 while (position < textData.length()) { 156 String line;
155 String line = collectNextLine(textData, &position); 157 while (m_lineReader.getLine(line)) {
156
157 switch (m_state) { 158 switch (m_state) {
158 case Initial: 159 case Initial:
159 // 4-12 - Check for a valid WebVTT signature. 160 // 4-12 - Check for a valid WebVTT signature.
160 if (!hasRequiredFileIdentifier(line)) { 161 if (!hasRequiredFileIdentifier(line)) {
161 if (m_client) 162 if (m_client)
162 m_client->fileFailedToParse(); 163 m_client->fileFailedToParse();
163 return; 164 return;
164 } 165 }
165 166
166 m_state = Header; 167 m_state = Header;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 case BadCue: 209 case BadCue:
209 // 54-62 - Collect and discard the remaining cue. 210 // 54-62 - Collect and discard the remaining cue.
210 m_state = ignoreBadCue(line); 211 m_state = ignoreBadCue(line);
211 break; 212 break;
212 } 213 }
213 } 214 }
214 } 215 }
215 216
216 void WebVTTParser::flushPendingCue() 217 void WebVTTParser::flushPendingCue()
217 { 218 {
219 ASSERT(m_lineReader.isAtEndOfStream());
218 // If we're in the CueText state when we run out of data, we emit the pendin g cue. 220 // If we're in the CueText state when we run out of data, we emit the pendin g cue.
219 if (m_state == CueText) 221 if (m_state == CueText)
220 createNewCue(); 222 createNewCue();
221 } 223 }
222 224
223 bool WebVTTParser::hasRequiredFileIdentifier(const String& line) 225 bool WebVTTParser::hasRequiredFileIdentifier(const String& line)
224 { 226 {
225 // A WebVTT file identifier consists of an optional BOM character, 227 // A WebVTT file identifier consists of an optional BOM character,
226 // the string "WEBVTT" followed by an optional space or tab character, 228 // the string "WEBVTT" followed by an optional space or tab character,
227 // and any number of characters that are not line terminators ... 229 // and any number of characters that are not line terminators ...
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 } 559 }
558 m_token.clear(); 560 m_token.clear();
559 } 561 }
560 562
561 void WebVTTParser::skipWhiteSpace(const String& line, unsigned* position) 563 void WebVTTParser::skipWhiteSpace(const String& line, unsigned* position)
562 { 564 {
563 while (*position < line.length() && isASpace(line[*position])) 565 while (*position < line.length() && isASpace(line[*position]))
564 (*position)++; 566 (*position)++;
565 } 567 }
566 568
567 void WebVTTParser::skipLineTerminator(const String& data, unsigned* position)
568 {
569 if (*position >= data.length())
570 return;
571 if (data[*position] == '\r')
572 (*position)++;
573 if (*position >= data.length())
574 return;
575 if (data[*position] == '\n')
576 (*position)++;
577 }
578
579 String WebVTTParser::collectNextLine(const String& data, unsigned* position)
580 {
581 unsigned oldPosition = *position;
582 while (*position < data.length() && data[*position] != '\r' && data[*positio n] != '\n')
583 (*position)++;
584 String line = data.substring(oldPosition, *position - oldPosition);
585 skipLineTerminator(data, position);
586 return line;
587 }
588
589 } 569 }
590 570
OLDNEW
« no previous file with comments | « Source/core/html/track/WebVTTParser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698