Skip to content

Conservative and liberal string encodings #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions LibGit2Sharp/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private Branch BuildFromReferenceName(string canonicalName)
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<Branch> GetEnumerator()
{
return Proxy.git_branch_foreach(repo.Handle, GitBranchType.GIT_BRANCH_LOCAL | GitBranchType.GIT_BRANCH_REMOTE, branchToCanoncialName)
return Proxy.git_branch_foreach(repo.Handle, GitBranchType.GIT_BRANCH_LOCAL | GitBranchType.GIT_BRANCH_REMOTE, BranchToCanonicalName)
.Select(n => this[n])
.GetEnumerator();
}
Expand Down Expand Up @@ -203,9 +203,9 @@ private static bool LooksLikeABranchName(string referenceName)
referenceName.LooksLikeRemoteTrackingBranch();
}

private static string branchToCanoncialName(IntPtr namePtr, GitBranchType branchType)
private static string BranchToCanonicalName(IntPtr namePtr, GitBranchType branchType)
{
string shortName = Utf8Marshaler.FromNative(namePtr);
string shortName = LaxUtf8Marshaler.FromNative(namePtr);

switch (branchType)
{
Expand Down
9 changes: 4 additions & 5 deletions LibGit2Sharp/CheckoutCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ private void OnGitCheckoutProgress(IntPtr str, UIntPtr completedSteps, UIntPtr t
if (onCheckoutProgress != null)
{
// Convert null strings into empty strings.
string path = (str != IntPtr.Zero) ? Utf8Marshaler.FromNative(str) : string.Empty;
FilePath path = LaxFilePathMarshaler.FromNative(str) ?? FilePath.Empty;

onCheckoutProgress(path, (int)completedSteps, (int)totalSteps);
onCheckoutProgress(path.Native, (int)completedSteps, (int)totalSteps);
}
}

Expand All @@ -103,9 +103,8 @@ private int OnGitCheckoutNotify(
int result = 0;
if (this.onCheckoutNotify != null)
{
string path = (pathPtr != IntPtr.Zero) ?
((FilePath)Utf8Marshaler.FromNative(pathPtr)).Native : string.Empty;
result = onCheckoutNotify(path, why) ? 0 : 1;
FilePath path = LaxFilePathMarshaler.FromNative(pathPtr) ?? FilePath.Empty;
result = onCheckoutNotify(path.Native, why) ? 0 : 1;
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ private IEnumerable<ConfigurationEntry<string>> BuildConfigEntries()
{
var entry = (GitConfigEntry)Marshal.PtrToStructure(entryPtr, typeof(GitConfigEntry));

return new ConfigurationEntry<string>(Utf8Marshaler.FromNative(entry.namePtr),
Utf8Marshaler.FromNative(entry.valuePtr),
return new ConfigurationEntry<string>(LaxUtf8Marshaler.FromNative(entry.namePtr),
LaxUtf8Marshaler.FromNative(entry.valuePtr),
(ConfigurationLevel)entry.level);
});
}
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/ContentChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ private int FileCallback(GitDiffDelta delta, float progress, IntPtr payload)

private int HunkCallback(GitDiffDelta delta, GitDiffRange range, IntPtr header, UIntPtr headerlen, IntPtr payload)
{
string decodedContent = Utf8Marshaler.FromNative(header, (int)headerlen);
string decodedContent = LaxUtf8Marshaler.FromNative(header, (int)headerlen);

AppendToPatch(decodedContent);
return 0;
}

private int LineCallback(GitDiffDelta delta, GitDiffRange range, GitDiffLineOrigin lineorigin, IntPtr content, UIntPtr contentlen, IntPtr payload)
{
string decodedContent = Utf8Marshaler.FromNative(content, (int)contentlen);
string decodedContent = LaxUtf8Marshaler.FromNative(content, (int)contentlen);

string prefix;

Expand Down
23 changes: 14 additions & 9 deletions LibGit2Sharp/Core/EncodingMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ public void CleanUpManagedData(object managedObj)

public virtual void CleanUpNativeData(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
{
return;
}

Marshal.FreeHGlobal(pNativeData);
Cleanup(pNativeData);
}

public int GetNativeDataSize()
Expand Down Expand Up @@ -83,7 +78,17 @@ public static unsafe IntPtr FromManaged(Encoding encoding, String value)
return new IntPtr(buffer);
}

public static unsafe String FromNative(Encoding encoding, IntPtr pNativeData)
public static void Cleanup(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
{
return;
}

Marshal.FreeHGlobal(pNativeData);
}

public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
{
Expand All @@ -107,7 +112,7 @@ public static unsafe String FromNative(Encoding encoding, IntPtr pNativeData)
return new String((sbyte*)pNativeData.ToPointer(), 0, (int)(walk - start), encoding);
}

public static unsafe String FromNative(Encoding encoding, IntPtr pNativeData, int length)
public static unsafe string FromNative(Encoding encoding, IntPtr pNativeData, int length)
{
if (pNativeData == IntPtr.Zero)
{
Expand All @@ -122,7 +127,7 @@ public static unsafe String FromNative(Encoding encoding, IntPtr pNativeData, in
return new String((sbyte*)pNativeData.ToPointer(), 0, length, encoding);
}

public static String FromBuffer(Encoding encoding, byte[] buffer)
public static string FromBuffer(Encoding encoding, byte[] buffer)
{
if (buffer == null)
{
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/Ensure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static void HandleError(int result)
}
else
{
errorMessage = Utf8Marshaler.FromNative(error.Message);
errorMessage = LaxUtf8Marshaler.FromNative(error.Message);
}

Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException> exceptionBuilder;
Expand Down
56 changes: 43 additions & 13 deletions LibGit2Sharp/Core/FilePathMarshaler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace LibGit2Sharp.Core
{
Expand All @@ -11,13 +12,13 @@ namespace LibGit2Sharp.Core
/// Use this marshaler for return values, for example:
/// [return: MarshalAs(UnmanagedType.CustomMarshaler,
/// MarshalCookie = UniqueId.UniqueIdentifier,
/// MarshalTypeRef = typeof(FilePathNoCleanupMarshaler))]
/// MarshalTypeRef = typeof(LaxFilePathNoCleanupMarshaler))]
/// </summary>
internal class FilePathNoCleanupMarshaler : FilePathMarshaler
internal class LaxFilePathNoCleanupMarshaler : LaxFilePathMarshaler
{
private static readonly FilePathNoCleanupMarshaler staticInstance = new FilePathNoCleanupMarshaler();
private static readonly LaxFilePathNoCleanupMarshaler staticInstance = new LaxFilePathNoCleanupMarshaler();

public static new ICustomMarshaler GetInstance(String cookie)
public new static ICustomMarshaler GetInstance(String cookie)
{
return staticInstance;
}
Expand All @@ -43,11 +44,11 @@ public override void CleanUpNativeData(IntPtr pNativeData)
/// internal static extern int git_index_open(out IndexSafeHandle index,
/// [MarshalAs(UnmanagedType.CustomMarshaler,
/// MarshalCookie = UniqueId.UniqueIdentifier,
/// MarshalTypeRef = typeof(FilePathMarshaler))] FilePath indexpath);
/// MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath indexpath);
/// </summary>
internal class FilePathMarshaler : Utf8Marshaler
internal class StrictFilePathMarshaler : StrictUtf8Marshaler
{
private static readonly FilePathMarshaler staticInstance = new FilePathMarshaler();
private static readonly StrictFilePathMarshaler staticInstance = new StrictFilePathMarshaler();

public new static ICustomMarshaler GetInstance(String cookie)
{
Expand All @@ -74,11 +75,6 @@ public override IntPtr MarshalManagedToNative(Object managedObj)
return FromManaged(filePath);
}

public override Object MarshalNativeToManaged(IntPtr pNativeData)
{
return (FilePath)FromNative(pNativeData);
}

#endregion

public static IntPtr FromManaged(FilePath filePath)
Expand All @@ -88,7 +84,41 @@ public static IntPtr FromManaged(FilePath filePath)
return IntPtr.Zero;
}

return Utf8Marshaler.FromManaged(filePath.Posix);
return StrictUtf8Marshaler.FromManaged(filePath.Posix);
}
}

/// <summary>
/// This marshaler is to be used for capturing a UTF-8 string allocated by libgit2 and
/// converting it to a managed FilePath instance. The marshaler will free the native pointer
/// after conversion.
/// </summary>
internal class LaxFilePathMarshaler : LaxUtf8Marshaler
{
private static readonly LaxFilePathMarshaler staticInstance = new LaxFilePathMarshaler();

public new static ICustomMarshaler GetInstance(String cookie)
{
return staticInstance;
}

#region ICustomMarshaler

public override Object MarshalNativeToManaged(IntPtr pNativeData)
{
return FromNative(pNativeData);
}

#endregion

public new static FilePath FromNative(IntPtr pNativeData)
{
return LaxUtf8Marshaler.FromNative(pNativeData);
}

public new static FilePath FromBuffer(byte[] buffer)
{
return LaxUtf8Marshaler.FromBuffer(buffer);
}
}
}
9 changes: 2 additions & 7 deletions LibGit2Sharp/Core/GitRepositoryInitOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static GitRepositoryInitOptions BuildFrom(FilePath workdirPath, bool isBa
{
Debug.Assert(!isBare);

opts.WorkDirPath = FilePathMarshaler.FromManaged(workdirPath);
opts.WorkDirPath = StrictFilePathMarshaler.FromManaged(workdirPath);
}

if (isBare)
Expand All @@ -41,12 +41,7 @@ public static GitRepositoryInitOptions BuildFrom(FilePath workdirPath, bool isBa

public void Dispose()
{
if (WorkDirPath == IntPtr.Zero)
{
return;
}

Marshal.FreeHGlobal(WorkDirPath);
EncodingMarshaler.Cleanup(WorkDirPath);
WorkDirPath = IntPtr.Zero;
}
}
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Core/GitStrArrayIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static GitStrArrayIn BuildFrom(FilePath[] paths)
for (int i = 0; i < nbOfPaths; i++)
{
var s = paths[i].Posix;
pathPtrs[i] = FilePathMarshaler.FromManaged(s);
pathPtrs[i] = StrictFilePathMarshaler.FromManaged(s);
}

int dim = IntPtr.Size * nbOfPaths;
Expand All @@ -45,7 +45,7 @@ public void Dispose()

for (int i = 0; i < nbOfPaths; i++)
{
Marshal.FreeHGlobal(pathPtrs[i]);
EncodingMarshaler.Cleanup(pathPtrs[i]);
}

Marshal.FreeHGlobal(strings);
Expand Down
Loading