手机电子书UMD数据格式实现代码(C#版)

原文地址:http://blog.joycode.com/uestc95/archive/2008/06/19/115155.aspx

UMD格式是国内手机阅读中使用较多的一种格式,但其公司却并没有将UMD数据格式公开,但是却用另外一种方式将其公开,你去访问一下他们的站点然后下载他的制作工具就知道。

下面的是文本的UMD相关处理代码,图形的下一篇post出来

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> namespace Ikari
{
using ICSharpCode.SharpZipLib.Zip.Compression;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

public class UMD_GENEGINE
{
private const int A_32K_BYTE = 0x8000 ;
private byte ACTUAL_WIDTH_S60_HORI = 0xcc ;
private byte ACTUAL_WIDTH_S60_VERT = 0xac ;
private byte ACTUAL_WIDTH_SP = 0xa6 ;
private const uint BASE_REFN_CHAP_OFF = 0x3000 ;
private const uint BASE_REFN_CHAP_STR = 0x4000 ;
private const uint BASE_REFN_CONTENT = 0x2000 ;
private const uint BASE_REFN_COVER = 0x1000 ;
private const uint BASE_REFN_PAGE_OFFSET = 0x7000 ;
private const string BEYOND_END_FLAG = " \0 " ;
private const int BYTE_LEN = 1 ;
private const byte COVER_TYPE_BMP = 0 ;
private const byte COVER_TYPE_GIF = 2 ;
private const byte COVER_TYPE_JPG = 1 ;
private const int CURR_VERSION = 1 ;
private const short DCTS_CMD_ID_AUTHOR = 3 ;
private const short DCTS_CMD_ID_CDS_KEY = 240 ;
private const short DCTS_CMD_ID_CHAP_OFF = 0x83 ;
private const short DCTS_CMD_ID_CHAP_STR = 0x84 ;
private const short DCTS_CMD_ID_CONTENT_ID = 10 ;
private const short DCTS_CMD_ID_COVER_PAGE = 130 ;
private const short DCTS_CMD_ID_DAY = 6 ;
private const short DCTS_CMD_ID_FILE_LENGTH = 11 ;
private const short DCTS_CMD_ID_FIXED_LEN = 12 ;
private const short DCTS_CMD_ID_GENDER = 7 ;
private const short DCTS_CMD_ID_LICENSE_KEY = 0xf1 ;
private const short DCTS_CMD_ID_MONTH = 5 ;
private const short DCTS_CMD_ID_PAGE_OFFSET = 0x87 ;
private const short DCTS_CMD_ID_PUBLISHER = 8 ;
private const short DCTS_CMD_ID_REF_CONTENT = 0x81 ;
private const short DCTS_CMD_ID_TITLE = 2 ;
private const short DCTS_CMD_ID_VENDOR = 9 ;
private const short DCTS_CMD_ID_VERSION = 1 ;
private const short DCTS_CMD_ID_YEAR = 4 ;
private const byte FIXED_LINE_PER_PAGE_S60 = 50 ;
private const byte FIXED_LINE_PER_PAGE_SP = 0x19 ;
private string iAuthor;
private byte []ibContent;
private int []iChapOff;
private ArrayListiChapStr = new ArrayList();
private ArrayListiChapter = new ArrayList();
private int iCID;
private string iContent;
private string iCoverFile;
private string iDay;
private string iGender;
private string iMonth;
private const int INT_LEN = 4 ;
private short iPGKSeed = 0 ;
private string iPublisher;
private string iSaveTo;
private string iTitle;
private int iTotalen;
private string iVendor;
private ArrayListiWidthData_S60 = new ArrayList();
private ArrayListiWidthData_SP = new ArrayList();
private string iYear;
private byte [][]iZippedSeg;
private const string KEY_CODE_TAB = " \t " ;
private const byte S60_FONT_SIZE_BIG = 0x10 ;
private const byte S60_FONT_SIZE_SMALL = 12 ;
private const byte SEREIS60_FONTS_COUNT = 2 ;
private const int SHORT_LEN = 2 ;
private const byte SP_FONT_SIZE_10 = 10 ;
private const byte SP_FONT_SIZE_MAX = 0x10 ;
private const byte SP_FONT_SIZE_MIN = 6 ;
private const string SYMBIAN_RETURN = " \u2029 " ;
private const string SYMBIAN_SPACE = " " ;
private const byte UMD_DCTD_HEAD_LEN = 9 ;
private const byte UMD_DCTS_HEAD_LEN = 5 ;
private const int UMD_FREE_CID_MIN = 0x5f5e100 ;
private const int UMD_FREE_PGK_SEED_MIN = 0x400 ;
private const int UMD_LICENSEKEY_LEN = 0x10 ;
private const int UMD_MAX_BOOKMARK_STR_LEN = 40 ;
private const byte UMD_PLATFORM_ID_NONE = 0 ;
private const byte UMD_PLATFORM_ID_S60 = 1 ;
private const byte UMD_PLATFORM_ID_SP = 5 ;
private const int UMD_SEGMENT_LENGTH = 0x8000 ;
private const byte VER_PKG_LEN = 3 ;
private const string WINDOWS_RETURN = " \r\n " ;
private const int ZIP_LEVEL = 9 ;

private byte CharWidth_S60( string Char, byte pFontSize)
{
ushort num = Char[ 0 ];
for ( int i = 0 ;i < this .iWidthData_S60.Count;i ++ )
{
SWidthDatadata
= (SWidthData) this .iWidthData_S60[i];
if (((data.FontSize == pFontSize) && (num >= data.rngFrom)) && (num <= data.rngTo))
{
if (data.vCount == 1 )
{
return data.Value[ 0 ];
}
return data.Value[num - data.rngFrom];
}
}
return pFontSize;
}

private byte CharWidth_SP( string Char, byte pFontSize)
{
ushort num = Char[ 0 ];
for ( int i = 0 ;i < this .iWidthData_SP.Count;i ++ )
{
SWidthDatadata
= (SWidthData) this .iWidthData_SP[i];
if (((data.FontSize == pFontSize) && (num >= data.rngFrom)) && (num <= data.rngTo))
{
if (data.vCount == 1 )
{
return data.Value[ 0 ];
}
return data.Value[num - data.rngFrom];
}
}
return pFontSize;
}

private bool GetPageOffsetS60( byte size, uint actual_width, ref ProgressBarpbar, out uint []result)
{
ArrayListpPageoff
= new ArrayList();
if ((size != 0x10 ) && (size != 12 ))
{
result
= new uint [ 0 ];
return false ;
}
this .GetWidthData_S60();
pPageoff.Add(
0 );
int num = pbar.Value;
while ((( int )pPageoff[pPageoff.Count - 1 ]) < this .iContent.Length)
{
this .ParseOnePage(( uint )(pPageoff.Count - 1 ),size,actual_width, ref pPageoff, 1 );
pbar.Value
= num + (( int )(( uint )pPageoff[pPageoff.Count - 1 ]));
}
result
= new uint [pPageoff.Count];
for ( int i = 0 ;i < pPageoff.Count;i ++ )
{
result[i]
= (( uint )pPageoff[i]) * 2 ;
}
return true ;
}

private bool GetPageOffsetSP( byte size, uint actual_width, ref ProgressBarpbar, out uint []result)
{
ArrayListpPageoff
= new ArrayList();
if ((size < 6 ) || (size > 0x10 ))
{
result
= new uint [ 0 ];
return false ;
}
this .GetWidthData_SP();
pPageoff.Add(
0 );
int num = pbar.Value;
while ((( uint )pPageoff[pPageoff.Count - 1 ]) < this .iContent.Length)
{
this .ParseOnePage(( uint )(pPageoff.Count - 1 ),size,actual_width, ref pPageoff, 5 );
if ((num + (( int )(( uint )pPageoff[pPageoff.Count - 1 ]))) < pbar.Maximum)
{
pbar.Value
= num + (( int )(( uint )pPageoff[pPageoff.Count - 1 ]));
}
}
result
= new uint [pPageoff.Count];
for ( int i = 0 ;i < pPageoff.Count;i ++ )
{
result[i]
= ( uint )pPageoff[i];
}
return true ;
}

private void GetWidthData_S60()
{
this .iWidthData_S60.Clear();
for ( int i = 0 ;i < 2 ;i ++ )
{
string path = "" ;
switch (i)
{
case 0 :
path
= Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Remove( 0 , 8 )) + @" \FontWidthData\S60CHS.S16.wdt " ;
break ;

case 1 :
path
= Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Remove( 0 , 8 )) + @" \FontWidthData\S60CHS.S12.wdt " ;
break ;
}
if (File.Exists(path))
{
FileStreaminput
= new FileStream(path,FileMode.Open,FileAccess.Read);
BinaryReaderreader
= new BinaryReader(input);
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
ushort num2 = reader.ReadUInt16();
ushort num3 = reader.ReadUInt16();
uint num4 = reader.ReadUInt16();
byte []buffer = new byte [num4];
for ( uint j = 0 ;j < num4;j ++ )
{
buffer[j]
= reader.ReadByte();
}
SWidthDatadata
= new SWidthData();
switch (i)
{
case 0 :
data.FontSize
= 0x10 ;
break ;

case 1 :
data.FontSize
= 12 ;
break ;
}
data.rngFrom
= num2;
data.rngTo
= num3;
data.vCount
= num4;
data.Value
= buffer;
this .iWidthData_S60.Add(data);
}
reader.Close();
input.Close();
}
}
}

private void GetWidthData_SP()
{
this .iWidthData_SP.Clear();
for ( int i = 0x10 ;i < 0x11 ;i ++ )
{
string path = (Assembly.GetExecutingAssembly().CodeBase.Remove( 0 , 8 ) + @" \FontWidthData\sunfon.s " ) + i.ToString() + " .wdt " ;
if (File.Exists(path))
{
FileStreaminput
= new FileStream(path,FileMode.Open,FileAccess.Read);
BinaryReaderreader
= new BinaryReader(input);
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
ushort num2 = reader.ReadUInt16();
ushort num3 = reader.ReadUInt16();
uint num4 = reader.ReadUInt16();
byte []buffer = new byte [num4];
for ( uint j = 0 ;j < num4;j ++ )
{
buffer[j]
= reader.ReadByte();
}
SWidthDatadata
= new SWidthData();
data.FontSize
= ( byte )i;
data.rngFrom
= num2;
data.rngTo
= num3;
data.vCount
= num4;
data.Value
= buffer;
this
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值