Skip to content

Commit

Permalink
Merge pull request #794 from Erior/feature/rar5-blake2
Browse files Browse the repository at this point in the history
Feature/rar5 blake2
  • Loading branch information
adamhathcock committed Jan 3, 2024
2 parents 2d4ce30 + 4f749da commit 741712f
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ I'm always looking for help or ideas. Please submit code or email with ideas. Un

## TODOs (always lots)

* RAR 5 decryption support
* RAR 5 decryption crc check support
* 7Zip writing
* Zip64 (Need writing and extend Reading)
* Multi-volume Zip support.
Expand Down
6 changes: 5 additions & 1 deletion src/SharpCompress/Archives/Rar/RarArchiveEntry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -40,7 +41,10 @@ public override long Crc
get
{
CheckIncomplete();
return parts.Select(fp => fp.FileHeader).Single(fh => !fh.IsSplitAfter).FileCrc;
return BitConverter.ToUInt32(
parts.Select(fp => fp.FileHeader).Single(fh => !fh.IsSplitAfter).FileCrc,
0
);
}
}

Expand Down
28 changes: 9 additions & 19 deletions src/SharpCompress/Common/Rar/Headers/FileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace SharpCompress.Common.Rar.Headers;

internal class FileHeader : RarHeader
{
private uint _fileCrc;
private byte[] _hash;

public FileHeader(RarHeader header, RarCrcBinaryReader reader, HeaderType headerType)
: base(header, reader, headerType) { }
Expand Down Expand Up @@ -53,7 +53,7 @@ private void ReadFromReaderV5(MarkingBinaryReader reader)

if (HasFlag(FileFlagsV5.HAS_CRC32))
{
FileCrc = reader.ReadUInt32();
FileCrc = reader.ReadBytes(4);
}

var compressionInfo = reader.ReadRarVIntUInt16();
Expand Down Expand Up @@ -136,14 +136,12 @@ private void ReadFromReaderV5(MarkingBinaryReader reader)

{
const uint FHEXTRA_HASH_BLAKE2 = 0x0;
const uint HASH_BLAKE2 = 0x03;
// const uint HASH_BLAKE2 = 0x03;
const int BLAKE2_DIGEST_SIZE = 0x20;
if ((uint)reader.ReadRarVInt() == FHEXTRA_HASH_BLAKE2)
{
var hash = HASH_BLAKE2;
var digest = reader.ReadBytes(BLAKE2_DIGEST_SIZE);

throw new InvalidFormatException("Not yet implemented " + hash);
// var hash = HASH_BLAKE2;
_hash = reader.ReadBytes(BLAKE2_DIGEST_SIZE);
}
// enum HASH_TYPE {HASH_NONE,HASH_RAR14,HASH_CRC32,HASH_BLAKE2};
}
Expand Down Expand Up @@ -242,7 +240,7 @@ private void ReadFromReaderV4(MarkingBinaryReader reader)

HostOs = reader.ReadByte();

FileCrc = reader.ReadUInt32();
FileCrc = reader.ReadBytes(4);

FileLastModifiedTime = Utility.DosDateToDateTime(reader.ReadUInt32());

Expand Down Expand Up @@ -414,18 +412,10 @@ private static string ConvertPathV4(string path)

private bool HasFlag(ushort flag) => (Flags & flag) == flag;

internal uint FileCrc
internal byte[] FileCrc
{
get
{
if (IsRar5 && !HasFlag(FileFlagsV5.HAS_CRC32))
{
//!!! rar5:
throw new InvalidOperationException("TODO rar5");
}
return _fileCrc;
}
private set => _fileCrc = value;
get => _hash;
private set => _hash = value;
}

// 0 - storing
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/Rar/RarEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class RarEntry : Entry
/// <summary>
/// The File's 32 bit CRC Hash
/// </summary>
public override long Crc => FileHeader.FileCrc;
public override long Crc => BitConverter.ToUInt32(FileHeader.FileCrc, 0);

/// <summary>
/// The path of the file internal to the Rar Archive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public override int Read(byte[] buffer, int offset, int count)

public override bool CanWrite => false;

public uint CurrentCrc { get; private set; }
public byte[] CurrentCrc { get; private set; }

public override void Flush() => throw new NotSupportedException();

Expand Down
Loading

0 comments on commit 741712f

Please sign in to comment.