/src/mozilla-central/dom/file/BlobSet.cpp
Line | Count | Source (jump to first uncovered line) |
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 "mozilla/dom/BlobSet.h" |
8 | | #include "mozilla/CheckedInt.h" |
9 | | #include "mozilla/dom/File.h" |
10 | | #include "MemoryBlobImpl.h" |
11 | | #include "MultipartBlobImpl.h" |
12 | | |
13 | | namespace mozilla { |
14 | | namespace dom { |
15 | | |
16 | | nsresult |
17 | | BlobSet::AppendVoidPtr(const void* aData, uint32_t aLength) |
18 | 0 | { |
19 | 0 | NS_ENSURE_ARG_POINTER(aData); |
20 | 0 | if (!aLength) { |
21 | 0 | return NS_OK; |
22 | 0 | } |
23 | 0 | |
24 | 0 | void* data = malloc(aLength); |
25 | 0 | if (!data) { |
26 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
27 | 0 | } |
28 | 0 | |
29 | 0 | memcpy((char*)data, aData, aLength); |
30 | 0 |
|
31 | 0 | RefPtr<BlobImpl> blobImpl = new MemoryBlobImpl(data, aLength, EmptyString()); |
32 | 0 | return AppendBlobImpl(blobImpl); |
33 | 0 | } |
34 | | |
35 | | nsresult |
36 | | BlobSet::AppendString(const nsAString& aString, bool nativeEOL) |
37 | 0 | { |
38 | 0 | nsCString utf8Str; |
39 | 0 | if (NS_WARN_IF(!AppendUTF16toUTF8(aString, utf8Str, mozilla::fallible))) { |
40 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
41 | 0 | } |
42 | 0 | |
43 | 0 | if (nativeEOL) { |
44 | 0 | if (utf8Str.Contains('\r')) { |
45 | 0 | utf8Str.ReplaceSubstring("\r\n", "\n"); |
46 | 0 | utf8Str.ReplaceSubstring("\r", "\n"); |
47 | 0 | } |
48 | | #ifdef XP_WIN |
49 | | utf8Str.ReplaceSubstring("\n", "\r\n"); |
50 | | #endif |
51 | | } |
52 | 0 |
|
53 | 0 | RefPtr<StringBlobImpl> blobImpl = |
54 | 0 | StringBlobImpl::Create(utf8Str, EmptyString()); |
55 | 0 | return AppendBlobImpl(blobImpl); |
56 | 0 | } |
57 | | |
58 | | nsresult |
59 | | BlobSet::AppendBlobImpl(BlobImpl* aBlobImpl) |
60 | 0 | { |
61 | 0 | NS_ENSURE_ARG_POINTER(aBlobImpl); |
62 | 0 |
|
63 | 0 | // If aBlobImpl is a MultipartBlobImpl, let's append the sub-blobImpls |
64 | 0 | // instead. |
65 | 0 | const nsTArray<RefPtr<BlobImpl>>* subBlobs = aBlobImpl->GetSubBlobImpls(); |
66 | 0 | if (subBlobs) { |
67 | 0 | for (BlobImpl* subBlob : *subBlobs) { |
68 | 0 | nsresult rv = AppendBlobImpl(subBlob); |
69 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
70 | 0 | return rv; |
71 | 0 | } |
72 | 0 | } |
73 | 0 |
|
74 | 0 | return NS_OK; |
75 | 0 | } |
76 | 0 | |
77 | 0 | if (NS_WARN_IF(!mBlobImpls.AppendElement(aBlobImpl, fallible))) { |
78 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
79 | 0 | } |
80 | 0 | return NS_OK; |
81 | 0 | } |
82 | | |
83 | | } // dom namespace |
84 | | } // mozilla namespace |