<commit_before>// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include <limits>

#include "codec_int.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "../../../testing/fx_string_testhelpers.h"

static const OPJ_OFF_T kSkipError = static_cast<OPJ_OFF_T>(-1);
static const OPJ_SIZE_T kReadError = static_cast<OPJ_SIZE_T>(-1);
static const OPJ_SIZE_T kWriteError = static_cast<OPJ_SIZE_T>(-1);

static unsigned char stream_data[] = {
    0x00, 0x01, 0x02, 0x03,
    0x84, 0x85, 0x86, 0x87,  // Include some hi-bytes, too.
};

TEST(fxcodec, DecodeDataNullDecodeData) {
  unsigned char buffer[16];
  DecodeData* ptr = nullptr;

  // Error codes, not segvs, should callers pass us a NULL pointer.
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), ptr));
  EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), ptr));
  EXPECT_EQ(kSkipError, opj_skip_from_memory(1, ptr));
  EXPECT_FALSE(opj_seek_from_memory(1, ptr));
}

TEST(fxcodec, DecodeDataNullStream) {
  DecodeData dd(nullptr, 0);
  unsigned char buffer[16];

  // Reads of size 0 do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // Reads of nonzero size do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // writes of size 0 do nothing but return an error code.
  EXPECT_EQ(kReadError, opj_write_from_memory(buffer, 0, &dd));

  // writes of nonzero size do nothing but return an error code.
  EXPECT_EQ(kReadError, opj_write_from_memory(buffer, sizeof(buffer), &dd));

  // Skips of size 0 always return an error code.
  EXPECT_EQ(kReadError, opj_skip_from_memory(0, &dd));

  // Skips of nonzero size always return an error code.
  EXPECT_EQ(kReadError, opj_skip_from_memory(1, &dd));

  // Seeks to 0 offset return in error.
  EXPECT_FALSE(opj_seek_from_memory(0, &dd));

  // Seeks to non-zero offsets return in error.
  EXPECT_FALSE(opj_seek_from_memory(1, &dd));
}

TEST(fxcodec, DecodeDataZeroSize) {
  DecodeData dd(stream_data, 0);
  unsigned char buffer[16];

  // Reads of size 0 do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // Reads of nonzero size do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // writes of size 0 do nothing but return an error code.
  EXPECT_EQ(kReadError, opj_write_from_memory(buffer, 0, &dd));

  // writes of nonzero size do nothing but return an error code.
  EXPECT_EQ(kReadError, opj_write_from_memory(buffer, sizeof(buffer), &dd));

  // Skips of size 0 always return an error code.
  EXPECT_EQ(kReadError, opj_skip_from_memory(0, &dd));

  // Skips of nonzero size always return an error code.
  EXPECT_EQ(kReadError, opj_skip_from_memory(1, &dd));

  // Seeks to 0 offset return in error.
  EXPECT_FALSE(opj_seek_from_memory(0, &dd));

  // Seeks to non-zero offsets return in error.
  EXPECT_FALSE(opj_seek_from_memory(1, &dd));
}

TEST(fxcodec, DecodeDataReadInBounds) {
  unsigned char buffer[16];
  {
    DecodeData dd(stream_data, sizeof(stream_data));

    // Exact sized read in a single call.
    memset(buffer, 0xbd, sizeof(buffer));
    EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer), &dd));
    EXPECT_EQ(0x00, buffer[0]);
    EXPECT_EQ(0x01, buffer[1]);
    EXPECT_EQ(0x02, buffer[2]);
    EXPECT_EQ(0x03, buffer[3]);<commit_msg>XFA: Make fx_codec_jpx_unittest.cpp match master<commit_after>// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include <limits>

#include "codec_int.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "../../../testing/fx_string_testhelpers.h"

static const OPJ_OFF_T kSkipError = static_cast<OPJ_OFF_T>(-1);
static const OPJ_SIZE_T kReadError = static_cast<OPJ_SIZE_T>(-1);
static const OPJ_SIZE_T kWriteError = static_cast<OPJ_SIZE_T>(-1);

static unsigned char stream_data[] = {
    0x00, 0x01, 0x02, 0x03,
    0x84, 0x85, 0x86, 0x87,  // Include some hi-bytes, too.
};

TEST(fxcodec, DecodeDataNullDecodeData) {
  unsigned char buffer[16];
  DecodeData* ptr = nullptr;

  // Error codes, not segvs, should callers pass us a NULL pointer.
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), ptr));
  EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), ptr));
  EXPECT_EQ(kSkipError, opj_skip_from_memory(1, ptr));
  EXPECT_FALSE(opj_seek_from_memory(1, ptr));
}

TEST(fxcodec, DecodeDataNullStream) {
  DecodeData dd(nullptr, 0);
  unsigned char buffer[16];

  // Reads of size 0 do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // Reads of nonzero size do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // writes of size 0 do nothing but return an error code.
  EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));

  // writes of nonzero size do nothing but return an error code.
  EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));

  // Skips of size 0 always return an error code.
  EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));

  // Skips of nonzero size always return an error code.
  EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd));

  // Seeks to 0 offset return in error.
  EXPECT_FALSE(opj_seek_from_memory(0, &dd));

  // Seeks to non-zero offsets return in error.
  EXPECT_FALSE(opj_seek_from_memory(1, &dd));
}

TEST(fxcodec, DecodeDataZeroSize) {
  DecodeData dd(stream_data, 0);
  unsigned char buffer[16];

  // Reads of size 0 do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, 0, &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // Reads of nonzero size do nothing but return an error code.
  memset(buffer, 0xbd, sizeof(buffer));
  EXPECT_EQ(kReadError, opj_read_from_memory(buffer, sizeof(buffer), &dd));
  EXPECT_EQ(0xbd, buffer[0]);

  // writes of size 0 do nothing but return an error code.
  EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, 0, &dd));

  // writes of nonzero size do nothing but return an error code.
  EXPECT_EQ(kWriteError, opj_write_from_memory(buffer, sizeof(buffer), &dd));

  // Skips of size 0 always return an error code.
  EXPECT_EQ(kSkipError, opj_skip_from_memory(0, &dd));

  // Skips of nonzero size always return an error code.
  EXPECT_EQ(kSkipError, opj_skip_from_memory(1, &dd));

  // Seeks to 0 offset return in error.
  EXPECT_FALSE(opj_seek_from_memory(0, &dd));

  // Seeks to non-zero offsets return in error.
  EXPECT_FALSE(opj_seek_from_memory(1, &dd));
}

TEST(fxcodec, DecodeDataReadInBounds) {
  unsigned char buffer[16];
  {
    DecodeData dd(stream_data, sizeof(stream_data));

    // Exact sized read in a single call.
    memset(buffer, 0xbd, sizeof(buffer));
    EXPECT_EQ(8u, opj_read_from_memory(buffer, sizeof(buffer), &dd));
    EXPECT_EQ(0x00, buffer[0]);
    EXPECT_EQ(0x01, buffer[1]);
    EXPECT_EQ(0x02, buffer[2]);
    EXPECT_EQ(0x03, buffer[3]);
{'fraction_non_alphanumeric': 0.11913683938493345, 'fraction_numerical': 0.02403411293448766, 'mean_word_length': 4.75037147102526, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5893228', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2801, 'n_words': 743}