summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sessions/compress_data_helper.cc
blob: 987af6a8839b23581926fba9b147b01962331537 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) 2012 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/sessions/compress_data_helper.h"

#include "base/memory/scoped_ptr.h"
#include "base/pickle.h"

#if defined(USE_SYSTEM_LIBBZ2)
#include <bzlib.h>
#else
#include "third_party/bzip2/bzlib.h"
#endif

#include <string>

// static
void CompressDataHelper::CompressAndWriteStringToPickle(const std::string& str,
                                                        int max_bytes,
                                                        Pickle* pickle,
                                                        int* bytes_written) {
  // If there is no data to write, only write the size (0).
  if (str.empty()) {
    pickle->WriteInt(0);
    return;
  }

  bool written = false;
  int max_output_size = max_bytes - *bytes_written;
  if (max_output_size > 0) {
    // We need a non-const char* to pass to the compression algorithm.
    int input_size = str.size();
    scoped_array<char> input(new char[input_size]);
    memcpy(input.get(), str.data(), input_size);

    scoped_array<char> output(new char[max_output_size]);
    memset(output.get(), 0, max_output_size);

    bz_stream stream;
    stream.bzalloc = NULL;
    stream.bzfree = NULL;
    stream.opaque = NULL;
    int result = BZ2_bzCompressInit(&stream,
                                    1,  // 100K block size ( > size of our data)
                                    0,  // quiet
                                    0);  // default work factor
    if (result == BZ_OK) {
      stream.next_in = input.get();
      stream.avail_in = input_size;
      stream.next_out = output.get();
      stream.avail_out = max_output_size;
      do {
        result = BZ2_bzCompress(&stream, BZ_FINISH);
      } while (result == BZ_FINISH_OK);

      // If there wasn't enough space for the output, the result won't be
      // BZ_STREAM_END.
      if (result == BZ_STREAM_END) {
        result = BZ2_bzCompressEnd(&stream);
        DCHECK(stream.total_out_lo32 <=
               static_cast<unsigned>(max_output_size));
        if (result == BZ_OK) {
          // Write the size of the original input, so that the decompression
          // part knows how much memory to allocate.
          pickle->WriteInt(input_size);
          pickle->WriteData(output.get(), stream.total_out_lo32);

          *bytes_written += stream.total_out_lo32;
          written = true;
        }
      } else {
        BZ2_bzCompressEnd(&stream);
      }
    }
  }
  if (!written) {
    // We cannot write any data. Write only the data size (0). The reading part
    // will not try to read any data after seeing the size 0.
    pickle->WriteInt(0);
  }
}

// static
bool CompressDataHelper::ReadAndDecompressStringFromPickle(const Pickle& pickle,
                                                           void** iter,
                                                           std::string* str) {
  // Read the size of the original data.
  int original_size = 0;
  if (!pickle.ReadLength(iter, &original_size))
    return false;

  if (original_size == 0) {
    // No data to decompress.
    return true;
  }

  // Read the compressed data.
  const char* compressed_data = NULL;
  int compressed_size = 0;
  if (!pickle.ReadData(iter, &compressed_data, &compressed_size))
    return false;

  scoped_array<char> original_data(new char[original_size]);
  memset(original_data.get(), 0, original_size);

  // We need a non-const char* to pass to the decompression algorithm.
  scoped_array<char> compressed_data_copy(new char[compressed_size]);
  memcpy(compressed_data_copy.get(), compressed_data, compressed_size);

  bz_stream stream;
  stream.bzalloc = NULL;
  stream.bzfree = NULL;
  stream.opaque = NULL;

  int result = BZ2_bzDecompressInit(&stream, 0, 0);
  if (result == BZ_OK) {
    stream.next_in = compressed_data_copy.get();
    stream.avail_in = compressed_size;
    stream.next_out = original_data.get();
    stream.avail_out = original_size;

    do {
      // We should know exactly how much space to allocate for the result, thus
      // no out-of-space errors should happen.
      DCHECK(stream.avail_out > 0);
      result = BZ2_bzDecompress(&stream);
    } while (result == BZ_OK);

    DCHECK(result == BZ_STREAM_END);
    if (result == BZ_STREAM_END) {
      result = BZ2_bzDecompressEnd(&stream);
      if (result == BZ_OK) {
        str->assign(original_data.get(), original_size);
      }
    } else {
      BZ2_bzDecompressEnd(&stream);
    }
  }
  return !str->empty();
}