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/. */
9 #include "mozilla/CheckedInt.h"
10 #include "mozilla/ErrorResult.h"
11 #include "nsIInputStream.h"
13 namespace mozilla::dom
{
15 // Makes sure that aStart and aEnd is less then or equal to aSize and greater
17 static void ParseSize(int64_t aSize
, int64_t& aStart
, int64_t& aEnd
) {
18 CheckedInt64 newStartOffset
= aStart
;
19 if (aStart
< -aSize
) {
21 } else if (aStart
< 0) {
22 newStartOffset
+= aSize
;
23 } else if (aStart
> aSize
) {
24 newStartOffset
= aSize
;
27 CheckedInt64 newEndOffset
= aEnd
;
30 } else if (aEnd
< 0) {
31 newEndOffset
+= aSize
;
32 } else if (aEnd
> aSize
) {
36 if (!newStartOffset
.isValid() || !newEndOffset
.isValid() ||
37 newStartOffset
.value() >= newEndOffset
.value()) {
40 aStart
= newStartOffset
.value();
41 aEnd
= newEndOffset
.value();
45 already_AddRefed
<BlobImpl
> BlobImpl::Slice(const Optional
<int64_t>& aStart
,
46 const Optional
<int64_t>& aEnd
,
47 const nsAString
& aContentType
,
49 // Truncate aStart and aEnd so that we stay within this file.
50 uint64_t thisLength
= GetSize(aRv
);
51 if (NS_WARN_IF(aRv
.Failed())) {
55 int64_t start
= aStart
.WasPassed() ? aStart
.Value() : 0;
56 int64_t end
= aEnd
.WasPassed() ? aEnd
.Value() : (int64_t)thisLength
;
58 ParseSize((int64_t)thisLength
, start
, end
);
60 nsAutoString
type(aContentType
);
61 Blob::MakeValidBlobType(type
);
62 return CreateSlice((uint64_t)start
, (uint64_t)(end
- start
), type
, aRv
);
65 nsresult
BlobImpl::GetSendInfo(nsIInputStream
** aBody
, uint64_t* aContentLength
,
66 nsACString
& aContentType
, nsACString
& aCharset
) {
67 MOZ_ASSERT(aContentLength
);
71 nsCOMPtr
<nsIInputStream
> stream
;
72 CreateInputStream(getter_AddRefs(stream
), rv
);
73 if (NS_WARN_IF(rv
.Failed())) {
74 return rv
.StealNSResult();
77 *aContentLength
= GetSize(rv
);
78 if (NS_WARN_IF(rv
.Failed())) {
79 return rv
.StealNSResult();
82 nsAutoString contentType
;
85 if (contentType
.IsEmpty()) {
86 aContentType
.SetIsVoid(true);
88 CopyUTF16toUTF8(contentType
, aContentType
);
97 NS_IMPL_ISUPPORTS(BlobImpl
, BlobImpl
)
99 } // namespace mozilla::dom