API - Doug's checksum function doesn't seem to work for me

API - Doug's checksum function doesn't seem to work for me

Anonymous
Not applicable
1,896 Views
4 Replies
Message 1 of 5

API - Doug's checksum function doesn't seem to work for me

Anonymous
Not applicable

I'm developing an app (for Vault 2011) that compares the the checksum of a file in the vault against the same file locally (equivalent paths).  I've used a web site to converted Doug's CalcCRC32 function (from VaultMirror) from C# to VB.NET. 

There are 2 problems.  CalcCRC32 is designed to returns an integer but sometime the number is larger than an Integer can hold (see 1st image below).  Changing CalcCRC32 to return a Long will stop the error from occuring but I'm not sure if I should.  The other problem is, for a file I just brought down from the vault, CalcCRC32 does not return the same checksum as the File in the vault (see the 2nd image below).  The 1st number in the MsgBox is the checksum from the vault and the 2nd number is from CalcCRC32.  Don't let the asterisk next to BJR-2005.idw fool you, I haven't changed anything (common ?Inventor problem).  All the code is in the 3rd image below.

 

Did the conversion from C# to VB.NET go wrong or is there a flaw in the code?   Should CalcCRC32 return a Long?

 

!temp2.jpg

 

 

 

 

!temp1.jpg

 

 

 

Imports Autodesk.Connectivity.WebServices
Imports System.IO

