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/. */
7 #include "mozilla/dom/BlobSet.h"
8 #include "mozilla/CheckedInt.h"
9 #include "mozilla/dom/File.h"
10 #include "MemoryBlobImpl.h"
11 #include "MultipartBlobImpl.h"
12 #include "StringBlobImpl.h"
14 namespace mozilla::dom
{
16 nsresult
BlobSet::AppendVoidPtr(const void* aData
, uint32_t aLength
) {
17 NS_ENSURE_ARG_POINTER(aData
);
22 void* data
= malloc(aLength
);
24 return NS_ERROR_OUT_OF_MEMORY
;
27 memcpy((char*)data
, aData
, aLength
);
29 RefPtr
<BlobImpl
> blobImpl
= new MemoryBlobImpl(data
, aLength
, u
""_ns
);
30 return AppendBlobImpl(blobImpl
);
33 nsresult
BlobSet::AppendVector(Vector
<uint8_t>&& aData
) {
34 size_t length
= aData
.length();
35 RefPtr
<BlobImpl
> blobImpl
=
36 new MemoryBlobImpl(aData
.extractOrCopyRawBuffer(), length
, u
""_ns
);
37 return AppendBlobImpl(blobImpl
);
40 nsresult
BlobSet::AppendUTF8String(const nsACString
& aUTF8String
,
43 if (NS_WARN_IF(!utf8Str
.Assign(aUTF8String
, mozilla::fallible
))) {
44 return NS_ERROR_OUT_OF_MEMORY
;
48 if (utf8Str
.Contains('\r')) {
50 !utf8Str
.ReplaceSubstring("\r\n", "\n", mozilla::fallible
) ||
51 !utf8Str
.ReplaceSubstring("\r", "\n", mozilla::fallible
))) {
52 return NS_ERROR_OUT_OF_MEMORY
;
57 !utf8Str
.ReplaceSubstring("\n", "\r\n", mozilla::fallible
))) {
58 return NS_ERROR_OUT_OF_MEMORY
;
63 RefPtr
<StringBlobImpl
> blobImpl
= StringBlobImpl::Create(utf8Str
, u
""_ns
);
64 return AppendBlobImpl(blobImpl
);
67 nsresult
BlobSet::AppendBlobImpl(BlobImpl
* aBlobImpl
) {
68 NS_ENSURE_ARG_POINTER(aBlobImpl
);
70 // If aBlobImpl is a MultipartBlobImpl, let's append the sub-blobImpls
72 const nsTArray
<RefPtr
<BlobImpl
>>* subBlobs
= aBlobImpl
->GetSubBlobImpls();
74 for (BlobImpl
* subBlob
: *subBlobs
) {
75 nsresult rv
= AppendBlobImpl(subBlob
);
76 if (NS_WARN_IF(NS_FAILED(rv
))) {
84 if (NS_WARN_IF(!mBlobImpls
.AppendElement(aBlobImpl
, fallible
))) {
85 return NS_ERROR_OUT_OF_MEMORY
;
90 } // namespace mozilla::dom