Skip to content

Commit 4d49d8a

Browse files
committed
wip
1 parent c2d33fa commit 4d49d8a

1 file changed

Lines changed: 66 additions & 13 deletions

File tree

LibCurlImpersonate/LibCurl.cs

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,57 @@ internal static partial int curl_easy_impersonate(
5252
int defaultHeaders
5353
);
5454

55+
private static readonly bool _isMacOsArm64 =
56+
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
57+
&& RuntimeInformation.ProcessArchitecture == Architecture.Arm64;
58+
59+
// curl_easy_setopt is variadic in C. On both x64 (R8) and ARM64/AAPCS64 (x2),
60+
// the 3rd argument is passed in the first available register — no stack padding needed.
61+
[LibraryImport(
62+
Lib,
63+
EntryPoint = "curl_easy_setopt",
64+
StringMarshalling = StringMarshalling.Utf8
65+
)]
66+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
67+
private static partial int setopt_str_impl(IntPtr h, int opt, string value);
68+
69+
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
70+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
71+
private static partial int setopt_long_impl(IntPtr h, int opt, long value);
72+
73+
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
74+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
75+
private static partial int setopt_ptr_impl(IntPtr h, int opt, IntPtr value);
76+
77+
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
78+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
79+
private static partial int setopt_cb_impl(
80+
IntPtr h,
81+
int opt,
82+
[MarshalAs(UnmanagedType.FunctionPtr)] WriteCallback value
83+
);
84+
85+
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
86+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
87+
private static partial int setopt_xcb_impl(
88+
IntPtr h,
89+
int opt,
90+
[MarshalAs(UnmanagedType.FunctionPtr)] XferInfoCallback value
91+
);
92+
93+
[LibraryImport(Lib, EntryPoint = "curl_easy_getinfo")]
94+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
95+
private static partial int getinfo_long_impl(IntPtr h, int info, out long value);
96+
5597
// ARM64 calling convention fix: fill x2-x7 with dummy zeros so the real value
5698
// lands on the stack at [old_sp], which is where Curl_vsetopt reads it from.
57-
5899
[LibraryImport(
59100
Lib,
60101
EntryPoint = "curl_easy_setopt",
61102
StringMarshalling = StringMarshalling.Utf8
62103
)]
63104
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
64-
private static partial int setopt_str_impl(
105+
private static partial int setopt_str_macos_arm64_impl(
65106
IntPtr h,
66107
int opt,
67108
nint _2,
@@ -75,7 +116,7 @@ string value
75116

76117
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
77118
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
78-
private static partial int setopt_long_impl(
119+
private static partial int setopt_long_macos_arm64_impl(
79120
IntPtr h,
80121
int opt,
81122
nint _2,
@@ -89,7 +130,7 @@ long value
89130

90131
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
91132
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
92-
private static partial int setopt_ptr_impl(
133+
private static partial int setopt_ptr_macos_arm64_impl(
93134
IntPtr h,
94135
int opt,
95136
nint _2,
@@ -103,7 +144,7 @@ IntPtr value
103144

104145
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
105146
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
106-
private static partial int setopt_cb_impl(
147+
private static partial int setopt_cb_macos_arm64_impl(
107148
IntPtr h,
108149
int opt,
109150
nint _2,
@@ -117,7 +158,7 @@ private static partial int setopt_cb_impl(
117158

118159
[LibraryImport(Lib, EntryPoint = "curl_easy_setopt")]
119160
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
120-
private static partial int setopt_xcb_impl(
161+
private static partial int setopt_xcb_macos_arm64_impl(
121162
IntPtr h,
122163
int opt,
123164
nint _2,
@@ -131,7 +172,7 @@ private static partial int setopt_xcb_impl(
131172

132173
[LibraryImport(Lib, EntryPoint = "curl_easy_getinfo")]
133174
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
134-
private static partial int getinfo_long_impl(
175+
private static partial int getinfo_long_macos_arm64_impl(
135176
IntPtr h,
136177
int info,
137178
nint _2,
@@ -144,22 +185,34 @@ out long value
144185
);
145186

146187
internal static int curl_easy_getinfo_long(IntPtr h, int info, out long value) =>
147-
getinfo_long_impl(h, info, 0, 0, 0, 0, 0, 0, out value);
188+
_isMacOsArm64
189+
? getinfo_long_macos_arm64_impl(h, info, 0, 0, 0, 0, 0, 0, out value)
190+
: getinfo_long_impl(h, info, out value);
148191

149192
internal static int curl_easy_setopt_str(IntPtr h, int opt, string v) =>
150-
setopt_str_impl(h, opt, 0, 0, 0, 0, 0, 0, v);
193+
_isMacOsArm64
194+
? setopt_str_macos_arm64_impl(h, opt, 0, 0, 0, 0, 0, 0, v)
195+
: setopt_str_impl(h, opt, v);
151196

152197
internal static int curl_easy_setopt_long(IntPtr h, int opt, long v) =>
153-
setopt_long_impl(h, opt, 0, 0, 0, 0, 0, 0, v);
198+
_isMacOsArm64
199+
? setopt_long_macos_arm64_impl(h, opt, 0, 0, 0, 0, 0, 0, v)
200+
: setopt_long_impl(h, opt, v);
154201

155202
internal static int curl_easy_setopt_ptr(IntPtr h, int opt, IntPtr v) =>
156-
setopt_ptr_impl(h, opt, 0, 0, 0, 0, 0, 0, v);
203+
_isMacOsArm64
204+
? setopt_ptr_macos_arm64_impl(h, opt, 0, 0, 0, 0, 0, 0, v)
205+
: setopt_ptr_impl(h, opt, v);
157206

158207
internal static int curl_easy_setopt_cb(IntPtr h, int opt, WriteCallback v) =>
159-
setopt_cb_impl(h, opt, 0, 0, 0, 0, 0, 0, v);
208+
_isMacOsArm64
209+
? setopt_cb_macos_arm64_impl(h, opt, 0, 0, 0, 0, 0, 0, v)
210+
: setopt_cb_impl(h, opt, v);
160211

161212
internal static int curl_easy_setopt_xcb(IntPtr h, int opt, XferInfoCallback v) =>
162-
setopt_xcb_impl(h, opt, 0, 0, 0, 0, 0, 0, v);
213+
_isMacOsArm64
214+
? setopt_xcb_macos_arm64_impl(h, opt, 0, 0, 0, 0, 0, 0, v)
215+
: setopt_xcb_impl(h, opt, v);
163216

164217
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
165218
internal delegate nuint WriteCallback(IntPtr data, nuint size, nuint nmemb, IntPtr userdata);

0 commit comments

Comments
 (0)