head	1.2;
access;
symbols
	netbsd-11-1-RELEASE:1.1.1.1.2.1
	netbsd-11-0-RELEASE:1.1.1.1
	netbsd-11-0-RC7:1.1.1.1
	netbsd-11-0-RC6:1.1.1.1
	netbsd-11-0-RC5:1.1.1.1
	netbsd-11-0-RC4:1.1.1.1
	netbsd-11-0-RC3:1.1.1.1
	netbsd-11-0-RC2:1.1.1.1
	netbsd-11-0-RC1:1.1.1.1
	netbsd-11:1.1.1.1.0.2
	netbsd-11-base:1.1.1.1
	brotli-1-0-9:1.1.1.1
	GOOGLE:1.1.1;
locks; strict;
comment	@ * @;


1.2
date	2026.10.28.21.13.12;	author mrg;	state dead;
branches;
next	1.1;
commitid	WySy0JIemcP5Wh1H;

1.1
date	2023.01.29.06.38.34;	author mrg;	state Exp;
branches
	1.1.1.1;
next	;
commitid	gOcuvoN6w4Ry1pbE;

1.1.1.1
date	2023.01.29.06.38.34;	author mrg;	state Exp;
branches
	1.1.1.1.2.1;
next	;
commitid	gOcuvoN6w4Ry1pbE;

1.1.1.1.2.1
date	2026.10.29.16.03.52;	author martin;	state dead;
branches;
next	;
commitid	Zd6vMoBhuWqLbo1H;


desc
@@


1.2
log
@merge brotli 1.2.1.
@
text
@/* Copyright 2013 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/* Sliding window over the input data. */

#ifndef BROTLI_ENC_RINGBUFFER_H_
#define BROTLI_ENC_RINGBUFFER_H_

#include <string.h>  /* memcpy */

#include "../common/platform.h"
#include <brotli/types.h>
#include "./memory.h"
#include "./quality.h"

#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif

/* A RingBuffer(window_bits, tail_bits) contains `1 << window_bits' bytes of
   data in a circular manner: writing a byte writes it to:
     `position() % (1 << window_bits)'.
   For convenience, the RingBuffer array contains another copy of the
   first `1 << tail_bits' bytes:
     buffer_[i] == buffer_[i + (1 << window_bits)], if i < (1 << tail_bits),
   and another copy of the last two bytes:
     buffer_[-1] == buffer_[(1 << window_bits) - 1] and
     buffer_[-2] == buffer_[(1 << window_bits) - 2]. */
typedef struct RingBuffer {
  /* Size of the ring-buffer is (1 << window_bits) + tail_size_. */
  const uint32_t size_;
  const uint32_t mask_;
  const uint32_t tail_size_;
  const uint32_t total_size_;

  uint32_t cur_size_;
  /* Position to write in the ring buffer. */
  uint32_t pos_;
  /* The actual ring buffer containing the copy of the last two bytes, the data,
     and the copy of the beginning as a tail. */
  uint8_t* data_;
  /* The start of the ring-buffer. */
  uint8_t* buffer_;
} RingBuffer;

static BROTLI_INLINE void RingBufferInit(RingBuffer* rb) {
  rb->cur_size_ = 0;
  rb->pos_ = 0;
  rb->data_ = 0;
  rb->buffer_ = 0;
}

static BROTLI_INLINE void RingBufferSetup(
    const BrotliEncoderParams* params, RingBuffer* rb) {
  int window_bits = ComputeRbBits(params);
  int tail_bits = params->lgblock;
  *(uint32_t*)&rb->size_ = 1u << window_bits;
  *(uint32_t*)&rb->mask_ = (1u << window_bits) - 1;
  *(uint32_t*)&rb->tail_size_ = 1u << tail_bits;
  *(uint32_t*)&rb->total_size_ = rb->size_ + rb->tail_size_;
}

static BROTLI_INLINE void RingBufferFree(MemoryManager* m, RingBuffer* rb) {
  BROTLI_FREE(m, rb->data_);
}

/* Allocates or re-allocates data_ to the given length + plus some slack
   region before and after. Fills the slack regions with zeros. */
