| 1 | /*
|
|---|
| 2 | * Copyright 2002 Mike McCormack for CodeWeavers
|
|---|
| 3 | * Copyright 2005 Juan Lang
|
|---|
| 4 | *
|
|---|
| 5 | * This library is free software; you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | * License as published by the Free Software Foundation; either
|
|---|
| 8 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 | *
|
|---|
| 10 | * This library is distributed in the hope that it will be useful,
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | * Lesser General Public License for more details.
|
|---|
| 14 | *
|
|---|
| 15 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | * License along with this library; if not, write to the Free Software
|
|---|
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | #include "config.h"
|
|---|
| 21 | #include <stdarg.h>
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 |
|
|---|
| 24 | #include "windef.h"
|
|---|
| 25 | #include "winbase.h"
|
|---|
| 26 | #include "wincrypt.h"
|
|---|
| 27 | #include "winreg.h"
|
|---|
| 28 | #include "winuser.h"
|
|---|
| 29 | #include "i_cryptasn1tls.h"
|
|---|
| 30 | #include "crypt32_private.h"
|
|---|
| 31 | #include "wine/debug.h"
|
|---|
| 32 |
|
|---|
| 33 | #define DBG_LOCALLOG DBG_crypt32
|
|---|
| 34 | #include "dbglocal.h"
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | ODINDEBUGCHANNEL(CRYPT32-CRYPT32)
|
|---|
| 38 |
|
|---|
| 39 | static HCRYPTPROV hDefProv;
|
|---|
| 40 | HINSTANCE hInstance;
|
|---|
| 41 |
|
|---|
| 42 | BOOL WINAPI Crypt32DllMain(HINSTANCE hInst, DWORD fdwReason, PVOID pvReserved)
|
|---|
| 43 | {
|
|---|
| 44 | switch (fdwReason)
|
|---|
| 45 | {
|
|---|
| 46 | case DLL_PROCESS_ATTACH:
|
|---|
| 47 | hInstance = hInst;
|
|---|
| 48 | DisableThreadLibraryCalls(hInst);
|
|---|
| 49 | crypt_oid_init();
|
|---|
| 50 | break;
|
|---|
| 51 | case DLL_PROCESS_DETACH:
|
|---|
| 52 | crypt_oid_free();
|
|---|
| 53 | crypt_sip_free();
|
|---|
| 54 | root_store_free();
|
|---|
| 55 | default_chain_engine_free();
|
|---|
| 56 | /* Don't release the default provider on process shutdown, there's
|
|---|
| 57 | * no guarantee the provider dll hasn't already been unloaded.
|
|---|
| 58 | */
|
|---|
| 59 | if (hDefProv && !pvReserved) CryptReleaseContext(hDefProv, 0);
|
|---|
| 60 | break;
|
|---|
| 61 | }
|
|---|
| 62 | return TRUE;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | HCRYPTPROV CRYPT_GetDefaultProvider(void)
|
|---|
| 66 | {
|
|---|
| 67 | if (!hDefProv)
|
|---|
| 68 | {
|
|---|
| 69 | HCRYPTPROV prov;
|
|---|
| 70 |
|
|---|
| 71 | CryptAcquireContextW(&prov, NULL, MS_ENHANCED_PROV_W, PROV_RSA_FULL,
|
|---|
| 72 | CRYPT_VERIFYCONTEXT);
|
|---|
| 73 | InterlockedCompareExchangePointer((PVOID *)&hDefProv, (PVOID)prov,
|
|---|
| 74 | NULL);
|
|---|
| 75 | if (hDefProv != prov)
|
|---|
| 76 | CryptReleaseContext(prov, 0);
|
|---|
| 77 | }
|
|---|
| 78 | return hDefProv;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | typedef void * HLRUCACHE;
|
|---|
| 82 |
|
|---|
| 83 | /* this function is called by Internet Explorer when it is about to verify a
|
|---|
| 84 | * downloaded component. The first parameter appears to be a pointer to an
|
|---|
| 85 | * unknown type, native fails unless it points to a buffer of at least 20 bytes.
|
|---|
| 86 | * The second parameter appears to be an out parameter, whatever it's set to is
|
|---|
| 87 | * passed (by cryptnet.dll) to I_CryptFlushLruCache.
|
|---|
| 88 | */
|
|---|
| 89 | BOOL WINAPI I_CryptCreateLruCache(void *unknown, HLRUCACHE *out)
|
|---|
| 90 | {
|
|---|
| 91 | FIXME("(%p, %p): stub!\n", unknown, out);
|
|---|
| 92 | *out = (void *)0xbaadf00d;
|
|---|
| 93 | return TRUE;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | BOOL WINAPI I_CryptFindLruEntry(DWORD unk0, DWORD unk1)
|
|---|
| 97 | {
|
|---|
| 98 | FIXME("(%08x, %08x): stub!\n", unk0, unk1);
|
|---|
| 99 | return FALSE;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | BOOL WINAPI I_CryptFindLruEntryData(DWORD unk0, DWORD unk1, DWORD unk2)
|
|---|
| 103 | {
|
|---|
| 104 | FIXME("(%08x, %08x, %08x): stub!\n", unk0, unk1, unk2);
|
|---|
| 105 | return FALSE;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | BOOL WINAPI I_CryptCreateLruEntry(HLRUCACHE h, DWORD unk0, DWORD unk1)
|
|---|
| 109 | {
|
|---|
| 110 | FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
|
|---|
| 111 | return FALSE;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | DWORD WINAPI I_CryptFlushLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
|
|---|
| 115 | {
|
|---|
| 116 | FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
|
|---|
| 117 | return 0;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | HLRUCACHE WINAPI I_CryptFreeLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
|
|---|
| 121 | {
|
|---|
| 122 | FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
|
|---|
| 123 | return h;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
|
|---|
| 127 | {
|
|---|
| 128 | return HeapAlloc(GetProcessHeap(), 0, cbSize);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | LPVOID WINAPI CryptMemRealloc(LPVOID pv, ULONG cbSize)
|
|---|
| 132 | {
|
|---|
| 133 | return HeapReAlloc(GetProcessHeap(), 0, pv, cbSize);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | VOID WINAPI CryptMemFree(LPVOID pv)
|
|---|
| 137 | {
|
|---|
| 138 | HeapFree(GetProcessHeap(), 0, pv);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | DWORD WINAPI I_CryptAllocTls(void)
|
|---|
| 142 | {
|
|---|
| 143 | return TlsAlloc();
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | LPVOID WINAPI I_CryptDetachTls(DWORD dwTlsIndex)
|
|---|
| 147 | {
|
|---|
| 148 | LPVOID ret;
|
|---|
| 149 |
|
|---|
| 150 | ret = TlsGetValue(dwTlsIndex);
|
|---|
| 151 | TlsSetValue(dwTlsIndex, NULL);
|
|---|
| 152 | return ret;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | LPVOID WINAPI I_CryptGetTls(DWORD dwTlsIndex)
|
|---|
| 156 | {
|
|---|
| 157 | return TlsGetValue(dwTlsIndex);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | BOOL WINAPI I_CryptSetTls(DWORD dwTlsIndex, LPVOID lpTlsValue)
|
|---|
| 161 | {
|
|---|
| 162 | return TlsSetValue(dwTlsIndex, lpTlsValue);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | BOOL WINAPI I_CryptFreeTls(DWORD dwTlsIndex, DWORD unknown)
|
|---|
| 166 | {
|
|---|
| 167 | TRACE("(%d, %d)\n", dwTlsIndex, unknown);
|
|---|
| 168 | return TlsFree(dwTlsIndex);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | BOOL WINAPI I_CryptGetOssGlobal(DWORD x)
|
|---|
| 172 | {
|
|---|
| 173 | FIXME("%08x\n", x);
|
|---|
| 174 | return FALSE;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | HCRYPTPROV WINAPI I_CryptGetDefaultCryptProv(DWORD reserved)
|
|---|
| 178 | {
|
|---|
| 179 | HCRYPTPROV ret;
|
|---|
| 180 |
|
|---|
| 181 | TRACE("(%08x)\n", reserved);
|
|---|
| 182 |
|
|---|
| 183 | if (reserved)
|
|---|
| 184 | {
|
|---|
| 185 | SetLastError(E_INVALIDARG);
|
|---|
| 186 | return (HCRYPTPROV)0;
|
|---|
| 187 | }
|
|---|
| 188 | ret = CRYPT_GetDefaultProvider();
|
|---|
| 189 | CryptContextAddRef(ret, NULL, 0);
|
|---|
| 190 | return ret;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | BOOL WINAPI I_CryptReadTrustedPublisherDWORDValueFromRegistry(LPCWSTR name,
|
|---|
| 194 | DWORD *value)
|
|---|
| 195 | {
|
|---|
| 196 | static const WCHAR safer[] = {
|
|---|
| 197 | 'S','o','f','t','w','a','r','e','\\','P','o','l','i','c','i','e','s','\\',
|
|---|
| 198 | 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
|
|---|
| 199 | 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
|
|---|
| 200 | 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r','\\',
|
|---|
| 201 | 'S','a','f','e','r',0 };
|
|---|
| 202 | HKEY key;
|
|---|
| 203 | LONG rc;
|
|---|
| 204 | BOOL ret = FALSE;
|
|---|
| 205 |
|
|---|
| 206 | TRACE("(%s, %p)\n", debugstr_w(name), value);
|
|---|
| 207 |
|
|---|
| 208 | *value = 0;
|
|---|
| 209 | rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, safer, &key);
|
|---|
| 210 | if (rc == ERROR_SUCCESS)
|
|---|
| 211 | {
|
|---|
| 212 | DWORD size = sizeof(DWORD);
|
|---|
| 213 |
|
|---|
| 214 | if (!RegQueryValueExW(key, name, NULL, NULL, (LPBYTE)value, &size))
|
|---|
| 215 | ret = TRUE;
|
|---|
| 216 | RegCloseKey(key);
|
|---|
| 217 | }
|
|---|
| 218 | return ret;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | DWORD WINAPI I_CryptInstallOssGlobal(DWORD x, DWORD y, DWORD z)
|
|---|
| 222 | {
|
|---|
| 223 | static int ret = 8;
|
|---|
| 224 | ret++;
|
|---|
| 225 | FIXME("%08x %08x %08x, return value %d\n", x, y, z,ret);
|
|---|
| 226 | return ret;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | BOOL WINAPI I_CryptInstallAsn1Module(ASN1module_t x, DWORD y, void* z)
|
|---|
| 230 | {
|
|---|
| 231 | FIXME("(%p %08x %p): stub\n", x, y, z);
|
|---|
| 232 | return TRUE;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | BOOL WINAPI I_CryptUninstallAsn1Module(HCRYPTASN1MODULE x)
|
|---|
| 236 | {
|
|---|
| 237 | FIXME("(%08x): stub\n", x);
|
|---|
| 238 | return TRUE;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | ASN1decoding_t WINAPI I_CryptGetAsn1Decoder(HCRYPTASN1MODULE x)
|
|---|
| 242 | {
|
|---|
| 243 | FIXME("(%08x): stub\n", x);
|
|---|
| 244 | return NULL;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | ASN1encoding_t WINAPI I_CryptGetAsn1Encoder(HCRYPTASN1MODULE x)
|
|---|
| 248 | {
|
|---|
| 249 | FIXME("(%08x): stub\n", x);
|
|---|
| 250 | return NULL;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | void ulong2string (unsigned long number, char *string, int n, int base)
|
|---|
| 254 | {
|
|---|
| 255 | static char *digits = "0123456789ABCDEF";
|
|---|
| 256 |
|
|---|
| 257 | unsigned long tmp = number;
|
|---|
| 258 | char *s = string;
|
|---|
| 259 | int len = 0;
|
|---|
| 260 | int l = 0;
|
|---|
| 261 | int i;
|
|---|
| 262 |
|
|---|
| 263 | if (n <= 0)
|
|---|
| 264 | {
|
|---|
| 265 | return;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | if (tmp == 0)
|
|---|
| 269 | {
|
|---|
| 270 | s[l++] = digits[0];
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | while (tmp != 0)
|
|---|
| 274 | {
|
|---|
| 275 | if (l >= n)
|
|---|
| 276 | {
|
|---|
| 277 | break;
|
|---|
| 278 | }
|
|---|
| 279 | s[l++] = digits[tmp%base];
|
|---|
| 280 | len++;
|
|---|
| 281 | tmp /= base;
|
|---|
| 282 | }
|
|---|
| 283 | if (l < n)
|
|---|
| 284 | {
|
|---|
| 285 | s[l++] = '\0';
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | s = string;
|
|---|
| 289 |
|
|---|
| 290 | for (i = 0; i < len/2; i++)
|
|---|
| 291 | {
|
|---|
| 292 | tmp = s[i];
|
|---|
| 293 | s[i] = s[len - i - 1];
|
|---|
| 294 | s[len - i - 1] = tmp;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | return;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | void long2string (long number, char *string, int n, int base)
|
|---|
| 301 | {
|
|---|
| 302 | if (n <= 0)
|
|---|
| 303 | {
|
|---|
| 304 | return;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | if (number < 0)
|
|---|
| 308 | {
|
|---|
| 309 | *string++ = '-';
|
|---|
| 310 | number = -number;
|
|---|
| 311 | n--;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | ulong2string (number, string, n, base);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | int string2ulong (const char *string, char **pstring2, unsigned long *pvalue, int base)
|
|---|
| 318 | {
|
|---|
| 319 | unsigned long value = 0;
|
|---|
| 320 | int sign = 1;
|
|---|
| 321 |
|
|---|
| 322 | const char *p = string;
|
|---|
| 323 |
|
|---|
| 324 | if (p[0] == '-')
|
|---|
| 325 | {
|
|---|
| 326 | sign = -1;
|
|---|
| 327 | p++;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | if (base == 0)
|
|---|
| 331 | {
|
|---|
| 332 | if (p[0] == 0 && (p[1] == 'x' || p[1] == 'X'))
|
|---|
| 333 | {
|
|---|
| 334 | base = 16;
|
|---|
| 335 | p += 2;
|
|---|
| 336 | }
|
|---|
| 337 | else if (p[0] == 0)
|
|---|
| 338 | {
|
|---|
| 339 | base = 8;
|
|---|
| 340 | p += 1;
|
|---|
| 341 | }
|
|---|
| 342 | else
|
|---|
| 343 | {
|
|---|
| 344 | base = 10;
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | while (*p)
|
|---|
| 349 | {
|
|---|
| 350 | int digit = 0;
|
|---|
| 351 |
|
|---|
| 352 | if ('0' <= *p && *p <= '9')
|
|---|
| 353 | {
|
|---|
| 354 | digit = *p - '0';
|
|---|
| 355 | }
|
|---|
| 356 | else if ('a' <= *p && *p <= 'f')
|
|---|
| 357 | {
|
|---|
| 358 | digit = *p - 'a' + 0xa;
|
|---|
| 359 | }
|
|---|
| 360 | else if ('A' <= *p && *p <= 'F')
|
|---|
| 361 | {
|
|---|
| 362 | digit = *p - 'A' + 0xa;
|
|---|
| 363 | }
|
|---|
| 364 | else
|
|---|
| 365 | {
|
|---|
| 366 | break;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | if (digit >= base)
|
|---|
| 370 | {
|
|---|
| 371 | break;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | value = value*base + digit;
|
|---|
| 375 |
|
|---|
| 376 | p++;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | if (pstring2)
|
|---|
| 380 | {
|
|---|
| 381 | *pstring2 = (char *)p;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | *pvalue = sign*value;
|
|---|
| 385 |
|
|---|
| 386 | return 1;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | int vsnprintf (char *buf, int n, const char *fmt, va_list args)
|
|---|
| 390 | {
|
|---|
| 391 | int count = 0;
|
|---|
| 392 | char *s = (char *)fmt;
|
|---|
| 393 | char *d = buf;
|
|---|
| 394 |
|
|---|
| 395 | if (n <= 0)
|
|---|
| 396 | {
|
|---|
| 397 | return 0;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | n--;
|
|---|
| 401 |
|
|---|
| 402 | while (*s && count < n)
|
|---|
| 403 | {
|
|---|
| 404 | char tmpstr[16];
|
|---|
| 405 |
|
|---|
| 406 | char *str = NULL;
|
|---|
| 407 |
|
|---|
| 408 | int width = 0;
|
|---|
| 409 | int precision = 0;
|
|---|
| 410 |
|
|---|
| 411 | if (*s == '%')
|
|---|
| 412 | {
|
|---|
| 413 | s++;
|
|---|
| 414 |
|
|---|
| 415 | if ('0' <= *s && *s <= '9' || *s == '-')
|
|---|
| 416 | {
|
|---|
| 417 | char setprec = (*s == '0');
|
|---|
| 418 | string2ulong (s, &s, (unsigned long *)&width, 10);
|
|---|
| 419 | if (setprec)
|
|---|
| 420 | {
|
|---|
| 421 | precision = width;
|
|---|
| 422 | }
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | if (*s == '.')
|
|---|
| 426 | {
|
|---|
| 427 | s++;
|
|---|
| 428 | string2ulong (s, &s, (unsigned long *)&precision, 10);
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | if (*s == 's')
|
|---|
| 432 | {
|
|---|
| 433 | str = va_arg(args, char *);
|
|---|
| 434 | s++;
|
|---|
| 435 | precision = 0;
|
|---|
| 436 | }
|
|---|
| 437 | else if (*s == 'c')
|
|---|
| 438 | {
|
|---|
| 439 | tmpstr[0] = va_arg(args, int);
|
|---|
| 440 | tmpstr[1] = 0;
|
|---|
| 441 | str = &tmpstr[0];
|
|---|
| 442 | s++;
|
|---|
| 443 | precision = 0;
|
|---|
| 444 | }
|
|---|
| 445 | else if (*s == 'p' || *s == 'P')
|
|---|
| 446 | {
|
|---|
| 447 | int num = va_arg(args, int);
|
|---|
| 448 |
|
|---|
| 449 | ulong2string (num, tmpstr, sizeof (tmpstr), 16);
|
|---|
| 450 |
|
|---|
| 451 | str = &tmpstr[0];
|
|---|
| 452 | s++;
|
|---|
| 453 | width = 8;
|
|---|
| 454 | precision = 8;
|
|---|
| 455 | }
|
|---|
| 456 | else
|
|---|
| 457 | {
|
|---|
| 458 | if (*s == 'l')
|
|---|
| 459 | {
|
|---|
| 460 | s++;
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| 463 | if (*s == 'd' || *s == 'i')
|
|---|
| 464 | {
|
|---|
| 465 | int num = va_arg(args, int);
|
|---|
| 466 |
|
|---|
| 467 | long2string (num, tmpstr, sizeof (tmpstr), 10);
|
|---|
| 468 |
|
|---|
| 469 | str = &tmpstr[0];
|
|---|
| 470 | s++;
|
|---|
| 471 | }
|
|---|
| 472 | else if (*s == 'u')
|
|---|
| 473 | {
|
|---|
| 474 | int num = va_arg(args, int);
|
|---|
| 475 |
|
|---|
| 476 | ulong2string (num, tmpstr, sizeof (tmpstr), 10);
|
|---|
| 477 |
|
|---|
| 478 | str = &tmpstr[0];
|
|---|
| 479 | s++;
|
|---|
| 480 | }
|
|---|
| 481 | else if (*s == 'x' || *s == 'X')
|
|---|
| 482 | {
|
|---|
| 483 | int num = va_arg(args, int);
|
|---|
| 484 |
|
|---|
| 485 | ulong2string (num, tmpstr, sizeof (tmpstr), 16);
|
|---|
| 486 |
|
|---|
| 487 | str = &tmpstr[0];
|
|---|
| 488 | s++;
|
|---|
| 489 | }
|
|---|
| 490 | }
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | if (str != NULL)
|
|---|
| 494 | {
|
|---|
| 495 | int i;
|
|---|
| 496 | char numstr[16];
|
|---|
| 497 | int len = strlen (str);
|
|---|
| 498 | int leftalign = 0;
|
|---|
| 499 |
|
|---|
| 500 | if (width < 0)
|
|---|
| 501 | {
|
|---|
| 502 | width = -width;
|
|---|
| 503 | leftalign = 1;
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | if (precision)
|
|---|
| 507 | {
|
|---|
| 508 | i = 0;
|
|---|
| 509 | while (precision > len)
|
|---|
| 510 | {
|
|---|
| 511 | numstr[i++] = '0';
|
|---|
| 512 | precision--;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | memcpy (&numstr[i], str, len);
|
|---|
| 516 |
|
|---|
| 517 | str = &numstr[0];
|
|---|
| 518 | len += i;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | if (len < width && !leftalign)
|
|---|
| 522 | {
|
|---|
| 523 | while (len < width && count < n)
|
|---|
| 524 | {
|
|---|
| 525 | *d++ = ' ';
|
|---|
| 526 | width--;
|
|---|
| 527 | count++;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | if (count >= n)
|
|---|
| 531 | {
|
|---|
| 532 | break;
|
|---|
| 533 | }
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | i = 0;
|
|---|
| 537 | while (i < len && count < n)
|
|---|
| 538 | {
|
|---|
| 539 | *d++ = str[i++];
|
|---|
| 540 | count++;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | if (count >= n)
|
|---|
| 544 | {
|
|---|
| 545 | break;
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | if (len < width && leftalign)
|
|---|
| 549 | {
|
|---|
| 550 | while (len < width && count < n)
|
|---|
| 551 | {
|
|---|
| 552 | *d++ = ' ';
|
|---|
| 553 | width--;
|
|---|
| 554 | count++;
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | if (count >= n)
|
|---|
| 558 | {
|
|---|
| 559 | break;
|
|---|
| 560 | }
|
|---|
| 561 | }
|
|---|
| 562 | }
|
|---|
| 563 | else
|
|---|
| 564 | {
|
|---|
| 565 | *d++ = *s++;
|
|---|
| 566 | count++;
|
|---|
| 567 | }
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | *d = '\0';
|
|---|
| 571 |
|
|---|
| 572 | return count + 1;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | int snprintf (char *buf, int n, const char *fmt, ...)
|
|---|
| 576 | {
|
|---|
| 577 | va_list args;
|
|---|
| 578 |
|
|---|
| 579 | int rc = 0;
|
|---|
| 580 |
|
|---|
| 581 | va_start (args, fmt);
|
|---|
| 582 |
|
|---|
| 583 | rc = vsnprintf (buf, n, fmt, args);
|
|---|
| 584 |
|
|---|
| 585 | va_end (args);
|
|---|
| 586 |
|
|---|
| 587 | return rc;
|
|---|
| 588 | }
|
|---|