mfbt/Compression.cpp
author L10n Bumper Bot <release+l10nbumper@mozilla.com>
Mon, 14 Oct 2019 17:11:10 +0000
changeset 558722 e1479d4d94da4261b90f09b0246f8de6d49756bb
parent 557739 992e2a6359f950a35bb9f01f77e6e6c8f7ed6740
permissions -rw-r--r--
no bug - Bumping Firefox l10n changesets r=release a=l10n-bump CLOSED TREE ach -> 9930b9972888 af -> f680dca19252 an -> 3b9d2ca374e3 ar -> a81e0d969918 ast -> 07782c298b80 az -> 32f6ae433ce3 be -> dac4e5f1c04f bg -> c224f0a09740 bn -> 647446bd205b bo -> removed br -> 89cc8224402b brx -> removed bs -> a7b8bb8fa791 ca -> 7b8a844bc8da ca-valencia -> d1ca5a95821e cak -> a90c6c823863 cs -> 003cbfa3debe cy -> ce274e425839 da -> 4560b38f91e3 de -> 1891d7f2cb5d dsb -> 89cedd1ba006 el -> 3d2f681da663 en-CA -> ea6730e239b7 en-GB -> 70dc4d8a8b3b eo -> cd145a0311e7 es-AR -> 5cba1eb7de74 es-CL -> c253d033d6b8 es-ES -> 6065243905d7 es-MX -> 93af5dbf7b36 et -> a94047fa77c0 eu -> a09fccbc24e7 fa -> 4e3683ccb0b5 ff -> 1da18cc49675 fi -> 686476895de2 fr -> 1fa8b6c5ce76 fy-NL -> c07b295f7cec ga-IE -> fbf4db0c9668 gd -> 41eb9778f6f7 gl -> 0eac79e7d451 gn -> 7f4dffc21d8f gu-IN -> a32d72c3a770 he -> cc9e38980653 hi-IN -> 6134ac98f87a hr -> 57cf30d7abe7 hsb -> 9c29e852940a hu -> 889bbee73f4a hy-AM -> 3bb93cb8f309 ia -> 6e8f36d3ff8e id -> 463d765b62d6 is -> 8d5fac9ff9a0 it -> e06b4bd08d66 ja -> 30d6a7edcf44 ja-JP-mac -> 8666fb9f34cc ka -> ae752c568715 kab -> 6414b401bb52 kk -> 156f9d6d8e2d km -> 997e80c1f6e0 kn -> c36d023ad255 ko -> 9e452324c3f6 lij -> 15f9c47a0d0d lo -> removed lt -> fe044f395ae3 ltg -> removed lv -> 3575945b8506 mk -> ed8f99069461 mr -> ea7626d10de6 ms -> 5a31f2148f3c my -> 2617d6da8ac5 nb-NO -> 12ad2cf0a778 ne-NP -> 3d870e50fd8e nl -> d96806e77bb7 nn-NO -> a12fa5329147 oc -> 72ec591aa33b pa-IN -> 4a2345792d94 pl -> 2a5b6337fc49 pt-BR -> 2c4fac89b983 pt-PT -> 27e9dc1df0be rm -> 819d0f4fedcf ro -> 2687a75e2990 ru -> f73048e29c42 si -> 972eab7ec419 sk -> f2d86c144b15 sl -> a64bd3c1720f son -> badbe5fc8df7 sq -> 1aba12cf1050 sr -> 59ec22003a78 sv-SE -> 9ff207966189 ta -> 394023514455 te -> 5003cb435de3 th -> ae09487f86e7 tl -> removed tr -> 3f1ef8c925a0 trs -> 4c04e4e52a35 uk -> 7b60875fbef2 ur -> e0e9963cc2f8 uz -> f96502d1fb00 vi -> d64e6ad1e326 wo -> removed xh -> 9c746071b529 zh-CN -> 31a21d4a8e0c zh-TW -> b9af93c6057f

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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 "mozilla/Compression.h"
#include "mozilla/CheckedInt.h"

// Without including <string>, MSVC 2015 complains about e.g. the impossibility
// to convert `const void* const` to `void*` when calling memchr from
// corecrt_memory.h.
#include <string>

#include "lz4/lz4.h"
#include "lz4/lz4frame.h"

using namespace mozilla;
using namespace mozilla::Compression;

/* Our wrappers */

size_t LZ4::compress(const char* aSource, size_t aInputSize, char* aDest) {
  CheckedInt<int> inputSizeChecked = aInputSize;
  MOZ_ASSERT(inputSizeChecked.isValid());
  return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
                              LZ4_compressBound(inputSizeChecked.value()));
}

size_t LZ4::compressLimitedOutput(const char* aSource, size_t aInputSize,
                                  char* aDest, size_t aMaxOutputSize) {
  CheckedInt<int> inputSizeChecked = aInputSize;
  MOZ_ASSERT(inputSizeChecked.isValid());
  CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
  MOZ_ASSERT(maxOutputSizeChecked.isValid());
  return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
                              maxOutputSizeChecked.value());
}

bool LZ4::decompress(const char* aSource, char* aDest, size_t aOutputSize) {
  CheckedInt<int> outputSizeChecked = aOutputSize;
  MOZ_ASSERT(outputSizeChecked.isValid());
  int ret = LZ4_decompress_fast(aSource, aDest, outputSizeChecked.value());
  return ret >= 0;
}

