Changeset 5261 for pjproject/trunk/third_party/srtp/crypto/math/datatypes.c
- Timestamp:
- Mar 15, 2016 3:57:39 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/third_party/srtp/crypto/math/datatypes.c
r5225 r5261 44 44 */ 45 45 46 #ifdef HAVE_CONFIG_H 47 #include <config.h> 48 #endif 49 46 50 #include "datatypes.h" 47 51 … … 114 118 115 119 /* truncate string if it would be too long */ 116 if (length > = MAX_PRINT_STRING_LEN-1)117 length = MAX_PRINT_STRING_LEN- 2;120 if (length > MAX_PRINT_STRING_LEN) 121 length = MAX_PRINT_STRING_LEN-1; 118 122 119 123 for (i=0; i < length; i+=2) { … … 150 154 case ('f'): return 0xf; 151 155 case ('F'): return 0xf; 152 default: break; /* this flags an error */ 153 } 154 return -1; 156 default: return -1; /* this flags an error */ 157 } 158 /* NOTREACHED */ 159 return -1; /* this keeps compilers from complaining */ 155 160 } 156 161 … … 207 212 char * 208 213 v128_bit_string(v128_t *x) { 209 int j, i ndex;214 int j, i; 210 215 uint32_t mask; 211 216 212 for (j=i ndex=0; j < 4; j++) {217 for (j=i=0; j < 4; j++) { 213 218 for (mask=0x80000000; mask > 0; mask >>= 1) { 214 219 if (x->v32[j] & mask) 215 bit_string[i ndex] = '1';220 bit_string[i] = '1'; 216 221 else 217 bit_string[i ndex] = '0';218 ++i ndex;222 bit_string[i] = '0'; 223 ++i; 219 224 } 220 225 } … … 323 328 324 329 void 325 v128_right_shift(v128_t *x, int index) {326 const int base_index = index>> 5;327 const int bit_index = index& 31;330 v128_right_shift(v128_t *x, int shift) { 331 const int base_index = shift >> 5; 332 const int bit_index = shift & 31; 328 333 int i, from; 329 334 uint32_t b; 330 335 331 if ( index> 127) {336 if (shift > 127) { 332 337 v128_set_to_zero(x); 333 338 return; … … 361 366 362 367 void 363 v128_left_shift(v128_t *x, int index) {368 v128_left_shift(v128_t *x, int shift) { 364 369 int i; 365 const int base_index = index>> 5;366 const int bit_index = index& 31;367 368 if ( index> 127) {370 const int base_index = shift >> 5; 371 const int bit_index = shift & 31; 372 373 if (shift > 127) { 369 374 v128_set_to_zero(x); 370 375 return; … … 387 392 } 388 393 394 /* functions manipulating bitvector_t */ 395 396 #ifndef DATATYPES_USE_MACROS /* little functions are not macros */ 397 398 int 399 bitvector_get_bit(const bitvector_t *v, int bit_index) 400 { 401 return _bitvector_get_bit(v, bit_index); 402 } 403 404 void 405 bitvector_set_bit(bitvector_t *v, int bit_index) 406 { 407 _bitvector_set_bit(v, bit_index); 408 } 409 410 void 411 bitvector_clear_bit(bitvector_t *v, int bit_index) 412 { 413 _bitvector_clear_bit(v, bit_index); 414 } 415 416 417 #endif /* DATATYPES_USE_MACROS */ 418 419 int 420 bitvector_alloc(bitvector_t *v, unsigned long length) { 421 unsigned long l; 422 423 /* Round length up to a multiple of bits_per_word */ 424 length = (length + bits_per_word - 1) & ~(unsigned long)((bits_per_word - 1)); 425 426 l = length / bits_per_word * bytes_per_word; 427 428 /* allocate memory, then set parameters */ 429 if (l == 0) 430 v->word = NULL; 431 else { 432 v->word = (uint32_t*)crypto_alloc(l); 433 if (v->word == NULL) { 434 v->word = NULL; 435 v->length = 0; 436 return -1; 437 } 438 } 439 v->length = length; 440 441 /* initialize bitvector to zero */ 442 bitvector_set_to_zero(v); 443 444 return 0; 445 } 446 447 448 void 449 bitvector_dealloc(bitvector_t *v) { 450 if (v->word != NULL) 451 crypto_free(v->word); 452 v->word = NULL; 453 v->length = 0; 454 } 455 456 void 457 bitvector_set_to_zero(bitvector_t *x) 458 { 459 /* C99 guarantees that memset(0) will set the value 0 for uint32_t */ 460 memset(x->word, 0, x->length >> 3); 461 } 462 463 char * 464 bitvector_bit_string(bitvector_t *x, char* buf, int len) { 465 int j, i; 466 uint32_t mask; 467 468 for (j=i=0; j < (int)(x->length>>5) && i < len-1; j++) { 469 for (mask=0x80000000; mask > 0; mask >>= 1) { 470 if (x->word[j] & mask) 471 buf[i] = '1'; 472 else 473 buf[i] = '0'; 474 ++i; 475 if (i >= len-1) 476 break; 477 } 478 } 479 buf[i] = 0; /* null terminate string */ 480 481 return buf; 482 } 483 484 void 485 bitvector_left_shift(bitvector_t *x, int shift) { 486 int i; 487 const int base_index = shift >> 5; 488 const int bit_index = shift & 31; 489 const int word_length = x->length >> 5; 490 491 if (shift >= (int)x->length) { 492 bitvector_set_to_zero(x); 493 return; 494 } 495 496 if (bit_index == 0) { 497 for (i=0; i < word_length - base_index; i++) 498 x->word[i] = x->word[i+base_index]; 499 } else { 500 for (i=0; i < word_length - base_index - 1; i++) 501 x->word[i] = (x->word[i+base_index] >> bit_index) ^ 502 (x->word[i+base_index+1] << (32 - bit_index)); 503 x->word[word_length - base_index-1] = x->word[word_length-1] >> bit_index; 504 } 505 506 /* now wrap up the final portion */ 507 for (i = word_length - base_index; i < word_length; i++) 508 x->word[i] = 0; 509 510 } 511 389 512 390 513 int … … 407 530 } 408 531 409 410 /* 411 * From RFC 1521: The Base64 Alphabet 412 * 413 * Value Encoding Value Encoding Value Encoding Value Encoding 414 * 0 A 17 R 34 i 51 z 415 * 1 B 18 S 35 j 52 0 416 * 2 C 19 T 36 k 53 1 417 * 3 D 20 U 37 l 54 2 418 * 4 E 21 V 38 m 55 3 419 * 5 F 22 W 39 n 56 4 420 * 6 G 23 X 40 o 57 5 421 * 7 H 24 Y 41 p 58 6 422 * 8 I 25 Z 42 q 59 7 423 * 9 J 26 a 43 r 60 8 424 * 10 K 27 b 44 s 61 9 425 * 11 L 28 c 45 t 62 + 426 * 12 M 29 d 46 u 63 / 427 * 13 N 30 e 47 v 428 * 14 O 31 f 48 w (pad) = 429 * 15 P 32 g 49 x 430 * 16 Q 33 h 50 y 431 */ 432 433 int 434 base64_char_to_sextet(uint8_t c) { 435 switch(c) { 436 case 'A': 437 return 0; 438 case 'B': 439 return 1; 440 case 'C': 441 return 2; 442 case 'D': 443 return 3; 444 case 'E': 445 return 4; 446 case 'F': 447 return 5; 448 case 'G': 449 return 6; 450 case 'H': 451 return 7; 452 case 'I': 453 return 8; 454 case 'J': 455 return 9; 456 case 'K': 457 return 10; 458 case 'L': 459 return 11; 460 case 'M': 461 return 12; 462 case 'N': 463 return 13; 464 case 'O': 465 return 14; 466 case 'P': 467 return 15; 468 case 'Q': 469 return 16; 470 case 'R': 471 return 17; 472 case 'S': 473 return 18; 474 case 'T': 475 return 19; 476 case 'U': 477 return 20; 478 case 'V': 479 return 21; 480 case 'W': 481 return 22; 482 case 'X': 483 return 23; 484 case 'Y': 485 return 24; 486 case 'Z': 487 return 25; 488 case 'a': 489 return 26; 490 case 'b': 491 return 27; 492 case 'c': 493 return 28; 494 case 'd': 495 return 29; 496 case 'e': 497 return 30; 498 case 'f': 499 return 31; 500 case 'g': 501 return 32; 502 case 'h': 503 return 33; 504 case 'i': 505 return 34; 506 case 'j': 507 return 35; 508 case 'k': 509 return 36; 510 case 'l': 511 return 37; 512 case 'm': 513 return 38; 514 case 'n': 515 return 39; 516 case 'o': 517 return 40; 518 case 'p': 519 return 41; 520 case 'q': 521 return 42; 522 case 'r': 523 return 43; 524 case 's': 525 return 44; 526 case 't': 527 return 45; 528 case 'u': 529 return 46; 530 case 'v': 531 return 47; 532 case 'w': 533 return 48; 534 case 'x': 535 return 49; 536 case 'y': 537 return 50; 538 case 'z': 539 return 51; 540 case '0': 541 return 52; 542 case '1': 543 return 53; 544 case '2': 545 return 54; 546 case '3': 547 return 55; 548 case '4': 549 return 56; 550 case '5': 551 return 57; 552 case '6': 553 return 58; 554 case '7': 555 return 59; 556 case '8': 557 return 60; 558 case '9': 559 return 61; 560 case '+': 561 return 62; 562 case '/': 563 return 63; 564 case '=': 565 return 64; 566 default: 567 break; 568 } 569 return -1; 570 } 571 572 /* 573 * base64_string_to_octet_string converts a hexadecimal string 574 * of length 2 * len to a raw octet string of length len 575 */ 576 577 int 578 base64_string_to_octet_string(char *raw, char *base64, int len) { 579 uint8_t x; 580 int tmp; 581 int base64_len; 582 583 base64_len = 0; 584 while (base64_len < len) { 585 tmp = base64_char_to_sextet(base64[0]); 586 if (tmp == -1) 587 return base64_len; 588 x = (tmp << 6); 589 base64_len++; 590 tmp = base64_char_to_sextet(base64[1]); 591 if (tmp == -1) 592 return base64_len; 593 x |= (tmp & 0xffff); 594 base64_len++; 595 *raw++ = x; 596 base64 += 2; 597 } 598 return base64_len; 599 } 532 #ifdef TESTAPP_SOURCE 533 534 static const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 535 "abcdefghijklmnopqrstuvwxyz0123456789+/"; 536 537 static int base64_block_to_octet_triple(char *out, char *in) { 538 unsigned char sextets[4] = {0}; 539 int j = 0; 540 int i; 541 542 for (i = 0; i < 4; i++) { 543 char *p = strchr(b64chars, in[i]); 544 if (p != NULL) sextets[i] = p - b64chars; 545 else j++; 546 } 547 548 out[0] = (sextets[0]<<2)|(sextets[1]>>4); 549 if (j < 2) out[1] = (sextets[1]<<4)|(sextets[2]>>2); 550 if (j < 1) out[2] = (sextets[2]<<6)|sextets[3]; 551 return j; 552 } 553 554 int base64_string_to_octet_string(char *out, int *pad, char *in, int len) { 555 int k = 0; 556 int i = 0; 557 int j = 0; 558 if (len % 4 != 0) return 0; 559 560 while (i < len && j == 0) { 561 j = base64_block_to_octet_triple(out + k, in + i); 562 k += 3; 563 i += 4; 564 } 565 *pad = j; 566 return i; 567 } 568 569 #endif
Note: See TracChangeset
for help on using the changeset viewer.