35
35
#include "cutils.h"
36
36
37
37
#undef NANOSEC
38
- #define NANOSEC ((uint64_t) 1e9)
38
+ #define NANOSEC 1000000000
39
39
40
40
#pragma GCC visibility push(default)
41
41
42
- void pstrcpy (char * buf , int buf_size , const char * str )
42
+ void pstrcpy (char * buf , size_t buf_size , const char * str )
43
43
{
44
- int c ;
45
44
char * q = buf ;
46
45
47
46
if (buf_size <= 0 )
48
47
return ;
49
48
50
49
for (;;) {
51
- c = * str ++ ;
50
+ char c = * str ++ ;
52
51
if (c == 0 || q >= buf + buf_size - 1 )
53
52
break ;
54
53
* q ++ = c ;
@@ -57,10 +56,9 @@ void pstrcpy(char *buf, int buf_size, const char *str)
57
56
}
58
57
59
58
/* strcat and truncate. */
60
- char * pstrcat (char * buf , int buf_size , const char * s )
59
+ char * pstrcat (char * buf , size_t buf_size , const char * s )
61
60
{
62
- int len ;
63
- len = strlen (buf );
61
+ size_t len = strlen (buf );
64
62
if (len < buf_size )
65
63
pstrcpy (buf + len , buf_size - len , s );
66
64
return buf ;
@@ -189,15 +187,16 @@ static int dbuf_vprintf_default(DynBuf *s, const char *fmt, va_list ap)
189
187
va_end (arg );
190
188
191
189
if (len >= 0 ) {
192
- if ((size_t )len >= avail ) {
193
- if (dbuf_realloc (s , s -> size + len + 1 ))
190
+ size_t ulen = (size_t )len ;
191
+ if (ulen >= avail ) {
192
+ if (dbuf_realloc (s , s -> size + ulen + 1 ))
194
193
return -1 ;
195
194
avail = s -> allocated_size - s -> size ;
196
195
va_copy (arg , ap );
197
196
vsnprintf ((char * )(s -> buf + s -> size ), avail , fmt , arg );
198
197
va_end (arg );
199
198
}
200
- s -> size += len ;
199
+ s -> size += ulen ;
201
200
}
202
201
return len ;
203
202
}
@@ -231,42 +230,55 @@ void dbuf_free(DynBuf *s)
231
230
232
231
/* Note: at most 31 bits are encoded. At most UTF8_CHAR_LEN_MAX bytes
233
232
are output. */
234
- int unicode_to_utf8 (uint8_t * buf , unsigned int c )
233
+ size_t unicode_to_utf8 (uint8_t * buf , unsigned int c )
235
234
{
236
- uint8_t * q = buf ;
237
-
238
235
if (c < 0x80 ) {
239
- * q ++ = c ;
240
- } else {
241
- if (c < 0x800 ) {
242
- * q ++ = (c >> 6 ) | 0xc0 ;
243
- } else {
244
- if (c < 0x10000 ) {
245
- * q ++ = (c >> 12 ) | 0xe0 ;
246
- } else {
247
- if (c < 0x00200000 ) {
248
- * q ++ = (c >> 18 ) | 0xf0 ;
249
- } else {
250
- if (c < 0x04000000 ) {
251
- * q ++ = (c >> 24 ) | 0xf8 ;
252
- } else if (c < 0x80000000 ) {
253
- * q ++ = (c >> 30 ) | 0xfc ;
254
- * q ++ = ((c >> 24 ) & 0x3f ) | 0x80 ;
255
- } else {
256
- return 0 ;
257
- }
258
- * q ++ = ((c >> 18 ) & 0x3f ) | 0x80 ;
259
- }
260
- * q ++ = ((c >> 12 ) & 0x3f ) | 0x80 ;
261
- }
262
- * q ++ = ((c >> 6 ) & 0x3f ) | 0x80 ;
263
- }
264
- * q ++ = (c & 0x3f ) | 0x80 ;
236
+ buf [0 ] = (uint8_t )c ;
237
+ return 1 ;
238
+ }
239
+ if (c < 0x800 ) {
240
+ buf [0 ] = (uint8_t )((c >> 6 ) | 0xc0 );
241
+ buf [1 ] = (uint8_t )((c & 0x3f ) | 0x80 );
242
+ return 2 ;
265
243
}
266
- return q - buf ;
244
+ if (c < 0x10000 ) {
245
+ buf [0 ] = (uint8_t )((c >> 12 ) | 0xe0 );
246
+ buf [1 ] = (uint8_t )(((c >> 6 ) & 0x3f ) | 0x80 );
247
+ buf [2 ] = (uint8_t )((c & 0x3f ) | 0x80 );
248
+ return 3 ;
249
+ }
250
+ if (c < 0x00200000 ) {
251
+ buf [0 ] = (uint8_t )((c >> 18 ) | 0xf0 );
252
+ buf [1 ] = (uint8_t )(((c >> 12 ) & 0x3f ) | 0x80 );
253
+ buf [2 ] = (uint8_t )(((c >> 6 ) & 0x3f ) | 0x80 );
254
+ buf [3 ] = (uint8_t )((c & 0x3f ) | 0x80 );
255
+ return 4 ;
256
+ }
257
+ #if 0
258
+ if (c < 0x04000000 ) {
259
+ buf [0 ] = (uint8_t )((c >> 24 ) | 0xf8 );
260
+ buf [1 ] = (uint8_t )(((c >> 18 ) & 0x3f ) | 0x80 );
261
+ buf [2 ] = (uint8_t )(((c >> 12 ) & 0x3f ) | 0x80 );
262
+ buf [3 ] = (uint8_t )(((c >> 6 ) & 0x3f ) | 0x80 );
263
+ buf [4 ] = (uint8_t )((c & 0x3f ) | 0x80 );
264
+ return 5 ;
265
+ }
266
+ buf [0 ] = (uint8_t )((c >> 30 ) | 0xfc );
267
+ buf [1 ] = (uint8_t )(((c >> 24 ) & 0x3f ) | 0x80 );
268
+ buf [2 ] = (uint8_t )(((c >> 18 ) & 0x3f ) | 0x80 );
269
+ buf [3 ] = (uint8_t )(((c >> 12 ) & 0x3f ) | 0x80 );
270
+ buf [4 ] = (uint8_t )(((c >> 6 ) & 0x3f ) | 0x80 );
271
+ buf [5 ] = (uint8_t )((c & 0x3f ) | 0x80 );
272
+ return 6 ;
273
+ #else
274
+ buf [0 ] = (uint8_t )((0xFFFD >> 12 ) | 0xe0 );
275
+ buf [1 ] = (uint8_t )(((0xFFFD >> 6 ) & 0x3f ) | 0x80 );
276
+ buf [2 ] = (uint8_t )((0xFFFD & 0x3f ) | 0x80 );
277
+ return 3 ;
278
+ #endif
267
279
}
268
280
269
- static const unsigned int utf8_min_code [5 ] = {
281
+ static const int utf8_min_code [5 ] = {
270
282
0x80 , 0x800 , 0x10000 , 0x00200000 , 0x04000000 ,
271
283
};
272
284
@@ -342,6 +354,7 @@ int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp)
342
354
343
355
/* 2 <= base <= 36 */
344
356
char const digits36 [36 ] = "0123456789abcdefghijklmnopqrstuvwxyz" ;
357
+ char const digits36_upper [36 ] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
345
358
346
359
/* using u32toa_shift variant */
347
360
@@ -350,7 +363,7 @@ char const digits36[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
350
363
else \
351
364
buf = (buf << 8) | (c)
352
365
353
- size_t u7toa_shift (char dest [minimum_length (8 )], uint32_t n )
366
+ static size_t u7toa_shift (char dest [minimum_length (8 )], uint32_t n )
354
367
{
355
368
size_t len = 1 ;
356
369
uint64_t buf = 0 ;
@@ -365,7 +378,7 @@ size_t u7toa_shift(char dest[minimum_length(8)], uint32_t n)
365
378
return len ;
366
379
}
367
380
368
- size_t u07toa_shift (char dest [minimum_length (8 )], uint32_t n , size_t len )
381
+ static size_t u07toa_shift (char dest [minimum_length (8 )], uint32_t n , size_t len )
369
382
{
370
383
size_t i ;
371
384
dest += len ;
@@ -389,39 +402,41 @@ size_t u32toa(char buf[minimum_length(11)], uint32_t n)
389
402
#define TEN_POW_7 10000000
390
403
if (n >= TEN_POW_7 ) {
391
404
uint32_t quo = n / TEN_POW_7 ;
405
+ size_t len ;
392
406
n %= TEN_POW_7 ;
393
- size_t len = u7toa_shift (buf , quo );
407
+ len = u7toa_shift (buf , quo );
394
408
return u07toa_shift (buf , n , len );
395
409
}
396
410
return u7toa_shift (buf , n );
397
411
}
398
412
399
413
size_t u64toa (char buf [minimum_length (21 )], uint64_t n )
400
414
{
415
+ size_t len ;
416
+
401
417
if (likely (n < 0x100000000 ))
402
- return u32toa (buf , n );
418
+ return u32toa (buf , ( uint32_t ) n );
403
419
404
- size_t len ;
405
420
if (n >= TEN_POW_7 ) {
406
421
uint64_t n1 = n / TEN_POW_7 ;
407
422
n %= TEN_POW_7 ;
408
423
if (n1 >= TEN_POW_7 ) {
409
- uint32_t quo = n1 / TEN_POW_7 ;
424
+ uint32_t quo = ( uint32_t )( n1 / TEN_POW_7 ) ;
410
425
n1 %= TEN_POW_7 ;
411
426
len = u7toa_shift (buf , quo );
412
- len = u07toa_shift (buf , n1 , len );
427
+ len = u07toa_shift (buf , ( uint32_t ) n1 , len );
413
428
} else {
414
- len = u7toa_shift (buf , n1 );
429
+ len = u7toa_shift (buf , ( uint32_t ) n1 );
415
430
}
416
- return u07toa_shift (buf , n , len );
431
+ return u07toa_shift (buf , ( uint32_t ) n , len );
417
432
}
418
- return u7toa_shift (buf , n );
433
+ return u7toa_shift (buf , ( uint32_t ) n );
419
434
}
420
435
421
436
size_t i32toa (char buf [minimum_length (12 )], int32_t n )
422
437
{
423
438
if (likely (n >= 0 ))
424
- return u32toa (buf , n );
439
+ return u32toa (buf , ( uint32_t ) n );
425
440
426
441
buf [0 ] = '-' ;
427
442
return 1 + u32toa (buf + 1 , - (uint32_t )n );
@@ -430,7 +445,7 @@ size_t i32toa(char buf[minimum_length(12)], int32_t n)
430
445
size_t i64toa (char buf [minimum_length (22 )], int64_t n )
431
446
{
432
447
if (likely (n >= 0 ))
433
- return u64toa (buf , n );
448
+ return u64toa (buf , ( uint64_t ) n );
434
449
435
450
buf [0 ] = '-' ;
436
451
return 1 + u64toa (buf + 1 , - (uint64_t )n );
@@ -447,6 +462,9 @@ static uint8_t const radix_shift[64] = {
447
462
448
463
size_t u32toa_radix (char buf [minimum_length (33 )], uint32_t n , unsigned base )
449
464
{
465
+ int shift ;
466
+ char * end ;
467
+
450
468
#ifdef USE_SPECIAL_RADIX_10
451
469
if (likely (base == 10 ))
452
470
return u32toa (buf , n );
@@ -456,13 +474,13 @@ size_t u32toa_radix(char buf[minimum_length(33)], uint32_t n, unsigned base)
456
474
buf [1 ] = '\0' ;
457
475
return 1 ;
458
476
}
459
- int shift = radix_shift [base & 63 ];
477
+ shift = radix_shift [base & 63 ];
460
478
if (shift ) {
461
479
uint32_t mask = (1 << shift ) - 1 ;
462
- size_t len = (32 - clz32 (n ) + shift - 1 ) / shift ;
480
+ size_t len = (size_t )(( 32 - clz32 (n ) + shift - 1 ) / shift ) ;
463
481
size_t last = n & mask ;
464
- n /= base ;
465
- char * end = buf + len ;
482
+ end = buf + len ;
483
+ n >>= shift ;
466
484
* end -- = '\0' ;
467
485
* end -- = digits36 [last ];
468
486
while (n >= base ) {
@@ -474,14 +492,14 @@ size_t u32toa_radix(char buf[minimum_length(33)], uint32_t n, unsigned base)
474
492
return len ;
475
493
} else {
476
494
size_t len = 2 ;
495
+ uint32_t nbase = base ;
477
496
size_t last = n % base ;
478
497
n /= base ;
479
- uint32_t nbase = base ;
480
498
while (n >= nbase ) {
481
499
nbase *= base ;
482
500
len ++ ;
483
501
}
484
- char * end = buf + len ;
502
+ end = buf + len ;
485
503
* end -- = '\0' ;
486
504
* end -- = digits36 [last ];
487
505
while (n >= base ) {
@@ -496,22 +514,24 @@ size_t u32toa_radix(char buf[minimum_length(33)], uint32_t n, unsigned base)
496
514
497
515
size_t u64toa_radix (char buf [minimum_length (65 )], uint64_t n , unsigned base )
498
516
{
499
- #ifdef USE_SPECIAL_RADIX_10
500
- if (likely (base == 10 ))
517
+ int shift ;
518
+ char * end ;
519
+
520
+ if (n < base ) {
521
+ buf [0 ] = digits36 [n ];
522
+ buf [1 ] = '\0' ;
523
+ return 1 ;
524
+ }
525
+ if (likely (base == 10 )) {
501
526
return u64toa (buf , n );
502
- #endif
503
- int shift = radix_shift [base & 63 ];
527
+ }
528
+ shift = radix_shift [base & 63 ];
504
529
if (shift ) {
505
- if (n < base ) {
506
- buf [0 ] = digits36 [n ];
507
- buf [1 ] = '\0' ;
508
- return 1 ;
509
- }
510
530
uint64_t mask = (1 << shift ) - 1 ;
511
- size_t len = (64 - clz64 (n ) + shift - 1 ) / shift ;
531
+ size_t len = (size_t )(( 64 - clz64 (n ) + shift - 1 ) / shift ) ;
512
532
size_t last = n & mask ;
513
- n /= base ;
514
- char * end = buf + len ;
533
+ end = buf + len ;
534
+ n >>= shift ;
515
535
* end -- = '\0' ;
516
536
* end -- = digits36 [last ];
517
537
while (n >= base ) {
@@ -521,18 +541,19 @@ size_t u64toa_radix(char buf[minimum_length(65)], uint64_t n, unsigned base)
521
541
}
522
542
* end = digits36 [n ];
523
543
return len ;
544
+ } else
545
+ if (likely (n < 0x100000000 )) {
546
+ return u32toa_radix (buf , (uint32_t )n , base );
524
547
} else {
525
- if (likely (n < 0x100000000 ))
526
- return u32toa_radix (buf , n , base );
527
548
size_t last = n % base ;
528
- n /= base ;
529
549
uint64_t nbase = base ;
530
550
size_t len = 2 ;
551
+ n /= base ;
531
552
while (n >= nbase ) {
532
553
nbase *= base ;
533
554
len ++ ;
534
555
}
535
- char * end = buf + len ;
556
+ end = buf + len ;
536
557
* end -- = '\0' ;
537
558
* end -- = digits36 [last ];
538
559
while (n >= base ) {
@@ -548,7 +569,7 @@ size_t u64toa_radix(char buf[minimum_length(65)], uint64_t n, unsigned base)
548
569
size_t i64toa_radix (char buf [minimum_length (66 )], int64_t n , unsigned int base )
549
570
{
550
571
if (likely (n >= 0 ))
551
- return u64toa_radix (buf , n , base );
572
+ return u64toa_radix (buf , ( uint64_t ) n , base );
552
573
553
574
buf [0 ] = '-' ;
554
575
return 1 + u64toa_radix (buf + 1 , - (uint64_t )n , base );
@@ -738,7 +759,14 @@ static inline void *med3(void *a, void *b, void *c, cmp_f cmp, void *opaque)
738
759
/* pointer based version with local stack and insertion sort threshhold */
739
760
void rqsort (void * base , size_t nmemb , size_t size , cmp_f cmp , void * opaque )
740
761
{
741
- struct { uint8_t * base ; size_t count ; int depth ; } stack [50 ], * sp = stack ;
762
+ struct {
763
+ uint8_t * base ;
764
+ size_t count ;
765
+ int depth ;
766
+ #if SIZE_MAX > UINT_MAX
767
+ int pad ;
768
+ #endif
769
+ } stack [50 ], * sp = stack ;
742
770
uint8_t * ptr , * pi , * pj , * plt , * pgt , * top , * m ;
743
771
size_t m4 , i , lt , gt , span , span2 ;
744
772
int c , depth ;
@@ -1121,12 +1149,9 @@ int js_cond_timedwait(js_cond_t *cond, js_mutex_t *mutex, uint64_t timeout) {
1121
1149
if (r == 0 )
1122
1150
return 0 ;
1123
1151
1124
- if (r == ETIMEDOUT )
1125
- return -1 ;
1126
-
1127
- abort ();
1152
+ if (r != ETIMEDOUT )
1153
+ abort ();
1128
1154
1129
- /* Pacify some compilers. */
1130
1155
return -1 ;
1131
1156
}
1132
1157
0 commit comments