Changeset 11631
- Timestamp:
- 09/23/20 20:30:50 (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/oscam-string.c
r11613 r11631 231 231 { 232 232 int32_t i = 0; 233 if(target == NULL || buf == NULL) 234 { 235 return NULL; 236 } 233 237 target[0] = '\0'; 234 238 m = m ? 3 : 2; 235 239 236 240 if(m * n >= len) 237 { n = (len / m) - 1; } 241 { 242 n = (len / m) - 1; 243 } 238 244 239 245 while(i < n) … … 243 249 } 244 250 return target; 251 } 252 253 /* 254 * For using gethexval we must check if char c is within range othervise gethexval return 255 * negative value so we must ensure we not shift left those negative value! So before 256 * using gethexval function you need to check char c with function gethexval_within_range to 257 * ensure char c is within accepted range! 258 */ 259 bool gethexval_within_range(char c) 260 { 261 if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 262 { 263 return true; 264 } 265 return false; 245 266 } 246 267 … … 256 277 { 257 278 int32_t i, rc; 279 if(buf == NULL || asc == NULL) 280 { 281 return -1; 282 } 258 283 for(i = 0; i < n; i++) 259 284 { 285 if(!gethexval_within_range(asc[i << 1]) || !gethexval_within_range(asc[(i << 1) + 1])) 286 { 287 return -1; 288 } 260 289 rc = (gethexval(asc[i << 1]) << 4) | gethexval(asc[(i << 1) + 1]); 261 290 if(rc & 0x100) 262 { return -1; } 291 { 292 return -1; 293 } 263 294 buf[i] = rc; 264 295 } … … 270 301 int32_t i, n = 0; 271 302 uint32_t rc = 0; 303 if(asc == NULL) 304 { 305 errno = EINVAL; 306 rc = val_on_err ? 0xFFFFFFFF : 0; 307 return rc; 308 } 272 309 for(i = ((l - 1) << 1), errno = 0; i >= 0 && n < 4; i -= 2) 273 310 { 274 int32_t b = (gethexval(asc[i]) << 4) | gethexval(asc[i + 1]); 275 if(b < 0) 311 if(!gethexval_within_range(asc[i]) || !gethexval_within_range(asc[i + 1])) 276 312 { 277 313 errno = EINVAL; … … 279 315 break; 280 316 } 317 int32_t b = (gethexval(asc[i]) << 4) | gethexval(asc[i + 1]); 318 if(b < 0) 319 { 320 errno = EINVAL; 321 rc = val_on_err ? 0xFFFFFFFF : 0; 322 break; 323 } 281 324 rc |= b << (n << 3); 282 325 n++; … … 287 330 int32_t byte_atob(char *asc) 288 331 { 289 int32_t rc; 332 int32_t rc = (-1); 333 if(asc == NULL) 334 { 335 return rc; 336 } 290 337 if(strlen(trim(asc)) != 2) 291 338 { … … 294 341 else 295 342 { 296 rc = (gethexval(asc[0]) << 4) | gethexval(asc[1]); 297 if(rc & 0x100) 298 { rc = -1; } 343 if(!gethexval_within_range(asc[0]) || !gethexval_within_range(asc[1])) 344 { 345 return rc; 346 } 347 else 348 { 349 rc = (gethexval(asc[0]) << 4) | gethexval(asc[1]); 350 if(rc & 0x100) 351 { 352 rc = -1; 353 } 354 } 299 355 } 300 356 return rc; … … 303 359 int32_t word_atob(char *asc) 304 360 { 305 int32_t rc; 361 int32_t rc = (-1); 362 363 if(asc == NULL) 364 { 365 return rc; 366 } 367 306 368 if(strlen(trim(asc)) != 4) 307 369 { … … 310 372 else 311 373 { 312 rc = gethexval(asc[0]) << 12 | gethexval(asc[1]) << 8 | 313 gethexval(asc[2]) << 4 | gethexval(asc[3]); 314 if(rc & 0x10000) 315 { rc = -1; } 374 if(!gethexval_within_range(asc[0]) || !gethexval_within_range(asc[1]) || !gethexval_within_range(asc[2]) || !gethexval_within_range(asc[3])) 375 { 376 return rc; 377 } 378 else 379 { 380 rc = gethexval(asc[0]) << 12 | gethexval(asc[1]) << 8 | gethexval(asc[2]) << 4 | gethexval(asc[3]); 381 if(rc & 0x10000) 382 { 383 rc = -1; 384 } 385 } 316 386 } 317 387 return rc; … … 325 395 { 326 396 int32_t rc = (-1); 327 int32_t i, len = strlen(trim(asc)); 328 397 int32_t i, len; 398 399 if(asc == NULL) 400 { 401 return rc; 402 } 403 len = strlen(trim(asc)); 329 404 if(len <= 6 && len > 0) 330 405 { 331 406 for(i = 0, rc = 0; i < len; i++) 332 407 { 408 if(!gethexval_within_range(asc[0]) || !gethexval_within_range(asc[1]) || !gethexval_within_range(asc[2]) || !gethexval_within_range(asc[3])) 409 { 410 return -1; 411 } 333 412 rc = rc << 4 | gethexval(asc[i]); 334 413 } 335 336 414 if(rc & 0x1000000) 337 { rc = -1; } 415 { 416 rc = -1; 417 } 338 418 } 339 419 return rc; … … 344 424 int32_t i, n1, n2, rc; 345 425 426 if(asc == NULL || bin == NULL) 427 { 428 return -1; 429 } 346 430 for(i = rc = 0; i < l; i += 2) 347 431 { 348 if((n1 = gethexval(asc[i])) < 0) { rc = -1; } 349 if((n2 = gethexval(asc[i + 1])) < 0) { rc = -1; } 350 bin[i >> 1] = (n1 << 4) + (n2 & 0xff); 432 if(!gethexval_within_range(asc[i]) || !gethexval_within_range(asc[i + 1])) 433 { 434 rc = -1; 435 } 436 else 437 { 438 n1 = gethexval(asc[i]); 439 n2 = gethexval(asc[i + 1]); 440 bin[i >> 1] = (n1 << 4) + (n2 & 0xff); 441 } 351 442 } 352 443 return rc; … … 355 446 uint32_t b2i(int32_t n, const uint8_t *b) 356 447 { 448 if(b == NULL) 449 { 450 return 0; 451 } 357 452 switch(n) 358 453 { … … 378 473 int32_t i; 379 474 uint64_t k = 0; 475 if(b == NULL) 476 { 477 return k; 478 } 380 479 for(i = 0; i < n; k += b[i++]) 381 { k <<= 8; } 480 { 481 k <<= 8; 482 } 382 483 return k; 383 484 } … … 425 526 int32_t i, n; 426 527 uint32_t rc; 427 528 if(asc == NULL) 529 { 530 errno = EINVAL; 531 return 0x1f1f1f; 532 } 428 533 for(rc = i = 0, n = strlen(trim(asc)) - 1; i < abs(bytes) << 1; n--, i++) 429 534 { … … 431 536 { 432 537 int32_t rcl; 433 if( (rcl = gethexval(asc[n])) < 0)538 if(!gethexval_within_range(asc[n])) 434 539 { 435 540 errno = EINVAL; 436 541 return 0x1f1f1f; 437 542 } 438 rc |= rcl << (i << 2); 543 else 544 { 545 rcl = gethexval(asc[n]); 546 rc |= rcl << (i << 2); 547 } 439 548 } 440 549 else 441 550 { 442 551 if(bytes < 0) 443 { rc |= 0xf << (i << 2); } 552 { 553 rc |= 0xf << (i << 2); 554 } 444 555 } 445 556 } … … 458 569 { 459 570 if(!value) 460 { return 0; } 571 { 572 return 0; 573 } 461 574 462 575 int32_t i; … … 485 598 fd = open("/dev/random", O_RDONLY); 486 599 if(fd < 0) 487 { return; } 600 { 601 return; 602 } 488 603 } 489 604 … … 577 692 { 578 693 if(!buf) 579 { return 0L; } 694 { 695 return 0L; 696 } 580 697 crc = crc ^ 0xffffffffL; 581 698 while(len >= 8) … … 774 891 } 775 892 if(!cs_malloc(out, outlen)) 776 { return -1; } 893 { 894 return -1; 895 } 777 896 base64_encode(in, inlen, *out, outlen); 778 897 return outlen - 1; … … 803 922 int32_t i, len = strlen((char *)result), j = 0, bits = 0, char_count = 0; 804 923 805 if(!b64decoder[0]) { b64prepare(); } 924 if(!b64decoder[0]) 925 { 926 b64prepare(); 927 } 806 928 807 929 for(i = 0; i < len; ++i) … … 837 959 switch(char_count) 838 960 { 839 case 1:840 result[j] = '\0';841 return 0;842 case 2:843 result[j++] = bits >> 10;844 result[j] = '\0';845 break;846 case 3:847 result[j++] = bits >> 16;848 result[j++] = (bits >> 8) & 0xff;849 result[j] = '\0';850 break;961 case 1: 962 result[j] = '\0'; 963 return 0; 964 case 2: 965 result[j++] = bits >> 10; 966 result[j] = '\0'; 967 break; 968 case 3: 969 result[j++] = bits >> 16; 970 result[j++] = (bits >> 8) & 0xff; 971 result[j] = '\0'; 972 break; 851 973 } 852 974 } … … 1120 1242 *inbytesleft = iend - iptr; 1121 1243 *outbytesleft = oend - optr; 1122 if( err)1244 if(err) 1123 1245 { 1124 1246 errno = err; -
trunk/oscam-string.h
r11612 r11631 16 16 17 17 char *cs_hexdump(int32_t m, const uint8_t *buf, int32_t n, char *target, int32_t len); 18 bool gethexval_within_range(char c); 18 19 int32_t gethexval(char c); 19 20 int32_t cs_atob(uint8_t *buf, char *asc, int32_t n);
Note:
See TracChangeset
for help on using the changeset viewer.