DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsTArray.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsEscape.h"
#include "nsIURI.h"
#include <utility>

#include "nsMediaFragmentURIParser.h"

using std::make_pair;
using std::pair;

namespace mozilla {
namespace net {

nsMediaFragmentURIParser::nsMediaFragmentURIParser(nsIURI* aURI)
    : mClipUnit(eClipUnit_Pixel) {
  nsAutoCString ref;
  aURI->GetRef(ref);
  Parse(ref);
}

nsMediaFragmentURIParser::nsMediaFragmentURIParser(nsCString& aRef)
    : mClipUnit(eClipUnit_Pixel) {
  Parse(aRef);
}

bool nsMediaFragmentURIParser::ParseNPT(nsDependentSubstring aString) {
  nsDependentSubstring original(aString);
  if (aString.Length() > 4 && aString[0] == 'n' && aString[1] == 'p' &&
      aString[2] == 't' && aString[3] == ':') {
    aString.Rebind(aString, 4);
  }

  if (aString.Length() == 0) {
    return false;
  }

  double start = -1.0;
  double end = -1.0;

  ParseNPTTime(aString, start);

  if (aString.Length() == 0) {
    mStart.emplace(start);
    return true;
  }

  if (aString[0] != ',') {
    aString.Rebind(original, 0);
    return false;
  }

  aString.Rebind(aString, 1);

  if (aString.Length() == 0) {
    aString.Rebind(original, 0);
    return false;
  }

  ParseNPTTime(aString, end);

  if (end <= start || aString.Length() != 0) {
    aString.Rebind(original, 0);
    return false;
  }

  mStart.emplace(start);
  mEnd.emplace(end);
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTTime(nsDependentSubstring& aString,
                                            double& aTime) {
  if (aString.Length() == 0) {
    return false;
  }

  return ParseNPTHHMMSS(aString, aTime) || ParseNPTMMSS(aString, aTime) ||
         ParseNPTSec(aString, aTime);
}

// Return true if the given character is a numeric character
static bool IsDigit(nsDependentSubstring::char_type aChar) {
  return (aChar >= '0' && aChar <= '9');
}

// Return the index of the first character in the string that is not
// a numerical digit, starting from 'aStart'.
static uint32_t FirstNonDigit(nsDependentSubstring& aString, uint32_t aStart) {
  while (aStart < aString.Length() && IsDigit(aString[aStart])) {
    ++aStart;
  }
  return aStart;
}

bool nsMediaFragmentURIParser::ParseNPTSec(nsDependentSubstring& aString,
                                           double& aSec) {
  nsDependentSubstring original(aString);
  if (aString.Length() == 0) {
    return false;
  }

  uint32_t index = FirstNonDigit(aString, 0);
  if (index == 0) {
    return false;
  }

  nsDependentSubstring n(aString, 0, index);
  nsresult ec;
  int32_t s = PromiseFlatString(n).ToInteger(&ec);
  if (NS_FAILED(ec)) {
    return false;
  }

  aString.Rebind(aString, index);
  double fraction = 0.0;
  if (!ParseNPTFraction(aString, fraction)) {
    aString.Rebind(original, 0);
    return false;
  }

  aSec = s + fraction;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTMMSS(nsDependentSubstring& aString,
                                            double& aTime) {
  nsDependentSubstring original(aString);
  uint32_t mm = 0;
  uint32_t ss = 0;
  double fraction = 0.0;
  if (!ParseNPTMM(aString, mm)) {
    aString.Rebind(original, 0);
    return false;
  }

  if (aString.Length() < 2 || aString[0] != ':') {
    aString.Rebind(original, 0);
    return false;
  }

  aString.Rebind(aString, 1);
  if (!ParseNPTSS(aString, ss)) {
    aString.Rebind(original, 0);
    return false;
  }

  if (!ParseNPTFraction(aString, fraction)) {
    aString.Rebind(original, 0);
    return false;
  }
  aTime = mm * 60 + ss + fraction;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTFraction(nsDependentSubstring& aString,
                                                double& aFraction) {
  double fraction = 0.0;

  if (aString.Length() > 0 && aString[0] == '.') {
    uint32_t index = FirstNonDigit(aString, 1);

    if (index > 1) {
      nsDependentSubstring number(aString, 0, index);
      nsresult ec;
      fraction = PromiseFlatString(number).ToDouble(&ec);
      if (NS_FAILED(ec)) {
        return false;
      }
    }
    aString.Rebind(aString, index);
  }

  aFraction = fraction;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTHHMMSS(nsDependentSubstring& aString,
                                              double& aTime) {
  nsDependentSubstring original(aString);
  uint32_t hh = 0;
  double seconds = 0.0;
  if (!ParseNPTHH(aString, hh)) {
    return false;
  }

  if (aString.Length() < 2 || aString[0] != ':') {
    aString.Rebind(original, 0);
    return false;
  }

  aString.Rebind(aString, 1);
  if (!ParseNPTMMSS(aString, seconds)) {
    aString.Rebind(original, 0);
    return false;
  }

  aTime = hh * 3600 + seconds;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTHH(nsDependentSubstring& aString,
                                          uint32_t& aHour) {
  if (aString.Length() == 0) {
    return false;
  }

  uint32_t index = FirstNonDigit(aString, 0);
  if (index == 0) {
    return false;
  }

  nsDependentSubstring n(aString, 0, index);
  nsresult ec;
  int32_t u = PromiseFlatString(n).ToInteger(&ec);
  if (NS_FAILED(ec)) {
    return false;
  }

  aString.Rebind(aString, index);
  aHour = u;
  return true;
}

bool nsMediaFragmentURIParser::ParseNPTMM(nsDependentSubstring& aString,
                                          uint32_t& aMinute) {
  return ParseNPTSS(aString, aMinute);
}

bool nsMediaFragmentURIParser::ParseNPTSS(nsDependentSubstring& aString,
                                          uint32_t& aSecond) {
  if (aString.Length() < 2) {
    return false;
  }

  if (IsDigit(aString[0]) && IsDigit(aString[1])) {
    nsDependentSubstring n(aString, 0, 2);
    nsresult ec;
    int32_t u = PromiseFlatString(n).ToInteger(&ec);
    if (NS_FAILED(ec)) {
      return false;
    }

    aString.Rebind(aString, 2);
    if (u >= 60) return false;

    aSecond = u;
    return true;
  }

  return false;
}

static bool ParseInteger(nsDependentSubstring& aString, int32_t& aResult) {
  uint32_t index = FirstNonDigit(aString, 0);
  if (index == 0) {
    return false;
  }

  nsDependentSubstring n(aString, 0, index);
  nsresult ec;
  int32_t s = PromiseFlatString(n).ToInteger(&ec);
  if (NS_FAILED(ec)) {
    return false;
  }

  aString.Rebind(aString, index);
  aResult = s;
  return true;
}

static bool ParseCommaSeparator(nsDependentSubstring& aString) {
  if (aString.Length() > 1 && aString[0] == ',') {
    aString.Rebind(aString, 1);
    return true;
  }

  return false;
}

bool nsMediaFragmentURIParser::ParseXYWH(nsDependentSubstring aString) {
  int32_t x, y, w, h;
  ClipUnit clipUnit;

  // Determine units.
  if (StringBeginsWith(aString, NS_LITERAL_STRING("pixel:"))) {
    clipUnit = eClipUnit_Pixel;
    aString.Rebind(aString, 6);
  } else if (StringBeginsWith(aString, NS_LITERAL_STRING("percent:"))) {
    clipUnit = eClipUnit_Percent;
    aString.Rebind(aString, 8);
  } else {
    clipUnit = eClipUnit_Pixel;
  }

  // Read and validate coordinates.
  if (ParseInteger(aString, x) && x >= 0 && ParseCommaSeparator(aString) &&
      ParseInteger(aString, y) && y >= 0 && ParseCommaSeparator(aString) &&
      ParseInteger(aString, w) && w > 0 && ParseCommaSeparator(aString) &&
      ParseInteger(aString, h) && h > 0 && aString.Length() == 0) {
    // Reject invalid percentage coordinates.
    if (clipUnit == eClipUnit_Percent && (x + w > 100 || y + h > 100)) {
      return false;
    }

    mClip.emplace(x, y, w, h);
    mClipUnit = clipUnit;
    return true;
  }

  return false;
}

void nsMediaFragmentURIParser::Parse(nsACString& aRef) {
  // Create an array of possibly-invalid media fragments.
  nsTArray<std::pair<nsCString, nsCString> > fragments;
  nsCCharSeparatedTokenizer tokenizer(aRef, '&');

  while (tokenizer.hasMoreTokens()) {
    const nsACString& nv = tokenizer.nextToken();
    int32_t index = nv.FindChar('=');
    if (index >= 0) {
      nsAutoCString name;
      nsAutoCString value;
      NS_UnescapeURL(StringHead(nv, index), esc_Ref | esc_AlwaysCopy, name);
      NS_UnescapeURL(Substring(nv, index + 1, nv.Length()),
                     esc_Ref | esc_AlwaysCopy, value);
      fragments.AppendElement(make_pair(name, value));
    }
  }

  // Parse the media fragment values.
  bool gotTemporal = false, gotSpatial = false;
  for (int i = fragments.Length() - 1; i >= 0; --i) {
    if (gotTemporal && gotSpatial) {
      // We've got one of each possible type. No need to look at the rest.
      break;
    } else if (!gotTemporal && fragments[i].first.EqualsLiteral("t")) {
      nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
      gotTemporal = ParseNPT(nsDependentSubstring(value, 0));
    } else if (!gotSpatial && fragments[i].first.EqualsLiteral("xywh")) {
      nsAutoString value = NS_ConvertUTF8toUTF16(fragments[i].second);
      gotSpatial = ParseXYWH(nsDependentSubstring(value, 0));
    }
  }
}

}  // namespace net
}  // namespace mozilla