X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FKernel%2Flibc.c;h=2950782453180f8ec63d564a7ac88e69793e0434;hb=6267c32524bc76488a14c5b71e0e8eededce31db;hp=a16687e44cf88cee34ae1de5e3150040ed1a6cf8;hpb=71bbd3999ce518ca1dcfd9a668ad104ae0cb213e;p=tpg%2Facess2.git diff --git a/KernelLand/Kernel/libc.c b/KernelLand/Kernel/libc.c index a16687e4..29507824 100644 --- a/KernelLand/Kernel/libc.c +++ b/KernelLand/Kernel/libc.c @@ -7,6 +7,7 @@ */ #include #include // For MM_* +#include // === CONSTANTS === #define RANDOM_SEED 0xACE55052 @@ -167,6 +168,8 @@ void itoa(char *buf, Uint64 num, int base, int minLength, char pad) // Convert while(num > base-1) { num = DivMod64U(num, base, &rem); // Shift `num` and get remainder + ASSERT(rem >= 0); + ASSERT(rem < base); tmpBuf[pos] = cUCDIGITS[ rem ]; pos++; } @@ -184,8 +187,8 @@ void itoa(char *buf, Uint64 num, int base, int minLength, char pad) * \brief Append a character the the vsnprintf output */ #define PUTCH(ch) do { \ - if(pos < __maxlen) { \ - if(__s) __s[pos] = ch; \ + if(pos < __maxlen && __s) { \ + __s[pos] = ch; \ } else { \ (void)ch;\ } \ @@ -198,13 +201,13 @@ void itoa(char *buf, Uint64 num, int base, int minLength, char pad) /** * \brief VArg String Number Print Formatted */ -int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args) +int vsnprintf(char *__s, const size_t __maxlen, const char *__format, va_list args) { char c, pad = ' '; int minSize = 0, precision = -1, len; char tmpBuf[34]; // For Integers const char *p = NULL; - int isLongLong = 0; + int isLongLong = 0, isLong; Uint64 val; size_t pos = 0; // Flags @@ -235,7 +238,10 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args) PUTCH( cUCDIGITS[ (ptr>>(len*4))&15 ] ); continue ; } - + + isLongLong = 0; + isLong = 0; + // - Padding Side Flag if(c == '-') { bPadLeft = 1; @@ -293,6 +299,7 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args) isLongLong = 0; if(c == 'l') // Long is actually the default on x86 { + isLong = 1; c = *__format++; if(c == 'l') { c = *__format++; @@ -356,6 +363,18 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args) // String - Null Terminated Array case 's': + if( isLong ) { + Uint16 *p16 = va_arg(args, Uint16*); + Uint8 tmp[5]; + while( *p16 && precision-- ) { + Uint32 cp; + p16 += ReadUTF16(p16, &cp); + tmp[WriteUTF8(tmp, cp)] = 0; + for(int i = 0; tmp[i] && i<5; i ++) + PUTCH(tmp[i]); + } + break; + } p = va_arg(args, char*); // Get Argument if( !p || !CheckString(p) ) p = "(inval)"; // Avoid #PFs printString: @@ -385,7 +404,7 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args) } } - if(__s && pos != __maxlen) + if(__s && pos < __maxlen) __s[pos] = '\0'; return pos; @@ -489,7 +508,7 @@ int isspace(int c) } int isupper(int c) { - return ('a' <= c && c <= 'z'); + return ('A' <= c && c <= 'Z'); } int isxdigit(int c) {