summaryrefslogtreecommitdiffstats
path: root/chrome/browser/privacy_blacklist/blacklist_store.cc
blob: ba3784d037052988bcaf09d9b56ea8b12feec3de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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();
}