bool LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
                     size_t aMaxOutputSize, size_t* aOutputSize) {
  CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
  MOZ_ASSERT(maxOutputSizeChecked.isValid());
  CheckedInt<int> inputSizeChecked = aInputSize;
  MOZ_ASSERT(inputSizeChecked.isValid());

  int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
                                maxOutputSizeChecked.value());
  if (ret >= 0) {
    *aOutputSize = ret;
    return true;
  }

  *aOutputSize = 0;
  return false;
}

bool LZ4::decompressPartial(const char* aSource, size_t aInputSize, char* aDest,
                            size_t aMaxOutputSize, size_t* aOutputSize) {
  CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
  MOZ_ASSERT(maxOutputSizeChecked.isValid());
  CheckedInt<int> inputSizeChecked = aInputSize;
  MOZ_ASSERT(inputSizeChecked.isValid());

  int ret = LZ4_decompress_safe_partial(
      aSource, aDest, inputSizeChecked.value(), maxOutputSizeChecked.value(),
      maxOutputSizeChecked.value());
  if (ret >= 0) {
    *aOutputSize = ret;
    return true;
  }

  *aOutputSize = 0;
  return false;
}

LZ4FrameCompressionContext::LZ4FrameCompressionContext(int aCompressionLevel,
                                                       size_t aMaxSrcSize,
                                                       bool aChecksum,
                                                       bool aStableSrc)
    : mContext(nullptr),
      mCompressionLevel(aCompressionLevel),
      mGenerateChecksum(aChecksum),
      mStableSrc(aStableSrc),
      mMaxSrcSize(aMaxSrcSize),
      mWriteBufLen(0) {
  LZ4F_contentChecksum_t checksum =
      mGenerateChecksum ? LZ4F_contentChecksumEnabled : LZ4F_noContentChecksum;
  LZ4F_preferences_t prefs = {
      {
          LZ4F_max256KB,
          LZ4F_blockLinked,
          checksum,
      },
      mCompressionLevel,
  };
  mWriteBufLen = LZ4F_compressBound(mMaxSrcSize, &prefs);
  LZ4F_errorCode_t err = LZ4F_createCompressionContext(&mContext, LZ4F_VERSION);
  MOZ_RELEASE_ASSERT(!LZ4F_isError(err));
}

LZ4FrameCompressionContext::~LZ4FrameCompressionContext() {
  LZ4F_freeCompressionContext(mContext);
}

Result<Span<const char>, size_t> LZ4FrameCompressionContext::BeginCompressing(
    Span<char> aWriteBuffer) {
  mWriteBuffer = aWriteBuffer;
  LZ4F_contentChecksum_t checksum =
      mGenerateChecksum ? LZ4F_contentChecksumEnabled : LZ4F_noContentChecksum;
  LZ4F_preferences_t prefs = {
      {
          LZ4F_max256KB,
          LZ4F_blockLinked,
          checksum,
      },
      mCompressionLevel,
  };
  size_t headerSize = LZ4F_compressBegin(mContext, mWriteBuffer.Elements(),
                                         mWriteBufLen, &prefs);
  if (LZ4F_isError(headerSize)) {
    return Err(headerSize);
  }

  return MakeSpan(static_cast<const char*>(mWriteBuffer.Elements()),
                  headerSize);
}

Result<Span<const char>, size_t>
LZ4FrameCompressionContext::ContinueCompressing(Span<const char> aInput) {
  LZ4F_compressOptions_t opts = {};
  opts.stableSrc = (uint32_t)mStableSrc;
  size_t outputSize =
      LZ4F_compressUpdate(mContext, mWriteBuffer.Elements(), mWriteBufLen,
                          aInput.Elements(), aInput.Length(), &opts);
  if (LZ4F_isError(outputSize)) {
    return Err(outputSize);
  }

  return MakeSpan(static_cast<const char*>(mWriteBuffer.Elements()),
                  outputSize);
}

Result<Span<const char>, size_t> LZ4FrameCompressionContext::EndCompressing() {
  size_t outputSize =
      LZ4F_compressEnd(mContext, mWriteBuffer.Elements(), mWriteBufLen,
                       /* options */ nullptr);
  if (LZ4F_isError(outputSize)) {
    return Err(outputSize);
  }

  return MakeSpan(static_cast<const char*>(mWriteBuffer.Elements()),
                  outputSize);
}

LZ4FrameDecompressionContext::LZ4FrameDecompressionContext(bool aStableDest)
    : mContext(nullptr), mStableDest(aStableDest) {
  LZ4F_errorCode_t err =
      LZ4F_createDecompressionContext(&mContext, LZ4F_VERSION);
  MOZ_RELEASE_ASSERT(!LZ4F_isError(err));
}

LZ4FrameDecompressionContext::~LZ4FrameDecompressionContext() {
  LZ4F_freeDecompressionContext(mContext);
}

Result<LZ4FrameDecompressionResult, size_t>
LZ4FrameDecompressionContext::Decompress(Span<char> aOutput,
                                         Span<const char> aInput) {
  LZ4F_decompressOptions_t opts = {};
  opts.stableDst = (uint32_t)mStableDest;
  size_t outBytes = aOutput.Length();
  size_t inBytes = aInput.Length();
  size_t result = LZ4F_decompress(mContext, aOutput.Elements(), &outBytes,
                                  aInput.Elements(), &inBytes, &opts);
  if (LZ4F_isError(result)) {
    return Err(result);
  }

  LZ4FrameDecompressionResult decompressionResult = {};
  decompressionResult.mFinished = !result;
  decompressionResult.mSizeRead = inBytes;
  decompressionResult.mSizeWritten = outBytes;
  return decompressionResult;
}