Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 :
7 : #include "BlobImpl.h"
8 : #include "File.h"
9 : #include "mozilla/CheckedInt.h"
10 :
11 : namespace mozilla {
12 : namespace dom {
13 :
14 : // Makes sure that aStart and aEnd is less then or equal to aSize and greater
15 : // than 0
16 : static void
17 0 : ParseSize(int64_t aSize, int64_t& aStart, int64_t& aEnd)
18 : {
19 0 : CheckedInt64 newStartOffset = aStart;
20 0 : if (aStart < -aSize) {
21 0 : newStartOffset = 0;
22 : }
23 0 : else if (aStart < 0) {
24 0 : newStartOffset += aSize;
25 : }
26 0 : else if (aStart > aSize) {
27 0 : newStartOffset = aSize;
28 : }
29 :
30 0 : CheckedInt64 newEndOffset = aEnd;
31 0 : if (aEnd < -aSize) {
32 0 : newEndOffset = 0;
33 : }
34 0 : else if (aEnd < 0) {
35 0 : newEndOffset += aSize;
36 : }
37 0 : else if (aEnd > aSize) {
38 0 : newEndOffset = aSize;
39 : }
40 :
41 0 : if (!newStartOffset.isValid() || !newEndOffset.isValid() ||
42 0 : newStartOffset.value() >= newEndOffset.value()) {
43 0 : aStart = aEnd = 0;
44 : }
45 : else {
46 0 : aStart = newStartOffset.value();
47 0 : aEnd = newEndOffset.value();
48 : }
49 0 : }
50 :
51 : already_AddRefed<BlobImpl>
52 0 : BlobImpl::Slice(const Optional<int64_t>& aStart,
53 : const Optional<int64_t>& aEnd,
54 : const nsAString& aContentType,
55 : ErrorResult& aRv)
56 : {
57 : // Truncate aStart and aEnd so that we stay within this file.
58 0 : uint64_t thisLength = GetSize(aRv);
59 0 : if (NS_WARN_IF(aRv.Failed())) {
60 0 : return nullptr;
61 : }
62 :
63 0 : int64_t start = aStart.WasPassed() ? aStart.Value() : 0;
64 0 : int64_t end = aEnd.WasPassed() ? aEnd.Value() : (int64_t)thisLength;
65 :
66 0 : ParseSize((int64_t)thisLength, start, end);
67 :
68 0 : nsAutoString type(aContentType);
69 0 : Blob::MakeValidBlobType(type);
70 0 : return CreateSlice((uint64_t)start, (uint64_t)(end - start), type, aRv);
71 : }
72 :
73 0 : NS_IMPL_ISUPPORTS(BlobImpl, BlobImpl)
74 :
75 : } // namespace dom
76 : } // namespace mozilla
|