Skip to content

Instantly share code, notes, and snippets.

@mohemohe
Created January 31, 2016 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohemohe/5d556f2fbd97dbfdc36e to your computer and use it in GitHub Desktop.
Save mohemohe/5d556f2fbd97dbfdc36e to your computer and use it in GitHub Desktop.
Primeなんとか
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace PrimeBitmap
{
class Program
{
static void Main(string[] args)
{
foreach (var s in args)
{
var ext = Path.GetExtension(s);
if (ext?.ToLower() == @".bmp")
{
Extract(s);
}
else
{
Camouflage(s);
}
}
}
static void Extract(string path)
{
using (var rfs = new FileStream(path, FileMode.Open))
using (var wfs = new FileStream(path.Substring(0, path.Length - 4), FileMode.Create))
{
rfs.Seek(Marshal.SizeOf(typeof(BITMAPFILEHEADER)) + Marshal.SizeOf(typeof(BITMAPINFOHEADER)), SeekOrigin.Begin);
rfs.CopyTo(wfs);
}
}
static void Camouflage(string path)
{
var payloadSize = (long)Math.Ceiling((double)(new FileInfo(path).Length) / 3);
var headerSize = Marshal.SizeOf(typeof (BITMAPFILEHEADER)) + Marshal.SizeOf(typeof (BITMAPINFOHEADER));
var fileSize = headerSize + payloadSize;
var bfh = new BITMAPFILEHEADER
{
bfType = 0x4d42,
bfSize = (uint)fileSize,
bfOffBits = (uint)headerSize
};
var size = GetSize(payloadSize);
var bih = new BITMAPINFOHEADER
{
biSize = 40,
biWidth = size.width,
biHeight = size.height,
biPlanes = 1,
biBitCount = 24,
biCompression = 0,
biSizeImage = 0,
biXPelsPerMeter = 0,
biYPelsPerMeter = 0,
biClrUsed = 0,
biClrImportant = 0
};
using (var rfs = new FileStream(path, FileMode.Open))
using (var wfs = new FileStream(path + @".bmp", FileMode.Create))
{
var bytes = new byte[Marshal.SizeOf(bfh)];
var gch = GCHandle.Alloc(bytes, GCHandleType.Pinned);
Marshal.StructureToPtr(bfh, gch.AddrOfPinnedObject(), false);
gch.Free();
wfs.Write(bytes, 0, bytes.Length);
bytes = new byte[Marshal.SizeOf(bih)];
gch = GCHandle.Alloc(bytes, GCHandleType.Pinned);
Marshal.StructureToPtr(bih, gch.AddrOfPinnedObject(), false);
gch.Free();
wfs.Write(bytes, 0, bytes.Length);
rfs.CopyTo(wfs);
}
}
static Size GetSize(long num)
{
var i = 1L;
var w = 1;
var h = 1;
while (i < num)
{
if (w + 1 <= h + 1)
{
w++;
}
else
{
h++;
}
i = w * h;
}
return new Size { width = w, height = h };
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct BITMAPFILEHEADER
{
public ushort bfType;
public uint bfSize;
public ushort bfReserved1;
public ushort bfReserved2;
public uint bfOffBits;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct BITMAPINFOHEADER
{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
}
struct Size
{
public int width;
public int height;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment