Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / io / nsBinaryStream.cpp
blob555edf534551b44f7e82114945d2fbb0d0ddbe56
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 /**
8 * This file contains implementations of the nsIBinaryInputStream and
9 * nsIBinaryOutputStream interfaces. Together, these interfaces allows reading
10 * and writing of primitive data types (integers, floating-point values,
11 * booleans, etc.) to a stream in a binary, untagged, fixed-endianness format.
12 * This might be used, for example, to implement network protocols or to
13 * produce architecture-neutral binary disk files, i.e. ones that can be read
14 * and written by both big-endian and little-endian platforms. Output is
15 * written in big-endian order (high-order byte first), as this is traditional
16 * network order.
18 * @See nsIBinaryInputStream
19 * @See nsIBinaryOutputStream
21 #include <algorithm>
22 #include <string.h>
24 #include "nsBinaryStream.h"
26 #include "mozilla/EndianUtils.h"
27 #include "mozilla/PodOperations.h"
28 #include "mozilla/RefPtr.h"
29 #include "mozilla/Span.h"
30 #include "mozilla/UniquePtr.h"
32 #include "nsCRT.h"
33 #include "nsString.h"
34 #include "nsISerializable.h"
35 #include "nsIClassInfo.h"
36 #include "nsComponentManagerUtils.h"
37 #include "nsIURI.h" // for NS_IURI_IID
38 #include "nsIX509Cert.h" // for NS_IX509CERT_IID
40 #include "js/ArrayBuffer.h" // JS::{GetArrayBuffer{,ByteLength},IsArrayBufferObject}
41 #include "js/GCAPI.h" // JS::AutoCheckCannotGC
42 #include "js/RootingAPI.h" // JS::{Handle,Rooted}
43 #include "js/Value.h" // JS::Value
45 using mozilla::AsBytes;
46 using mozilla::MakeUnique;
47 using mozilla::PodCopy;
48 using mozilla::Span;
49 using mozilla::UniquePtr;
51 already_AddRefed<nsIObjectOutputStream> NS_NewObjectOutputStream(
52 nsIOutputStream* aOutputStream) {
53 MOZ_ASSERT(aOutputStream);
54 auto stream = mozilla::MakeRefPtr<nsBinaryOutputStream>();
56 MOZ_ALWAYS_SUCCEEDS(stream->SetOutputStream(aOutputStream));
57 return stream.forget();
60 already_AddRefed<nsIObjectInputStream> NS_NewObjectInputStream(
61 nsIInputStream* aInputStream) {
62 MOZ_ASSERT(aInputStream);
63 auto stream = mozilla::MakeRefPtr<nsBinaryInputStream>();
65 MOZ_ALWAYS_SUCCEEDS(stream->SetInputStream(aInputStream));
66 return stream.forget();
69 NS_IMPL_ISUPPORTS(nsBinaryOutputStream, nsIObjectOutputStream,
70 nsIBinaryOutputStream, nsIOutputStream)
72 NS_IMETHODIMP
73 nsBinaryOutputStream::Flush() {
74 if (NS_WARN_IF(!mOutputStream)) {
75 return NS_ERROR_UNEXPECTED;
77 return mOutputStream->Flush();
80 NS_IMETHODIMP
81 nsBinaryOutputStream::Close() {
82 if (NS_WARN_IF(!mOutputStream)) {
83 return NS_ERROR_UNEXPECTED;
85 return mOutputStream->Close();
88 NS_IMETHODIMP
89 nsBinaryOutputStream::StreamStatus() {
90 if (NS_WARN_IF(!mOutputStream)) {
91 return NS_ERROR_UNEXPECTED;
93 return mOutputStream->StreamStatus();
96 NS_IMETHODIMP
97 nsBinaryOutputStream::Write(const char* aBuf, uint32_t aCount,
98 uint32_t* aActualBytes) {
99 if (NS_WARN_IF(!mOutputStream)) {
100 return NS_ERROR_UNEXPECTED;
102 return mOutputStream->Write(aBuf, aCount, aActualBytes);
105 NS_IMETHODIMP
106 nsBinaryOutputStream::WriteFrom(nsIInputStream* aInStr, uint32_t aCount,
107 uint32_t* aResult) {
108 MOZ_ASSERT_UNREACHABLE("WriteFrom");
109 return NS_ERROR_NOT_IMPLEMENTED;
112 NS_IMETHODIMP
113 nsBinaryOutputStream::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
114 uint32_t aCount, uint32_t* aResult) {
115 MOZ_ASSERT_UNREACHABLE("WriteSegments");
116 return NS_ERROR_NOT_IMPLEMENTED;
119 NS_IMETHODIMP
120 nsBinaryOutputStream::IsNonBlocking(bool* aNonBlocking) {
121 if (NS_WARN_IF(!mOutputStream)) {
122 return NS_ERROR_UNEXPECTED;
124 return mOutputStream->IsNonBlocking(aNonBlocking);
127 nsresult nsBinaryOutputStream::WriteFully(const char* aBuf, uint32_t aCount) {
128 if (NS_WARN_IF(!mOutputStream)) {
129 return NS_ERROR_UNEXPECTED;
132 nsresult rv;
133 uint32_t bytesWritten;
135 rv = mOutputStream->Write(aBuf, aCount, &bytesWritten);
136 if (NS_FAILED(rv)) {
137 return rv;
139 if (bytesWritten != aCount) {
140 return NS_ERROR_FAILURE;
142 return NS_OK;
145 NS_IMETHODIMP
146 nsBinaryOutputStream::SetOutputStream(nsIOutputStream* aOutputStream) {
147 if (NS_WARN_IF(!aOutputStream)) {
148 return NS_ERROR_INVALID_ARG;
150 mOutputStream = aOutputStream;
151 mBufferAccess = do_QueryInterface(aOutputStream);
152 return NS_OK;
155 NS_IMETHODIMP
156 nsBinaryOutputStream::WriteBoolean(bool aBoolean) { return Write8(aBoolean); }
158 NS_IMETHODIMP
159 nsBinaryOutputStream::Write8(uint8_t aByte) {
160 return WriteFully((const char*)&aByte, sizeof(aByte));
163 NS_IMETHODIMP
164 nsBinaryOutputStream::Write16(uint16_t aNum) {
165 aNum = mozilla::NativeEndian::swapToBigEndian(aNum);
166 return WriteFully((const char*)&aNum, sizeof(aNum));
169 NS_IMETHODIMP
170 nsBinaryOutputStream::Write32(uint32_t aNum) {
171 aNum = mozilla::NativeEndian::swapToBigEndian(aNum);
172 return WriteFully((const char*)&aNum, sizeof(aNum));
175 NS_IMETHODIMP
176 nsBinaryOutputStream::Write64(uint64_t aNum) {
177 nsresult rv;
178 uint32_t bytesWritten;
180 aNum = mozilla::NativeEndian::swapToBigEndian(aNum);
181 rv = Write(reinterpret_cast<char*>(&aNum), sizeof(aNum), &bytesWritten);
182 if (NS_FAILED(rv)) {
183 return rv;
185 if (bytesWritten != sizeof(aNum)) {
186 return NS_ERROR_FAILURE;
188 return rv;
191 NS_IMETHODIMP
192 nsBinaryOutputStream::WriteFloat(float aFloat) {
193 static_assert(sizeof(float) == sizeof(uint32_t),
194 "False assumption about sizeof(float)");
195 return Write32(*reinterpret_cast<uint32_t*>(&aFloat));
198 NS_IMETHODIMP
199 nsBinaryOutputStream::WriteDouble(double aDouble) {
200 static_assert(sizeof(double) == sizeof(uint64_t),
201 "False assumption about sizeof(double)");
202 return Write64(*reinterpret_cast<uint64_t*>(&aDouble));
205 NS_IMETHODIMP
206 nsBinaryOutputStream::WriteStringZ(const char* aString) {
207 uint32_t length;
208 nsresult rv;
210 length = strlen(aString);
211 rv = Write32(length);
212 if (NS_FAILED(rv)) {
213 return rv;
215 return WriteFully(aString, length);
218 NS_IMETHODIMP
219 nsBinaryOutputStream::WriteWStringZ(const char16_t* aString) {
220 uint32_t length = NS_strlen(aString);
221 nsresult rv = Write32(length);
222 if (NS_FAILED(rv)) {
223 return rv;
226 if (length == 0) {
227 return NS_OK;
230 #ifdef IS_BIG_ENDIAN
231 rv = WriteBytes(AsBytes(Span(aString, length)));
232 #else
233 // XXX use WriteSegments here to avoid copy!
234 char16_t* copy;
235 char16_t temp[64];
236 if (length <= 64) {
237 copy = temp;
238 } else {
239 copy = static_cast<char16_t*>(malloc(length * sizeof(char16_t)));
240 if (!copy) {
241 return NS_ERROR_OUT_OF_MEMORY;
244 NS_ASSERTION((uintptr_t(aString) & 0x1) == 0, "aString not properly aligned");
245 mozilla::NativeEndian::copyAndSwapToBigEndian(copy, aString, length);
246 rv = WriteBytes(AsBytes(Span(copy, length)));
247 if (copy != temp) {
248 free(copy);
250 #endif
252 return rv;
255 NS_IMETHODIMP
256 nsBinaryOutputStream::WriteUtf8Z(const char16_t* aString) {
257 return WriteStringZ(NS_ConvertUTF16toUTF8(aString).get());
260 nsresult nsBinaryOutputStream::WriteBytes(Span<const uint8_t> aBytes) {
261 nsresult rv;
262 uint32_t bytesWritten;
264 rv = Write(reinterpret_cast<const char*>(aBytes.Elements()), aBytes.Length(),
265 &bytesWritten);
266 if (NS_FAILED(rv)) {
267 return rv;
269 if (bytesWritten != aBytes.Length()) {
270 return NS_ERROR_FAILURE;
272 return rv;
275 NS_IMETHODIMP
276 nsBinaryOutputStream::WriteBytesFromJS(const char* aString, uint32_t aLength) {
277 return WriteBytes(AsBytes(Span(aString, aLength)));
280 NS_IMETHODIMP
281 nsBinaryOutputStream::WriteByteArray(const nsTArray<uint8_t>& aByteArray) {
282 return WriteBytes(aByteArray);
285 NS_IMETHODIMP
286 nsBinaryOutputStream::WriteObject(nsISupports* aObject, bool aIsStrongRef) {
287 return WriteCompoundObject(aObject, NS_GET_IID(nsISupports), aIsStrongRef);
290 NS_IMETHODIMP
291 nsBinaryOutputStream::WriteSingleRefObject(nsISupports* aObject) {
292 return WriteCompoundObject(aObject, NS_GET_IID(nsISupports), true);
295 NS_IMETHODIMP
296 nsBinaryOutputStream::WriteCompoundObject(nsISupports* aObject,
297 const nsIID& aIID,
298 bool aIsStrongRef) {
299 nsCOMPtr<nsIClassInfo> classInfo = do_QueryInterface(aObject);
300 nsCOMPtr<nsISerializable> serializable = do_QueryInterface(aObject);
302 // Can't deal with weak refs
303 if (NS_WARN_IF(!aIsStrongRef)) {
304 return NS_ERROR_UNEXPECTED;
306 if (NS_WARN_IF(!classInfo) || NS_WARN_IF(!serializable)) {
307 return NS_ERROR_NOT_AVAILABLE;
310 nsCID cid;
311 nsresult rv = classInfo->GetClassIDNoAlloc(&cid);
312 if (NS_SUCCEEDED(rv)) {
313 rv = WriteID(cid);
314 } else {
315 nsCID* cidptr = nullptr;
316 rv = classInfo->GetClassID(&cidptr);
317 if (NS_WARN_IF(NS_FAILED(rv))) {
318 return rv;
321 rv = WriteID(*cidptr);
323 free(cidptr);
326 if (NS_WARN_IF(NS_FAILED(rv))) {
327 return rv;
330 rv = WriteID(aIID);
331 if (NS_WARN_IF(NS_FAILED(rv))) {
332 return rv;
335 return serializable->Write(this);
338 NS_IMETHODIMP
339 nsBinaryOutputStream::WriteID(const nsIID& aIID) {
340 nsresult rv = Write32(aIID.m0);
341 if (NS_WARN_IF(NS_FAILED(rv))) {
342 return rv;
345 rv = Write16(aIID.m1);
346 if (NS_WARN_IF(NS_FAILED(rv))) {
347 return rv;
350 rv = Write16(aIID.m2);
351 if (NS_WARN_IF(NS_FAILED(rv))) {
352 return rv;
355 rv = WriteBytes(aIID.m3);
356 if (NS_WARN_IF(NS_FAILED(rv))) {
357 return rv;
360 return NS_OK;
363 NS_IMETHODIMP_(char*)
364 nsBinaryOutputStream::GetBuffer(uint32_t aLength, uint32_t aAlignMask) {
365 if (mBufferAccess) {
366 return mBufferAccess->GetBuffer(aLength, aAlignMask);
368 return nullptr;
371 NS_IMETHODIMP_(void)
372 nsBinaryOutputStream::PutBuffer(char* aBuffer, uint32_t aLength) {
373 if (mBufferAccess) {
374 mBufferAccess->PutBuffer(aBuffer, aLength);
378 NS_IMPL_ISUPPORTS(nsBinaryInputStream, nsIObjectInputStream,
379 nsIBinaryInputStream, nsIInputStream)
381 NS_IMETHODIMP
382 nsBinaryInputStream::Available(uint64_t* aResult) {
383 if (NS_WARN_IF(!mInputStream)) {
384 return NS_ERROR_UNEXPECTED;
386 return mInputStream->Available(aResult);
389 NS_IMETHODIMP
390 nsBinaryInputStream::StreamStatus() {
391 if (NS_WARN_IF(!mInputStream)) {
392 return NS_ERROR_UNEXPECTED;
394 return mInputStream->StreamStatus();
397 NS_IMETHODIMP
398 nsBinaryInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aNumRead) {
399 if (NS_WARN_IF(!mInputStream)) {
400 return NS_ERROR_UNEXPECTED;
403 // mInputStream might give us short reads, so deal with that.
404 uint32_t totalRead = 0;
406 uint32_t bytesRead;
407 do {
408 nsresult rv = mInputStream->Read(aBuffer, aCount, &bytesRead);
409 if (rv == NS_BASE_STREAM_WOULD_BLOCK && totalRead != 0) {
410 // We already read some data. Return it.
411 break;
414 if (NS_FAILED(rv)) {
415 return rv;
418 totalRead += bytesRead;
419 aBuffer += bytesRead;
420 aCount -= bytesRead;
421 } while (aCount != 0 && bytesRead != 0);
423 *aNumRead = totalRead;
425 return NS_OK;
428 // when forwarding ReadSegments to mInputStream, we need to make sure
429 // 'this' is being passed to the writer each time. To do this, we need
430 // a thunking function which keeps the real input stream around.
432 // the closure wrapper
433 struct MOZ_STACK_CLASS ReadSegmentsClosure {
434 nsCOMPtr<nsIInputStream> mRealInputStream;
435 void* mRealClosure;
436 nsWriteSegmentFun mRealWriter;
437 nsresult mRealResult;
438 uint32_t mBytesRead; // to properly implement aToOffset
441 // the thunking function
442 static nsresult ReadSegmentForwardingThunk(nsIInputStream* aStream,
443 void* aClosure,
444 const char* aFromSegment,
445 uint32_t aToOffset, uint32_t aCount,
446 uint32_t* aWriteCount) {
447 ReadSegmentsClosure* thunkClosure =
448 reinterpret_cast<ReadSegmentsClosure*>(aClosure);
450 NS_ASSERTION(NS_SUCCEEDED(thunkClosure->mRealResult),
451 "How did this get to be a failure status?");
453 thunkClosure->mRealResult = thunkClosure->mRealWriter(
454 thunkClosure->mRealInputStream, thunkClosure->mRealClosure, aFromSegment,
455 thunkClosure->mBytesRead + aToOffset, aCount, aWriteCount);
457 return thunkClosure->mRealResult;
460 NS_IMETHODIMP
461 nsBinaryInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
462 uint32_t aCount, uint32_t* aResult) {
463 if (NS_WARN_IF(!mInputStream)) {
464 return NS_ERROR_UNEXPECTED;
467 ReadSegmentsClosure thunkClosure = {this, aClosure, aWriter, NS_OK, 0};
469 // mInputStream might give us short reads, so deal with that.
470 uint32_t bytesRead;
471 do {
472 nsresult rv = mInputStream->ReadSegments(ReadSegmentForwardingThunk,
473 &thunkClosure, aCount, &bytesRead);
475 if (rv == NS_BASE_STREAM_WOULD_BLOCK && thunkClosure.mBytesRead != 0) {
476 // We already read some data. Return it.
477 break;
480 if (NS_FAILED(rv)) {
481 return rv;
484 thunkClosure.mBytesRead += bytesRead;
485 aCount -= bytesRead;
486 } while (aCount != 0 && bytesRead != 0 &&
487 NS_SUCCEEDED(thunkClosure.mRealResult));
489 *aResult = thunkClosure.mBytesRead;
491 return NS_OK;
494 NS_IMETHODIMP
495 nsBinaryInputStream::IsNonBlocking(bool* aNonBlocking) {
496 if (NS_WARN_IF(!mInputStream)) {
497 return NS_ERROR_UNEXPECTED;
499 return mInputStream->IsNonBlocking(aNonBlocking);
502 NS_IMETHODIMP
503 nsBinaryInputStream::Close() {
504 if (NS_WARN_IF(!mInputStream)) {
505 return NS_ERROR_UNEXPECTED;
507 return mInputStream->Close();
510 NS_IMETHODIMP
511 nsBinaryInputStream::SetInputStream(nsIInputStream* aInputStream) {
512 if (NS_WARN_IF(!aInputStream)) {
513 return NS_ERROR_INVALID_ARG;
515 mInputStream = aInputStream;
516 mBufferAccess = do_QueryInterface(aInputStream);
517 return NS_OK;
520 NS_IMETHODIMP
521 nsBinaryInputStream::ReadBoolean(bool* aBoolean) {
522 uint8_t byteResult;
523 nsresult rv = Read8(&byteResult);
524 if (NS_FAILED(rv)) {
525 return rv;
527 *aBoolean = !!byteResult;
528 return rv;
531 NS_IMETHODIMP
532 nsBinaryInputStream::Read8(uint8_t* aByte) {
533 nsresult rv;
534 uint32_t bytesRead;
536 rv = Read(reinterpret_cast<char*>(aByte), sizeof(*aByte), &bytesRead);
537 if (NS_FAILED(rv)) {
538 return rv;
540 if (bytesRead != 1) {
541 return NS_ERROR_FAILURE;
543 return rv;
546 NS_IMETHODIMP
547 nsBinaryInputStream::Read16(uint16_t* aNum) {
548 uint32_t bytesRead;
549 nsresult rv = Read(reinterpret_cast<char*>(aNum), sizeof(*aNum), &bytesRead);
550 if (NS_FAILED(rv)) {
551 return rv;
553 if (bytesRead != sizeof(*aNum)) {
554 return NS_ERROR_FAILURE;
556 *aNum = mozilla::NativeEndian::swapFromBigEndian(*aNum);
557 return rv;
560 NS_IMETHODIMP
561 nsBinaryInputStream::Read32(uint32_t* aNum) {
562 uint32_t bytesRead;
563 nsresult rv = Read(reinterpret_cast<char*>(aNum), sizeof(*aNum), &bytesRead);
564 if (NS_FAILED(rv)) {
565 return rv;
567 if (bytesRead != sizeof(*aNum)) {
568 return NS_ERROR_FAILURE;
570 *aNum = mozilla::NativeEndian::swapFromBigEndian(*aNum);
571 return rv;
574 NS_IMETHODIMP
575 nsBinaryInputStream::Read64(uint64_t* aNum) {
576 uint32_t bytesRead;
577 nsresult rv = Read(reinterpret_cast<char*>(aNum), sizeof(*aNum), &bytesRead);
578 if (NS_FAILED(rv)) {
579 return rv;
581 if (bytesRead != sizeof(*aNum)) {
582 return NS_ERROR_FAILURE;
584 *aNum = mozilla::NativeEndian::swapFromBigEndian(*aNum);
585 return rv;
588 NS_IMETHODIMP
589 nsBinaryInputStream::ReadFloat(float* aFloat) {
590 static_assert(sizeof(float) == sizeof(uint32_t),
591 "False assumption about sizeof(float)");
592 return Read32(reinterpret_cast<uint32_t*>(aFloat));
595 NS_IMETHODIMP
596 nsBinaryInputStream::ReadDouble(double* aDouble) {
597 static_assert(sizeof(double) == sizeof(uint64_t),
598 "False assumption about sizeof(double)");
599 return Read64(reinterpret_cast<uint64_t*>(aDouble));
602 static nsresult WriteSegmentToCString(nsIInputStream* aStream, void* aClosure,
603 const char* aFromSegment,
604 uint32_t aToOffset, uint32_t aCount,
605 uint32_t* aWriteCount) {
606 nsACString* outString = static_cast<nsACString*>(aClosure);
608 outString->Append(aFromSegment, aCount);
610 *aWriteCount = aCount;
612 return NS_OK;
615 NS_IMETHODIMP
616 nsBinaryInputStream::ReadCString(nsACString& aString) {
617 nsresult rv;
618 uint32_t length, bytesRead;
620 rv = Read32(&length);
621 if (NS_FAILED(rv)) {
622 return rv;
625 aString.Truncate();
626 rv = ReadSegments(WriteSegmentToCString, &aString, length, &bytesRead);
627 if (NS_FAILED(rv)) {
628 return rv;
631 if (bytesRead != length) {
632 return NS_ERROR_FAILURE;
635 return NS_OK;
638 // sometimes, WriteSegmentToString will be handed an odd-number of
639 // bytes, which means we only have half of the last char16_t
640 struct WriteStringClosure {
641 char16_t* mWriteCursor;
642 bool mHasCarryoverByte;
643 char mCarryoverByte;
646 // there are a few cases we have to account for here:
647 // * even length buffer, no carryover - easy, just append
648 // * odd length buffer, no carryover - the last byte needs to be saved
649 // for carryover
650 // * odd length buffer, with carryover - first byte needs to be used
651 // with the carryover byte, and
652 // the rest of the even length
653 // buffer is appended as normal
654 // * even length buffer, with carryover - the first byte needs to be
655 // used with the previous carryover byte.
656 // this gives you an odd length buffer,
657 // so you have to save the last byte for
658 // the next carryover
660 // same version of the above, but with correct casting and endian swapping
661 static nsresult WriteSegmentToString(nsIInputStream* aStream, void* aClosure,
662 const char* aFromSegment,
663 uint32_t aToOffset, uint32_t aCount,
664 uint32_t* aWriteCount) {
665 MOZ_ASSERT(aCount > 0, "Why are we being told to write 0 bytes?");
666 static_assert(sizeof(char16_t) == 2, "We can't handle other sizes!");
668 WriteStringClosure* closure = static_cast<WriteStringClosure*>(aClosure);
669 char16_t* cursor = closure->mWriteCursor;
671 // we're always going to consume the whole buffer no matter what
672 // happens, so take care of that right now.. that allows us to
673 // tweak aCount later. Do NOT move this!
674 *aWriteCount = aCount;
676 // if the last Write had an odd-number of bytes read, then
677 if (closure->mHasCarryoverByte) {
678 // re-create the two-byte sequence we want to work with
679 char bytes[2] = {closure->mCarryoverByte, *aFromSegment};
680 *cursor = *(char16_t*)bytes;
681 // Now the little endianness dance
682 mozilla::NativeEndian::swapToBigEndianInPlace(cursor, 1);
683 ++cursor;
685 // now skip past the first byte of the buffer.. code from here
686 // can assume normal operations, but should not assume aCount
687 // is relative to the ORIGINAL buffer
688 ++aFromSegment;
689 --aCount;
691 closure->mHasCarryoverByte = false;
694 // this array is possibly unaligned... be careful how we access it!
695 const char16_t* unicodeSegment =
696 reinterpret_cast<const char16_t*>(aFromSegment);
698 // calculate number of full characters in segment (aCount could be odd!)
699 uint32_t segmentLength = aCount / sizeof(char16_t);
701 // copy all data into our aligned buffer. byte swap if necessary.
702 // cursor may be unaligned, so we cannot use copyAndSwapToBigEndian directly
703 memcpy(cursor, unicodeSegment, segmentLength * sizeof(char16_t));
704 char16_t* end = cursor + segmentLength;
705 mozilla::NativeEndian::swapToBigEndianInPlace(cursor, segmentLength);
706 closure->mWriteCursor = end;
708 // remember this is the modifed aCount and aFromSegment,
709 // so that will take into account the fact that we might have
710 // skipped the first byte in the buffer
711 if (aCount % sizeof(char16_t) != 0) {
712 // we must have had a carryover byte, that we'll need the next
713 // time around
714 closure->mCarryoverByte = aFromSegment[aCount - 1];
715 closure->mHasCarryoverByte = true;
718 return NS_OK;
721 NS_IMETHODIMP
722 nsBinaryInputStream::ReadString(nsAString& aString) {
723 nsresult rv;
724 uint32_t length, bytesRead;
726 rv = Read32(&length);
727 if (NS_FAILED(rv)) {
728 return rv;
731 if (length == 0) {
732 aString.Truncate();
733 return NS_OK;
736 // pre-allocate output buffer, and get direct access to buffer...
737 if (!aString.SetLength(length, mozilla::fallible)) {
738 return NS_ERROR_OUT_OF_MEMORY;
741 WriteStringClosure closure;
742 closure.mWriteCursor = aString.BeginWriting();
743 closure.mHasCarryoverByte = false;
745 rv = ReadSegments(WriteSegmentToString, &closure, length * sizeof(char16_t),
746 &bytesRead);
747 if (NS_FAILED(rv)) {
748 return rv;
751 NS_ASSERTION(!closure.mHasCarryoverByte, "some strange stream corruption!");
753 if (bytesRead != length * sizeof(char16_t)) {
754 return NS_ERROR_FAILURE;
757 return NS_OK;
760 nsresult nsBinaryInputStream::ReadBytesToBuffer(uint32_t aLength,
761 uint8_t* aBuffer) {
762 uint32_t bytesRead;
763 nsresult rv = Read(reinterpret_cast<char*>(aBuffer), aLength, &bytesRead);
764 if (NS_FAILED(rv)) {
765 return rv;
767 if (bytesRead != aLength) {
768 return NS_ERROR_FAILURE;
771 return NS_OK;
774 NS_IMETHODIMP
775 nsBinaryInputStream::ReadBytes(uint32_t aLength, char** aResult) {
776 char* s = static_cast<char*>(malloc(aLength));
777 if (!s) {
778 return NS_ERROR_OUT_OF_MEMORY;
781 nsresult rv = ReadBytesToBuffer(aLength, reinterpret_cast<uint8_t*>(s));
782 if (NS_FAILED(rv)) {
783 free(s);
784 return rv;
787 *aResult = s;
788 return NS_OK;
791 NS_IMETHODIMP
792 nsBinaryInputStream::ReadByteArray(uint32_t aLength,
793 nsTArray<uint8_t>& aResult) {
794 if (!aResult.SetLength(aLength, mozilla::fallible)) {
795 return NS_ERROR_OUT_OF_MEMORY;
797 nsresult rv = ReadBytesToBuffer(aLength, aResult.Elements());
798 if (NS_FAILED(rv)) {
799 aResult.Clear();
801 return rv;
804 NS_IMETHODIMP
805 nsBinaryInputStream::ReadArrayBuffer(uint64_t aLength,
806 JS::Handle<JS::Value> aBuffer,
807 JSContext* aCx, uint64_t* aReadLength) {
808 if (!aBuffer.isObject()) {
809 return NS_ERROR_FAILURE;
811 JS::Rooted<JSObject*> buffer(aCx, &aBuffer.toObject());
812 if (!JS::IsArrayBufferObject(buffer)) {
813 return NS_ERROR_FAILURE;
816 size_t bufferLength = JS::GetArrayBufferByteLength(buffer);
817 if (bufferLength < aLength) {
818 return NS_ERROR_FAILURE;
821 uint32_t bufSize = std::min<uint64_t>(aLength, 4096);
822 UniquePtr<char[]> buf = MakeUnique<char[]>(bufSize);
824 uint64_t pos = 0;
825 *aReadLength = 0;
826 do {
827 // Read data into temporary buffer.
828 uint32_t bytesRead;
829 uint32_t amount = std::min<uint64_t>(aLength - pos, bufSize);
830 nsresult rv = Read(buf.get(), amount, &bytesRead);
831 if (NS_WARN_IF(NS_FAILED(rv))) {
832 return rv;
834 MOZ_ASSERT(bytesRead <= amount);
836 if (bytesRead == 0) {
837 break;
840 // Copy data into actual buffer.
842 JS::AutoCheckCannotGC nogc;
843 bool isShared;
844 if (bufferLength != JS::GetArrayBufferByteLength(buffer)) {
845 return NS_ERROR_FAILURE;
848 char* data = reinterpret_cast<char*>(
849 JS::GetArrayBufferData(buffer, &isShared, nogc));
850 MOZ_ASSERT(!isShared); // Implied by JS::GetArrayBufferData()
851 if (!data) {
852 return NS_ERROR_FAILURE;
855 *aReadLength += bytesRead;
856 PodCopy(data + pos, buf.get(), bytesRead);
858 pos += bytesRead;
859 } while (pos < aLength);
861 return NS_OK;
864 NS_IMETHODIMP
865 nsBinaryInputStream::ReadObject(bool aIsStrongRef, nsISupports** aObject) {
866 nsCID cid;
867 nsIID iid;
868 nsresult rv = ReadID(&cid);
869 if (NS_WARN_IF(NS_FAILED(rv))) {
870 return rv;
873 rv = ReadID(&iid);
874 if (NS_WARN_IF(NS_FAILED(rv))) {
875 return rv;
878 // HACK: Intercept old (pre-gecko6) nsIURI IID, and replace with
879 // the updated IID, so that we're QI'ing to an actual interface.
880 // (As soon as we drop support for upgrading from pre-gecko6, we can
881 // remove this chunk.)
882 static const nsIID oldURIiid = {
883 0x7a22cc0,
884 0xce5,
885 0x11d3,
886 {0x93, 0x31, 0x0, 0x10, 0x4b, 0xa0, 0xfd, 0x40}};
888 // hackaround for bug 670542
889 static const nsIID oldURIiid2 = {
890 0xd6d04c36,
891 0x0fa4,
892 0x4db3,
893 {0xbe, 0x05, 0x4a, 0x18, 0x39, 0x71, 0x03, 0xe2}};
895 // hackaround for bug 682031
896 static const nsIID oldURIiid3 = {
897 0x12120b20,
898 0x0929,
899 0x40e9,
900 {0x88, 0xcf, 0x6e, 0x08, 0x76, 0x6e, 0x8b, 0x23}};
902 // hackaround for bug 1195415
903 static const nsIID oldURIiid4 = {
904 0x395fe045,
905 0x7d18,
906 0x4adb,
907 {0xa3, 0xfd, 0xaf, 0x98, 0xc8, 0xa1, 0xaf, 0x11}};
909 if (iid.Equals(oldURIiid) || iid.Equals(oldURIiid2) ||
910 iid.Equals(oldURIiid3) || iid.Equals(oldURIiid4)) {
911 const nsIID newURIiid = NS_IURI_IID;
912 iid = newURIiid;
915 // Hack around bug 1508939
916 // The old CSP serialization can't be handled cleanly when
917 // it's embedded in an old style principal
918 static const nsIID oldCSPiid = {
919 0xb3c4c0ae,
920 0xbd5e,
921 0x4cad,
922 {0x87, 0xe0, 0x8d, 0x21, 0x0d, 0xbb, 0x3f, 0x9f}};
923 if (iid.Equals(oldCSPiid)) {
924 return NS_ERROR_FAILURE;
926 // END HACK
928 // HACK: Service workers store resource security info on disk in the dom
929 // Cache API. When the uuid of the nsIX509Cert interface changes
930 // these serialized objects cannot be loaded any more. This hack
931 // works around this issue.
933 // hackaround for bug 1247580 (FF45 to FF46 transition)
934 static const nsIID oldCertIID = {
935 0xf8ed8364,
936 0xced9,
937 0x4c6e,
938 {0x86, 0xba, 0x48, 0xaf, 0x53, 0xc3, 0x93, 0xe6}};
940 if (iid.Equals(oldCertIID)) {
941 const nsIID newCertIID = NS_IX509CERT_IID;
942 iid = newCertIID;
944 // END HACK
946 nsCOMPtr<nsISupports> object = do_CreateInstance(cid, &rv);
947 if (NS_WARN_IF(NS_FAILED(rv))) {
948 return rv;
951 nsCOMPtr<nsISerializable> serializable = do_QueryInterface(object);
952 if (NS_WARN_IF(!serializable)) {
953 return NS_ERROR_UNEXPECTED;
956 rv = serializable->Read(this);
957 if (NS_WARN_IF(NS_FAILED(rv))) {
958 return rv;
961 return object->QueryInterface(iid, reinterpret_cast<void**>(aObject));
964 NS_IMETHODIMP
965 nsBinaryInputStream::ReadID(nsID* aResult) {
966 nsresult rv = Read32(&aResult->m0);
967 if (NS_WARN_IF(NS_FAILED(rv))) {
968 return rv;
971 rv = Read16(&aResult->m1);
972 if (NS_WARN_IF(NS_FAILED(rv))) {
973 return rv;
976 rv = Read16(&aResult->m2);
977 if (NS_WARN_IF(NS_FAILED(rv))) {
978 return rv;
981 const uint32_t toRead = sizeof(aResult->m3);
982 uint32_t bytesRead = 0;
983 rv = Read(reinterpret_cast<char*>(&aResult->m3[0]), toRead, &bytesRead);
984 if (NS_WARN_IF(NS_FAILED(rv))) {
985 return rv;
987 if (bytesRead != toRead) {
988 return NS_ERROR_FAILURE;
991 return NS_OK;
994 NS_IMETHODIMP_(char*)
995 nsBinaryInputStream::GetBuffer(uint32_t aLength, uint32_t aAlignMask) {
996 if (mBufferAccess) {
997 return mBufferAccess->GetBuffer(aLength, aAlignMask);
999 return nullptr;
1002 NS_IMETHODIMP_(void)
1003 nsBinaryInputStream::PutBuffer(char* aBuffer, uint32_t aLength) {
1004 if (mBufferAccess) {
1005 mBufferAccess->PutBuffer(aBuffer, aLength);