1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
|
/*
* digest.c --
*
* Implements and registers code common to all message digests.
*
*
* Copyright (c) 1996 Andreas Kupries (andreas_kupries@users.sourceforge.net)
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL I LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
* SOFTWARE AND ITS DOCUMENTATION, EVEN IF I HAVE BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* I SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
* I HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*
* CVS: $Id: digest.c,v 1.18 2009/05/07 05:30:35 andreas_kupries Exp $
*/
#include "transformInt.h"
/*
* Declarations of internal procedures.
*/
static Trf_ControlBlock CreateEncoder _ANSI_ARGS_ ((ClientData writeClientData,
Trf_WriteProc *fun,
Trf_Options optInfo,
Tcl_Interp* interp,
ClientData clientData));
static void DeleteEncoder _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
ClientData clientData));
static int Encode _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
unsigned int character,
Tcl_Interp* interp,
ClientData clientData));
static int EncodeBuffer _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
unsigned char* buffer, int bufLen,
Tcl_Interp* interp,
ClientData clientData));
static int FlushEncoder _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
Tcl_Interp* interp,
ClientData clientData));
static void ClearEncoder _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
ClientData clientData));
static Trf_ControlBlock CreateDecoder _ANSI_ARGS_ ((ClientData writeClientData,
Trf_WriteProc *fun,
Trf_Options optInfo,
Tcl_Interp* interp,
ClientData clientData));
static void DeleteDecoder _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
ClientData clientData));
static int Decode _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
unsigned int character,
Tcl_Interp* interp,
ClientData clientData));
static int DecodeBuffer _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
unsigned char* buffer, int bufLen,
Tcl_Interp* interp,
ClientData clientData));
static int FlushDecoder _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
Tcl_Interp* interp,
ClientData clientData));
static void ClearDecoder _ANSI_ARGS_ ((Trf_ControlBlock ctrlBlock,
ClientData clientData));
/*
* Generator definition.
*/
static Trf_TypeDefinition mdDefinition = /* THREADING: constant, read-only => safe */
{
NULL, /* filled later by Trf_RegisterMessageDigest (in a copy) */
NULL, /* filled later by Trf_RegisterMessageDigest (in a copy) */
NULL, /* filled later by Trf_RegisterMessageDigest (in a copy) */
{
CreateEncoder,
DeleteEncoder,
Encode,
EncodeBuffer,
FlushEncoder,
ClearEncoder,
NULL /* no MaxRead */
}, {
CreateDecoder,
DeleteDecoder,
Decode,
DecodeBuffer,
FlushDecoder,
ClearDecoder,
NULL /* no MaxRead */
},
TRF_UNSEEKABLE
};
/*
* Definition of the control blocks for en- and decoder.
*/
typedef struct _EncoderControl_ {
Trf_WriteProc* write;
ClientData writeClientData;
int operation_mode;
char* destHandle; /* Name of target for ATTACH_WRITE */
Tcl_Channel dest; /* Channel target for ATTACH_WRITE. possibly NULL */
Tcl_Interp* vInterp; /* Interpreter containing 'destHandle', if name of variable */
/* Possible combinations:
* destHandle != NULL, vInterp != NULL, dest == NULL /ATTACH_WRITE, to variable
* destHandle == NULL, vInterp == NULL, dest != NULL /ATTACH_WRITE, to channel
* destHandle == NULL, vInterp == NULL, dest == NULL /ATTACH_ABSORB, or IMMEDIATE
*
* TRF_TRANSPARENT <=> TRF_WRITE_HASH
*/
VOID* context;
} EncoderControl;
#define IMMEDIATE (0)
#define ATTACH_ABSORB (1)
#define ATTACH_WRITE (2)
#define ATTACH_TRANS (3)
typedef struct _DecoderControl_ {
Trf_WriteProc* write;
ClientData writeClientData;
int operation_mode;
char* destHandle; /* Name of target for ATTACH_WRITE */
Tcl_Channel dest; /* Channel target for ATTACH_WRITE. possibly NULL */
Tcl_Interp* vInterp; /* Interpreter containing 'destHandle', if name of variable */
/* Possible combinations:
* destHandle != NULL, dest == NULL /ATTACH_WRITE, to variable
* destHandle == NULL, dest != NULL /ATTACH_WRITE, to channel
* destHandle == NULL, dest == NULL /ATTACH_ABSORB, or IMMEDIATE
* vInterp always set, because of 'matchFlag'.
*
* TRF_TRANSPARENT <=> TRF_WRITE_HASH
*/
VOID* context;
char* matchFlag; /* target for ATTACH_ABSORB */
unsigned char* digest_buffer;
short buffer_pos;
unsigned short charCount;
} DecoderControl;
static int
WriteDigest _ANSI_ARGS_ ((Tcl_Interp* interp, char* destHandle,
Tcl_Channel dest, char* digest,
Trf_MessageDigestDescription* md));
/*
*------------------------------------------------------*
*
* Trf_RegisterMessageDigest --
*
* ------------------------------------------------*
* Register the specified generator as transformer.
* ------------------------------------------------*
*
* Sideeffects:
* Allocates memory. As of 'Trf_Register'.
*
* Result:
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
int
Trf_RegisterMessageDigest (interp, md_desc)
Tcl_Interp* interp;
CONST Trf_MessageDigestDescription* md_desc;
{
Trf_TypeDefinition* md;
int res;
START (Trf_RegisterMessageDigest);
/* THREADING: read-only access => safe */
md = (Trf_TypeDefinition*) ckalloc (sizeof (Trf_TypeDefinition));
memcpy ((VOID*) md, (VOID*) &mdDefinition, sizeof (Trf_TypeDefinition));
md->name = md_desc->name;
md->clientData = (ClientData) md_desc;
md->options = TrfMDOptions ();
PRINT ("MD_Desc %p\n", md_desc); FL; IN;
PRINT ("Name: %s\n", md_desc->name);
PRINT ("Context: %d\n", md_desc->context_size);
PRINT ("Digest: %d\n", md_desc->digest_size);
PRINT ("Start: %p\n", md_desc->startProc);
PRINT ("Update: %p\n", md_desc->updateProc);
PRINT ("UpdateBuf: %p\n", md_desc->updateBufProc);
PRINT ("Final %p\n", md_desc->finalProc);
PRINT ("Check: %p\n", md_desc->checkProc);
OT;
res = Trf_Register (interp, md);
/* 'md' is a memory leak, it will never be released.
*/
DONE (Trf_RegisterMessageDigest);
return res;
}
/*
*------------------------------------------------------*
*
* CreateEncoder --
*
* ------------------------------------------------*
* Allocate and initialize the control block of a
* data encoder.
* ------------------------------------------------*
*
* Sideeffects:
* Allocates memory.
*
* Result:
* An opaque reference to the control block.
*
*------------------------------------------------------*
*/
static Trf_ControlBlock
CreateEncoder (writeClientData, fun, optInfo, interp, clientData)
ClientData writeClientData;
Trf_WriteProc *fun;
Trf_Options optInfo;
Tcl_Interp* interp;
ClientData clientData;
{
EncoderControl* c;
TrfMDOptionBlock* o = (TrfMDOptionBlock*) optInfo;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
START (digest.CreateEncoder);
PRINT ("%p: %s\n", md, md->name);
c = (EncoderControl*) ckalloc (sizeof (EncoderControl));
c->write = fun;
c->writeClientData = writeClientData;
PRINT ("Setting up state\n"); FL;
if ((o->behaviour == TRF_IMMEDIATE) || (o->mode == TRF_ABSORB_HASH)) {
c->operation_mode = (o->behaviour == TRF_IMMEDIATE) ? IMMEDIATE : ATTACH_ABSORB;
c->vInterp = (Tcl_Interp*) NULL;
c->destHandle = (char*) NULL;
c->dest = (Tcl_Channel) NULL;
} else {
if (o->mode == TRF_WRITE_HASH)
c->operation_mode = ATTACH_WRITE;
else
c->operation_mode = ATTACH_TRANS;
if (o->wdIsChannel) {
c->vInterp = (Tcl_Interp*) NULL;
c->destHandle = (char*) NULL;
c->dest = o->wdChannel;
} else {
c->vInterp = o->vInterp;
c->destHandle = o->writeDestination;
c->dest = (Tcl_Channel) NULL;
o->writeDestination = (char*) NULL; /* ownership moved, prevent early free */
}
}
/*
* Create and initialize the context.
*/
PRINT ("Setting up context (%d bytes)\n", md->context_size); FL;
c->context = (VOID*) ckalloc (md->context_size);
(*md->startProc) (c->context);
DONE (digest.CreateEncoder);
return (ClientData) c;
}
/*
*------------------------------------------------------*
*
* DeleteEncoder --
*
* ------------------------------------------------*
* Destroy the control block of an encoder.
* ------------------------------------------------*
*
* Sideeffects:
* Releases the memory allocated by 'CreateEncoder'
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
DeleteEncoder (ctrlBlock, clientData)
Trf_ControlBlock ctrlBlock;
ClientData clientData;
{
EncoderControl* c = (EncoderControl*) ctrlBlock;
/* [Bug 2788106]. */
if (c->destHandle) {
ckfree (c->destHandle);
}
ckfree ((char*) c->context);
ckfree ((char*) c);
}
/*
*------------------------------------------------------*
*
* Encode --
*
* ------------------------------------------------*
* Encode the given character and write the result.
* ------------------------------------------------*
*
* Sideeffects:
* As of the called WriteFun.
*
* Result:
* Generated bytes implicitly via WriteFun.
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
Encode (ctrlBlock, character, interp, clientData)
Trf_ControlBlock ctrlBlock;
unsigned int character;
Tcl_Interp* interp;
ClientData clientData;
{
EncoderControl* c = (EncoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
unsigned char buf;
buf = character;
(*md->updateProc) (c->context, character);
if ((c->operation_mode == ATTACH_ABSORB) ||
(c->operation_mode == ATTACH_TRANS)) {
/*
* absorption/transparent mode: incoming characters flow
* unchanged through transformation.
*/
return c->write (c->writeClientData, &buf, 1, interp);
}
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* EncodeBuffer --
*
* ------------------------------------------------*
* Encode the given buffer and write the result.
* ------------------------------------------------*
*
* Sideeffects:
* As of the called WriteFun.
*
* Result:
* Generated bytes implicitly via WriteFun.
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
EncodeBuffer (ctrlBlock, buffer, bufLen, interp, clientData)
Trf_ControlBlock ctrlBlock;
unsigned char* buffer;
int bufLen;
Tcl_Interp* interp;
ClientData clientData;
{
EncoderControl* c = (EncoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
if (*md->updateBufProc != (Trf_MDUpdateBuf*) NULL) {
(*md->updateBufProc) (c->context, buffer, bufLen);
} else {
unsigned int character, i;
for (i=0; i < ((unsigned int) bufLen); i++) {
character = buffer [i];
(*md->updateProc) (c->context, character);
}
}
if ((c->operation_mode == ATTACH_ABSORB) ||
(c->operation_mode == ATTACH_TRANS)) {
/*
* absorption/transparent mode: incoming characters flow
* unchanged through transformation.
*/
return c->write (c->writeClientData, buffer, bufLen, interp);
}
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* FlushEncoder --
*
* ------------------------------------------------*
* Encode an incomplete character sequence (if possible).
* ------------------------------------------------*
*
* Sideeffects:
* As of the called WriteFun.
*
* Result:
* Generated bytes implicitly via WriteFun.
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
FlushEncoder (ctrlBlock, interp, clientData)
Trf_ControlBlock ctrlBlock;
Tcl_Interp* interp;
ClientData clientData;
{
EncoderControl* c = (EncoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
char* digest;
int res = TCL_OK;
/*
* Get a bit more, for a trailing \0 in 7.6, see 'WriteDigest' too
*/
digest = (char*) ckalloc (2 + md->digest_size);
(*md->finalProc) (c->context, digest);
if ((c->operation_mode == ATTACH_WRITE) ||
(c->operation_mode == ATTACH_TRANS)) {
res = WriteDigest (c->vInterp, c->destHandle, c->dest, digest, md);
} else {
/*
* Immediate execution or attached channel absorbing the checksum.
*/
/* -W- check wether digest can be declared 'uchar*', or if this has
* -W- other sideeffects, see lines 636, 653, 82 too.
*/
res = c->write (c->writeClientData, (unsigned char*) digest, md->digest_size, interp);
}
ckfree (digest);
return res;
}
/*
*------------------------------------------------------*
*
* ClearEncoder --
*
* ------------------------------------------------*
* Discard an incomplete character sequence.
* ------------------------------------------------*
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ClearEncoder (ctrlBlock, clientData)
Trf_ControlBlock ctrlBlock;
ClientData clientData;
{
EncoderControl* c = (EncoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
(*md->startProc) (c->context);
}
/*
*------------------------------------------------------*
*
* CreateDecoder --
*
* ------------------------------------------------*
* Allocate and initialize the control block of a
* data decoder.
* ------------------------------------------------*
*
* Sideeffects:
* Allocates memory.
*
* Result:
* An opaque reference to the control block.
*
*------------------------------------------------------*
*/
static Trf_ControlBlock
CreateDecoder (writeClientData, fun, optInfo, interp, clientData)
ClientData writeClientData;
Trf_WriteProc *fun;
Trf_Options optInfo;
Tcl_Interp* interp;
ClientData clientData;
{
DecoderControl* c;
TrfMDOptionBlock* o = (TrfMDOptionBlock*) optInfo;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
c = (DecoderControl*) ckalloc (sizeof (DecoderControl));
c->write = fun;
c->writeClientData = writeClientData;
c->matchFlag = o->matchFlag;
c->vInterp = o->vInterp;
o->matchFlag = NULL;
/* impossible: (o->behaviour == TRF_IMMEDIATE) */
if (o->mode == TRF_ABSORB_HASH) {
c->operation_mode = ATTACH_ABSORB;
c->destHandle = (char*) NULL;
c->dest = (Tcl_Channel) NULL;
} else {
if (o->mode == TRF_WRITE_HASH)
c->operation_mode = ATTACH_WRITE;
else
c->operation_mode = ATTACH_TRANS;
if (o->rdIsChannel) {
c->destHandle = (char*) NULL;
c->dest = o->rdChannel;
} else {
c->destHandle = o->readDestination;
c->dest = (Tcl_Channel) NULL;
o->readDestination = (char*) NULL; /* ownership moved, prevent early free */
}
}
c->buffer_pos = 0;
c->charCount = 0;
c->context = (VOID*) ckalloc (md->context_size);
(*md->startProc) (c->context);
c->digest_buffer = (unsigned char*) ckalloc (md->digest_size);
memset (c->digest_buffer, '\0', md->digest_size);
return (ClientData) c;
}
/*
*------------------------------------------------------*
*
* DeleteDecoder --
*
* ------------------------------------------------*
* Destroy the control block of an decoder.
* ------------------------------------------------*
*
* Sideeffects:
* Releases the memory allocated by 'CreateDecoder'
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
DeleteDecoder (ctrlBlock, clientData)
Trf_ControlBlock ctrlBlock;
ClientData clientData;
{
DecoderControl* c = (DecoderControl*) ctrlBlock;
/* [Bug 2788106]. */
if (c->destHandle) {
ckfree (c->destHandle);
}
ckfree ((char*) c->digest_buffer);
ckfree ((char*) c->context);
ckfree ((char*) c);
}
/*
*------------------------------------------------------*
*
* Decode --
*
* ------------------------------------------------*
* Decode the given character and write the result.
* ------------------------------------------------*
*
* Sideeffects:
* As of the called WriteFun.
*
* Result:
* Generated bytes implicitly via WriteFun.
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
Decode (ctrlBlock, character, interp, clientData)
Trf_ControlBlock ctrlBlock;
unsigned int character;
Tcl_Interp* interp;
ClientData clientData;
{
DecoderControl* c = (DecoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
char buf;
if (c->operation_mode == ATTACH_WRITE) {
buf = character;
(*md->updateProc) (c->context, character);
} else if (c->operation_mode == ATTACH_TRANS) {
buf = character;
(*md->updateProc) (c->context, character);
return c->write (c->writeClientData, (unsigned char*) &buf, 1, interp);
} else {
if (c->charCount == md->digest_size) {
/*
* ringbuffer full, forward oldest character
* and replace with new one.
*/
buf = c->digest_buffer [c->buffer_pos];
c->digest_buffer [c->buffer_pos] = character;
c->buffer_pos ++;
c->buffer_pos %= md->digest_size;
character = buf;
(*md->updateProc) (c->context, character);
return c->write (c->writeClientData, (unsigned char*) &buf, 1, interp);
} else {
/*
* Fill ringbuffer.
*/
c->digest_buffer [c->buffer_pos] = character;
c->buffer_pos ++;
c->charCount ++;
}
}
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* DecodeBuffer --
*
* ------------------------------------------------*
* Decode the given buffer and write the result.
* ------------------------------------------------*
*
* Sideeffects:
* As of the called WriteFun.
*
* Result:
* Generated bytes implicitly via WriteFun.
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
DecodeBuffer (ctrlBlock, buffer, bufLen, interp, clientData)
Trf_ControlBlock ctrlBlock;
unsigned char* buffer;
int bufLen;
Tcl_Interp* interp;
ClientData clientData;
{
DecoderControl* c = (DecoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
if (c->operation_mode == ATTACH_WRITE) {
if (*md->updateBufProc != (Trf_MDUpdateBuf*) NULL) {
(*md->updateBufProc) (c->context, buffer, bufLen);
} else {
int character, i;
for (i=0; i < bufLen; i++) {
character = buffer [i];
(*md->updateProc) (c->context, character);
}
}
} else if (c->operation_mode == ATTACH_TRANS) {
if (*md->updateBufProc != (Trf_MDUpdateBuf*) NULL) {
(*md->updateBufProc) (c->context, buffer, bufLen);
} else {
int character, i;
for (i=0; i < bufLen; i++) {
character = buffer [i];
(*md->updateProc) (c->context, character);
}
}
return c->write (c->writeClientData, buffer, bufLen, interp);
} else {
/* try to use more than character at a time. */
if (*md->updateBufProc != NULL) {
/*
* 2 cases:
*
* - Incoming buffer and data stored from previous calls are less
* or just enough to fill the ringbuffer. Copy the incoming bytes
* into the buffer and return.
*
* - Incoming data + ringbuffer contain more than reqired for a
* digest. Concatenate both and use the oldest bytes to update the
* hash context. Place the remaining 'digest_size' bytes into the
* ringbuffer again.
*
* Both cases assume the ringbuffer data to be starting at index '0'.
*/
if ((c->charCount + bufLen) <= md->digest_size) {
/* extend ring buffer */
memcpy ( (VOID*) (c->digest_buffer + c->charCount), (VOID*) buffer, bufLen);
c->charCount += bufLen;
} else {
/*
* n contains the number of bytes we are allowed to hash into the context.
*/
int n = c->charCount + bufLen - md->digest_size;
int res;
if (c->charCount > 0) {
if (n <= c->charCount) {
/*
* update context, then shift down the remaining bytes
*/
(*md->updateBufProc) (c->context, c->digest_buffer, n);
res = c->write (c->writeClientData, c->digest_buffer, n, interp);
memmove ((VOID*) c->digest_buffer,
(VOID*) (c->digest_buffer + n), c->charCount - n);
c->charCount -= n;
n = 0;
} else {
/*
* Hash everything inside the buffer.
*/
(*md->updateBufProc) (c->context, c->digest_buffer, c->charCount);
res = c->write (c->writeClientData, c->digest_buffer, c->charCount, interp);
n -= c->charCount;
c->charCount = 0;
}
if (res != TCL_OK)
return res;
}
if (n > 0) {
(*md->updateBufProc) (c->context, buffer, n);
res = c->write (c->writeClientData, buffer, n, interp);
memcpy ((VOID*) (c->digest_buffer + c->charCount),
(VOID*) (buffer + n),
(bufLen - n));
c->charCount = md->digest_size; /* <=> 'c->charCount += bufLen - n;' */
if (res != TCL_OK)
return res;
}
}
} else {
/*
* no 'updateBufProc', simulate it using the
* underlying single character routine
*/
int character, i, res;
char buf;
for (i=0; i < bufLen; i++) {
buf = c->digest_buffer [c->buffer_pos];
character = buffer [i];
if (c->charCount == md->digest_size) {
/*
* ringbuffer full, forward oldest character
* and replace with new one.
*/
c->digest_buffer [c->buffer_pos] = character;
c->buffer_pos ++;
c->buffer_pos %= md->digest_size;
character = buf;
(*md->updateProc) (c->context, character);
res = c->write (c->writeClientData, (unsigned char*) &buf, 1, interp);
if (res != TCL_OK)
return res;
} else {
/*
* Fill ringbuffer.
*/
c->digest_buffer [c->buffer_pos] = character;
c->buffer_pos ++;
c->charCount ++;
}
} /* for */
}
}
return TCL_OK;
}
/*
*------------------------------------------------------*
*
* FlushDecoder --
*
* ------------------------------------------------*
* Decode an incomplete character sequence (if possible).
* ------------------------------------------------*
*
* Sideeffects:
* As of the called WriteFun.
*
* Result:
* Generated bytes implicitly via WriteFun.
* A standard Tcl error code.
*
*------------------------------------------------------*
*/
static int
FlushDecoder (ctrlBlock, interp, clientData)
Trf_ControlBlock ctrlBlock;
Tcl_Interp* interp;
ClientData clientData;
{
DecoderControl* c = (DecoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
char* digest;
int res= TCL_OK;
/*
* Get a bit more, for a trailing \0 in 7.6, see 'WriteDigest' too
*/
digest = (char*) ckalloc (2 + md->digest_size);
(*md->finalProc) (c->context, digest);
if ((c->operation_mode == ATTACH_WRITE) ||
(c->operation_mode == ATTACH_TRANS)) {
res = WriteDigest (c->vInterp, c->destHandle, c->dest, digest, md);
} else if (c->charCount < md->digest_size) {
/*
* ATTACH_ABSORB, not enough data in input!
*/
if (interp) {
Tcl_AppendResult (interp, "not enough bytes in input", (char*) NULL);
}
res = TCL_ERROR;
} else {
char* result_text;
if (c->buffer_pos > 0) {
/*
* Reorder bytes in ringbuffer to form the correct digest.
*/
char* temp;
int i,j;
temp = (char*) ckalloc (md->digest_size);
for (i= c->buffer_pos, j=0;
j < md->digest_size;
i = (i+1) % md->digest_size, j++) {
temp [j] = c->digest_buffer [i];
}
memcpy ((VOID*) c->digest_buffer, (VOID*) temp, md->digest_size);
ckfree (temp);
}
/*
* Compare computed and transmitted checksums.
*/
result_text = (0 == memcmp ((VOID*) digest, (VOID*) c->digest_buffer, md->digest_size) ?
"ok" : "failed");
Tcl_SetVar (c->vInterp, c->matchFlag, result_text, TCL_GLOBAL_ONLY);
}
ckfree (digest);
return res;
}
/*
*------------------------------------------------------*
*
* ClearDecoder --
*
* ------------------------------------------------*
* Discard an incomplete character sequence.
* ------------------------------------------------*
*
* Sideeffects:
* See above.
*
* Result:
* None.
*
*------------------------------------------------------*
*/
static void
ClearDecoder (ctrlBlock, clientData)
Trf_ControlBlock ctrlBlock;
ClientData clientData;
{
DecoderControl* c = (DecoderControl*) ctrlBlock;
Trf_MessageDigestDescription* md = (Trf_MessageDigestDescription*) clientData;
c->buffer_pos = 0;
c->charCount = 0;
(*md->startProc) (c->context);
memset (c->digest_buffer, '\0', md->digest_size);
}
/*
*------------------------------------------------------*
*
* WriteDigest --
*
* ------------------------------------------------*
* Writes the generated digest into the destination
* variable, or channel.
* ------------------------------------------------*
*
* Sideeffects:
* May leave an error message in the
* interpreter result area.
*
* Result:
* A standard Tcl error message.
*
*------------------------------------------------------*
*/
static int
WriteDigest (interp, destHandle, dest, digest, md)
Tcl_Interp* interp;
char* destHandle;
Tcl_Channel dest;
char* digest;
Trf_MessageDigestDescription* md;
{
if (destHandle != (char*) NULL) {
#if GT81
Tcl_Obj* digestObj = Tcl_NewByteArrayObj (digest, md->digest_size);
#else
Tcl_Obj* digestObj = Tcl_NewStringObj (digest, md->digest_size);
#endif
Tcl_Obj* result;
/*#if GT81
/ * 8.1 and beyond * /
Tcl_IncrRefCount(digestObj);
result = Tcl_SetObjVar2 (interp, destHandle, (char*) NULL, digestObj,
TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
#else*/
/* 8.0 section */
Tcl_Obj* varName = Tcl_NewStringObj (destHandle, strlen (destHandle));
Tcl_IncrRefCount(varName);
Tcl_IncrRefCount(digestObj);
result = Tcl_ObjSetVar2 (interp, varName, (Tcl_Obj*) NULL, digestObj,
TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY |
TCL_PARSE_PART1);
Tcl_DecrRefCount(varName);
/*#endif / * GT81 */
Tcl_DecrRefCount(digestObj);
if (result == (Tcl_Obj*) NULL) {
return TCL_ERROR;
}
} else if (dest != (Tcl_Channel) NULL) {
int res = Tcl_Write (dest, digest, md->digest_size);
if (res < 0) {
if (interp) {
Tcl_AppendResult (interp,
"error writing \"", Tcl_GetChannelName (dest),
"\": ", Tcl_PosixError (interp), (char*) NULL);
}
return TCL_ERROR;
}
}
return TCL_OK;
}
|