1
0
mirror of https://github.com/aluxnimm/outlookcaldavsynchronizer.git synced 2025-10-06 00:12:52 +02:00

Catch FormatException in vCardStandardReader and log warnings from vcard deserialization.

This commit is contained in:
Alexander Nimmervoll
2017-01-24 20:54:16 +01:00
parent 349fd8a2de
commit 02e86d716c
4 changed files with 65 additions and 32 deletions

View File

@@ -18,6 +18,7 @@ using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CalDavSynchronizer.DataAccess;
using CalDavSynchronizer.Diagnostics;
using GenSync.Logging;
@@ -88,7 +89,17 @@ namespace CalDavSynchronizer.Implementation.Contacts
{
using (var reader = new StringReader(vcardData))
{
return serializer.Read(reader);
var card = serializer.Read(reader);
if (serializer.Warnings.Count > 0)
{
var warningsBuilder = new StringBuilder();
foreach (var warning in serializer.Warnings)
{
warningsBuilder.AppendLine(warning);
}
s_logger.WarnFormat ("Encountered warnings while reading vCardData:\r\n{0}\r\n{1}", warningsBuilder, vcardData);
}
return card;
}
}
}

View File

@@ -1,10 +1,10 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.237
// Dieser Code wurde von einem Tool generiert.
// Laufzeitversion:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
// der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------
@@ -13,12 +13,12 @@ namespace Thought.vCards {
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
// Diese Klasse wurde von der StronglyTypedResourceBuilder automatisch generiert
// -Klasse über ein Tool wie ResGen oder Visual Studio automatisch generiert.
// Um einen Member hinzuzufügen oder zu entfernen, bearbeiten Sie die .ResX-Datei und führen dann ResGen
// mit der /str-Option erneut aus, oder Sie erstellen Ihr VS-Projekt neu.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
@@ -33,7 +33,7 @@ namespace Thought.vCards {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// Gibt die zwischengespeicherte ResourceManager-Instanz zurück, die von dieser Klasse verwendet wird.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
@@ -47,8 +47,8 @@ namespace Thought.vCards {
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// Überschreibt die CurrentUICulture-Eigenschaft des aktuellen Threads für alle
/// Ressourcenzuordnungen, die diese stark typisierte Ressourcenklasse verwenden.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
@@ -61,7 +61,7 @@ namespace Thought.vCards {
}
/// <summary>
/// Looks up a localized string similar to Line {0} A blank line was encountered. This is not allowed in the vCard specification..
/// Sucht eine lokalisierte Zeichenfolge, die Line {0} A blank line was encountered. This is not allowed in the vCard specification. ähnelt.
/// </summary>
internal static string BlankLine {
get {
@@ -70,7 +70,7 @@ namespace Thought.vCards {
}
/// <summary>
/// Looks up a localized string similar to Line {0}: A colon (:) is missing. All properties must be in NAME:VALUE format..
/// Sucht eine lokalisierte Zeichenfolge, die Line {0}: A colon (:) is missing. All properties must be in NAME:VALUE format. ähnelt.
/// </summary>
internal static string ColonMissing {
get {
@@ -79,12 +79,21 @@ namespace Thought.vCards {
}
/// <summary>
/// Looks up a localized string similar to Line {0}: The name section of the property is empty..
/// Sucht eine lokalisierte Zeichenfolge, die Line {0}: The name section of the property is empty. ähnelt.
/// </summary>
internal static string EmptyName {
get {
return ResourceManager.GetString("EmptyName", resourceCulture);
}
}
/// <summary>
/// Sucht eine lokalisierte Zeichenfolge, die Line {0}: This property has an invalid base64 encoding. ähnelt.
/// </summary>
internal static string InvalidEncoding {
get {
return ResourceManager.GetString("InvalidEncoding", resourceCulture);
}
}
}
}

View File

@@ -112,10 +112,10 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="BlankLine" xml:space="preserve">
<value>Line {0} A blank line was encountered. This is not allowed in the vCard specification.</value>
@@ -126,4 +126,7 @@
<data name="EmptyName" xml:space="preserve">
<value>Line {0}: The name section of the property is empty.</value>
</data>
<data name="InvalidEncoding" xml:space="preserve">
<value>Line {0}: This property has an invalid base64 encoding.</value>
</data>
</root>

View File

@@ -97,7 +97,7 @@ namespace Thought.vCards
/// <returns>
/// The decoded data as a byte array.
/// </returns>
public static byte[] DecodeBase64(string value)
public byte[] DecodeBase64(string value)
{
// Currently the .NET implementation is acceptable. However,
@@ -106,8 +106,16 @@ namespace Thought.vCards
// instead of the FromBase64String function in .NET.
// Performance is not an issue because the runtime engine
// will inline the code or eliminate the extra call.
return Convert.FromBase64String(value);
try
{
return Convert.FromBase64String(value);
}
catch (FormatException)
{
Warnings.Add (WarningMessages.InvalidEncoding);
return new byte[] {};
}
}
#endregion
@@ -1700,20 +1708,22 @@ namespace Thought.vCards
private void ReadInto_KEY(vCard card, vCardProperty property)
{
// The KEY property defines a security certificate
// that has been attached to the vCard. Key values
// are usually encoded in BASE64 because they
// often consist of binary data.
if (((byte[])property.Value).Length == 0) return;
vCardCertificate certificate = new vCardCertificate();
certificate.Data = (byte[])property.Value;
// The KEY property defines a security certificate
// that has been attached to the vCard. Key values
// are usually encoded in BASE64 because they
// often consist of binary data.
// TODO: Support other key types.
vCardCertificate certificate = new vCardCertificate();
certificate.Data = (byte[]) property.Value;
if (property.Subproperties.Contains("X509"))
certificate.KeyType = "X509";
// TODO: Support other key types.
card.Certificates.Add(certificate);
if (property.Subproperties.Contains("X509"))
certificate.KeyType = "X509";
card.Certificates.Add(certificate);
}
@@ -1969,7 +1979,7 @@ namespace Thought.vCards
{
card.Photos.Add(new vCardPhoto((string)property.Value, true));
}
else
else if (((byte[])property.Value).Length >0)
{
card.Photos.Add(new vCardPhoto((byte[])property.Value));
}