Skip to content

Commit 4da5e69

Browse files
committed
blobonly option replaced to dataarray option, added maxblocksize option
1 parent 32a909c commit 4da5e69

File tree

5 files changed

+75
-54
lines changed

5 files changed

+75
-54
lines changed

Blazer.Exe/CommandLine/BlazerCommandLineOptions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ public class BlazerCommandLineOptions
3535
[CommandLineOption("mode", "Compression mode: none, block (default), stream, streamhigh")]
3636
public string Mode { get; set; }
3737

38-
[CommandLineOption("blobonly", "Compress to blob (no header and footer)")]
39-
public bool BlobOnly { get; set; }
38+
[CommandLineOption("maxblocksize", "Specifies maximum size of data chunk")]
39+
public string MaxBlockSize { get; set; }
40+
41+
[CommandLineOption("dataarray", "Compress to solid array with 4-bytes length prefix")]
42+
public bool DataArray { get; set; }
4043

4144
[CommandLineOption("comment", "Add comment to archive")]
4245
public string Comment { get; set; }

Blazer.Exe/Program.cs

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,41 @@ private static int ProcessCompress(CommandLineParser<BlazerCommandLineOptions> o
149149
}
150150
else throw new InvalidOperationException("Invalid compression mode");
151151

152-
if (opt.BlobOnly)
152+
if (!string.IsNullOrEmpty(opt.MaxBlockSize))
153153
{
154-
compressionOptions.IncludeCrc = false;
155-
compressionOptions.IncludeFooter = false;
156-
compressionOptions.IncludeHeader = false;
157-
compressionOptions.MaxBlockSize = 1 << 24;
158-
compressionOptions.FileInfo = null;
154+
if (opt.MaxBlockSize.All(char.IsDigit)) compressionOptions.MaxBlockSize = Convert.ToInt32(opt.MaxBlockSize);
155+
else
156+
{
157+
BlazerFlags flagsBlockSize;
158+
if (Enum.TryParse("InBlockSize" + opt.MaxBlockSize, true, out flagsBlockSize))
159+
compressionOptions.SetMaxBlockSizeFromFlags(flagsBlockSize);
160+
}
159161
}
160162

161163
if (truncateOutFile)
162164
new FileStream(archiveName, FileMode.Truncate, FileAccess.Write).Close();
163165

164166
var outStream = opt.Stdout ? Console.OpenStandardOutput() : new FileStream(archiveName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
165167

166-
Stream blazerStream = new BlazerInputStream(outStream, compressionOptions);
168+
Stream blazerStream = opt.DataArray ? (Stream)new MemoryStream() : new BlazerInputStream(outStream, compressionOptions);
167169

168170
using (var inFile = new StatStream(opt.Stdin ? Console.OpenStandardInput() : File.OpenRead(fileName), !opt.Stdout))
169171
using (var outFile = blazerStream)
170172
{
171173
inFile.CopyTo(outFile);
172174
}
173175

176+
if (opt.DataArray)
177+
{
178+
var sourceData = (blazerStream as MemoryStream).ToArray();
179+
var encoder = compressionOptions.Encoder;
180+
encoder.Init(sourceData.Length);
181+
var res = encoder.Encode(sourceData, 0, sourceData.Length);
182+
outStream.Write(new[] { (byte)sourceData.Length, (byte)(sourceData.Length >> 8), (byte)(sourceData.Length >> 16), (byte)(sourceData.Length >> 24) }, 0, 4);
183+
outStream.Write(res.Buffer, res.Offset, res.Count);
184+
outStream.Close();
185+
}
186+
174187
return 0;
175188
}
176189

@@ -212,34 +225,39 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
212225

213226
var decOptions = new BlazerDecompressionOptions(opt.Password) { EncyptFull = opt.EncryptFull };
214227

215-
if (opt.BlobOnly)
216-
{
217-
decOptions.CompressionOptions = new BlazerCompressionOptions
218-
{
219-
IncludeCrc = false,
220-
IncludeFooter = false,
221-
IncludeHeader = false,
222-
FileInfo = null,
223-
MaxBlockSize = 1 << 24
224-
};
228+
BlazerOutputStream outBlazerStream = null;
229+
Stream outStream = null;
225230

231+
if (opt.DataArray)
232+
{
226233
var mode = (opt.Mode ?? "block").ToLowerInvariant();
227234
if (mode == "stream" || mode == "streamhigh") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.Stream);
228235
else if (mode == "none") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.NoCompress);
229236
else if (mode == "block") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.Block);
230237
else throw new InvalidOperationException("Unsupported mode");
238+
var ms = new MemoryStream();
239+
inStreamSource.CopyTo(ms);
240+
var comprArray = ms.ToArray();
241+
var uncomprLength = comprArray[0] | (comprArray[1] << 8) | (comprArray[2] << 16) | (comprArray[3] << 24);
242+
var decoder = decOptions.Decoder;
243+
decoder.Init(uncomprLength);
244+
var decoded = decoder.Decode(comprArray, 4, comprArray.Length, true);
245+
outStream = new MemoryStream(decoded.Buffer, decoded.Offset, decoded.Count);
246+
}
247+
else
248+
{
249+
outBlazerStream = new BlazerOutputStream(inStreamSource, decOptions);
250+
outStream = outBlazerStream;
231251
}
232-
233-
var outStream = new BlazerOutputStream(inStreamSource, decOptions);
234252

