Permalink
Browse files

Properly handle encoding of strings in Steam API. Fixes #5.

  • Loading branch information...
gibbed committed Nov 14, 2017
1 parent 8e48814 commit cbef61fc2a6ff46664964704ce0f273e45ef40a7
View
@@ -0,0 +1,141 @@
/* Copyright (c) 2017 Rick (rick 'at' gibbed 'dot' us)
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would
* be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
using System;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;
namespace SAM.API
{
internal class NativeStrings
{
public sealed class StringHandle : SafeHandleZeroOrMinusOneIsInvalid
{
internal StringHandle(IntPtr preexistingHandle, bool ownsHandle)
: base(ownsHandle)
{
this.SetHandle(preexistingHandle);
}
public IntPtr Handle
{
get { return this.handle; }
}
protected override bool ReleaseHandle()
{
if (handle != IntPtr.Zero)
{
Marshal.FreeHGlobal(handle);
handle = IntPtr.Zero;
return true;
}
return false;
}
}
public static unsafe StringHandle StringToStringHandle(string value)
{
if (value == null)
{
return new StringHandle(IntPtr.Zero, true);
}
var bytes = Encoding.UTF8.GetBytes(value);
var length = bytes.Length;
var p = Marshal.AllocHGlobal(length + 1);
Marshal.Copy(bytes, 0, p, bytes.Length);
((byte*)p)[length] = 0;
return new StringHandle(p, true);
}
public static unsafe string PointerToString(sbyte* bytes)
{
if (bytes == null)
{
return null;
}
int running = 0;
var b = bytes;
if (*b == 0)
{
return string.Empty;
}
while ((*b++) != 0)
{
running++;
}
return new string(bytes, 0, running, Encoding.UTF8);
}
public static unsafe string PointerToString(byte* bytes)
{
return PointerToString((sbyte*)bytes);
}
public static unsafe string PointerToString(IntPtr nativeData)
{
return PointerToString((sbyte*)nativeData.ToPointer());
}
public static unsafe string PointerToString(sbyte* bytes, int length)
{
if (bytes == null)
{
return null;
}
int running = 0;
var b = bytes;
if (length == 0 || *b == 0)
{
return string.Empty;
}
while ((*b++) != 0 &&
running < length)
{
running++;
}
return new string(bytes, 0, running, Encoding.UTF8);
}
public static unsafe string PointerToString(byte* bytes, int length)
{
return PointerToString((sbyte*)bytes, length);
}
public static unsafe string PointerToString(IntPtr nativeData, int length)
{
return PointerToString((sbyte*)nativeData.ToPointer(), length);
}
}
}
View
@@ -39,6 +39,7 @@
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
@@ -50,6 +51,7 @@
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@@ -69,6 +71,7 @@
<Compile Include="Callbacks\AppDataChanged.cs" />
<Compile Include="Callbacks\UserStatsReceived.cs" />
<Compile Include="ICallback.cs" />
<Compile Include="NativeStrings.cs" />
<Compile Include="Types\CallbackMessage.cs" />
<Compile Include="Wrappers\SteamUserStats007.cs" />
<Compile Include="Types\AccountType.cs" />
@@ -22,7 +22,6 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using SAM.API.Interfaces;
namespace SAM.API.Wrappers
@@ -34,22 +33,27 @@ public class SteamApps001 : NativeWrapper<ISteamApps001>
private delegate int NativeGetAppData(
IntPtr self,
uint appId,
string key,
StringBuilder valueBuffer,
IntPtr key,
IntPtr value,
int valueLength);
public string GetAppData(uint appId, string key)
{
var sb = new StringBuilder();
sb.EnsureCapacity(1024);
int result = this.Call<int, NativeGetAppData>(
this.Functions.GetAppData,
this.ObjectAddress,
appId,
key,
sb,
sb.Capacity);
return result == 0 ? null : sb.ToString();
using (var nativeHandle = NativeStrings.StringToStringHandle(key))
{
const int valueLength = 1024;
var valuePointer = Marshal.AllocHGlobal(valueLength);
int result = this.Call<int, NativeGetAppData>(
this.Functions.GetAppData,
this.ObjectAddress,
appId,
nativeHandle.Handle,
valuePointer,
valueLength);
var value = result == 0 ? null : NativeStrings.PointerToString(valuePointer, valueLength);
Marshal.FreeHGlobal(valuePointer);
return value;
}
}
#endregion
}
@@ -48,7 +48,7 @@ public string GetCurrentGameLanguage()
var languagePointer = this.Call<IntPtr, NativeGetCurrentGameLanguage>(
this.Functions.GetCurrentGameLanguage,
this.ObjectAddress);
return Marshal.PtrToStringAnsi(languagePointer);
return NativeStrings.PointerToString(languagePointer);
}
#endregion
}
@@ -95,20 +95,23 @@ public void SetLocalIPBinding(uint host, ushort port)
#region GetISteamUser
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr NativeGetISteamUser(IntPtr self, int user, int pipe, string version);
private delegate IntPtr NativeGetISteamUser(IntPtr self, int user, int pipe, IntPtr version);
private TClass GetISteamUser<TClass>(int user, int pipe, string version)
where TClass : INativeWrapper, new()
{
IntPtr address = this.Call<IntPtr, NativeGetISteamUser>(
this.Functions.GetISteamUser,
this.ObjectAddress,
user,
pipe,
version);
var result = new TClass();
result.SetupFunctions(address);
return result;
using (var nativeVersion = NativeStrings.StringToStringHandle(version))
{
IntPtr address = this.Call<IntPtr, NativeGetISteamUser>(
this.Functions.GetISteamUser,
this.ObjectAddress,
user,
pipe,
nativeVersion.Handle);
var result = new TClass();
result.SetupFunctions(address);
return result;
}
}
#endregion
@@ -121,20 +124,23 @@ public SteamUser012 GetSteamUser012(int user, int pipe)
#region GetISteamUserStats
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr NativeGetISteamUserStats(IntPtr self, int user, int pipe, string version);
private delegate IntPtr NativeGetISteamUserStats(IntPtr self, int user, int pipe, IntPtr version);
private TClass GetISteamUserStats<TClass>(int user, int pipe, string version)
where TClass : INativeWrapper, new()
{
IntPtr address = this.Call<IntPtr, NativeGetISteamUserStats>(
this.Functions.GetISteamUserStats,
this.ObjectAddress,
user,
pipe,
version);
var result = new TClass();
result.SetupFunctions(address);
return result;
using (var nativeVersion = NativeStrings.StringToStringHandle(version))
{
IntPtr address = this.Call<IntPtr, NativeGetISteamUserStats>(
this.Functions.GetISteamUserStats,
this.ObjectAddress,
user,
pipe,
nativeVersion.Handle);
var result = new TClass();
result.SetupFunctions(address);
return result;
}
}
#endregion
@@ -147,19 +153,22 @@ public SteamUserStats007 GetSteamUserStats006(int user, int pipe)
#region GetISteamUtils
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr NativeGetISteamUtils(IntPtr self, int pipe, string version);
private delegate IntPtr NativeGetISteamUtils(IntPtr self, int pipe, IntPtr version);
public TClass GetISteamUtils<TClass>(int pipe, string version)
where TClass : INativeWrapper, new()
{
IntPtr address = this.Call<IntPtr, NativeGetISteamUtils>(
this.Functions.GetISteamUtils,
this.ObjectAddress,
pipe,
version);
var result = new TClass();
result.SetupFunctions(address);
return result;
using (var nativeVersion = NativeStrings.StringToStringHandle(version))
{
IntPtr address = this.Call<IntPtr, NativeGetISteamUtils>(
this.Functions.GetISteamUtils,
this.ObjectAddress,
pipe,
nativeVersion.Handle);
var result = new TClass();
result.SetupFunctions(address);
return result;
}
}
#endregion
@@ -171,15 +180,22 @@ public SteamUtils005 GetSteamUtils004(int pipe)
#endregion
#region GetISteamApps
private delegate IntPtr NativeGetISteamApps(int user, int pipe, string version);
private delegate IntPtr NativeGetISteamApps(int user, int pipe, IntPtr version);
private TClass GetISteamApps<TClass>(int user, int pipe, string version)
where TClass : INativeWrapper, new()
{
IntPtr address = this.Call<IntPtr, NativeGetISteamApps>(this.Functions.GetISteamApps, user, pipe, version);
var result = new TClass();
result.SetupFunctions(address);
return result;
using (var nativeVersion = NativeStrings.StringToStringHandle(version))
{
IntPtr address = this.Call<IntPtr, NativeGetISteamApps>(
this.Functions.GetISteamApps,
user,
pipe,
nativeVersion.Handle);
var result = new TClass();
result.SetupFunctions(address);
return result;
}
}
#endregion
Oops, something went wrong.

0 comments on commit cbef61f

Please sign in to comment.