RsdparsaSdpGlue.cpp (2593B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #include "sdp/RsdparsaSdpGlue.h" 7 8 #include <cstdint> 9 #include <string> 10 11 #include "sdp/RsdparsaSdpInc.h" 12 namespace mozilla { 13 14 std::string convertStringView(StringView str) { 15 if (nullptr == str.buf) { 16 return std::string(); 17 } else { 18 return std::string(str.buf, str.len); 19 } 20 } 21 22 std::vector<std::string> convertStringVec(StringVec* vec) { 23 std::vector<std::string> ret; 24 size_t len = string_vec_len(vec); 25 for (size_t i = 0; i < len; i++) { 26 StringView view; 27 string_vec_get_view(vec, i, &view); 28 ret.push_back(convertStringView(view)); 29 } 30 return ret; 31 } 32 33 sdp::AddrType convertAddressType(RustSdpAddressType addrType) { 34 switch (addrType) { 35 case RustSdpAddressType::kRustAddrIp4: 36 return sdp::kIPv4; 37 case RustSdpAddressType::kRustAddrIp6: 38 return sdp::kIPv6; 39 } 40 41 MOZ_CRASH("unknown address type"); 42 } 43 44 std::string convertAddress(RustAddress* address) { 45 return address->isFqdn ? convertStringView(address->fqdn) 46 : std::string(address->ipAddress); 47 } 48 49 std::pair<sdp::AddrType, std::string> convertExplicitlyTypedAddress( 50 RustExplicitlyTypedAddress* address) { 51 return std::make_pair(convertAddressType(address->addressType), 52 convertAddress(&address->address)); 53 } 54 55 std::vector<uint8_t> convertU8Vec(U8Vec* vec) { 56 std::vector<std::uint8_t> ret; 57 58 size_t len = u8_vec_len(vec); 59 for (size_t i = 0; i < len; i++) { 60 uint8_t byte; 61 u8_vec_get(vec, i, &byte); 62 ret.push_back(byte); 63 } 64 65 return ret; 66 } 67 68 std::vector<uint16_t> convertU16Vec(U16Vec* vec) { 69 std::vector<std::uint16_t> ret; 70 71 size_t len = u16_vec_len(vec); 72 for (size_t i = 0; i < len; i++) { 73 uint16_t word; 74 u16_vec_get(vec, i, &word); 75 ret.push_back(word); 76 } 77 78 return ret; 79 } 80 81 std::vector<uint32_t> convertU32Vec(U32Vec* vec) { 82 std::vector<std::uint32_t> ret; 83 84 size_t len = u32_vec_len(vec); 85 for (size_t i = 0; i < len; i++) { 86 uint32_t num; 87 u32_vec_get(vec, i, &num); 88 ret.push_back(num); 89 } 90 91 return ret; 92 } 93 94 std::vector<float> convertF32Vec(F32Vec* vec) { 95 std::vector<float> ret; 96 97 size_t len = f32_vec_len(vec); 98 for (size_t i = 0; i < len; i++) { 99 float flt; 100 f32_vec_get(vec, i, &flt); 101 ret.push_back(flt); 102 } 103 104 return ret; 105 } 106 107 } // namespace mozilla