File: security_key_message_writer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (81 lines) | stat: -rw-r--r-- 2,633 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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/security_key/security_key_message_writer.h"

#include <cstdint>
#include <string>
#include <utility>

#include "remoting/host/security_key/security_key_message.h"

namespace remoting {

SecurityKeyMessageWriter::SecurityKeyMessageWriter(base::File output_file)
    : output_stream_(std::move(output_file)) {}

SecurityKeyMessageWriter::~SecurityKeyMessageWriter() {}

bool SecurityKeyMessageWriter::WriteMessage(
    SecurityKeyMessageType message_type) {
  return WriteMessageWithPayload(message_type, std::string());
}

bool SecurityKeyMessageWriter::WriteMessageWithPayload(
    SecurityKeyMessageType message_type,
    const std::string& message_payload) {
  if (write_failed_ || !output_stream_.IsValid()) {
    return false;
  }

  int message_payload_size_bytes = message_payload.size();
  uint32_t total_message_size_bytes =
      SecurityKeyMessage::kMessageTypeSizeBytes + message_payload_size_bytes;
  CHECK(SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes));

  // First we send the message header which is the length of the message_type
  // and message_payload in bytes.
  if (!WriteBytesToOutput(reinterpret_cast<char*>(&total_message_size_bytes),
                          SecurityKeyMessage::kHeaderSizeBytes)) {
    LOG(ERROR) << "Failed to send message header.";
    return false;
  }

  // Next we send the message_type.
  if (!WriteBytesToOutput(reinterpret_cast<char*>(&message_type),
                          SecurityKeyMessage::kMessageTypeSizeBytes)) {
    LOG(ERROR) << "Failed to send message type.";
    return false;
  }

  // Lastly, send the message data if appropriate.
  if (!message_payload.empty()) {
    if (!WriteBytesToOutput(message_payload.data(),
                            message_payload_size_bytes)) {
      LOG(ERROR) << "Failed to send message payload.";
      return false;
    }
  }

  return true;
}

bool SecurityKeyMessageWriter::WriteBytesToOutput(const char* message,
                                                  int bytes_to_write) {
  DCHECK(message);
  DCHECK_GT(bytes_to_write, 0);

  int result = output_stream_.WriteAtCurrentPos(message, bytes_to_write);
  if (result != bytes_to_write) {
    LOG(ERROR) << "Failed to write all bytes to output stream.  bytes written: "
               << result << ", file error: "
               << base::File::ErrorToString(output_stream_.error_details());
    write_failed_ = true;
    return false;
  }

  return true;
}

}  // namespace remoting