forked from Aeroblast/UnpackKindleS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzw6File.cs
93 lines (82 loc) · 2.76 KB
/
Azw6File.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
namespace UnpackKindleS
{
public class Azw6File : AzwFile
{
public string title
{
get { return header.title; }
}
public Azw6Header header;
public HREF_Section hrefs_sec;
public List<string> hrefs { get { return hrefs_sec.hrefs; } }
public List<int> image_sections = new List<int>();
public Azw6File(string path) : base(path)
{
if (section_count > 0)
{
sections = new Section[section_count];
if (ident == "RBINCONT")
{
header = new Azw6Header(GetSectionData(0));
sections[0] = header;
ProcessRes();
if(image_sections.Count!=hrefs.Count){throw new UnpackKindleSException("HD Container herf and section 数量不一致");}
}
}
}
void ProcessRes()
{
for (uint i = 0; i < section_count; i++)
{
sections[i] = new Section(GetSectionData(i));
switch (sections[i].type)
{
case "kind":
hrefs_sec = new HREF_Section(sections[i].raw);
sections[i] = hrefs_sec;
break;
case "CRES":
sections[i] = new CRES_Section(sections[i]);
image_sections.Add((int)i);
break;
default:
if(Util.GetUInt32(sections[i].raw,0)==0xa0a0a0a0)
sections[i]=new PlaceHolder_Section(sections[i]);
break;
}
}
}
}
public class HREF_Section : Section
{
public List<string> hrefs;
public HREF_Section(byte[] raw) : base("href of Images", raw)
{
string[] _hrefs = Encoding.UTF8.GetString(raw).Split('|');
List<string> ist = new List<string>();
Regex regex = new Regex("kindle:embed:(.*?)\\?mime=image/(.*)");
foreach (string s in _hrefs) { Match m=regex.Match(s);if (m.Success) ist.Add(m.Groups[0].Value); }
hrefs = ist;
}
}
public class CRES_Section : Section
{
public string ext;
public byte[] img;
public CRES_Section(Section s) : base(s)
{
img = Util.SubArray(raw, 12, (ulong)raw.Length - 12);
ext = Util.GuessImageType(img);
}
}
public class PlaceHolder_Section :Section
{
public PlaceHolder_Section(Section s):base(s){}
}
}