nsMediaFragmentURIParser.cpp 9.3 KB

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