Ejemplo n.º 1
0
        public RtdVCard(byte[] payload) : base("text/x-vCard")
        {
            Trace.WriteLine("Parsing VCard Payload");

            /* First: loop to determine how many lines are present in the VCard	*/
            int i        = 0;
            int Nb_lines = 0;

            while (i < payload.Length)
            {
                if (i + 1 < payload.Length)
                {
                    if (payload[i] == 0x0D && payload[i + 1] == 0x0A)
                    {
                        Nb_lines++;
                        i += 2;
                    }
                    else
                    {
                        i++;
                    }
                }
                else
                {
                    i++;
                }
            }

            /* Second: create an array of strings :                                     */
            /* Each entry corresponds to a line present in the VCard	*/
            string[] lines_array       = new string[Nb_lines];
            int      index             = 0;
            int      begining_position = 0;

            byte[] line;
            i = 0;
            while (i < payload.Length)
            {
                if ((i + 1) < payload.Length)
                {
                    if ((payload[i] == 0x0D) && (payload[i + 1] == 0x0A))
                    {
                        line = new byte[i - begining_position];
                        Array.ConstrainedCopy(payload, begining_position, line, 0, i - begining_position);
                        lines_array[index] = CardBuffer.StringFromBytes(line);
                        index++;
                        i += 2;
                        begining_position = i;
                    }
                    else
                    {
                        i++;
                    }
                }
                else
                {
                    i++;
                }
            }

            /* Third: for each entry in the array of strings							*/
            /* Determine what it represents and populate the VCard Object	*/
            string[] elements;
            char[]   colon = { ':' };
            bool     email_already_found = false;

            for (int j = 0; j < lines_array.Length; j++)
            {
                elements = lines_array[j].Split(colon);

                if (elements[0].IndexOf("FN") != -1)
                {
                    string[] names;
                    char[]   space = { ' ' };
                    names = elements[1].Split(space);
                    if (names.Length > 1)
                    {
                        _first_name  = names[0];
                        _family_name = names[names.Length - 1];
                    }
                }

                if (elements[0].Equals("N"))
                {
                    string[] names;
                    char[]   semicolon = { ';' };
                    names        = elements[1].Split(semicolon);
                    _family_name = names[0];

                    if (names.Length > 0)
                    {
                        _first_name = names[1];
                    }

                    if (names.Length > 1)
                    {
                        _middle_name = names[2];
                    }

                    if (names.Length > 2)
                    {
                        _prefix_name = names[3];
                    }

                    if (names.Length > 3)
                    {
                        _suffix_name = names[4];
                    }
                }

                if (elements[0].IndexOf("NICKNAME") != -1)
                {
                    _nickname = elements[elements.Length - 1];
                }

                if (elements[0].IndexOf("BDAY") != -1)
                {
                    _birthday = elements[elements.Length - 1];
                }

                if (elements[0].IndexOf("ADR") != -1)
                {
                    if (elements[0].IndexOf("work") != -1)
                    {
                        /* Work address																							*/
                        string[] addr_lines;
                        char[]   semicolon = { ';' };
                        addr_lines = elements[elements.Length - 1].Split(semicolon);

                        /* There must be exactly 7 elements, even if some are null	*/
                        /* The first of them, "PO BOX", is ignored								    */
                        if (addr_lines.Length == 7)
                        {
                            if (addr_lines[1].Length > 0)
                            {
                                _pro_Address1 = addr_lines[1];
                            }

                            if (addr_lines[2].Length > 0)
                            {
                                _pro_Address2 = addr_lines[2];
                            }

                            if (addr_lines[3].Length > 0)
                            {
                                _pro_Town = addr_lines[3];
                            }

                            if (addr_lines[4].Length > 0)
                            {
                                _pro_Region_State = addr_lines[4];
                            }

                            if (addr_lines[5].Length > 0)
                            {
                                _pro_Post_Code = addr_lines[5];
                            }

                            if (addr_lines[6].Length > 0)
                            {
                                _pro_Country = addr_lines[6];
                            }
                        }
                    }
                    else
                    {
                        /* Work is not specified	-> "private" tab */
                        string[] addr_lines;
                        char[]   semicolon = { ';' };
                        addr_lines = elements[elements.Length - 1].Split(semicolon);

                        /* There must be exactly 7 elements, even if some are null */
                        /* The first of them : PO BOX, is ignored									 */
                        if (addr_lines.Length == 7)
                        {
                            if (addr_lines[1].Length > 0)
                            {
                                _address1 = addr_lines[1];
                            }

                            if (addr_lines[2].Length > 0)
                            {
                                _address2 = addr_lines[2];
                            }

                            if (addr_lines[3].Length > 0)
                            {
                                _town = addr_lines[3];
                            }

                            if (addr_lines[4].Length > 0)
                            {
                                _region_State = addr_lines[4];
                            }

                            if (addr_lines[5].Length > 0)
                            {
                                _post_Code = addr_lines[5];
                            }

                            if (addr_lines[6].Length > 0)
                            {
                                _country = addr_lines[6];
                            }
                        }
                    }
                }

                if (elements[0].IndexOf("TEL") != -1)
                {
                    if (elements[0].IndexOf("TYPE=") != -1)
                    {
                        if (elements[0].IndexOf("work") != -1)
                        {
                            _business_phone = elements[elements.Length - 1];
                        }

                        if (elements[0].IndexOf("cell") != -1)
                        {
                            _cell_phone = elements[elements.Length - 1];
                        }

                        if (elements[0].IndexOf("home") != -1)
                        {
                            _home_phone = elements[elements.Length - 1];
                        }

                        if (elements[0].IndexOf("pager") != -1)
                        {
                            _pager = elements[elements.Length - 1];
                        }

                        if (elements[0].IndexOf("fax") != -1)
                        {
                            _fax = elements[elements.Length - 1];
                        }
                    }
                    else
                    {
                        /* No TYPE specified => By default : Business phone	*/
                        _business_phone = elements[elements.Length - 1];
                    }
                }

                if (elements[0].IndexOf("EMAIL") != -1)
                {
                    if (!email_already_found)
                    {
                        _email = elements[elements.Length - 1];
                        email_already_found = true;
                    }
                    else
                    {
                        _email_alternative = elements[elements.Length - 1];
                    }
                }

                if (elements[0].IndexOf("TITLE") != -1)
                {
                    _title = elements[elements.Length - 1];
                }

                if (elements[0].IndexOf("ROLE") != -1)
                {
                    _role = elements[elements.Length - 1];
                }

                if (elements[0].IndexOf("ORG") != -1)
                {
                    _company = elements[elements.Length - 1];
                }

                if (elements[0].IndexOf("PHOTO") != -1)
                {
                    /*
                     * string[] spec;
                     * char[] comma = {','};
                     * spec = elements[elements.Length - 1].Split(comma);
                     * _photo = spec[spec.Length - 1];
                     */
                    _photo = elements[elements.Length - 1];
                }
            }
            Trace.WriteLine("VCard Payload successfully parsed");
        }
