File: custom_data_helper.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 3,455 bytes parent folder | download | duplicates (7)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// TODO(dcheng): For efficiency reasons, consider passing custom data around
// as a vector instead. It allows us to append a
// std::pair<std::u16string, std::u16string> and swap the deserialized values.

#include "ui/base/clipboard/custom_data_helper.h"

#include <tuple>
#include <utility>

#include "base/containers/span.h"
#include "base/pickle.h"

namespace ui {

namespace {

bool SkipString16(base::PickleIterator* iter) {
  DCHECK(iter);

  size_t len;
  if (!iter->ReadLength(&len))
    return false;
  return iter->SkipBytes(len * sizeof(char16_t));
}

}  // namespace

void ReadCustomDataTypes(base::span<const uint8_t> data,
                         std::vector<std::u16string>* types) {
  base::Pickle pickle = base::Pickle::WithData(data);
  base::PickleIterator iter(pickle);

  uint32_t size = 0;
  if (!iter.ReadUInt32(&size))
    return;

  // Keep track of the original elements in the types vector. On failure, we
  // truncate the vector to the original size since we want to ignore corrupt
  // custom data pickles.
  size_t original_size = types->size();

  for (uint32_t i = 0; i < size; ++i) {
    types->push_back(std::u16string());
    if (!iter.ReadString16(&types->back()) || !SkipString16(&iter)) {
      types->resize(original_size);
      return;
    }
  }
}

std::optional<std::u16string> ReadCustomDataForType(
    base::span<const uint8_t> data,
    std::u16string_view type) {
  base::Pickle pickle = base::Pickle::WithData(data);
  base::PickleIterator iter(pickle);

  uint32_t size = 0;
  if (!iter.ReadUInt32(&size))
    return std::nullopt;

  for (uint32_t i = 0; i < size; ++i) {
    std::u16string deserialized_type;
    if (!iter.ReadString16(&deserialized_type)) {
      return std::nullopt;
    }
    if (deserialized_type == type) {
      std::u16string result;
      if (iter.ReadString16(&result)) {
        return std::move(result);
      }
      return std::nullopt;
    }
    if (!SkipString16(&iter)) {
      return std::nullopt;
    }
  }
  return std::nullopt;
}

std::optional<std::unordered_map<std::u16string, std::u16string>>
ReadCustomDataIntoMap(base::span<const uint8_t> data) {
  base::Pickle pickle = base::Pickle::WithData(data);
  base::PickleIterator iter(pickle);

  uint32_t size = 0;
  if (!iter.ReadUInt32(&size)) {
    return std::nullopt;
  }

  std::unordered_map<std::u16string, std::u16string> result;
  for (uint32_t i = 0; i < size; ++i) {
    std::u16string custom_type;
    if (!iter.ReadString16(&custom_type)) {
      return std::nullopt;
    }
    std::u16string custom_data;
    if (!iter.ReadString16(&custom_data)) {
      return std::nullopt;
    }
    result.insert({std::move(custom_type), std::move(custom_data)});
  }
  return std::move(result);
}

void WriteCustomDataToPickle(
    const std::unordered_map<std::u16string, std::u16string>& data,
    base::Pickle* pickle) {
  pickle->WriteUInt32(data.size());
  for (const auto& it : data) {
    pickle->WriteString16(it.first);
    pickle->WriteString16(it.second);
  }
}

void WriteCustomDataToPickle(
    const base::flat_map<std::u16string, std::u16string>& data,
    base::Pickle* pickle) {
  pickle->WriteUInt32(data.size());
  for (const auto& it : data) {
    pickle->WriteString16(it.first);
    pickle->WriteString16(it.second);
  }
}

}  // namespace ui