tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MLSTransactionMessage.cpp (2450B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "MLSTransactionMessage.h"
      8 
      9 #include "ipc/IPCMessageUtils.h"
     10 #include "ipc/IPCMessageUtilsSpecializations.h"
     11 #include "mozilla/Assertions.h"
     12 #include "mozilla/dom/MLSTransactionMessage.h"
     13 #include "mozilla/security/mls/mls_gk_ffi_generated.h"
     14 #include "nsTArray.h"
     15 
     16 using namespace mozilla::security::mls;
     17 
     18 void IPC::ParamTraits<mozilla::security::mls::GkReceived>::Write(
     19    MessageWriter* aWriter, const paramType& aValue) {
     20  // Serialize the enum variant tag
     21  IPC::WriteParam(aWriter, aValue.tag);
     22 
     23  switch (aValue.tag) {
     24    case paramType::Tag::ApplicationMessage:
     25      IPC::WriteParam(aWriter, aValue.application_message._0);
     26      break;
     27    case paramType::Tag::GroupIdEpoch:
     28      IPC::WriteParam(aWriter, aValue.group_id_epoch._0);
     29      break;
     30    case paramType::Tag::CommitOutput:
     31      IPC::WriteParam(aWriter, aValue.commit_output._0);
     32      break;
     33    default:
     34      break;
     35  }
     36 }
     37 
     38 bool IPC::ParamTraits<mozilla::security::mls::GkReceived>::Read(
     39    MessageReader* aReader, paramType* aResult) {
     40  MOZ_ASSERT(aResult->tag == paramType::Tag::None,
     41             "Clobbering already-initialized result");
     42 
     43  // Deserialize the tag
     44  if (!IPC::ReadParam(aReader, &aResult->tag)) {
     45    // Ensure that the tag has a safe value for the destructor before returning.
     46    aResult->tag = paramType::Tag::None;
     47    return false;
     48  }
     49 
     50  // Use placement-new to initialize the relevant variant of the internal union,
     51  // then deserialize the bodies.
     52  switch (aResult->tag) {
     53    case paramType::Tag::None:
     54      return true;  // No data payload
     55    case paramType::Tag::ApplicationMessage:
     56      new (&aResult->application_message) paramType::ApplicationMessage_Body;
     57      return IPC::ReadParam(aReader, &aResult->application_message._0);
     58    case paramType::Tag::GroupIdEpoch:
     59      new (&aResult->group_id_epoch) paramType::GroupIdEpoch_Body;
     60      return IPC::ReadParam(aReader, &aResult->group_id_epoch._0);
     61    case paramType::Tag::CommitOutput:
     62      new (&aResult->commit_output) paramType::CommitOutput_Body;
     63      return IPC::ReadParam(aReader, &aResult->commit_output._0);
     64  }
     65  return false;
     66 }