ON__UINT32 ON_ArcCurve::DataCRC(ON__UINT32 current_remainder) const { current_remainder = ON_CRC32(current_remainder,sizeof(m_arc),&m_arc); current_remainder = ON_CRC32(current_remainder,sizeof(m_t),&m_t); current_remainder = ON_CRC32(current_remainder,sizeof(m_dim),&m_dim); return current_remainder; }
ON__UINT32 ON_PlaneSurface::DataCRC(ON__UINT32 current_remainder) const { current_remainder = ON_CRC32(current_remainder,sizeof(m_plane),&m_plane); current_remainder = ON_CRC32(current_remainder,2*sizeof(m_domain[0]),&m_domain[0]); current_remainder = ON_CRC32(current_remainder,2*sizeof(m_extents[0]),&m_extents[0]); return current_remainder; }
ON_MappingTag CZAnalysisVAM::MappingTag() const { ON_MappingTag mt; // Since the false colors that are shown will change if // the mesh is transformed, we have to initialize the // transformation. mt.m_mesh_xform.Identity(); // This is the analysis mode id passed to the // CRhinoVisualAnalysisMode constructor. Use the // m_am_id member and it this code will alwasy // work correctly. mt.m_mapping_id = m_am_id; // This is a 32 bit CRC or the information used to // set the false colors. // For this example, the m_z_range and m_hue_range // intervals controlthe colors, so we calculate // their crc. mt.m_mapping_crc = 0; mt.m_mapping_crc = ON_CRC32(mt.m_mapping_crc,sizeof(m_z_range),&m_z_range); mt.m_mapping_crc = ON_CRC32(mt.m_mapping_crc,sizeof(m_hue_range),&m_hue_range); return mt; }
ON__UINT32 ON_CurveProxy::DataCRC(ON__UINT32 current_remainder) const { if ( m_real_curve ) current_remainder = m_real_curve->DataCRC(current_remainder); current_remainder = ON_CRC32(current_remainder,sizeof(m_bReversed),&m_bReversed); current_remainder = ON_CRC32(current_remainder,sizeof(m_real_curve_domain),&m_real_curve_domain); current_remainder = ON_CRC32(current_remainder,sizeof(m_this_domain),&m_this_domain); return current_remainder; }
bool ON_CheckSum::SetFileCheckSum( FILE* fp ) { bool rc = false; Zero(); if ( fp ) { size_t filesize = 0; time_t filetime = 0; if ( ON::GetFileStats(fp,&filesize,NULL,&filetime) ) { m_time = filetime; } unsigned char buffer[1024]; int count=1024; ON__INT32 crc = 0; size_t sz0 = 0, maxsize = 0x40000; for( int i = 0; i < 7; i++ ) { sz0 += maxsize; while(1024 == count && m_size < sz0) { count = (int)fread( buffer, 1, 1024, fp ); // the (int) is for 64 bit size_t conversion if ( count > 0 ) { m_size += count; crc = ON_CRC32( crc, count, buffer ); } } maxsize *= 2; m_crc[i] = crc; } while(1024 == count) { count = (int)fread( buffer, 1, 1024, fp ); // the (int) is for 64 bit size_t conversion if ( count > 0 ) { m_size += count; crc = ON_CRC32( crc, count, buffer ); } } m_crc[7] = crc; rc = (filesize == m_size); } return rc; }
ON__UINT32 ON_SurfaceProxy::DataCRC(ON__UINT32 current_remainder) const { if ( m_surface ) current_remainder = m_surface->DataCRC(current_remainder); current_remainder = ON_CRC32(current_remainder,sizeof(m_bTransposed),&m_bTransposed); return current_remainder; }
ON__UINT32 ON_String::DataCRC(ON__UINT32 current_remainder) const { int string_length = Header()->string_length; if ( string_length > 0 ) { current_remainder = ON_CRC32(current_remainder,string_length*sizeof(*m_s),m_s); } return current_remainder; }
bool ON_BinaryArchive::WriteCompressedBuffer( size_t sizeof__inbuffer, // sizeof uncompressed input data const void* inbuffer // uncompressed input data ) { size_t compressed_size = 0; bool rc = false; if ( !WriteMode() ) return false; if ( sizeof__inbuffer > 0 && 0 == inbuffer ) return false; // number of bytes of uncompressed data if (!WriteSize(sizeof__inbuffer)) return false; if ( 0 == sizeof__inbuffer ) return true; // 32 bit crc of uncompressed data const unsigned int buffer_crc = ON_CRC32( 0, sizeof__inbuffer, inbuffer ); if (!WriteInt(buffer_crc)) return false; unsigned char method = (sizeof__inbuffer > 128) ? 1 : 0; if ( method ) { if ( !CompressionInit() ) { CompressionEnd(); method = 0; } } if ( !WriteChar(method) ) return false; switch ( method ) { case 0: // uncompressed rc = WriteByte(sizeof__inbuffer, inbuffer); if ( rc ) { compressed_size = sizeof__inbuffer; } break; case 1: // compressed compressed_size = WriteDeflate( sizeof__inbuffer, inbuffer ); rc = ( compressed_size > 0 ) ? true : false; CompressionEnd(); break; } return rc; }
bool ON_BinaryArchive::ReadCompressedBuffer( // read and uncompress size_t sizeof__outbuffer, // sizeof of uncompressed buffer to read void* outbuffer, // uncompressed output data returned here int* bFailedCRC ) { bool rc = false; unsigned int buffer_crc0 = 0; unsigned int buffer_crc1 = 0; char method = 0; if ( bFailedCRC) *bFailedCRC = false; if ( !ReadMode() ) return false; if ( 0 == sizeof__outbuffer ) return true; if ( 0 == outbuffer ) return false; if ( !ReadInt(&buffer_crc0) ) // 32 bit crc of uncompressed buffer return false; if ( !ReadChar(&method) ) return false; if ( method != 0 && method != 1 ) return false; switch(method) { case 0: // uncompressed rc = ReadByte(sizeof__outbuffer, outbuffer); break; case 1: // compressed rc = CompressionInit(); if (rc) rc = ReadInflate( sizeof__outbuffer, outbuffer ); CompressionEnd(); break; } if (rc ) { buffer_crc1 = ON_CRC32( 0, sizeof__outbuffer, outbuffer ); if ( buffer_crc1 != buffer_crc0 ) { ON_ERROR("ON_BinaryArchive::ReadCompressedBuffer() crc error"); if ( bFailedCRC ) *bFailedCRC = true; } } return rc; }
bool ON_CheckSum::SetBufferCheckSum( size_t size, const void* buffer, time_t time ) { bool rc = false; Zero(); if ( size != 0 && buffer != 0 ) { m_size = (unsigned int)size; ON__INT32 crc = 0; size_t sz, maxsize = 0x40000; const unsigned char* p = (const unsigned char*)buffer; for ( int i = 0; i < 7; i++ ) { if ( size > 0 ) { sz = (size > maxsize) ? maxsize : size; crc = ON_CRC32(crc,sz,p); p += sz; size -= sz; maxsize *= 2; } m_crc[i] = crc; } if ( size > 0 ) { crc = ON_CRC32(crc,size,p); } m_crc[7] = crc; rc = true; } else if ( 0 == size ) { rc = true; } m_time = time; return rc; }
bool ON_CheckSum::CheckBuffer( size_t size, const void* buffer ) const { if ( m_size != size ) return false; if ( 0 == size ) return true; if ( 0 == buffer ) return false; ON__UINT32 crc = 0; size_t sz, maxsize = 0x40000; const unsigned char* p = (const unsigned char*)buffer; for ( int i = 0; i < 7; i++ ) { if ( size > 0 ) { sz = (size > maxsize) ? maxsize : size; crc = ON_CRC32(crc,sz,p); p += sz; size -= sz; maxsize *= 2; } if ( m_crc[i] != crc ) return false; } if ( size > 0 ) { crc = ON_CRC32(crc,size,p); } if ( m_crc[7] != crc ) return false; return true; }
bool ON_UncompressStream::End() { if ( 0 == m_implementation ) { ErrorHandler(); return false; } struct ON_ZlibImplementation* imp = (struct ON_ZlibImplementation*)m_implementation; z_stream& strm = imp->m_strm; if ( 0 != strm.avail_in || 0 != strm.next_in ) { // strm.avail_in is always zero when we leave an ON_UncompressStream function. ErrorHandler(); return false; } const ON__UINT32 sizeof_out_buffer = (ON__UINT32)(sizeof(imp->m_zlib_out_buffer)); void* out_buffer = imp->m_zlib_out_buffer; int zrc = Z_OK; bool rc = false; ON__UINT32 inflate_output_count; // counter prevents infinte loops if there is a bug in zlib return codes. for( int counter = 512; counter > 0; counter-- ) { // provide storage for compressed stream output strm.avail_in = 0; strm.next_in = 0; strm.next_out = (z_Bytef*)out_buffer; strm.avail_out = sizeof_out_buffer; // finish compression calculation zrc = z_inflate( &strm, Z_FINISH ); if ( zrc < 0 ) { // Something went haywire - bail out. ErrorHandler(); rc = false; break; } inflate_output_count = sizeof_out_buffer - strm.avail_out; if ( inflate_output_count > 0 ) { // The last call to inflate created uncompressed output. // Send the output to the uncompressed stream handler. // Calculate the updated crc and size before we call // the output handler because someday sombody will // decide it's a good idea to modify the values // in the buffer argument. ON__UINT32 out_crc1 = ON_CRC32( m_out_crc, inflate_output_count, out_buffer); ON__UINT64 out_size1 = m_out_size + inflate_output_count; rc = (0 != m_out_callback_function) ? m_out_callback_function( m_out_callback_context, inflate_output_count, out_buffer ) : Out( m_out_callback_context, inflate_output_count, out_buffer ); if ( !rc ) break; // Update compressed stream crc and size m_out_crc = out_crc1; m_out_size = out_size1; counter = 512; // created output - reset counter that detects stalls } if ( Z_STREAM_END == zrc ) { // no input left, all pending compressing is finished, // and all compressed output has been returned. rc = true; break; } } strm.avail_in = 0; strm.next_in = 0; strm.next_out = 0; strm.avail_out = 0; inflateEnd(&strm); onfree(m_implementation); m_implementation = 0; return rc; }
bool ON_CheckSum::CheckFile( FILE* fp, bool bSkipTimeCheck ) const { if ( !fp ) return false; size_t filesize=0; time_t filetime=0; if ( ON::GetFileStats( fp, &filesize, NULL, &filetime ) ) { if ( m_size != filesize ) { return false; } if ( !bSkipTimeCheck && m_time != filetime) { return false; } } unsigned char buffer[1024]; int count=1024; ON__UINT32 crc = 0; size_t sz0 = 0, maxsize = 0x40000; size_t sz = 0; for( int i = 0; i < 7; i++ ) { sz0 += maxsize; while(1024 == count && sz < sz0) { count = (int)fread( buffer, 1, 1024, fp ); // the (int) is for 64 bit size_t conversion if ( count > 0 ) { sz += count; crc = ON_CRC32( crc, count, buffer ); } } maxsize *= 2; if ( m_crc[i] != crc ) return false; } while(1024 == count) { count = (int)fread( buffer, 1, 1024, fp ); // the (int) is for 64 bit size_t conversion if ( count > 0 ) { sz += count; crc = ON_CRC32( crc, count, buffer ); } } if (m_crc[7] != crc) return false; if ( sz != m_size ) return false; return true; }
RH_C_FUNCTION unsigned int ON_CRC32_Compute(unsigned int current_remainder, int count, /*ARRAY*/ const char* bytes) { return ON_CRC32(current_remainder, count, bytes); }
bool ON_CompressedBuffer::Compress( size_t sizeof__inbuffer, // sizeof uncompressed input data const void* inbuffer, // uncompressed input data int sizeof_element ) { Destroy(); //size_t compressed_size = 0; bool rc = false; if ( sizeof__inbuffer > 0 && 0 == inbuffer ) return false; if ( 0 == sizeof__inbuffer ) return true; // number of bytes of uncompressed data m_sizeof_uncompressed = sizeof__inbuffer; ON_CompressedBufferHelper helper; memset(&helper,0,sizeof(helper)); helper.action = 1; bool bToggleByteOrder = false; switch(sizeof_element) { case 2: case 4: case 8: if ( 0 == (sizeof__inbuffer%sizeof_element) ) { m_sizeof_element = sizeof_element; bToggleByteOrder = (ON::big_endian == ON::Endian()); } break; }; if ( bToggleByteOrder ) { ON_BinaryFile::ToggleByteOrder( (int)(sizeof__inbuffer/m_sizeof_element), m_sizeof_element, inbuffer, (void*)inbuffer ); } m_method = (sizeof__inbuffer > 128) ? 1 : 0; if ( m_method ) { if ( !CompressionInit(&helper) ) { CompressionEnd(&helper); m_method = 0; } else { m_buffer_compressed = onmalloc(sizeof__inbuffer/4); size_t sizeof_compressed = DeflateHelper( &helper, sizeof__inbuffer, inbuffer ); CompressionEnd(&helper); if ( sizeof_compressed > 0 && sizeof_compressed == m_sizeof_compressed ) { rc = true; if ( 2*m_buffer_compressed_capacity > 3*m_sizeof_compressed ) { // release memory we don't need m_buffer_compressed_capacity = m_sizeof_compressed; m_buffer_compressed = onrealloc(m_buffer_compressed,m_buffer_compressed_capacity); } } else { Destroy(); m_method = 0; } } } if ( 0 == m_method ) { // uncompressed m_buffer_compressed = onmalloc(sizeof__inbuffer); if ( m_buffer_compressed ) { m_sizeof_compressed = sizeof__inbuffer; m_buffer_compressed_capacity = sizeof__inbuffer; memcpy(m_buffer_compressed,inbuffer,sizeof__inbuffer); rc = true; } } if ( bToggleByteOrder ) { ON_BinaryFile::ToggleByteOrder( (int)(sizeof__inbuffer/m_sizeof_element), m_sizeof_element, inbuffer, (void*)inbuffer ); } if (rc) { m_crc_uncompressed = ON_CRC32( 0, sizeof__inbuffer, inbuffer ); m_crc_compressed = ON_CRC32( 0, m_sizeof_compressed, m_buffer_compressed ); } return rc; }
bool ON_CompressedBuffer::Uncompress( void* outbuffer, int* bFailedCRC ) const { bool rc = false; if ( bFailedCRC) *bFailedCRC = false; if ( 0 == m_sizeof_uncompressed ) return true; if ( 0 == outbuffer ) return false; if ( m_method != 0 && m_method != 1 ) return false; ON__UINT32 compressed_crc = ON_CRC32( 0, m_sizeof_compressed, m_buffer_compressed ); if ( compressed_crc != m_crc_compressed ) { // m_buffer_compressed is corrupt - let's hope the corruption // is near the end and we ge something useful from the // beginning. memset(outbuffer,0,m_sizeof_uncompressed); if ( bFailedCRC) *bFailedCRC = false; } switch(m_method) { case 0: // uncompressed if ( m_buffer_compressed && m_sizeof_uncompressed == m_sizeof_compressed ) { memcpy(outbuffer,m_buffer_compressed,m_sizeof_uncompressed); rc = true; } break; case 1: // compressed { ON_CompressedBufferHelper helper; memset(&helper,0,sizeof(helper)); helper.action = 2; rc = CompressionInit(&helper); if (rc) { rc = InflateHelper( &helper, m_sizeof_uncompressed, outbuffer ); CompressionEnd(&helper); } } break; } switch(m_sizeof_element) { case 2: case 4: case 8: if ( 0 == (m_sizeof_uncompressed%m_sizeof_element) ) { if ( ON::big_endian == ON::Endian() ) { ON_BinaryFile::ToggleByteOrder( (int)(m_sizeof_uncompressed/m_sizeof_element), m_sizeof_element, outbuffer, outbuffer ); } } break; }; if (rc ) { ON__UINT32 uncompressed_crc = ON_CRC32( 0, m_sizeof_uncompressed, outbuffer ); if ( uncompressed_crc != m_crc_uncompressed ) { ON_ERROR("ON_CompressedBuffer::Uncompress() crc error"); if ( bFailedCRC ) *bFailedCRC = true; } } return rc; }
ON__UINT32 ON_ClippingPlaneSurface::DataCRC(ON__UINT32 current_remainder) const { ON__UINT32 crc = ON_PlaneSurface::DataCRC(current_remainder); crc = ON_CRC32(crc,sizeof(m_clipping_plane),&m_clipping_plane); return crc; }
bool ON_CompressStream::In( ON__UINT64 size, const void* uncompressed_buffer ) { if ( size <= 0 ) return true; if ( 0 == m_implementation ) { ErrorHandler(); return false; } if ( 0 == uncompressed_buffer ) { ErrorHandler(); return false; } struct ON_ZlibImplementation* imp = (struct ON_ZlibImplementation*)m_implementation; z_stream& strm = imp->m_strm; if ( 0 != strm.avail_in || 0 != strm.next_in ) { // strm.avail_in is always zero when we leave an ON_CompressStream function. ErrorHandler(); return false; } const ON__UINT32 sizeof_out_buffer = (ON__UINT32)(sizeof(imp->m_zlib_out_buffer)); void* out_buffer = imp->m_zlib_out_buffer; int zrc = Z_OK; const ON__UINT64 max_sz = 0x7FFFFFF0; bool rc = false; ON__UINT32 deflate_output_count; // counter prevents infinte loops if there is a bug in zlib return codes. for( int counter = 512; counter > 0; counter-- ) { // Call zlib's deflate function. It can either process // more input from m_zlib.strm.next_in[], create more // compressed output in m_zlib.strm.next_out[], or do both. // provide storage for compressed stream output strm.next_out = (z_Bytef*)out_buffer; strm.avail_out = sizeof_out_buffer; if ( strm.avail_in <= 0 ) { if ( size <= 0 ) { // finshed with uncompressed input break; } // submit a portion of uncompressed_buffer to zlib ON__UINT64 sz = (size > max_sz) ? max_sz : size; m_in_size += sz; m_in_crc = ON_CRC32(m_in_crc,(size_t)sz,uncompressed_buffer); // (size_t) cast is safe because sz <= max_sz = 0x7FFFFFF0 strm.next_in = (z_Bytef*)uncompressed_buffer; strm.avail_in = (ON__UINT32)sz; uncompressed_buffer = ((const unsigned char*)uncompressed_buffer) + sz; size -= sz; counter = 512; // added input - reset the counter that detects stalls } // calculate compression ON__UINT32 avail_in0 = strm.avail_in; ON__UINT32 avail_out0 = strm.avail_out; zrc = z_deflate( &strm, Z_NO_FLUSH ); if ( zrc < 0 ) { // Something went haywire - bail out. ErrorHandler(); rc = false; break; } if ( strm.avail_in < avail_in0 || strm.avail_out > avail_out0 ) { // zlib did something rc = true; } deflate_output_count = sizeof_out_buffer - strm.avail_out; if ( deflate_output_count > 0 ) { // The last call to deflate created compressed output. // Send the output to compressed stream handler. // Calculate the updated crc and size before we call // the output handler because someday sombody will // decide it's a good idea to modify the values // in the buffer argument. ON__UINT32 out_crc1 = ON_CRC32( m_out_crc, deflate_output_count, out_buffer); ON__UINT64 out_size1 = m_out_size + deflate_output_count; rc = (0 != m_out_callback_function) ? m_out_callback_function( m_out_callback_context, deflate_output_count, out_buffer ) : Out( m_out_callback_context, deflate_output_count, out_buffer ); if ( !rc ) break; // Update compressed stream crc and size m_out_crc = out_crc1; m_out_size = out_size1; counter = 512; // created output - reset counter that detects stalls } if ( size <= 0 && strm.avail_in <= 0 ) { // no input left break; } } strm.avail_in = 0; strm.next_in = 0; strm.next_out = 0; strm.avail_out = 0; return rc; }
// virtual ON_Object override ON__UINT32 ON__IDefAlternativePathUserData::DataCRC(ON__UINT32 current_remainder) const { ON__UINT32 crc = ON_CRC32(current_remainder,sizeof(m_bRelativePath),&m_bRelativePath); crc = m_alternate_path.DataCRC(crc); return crc; }