win-pvdrivers

view mingw/mingw_extras.c @ 783:644e5ddb1b47

Handle SCSI INQUIRY command better in xenvbd
author James Harper <james.harper@bendigoit.com.au>
date Mon Feb 15 20:53:57 2010 +1100 (2010-02-15)
parents 8e10579159a0
children
line source
1 /*
2 * Provide missing std functions needed by the driver but not provided by
3 * MinGW. Code mostly taken from Linux or as noted.
4 *
5 * Remaining code is
6 * Copyright Andy Grover <andy.grover@oracle.com>
7 * and licensed under the GPLv2.
8 */
10 #include <ntddk.h>
11 #include "mingw_extras.h"
13 NTSTATUS bit_scan_forward(unsigned long *index, unsigned long mask)
14 {
15 int i;
17 for (i = 0; i < sizeof(unsigned long)*8; i++)
18 {
19 if (mask & (1 << i)) {
20 *index = i + 1;
21 KdPrint(("file %s line %s Check that I work as expected!\n", __FILE__, __LINE__));
22 return 1;
23 }
24 }
26 KdPrint(("file %s line %s Check that I work as expected!\n", __FILE__, __LINE__));
28 return 0;
29 }
31 /**
32 From linux include/asm-i386/bitops.h
33 */
34 int synch_set_bit(int nr, volatile long * addr)
35 {
36 int oldbit;
38 __asm__ __volatile__( "lock;"
39 "btsl %2,%1\n\tsbbl %0,%0"
40 :"=r" (oldbit),"+m" (*(volatile long *) addr)
41 :"Ir" (nr) : "memory");
43 KdPrint(("file %s line %s Check that I work as expected!\n", __FILE__, __LINE__));
45 return oldbit;
46 }
48 int synch_clear_bit(int nr, volatile long * addr)
49 {
50 int oldbit;
52 __asm__ __volatile__( "lock;"
53 "btrl %2,%1\n\tsbbl %0,%0"
54 :"=r" (oldbit),"+m" (*(volatile long *) addr)
55 :"Ir" (nr) : "memory");
57 KdPrint(("file %s line %s Check that I work as expected!\n", __FILE__, __LINE__));
59 return oldbit;
60 }
62 NTSTATUS
63 RtlStringCbPrintfW(
64 win_wchar_t *dest_str,
65 size_t dest_size,
66 win_wchar_t *format,
67 ...)
68 {
69 va_list args;
70 int len;
71 int i;
72 char tmp_buf[512];
73 NTSTATUS status = STATUS_SUCCESS;
75 if (dest_size > sizeof(tmp_buf))
76 dest_size = sizeof(tmp_buf);
78 /* we don't have a 2-byte version of vsnprintf, so write it to a single-byte
79 array using vsnprintf() and then copy result to the wchar buffer.
80 This should be seldom executed, so this inefficiency should be ok. */
81 va_start(args, format);
82 len = vsnprintf(tmp_buf, sizeof(tmp_buf), (char *)format, args);
83 va_end(args);
85 if (len >= (dest_size * sizeof(win_wchar_t))) {
86 /* output buffer truncated */
87 status = STATUS_BUFFER_OVERFLOW;
88 len = sizeof(tmp_buf) - 1;
89 tmp_buf[len] = '\0';
90 }
92 /* copy byte-string to short_string, incl NULL */
93 for (i = 0; i < (len + 1); i++)
94 {
95 dest_str[i] = tmp_buf[i];
96 }
98 return status;
99 }
101 /* ----- BEGIN Other people's code --------- */
103 /* from arch/x86/boot/string.c, used under GPLv2 */
104 /* Copyright (C) 1991, 1992 Linus Torvalds
105 * Copyright 2007 rPath, Inc. - All Rights Reserved
106 */
107 size_t strnlen(const char *s, size_t maxlen)
108 {
109 const char *es = s;
110 while (*es && maxlen) {
111 es++;
112 maxlen--;
113 }
115 return (es - s);
116 }
118 /* from arch/x86/boot/boot.h, used under GPLv2 */
119 /* Copyright (C) 1991, 1992 Linus Torvalds
120 * Copyright 2007 rPath, Inc. - All Rights Reserved
121 */
122 static int isdigit(int ch)
123 {
124 return (ch >= '0') && (ch <= '9');
125 }
127 /* from K&Rv2 p. 43 */
128 int atoi(const char s[])
129 {
130 int i, n;
132 n = 0;
133 for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
134 n = 10 * n + (s[i] - '0');
135 return n;
136 }
138 /* from linux/lib/vsprintf.c, used under GPLv2 */
139 /* Copyright (C) 1991, 1992 Linus Torvalds
140 * Wirzenius wrote this portably, Torvalds fucked it up :-)
141 */
142 static int skip_atoi(const char **s)
143 {
144 int i=0;
146 while (isdigit(**s))
147 i = i*10 + *((*s)++) - '0';
148 return i;
149 }
151 int snprintf(char * buf, size_t size, const char *fmt, ...)
152 {
153 va_list args;
154 int i;
156 va_start(args, fmt);
157 i = vsnprintf(buf,size,fmt,args);
158 va_end(args);
159 return i;
160 }
162 #define do_div(n,base) ({ \
163 int __res; \
164 __res = ((unsigned long) n) % (unsigned) base; \
165 n = ((unsigned long) n) / (unsigned) base; \
166 __res; })
169 /* Decimal conversion is by far the most typical, and is used
170 * for /proc and /sys data. This directly impacts e.g. top performance
171 * with many processes running. We optimize it for speed
172 * using code from
173 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
174 * (with permission from the author, Douglas W. Jones). */
176 /* Formats correctly any integer in [0,99999].
177 * Outputs from one to five digits depending on input.
178 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
179 static char* put_dec_trunc(char *buf, unsigned q)
180 {
181 unsigned d3, d2, d1, d0;
182 d1 = (q>>4) & 0xf;
183 d2 = (q>>8) & 0xf;
184 d3 = (q>>12);
186 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
187 q = (d0 * 0xcd) >> 11;
188 d0 = d0 - 10*q;
189 *buf++ = d0 + '0'; /* least significant digit */
190 d1 = q + 9*d3 + 5*d2 + d1;
191 if (d1 != 0) {
192 q = (d1 * 0xcd) >> 11;
193 d1 = d1 - 10*q;
194 *buf++ = d1 + '0'; /* next digit */
196 d2 = q + 2*d2;
197 if ((d2 != 0) || (d3 != 0)) {
198 q = (d2 * 0xd) >> 7;
199 d2 = d2 - 10*q;
200 *buf++ = d2 + '0'; /* next digit */
202 d3 = q + 4*d3;
203 if (d3 != 0) {
204 q = (d3 * 0xcd) >> 11;
205 d3 = d3 - 10*q;
206 *buf++ = d3 + '0'; /* next digit */
207 if (q != 0)
208 *buf++ = q + '0'; /* most sign. digit */
209 }
210 }
211 }
212 return buf;
213 }
214 /* Same with if's removed. Always emits five digits */
215 static char* put_dec_full(char *buf, unsigned q)
216 {
217 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
218 /* but anyway, gcc produces better code with full-sized ints */
219 unsigned d3, d2, d1, d0;
220 d1 = (q>>4) & 0xf;
221 d2 = (q>>8) & 0xf;
222 d3 = (q>>12);
224 /* Possible ways to approx. divide by 10 */
225 /* gcc -O2 replaces multiply with shifts and adds */
226 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
227 // (x * 0x67) >> 10: 1100111
228 // (x * 0x34) >> 9: 110100 - same
229 // (x * 0x1a) >> 8: 11010 - same
230 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
232 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
233 q = (d0 * 0xcd) >> 11;
234 d0 = d0 - 10*q;
235 *buf++ = d0 + '0';
236 d1 = q + 9*d3 + 5*d2 + d1;
237 q = (d1 * 0xcd) >> 11;
238 d1 = d1 - 10*q;
239 *buf++ = d1 + '0';
241 d2 = q + 2*d2;
242 q = (d2 * 0xd) >> 7;
243 d2 = d2 - 10*q;
244 *buf++ = d2 + '0';
246 d3 = q + 4*d3;
247 q = (d3 * 0xcd) >> 11; /* - shorter code */
248 /* q = (d3 * 0x67) >> 10; - would also work */
249 d3 = d3 - 10*q;
250 *buf++ = d3 + '0';
251 *buf++ = q + '0';
252 return buf;
253 }
255 static char* put_dec(char *buf, unsigned long long num)
256 {
257 while (1) {
258 unsigned rem;
259 if (num < 100000)
260 return put_dec_trunc(buf, num);
261 rem = do_div(num, 100000);
262 buf = put_dec_full(buf, rem);
263 }
264 }
267 #define ZEROPAD 1 /* pad with zero */
268 #define SIGN 2 /* unsigned/signed long */
269 #define PLUS 4 /* show plus */
270 #define SPACE 8 /* space if plus */
271 #define LEFT 16 /* left justified */
272 #define SMALL 32 /* Must be 32 == 0x20 */
273 #define SPECIAL 64 /* 0x */
275 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
276 {
277 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
278 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
280 char tmp[66];
281 char sign;
282 char locase;
283 int need_pfx = ((type & SPECIAL) && base != 10);
284 int i;
286 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
287 * produces same digits or (maybe lowercased) letters */
288 locase = (type & SMALL);
289 if (type & LEFT)
290 type &= ~ZEROPAD;
291 sign = 0;
292 if (type & SIGN) {
293 if ((signed long long) num < 0) {
294 sign = '-';
295 num = - (signed long long) num;
296 size--;
297 } else if (type & PLUS) {
298 sign = '+';
299 size--;
300 } else if (type & SPACE) {
301 sign = ' ';
302 size--;
303 }
304 }
305 if (need_pfx) {
306 size--;
307 if (base == 16)
308 size--;
309 }
311 /* generate full string in tmp[], in reverse order */
312 i = 0;
313 if (num == 0)
314 tmp[i++] = '0';
315 /* Generic code, for any base:
316 else do {
317 tmp[i++] = (digits[do_div(num,base)] | locase);
318 } while (num != 0);
319 */
320 else if (base != 10) { /* 8 or 16 */
321 int mask = base - 1;
322 int shift = 3;
323 if (base == 16) shift = 4;
324 do {
325 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
326 num >>= shift;
327 } while (num);
328 } else { /* base 10 */
329 i = put_dec(tmp, num) - tmp;
330 }
332 /* printing 100 using %2d gives "100", not "00" */
333 if (i > precision)
334 precision = i;
335 /* leading space padding */
336 size -= precision;
337 if (!(type & (ZEROPAD+LEFT))) {
338 while(--size >= 0) {
339 if (buf < end)
340 *buf = ' ';
341 ++buf;
342 }
343 }
344 /* sign */
345 if (sign) {
346 if (buf < end)
347 *buf = sign;
348 ++buf;
349 }
350 /* "0x" / "0" prefix */
351 if (need_pfx) {
352 if (buf < end)
353 *buf = '0';
354 ++buf;
355 if (base == 16) {
356 if (buf < end)
357 *buf = ('X' | locase);
358 ++buf;
359 }
360 }
361 /* zero or space padding */
362 if (!(type & LEFT)) {
363 char c = (type & ZEROPAD) ? '0' : ' ';
364 while (--size >= 0) {
365 if (buf < end)
366 *buf = c;
367 ++buf;
368 }
369 }
370 /* hmm even more zero padding? */
371 while (i <= --precision) {
372 if (buf < end)
373 *buf = '0';
374 ++buf;
375 }
376 /* actual digits of result */
377 while (--i >= 0) {
378 if (buf < end)
379 *buf = tmp[i];
380 ++buf;
381 }
382 /* trailing space padding */
383 while (--size >= 0) {
384 if (buf < end)
385 *buf = ' ';
386 ++buf;
387 }
388 return buf;
389 }
391 /**
392 * vsnprintf - Format a string and place it in a buffer
393 * @buf: The buffer to place the result into
394 * @size: The size of the buffer, including the trailing null space
395 * @fmt: The format string to use
396 * @args: Arguments for the format string
397 *
398 * The return value is the number of characters which would
399 * be generated for the given input, excluding the trailing
400 * '\0', as per ISO C99. If you want to have the exact
401 * number of characters written into @buf as return value
402 * (not including the trailing '\0'), use vscnprintf(). If the
403 * return is greater than or equal to @size, the resulting
404 * string is truncated.
405 *
406 * Call this function if you are already dealing with a va_list.
407 * You probably want snprintf() instead.
408 */
409 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
410 {
411 int len;
412 unsigned long long num;
413 int i, base;
414 char *str, *end, c;
415 const char *s;
417 int flags; /* flags to number() */
419 int field_width; /* width of output field */
420 int precision; /* min. # of digits for integers; max
421 number of chars for from string */
422 int qualifier; /* 'h', 'l', or 'L' for integer fields */
423 /* 'z' support added 23/7/1999 S.H. */
424 /* 'z' changed to 'Z' --davidm 1/25/99 */
425 /* 't' added for ptrdiff_t */
427 /* Reject out-of-range values early. Large positive sizes are
428 used for unknown buffer sizes. */
429 if ((int) size < 0)
430 return 0;
432 str = buf;
433 end = buf + size;
435 /* Make sure end is always >= buf */
436 if (end < buf) {
437 end = ((void *)-1);
438 size = end - buf;
439 }
441 for (; *fmt ; ++fmt) {
442 if (*fmt != '%') {
443 if (str < end)
444 *str = *fmt;
445 ++str;
446 continue;
447 }
449 /* process flags */
450 flags = 0;
451 repeat:
452 ++fmt; /* this also skips first '%' */
453 switch (*fmt) {
454 case '-': flags |= LEFT; goto repeat;
455 case '+': flags |= PLUS; goto repeat;
456 case ' ': flags |= SPACE; goto repeat;
457 case '#': flags |= SPECIAL; goto repeat;
458 case '0': flags |= ZEROPAD; goto repeat;
459 }
461 /* get field width */
462 field_width = -1;
463 if (isdigit(*fmt))
464 field_width = skip_atoi(&fmt);
465 else if (*fmt == '*') {
466 ++fmt;
467 /* it's the next argument */
468 field_width = va_arg(args, int);
469 if (field_width < 0) {
470 field_width = -field_width;
471 flags |= LEFT;
472 }
473 }
475 /* get the precision */
476 precision = -1;
477 if (*fmt == '.') {
478 ++fmt;
479 if (isdigit(*fmt))
480 precision = skip_atoi(&fmt);
481 else if (*fmt == '*') {
482 ++fmt;
483 /* it's the next argument */
484 precision = va_arg(args, int);
485 }
486 if (precision < 0)
487 precision = 0;
488 }
490 /* get the conversion qualifier */
491 qualifier = -1;
492 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
493 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
494 qualifier = *fmt;
495 ++fmt;
496 if (qualifier == 'l' && *fmt == 'l') {
497 qualifier = 'L';
498 ++fmt;
499 }
500 }
502 /* default base */
503 base = 10;
505 switch (*fmt) {
506 case 'c':
507 if (!(flags & LEFT)) {
508 while (--field_width > 0) {
509 if (str < end)
510 *str = ' ';
511 ++str;
512 }
513 }
514 c = (unsigned char) va_arg(args, int);
515 if (str < end)
516 *str = c;
517 ++str;
518 while (--field_width > 0) {
519 if (str < end)
520 *str = ' ';
521 ++str;
522 }
523 continue;
525 case 's':
526 s = va_arg(args, char *);
527 if ((unsigned long)s < PAGE_SIZE)
528 s = "<NULL>";
530 len = strnlen(s, precision);
532 if (!(flags & LEFT)) {
533 while (len < field_width--) {
534 if (str < end)
535 *str = ' ';
536 ++str;
537 }
538 }
539 for (i = 0; i < len; ++i) {
540 if (str < end)
541 *str = *s;
542 ++str; ++s;
543 }
544 while (len < field_width--) {
545 if (str < end)
546 *str = ' ';
547 ++str;
548 }
549 continue;
551 case 'p':
552 flags |= SMALL;
553 if (field_width == -1) {
554 field_width = 2*sizeof(void *);
555 flags |= ZEROPAD;
556 }
557 str = number(str, end,
558 (unsigned long) va_arg(args, void *),
559 16, field_width, precision, flags);
560 continue;
563 case 'n':
564 /* FIXME:
565 * What does C99 say about the overflow case here? */
566 if (qualifier == 'l') {
567 long * ip = va_arg(args, long *);
568 *ip = (str - buf);
569 } else if (qualifier == 'Z' || qualifier == 'z') {
570 size_t * ip = va_arg(args, size_t *);
571 *ip = (str - buf);
572 } else {
573 int * ip = va_arg(args, int *);
574 *ip = (str - buf);
575 }
576 continue;
578 case '%':
579 if (str < end)
580 *str = '%';
581 ++str;
582 continue;
584 /* integer number formats - set up the flags and "break" */
585 case 'o':
586 base = 8;
587 break;
589 case 'x':
590 flags |= SMALL;
591 case 'X':
592 base = 16;
593 break;
595 case 'd':
596 case 'i':
597 flags |= SIGN;
598 case 'u':
599 break;
601 default:
602 if (str < end)
603 *str = '%';
604 ++str;
605 if (*fmt) {
606 if (str < end)
607 *str = *fmt;
608 ++str;
609 } else {
610 --fmt;
611 }
612 continue;
613 }
614 if (qualifier == 'L')
615 num = va_arg(args, long long);
616 else if (qualifier == 'l') {
617 num = va_arg(args, unsigned long);
618 if (flags & SIGN)
619 num = (signed long) num;
620 } else if (qualifier == 'Z' || qualifier == 'z') {
621 num = va_arg(args, size_t);
622 } else if (qualifier == 't') {
623 num = va_arg(args, ptrdiff_t);
624 } else if (qualifier == 'h') {
625 num = (unsigned short) va_arg(args, int);
626 if (flags & SIGN)
627 num = (signed short) num;
628 } else {
629 num = va_arg(args, unsigned int);
630 if (flags & SIGN)
631 num = (signed int) num;
632 }
633 str = number(str, end, num, base,
634 field_width, precision, flags);
635 }
636 if (size > 0) {
637 if (str < end)
638 *str = '\0';
639 else
640 end[-1] = '\0';
641 }
642 /* the trailing null byte doesn't count towards the total */
643 return str-buf;
644 }
645 /* ----- END Other people's code --------- */