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