Ejemplo n.º 2
0
        public static bool Parse(byte[] buffer, ref int offset, ref Ndef ndef, ref bool terminated)
        {
            if (offset > buffer.Length)
            {
                return(false);
            }

            terminated = false;
            ndef       = null;

            /*  Header */
            if (offset + 1 > buffer.Length)
            {
                Trace.WriteLine("NDEF truncated after 'Header' byte");
                return(false);
            }
            byte header = buffer[offset++];

            if (header == 0)
            {
                Trace.WriteLine("Empty byte?");
                return(false);
            }

            /* Type length		*/
            if (offset + 1 > buffer.Length)
            {
                Trace.WriteLine("NDEF truncated after 'Type Length' byte");
                return(false);
            }
            int type_length = buffer[offset++];

            /* Payload length	*/
            int payload_length = 0;

            if ((header & NDEF_HEADER_SHORT_RECORD) != 0)
            {
                if (offset + 1 > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Payload Length' byte");
                    return(false);
                }
                payload_length = buffer[offset++];
            }
            else
            {
                if (offset + 4 > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Payload Length' dword");
                    return(false);
                }
                payload_length  = buffer[offset++]; payload_length *= 0x00000100;
                payload_length += buffer[offset++]; payload_length *= 0x00000100;
                payload_length += buffer[offset++]; payload_length *= 0x00000100;
                payload_length += buffer[offset++];
            }

            /*  ID Length			*/
            int id_length = 0;

            if ((header & NDEF_HEADER_ID_LENGTH_PRESENT) != 0)
            {
                if (offset + 1 > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'ID Length' byte");
                    return(false);
                }
                id_length = buffer[offset++];
            }

            /* Type */
            byte[] type = null;
            if (type_length > 0)
            {
                if (offset + type_length > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Type' bytes");
                    return(false);
                }
                type = new byte[type_length];
                for (int i = 0; i < type_length; i++)
                {
                    type[i] = buffer[offset++];
                }
            }

            /* ID */
            byte[] id = null;
            if (id_length > 0)
            {
                if (offset + id_length > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'ID' bytes");
                    return(false);
                }
                id = new byte[id_length];
                for (int i = 0; i < id_length; i++)
                {
                    id[i] = buffer[offset++];
                }
            }

            /* Payload */
            byte[] payload = null;
            if (payload_length > 0)
            {
                if (offset + payload_length > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Payload' bytes");
                    return(false);
                }
                payload = new byte[payload_length];
                for (int i = 0; i < payload_length; i++)
                {
                    payload[i] = buffer[offset++];
                }
            }

            /* OK */
            string type_s = CardBuffer.StringFromBytes(type);

            switch (header & NDEF_HEADER_TNF_MASK)
            {
            case NDEF_HEADER_TNF_EMPTY:
                break;

            case NDEF_HEADER_TNF_NFC_RTD_WKN:
                if (type_s.Equals("Sp"))
                {
                    Trace.WriteLine("Found a SmartPoster");
                    ndef = new RtdSmartPoster(payload);
                }
                else
                if (type_s.Equals("U"))
                {
                    Trace.WriteLine("Found an URI");
                    ndef = new RtdUri(payload);
                }
                else
                if (type_s.Equals("T"))
                {
                    Trace.WriteLine("Found a Text");
                    ndef = new RtdText(payload);
                }
                else
                if (type_s.Equals("act"))
                {
                    Trace.WriteLine("Found an Action");
                    ndef = new RtdSmartPosterAction(payload);
                }
                else
                if (type_s.Equals("s"))
                {
                    Trace.WriteLine("Found a Size");
                    ndef = new RtdSmartPosterTargetSize(payload);
                }
                else
                if (type_s.Equals("t"))
                {
                    Trace.WriteLine("Found a MIME-Type");
                    ndef = new RtdSmartPosterTargetType(payload);
                }
                else
                if (type_s.Equals("Hs"))
                {
                    Trace.WriteLine("Found a Handover Selector");
                    ndef = new RtdHandoverSelector(payload, ref buffer, ref offset);
                }
                else
                if (type_s.Equals("ac"))
                {
                    Trace.WriteLine("Found a Alternative Carrier");
                    ndef = new RtdAlternativeCarrier(payload);
                }
                else
                {
                    Trace.WriteLine("Found an unknown RTD : " + type_s);
                }
                break;

            case NDEF_HEADER_TNF_MEDIA_TYPE:
                if (type_s.ToLower().Equals("text/x-vcard"))
                {
                    Trace.WriteLine("Found a vCard");
                    ndef = new RtdVCard(payload);
                }
                else
                {
                    Trace.WriteLine("Found a MIME Media : " + type_s);
                    ndef = new RtdMedia(type_s, payload);
                }
                break;

            case NDEF_HEADER_TNF_ABSOLUTE_URI:
                if (type_s.Equals("U"))
                {
                    Trace.WriteLine("Found an absolute URI");
                    ndef = new AbsoluteUri(id, payload);
                }
                break;

            case NDEF_HEADER_TNF_NFC_RTD_EXT:
                Trace.WriteLine("Found TNF urn:nfc:ext");
                break;

            case NDEF_HEADER_TNF_UNKNOWN:
                Trace.WriteLine("Found TNF unknown");
                break;

            case NDEF_HEADER_TNF_UNCHANGED:
                Trace.WriteLine("Found TNF unchanged");
                break;

            case NDEF_HEADER_TNF_RESERVED:
                Trace.WriteLine("Found TNF reserved");
                break;

            default:
                return(false);                        // Invalid
            }

            if (ndef == null)
            {
                ndef = new Ndef(header, type, id, payload);
            }

            if (offset >= buffer.Length)
            {
                Trace.WriteLine("Done!");
                terminated = true;
            }

            return(true);
        }