Ignore:
Timestamp:
Jul 3, 2009, 11:59:02 PM (16 years ago)
Author:
ydario
Message:

advapi32 and crypt32 updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/crypt32/main.c

    r21311 r21325  
    250250    return NULL;
    251251}
     252
     253void 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
     300void 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 
     317int 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
     389int 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
     575int 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}
Note: See TracChangeset for help on using the changeset viewer.