static BROTLI_INLINE void RingBufferInitBuffer(
    MemoryManager* m, const uint32_t buflen, RingBuffer* rb) {
  static const size_t kSlackForEightByteHashingEverywhere = 7;
  uint8_t* new_data = BROTLI_ALLOC(
      m, uint8_t, 2 + buflen + kSlackForEightByteHashingEverywhere);
  size_t i;
  if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(new_data)) return;
  if (rb->data_) {
    memcpy(new_data, rb->data_,
        2 + rb->cur_size_ + kSlackForEightByteHashingEverywhere);
    BROTLI_FREE(m, rb->data_);
  }
  rb->data_ = new_data;
  rb->cur_size_ = buflen;
  rb->buffer_ = rb->data_ + 2;
  rb->buffer_[-2] = rb->buffer_[-1] = 0;
  for (i = 0; i < kSlackForEightByteHashingEverywhere; ++i) {
    rb->buffer_[rb->cur_size_ + i] = 0;
  }
}

static BROTLI_INLINE void RingBufferWriteTail(
    const uint8_t* bytes, size_t n, RingBuffer* rb) {
  const size_t masked_pos = rb->pos_ & rb->mask_;
  if (BROTLI_PREDICT_FALSE(masked_pos < rb->tail_size_)) {
    /* Just fill the tail buffer with the beginning data. */
    const size_t p = rb->size_ + masked_pos;
    memcpy(&rb->buffer_[p], bytes,
        BROTLI_MIN(size_t, n, rb->tail_size_ - masked_pos));
  }
}

/* Push bytes into the ring buffer. */
static BROTLI_INLINE void RingBufferWrite(
    MemoryManager* m, const uint8_t* bytes, size_t n, RingBuffer* rb) {
  if (rb->pos_ == 0 && n < rb->tail_size_) {
    /* Special case for the first write: to process the first block, we don't
       need to allocate the whole ring-buffer and we don't need the tail
       either. However, we do this memory usage optimization only if the
       first write is less than the tail size, which is also the input block
       size, otherwise it is likely that other blocks will follow and we
       will need to reallocate to the full size anyway. */
    rb->pos_ = (uint32_t)n;
    RingBufferInitBuffer(m, rb->pos_, rb);
    if (BROTLI_IS_OOM(m)) return;
    memcpy(rb->buffer_, bytes, n);
    return;
  }
  if (rb->cur_size_ < rb->total_size_) {
    /* Lazily allocate the full buffer. */
    RingBufferInitBuffer(m, rb->total_size_, rb);
    if (BROTLI_IS_OOM(m)) return;
    /* Initialize the last two bytes to zero, so that we don't have to worry
       later when we copy the last two bytes to the first two positions. */
    rb->buffer_[rb->size_ - 2] = 0;
    rb->buffer_[rb->size_ - 1] = 0;
    /* Initialize tail; might be touched by "best_len++" optimization when
       ring buffer is "full". */
    rb->buffer_[rb->size_] = 241;
  }
  {
    const size_t masked_pos = rb->pos_ & rb->mask_;
    /* The length of the writes is limited so that we do not need to worry
       about a write */
    RingBufferWriteTail(bytes, n, rb);
    if (BROTLI_PREDICT_TRUE(masked_pos + n <= rb->size_)) {
      /* A single write fits. */
      memcpy(&rb->buffer_[masked_pos], bytes, n);
    } else {
      /* Split into two writes.
         Copy into the end of the buffer, including the tail buffer. */
      memcpy(&rb->buffer_[masked_pos], bytes,
             BROTLI_MIN(size_t, n, rb->total_size_ - masked_pos));
      /* Copy into the beginning of the buffer */
      memcpy(&rb->buffer_[0], bytes + (rb->size_ - masked_pos),
             n - (rb->size_ - masked_pos));
    }
  }
  {
    BROTLI_BOOL not_first_lap = (rb->pos_ & (1u << 31)) != 0;
    uint32_t rb_pos_mask = (1u << 31) - 1;
    rb->buffer_[-2] = rb->buffer_[rb->size_ - 2];
    rb->buffer_[-1] = rb->buffer_[rb->size_ - 1];
    rb->pos_ = (rb->pos_ & rb_pos_mask) + (uint32_t)(n & rb_pos_mask);
    if (not_first_lap) {
      /* Wrap, but preserve not-a-first-lap feature. */
      rb->pos_ |= 1u << 31;
    }
  }
}

