<commit_before>// Copyright (c) 2009 The Chromium 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 "chrome/browser/privacy_blacklist/blacklist_store.h"

#include <cstdio>

#include "base/basictypes.h"
#include "base/file_util.h"

namespace {

const char cookie[] = "GCPBL100";

}

void BlacklistStoreOutput::WriteUInt(uint32 i) {
  fwrite(reinterpret_cast<char*>(&i), 1, sizeof(uint32), file_);
}

void BlacklistStoreOutput::WriteString(const std::string& s) {
  uint32 n = s.size();
  fwrite(reinterpret_cast<char*>(&n), 1, sizeof(uint32), file_);
  fwrite(s.c_str(), 1, s.size(), file_);
}

BlacklistStoreOutput::BlacklistStoreOutput(FILE* file) : file_(file) {
  fwrite(cookie, 1, sizeof(cookie), file_);
}

BlacklistStoreOutput::~BlacklistStoreOutput() {
  file_util::CloseFile(file_);
}

void BlacklistStoreOutput::ReserveProviders(uint32 num) {
  WriteUInt(num);
}

void BlacklistStoreOutput::StoreProvider(const std::string& name,
                                         const std::string& url) {
  WriteString(name);
  WriteString(url);
}

void BlacklistStoreOutput::ReserveEntries(uint32 num) {
  WriteUInt(num);
}

void BlacklistStoreOutput::StoreEntry(const std::string& pattern,
                                      uint32 attributes,
                                      const std::vector<std::string>& types,
                                      uint32 provider) {
  WriteString(pattern);
  WriteUInt(attributes);
  WriteUInt(types.size());
  for (uint32 i = 0; i < types.size(); ++i)
    WriteString(types[i]);
  WriteUInt(provider);
}

uint32 BlacklistStoreInput::ReadUInt() {
  char buf[sizeof(uint32)];
  if (fread(buf, 1, sizeof(uint32), file_) != sizeof(uint32))
    return 0;
  return *reinterpret_cast<uint32*>(buf);
}

std::string BlacklistStoreInput::ReadString() {
  uint32 size = ReadUInt();

  // Too long strings are not allowed.
  if (size > 8192) {
    return std::string();
  }

  char buf[8192];
  if (fread(buf, 1, size, file_) != size)
    return std::string();
  return std::string(buf, size);
}

BlacklistStoreInput::BlacklistStoreInput(FILE* file) : file_(file) {
  fseek(file_, sizeof(cookie), SEEK_CUR);
}

BlacklistStoreInput::~BlacklistStoreInput() {
  file_util::CloseFile(file_);
}

uint32 BlacklistStoreInput::ReadNumProviders() {
  return ReadUInt();
}

void BlacklistStoreInput::ReadProvider(std::string* name, std::string* url) {
  *name = ReadString();
  *url = ReadString();
}

uint32 BlacklistStoreInput::ReadNumEntries() {
  return ReadUInt();
}

void BlacklistStoreInput::ReadEntry(std::string* pattern,
                                    uint32* attributes,
                                    std::vector<std::string>* types,
                                    uint32* provider) {
  *pattern = ReadString();
  *attributes = ReadUInt();
  if (uint32 n = ReadUInt()) {
    while (n--)
      types->push_back(ReadString());
  }
  *provider = ReadUInt();
}
<commit_msg>Fix a strict-aliasing issue in BlacklistStoreInput::ReadUInt.<commit_after>// Copyright (c) 2009 The Chromium 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 "chrome/browser/privacy_blacklist/blacklist_store.h"

#include <cstdio>

#include "base/basictypes.h"
#include "base/file_util.h"

namespace {

const char cookie[] = "GCPBL100";

}

void BlacklistStoreOutput::WriteUInt(uint32 i) {
  fwrite(reinterpret_cast<char*>(&i), 1, sizeof(uint32), file_);
}

void BlacklistStoreOutput::WriteString(const std::string& s) {
  uint32 n = s.size();
  fwrite(reinterpret_cast<char*>(&n), 1, sizeof(uint32), file_);
  fwrite(s.c_str(), 1, s.size(), file_);
}

BlacklistStoreOutput::BlacklistStoreOutput(FILE* file) : file_(file) {
  fwrite(cookie, 1, sizeof(cookie), file_);
}

BlacklistStoreOutput::~BlacklistStoreOutput() {
  file_util::CloseFile(file_);
}

void BlacklistStoreOutput::ReserveProviders(uint32 num) {
  WriteUInt(num);
}

void BlacklistStoreOutput::StoreProvider(const std::string& name,
                                         const std::string& url) {
  WriteString(name);
  WriteString(url);
}

void BlacklistStoreOutput::ReserveEntries(uint32 num) {
  WriteUInt(num);
}

void BlacklistStoreOutput::StoreEntry(const std::string& pattern,
                                      uint32 attributes,
                                      const std::vector<std::string>& types,
                                      uint32 provider) {
  WriteString(pattern);
  WriteUInt(attributes);
  WriteUInt(types.size());
  for (uint32 i = 0; i < types.size(); ++i)
    WriteString(types[i]);
  WriteUInt(provider);
}

uint32 BlacklistStoreInput::ReadUInt() {
  uint32 buf;
  if (fread(&buf, 1, sizeof(uint32), file_) != sizeof(uint32))
    return 0;
  return buf;
}

std::string BlacklistStoreInput::ReadString() {
  uint32 size = ReadUInt();

  // Too long strings are not allowed.
  if (size > 8192) {
    return std::string();
  }

  char buf[8192];
  if (fread(buf, 1, size, file_) != size)
    return std::string();
  return std::string(buf, size);
}

BlacklistStoreInput::BlacklistStoreInput(FILE* file) : file_(file) {
  fseek(file_, sizeof(cookie), SEEK_CUR);
}

BlacklistStoreInput::~BlacklistStoreInput() {
  file_util::CloseFile(file_);
}

uint32 BlacklistStoreInput::ReadNumProviders() {
  return ReadUInt();
}

void BlacklistStoreInput::ReadProvider(std::string* name, std::string* url) {
  *name = ReadString();
  *url = ReadString();
}

uint32 BlacklistStoreInput::ReadNumEntries() {
  return ReadUInt();
}

void BlacklistStoreInput::ReadEntry(std::string* pattern,
                                    uint32* attributes,
                                    std::vector<std::string>* types,
                                    uint32* provider) {
  *pattern = ReadString();
  *attributes = ReadUInt();
  if (uint32 n = ReadUInt()) {
    while (n--)
      types->push_back(ReadString());
  }
  *provider = ReadUInt();
}

{'fraction_non_alphanumeric': 0.1394079708946585, 'fraction_numerical': 0.02050603605093435, 'mean_word_length': 3.4601769911504423, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3743665', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1875, 'n_words': 411}