Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

Commit 01d017f

Browse files
Privacy Sandbox Teamcopybara-github
authored andcommitted
feat: Move string_padder utilities to common repo
Bug: b/398920134 Change-Id: Ie45e9925ad1b357f18cf2de5ac2af62fa418e43a GitOrigin-RevId: f456cb3e65948e78b73b1868b07ba0f28f315f49
1 parent fc02ba4 commit 01d017f

4 files changed

Lines changed: 180 additions & 0 deletions

File tree

src/communication/BUILD.bazel

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,35 @@ cc_test(
9292
],
9393
)
9494

95+
cc_library(
96+
name = "string_padder",
97+
srcs = [
98+
"string_padder.cc",
99+
],
100+
hdrs = [
101+
"string_padder.h",
102+
],
103+
deps = [
104+
"@com_github_google_quiche//quiche:quiche_unstable_api",
105+
"@com_google_absl//absl/log",
106+
"@com_google_absl//absl/status",
107+
"@com_google_absl//absl/status:statusor",
108+
"@com_google_absl//absl/strings",
109+
],
110+
)
111+
112+
cc_test(
113+
name = "string_padder_test",
114+
size = "small",
115+
srcs = [
116+
"string_padder_test.cc",
117+
],
118+
deps = [
119+
":string_padder",
120+
"@com_google_googletest//:gtest_main",
121+
],
122+
)
123+
95124
cc_library(
96125
name = "compression",
97126
srcs = [

src/communication/string_padder.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include "src/communication/string_padder.h"
15+
16+
#include <string>
17+
18+
#include "absl/log/log.h"
19+
#include "quiche/common/quiche_data_reader.h"
20+
#include "quiche/common/quiche_data_writer.h"
21+
22+
namespace privacy_sandbox::server_common {
23+
24+
std::string Pad(std::string_view string_to_pad, int32_t extra_padding) {
25+
int output_size = sizeof(u_int32_t) + string_to_pad.size() + extra_padding;
26+
std::string output(output_size, '0');
27+
28+
quiche::QuicheDataWriter data_writer(output.size(), output.data());
29+
data_writer.WriteUInt32(string_to_pad.size());
30+
data_writer.WriteStringPiece(string_to_pad);
31+
return output;
32+
}
33+
34+
absl::StatusOr<std::string> Unpad(std::string_view padded_string) {
35+
auto data_reader = quiche::QuicheDataReader(padded_string);
36+
uint32_t string_size = 0;
37+
if (!data_reader.ReadUInt32(&string_size)) {
38+
return absl::InvalidArgumentError("Failed to read string size");
39+
}
40+
VLOG(9) << "string size: " << string_size;
41+
std::string_view output;
42+
if (!data_reader.ReadStringPiece(&output, string_size)) {
43+
return absl::InvalidArgumentError("Failed to read a string");
44+
}
45+
VLOG(9) << "string: " << output;
46+
return std::string(output);
47+
}
48+
49+
} // namespace privacy_sandbox::server_common

src/communication/string_padder.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef COMMUNICATION_STRING_PADDER_H_
18+
#define COMMUNICATION_STRING_PADDER_H_
19+
20+
#include <string>
21+
22+
#include "absl/status/status.h"
23+
#include "absl/status/statusor.h"
24+
25+
namespace privacy_sandbox::server_common {
26+
// Returns the string of the following format:
27+
// [32 bit unsigned int][string_to_pad][padding]
28+
// length data filler
29+
// filler.size() == extra_padding
30+
std::string Pad(std::string_view string_to_pad, int32_t extra_padding);
31+
// Takes the string padded with the method above OR in the same format
32+
// and returns the string.
33+
absl::StatusOr<std::string> Unpad(std::string_view padded_string);
34+
} // namespace privacy_sandbox::server_common
35+
36+
#endif // COMMUNICATION_STRING_PADDER_H_
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "src/communication/string_padder.h"
16+
17+
#include <string_view>
18+
19+
#include "gtest/gtest.h"
20+
21+
namespace privacy_sandbox::server_common {
22+
namespace {
23+
24+
TEST(PadUnpad, Success) {
25+
const std::string_view kTestString = "string to pad";
26+
const int32_t padding_size = 100;
27+
auto padded_string = Pad(kTestString, padding_size);
28+
int32_t expected_length =
29+
sizeof(u_int32_t) + kTestString.size() + padding_size;
30+
EXPECT_EQ(expected_length, padded_string.size());
31+
auto original_string_status = Unpad(padded_string);
32+
ASSERT_TRUE(original_string_status.ok());
33+
EXPECT_EQ(*original_string_status, kTestString);
34+
}
35+
36+
TEST(PadUnpadZeroPadding, Success) {
37+
const std::string_view kTestString = "string to pad";
38+
const int32_t padding_size = 0;
39+
auto padded_string = Pad(kTestString, padding_size);
40+
int32_t expected_length =
41+
sizeof(u_int32_t) + kTestString.size() + padding_size;
42+
EXPECT_EQ(expected_length, padded_string.size());
43+
auto original_string_status = Unpad(padded_string);
44+
ASSERT_TRUE(original_string_status.ok());
45+
EXPECT_EQ(*original_string_status, kTestString);
46+
}
47+
48+
TEST(PadUnpadEmtpyString, Success) {
49+
const std::string_view kTestString = "";
50+
const int32_t padding_size = 100;
51+
auto padded_string = Pad(kTestString, padding_size);
52+
int32_t expected_length =
53+
sizeof(u_int32_t) + kTestString.size() + padding_size;
54+
EXPECT_EQ(expected_length, padded_string.size());
55+
auto original_string_status = Unpad(padded_string);
56+
ASSERT_TRUE(original_string_status.ok());
57+
EXPECT_EQ(*original_string_status, kTestString);
58+
}
59+
60+
TEST(UnpadFailure, Success) {
61+
auto original_string_status = Unpad("garbage");
62+
ASSERT_FALSE(original_string_status.ok());
63+
}
64+
65+
} // namespace
66+
} // namespace privacy_sandbox::server_common

0 commit comments

Comments
 (0)