58 if( envelopeInfoPtr->buffer == NULL || \
59 envelopeInfoPtr->
bufPos < 0 || \
75 envelopeInfoPtr->
bufPos == 0 )
135 int iterationCount,
status;
138 assert( dataLength == 0 ||
isReadPtr( data, dataLength ) );
143 for( iterationCount = 0;
144 actionListPtr != NULL && \
146 actionListPtr = actionListPtr->
next, iterationCount++ )
161 ENSURES( iterationCount < FAILSAFE_ITERATIONS_MED );
181 #define lengthOfLength( length ) ( ( length < 0x80 ) ? 1 : \
182 ( length < 0x100 ) ? 2 : \
183 ( length < 0x10000 ) ? 3 : \
184 ( length < 0x1000000 ) ? 4 : 5 )
186 #define findThreshold( length ) ( ( length < 0x80 ) ? 0x7F : \
187 ( length < 0x100 ) ? 0xFF : \
188 ( length < 0x10000 ) ? 0xFFFF : \
189 ( length < 0x1000000 ) ? 0xFFFFFF : INT_MAX )
192 #define lengthOfLength( length ) ( ( length < 0x80 ) ? 1 : \
193 ( length < 0x100 ) ? 2 : 3 )
195 #define findThreshold( length ) ( ( length < 0x80 ) ? 127 : \
196 ( length < 0x100 ) ? 0xFF : INT_MAX )
217 int segHeaderSize = 0;
221 REQUIRES( sanityCheck( envelopeInfoPtr ) );
229 segHeaderSize = TAG_SIZE + \
230 lengthOfLength( envelopeInfoPtr->bufSize );
241 if( envelopeInfoPtr->bufPos + segHeaderSize + \
242 envelopeInfoPtr->blockBufferPos >= envelopeInfoPtr->bufSize )
247 envelopeInfoPtr->segmentStart = envelopeInfoPtr->bufPos;
257 envelopeInfoPtr->bufPos += segHeaderSize;
259 envelopeInfoPtr->segmentDataStart = envelopeInfoPtr->bufPos;
260 ENSURES( envelopeInfoPtr->bufPos + \
261 envelopeInfoPtr->blockBufferPos <= envelopeInfoPtr->bufSize );
267 if( envelopeInfoPtr->blockBufferPos > 0 )
270 envelopeInfoPtr->blockBufferPos,
271 envelopeInfoPtr->bufSize ) );
272 memcpy( envelopeInfoPtr->buffer + envelopeInfoPtr->bufPos,
273 envelopeInfoPtr->blockBuffer, envelopeInfoPtr->blockBufferPos );
274 envelopeInfoPtr->bufPos += envelopeInfoPtr->blockBufferPos;
276 envelopeInfoPtr->blockBufferPos = 0;
281 ENSURES( sanityCheck( envelopeInfoPtr ) );
299 const int oldHdrLen = envelopeInfoPtr->segmentDataStart - \
300 envelopeInfoPtr->segmentStart;
303 int dataLen = envelopeInfoPtr->bufPos - envelopeInfoPtr->segmentDataStart;
304 int hdrLen, remainder = 0,
status;
306 REQUIRES( sanityCheck( envelopeInfoPtr ) );
323 if( envelopeInfoPtr->segmentDataStart + dataLen + \
324 envelopeInfoPtr->blockSize < envelopeInfoPtr->bufSize )
325 dataLen += envelopeInfoPtr->blockSize;
327 needsPadding =
FALSE;
329 ENSURES( dataLen > 0 && envelopeInfoPtr->segmentDataStart + \
330 dataLen <= envelopeInfoPtr->
bufSize );
335 hdrLen = ( envelopeInfoPtr->payloadSize ==
CRYPT_UNUSED ) ? \
336 TAG_SIZE + lengthOfLength( dataLen ) : 0;
351 int quantisedTotalLen, threshold;
354 quantisedTotalLen = dataLen & envelopeInfoPtr->blockSizeMask;
359 threshold = findThreshold( quantisedTotalLen );
360 if( quantisedTotalLen <= threshold && dataLen > threshold )
366 remainder = dataLen - quantisedTotalLen;
367 dataLen = quantisedTotalLen;
371 hdrLen > 0 && hdrLen <= TAG_SIZE + 5 ) );
372 ENSURES( ( envelopeInfoPtr->blockSize == 0 && remainder == 0 ) || \
373 ( envelopeInfoPtr->blockSize > 0 && \
374 remainder >= 0 && remainder < envelopeInfoPtr->
blockSize && \
409 if( hdrLen > 0 && hdrLen < oldHdrLen )
411 BYTE *segmentDataPtr = envelopeInfoPtr->buffer + \
412 envelopeInfoPtr->segmentStart;
413 const int delta = oldHdrLen - hdrLen;
415 memmove( segmentDataPtr + hdrLen, segmentDataPtr + oldHdrLen,
416 envelopeInfoPtr->bufPos - envelopeInfoPtr->segmentDataStart );
417 envelopeInfoPtr->bufPos -= delta;
418 envelopeInfoPtr->segmentDataStart -= delta;
420 ENSURES( sanityCheck( envelopeInfoPtr ) );
421 ENSURES( envelopeInfoPtr->segmentDataStart + \
422 dataLen <= envelopeInfoPtr->
bufSize );
433 const int padSize = envelopeInfoPtr->blockSize - remainder;
437 envelopeInfoPtr->bufPos + \
438 padSize <= envelopeInfoPtr->
bufSize );
442 for( i = 0; i < padSize; i++ )
444 envelopeInfoPtr->buffer[ envelopeInfoPtr->bufPos + i ] = \
445 intToByte( padSize );
447 envelopeInfoPtr->bufPos += padSize;
449 ENSURES( envelopeInfoPtr->bufPos >= 0 && \
450 envelopeInfoPtr->bufPos <= envelopeInfoPtr->bufSize );
458 REQUIRES( envelopeInfoPtr->bufPos > remainder );
459 memcpy( envelopeInfoPtr->blockBuffer,
460 envelopeInfoPtr->buffer + envelopeInfoPtr->bufPos - \
461 remainder, remainder );
462 envelopeInfoPtr->blockBufferPos = remainder;
463 envelopeInfoPtr->bufPos -= remainder;
467 ENSURES( sanityCheck( envelopeInfoPtr ) );
474 sMemOpen( &stream, envelopeInfoPtr->buffer + \
475 envelopeInfoPtr->segmentStart, hdrLen );
476 status = writeOctetStringHole( &stream, dataLen,
DEFAULT_TAG );
478 sMemDisconnect( &stream );
485 const BOOLEAN forceCompletion )
491 REQUIRES( sanityCheck( envelopeInfoPtr ) );
501 if( !forceCompletion && \
503 ( envelopeInfoPtr->bufPos - envelopeInfoPtr->segmentDataStart ) < 10 )
508 envelopeInfoPtr->segmentDataEnd = envelopeInfoPtr->segmentStart;
515 status = encodeSegmentHeader( envelopeInfoPtr );
519 if( envelopeInfoPtr->iCryptContext !=
CRYPT_ERROR )
521 void *dataPtr = envelopeInfoPtr->buffer + \
522 envelopeInfoPtr->segmentDataStart;
523 const int dataLen = envelopeInfoPtr->bufPos - \
524 envelopeInfoPtr->segmentDataStart;
534 status = hashEnvelopeData( envelopeInfoPtr->actionList, dataPtr,
542 envelopeInfoPtr->segmentDataEnd = envelopeInfoPtr->bufPos;
566 REQUIRES( sanityCheck( envelopeInfoPtr ) );
571 envelopeInfoPtr->segmentSize != 0 )
574 #ifdef USE_COMPRESSION
589 status = beginSegment( envelopeInfoPtr );
592 if( envelopeInfoPtr->bufPos >= envelopeInfoPtr->bufSize )
597 bytesToCopy = envelopeInfoPtr->bufSize - envelopeInfoPtr->bufPos;
598 envelopeInfoPtr->zStream.next_in = NULL;
599 envelopeInfoPtr->zStream.avail_in = 0;
600 envelopeInfoPtr->zStream.next_out = envelopeInfoPtr->buffer + \
601 envelopeInfoPtr->bufPos;
602 envelopeInfoPtr->zStream.avail_out = bytesToCopy;
615 envelopeInfoPtr->bufPos += bytesToCopy - \
616 envelopeInfoPtr->zStream.avail_out;
617 ENSURES( envelopeInfoPtr->bufPos >= 0 && \
618 envelopeInfoPtr->bufPos <= envelopeInfoPtr->bufSize );
625 status = completeSegment( envelopeInfoPtr,
TRUE );
634 if( envelopeInfoPtr->blockSize > 1 )
641 needNewSegment =
TRUE;
653 status = beginSegment( envelopeInfoPtr );
656 if( envelopeInfoPtr->bufPos >= envelopeInfoPtr->bufSize )
668 status = completeSegment( envelopeInfoPtr,
TRUE );
689 return( hashEnvelopeData( envelopeInfoPtr->actionList,
"", 0 ) );
707 assert( length == 0 ||
isReadPtr( buffer, length ) );
709 REQUIRES( sanityCheck( envelopeInfoPtr ) );
710 REQUIRES( ( buffer == NULL && length == 0 ) || \
711 ( buffer != NULL && length >= 0 && length <
MAX_INTLENGTH ) );
716 if( envelopeInfoPtr->bufPos >= envelopeInfoPtr->bufSize )
729 status = hashEnvelopeData( envelopeInfoPtr->actionList,
731 buffer : (
const void * )
"", length );
740 status = flushEnvelopeData( envelopeInfoPtr );
747 length > envelopeInfoPtr->segmentSize )
754 status = beginSegment( envelopeInfoPtr );
757 if( envelopeInfoPtr->bufPos >= envelopeInfoPtr->bufSize )
766 bufPtr = envelopeInfoPtr->buffer + envelopeInfoPtr->bufPos;
767 bytesToCopy = envelopeInfoPtr->bufSize - envelopeInfoPtr->bufPos;
769 envelopeInfoPtr->bufPos + \
770 bytesToCopy <= envelopeInfoPtr->
bufSize );
771 #ifdef USE_COMPRESSION
775 envelopeInfoPtr->zStream.next_in = (
BYTE * ) buffer;
776 envelopeInfoPtr->zStream.avail_in =
length;
777 envelopeInfoPtr->zStream.next_out = bufPtr;
778 envelopeInfoPtr->zStream.avail_out = bytesToCopy;
789 envelopeInfoPtr->bufPos += bytesToCopy - \
790 envelopeInfoPtr->zStream.avail_out;
791 bytesToCopy = length - envelopeInfoPtr->zStream.avail_in;
795 if( envelopeInfoPtr->zStream.avail_out <= 0 )
796 needCompleteSegment =
TRUE;
802 if( bytesToCopy > length )
804 memcpy( bufPtr, buffer, bytesToCopy );
805 envelopeInfoPtr->bufPos += bytesToCopy;
810 status = hashEnvelopeData( envelopeInfoPtr->actionList, bufPtr,
818 if( bytesToCopy < length )
819 needCompleteSegment =
TRUE;
824 envelopeInfoPtr->segmentSize -= bytesToCopy;
826 ENSURES( sanityCheck( envelopeInfoPtr ) );
829 if( needCompleteSegment )
831 status = completeSegment( envelopeInfoPtr,
FALSE );
836 return( bytesToCopy );
854 IN_FLAGS( ENVCOPY )
const int flags )
859 assert( maxLength == 0 ||
isWritePtr( buffer, maxLength ) );
861 REQUIRES( sanityCheck( envelopeInfoPtr ) );
866 memset( buffer, 0,
min( 16, maxLength ) );
872 if( bytesToCopy > envelopeInfoPtr->segmentDataEnd )
882 const int status = completeSegment( envelopeInfoPtr,
FALSE );
888 if( envelopeInfoPtr->segmentDataEnd < bytesToCopy )
889 bytesToCopy = envelopeInfoPtr->segmentDataEnd;
891 remainder = envelopeInfoPtr->bufPos - bytesToCopy;
892 ENSURES( bytesToCopy >= 0 && bytesToCopy <= envelopeInfoPtr->bufPos );
893 ENSURES( remainder >= 0 && remainder <= envelopeInfoPtr->bufPos );
897 if( bytesToCopy > 0 )
899 memcpy( buffer, envelopeInfoPtr->buffer, bytesToCopy );
905 envelopeInfoPtr->bufPos ) );
906 memmove( envelopeInfoPtr->buffer,
907 envelopeInfoPtr->buffer + bytesToCopy, remainder );
909 envelopeInfoPtr->bufPos = remainder;
918 envelopeInfoPtr->segmentStart -= bytesToCopy;
919 if( envelopeInfoPtr->segmentStart < 0 )
920 envelopeInfoPtr->segmentStart = 0;
921 envelopeInfoPtr->segmentDataStart -= bytesToCopy;
922 if( envelopeInfoPtr->segmentDataStart < 0 )
923 envelopeInfoPtr->segmentDataStart = 0;
924 envelopeInfoPtr->segmentDataEnd -= bytesToCopy;
925 ENSURES( envelopeInfoPtr->segmentDataEnd >= 0 && \
928 *length = bytesToCopy;
930 ENSURES( sanityCheck( envelopeInfoPtr ) );
944 envelopeInfoPtr->copyToEnvelopeFunction = copyToEnvelope;
945 envelopeInfoPtr->copyFromEnvelopeFunction = copyFromEnvelope;