Skip to content

Instantly share code, notes, and snippets.

@poizan42
Created March 3, 2014 18:16
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 poizan42/9331195 to your computer and use it in GitHub Desktop.
Save poizan42/9331195 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CCITTFaxFileScan
{
class Program
{
static void Main(string[] args)
{
FileStream itwFile = File.OpenRead(args[0]);
Console.WriteLine("Scanning for EOFB with fill order msb-first");
ScanForEOFB(itwFile, 1);
Console.WriteLine("Scanning for EOFB with fill order lsb-first");
ScanForEOFB(itwFile, 2);
Console.WriteLine("Scanning for EOL with fill order msb-first");
ScanForEOL(itwFile, 1);
Console.WriteLine("Scanning for EOL with fill order lsb-first");
ScanForEOL(itwFile, 2);
Console.ReadLine();
}
private static void ScanForEOFB(FileStream itwFile, int fillOrder)
{
const int EOFB = 0x1001; //000000000001000000000001
itwFile.Seek(0, SeekOrigin.Begin);
int last24Bits = 0;
int bi;
while ((bi = itwFile.ReadByte()) != -1)
{
byte b = (byte)bi;
for (int bit = 0; bit < 8; bit++)
{
last24Bits <<= 1;
last24Bits &= 0x00FFFFFF;
if (fillOrder == 1)
last24Bits |= (b >> (7-bit)) & 1;
else
last24Bits |= (b >> bit) & 1;
if (last24Bits == EOFB)
{
Console.WriteLine("EOFB at {0:X8}.{1}", itwFile.Position, bit);
}
}
}
}
private static void ScanForEOL(FileStream itwFile, int fillOrder)
{
const int EOL = 0x1; //0000 0000-0001
itwFile.Seek(0, SeekOrigin.Begin);
int last12Bits = 0;
int bi;
while ((bi = itwFile.ReadByte()) != -1)
{
byte b = (byte)bi;
for (int bit = 0; bit < 8; bit++)
{
last12Bits <<= 1;
last12Bits &= 0x00000FFF;
if (fillOrder == 1)
last12Bits |= (b >> (7-bit)) & 1;
else
last12Bits |= (b >> bit) & 1;
if (last12Bits == EOL)
{
Console.WriteLine("EOL at {0:X8}.{1}", itwFile.Position, bit);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment