2019-04-29 04:10:42 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "SnappyUtils.h"
|
|
|
|
|
2020-10-21 13:17:18 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include "mozilla/Assertions.h"
|
2021-11-22 15:53:45 +00:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2020-10-21 13:17:18 +00:00
|
|
|
#include "mozilla/fallible.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsString.h"
|
2019-04-29 04:10:42 +00:00
|
|
|
#include "snappy/snappy.h"
|
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
namespace mozilla::dom {
|
2019-04-29 04:10:42 +00:00
|
|
|
|
2024-04-08 13:48:06 +00:00
|
|
|
static_assert(SNAPPY_VERSION == 0x010200);
|
2022-07-12 15:38:23 +00:00
|
|
|
|
2019-04-29 04:10:42 +00:00
|
|
|
bool SnappyCompress(const nsACString& aSource, nsACString& aDest) {
|
|
|
|
MOZ_ASSERT(!aSource.IsVoid());
|
|
|
|
|
|
|
|
size_t uncompressedLength = aSource.Length();
|
|
|
|
|
|
|
|
if (uncompressedLength <= 16) {
|
2019-09-14 05:33:01 +00:00
|
|
|
aDest.SetIsVoid(true);
|
|
|
|
return true;
|
2019-04-29 04:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t compressedLength = snappy::MaxCompressedLength(uncompressedLength);
|
|
|
|
|
2019-09-14 05:33:01 +00:00
|
|
|
if (NS_WARN_IF(!aDest.SetLength(compressedLength, fallible))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-29 04:10:42 +00:00
|
|
|
|
|
|
|
snappy::RawCompress(aSource.BeginReading(), uncompressedLength,
|
|
|
|
aDest.BeginWriting(), &compressedLength);
|
|
|
|
|
|
|
|
if (compressedLength >= uncompressedLength) {
|
2019-09-14 05:33:01 +00:00
|
|
|
aDest.SetIsVoid(true);
|
|
|
|
return true;
|
2019-04-29 04:10:42 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 05:33:01 +00:00
|
|
|
if (NS_WARN_IF(!aDest.SetLength(compressedLength, fallible))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-29 04:10:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SnappyUncompress(const nsACString& aSource, nsACString& aDest) {
|
|
|
|
MOZ_ASSERT(!aSource.IsVoid());
|
|
|
|
|
|
|
|
const char* compressed = aSource.BeginReading();
|
|
|
|
|
2021-11-22 15:53:45 +00:00
|
|
|
auto compressedLength = static_cast<size_t>(aSource.Length());
|
2019-04-29 04:10:42 +00:00
|
|
|
|
2021-11-22 15:53:45 +00:00
|
|
|
size_t uncompressedLength = 0u;
|
2019-04-29 04:10:42 +00:00
|
|
|
if (!snappy::GetUncompressedLength(compressed, compressedLength,
|
|
|
|
&uncompressedLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-22 15:53:45 +00:00
|
|
|
CheckedUint32 checkedLength(uncompressedLength);
|
|
|
|
if (!checkedLength.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDest.SetLength(checkedLength.value());
|
2019-04-29 04:10:42 +00:00
|
|
|
|
|
|
|
if (!snappy::RawUncompress(compressed, compressedLength,
|
|
|
|
aDest.BeginWriting())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
} // namespace mozilla::dom
|