Public Class Form1

    Private moSecSvc As Autodesk.Connectivity.WebServices.SecurityService
    Private moDocSvc As Autodesk.Connectivity.WebServices.DocumentService
    Private Const HOST As String = "L1429783"
    Private goInvApp As Inventor.Application
    Private goActiveDocument As Inventor.Document
    Private Shared MAX_FILE_SIZE As Long = 50 * 1024 * 1024    ' 50 MB 
    Private Const m_chunkSize As Integer = 16384
    Private Shared m_crc_32_tab As UInteger() = { _
         &H0, &H77073096, &HEE0E612CUI, &H990951BAUI, &H76DC419, &H706AF48F, _
         &HE963A535UI, &H9E6495A3UI, &HEDB8832, &H79DCB8A4, &HE0D5E91EUI, &H97D2D988UI, _
         &H9B64C2B, &H7EB17CBD, &HE7B82D07UI, &H90BF1D91UI, &H1DB71064, &H6AB020F2, _
         &HF3B97148UI, &H84BE41DEUI, &H1ADAD47D, &H6DDDE4EB, &HF4D4B551UI, &H83D385C7UI, _
         &H136C9856, &H646BA8C0, &HFD62F97AUI, &H8A65C9ECUI, &H14015C4F, &H63066CD9, _
         &HFA0F3D63UI, &H8D080DF5UI, &H3B6E20C8, &H4C69105E, &HD56041E4UI, &HA2677172UI, _
         &H3C03E4D1, &H4B04D447, &HD20D85FDUI, &HA50AB56BUI, &H35B5A8FA, &H42B2986C, _
         &HDBBBC9D6UI, &HACBCF940UI, &H32D86CE3, &H45DF5C75, &HDCD60DCFUI, &HABD13D59UI, _
         &H26D930AC, &H51DE003A, &HC8D75180UI, &HBFD06116UI, &H21B4F4B5, &H56B3C423, _
         &HCFBA9599UI, &HB8BDA50FUI, &H2802B89E, &H5F058808, &HC60CD9B2UI, &HB10BE924UI, _
         &H2F6F7C87, &H58684C11, &HC1611DABUI, &HB6662D3DUI, &H76DC4190, &H1DB7106, _
         &H98D220BCUI, &HEFD5102AUI, &H71B18589, &H6B6B51F, &H9FBFE4A5UI, &HE8B8D433UI, _
         &H7807C9A2, &HF00F934, &H9609A88EUI, &HE10E9818UI, &H7F6A0DBB, &H86D3D2D, _
         &H91646C97UI, &HE6635C01UI, &H6B6B51F4, &H1C6C6162, &H856530D8UI, &HF262004EUI, _
         &H6C0695ED, &H1B01A57B, &H8208F4C1UI, &HF50FC457UI, &H65B0D9C6, &H12B7E950, _
         &H8BBEB8EAUI, &HFCB9887CUI, &H62DD1DDF, &H15DA2D49, &H8CD37CF3UI, &HFBD44C65UI, _
         &H4DB26158, &H3AB551CE, &HA3BC0074UI, &HD4BB30E2UI, &H4ADFA541, &H3DD895D7, _
         &HA4D1C46DUI, &HD3D6F4FBUI, &H4369E96A, &H346ED9FC, &HAD678846UI, &HDA60B8D0UI, _
         &H44042D73, &H33031DE5, &HAA0A4C5FUI, &HDD0D7CC9UI, &H5005713C, &H270241AA, _
         &HBE0B1010UI, &HC90C2086UI, &H5768B525, &H206F85B3, &HB966D409UI, &HCE61E49FUI, _
         &H5EDEF90E, &H29D9C998, &HB0D09822UI, &HC7D7A8B4UI, &H59B33D17, &H2EB40D81, _
         &HB7BD5C3BUI, &HC0BA6CADUI, &HEDB88320UI, &H9ABFB3B6UI, &H3B6E20C, &H74B1D29A, _
         &HEAD54739UI, &H9DD277AFUI, &H4DB2615, &H73DC1683, &HE3630B12UI, &H94643B84UI, _
         &HD6D6A3E, &H7A6A5AA8, &HE40ECF0BUI, &H9309FF9DUI, &HA00AE27, &H7D079EB1, _
         &HF00F9344UI, &H8708A3D2UI, &H1E01F268, &H6906C2FE, &HF762575DUI, &H806567CBUI, _
         &H196C3671, &H6E6B06E7, &HFED41B76UI, &H89D32BE0UI, &H10DA7A5A, &H67DD4ACC, _
         &HF9B9DF6FUI, &H8EBEEFF9UI, &H17B7BE43, &H60B08ED5, &HD6D6A3E8UI, &HA1D1937EUI, _
         &H38D8C2C4, &H4FDFF252, &HD1BB67F1UI, &HA6BC5767UI, &H3FB506DD, &H48B2364B, _
         &HD80D2BDAUI, &HAF0A1B4CUI, &H36034AF6, &H41047A60, &HDF60EFC3UI, &HA867DF55UI, _
         &H316E8EEF, &H4669BE79, &HCB61B38CUI, &HBC66831AUI, &H256FD2A0, &H5268E236, _
         &HCC0C7795UI, &HBB0B4703UI, &H220216B9, &H5505262F, &HC5BA3BBEUI, &HB2BD0B28UI, _
         &H2BB45A92, &H5CB36A04, &HC2D7FFA7UI, &HB5D0CF31UI, &H2CD99E8B, &H5BDEAE1D, _
         &H9B64C2B0UI, &HEC63F226UI, &H756AA39C, &H26D930A, &H9C0906A9UI, &HEB0E363FUI, _
         &H72076785, &H5005713, &H95BF4A82UI, &HE2B87A14UI, &H7BB12BAE, &HCB61B38, _
         &H92D28E9BUI, &HE5D5BE0DUI, &H7CDCEFB7, &HBDBDF21, &H86D3D2D4UI, &HF1D4E242UI, _
         &H68DDB3F8, &H1FDA836E, &H81BE16CDUI, &HF6B9265BUI, &H6FB077E1, &H18B74777, _
         &H88085AE6UI, &HFF0F6A70UI, &H66063BCA, &H11010B5C, &H8F659EFFUI, &HF862AE69UI, _
         &H616BFFD3, &H166CCF45, &HA00AE278UI, &HD70DD2EEUI, &H4E048354, &H3903B3C2, _
         &HA7672661UI, &HD06016F7UI, &H4969474D, &H3E6E77DB, &HAED16A4AUI, &HD9D65ADCUI, _
         &H40DF0B66, &H37D83BF0, &HA9BCAE53UI, &HDEBB9EC5UI, &H47B2CF7F, &H30B5FFE9, _
         &HBDBDF21CUI, &HCABAC28AUI, &H53B39330, &H24B4A3A6, &HBAD03605UI, &HCDD70693UI, _
         &H54DE5729, &H23D967BF, &HB3667A2EUI, &HC4614AB8UI, &H5D681B02, &H2A6F2B94, _
         &HB40BBE37UI, &HC30C8EA1UI, &H5A05DF1B, &H2D02EF8D}


    Private Sub btnEditedOutOfTurn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditedOutOfTurn.Click
        moSecSvc = New SecurityService()
        moSecSvc.SecurityHeaderValue = New Autodesk.Connectivity.WebServices.SecuritySvc.SecurityHeader
        moSecSvc.Url = "http://" + HOST + "/AutodeskDM/Services/SecurityService.asmx"

        moSecSvc.SignInReadOnly("Administrator", "", "PCI PS Vault")  '"PCI PS Vault"

        moDocSvc = New DocumentService()
        moDocSvc.SecurityHeaderValue = New Autodesk.Connectivity.WebServices.DocumentSvc.SecurityHeader
        moDocSvc.SecurityHeaderValue.UserId = moSecSvc.SecurityHeaderValue.UserId
        moDocSvc.SecurityHeaderValue.Ticket = moSecSvc.SecurityHeaderValue.Ticket
        moDocSvc.Url = "http://" + HOST + "/AutodeskDM/Services/DocumentService.asmx"

        Dim sLocalRoot As String
        If moDocSvc.GetEnforceWorkingFolder Then
            sLocalRoot = moDocSvc.GetRequiredWorkingFolderLocation
        Else
            'In the future obtain the working folder from the XML file
            sLocalRoot = "C:\Local Workspace"
        End If

        goInvApp = GetObject(, "Inventor.Application")
        goActiveDocument = goInvApp.ActiveDocument

        Dim sRelativePathFile As String
        Dim sVaultPathFile As String
        Dim aoFiles() As Autodesk.Connectivity.WebServices.File
        Dim oFile As Autodesk.Connectivity.WebServices.File
        Dim oAllReferencedDocuments As Inventor.DocumentsEnumerator
        Dim sFile As String

        sFile = goActiveDocument.FullFileName
        sFile = Mid(sFile, InStrRev(sFile, "\") + 1)
        sRelativePathFile = Mid(goActiveDocument.FullFileName, Len(sLocalRoot) + 1)
        sVaultPathFile = Replace("$" & sRelativePathFile, "\", "/")
        Dim asFiles2() As String = {sVaultPathFile}
        aoFiles = moDocSvc.FindLatestFilesByPaths(asFiles2)
        oFile = aoFiles(0)
        MsgBox(sFile & "    " & oFile.Cksum & "    " & CalcCRC32(goActiveDocument.FullFileName))

        goActiveDocument = Nothing
        goInvApp = Nothing
        moSecSvc.SignOut()
    End Sub

    Public Shared Function CalcCRC32(ByVal filename As String) As Long
        Using stream As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, m_chunkSize)
            Return CalcCRC32(stream)
        End Using
    End Function

    Public Shared Function CalcCRC32(ByVal stream As Stream) As Long
        If stream Is Nothing Then
            Throw New Exception("Checksum.CalcCRC32() - Invalid stream")
        End If

        If Not stream.CanRead Then
            Throw New Exception("Checksum.CalcCRC32() - Cannot read stream")
        End If

        Dim crc32 As UInteger = &HFFFFFFFFUI

        stream.Position = 0
        ' beginning
        Dim bytesRead As Integer = 0
        Dim streamData As Byte() = New Byte(m_chunkSize - 1) {}
        Do
            bytesRead = stream.Read(streamData, 0, m_chunkSize)

            For i As Integer = 0 To bytesRead - 1
                crc32 = (m_crc_32_tab((crc32 Xor streamData(i)) And &HFF) Xor (crc32 >> 8))
            Next
        Loop While bytesRead > 0

        stream.Position = 0
        ' reset
        crc32 = Not crc32
        Return CLng(crc32)
    End Function

End Class

 

 

0 Likes
Accepted solutions (1)
1,897 Views
4 Replies
Replies (4)
Message 2 of 5

Redmond.D
Autodesk
Autodesk
Accepted solution

My guess is that there is a flaw in the conversion.  I have two suggestions.

 

1.  Check the Autodesk Developer Network to see if they have a VB version of the algorithm.

2.  Use the C# version of the checkusm algorithm.  You can keep the rest of your code in VB.  To do this, just create a new C# project and paste in the checksum code.  Your VB code can then just reference the C# project.

 



Doug Redmond
Software Engineer
Autodesk, Inc.

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks Doug.  I did the code below on the internet that works.

 

 

Public

ClassCRC32

 

'This is v2 of the VB CRC32 algorithm provided by Paul (wpsjr1@succeed.net) - 'much quicker than the nasty original version I posted. Excellent work!Private crc32Table() AsIntegerPrivateConst BUFFER_SIZE AsInteger= 1024

 

PublicFunction GetCrc32(ByRef stream As System.IO.Stream) AsIntegerDim crc32Result AsInteger= &HFFFFFFFF

 

Dim buffer(BUFFER_SIZE) AsByteDim readSize AsInteger= BUFFER_SIZE

 

Dim count AsInteger= stream.Read(buffer, 0, readSize)

 

Dim i AsIntegerDim iLookup AsIntegerDim tot AsInteger= 0

 

DoWhile(count > 0)

 

For i = 0 Tocount - 1

iLookup = (crc32Result

And &HFF) Xorbuffer(i)

crc32Result = ((crc32Result

And &HFFFFFF00) \ &H100) And &HFFFFFF ' nasty shr 8 with vb 😕

crc32Result = crc32Result

Xorcrc32Table(iLookup)

 

Nexti

count = stream.Read(buffer, 0, readSize)

Loop

GetCrc32 =

Not(crc32Result)

 

EndFunctionPublicSubNew()

 

'This is the official polynomial used by CRC32 in PKZip.'Often the polynomial is shown reversed (04C11DB7).Dim dwPolynomial AsInteger= &HEDB88320

 

Dim i AsInteger, j AsIntegerReDimcrc32Table(256)

 

Dim dwCrc AsIntegerFor i = 0 To255

dwCrc = i

For j = 8 To 1 Step-1

 

If (dwCrc And 1) Then

dwCrc = ((dwCrc

And &HFFFFFFFE) \ 2&) And&H7FFFFFFF

dwCrc = dwCrc

XordwPolynomial

 

Else

dwCrc = ((dwCrc

And &HFFFFFFFE) \ 2&) And&H7FFFFFFF

 

EndIfNextj

crc32Table(i) = dwCrc

Nexti

 

EndSub

End

Class

0 Likes
Message 4 of 5

AlexFielder
Advisor
Advisor

Hi,

 

I'm interested in reusing the converted class you posted, but something has gone wrong in the upload - any chance you can post the actual .vb class file?

 

Also, I'm using Vault Professional 2012 and can't find any mention of the getcrc32 class you mention in the VaultMirror solution files?

 

Thanks,

 

Alex.

0 Likes
Message 5 of 5

Redmond.D
Autodesk
Autodesk

This post contains the original C# code:  http://forums.autodesk.com/t5/Vault-General/Code-snippets-Vault-API/td-p/1714611

It's in the .zip file from the second posting.



Doug Redmond
Software Engineer
Autodesk, Inc.

0 Likes