netwerk/base/src/nsMediaFragmentURIParser.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsTArray.h"
michael@0 8 #include "nsCharSeparatedTokenizer.h"
michael@0 9 #include "nsEscape.h"
michael@0 10 #include "nsIURI.h"
michael@0 11 #include <utility>
michael@0 12
michael@0 13 #include "nsMediaFragmentURIParser.h"
michael@0 14
michael@0 15 using std::pair;
michael@0 16 using std::make_pair;
michael@0 17
michael@0 18 namespace mozilla { namespace net {
michael@0 19
michael@0 20 nsMediaFragmentURIParser::nsMediaFragmentURIParser(nsIURI* aURI)
michael@0 21 : mClipUnit(eClipUnit_Pixel)
michael@0 22 {
michael@0 23 nsAutoCString ref;
michael@0 24 aURI->GetRef(ref);
michael@0 25 Parse(ref);
michael@0 26 }
michael@0 27
michael@0 28 nsMediaFragmentURIParser::nsMediaFragmentURIParser(nsCString& aRef)
michael@0 29 : mClipUnit(eClipUnit_Pixel)
michael@0 30 {
michael@0 31 Parse(aRef);
michael@0 32 }
michael@0 33
michael@0 34 bool nsMediaFragmentURIParser::ParseNPT(nsDependentSubstring aString)
michael@0 35 {
michael@0 36 nsDependentSubstring original(aString);
michael@0 37 if (aString.Length() > 4 &&
michael@0 38 aString[0] == 'n' && aString[1] == 'p' &&
michael@0 39 aString[2] == 't' && aString[3] == ':') {
michael@0 40 aString.Rebind(aString, 4);
michael@0 41 }
michael@0 42
michael@0 43 if (aString.Length() == 0) {
michael@0 44 return false;
michael@0 45 }
michael@0 46
michael@0 47 double start = -1.0;
michael@0 48 double end = -1.0;
michael@0 49
michael@0 50 ParseNPTTime(aString, start);
michael@0 51
michael@0 52 if (aString.Length() == 0) {
michael@0 53 mStart.construct(start);
michael@0 54 return true;
michael@0 55 }
michael@0 56
michael@0 57 if (aString[0] != ',') {
michael@0 58 aString.Rebind(original, 0);
michael@0 59 return false;
michael@0 60 }
michael@0 61
michael@0 62 aString.Rebind(aString, 1);
michael@0 63
michael@0 64 if (aString.Length() == 0) {
michael@0 65 aString.Rebind(original, 0);
michael@0 66 return false;
michael@0 67 }
michael@0 68
michael@0 69 ParseNPTTime(aString, end);
michael@0 70
michael@0 71 if (end <= start || aString.Length() != 0) {
michael@0 72 aString.Rebind(original, 0);
michael@0 73 return false;
michael@0 74 }
michael@0 75
michael@0 76 mStart.construct(start);
michael@0 77 mEnd.construct(end);
michael@0 78 return true;
michael@0 79 }
michael@0 80
michael@0 81 bool nsMediaFragmentURIParser::ParseNPTTime(nsDependentSubstring& aString, double& aTime)
michael@0 82 {
michael@0 83 if (aString.Length() == 0) {
michael@0 84 return false;
michael@0 85 }
michael@0 86
michael@0 87 return
michael@0 88 ParseNPTHHMMSS(aString, aTime) ||
michael@0 89 ParseNPTMMSS(aString, aTime) ||
michael@0 90 ParseNPTSec(aString, aTime);
michael@0 91 }
michael@0 92
michael@0 93 // Return true if the given character is a numeric character
michael@0 94 static bool IsDigit(nsDependentSubstring::char_type aChar)
michael@0 95 {
michael@0 96 return (aChar >= '0' && aChar <= '9');
michael@0 97 }
michael@0 98
michael@0 99 // Return the index of the first character in the string that is not
michael@0 100 // a numerical digit, starting from 'aStart'.
michael@0 101 static uint32_t FirstNonDigit(nsDependentSubstring& aString, uint32_t aStart)
michael@0 102 {
michael@0 103 while (aStart < aString.Length() && IsDigit(aString[aStart])) {
michael@0 104 ++aStart;
michael@0 105 }
michael@0 106 return aStart;
michael@0 107 }
michael@0 108
michael@0 109 bool nsMediaFragmentURIParser::ParseNPTSec(nsDependentSubstring& aString, double& aSec)
michael@0 110 {
michael@0 111 nsDependentSubstring original(aString);
michael@0 112 if (aString.Length() == 0) {
michael@0 113 return false;
michael@0 114 }
michael@0 115
michael@0 116 uint32_t index = FirstNonDigit(aString, 0);
michael@0 117 if (index == 0) {
michael@0 118 return false;
michael@0 119 }
michael@0 120
michael@0 121 nsDependentSubstring n(aString, 0, index);
michael@0 122 nsresult ec;
michael@0 123 int32_t s = PromiseFlatString(n).ToInteger(&ec);
michael@0 124 if (NS_FAILED(ec)) {
michael@0 125 return false;
michael@0 126 }
michael@0 127
michael@0 128 aString.Rebind(aString, index);
michael@0 129 double fraction = 0.0;
michael@0 130 if (!ParseNPTFraction(aString, fraction)) {
michael@0 131 aString.Rebind(original, 0);
michael@0 132 return false;
michael@0 133 }
michael@0 134
michael@0 135 aSec = s + fraction;
michael@0 136 return true;
michael@0 137 }
michael@0 138
michael@0 139 bool nsMediaFragmentURIParser::ParseNPTMMSS(nsDependentSubstring& aString, double& aTime)
michael@0 140 {
michael@0 141 nsDependentSubstring original(aString);
michael@0 142 uint32_t mm = 0;
michael@0 143 uint32_t ss = 0;
michael@0 144 double fraction = 0.0;
michael@0 145 if (!ParseNPTMM(aString, mm)) {
michael@0 146 aString.Rebind(original, 0);
michael@0 147 return false;
michael@0 148 }
michael@0 149
michael@0 150 if (aString.Length() < 2 || aString[0] != ':') {
michael@0 151 aString.Rebind(original, 0);
michael@0 152 return false;
michael@0 153 }
michael@0 154
michael@0 155 aString.Rebind(aString, 1);
michael@0 156 if (!ParseNPTSS(aString, ss)) {
michael@0 157 aString.Rebind(original, 0);
michael@0 158 return false;
michael@0 159 }
michael@0 160
michael@0 161 if (!ParseNPTFraction(aString, fraction)) {
michael@0 162 aString.Rebind(original, 0);
michael@0 163 return false;
michael@0 164 }
michael@0 165 aTime = mm * 60 + ss + fraction;
michael@0 166 return true;
michael@0 167 }
michael@0 168
michael@0 169 bool nsMediaFragmentURIParser::ParseNPTFraction(nsDependentSubstring& aString, double& aFraction)
michael@0 170 {
michael@0 171 double fraction = 0.0;
michael@0 172
michael@0 173 if (aString.Length() > 0 && aString[0] == '.') {
michael@0 174 uint32_t index = FirstNonDigit(aString, 1);
michael@0 175
michael@0 176 if (index > 1) {
michael@0 177 nsDependentSubstring number(aString, 0, index);
michael@0 178 nsresult ec;
michael@0 179 fraction = PromiseFlatString(number).ToDouble(&ec);
michael@0 180 if (NS_FAILED(ec)) {
michael@0 181 return false;
michael@0 182 }
michael@0 183 }
michael@0 184 aString.Rebind(aString, index);
michael@0 185 }
michael@0 186
michael@0 187 aFraction = fraction;
michael@0 188 return true;
michael@0 189 }
michael@0 190
michael@0 191 bool nsMediaFragmentURIParser::ParseNPTHHMMSS(nsDependentSubstring& aString, double& aTime)
michael@0 192 {
michael@0 193 nsDependentSubstring original(aString);
michael@0 194 uint32_t hh = 0;
michael@0 195 double seconds = 0.0;
michael@0 196 if (!ParseNPTHH(aString, hh)) {
michael@0 197 return false;
michael@0 198 }
michael@0 199
michael@0 200 if (aString.Length() < 2 || aString[0] != ':') {
michael@0 201 aString.Rebind(original, 0);
michael@0 202 return false;
michael@0 203 }
michael@0 204
michael@0 205 aString.Rebind(aString, 1);
michael@0 206 if (!ParseNPTMMSS(aString, seconds)) {
michael@0 207 aString.Rebind(original, 0);
michael@0 208 return false;
michael@0 209 }
michael@0 210
michael@0 211 aTime = hh * 3600 + seconds;
michael@0 212 return true;
michael@0 213 }
michael@0 214
michael@0 215 bool nsMediaFragmentURIParser::ParseNPTHH(nsDependentSubstring& aString, uint32_t& aHour)
michael@0 216 {
michael@0 217 if (aString.Length() == 0) {
michael@0 218 return false;
michael@0 219 }
michael@0 220
michael@0 221 uint32_t index = FirstNonDigit(aString, 0);
michael@0 222 if (index == 0) {
michael@0 223 return false;
michael@0 224 }
michael@0 225
michael@0 226 nsDependentSubstring n(aString, 0, index);
michael@0 227 nsresult ec;
michael@0 228 int32_t u = PromiseFlatString(n).ToInteger(&ec);
michael@0 229 if (NS_FAILED(ec)) {
michael@0 230 return false;
michael@0 231 }
michael@0 232
michael@0 233 aString.Rebind(aString, index);
michael@0 234 aHour = u;
michael@0 235 return true;
michael@0 236 }
michael@0 237
michael@0 238 bool nsMediaFragmentURIParser::ParseNPTMM(nsDependentSubstring& aString, uint32_t& aMinute)
michael@0 239 {
michael@0 240 return ParseNPTSS(aString, aMinute);
michael@0 241 }
michael@0 242
michael@0 243 bool nsMediaFragmentURIParser::ParseNPTSS(nsDependentSubstring& aString, uint32_t& aSecond)
michael@0 244 {
michael@0 245 if (aString.Length() < 2) {
michael@0 246 return false;
michael@0 247 }
michael@0 248
michael@0 249 if (IsDigit(aString[0]) && IsDigit(aString[1])) {
michael@0 250 nsDependentSubstring n(aString, 0, 2);
michael@0 251 nsresult ec;
michael@0 252 int32_t u = PromiseFlatString(n).ToInteger(&ec);
michael@0 253 if (NS_FAILED(ec)) {
michael@0 254 return false;
michael@0 255 }
michael@0 256
michael@0 257 aString.Rebind(aString, 2);
michael@0 258 if (u >= 60)
michael@0 259 return false;
michael@0 260
michael@0 261 aSecond = u;
michael@0 262 return true;
michael@0 263 }
michael@0 264
michael@0 265 return false;
michael@0 266 }
michael@0 267
michael@0 268 static bool ParseInteger(nsDependentSubstring& aString,
michael@0 269 int32_t& aResult)
michael@0 270 {
michael@0 271 uint32_t index = FirstNonDigit(aString, 0);
michael@0 272 if (index == 0) {
michael@0 273 return false;
michael@0 274 }
michael@0 275
michael@0 276 nsDependentSubstring n(aString, 0, index);
michael@0 277 nsresult ec;
michael@0 278 int32_t s = PromiseFlatString(n).ToInteger(&ec);
michael@0 279 if (NS_FAILED(ec)) {
michael@0 280 return false;
michael@0 281 }
michael@0 282
michael@0 283 aString.Rebind(aString, index);
michael@0 284 aResult = s;
michael@0 285 return true;
michael@0 286 }
michael@0 287
michael@0 288 static bool ParseCommaSeparator(nsDependentSubstring& aString)
michael@0 289 {
michael@0 290 if (aString.Length() > 1 && aString[0] == ',') {
michael@0 291 aString.Rebind(aString, 1);
michael@0 292 return true;
michael@0 293 }
michael@0 294
michael@0 295 return false;
michael@0 296 }
michael@0 297
michael@0 298 bool nsMediaFragmentURIParser::ParseXYWH(nsDependentSubstring aString)
michael@0 299 {
michael@0 300 int32_t x, y, w, h;
michael@0 301 ClipUnit clipUnit;
michael@0 302
michael@0 303 // Determine units.
michael@0 304 if (StringBeginsWith(aString, NS_LITERAL_STRING("pixel:"))) {
michael@0 305 clipUnit = eClipUnit_Pixel;
michael@0 306 aString.Rebind(aString, 6);
michael@0 307 } else if (StringBeginsWith(aString, NS_LITERAL_STRING("percent:"))) {
michael@0 308 clipUnit = eClipUnit_Percent;
michael@0 309 aString.Rebind(aString, 8);
michael@0 310 } else {
michael@0 311 clipUnit = eClipUnit_Pixel;
michael@0 312 }
michael@0 313
michael@0 314 // Read and validate coordinates.
michael@0 315 if (ParseInteger(aString, x) && x >= 0 &&
michael@0 316 ParseCommaSeparator(aString) &&
michael@0 317 ParseInteger(aString, y) && y >= 0 &&
michael@0 318 ParseCommaSeparator(aString) &&
michael@0 319 ParseInteger(aString, w) && w > 0 &&
michael@0 320 ParseCommaSeparator(aString) &&
michael@0 321 ParseInteger(aString, h) && h > 0 &&
michael@0 322 aString.Length() == 0) {
michael@0 323
michael@0 324 // Reject invalid percentage coordinates.
michael@0 325 if (clipUnit == eClipUnit_Percent &&
michael@0 326 (x + w > 100 || y + h > 100)) {
michael@0 327 return false;
michael@0 328 }
michael@0 329
michael@0 330 mClip.construct(x, y, w, h);
michael@0 331 mClipUnit = clipUnit;
michael@0 332 return true;
michael@0 333 }
michael@0 334
michael@0 335 return false;
michael@0 336 }
michael@0 337
michael@0 338 bool nsMediaFragmentURIParser::ParseMozResolution(nsDependentSubstring aString)
michael@0 339 {
michael@0 340 int32_t w, h;
michael@0 341
michael@0 342 // Read and validate coordinates.
michael@0 343 if (ParseInteger(aString, w) && w >= 0 &&
michael@0 344 ParseCommaSeparator(aString) &&
michael@0 345 ParseInteger(aString, h) && h >= 0 &&
michael@0 346 aString.Length() == 0) {
michael@0 347 mResolution.construct(w,h);
michael@0 348 return true;
michael@0 349 }
michael@0 350
michael@0 351 return false;
michael@0 352 }
michael@0 353
michael@0 354 bool nsMediaFragmentURIParser::ParseMozSampleSize(nsDependentSubstring aString)
michael@0 355 {
michael@0 356 int32_t sampleSize;
michael@0 357
michael@0 358 // Read and validate coordinates.
michael@0 359 if (ParseInteger(aString, sampleSize) && sampleSize > 0) {
michael@0 360 mSampleSize.construct(sampleSize);
michael@0 361 return true;
michael@0 362 }
michael@0 363
michael@0 364 return false;
michael@0 365 }
michael@0 366
michael@0 367 void nsMediaFragmentURIParser::Parse(nsACString& aRef)
michael@0 368 {
michael@0 369 // Create an array of possibly-invalid media fragments.
michael@0 370 nsTArray< std::pair<nsCString, nsCString> > fragments;
michael@0 371 nsCCharSeparatedTokenizer tokenizer(aRef, '&');
michael@0 372
michael@0 373 while (tokenizer.hasMoreTokens()) {
michael@0 374 const nsCSubstring& nv = tokenizer.nextToken();
michael@0 375 int32_t index = nv.FindChar('=');
michael@0 376 if (index >= 0) {
michael@0 377 nsAutoCString name;
michael@0 378 nsAutoCString value;
michael@0 379 NS_UnescapeURL(StringHead(nv, index), esc_Ref | esc_AlwaysCopy, name);
michael@0 380 NS_UnescapeURL(Substring(nv, index + 1, nv.Length()),
michael@0 381 esc_Ref | esc_AlwaysCopy, value);
michael@0 382 fragments.AppendElement(make_pair(name, value));
michael@0 383 }
michael@0 384 }
michael@0 385
michael@0 386 // Parse the media fragment values.
michael@0 387 bool gotTemporal = false, gotSpatial = false,
michael@0 388 gotResolution = false, gotSampleSize = false;
michael@0 389 for (int i = fragments.Length() - 1 ; i >= 0 ; --i) {
michael@0 390 if (gotTemporal && gotSpatial && gotResolution && gotSampleSize) {
michael@0 391 // We've got one of each possible type. No need to look at the rest.
michael@0 392 break;
michael@0 393 } else if (!gotTemporal && fragments[i].first.EqualsLiteral("t")) {
michael@0 394 nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
michael@0 395 gotTemporal = ParseNPT(nsDependentSubstring(value, 0));
michael@0 396 } else if (!gotSpatial && fragments[i].first.EqualsLiteral("xywh")) {
michael@0 397 nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
michael@0 398 gotSpatial = ParseXYWH(nsDependentSubstring(value, 0));
michael@0 399 } else if (!gotResolution && fragments[i].first.EqualsLiteral("-moz-resolution")) {
michael@0 400 nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
michael@0 401 gotResolution = ParseMozResolution(nsDependentSubstring(value, 0));
michael@0 402 } else if (!gotSampleSize && fragments[i].first.EqualsLiteral("-moz-samplesize")) {
michael@0 403 nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
michael@0 404 gotSampleSize = ParseMozSampleSize(nsDependentSubstring(value, 0));
michael@0 405 }
michael@0 406 }
michael@0 407 }
michael@0 408
michael@0 409 }} // namespace mozilla::net

mercurial