crc32.bmx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. '
  2. ' Copyright (C) 2019-2022 Bruce A Henderson
  3. '
  4. ' This software is provided 'as-is', without any express or implied
  5. ' warranty. In no event will the authors be held liable for any damages
  6. ' arising from the use of this software.
  7. '
  8. ' Permission is granted to anyone to use this software for any purpose,
  9. ' including commercial applications, and to alter it and redistribute it
  10. ' freely, subject to the following restrictions:
  11. '
  12. ' 1. The origin of this software must not be misrepresented; you must not
  13. ' claim that you wrote the original software. If you use this software
  14. ' in a product, an acknowledgment in the product documentation would be
  15. ' appreciated but is not required.
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. ' 3. This notice may not be removed or altered from any source distribution.
  19. '
  20. SuperStrict
  21. Rem
  22. bbdoc: CRC32 Redundency check
  23. End Rem
  24. Module Crypto.CRC32
  25. ModuleInfo "CC_OPTS: -DLTC_NO_TEST -DLTC_NO_FILE"
  26. Import "common.bmx"
  27. New TCRC32Register
  28. Rem
  29. bbdoc: A CRC32 function.
  30. End Rem
  31. Type TCRC32 Extends TMessageDigest
  32. Method New()
  33. digestPtr = bmx_digest_crc32_init()
  34. End Method
  35. Method OutBytes:Int() Override
  36. Return 4
  37. End Method
  38. Rem
  39. bbdoc: Updates the calculation with @dataLen bytes of data.
  40. End Rem
  41. Method Update:Int(data:Byte Ptr, dataLen:Int) Override
  42. bmx_digest_crc32_update(digestPtr, data, dataLen)
  43. End Method
  44. Rem
  45. bbdoc: Calculates the CRC for the given #String, setting the value in @result.
  46. End Rem
  47. Method Digest(txt:String, result:Int Var)
  48. Local buf:Byte Ptr = txt.ToUTF8String()
  49. Update(buf, Int(strlen_(buf)))
  50. MemFree(buf)
  51. Finish(result)
  52. End Method
  53. Rem
  54. bbdoc: Calculates the CRC for the given #TStream, setting the value in @result.
  55. End Rem
  56. Method Digest(stream:TStream, result:Int Var)
  57. Local buf:Byte[8192]
  58. Local bytesRead:Long
  59. Repeat
  60. bytesRead = stream.Read(buf, buf.length)
  61. If bytesRead Then
  62. Update(buf, Int(bytesRead))
  63. End If
  64. Until bytesRead <= 0
  65. If Not bytesRead Then
  66. Finish(result)
  67. End If
  68. End Method
  69. Rem
  70. bbdoc: Finishes calculation and produces the result, filling @result with the calculated bytes.
  71. about: The state is reset, ready to create a new calculation.
  72. End Rem
  73. Method Finish:Int(result:Byte[]) Override
  74. Assert result.length >= 4, "Byte array must be at least 4 bytes."
  75. bmx_digest_crc32_finish(digestPtr, result, 4)
  76. End Method
  77. Rem
  78. bbdoc: Finishes calculation and produces the result, setting @result with the result.
  79. about: The state is reset, ready to create a new calculation.
  80. End Rem
  81. Method Finish:Int(result:Int Var)
  82. bmx_digest_crc32_finish_int(digestPtr, result)
  83. End Method
  84. End Type
  85. Type TCRC32Register Extends TDigestRegister
  86. Method GetDigest:TMessageDigest( name:String ) Override
  87. If name.ToUpper() = "CRC32" Then
  88. Return New TCRC32
  89. End If
  90. End Method
  91. Method ToString:String() Override
  92. Return "CRC32"
  93. End Method
  94. End Type