ST / NDefLib

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of NDefLib by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordVCard.cpp Source File

RecordVCard.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    RecordVCard.cpp
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   RecordVCard implementation
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 #include <cstring>
00038 #include "RecordVCard.h"
00039 
00040 namespace NDefLib {
00041 
00042 const std::string RecordVCard::sEmptyTagContent("");
00043 const std::string RecordVCard::sVcardMimeType("text/vcard");
00044 const std::string RecordVCard::sStartVCardTag("BEGIN:VCARD\nVERSION:3.0\n");
00045 const std::string RecordVCard::sStartFieldTag[] = {
00046     "ADR:", "ADR;TYPE=home:", "ADR;TYPE=work:", "AGENT:", "BDAY:",
00047     "CATEGORIES:", "EMAIL:", "EMAIL;TYPE=home:", "EMAIL;TYPE=work:",
00048     "FN:", "GEO:", "IMPP:", "KEY;TYPE=PGP:",
00049     "KEY;TYPE=PGP;ENCODING=B:","LOGO:", "LOGO;VALUE=uri:",
00050     "LOGO;ENCODING=B;", "N:", "NICKNAME:", "NOTE:", "ORG:",
00051     "PHOTO;VALUE=uri:", "PHOTO;ENCODING=B;", "REV:", "SOURCE:",
00052     "TEL:", "TEL;TYPE=HOME:", "TEL;TYPE=WORK:", "TEL;TYPE=CELL:",
00053     "TITLE:", "URL:"
00054 };
00055 
00056 const std::string RecordVCard::sEndFieldTag("\n");
00057 const std::string RecordVCard::sEndVCardTag("END:VCARD");
00058 
00059 RecordVCard::RecordVCard(const VCardInfo_t &info) :
00060     RecordMimeType(sVcardMimeType), mCardInfo(info),mContentIsChange(true) {
00061     update_content_info_string();
00062 }
00063 
00064 void RecordVCard::update_content_info_string() {
00065     if (!mContentIsChange) {
00066         return;
00067     }
00068 
00069     mCardInfoString = sStartVCardTag;
00070 
00071     VCardInfo_t::const_iterator it = mCardInfo.begin();
00072     VCardInfo_t::const_iterator end = mCardInfo.end();
00073 
00074     for (; it != end; ++it) {
00075         mCardInfoString += sStartFieldTag[it->first];
00076         mCardInfoString += it->second;
00077         mCardInfoString += sEndFieldTag;
00078     }
00079 
00080     mCardInfoString += sEndVCardTag;
00081 
00082     set_mime_data_pointer((uint8_t*)mCardInfoString.data(),mCardInfoString.size());
00083 
00084     mContentIsChange=false;
00085 }
00086 
00087 uint16_t RecordVCard::find_VCard_field_data_lenght(const std::string &content, uint16_t offset) {
00088     std::size_t pos = content.find(sEndFieldTag, offset);
00089     if (pos == std::string::npos) { //if we don't find the end, lets consume all the content
00090         return content.size() - offset;
00091     }
00092     return content.find(sEndFieldTag, offset) - offset;
00093 }
00094 
00095 int8_t RecordVCard::find_VCard_field_type(const std::string &content, uint16_t offset) {
00096     const uint32_t nFieldTag = sizeof(sStartFieldTag) / sizeof(sStartFieldTag[0]);
00097     for (uint32_t i = 0; i < nFieldTag; i++) {
00098         if (content.find(sStartFieldTag[i], offset) != std::string::npos)
00099             return i;
00100     }
00101 
00102     return -1;
00103 }
00104 
00105 RecordVCard* RecordVCard::parse(const RecordHeader &header, const uint8_t* buffer) {
00106     if (header.get_FNT() != RecordHeader::Mime_media_type || header.get_type_length() != sVcardMimeType.size()) {
00107         return NULL;
00108     }
00109     if (sVcardMimeType.compare(0, sVcardMimeType.size(), (const char*) buffer, sVcardMimeType.size()) != 0) {
00110         return NULL;
00111     }
00112     buffer += header.get_type_length();
00113     //we are at the start of the vcard data
00114     if (sStartVCardTag.compare(0, sStartVCardTag.size(), (const char*) (buffer), sStartVCardTag.size()) != 0) {
00115         return NULL;
00116     }
00117     //the version is ok
00118     const std::string vCardContent((const char*) buffer + sStartVCardTag.size(),
00119         header.get_payload_length() - sStartVCardTag.size());
00120     uint16_t offset = 0;
00121     buffer += sStartVCardTag.size(); // for debug
00122     VCardInfo_t info;
00123     do {
00124         const int8_t type = find_VCard_field_type(vCardContent, offset);
00125 
00126         if (type >= 0) { //if is a valid type
00127             const VCardField_t fieldType = (VCardField_t) type;
00128             offset += sStartFieldTag[fieldType].size(); // skip the tag name
00129             //search the tag size
00130             const uint16_t length = find_VCard_field_data_lenght(vCardContent, offset);
00131             info[fieldType] = vCardContent.substr(offset, length);
00132             offset += length + sEndFieldTag.size();
00133         } else {
00134             //unknow field, skip until next one
00135             offset += find_VCard_field_data_lenght(vCardContent, offset) + sEndFieldTag.size();
00136         }
00137 
00138     } while (offset < vCardContent.size());
00139 
00140     return new RecordVCard(info);
00141 }
00142 
00143 } /* namespace NDefLib */
00144 
00145 
00146 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/