Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Mime.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include <string.h> 00018 00019 #include "nfc/ndef/common/Mime.h" 00020 00021 namespace mbed { 00022 namespace nfc { 00023 namespace ndef { 00024 namespace common { 00025 00026 Mime::Mime() : 00027 _mime(NULL), 00028 _type_size(0), 00029 _content_size(0) 00030 { } 00031 00032 Mime::Mime( 00033 const Span<const uint8_t> &mime_type, 00034 const Span<const uint8_t> &content 00035 ) : _mime(new uint8_t[mime_type.size() + content.size()]), 00036 _type_size(mime_type.size()), 00037 _content_size(content.size()) 00038 { 00039 memcpy(_mime, mime_type.data(), mime_type.size()); 00040 memcpy(_mime + mime_type.size(), content.data(), content.size()); 00041 } 00042 00043 Mime::Mime(const Mime &to_copy) : 00044 _mime(new uint8_t[to_copy.mime_size()]), 00045 _type_size(to_copy._type_size), 00046 _content_size(to_copy._content_size) 00047 { 00048 memcpy(_mime, to_copy._mime, to_copy.mime_size()); 00049 } 00050 00051 Mime::~Mime() 00052 { 00053 delete[] _mime; 00054 } 00055 00056 Mime &Mime::operator=(const Mime &to_copy) 00057 { 00058 if (this == &to_copy) { 00059 return * this; 00060 } 00061 00062 delete[] _mime; 00063 00064 _mime = new uint8_t[to_copy.mime_size()]; 00065 memcpy(_mime, to_copy._mime, to_copy.mime_size()); 00066 _type_size = to_copy._type_size; 00067 _content_size = to_copy._content_size; 00068 00069 return *this; 00070 } 00071 00072 void Mime::set_mime( 00073 const Span<const uint8_t> &mime_type, 00074 const Span<const uint8_t> &content 00075 ) 00076 { 00077 delete[] _mime; 00078 00079 _mime = new uint8_t[mime_type.size() + content.size()]; 00080 memcpy(_mime, mime_type.data(), mime_type.size()); 00081 memcpy(_mime + mime_type.size(), content.data(), content.size()); 00082 _type_size = mime_type.size(); 00083 _content_size = content.size(); 00084 } 00085 00086 Span<const uint8_t> Mime::get_mime_type() const 00087 { 00088 return make_const_Span(_mime, _type_size); 00089 } 00090 00091 Span<const uint8_t> Mime::get_mime_content() const 00092 { 00093 return make_const_Span(_mime + _type_size, _content_size); 00094 } 00095 00096 bool Mime::append_as_record( 00097 MessageBuilder &message_builder, 00098 bool is_last_record 00099 ) const 00100 { 00101 return message_builder.append_record( 00102 RecordType( 00103 RecordType::media_type, 00104 get_mime_type() 00105 ), 00106 get_mime_content(), 00107 is_last_record 00108 ); 00109 } 00110 00111 size_t Mime::get_record_size() const 00112 { 00113 return MessageBuilder::compute_record_size( 00114 Record( 00115 RecordType( 00116 RecordType::media_type, 00117 get_mime_type() 00118 ), 00119 get_mime_content(), 00120 RecordID(), 00121 /* chunk */ false, 00122 /* last record */ false 00123 ) 00124 ); 00125 } 00126 00127 void Mime::move_data( 00128 uint8_t *mime_record, 00129 size_t mime_type_size, 00130 size_t mime_content_size 00131 ) 00132 { 00133 delete[] _mime; 00134 _mime = mime_record; 00135 _type_size = mime_type_size; 00136 _content_size = mime_content_size; 00137 } 00138 00139 size_t Mime::mime_size() const 00140 { 00141 return _type_size + _content_size; 00142 } 00143 00144 bool MimeParser::do_parse(const Record &record, Mime &mime) 00145 { 00146 if (record.type.tnf != RecordType::media_type) { 00147 return false; 00148 } 00149 00150 // A type and a payload should be present 00151 if (record.type.value.empty() || record.payload.empty()) { 00152 return false; 00153 } 00154 00155 // create the buffer 00156 size_t type_size = record.type.value.size(); 00157 size_t content_size = record.payload.size(); 00158 uint8_t *mime_buffer = new uint8_t[type_size + content_size]; 00159 00160 // copy type 00161 memcpy(mime_buffer, record.type.value.data(), type_size); 00162 00163 // copy content 00164 memcpy(mime_buffer + type_size, record.payload.data(), content_size); 00165 00166 mime.move_data(mime_buffer, type_size, content_size); 00167 00168 return true; 00169 } 00170 00171 } // namespace common 00172 } // namespace ndef 00173 } // namespace nfc 00174 } // namespace mbed
Generated on Tue Aug 9 2022 00:37:15 by
1.7.2