62 if( envelopeInfoPtr->buffer == NULL || \
63 envelopeInfoPtr->
bufPos < 0 || \
112 REQUIRES( sanityCheck( envelopeInfoPtr ) );
116 if( envelopeInfoPtr->blockSize > 1 )
121 padSize = envelopeInfoPtr->buffer[ envelopeInfoPtr->bufPos - 1 ];
122 if( padSize < 1 || padSize > envelopeInfoPtr->blockSize || \
123 padSize > envelopeInfoPtr->bufPos )
127 envelopeInfoPtr->bufPos -= padSize;
128 for( i = 0; i < padSize - 1; i++ )
130 if( envelopeInfoPtr->buffer[ envelopeInfoPtr->bufPos + i ] != padSize )
133 ENSURES( envelopeInfoPtr->bufPos >= 0 && \
134 envelopeInfoPtr->bufPos < envelopeInfoPtr->bufSize );
140 envelopeInfoPtr->dataLeft = envelopeInfoPtr->bufPos;
142 ENSURES( sanityCheck( envelopeInfoPtr ) );
158 assert(
isWritePtr( segmentLength,
sizeof(
long ) ) );
160 REQUIRES( sanityCheck( envelopeInfoPtr ) );
166 status = checkEOC( stream );
172 return( processDataEnd( envelopeInfoPtr ) );
176 status = readLongGenericHole( stream, segmentLength,
BER_OCTETSTRING );
200 assert(
isWritePtr( segmentLength,
sizeof(
long ) ) );
202 REQUIRES( sanityCheck( envelopeInfoPtr ) );
208 status = pgpReadPartialLength( stream, segmentLength );
230 if( *segmentLength <= 0 )
235 status = processDataEnd( envelopeInfoPtr );
254 if( *segmentLength < 0 )
256 DEBUG_DIAG((
"MDC data was broken over a partial-length "
270 envelopeInfoPtr->payloadSize = *segmentLength;
273 ENSURES( sanityCheck( envelopeInfoPtr ) );
286 SEGMENT_INSUFFICIENTDATA,
304 assert(
isWritePtr( bytesConsumed,
sizeof(
int ) ) );
305 assert(
isWritePtr( segmentStatus,
sizeof( SEGMENT_STATUS ) ) );
307 REQUIRES( sanityCheck( envelopeInfoPtr ) );
312 *segmentStatus = SEGMENT_NONE;
320 REQUIRES( envelopeInfoPtr->segmentSize <= 0 );
322 *segmentStatus = SEGMENT_ENDOFDATA;
330 envelopeInfoPtr->segmentSize = envelopeInfoPtr->payloadSize;
331 *segmentStatus = SEGMENT_FIXEDLENGTH;
349 REQUIRES( envelopeInfoPtr->segmentSize <= 0 );
351 *segmentStatus = SEGMENT_FIXEDLENGTH;
365 *segmentStatus = SEGMENT_INSUFFICIENTDATA;
370 sMemConnect( &stream, buffer, length );
374 status = processPgpSegment( envelopeInfoPtr, &stream,
380 status = processSegment( envelopeInfoPtr, &stream,
384 *bytesConsumed = stell( &stream );
385 sMemDisconnect( &stream );
393 *segmentStatus = SEGMENT_INSUFFICIENTDATA;
398 ENSURES( *bytesConsumed > 0 && *bytesConsumed <= length );
401 envelopeInfoPtr->segmentSize = segmentLength;
403 ENSURES( sanityCheck( envelopeInfoPtr ) );
437 BYTE *bufPtr = envelopeInfoPtr->buffer + envelopeInfoPtr->bufPos;
438 int bytesFromBB = 0, quantizedBytesToCopy, bytesToBB,
status;
442 assert(
isWritePtr( bytesCopied,
sizeof(
int ) ) );
444 REQUIRES( sanityCheck( envelopeInfoPtr ) );
446 envelopeInfoPtr->bufPos + \
447 envelopeInfoPtr->blockBufferPos + \
448 length <= envelopeInfoPtr->
bufSize + \
449 envelopeInfoPtr->blockSize );
457 if( envelopeInfoPtr->blockBufferPos + length < envelopeInfoPtr->
blockSize )
459 memcpy( envelopeInfoPtr->blockBuffer + envelopeInfoPtr->blockBufferPos,
461 envelopeInfoPtr->blockBufferPos +=
length;
464 envelopeInfoPtr->segmentSize -=
length;
467 ENSURES( sanityCheck( envelopeInfoPtr ) );
482 if( envelopeInfoPtr->bufPos + \
483 envelopeInfoPtr->blockSize > envelopeInfoPtr->bufSize )
492 if( envelopeInfoPtr->blockBufferPos > 0 )
495 bytesFromBB <= envelopeInfoPtr->blockSize );
497 bytesFromBB = envelopeInfoPtr->blockBufferPos;
498 memcpy( bufPtr, envelopeInfoPtr->blockBuffer, bytesFromBB );
500 envelopeInfoPtr->blockBufferPos = 0;
504 quantizedBytesToCopy = ( length + bytesFromBB ) & \
505 envelopeInfoPtr->blockSizeMask;
506 quantizedBytesToCopy -= bytesFromBB;
507 ENSURES( quantizedBytesToCopy > 0 && quantizedBytesToCopy <= length && \
508 envelopeInfoPtr->bufPos + bytesFromBB + \
509 quantizedBytesToCopy <= envelopeInfoPtr->
bufSize );
510 ENSURES( ( ( bytesFromBB + quantizedBytesToCopy ) & \
511 ( envelopeInfoPtr->blockSize - 1 ) ) == 0 );
518 memmove( bufPtr + bytesFromBB, buffer, quantizedBytesToCopy );
523 status = hashEnvelopeData( envelopeInfoPtr->actionList, bufPtr,
524 bytesFromBB + quantizedBytesToCopy );
530 bytesFromBB + quantizedBytesToCopy );
533 envelopeInfoPtr->bufPos += bytesFromBB + quantizedBytesToCopy;
534 envelopeInfoPtr->segmentSize -=
length;
535 ENSURES( envelopeInfoPtr->bufPos >= 0 && \
536 envelopeInfoPtr->bufPos <= envelopeInfoPtr->bufSize );
537 ENSURES( envelopeInfoPtr->segmentSize >= 0 && \
543 envelopeInfoPtr->segmentSize <= 0 )
545 status = processDataEnd( envelopeInfoPtr );
549 ENSURES( sanityCheck( envelopeInfoPtr ) );
557 bytesToBB = length - quantizedBytesToCopy;
558 REQUIRES( bytesToBB >= 0 && bytesToBB <= envelopeInfoPtr->blockSize );
561 memcpy( envelopeInfoPtr->blockBuffer, buffer + quantizedBytesToCopy,
564 envelopeInfoPtr->blockBufferPos = bytesToBB;
566 ENSURES( sanityCheck( envelopeInfoPtr ) );
600 BYTE *bufPtr = envelopeInfoPtr->buffer + envelopeInfoPtr->bufPos;
605 assert(
isWritePtr( bytesCopied,
sizeof(
int ) ) );
607 REQUIRES( sanityCheck( envelopeInfoPtr ) );
617 bytesToCopy > envelopeInfoPtr->segmentSize )
618 bytesToCopy = envelopeInfoPtr->segmentSize;
655 bytesLeft = envelopeInfoPtr->bufSize - \
656 ( envelopeInfoPtr->bufPos + envelopeInfoPtr->blockBufferPos );
666 if( bytesLeft < bytesToCopy )
667 bytesToCopy = bytesLeft;
668 ENSURES( bytesToCopy > 0 && bytesToCopy <= length );
672 if( envelopeInfoPtr->blockSize > 1 )
674 return( copyEncryptedDataBlocks( envelopeInfoPtr, buffer,
675 bytesToCopy, bytesCopied ) );
681 memmove( bufPtr, buffer, bytesToCopy );
682 if( envelopeInfoPtr->iCryptContext !=
CRYPT_ERROR )
688 status = hashEnvelopeData( envelopeInfoPtr->actionList, bufPtr,
699 envelopeInfoPtr->bufPos += bytesToCopy;
701 envelopeInfoPtr->segmentSize -= bytesToCopy;
706 envelopeInfoPtr->segmentSize <= 0 )
708 status = processDataEnd( envelopeInfoPtr );
713 ENSURES( bytesToCopy > 0 && bytesToCopy <= length );
714 ENSURES( sanityCheck( envelopeInfoPtr ) );
716 *bytesCopied = bytesToCopy;
734 REQUIRES( sanityCheck( envelopeInfoPtr ) );
740 if( envelopeInfoPtr->bufPos >= envelopeInfoPtr->bufSize )
753 status = hashEnvelopeData( envelopeInfoPtr->actionList,
754 buffer, currentLength );
776 for( segmentCount = 0; \
778 envelopeInfoPtr->segmentSize <= 0; \
781 SEGMENT_STATUS segmentStatus;
784 status = getNextSegment( envelopeInfoPtr, bufPtr, currentLength,
785 &bytesConsumed, &segmentStatus );
789 if( segmentStatus == SEGMENT_ENDOFDATA )
790 return( length - currentLength );
795 ENSURES( segmentStatus == SEGMENT_FIXEDLENGTH );
800 if( bytesConsumed <= 0 )
802 const int prevBytesConsumed = length - currentLength;
808 ENSURES( segmentStatus == SEGMENT_INSUFFICIENTDATA );
809 ENSURES( sanityCheck( envelopeInfoPtr ) );
810 return( ( prevBytesConsumed <= 0 ) ?
813 bufPtr += bytesConsumed;
814 currentLength -= bytesConsumed;
821 ENSURES( sanityCheck( envelopeInfoPtr ) );
822 return( length - currentLength );
832 ENSURES( currentLength > 0 && currentLength <= length && \
842 status = copyData( envelopeInfoPtr, bufPtr, currentLength,
849 ENSURES( sanityCheck( envelopeInfoPtr ) );
850 ENSURES( currentLength >= 0 && currentLength <= length && \
853 assert( ( envelopeInfoPtr->segmentSize >= 0 ) || \
858 while( currentLength > 0 && bytesCopied > 0 && \
862 ENSURES( sanityCheck( envelopeInfoPtr ) );
863 return( length - currentLength );
883 const int oobBytesToCopy =
min( maxLength, envelopeInfoPtr->oobBufPos );
884 const int oobRemainder = envelopeInfoPtr->oobBufPos - oobBytesToCopy;
888 assert(
isWritePtr( length,
sizeof(
int ) ) );
890 REQUIRES( sanityCheck( envelopeInfoPtr ) );
893 oobBytesToCopy <= envelopeInfoPtr->oobBufPos && \
896 memcpy( buffer, envelopeInfoPtr->oobBuffer, oobBytesToCopy );
897 *length = oobBytesToCopy;
902 ENSURES( sanityCheck( envelopeInfoPtr ) );
909 if( oobRemainder > 0 )
913 memmove( envelopeInfoPtr->oobBuffer,
914 envelopeInfoPtr->oobBuffer + oobBytesToCopy, oobRemainder );
916 envelopeInfoPtr->oobBufPos = oobRemainder;
918 ENSURES( sanityCheck( envelopeInfoPtr ) );
930 IN_FLAGS( ENVCOPY )
const int flags )
939 assert(
isWritePtr( length,
sizeof(
int ) ) );
941 REQUIRES( sanityCheck( envelopeInfoPtr ) );
944 ( isLookaheadRead && \
949 memset( buffer, 0,
min( 16, bytesToCopy ) );
960 if( envelopeInfoPtr->oobBufPos > 0 )
962 status = copyOobData( envelopeInfoPtr, buffer, bytesToCopy,
963 &oobBytesCopied, isLookaheadRead );
966 bytesToCopy -= oobBytesCopied;
967 buffer += oobBytesCopied;
968 if( bytesToCopy <= 0 )
970 *length = oobBytesCopied;
972 ENSURES( sanityCheck( envelopeInfoPtr ) );
976 ENSURES( bytesToCopy > 0 && bytesToCopy <= maxLength && \
981 #ifdef USE_COMPRESSION
984 const int originalInLength = bytesToCopy;
985 const int bytesIn = \
986 ( envelopeInfoPtr->dataLeft > 0 && \
987 envelopeInfoPtr->dataLeft < envelopeInfoPtr->bufPos ) ? \
988 envelopeInfoPtr->dataLeft : envelopeInfoPtr->bufPos;
1016 envelopeInfoPtr->zStream.next_in = envelopeInfoPtr->buffer;
1017 envelopeInfoPtr->zStream.avail_in = bytesIn;
1018 envelopeInfoPtr->zStream.next_out = buffer;
1019 envelopeInfoPtr->zStream.avail_out = bytesToCopy;
1036 bytesCopied = bytesIn - envelopeInfoPtr->zStream.avail_in;
1037 bytesToCopy -= envelopeInfoPtr->zStream.avail_out;
1039 bytesToCopy >= 0 && bytesToCopy <= originalInLength && \
1052 if( envelopeInfoPtr->zStream.avail_in <= 0 && \
1053 envelopeInfoPtr->dataLeft > 0 && \
1054 envelopeInfoPtr->dataLeft < envelopeInfoPtr->bufPos )
1058 ( envelopeInfoPtr->bufPos - envelopeInfoPtr->dataLeft != 2 ) ) )
1064 envelopeInfoPtr->dataLeft = envelopeInfoPtr->bufPos;
1077 if( isLookaheadRead )
1079 REQUIRES( envelopeInfoPtr->oobBufPos + \
1082 memcpy( envelopeInfoPtr->oobBuffer + envelopeInfoPtr->oobBufPos,
1083 buffer, originalInLength );
1084 envelopeInfoPtr->oobBufPos += originalInLength;
1092 if( bytesToCopy > envelopeInfoPtr->bufPos )
1093 bytesToCopy = envelopeInfoPtr->bufPos;
1094 if( envelopeInfoPtr->dataLeft > 0 && \
1095 bytesToCopy > envelopeInfoPtr->dataLeft )
1096 bytesToCopy = envelopeInfoPtr->dataLeft;
1097 ENSURES( bytesToCopy >= 0 && bytesToCopy <= maxLength && \
1103 ENSURES( sanityCheck( envelopeInfoPtr ) );
1110 if( envelopeInfoPtr->blockSize > 1 && \
1112 envelopeInfoPtr->blockBufferPos > 0 )
1113 bytesToCopy -= envelopeInfoPtr->blockSize;
1117 if( bytesToCopy <= 0 )
1119 *length = oobBytesCopied;
1127 envelopeInfoPtr->dataLeft <= 0 )
1129 *length = oobBytesCopied;
1135 if( isLookaheadRead )
1139 memcpy( buffer, envelopeInfoPtr->buffer, bytesToCopy );
1140 *length = bytesToCopy;
1147 status = hashEnvelopeData( envelopeInfoPtr->actionList,
1148 envelopeInfoPtr->buffer,
1155 memcpy( buffer, envelopeInfoPtr->buffer, bytesToCopy );
1156 bytesCopied = bytesToCopy;
1158 ENSURES( envelopeInfoPtr->bufPos - bytesCopied >= 0 );
1161 remainder = envelopeInfoPtr->bufPos -
bytesCopied;
1164 bytesCopied + remainder <= envelopeInfoPtr->bufSize );
1165 if( remainder > 0 && bytesCopied > 0 )
1168 envelopeInfoPtr->bufSize ) );
1169 memmove( envelopeInfoPtr->buffer,
1170 envelopeInfoPtr->buffer + bytesCopied, remainder );
1172 envelopeInfoPtr->bufPos = remainder;
1176 if( envelopeInfoPtr->dataLeft > 0 && bytesCopied > 0 )
1178 ENSURES( envelopeInfoPtr->dataLeft >= 0 && \
1180 *length = oobBytesCopied + bytesToCopy;
1182 ENSURES( sanityCheck( envelopeInfoPtr ) );
1198 const long dataStartPos = stell( stream );
1199 const int oldBufPos = envelopeInfoPtr->bufPos;
1200 const int bytesLeft = sMemDataLeft( stream );
1206 REQUIRES( sanityCheck( envelopeInfoPtr ) );
1225 envelopeInfoPtr->bufPos = 0;
1226 if( bytesLeft <= 0 )
1232 sMemDisconnect( stream );
1233 sMemConnect( stream, envelopeInfoPtr->buffer, bytesLeft );
1234 bytesCopied = envelopeInfoPtr->copyToEnvelopeFunction( envelopeInfoPtr,
1235 envelopeInfoPtr->buffer + dataStartPos, bytesLeft );
1244 envelopeInfoPtr->bufPos = oldBufPos;
1245 ENSURES( sanityCheck( envelopeInfoPtr ) );
1246 return( bytesCopied );
1281 bytesCopied < bytesLeft )
1286 envelopeInfoPtr->dataLeft + \
1287 bytesToCopy <= envelopeInfoPtr->bufSize && \
1288 bytesCopied + dataStartPos + \
1289 bytesToCopy <= envelopeInfoPtr->bufSize );
1290 memmove( envelopeInfoPtr->buffer + envelopeInfoPtr->dataLeft,
1291 envelopeInfoPtr->buffer + dataStartPos + bytesCopied,
1293 envelopeInfoPtr->bufPos = envelopeInfoPtr->dataLeft + bytesToCopy;
1296 ENSURES( sanityCheck( envelopeInfoPtr ) );
1311 assert( length == 0 ||
isReadPtr( buffer, length ) );
1313 REQUIRES( sanityCheck( envelopeInfoPtr ) );
1331 return( hashEnvelopeData( envelopeInfoPtr->actionList, buffer,
1338 status = hashEnvelopeData( envelopeInfoPtr->actionList,
"", 0 );
1342 ENVDATA_AUTHENCACTIONSACTIVE );
1357 envelopeInfoPtr->copyToEnvelopeFunction = copyToDeenvelope;
1358 envelopeInfoPtr->copyFromEnvelopeFunction = copyFromDeenvelope;
1359 envelopeInfoPtr->syncDeenvelopeData = syncDeenvelopeData;
1360 envelopeInfoPtr->processExtraData = processExtraData;