235253
var fileName = archiveName;
236254
var applyFileInfoAfterComplete = false;
237255
if (archiveName.EndsWith(".blz")) fileName = fileName.Substring(0, fileName.Length - 4);
238256
else fileName += ".unpacked";
239257

240-
if (outStream.FileInfo != null && !opt.NoFileName)
258+
if (outBlazerStream != null && outBlazerStream.FileInfo != null && !opt.NoFileName)
241259
{
242-
fileName = outStream.FileInfo.FileName;
260+
fileName = outBlazerStream.FileInfo.FileName;
243261
applyFileInfoAfterComplete = true;
244262
}
245263

@@ -263,7 +281,7 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
263281
inFile.CopyTo(outFile);
264282
}
265283

266-
if (applyFileInfoAfterComplete) outStream.FileInfo.ApplyToFile();
284+
if (applyFileInfoAfterComplete) outBlazerStream.FileInfo.ApplyToFile();
267285

268286
return 0;
269287
}
@@ -291,25 +309,29 @@ private static int ProcessTest(CommandLineParser<BlazerCommandLineOptions> optio
291309

292310
var decOptions = new BlazerDecompressionOptions(opt.Password) { EncyptFull = opt.EncryptFull };
293311

294-
if (opt.BlobOnly)
295-
{
296-
decOptions.CompressionOptions = new BlazerCompressionOptions
297-
{
298-
IncludeCrc = false,
299-
IncludeFooter = false,
300-
IncludeHeader = false,
301-
FileInfo = null,
302-
MaxBlockSize = 1 << 24
303-
};
312+
Stream outStream;
304313

314+
if (opt.DataArray)
315+
{
305316
var mode = (opt.Mode ?? "block").ToLowerInvariant();
306317
if (mode == "stream" || mode == "streamhigh") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.Stream);
307318
else if (mode == "none") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.NoCompress);
308319
else if (mode == "block") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.Block);
309320
else throw new InvalidOperationException("Unsupported mode");
321+
var ms = new MemoryStream();
322+
inStreamSource.CopyTo(ms);
323+
var comprArray = ms.ToArray();
324+
var uncomprLength = comprArray[0] | (comprArray[1] << 8) | (comprArray[2] << 16) | (comprArray[3] << 24);
325+
var decoder = decOptions.Decoder;
326+
decoder.Init(uncomprLength);
327+
// if we do not fail - all ok
328+
var decoded = decoder.Decode(comprArray, 4, comprArray.Length, true);
329+
outStream = new MemoryStream(decoded.Buffer, decoded.Offset, decoded.Count);
330+
}
331+
else
332+
{
333+
outStream = new BlazerOutputStream(inStreamSource, decOptions);
310334
}
311-
312-
var outStream = new BlazerOutputStream(inStreamSource, decOptions);
313335

314336
using (var inFile = outStream)
315337
using (var outFile = new StatStream(new NullStream(), true))
@@ -346,22 +368,10 @@ private static int ProcessList(CommandLineParser<BlazerCommandLineOptions> optio
346368

347369
var decOptions = new BlazerDecompressionOptions(opt.Password) { EncyptFull = opt.EncryptFull };
348370

349-
if (opt.BlobOnly)
371+
if (opt.DataArray)
350372
{
351-
decOptions.CompressionOptions = new BlazerCompressionOptions
352-
{
353-
IncludeCrc = false,
354-
IncludeFooter = false,
355-
IncludeHeader = false,
356-
FileInfo = null,
357-
MaxBlockSize = 1 << 24
358-
};
359-
360-
var mode = (opt.Mode ?? "block").ToLowerInvariant();
361-
if (mode == "stream" || mode == "streamhigh") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.Stream);
362-
else if (mode == "none") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.NoCompress);
363-
else if (mode == "block") decOptions.SetDecoderByAlgorithm(BlazerAlgorithm.Block);
364-
else throw new InvalidOperationException("Unsupported mode");
373+
Console.WriteLine("Data array does not contain file info");
374+
return 1;
365375
}
366376

367377
// format is simplified 7z output

Blazer.Exe/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("0.8.0.0")]
35-
[assembly: AssemblyFileVersion("0.8.1.7")]
34+
[assembly: AssemblyVersion("0.9.0.0")]
35+
[assembly: AssemblyFileVersion("0.9.0.8")]

Blazer.Native/BlazerStream.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ extern "C" __declspec(dllexport) __int32 blazer_stream_compress_block(unsigned c
116116
int hashVal = hashArr[hashKey] - globalOfs;
117117
hashArr[hashKey] = idxIn + globalOfs;
118118
int backRef = idxIn - hashVal;
119-
int isBig = backRef < 257 ? 0 : 1;
120119

121120
if (hashVal == 0
122121
|| backRef >= MAX_BACK_REF

Blazer.Net/BlazerCompressionOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ public int MaxBlockSize
131131
}
132132
}
133133

134+
/// <summary>
135+
/// Sets max block size from flags
136+
/// </summary>
137+
public void SetMaxBlockSizeFromFlags(BlazerFlags sizeFlags)
138+
{
139+
_flags &= ~BlazerFlags.InBlockSize16M;
140+
_flags |= sizeFlags & BlazerFlags.InBlockSize16M;
141+
}
142+
134143
/// <summary>
135144
/// Gets default block size for Stream algorithm
136145
/// </summary>

0 commit comments

Comments
 (0)