#if defined(__cplusplus) || defined(c_plusplus)
}  /* extern "C" */
#endif

#endif  /* BROTLI_ENC_RINGBUFFER_H_ */
@


1.1
log
@Initial revision
@
text
@@


1.1.1.1
log
@initial import of brotli 1.0.9.

modern blender wants freetype to have brotli support enabled or it
won't build.  fortunately, enabling the decoder support (all it
needs currently) is relatively easy and small (~160k on amd64)
without exposing brotli as a first class citizen is easy, so this
will be linked into xsrc freetype directly.

XXX: pullup-10?  or perhaps force -10 to use pkgsrc freetype?

from pkgsrc DESCR:
Brotli is a generic-purpose lossless compression algorithm that compresses data
using a combination of a modern variant of the LZ77 algorithm, Huffman coding
and 2nd order context modeling, with a compression ratio comparable to the best
currently available general-purpose compression methods. It is similar in speed
with deflate but offers more dense compression.
@
text
@@


1.1.1.1.2.1
log
@Pull up the following, requested by mrg in ticket #562:

	external/mit/brotli/dist/BUILD.bazel            up to 1.1.1.1
	external/mit/brotli/dist/c/common/dictionary_inc.h up to 1.1.1.1
	external/mit/brotli/dist/c/common/shared_dictionary.c up to 1.1.1.1
	external/mit/brotli/dist/c/common/shared_dictionary_internal.h up to 1.1.1.1
	external/mit/brotli/dist/c/common/static_init.h up to 1.1.1.1
	external/mit/brotli/dist/c/dec/prefix.c         up to 1.1.1.1
	external/mit/brotli/dist/c/dec/prefix_inc.h     up to 1.1.1.1
	external/mit/brotli/dist/c/dec/static_init.c    up to 1.1.1.1
	external/mit/brotli/dist/c/dec/static_init.h    up to 1.1.1.1
	external/mit/brotli/dist/c/include/brotli/shared_dictionary.h up to 1.1.1.1
	external/mit/brotli/dist/docs/brotli.svg        up to 1.1.1.1
	external/mit/brotli/dist/scripts/download_testdata.sh up to 1.1.1.1
	external/mit/brotli/dist/tests/testdata/zerosukkanooa.compressed up to 1.1.1.1
	external/mit/brotli/dist/tests/cli_test.sh      up to 1.1.1.1
	external/mit/brotli/dist/SECURITY.md            up to 1.1.1.1
	external/mit/brotli/dist/.bazelignore           up to 1.1.1.1
	external/mit/brotli/dist/CHANGELOG.md           up to 1.1.1.1
	external/mit/brotli/dist/BUILD                  delete
	external/mit/brotli/dist/Makefile               delete
	external/mit/brotli/dist/Makefile.am            delete
	external/mit/brotli/dist/WORKSPACE              delete
	external/mit/brotli/dist/bootstrap              delete
	external/mit/brotli/dist/compiler_config_setting.bzl delete
	external/mit/brotli/dist/configure              delete
	external/mit/brotli/dist/configure-cmake        delete
	external/mit/brotli/dist/configure.ac           delete
	external/mit/brotli/dist/premake5.lua           delete
	external/mit/brotli/dist/c/enc/backward_references.c delete
	external/mit/brotli/dist/c/enc/backward_references.h delete
	external/mit/brotli/dist/c/enc/backward_references_hq.c delete
	external/mit/brotli/dist/c/enc/backward_references_hq.h delete
	external/mit/brotli/dist/c/enc/backward_references_inc.h delete
	external/mit/brotli/dist/c/enc/bit_cost.c       delete
	external/mit/brotli/dist/c/enc/bit_cost.h       delete
	external/mit/brotli/dist/c/enc/bit_cost_inc.h   delete
	external/mit/brotli/dist/c/enc/block_encoder_inc.h delete
	external/mit/brotli/dist/c/enc/block_splitter.c delete
	external/mit/brotli/dist/c/enc/block_splitter.h delete
	external/mit/brotli/dist/c/enc/block_splitter_inc.h delete
	external/mit/brotli/dist/c/enc/brotli_bit_stream.c delete
	external/mit/brotli/dist/c/enc/brotli_bit_stream.h delete
	external/mit/brotli/dist/c/enc/cluster.c        delete
	external/mit/brotli/dist/c/enc/cluster.h        delete
	external/mit/brotli/dist/c/enc/cluster_inc.h    delete
	external/mit/brotli/dist/c/enc/command.c        delete
	external/mit/brotli/dist/c/enc/command.h        delete
	external/mit/brotli/dist/c/enc/compress_fragment.c delete
	external/mit/brotli/dist/c/enc/compress_fragment.h delete
	external/mit/brotli/dist/c/enc/compress_fragment_two_pass.c delete
	external/mit/brotli/dist/c/enc/compress_fragment_two_pass.h delete
	external/mit/brotli/dist/c/enc/dictionary_hash.c delete
	external/mit/brotli/dist/c/enc/dictionary_hash.h delete
	external/mit/brotli/dist/c/enc/encode.c         delete
	external/mit/brotli/dist/c/enc/encoder_dict.c   delete
	external/mit/brotli/dist/c/enc/encoder_dict.h   delete
	external/mit/brotli/dist/c/enc/entropy_encode.c delete
	external/mit/brotli/dist/c/enc/entropy_encode.h delete
	external/mit/brotli/dist/c/enc/entropy_encode_static.h delete
	external/mit/brotli/dist/c/enc/fast_log.c       delete
	external/mit/brotli/dist/c/enc/fast_log.h       delete
	external/mit/brotli/dist/c/enc/find_match_length.h delete
	external/mit/brotli/dist/c/enc/hash.h           delete
	external/mit/brotli/dist/c/enc/hash_composite_inc.h delete
	external/mit/brotli/dist/c/enc/hash_forgetful_chain_inc.h delete
	external/mit/brotli/dist/c/enc/hash_longest_match64_inc.h delete
	external/mit/brotli/dist/c/enc/hash_longest_match_inc.h delete
	external/mit/brotli/dist/c/enc/hash_longest_match_quickly_inc.h delete
	external/mit/brotli/dist/c/enc/hash_rolling_inc.h delete
	external/mit/brotli/dist/c/enc/hash_to_binary_tree_inc.h delete
	external/mit/brotli/dist/c/enc/histogram.c      delete
	external/mit/brotli/dist/c/enc/histogram.h      delete
	external/mit/brotli/dist/c/enc/histogram_inc.h  delete
	external/mit/brotli/dist/c/enc/literal_cost.c   delete
	external/mit/brotli/dist/c/enc/literal_cost.h   delete
	external/mit/brotli/dist/c/enc/memory.c         delete
	external/mit/brotli/dist/c/enc/memory.h         delete
	external/mit/brotli/dist/c/enc/metablock.c      delete
	external/mit/brotli/dist/c/enc/metablock.h      delete
	external/mit/brotli/dist/c/enc/metablock_inc.h  delete
	external/mit/brotli/dist/c/enc/params.h         delete
	external/mit/brotli/dist/c/enc/prefix.h         delete
	external/mit/brotli/dist/c/enc/quality.h        delete
	external/mit/brotli/dist/c/enc/utf8_util.c      delete
	external/mit/brotli/dist/c/enc/ringbuffer.h     delete
	external/mit/brotli/dist/c/enc/static_dict.c    delete
	external/mit/brotli/dist/c/enc/static_dict.h    delete
	external/mit/brotli/dist/c/enc/static_dict_lut.h delete
	external/mit/brotli/dist/c/enc/utf8_util.h      delete
	external/mit/brotli/dist/c/enc/write_bits.h     delete
	external/mit/brotli/dist/python/Makefile        delete
	external/mit/brotli/dist/python/README.md       delete
	external/mit/brotli/dist/python/_brotli.cc      delete
	external/mit/brotli/dist/python/bro.py          delete
	external/mit/brotli/dist/python/brotli.py       delete
	external/mit/brotli/dist/python/tests/__init__.py delete
	external/mit/brotli/dist/python/tests/_test_utils.py delete
	external/mit/brotli/dist/python/tests/bro_test.py delete
	external/mit/brotli/dist/python/tests/compress_test.py delete
	external/mit/brotli/dist/python/tests/compressor_test.py delete
	external/mit/brotli/dist/python/tests/decompress_test.py delete
	external/mit/brotli/dist/python/tests/decompressor_test.py delete
	external/mit/brotli/dist/scripts/sources.lst    delete
	external/mit/brotli/dist/tests/Makefile         delete
	external/mit/brotli/dist/tests/compatibility_test.sh delete
	external/mit/brotli/dist/tests/roundtrip_test.sh delete
	external/mit/brotli/dist/CMakeLists.txt         up to 1.1.1.2
	external/mit/brotli/dist/CONTRIBUTING.md        up to 1.1.1.2
	external/mit/brotli/dist/MANIFEST.in            up to 1.1.1.2
	external/mit/brotli/dist/README                 up to 1.1.1.2
	external/mit/brotli/dist/README.md              up to 1.1.1.2
	external/mit/brotli/dist/setup.cfg              up to 1.1.1.2
	external/mit/brotli/dist/setup.py               up to 1.1.1.2
	external/mit/brotli/dist/c/common/constants.c   up to 1.1.1.2
	external/mit/brotli/dist/c/common/constants.h   up to 1.1.1.2
	external/mit/brotli/dist/c/common/context.c     up to 1.1.1.2
	external/mit/brotli/dist/c/common/context.h     up to 1.1.1.2
	external/mit/brotli/dist/c/common/dictionary.c  up to 1.1.1.2
	external/mit/brotli/dist/c/common/dictionary.h  up to 1.1.1.2
	external/mit/brotli/dist/c/common/platform.c    up to 1.1.1.2
	external/mit/brotli/dist/c/common/platform.h    up to 1.2
	external/mit/brotli/dist/c/common/transform.c   up to 1.1.1.2
	external/mit/brotli/dist/c/common/transform.h   up to 1.1.1.2
	external/mit/brotli/dist/c/common/version.h     up to 1.1.1.2
	external/mit/brotli/dist/c/dec/bit_reader.c     up to 1.1.1.2
	external/mit/brotli/dist/c/dec/bit_reader.h     up to 1.1.1.2
	external/mit/brotli/dist/c/dec/decode.c         up to 1.1.1.2
	external/mit/brotli/dist/c/dec/huffman.c        up to 1.1.1.2
	external/mit/brotli/dist/c/dec/huffman.h        up to 1.1.1.2
	external/mit/brotli/dist/c/dec/prefix.h         up to 1.1.1.2
	external/mit/brotli/dist/c/dec/state.c          up to 1.1.1.2
	external/mit/brotli/dist/c/dec/state.h          up to 1.1.1.2
	external/mit/brotli/dist/c/include/brotli/decode.h up to 1.1.1.2
	external/mit/brotli/dist/c/include/brotli/encode.h up to 1.1.1.2
	external/mit/brotli/dist/c/include/brotli/port.h up to 1.1.1.2
	external/mit/brotli/dist/c/include/brotli/types.h up to 1.1.1.2
	external/mit/brotli/dist/c/tools/brotli.c       up to 1.1.1.2
	external/mit/brotli/dist/c/tools/brotli.md      up to 1.1.1.2
	external/mit/brotli/dist/docs/brotli.1          up to 1.1.1.2
	external/mit/brotli/dist/docs/constants.h.3     up to 1.1.1.2
	external/mit/brotli/dist/docs/decode.h.3        up to 1.1.1.2
	external/mit/brotli/dist/docs/encode.h.3        up to 1.1.1.2
	external/mit/brotli/dist/docs/types.h.3         up to 1.1.1.2
	external/mit/brotli/dist/scripts/libbrotlicommon.pc.in up to 1.1.1.2
	external/mit/brotli/dist/scripts/libbrotlidec.pc.in up to 1.1.1.2
	external/mit/brotli/dist/scripts/libbrotlienc.pc.in up to 1.1.1.2

Initial import of brotli 1.2.1, that fixes CVE 2026-111497.
@
text
@@

