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"
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
, EmptyString());
30 return AppendBlobImpl(blobImpl
);
33 nsresult
BlobSet::AppendString(const nsAString
& aString
, bool nativeEOL
) {
35 if (NS_WARN_IF(!AppendUTF16toUTF8(aString
, utf8Str
, mozilla::fallible
))) {
36 return NS_ERROR_OUT_OF_MEMORY
;
40 if (utf8Str
.Contains('\r')) {
41 utf8Str
.ReplaceSubstring("\r\n", "\n");
42 utf8Str
.ReplaceSubstring("\r", "\n");
45 utf8Str
.ReplaceSubstring("\n", "\r\n");
49 RefPtr
<StringBlobImpl
> blobImpl
=
50 StringBlobImpl::Create(utf8Str
, EmptyString());
51 return AppendBlobImpl(blobImpl
);
54 nsresult
BlobSet::AppendBlobImpl(BlobImpl
* aBlobImpl
) {
55 NS_ENSURE_ARG_POINTER(aBlobImpl
);
57 // If aBlobImpl is a MultipartBlobImpl, let's append the sub-blobImpls
59 const nsTArray
<RefPtr
<BlobImpl
>>* subBlobs
= aBlobImpl
->GetSubBlobImpls();
61 for (BlobImpl
* subBlob
: *subBlobs
) {
62 nsresult rv
= AppendBlobImpl(subBlob
);
63 if (NS_WARN_IF(NS_FAILED(rv
))) {
71 if (NS_WARN_IF(!mBlobImpls
.AppendElement(aBlobImpl
, fallible
))) {
72 return NS_ERROR_OUT_OF_MEMORY
;
78 } // namespace mozilla