9
9
/* INCLUDES *****************************************************************/
10
10
11
11
#include <rtl.h>
12
-
12
+ #include <ntstrsafe.h>
13
13
#define NDEBUG
14
14
#include <debug.h>
15
15
@@ -106,18 +106,29 @@ PSTR
106
106
NTAPI
107
107
RtlIpv4AddressToStringA (
108
108
_In_ const struct in_addr * Addr ,
109
- _Out_writes_ (16 ) PCHAR S )
109
+ _Out_writes_ (IPV4_ADDR_STRING_MAX_LEN ) PCHAR S )
110
110
{
111
- INT Length ;
112
-
113
- if (!S ) return (PSTR )~0 ;
114
-
115
- Length = sprintf (S , "%u.%u.%u.%u" , Addr -> S_un .S_un_b .s_b1 ,
116
- Addr -> S_un .S_un_b .s_b2 ,
117
- Addr -> S_un .S_un_b .s_b3 ,
118
- Addr -> S_un .S_un_b .s_b4 );
111
+ NTSTATUS Status ;
112
+ PSTR End ;
113
+
114
+ if (!S )
115
+ return (PSTR )~0 ;
116
+
117
+ Status = RtlStringCchPrintfExA (S ,
118
+ IPV4_ADDR_STRING_MAX_LEN ,
119
+ & End ,
120
+ NULL ,
121
+ 0 ,
122
+ "%u.%u.%u.%u" ,
123
+ Addr -> S_un .S_un_b .s_b1 ,
124
+ Addr -> S_un .S_un_b .s_b2 ,
125
+ Addr -> S_un .S_un_b .s_b3 ,
126
+ Addr -> S_un .S_un_b .s_b4 );
127
+ ASSERT (Status == STATUS_SUCCESS );
128
+ if (!NT_SUCCESS (Status ))
129
+ return (PSTR )~0 ;
119
130
120
- return S + Length ;
131
+ return End ;
121
132
}
122
133
123
134
/*
@@ -131,23 +142,38 @@ RtlIpv4AddressToStringExA(
131
142
_Out_writes_to_ (* AddressStringLength , * AddressStringLength ) PCHAR AddressString ,
132
143
_Inout_ PULONG AddressStringLength )
133
144
{
134
- CHAR Buffer [IPV4_ADDR_STRING_MAX_LEN + IPV4_PORT_STRING_MAX_LEN ];
145
+ CHAR Buffer [IPV4_ADDR_STRING_MAX_LEN + IPV4_PORT_STRING_MAX_LEN ];
146
+ NTSTATUS Status ;
135
147
ULONG Length ;
148
+ PSTR End ;
136
149
137
150
if (!Address || !AddressString || !AddressStringLength )
138
151
return STATUS_INVALID_PARAMETER ;
139
152
140
- Length = sprintf (Buffer , "%u.%u.%u.%u" , Address -> S_un .S_un_b .s_b1 ,
141
- Address -> S_un .S_un_b .s_b2 ,
142
- Address -> S_un .S_un_b .s_b3 ,
143
- Address -> S_un .S_un_b .s_b4 );
144
-
145
- if (Port ) Length += sprintf (Buffer + Length , ":%u" , WN2H (Port ));
153
+ Status = RtlStringCchPrintfExA (Buffer ,
154
+ RTL_NUMBER_OF (Buffer ),
155
+ & End ,
156
+ NULL ,
157
+ 0 ,
158
+ Port ? "%u.%u.%u.%u:%u"
159
+ : "%u.%u.%u.%u" ,
160
+ Address -> S_un .S_un_b .s_b1 ,
161
+ Address -> S_un .S_un_b .s_b2 ,
162
+ Address -> S_un .S_un_b .s_b3 ,
163
+ Address -> S_un .S_un_b .s_b4 ,
164
+ WN2H (Port ));
165
+ ASSERT (Status == STATUS_SUCCESS );
166
+ if (!NT_SUCCESS (Status ))
167
+ return STATUS_INVALID_PARAMETER ;
146
168
169
+ Length = End - Buffer ;
147
170
if (* AddressStringLength > Length )
148
171
{
172
+ Status = RtlStringCchCopyA (AddressString ,
173
+ * AddressStringLength ,
174
+ Buffer );
175
+ ASSERT (Status == STATUS_SUCCESS );
149
176
* AddressStringLength = Length + 1 ;
150
- strcpy (AddressString , Buffer );
151
177
return STATUS_SUCCESS ;
152
178
}
153
179
@@ -162,17 +188,29 @@ PWSTR
162
188
NTAPI
163
189
RtlIpv4AddressToStringW (
164
190
_In_ const struct in_addr * Addr ,
165
- _Out_writes_ (16 ) PWCHAR S )
191
+ _Out_writes_ (IPV4_ADDR_STRING_MAX_LEN ) PWCHAR S )
166
192
{
167
- INT Length ;
168
-
169
- if (!S ) return (PWSTR )~0 ;
193
+ NTSTATUS Status ;
194
+ PWSTR End ;
195
+
196
+ if (!S )
197
+ return (PWSTR )~0 ;
198
+
199
+ Status = RtlStringCchPrintfExW (S ,
200
+ IPV4_ADDR_STRING_MAX_LEN ,
201
+ & End ,
202
+ NULL ,
203
+ 0 ,
204
+ L"%u.%u.%u.%u" ,
205
+ Addr -> S_un .S_un_b .s_b1 ,
206
+ Addr -> S_un .S_un_b .s_b2 ,
207
+ Addr -> S_un .S_un_b .s_b3 ,
208
+ Addr -> S_un .S_un_b .s_b4 );
209
+ ASSERT (Status == STATUS_SUCCESS );
210
+ if (!NT_SUCCESS (Status ))
211
+ return (PWSTR )~0 ;
170
212
171
- Length = swprintf (S , L"%u.%u.%u.%u" , Addr -> S_un .S_un_b .s_b1 ,
172
- Addr -> S_un .S_un_b .s_b2 ,
173
- Addr -> S_un .S_un_b .s_b3 ,
174
- Addr -> S_un .S_un_b .s_b4 );
175
- return S + Length ;
213
+ return End ;
176
214
}
177
215
178
216
/*
@@ -186,23 +224,38 @@ RtlIpv4AddressToStringExW(
186
224
_Out_writes_to_ (* AddressStringLength , * AddressStringLength ) PWCHAR AddressString ,
187
225
_Inout_ PULONG AddressStringLength )
188
226
{
189
- WCHAR Buffer [IPV4_ADDR_STRING_MAX_LEN + IPV4_PORT_STRING_MAX_LEN ];
227
+ WCHAR Buffer [IPV4_ADDR_STRING_MAX_LEN + IPV4_PORT_STRING_MAX_LEN ];
228
+ NTSTATUS Status ;
190
229
ULONG Length ;
230
+ PWSTR End ;
191
231
192
232
if (!Address || !AddressString || !AddressStringLength )
193
233
return STATUS_INVALID_PARAMETER ;
194
234
195
- Length = swprintf (Buffer , L"%u.%u.%u.%u" , Address -> S_un .S_un_b .s_b1 ,
196
- Address -> S_un .S_un_b .s_b2 ,
197
- Address -> S_un .S_un_b .s_b3 ,
198
- Address -> S_un .S_un_b .s_b4 );
199
-
200
- if (Port ) Length += swprintf (Buffer + Length , L":%u" , WN2H (Port ));
235
+ Status = RtlStringCchPrintfExW (Buffer ,
236
+ RTL_NUMBER_OF (Buffer ),
237
+ & End ,
238
+ NULL ,
239
+ 0 ,
240
+ Port ? L"%u.%u.%u.%u:%u"
241
+ : L"%u.%u.%u.%u" ,
242
+ Address -> S_un .S_un_b .s_b1 ,
243
+ Address -> S_un .S_un_b .s_b2 ,
244
+ Address -> S_un .S_un_b .s_b3 ,
245
+ Address -> S_un .S_un_b .s_b4 ,
246
+ WN2H (Port ));
247
+ ASSERT (Status == STATUS_SUCCESS );
248
+ if (!NT_SUCCESS (Status ))
249
+ return STATUS_INVALID_PARAMETER ;
201
250
251
+ Length = End - AddressString ;
202
252
if (* AddressStringLength > Length )
203
253
{
254
+ Status = RtlStringCchCopyW (AddressString ,
255
+ * AddressStringLength ,
256
+ Buffer );
257
+ ASSERT (Status == STATUS_SUCCESS );
204
258
* AddressStringLength = Length + 1 ;
205
- wcscpy (AddressString , Buffer );
206
259
return STATUS_SUCCESS ;
207
260
}
208
261
0 commit comments