<commit_before>/* Swfdec
 * Copyright (C) 2003-2006 <NAME> <<EMAIL>>
 *		 2005-2006 <NAME> <<EMAIL>>
 *		 2006-2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <zlib.h>

#include "swfdec_bits.h"
#include "swfdec_debug.h"
#include "swfdec_decoder.h"
#include "swfdec_rect.h"


#define SWFDEC_BITS_CHECK(b,n) G_STMT_START { \
  if (swfdec_bits_left(b) < (n)) { \
    SWFDEC_ERROR ("reading past end of buffer"); \
    b->ptr = b->end; \
    b->idx = 0; \
    return 0; \
  } \
}G_STMT_END
#define SWFDEC_BYTES_CHECK(b,n) G_STMT_START { \
  g_assert (b->end >= b->ptr); \
  g_assert (b->idx == 0); \
  if ((unsigned long) (b->end - b->ptr) < n) { \
    SWFDEC_ERROR ("reading past end of buffer"); \
    b->ptr = b->end; \
    b->idx = 0; \
    return 0; \
  } \
} G_STMT_END

/**
 * swfdec_bits_init:
 * @bits: a #SwfdecBits
 * @buffer: buffer to use for data or NULL
 *
 * initializes @bits for use with the data in @buffer. The buffer will not be
 * referenced, so you are responsible for keeping it around while @bits is used.
 **/
void 
swfdec_bits_init (SwfdecBits *bits, SwfdecBuffer *buffer)
{
  g_return_if_fail (bits != NULL);

  if (buffer) {
    bits->buffer = buffer;
    bits->ptr = buffer->data;
    bits->idx = 0;
    bits->end = buffer->data + buffer->length;
  } else {
    memset (bits, 0, sizeof (SwfdecBits));
  }
}

/**
 * swfdec_bits_init_bits:
 * @bits: a #SwfdecBits
 * @from: a #SwfdecBits to initialize from
 * @bytes: number of bytes to move to @bits
 *
 * Initializes @bits for use with the next @bytes bytes from @from. If not
 * enough bytes are available, less bytes will be available in @bits. @from
 * will skip the bytes now available in @bits. If you want to know if this
 * function moves enough bytes, you should ensure that enough data is 
 * available using swfdec_bits_left() before calling this function.
 **/
void
swfdec_bits_init_bits (SwfdecBits *bits, SwfdecBits *from, guint bytes)
{
  g_return_if_fail (bits != NULL);
  g_return_if_fail (from != NULL);
  g_return_if_fail (from->idx == 0);

  bits->buffer = from->buffer;
  bits->ptr = from->ptr;
  bits->end = MIN (bits->ptr + bytes, from->end);
  bits->idx = 0;
  from->ptr = bits->end;
}

/**
 * swfdec_bits_init_data:
 * @bits: the #SwfdecBits to initialize
 * @data: data to initialize with
 * @len: length of the data
 *
 * Initializes @bits for use with the given @data. All operations on @bits will
 * return copies of the data, so after use, you can free the supplied data. Using 
 * %NULL for @data is valid if @len is 0.
 **/<commit_msg>avoid overflow issues when bytes is near MAXUINT<commit_after>/* Swfdec
 * Copyright (C) 2003-2006 <NAME> <<EMAIL>>
 *		 2005-2006 <NAME> <<EMAIL>>
 *		 2006-2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <zlib.h>

#include "swfdec_bits.h"
#include "swfdec_debug.h"
#include "swfdec_decoder.h"
#include "swfdec_rect.h"


#define SWFDEC_BITS_CHECK(b,n) G_STMT_START { \
  if (swfdec_bits_left(b) < (n)) { \
    SWFDEC_ERROR ("reading past end of buffer"); \
    b->ptr = b->end; \
    b->idx = 0; \
    return 0; \
  } \
}G_STMT_END
#define SWFDEC_BYTES_CHECK(b,n) G_STMT_START { \
  g_assert (b->end >= b->ptr); \
  g_assert (b->idx == 0); \
  if ((unsigned long) (b->end - b->ptr) < n) { \
    SWFDEC_ERROR ("reading past end of buffer"); \
    b->ptr = b->end; \
    b->idx = 0; \
    return 0; \
  } \
} G_STMT_END

/**
 * swfdec_bits_init:
 * @bits: a #SwfdecBits
 * @buffer: buffer to use for data or NULL
 *
 * initializes @bits for use with the data in @buffer. The buffer will not be
 * referenced, so you are responsible for keeping it around while @bits is used.
 **/
void 
swfdec_bits_init (SwfdecBits *bits, SwfdecBuffer *buffer)
{
  g_return_if_fail (bits != NULL);

  if (buffer) {
    bits->buffer = buffer;
    bits->ptr = buffer->data;
    bits->idx = 0;
    bits->end = buffer->data + buffer->length;
  } else {
    memset (bits, 0, sizeof (SwfdecBits));
  }
}

/**
 * swfdec_bits_init_bits:
 * @bits: a #SwfdecBits
 * @from: a #SwfdecBits to initialize from
 * @bytes: number of bytes to move to @bits
 *
 * Initializes @bits for use with the next @bytes bytes from @from. If not
 * enough bytes are available, less bytes will be available in @bits. @from
 * will skip the bytes now available in @bits. If you want to know if this
 * function moves enough bytes, you should ensure that enough data is 
 * available using swfdec_bits_left() before calling this function.
 **/
void
swfdec_bits_init_bits (SwfdecBits *bits, SwfdecBits *from, guint bytes)
{
  g_return_if_fail (bits != NULL);
  g_return_if_fail (from != NULL);
  g_return_if_fail (from->idx == 0);

  bits->buffer = from->buffer;
  bits->ptr = from->ptr;
  if (bytes > (guint) (from->end - from->ptr))
    bytes = from->end - from->ptr;
  bits->end = bits->ptr + bytes;
  bits->idx = 0;
  from->ptr = bits->end;
}

/**
 * swfdec_bits_init_data:
 * @bits: the #SwfdecBits to initialize
 * @data: data to initialize with
 * @len: length of the data
 *
 * Initializes @bits for use with the given @data. All operations on @bits will
 * return copies of the data, so after use, you can free the supplied data. Using 
 * %NULL for @data is valid if @len is 0.
 **/
{'fraction_non_alphanumeric': 0.12032950867902324, 'fraction_numerical': 0.013827596351868197, 'mean_word_length': 3.406351263771873, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1967496', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2273, 'n_words': 847}