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::AppendUTF8String(const nsACString
& aUTF8String
,
36 if (NS_WARN_IF(!utf8Str
.Assign(aUTF8String
, mozilla::fallible
))) {
37 return NS_ERROR_OUT_OF_MEMORY
;
41 if (utf8Str
.Contains('\r')) {
43 !utf8Str
.ReplaceSubstring("\r\n", "\n", mozilla::fallible
) ||
44 !utf8Str
.ReplaceSubstring("\r", "\n", mozilla::fallible
))) {
45 return NS_ERROR_OUT_OF_MEMORY
;
50 !utf8Str
.ReplaceSubstring("\n", "\r\n", mozilla::fallible
))) {
51 return NS_ERROR_OUT_OF_MEMORY
;
56 RefPtr
<StringBlobImpl
> blobImpl
= StringBlobImpl::Create(utf8Str
, u
""_ns
);
57 return AppendBlobImpl(blobImpl
);
60 nsresult
BlobSet::AppendBlobImpl(BlobImpl
* aBlobImpl
) {
61 NS_ENSURE_ARG_POINTER(aBlobImpl
);
63 // If aBlobImpl is a MultipartBlobImpl, let's append the sub-blobImpls
65 const nsTArray
<RefPtr
<BlobImpl
>>* subBlobs
= aBlobImpl
->GetSubBlobImpls();
67 for (BlobImpl
* subBlob
: *subBlobs
) {
68 nsresult rv
= AppendBlobImpl(subBlob
);
69 if (NS_WARN_IF(NS_FAILED(rv
))) {
77 if (NS_WARN_IF(!mBlobImpls
.AppendElement(aBlobImpl
, fallible
))) {
78 return NS_ERROR_OUT_OF_MEMORY
;
83 } // namespace mozilla::dom