File: ChmFile.cs

package info (click to toggle)
beagle 0.2.12-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,204 kB
  • ctags: 16,188
  • sloc: cs: 91,628; sh: 27,627; ansic: 10,646; makefile: 2,248; xml: 15
file content (580 lines) | stat: -rw-r--r-- 11,432 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//
// CHMFile.cs: Basic chmlib Wrapper, CHM file format reader.
//
// 
// Copyright (C) 2005,2006  Miguel Fernando Cabrera <mfcabrera@gmail.com>
//   
// Based on Razvan Cojocaru's X-CHM::CHMFile. 
// Uses Jed Wing's CHMLib.
// For more information about CHM file format 
// check out Pabs' CHM spec at http://bonedaddy.net/pabs3/hhm
// 
 
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.IO;
using System.Text;


#if true
namespace Beagle.Util {
#endif




/*just to make it 'right'*/
	enum ChmResolve {
		Sucess,
		Failure
	}
	
	
	enum ChmEnumerate {
		Normal = 1,
		Meta = 1 << 1,
		Special = 1 << 2,
		Files = 1 << 3,
		Dirs = 1 << 4,
		All = (1 << 5) -1
	
	}


	public delegate void ChmHtmlParseFunc(TextReader stream);

	public class ChmFile : IDisposable {

	
		private string title = "";
		//private ArrayList tocStrings;
		private ArrayList htmlFiles;
		private string topicsFile;
		private bool hasTopics = false;
		private IntPtr chmfile = IntPtr.Zero;
				
		private bool loaded = false;
		private const int  bufSize = 4096;
		private string defaultFile = "";
		
		public string Title {
			get { return title; }
			
		}
		
		public bool HasTopics {
			get { return hasTopics; }
			
		}	
		
		
		
		
		
		/* ChmLib Glue */
		
		[ StructLayout (LayoutKind.Sequential) ]
		private  class  chmUnitInfo {

			public UInt64             start;
			public UInt64             length;
			public int                space;
			public int                flags;
			[MarshalAs (UnmanagedType.ByValTStr, SizeConst=512)]
			public string path;
			/*[MarshalAs (UnmanagedType.ByValArray, SizeConst=257)]
			  public char[] path;*/
			
		}
		
		
		private  delegate int  ChmEnumerator (IntPtr chmFile,
						      chmUnitInfo info,
						      IntPtr context);
		
	
		[DllImport ("libchm.so.0")]
		private static extern IntPtr  chm_open(string filename);
		
		[DllImport ("libchm.so.0")]
		private static extern ChmResolve chm_resolve_object(IntPtr raw,
								    string  objPath,
								    [Out] chmUnitInfo ui);
		
		[DllImport ("libchm.so.0")]
		private static extern UInt64 chm_retrieve_object(IntPtr raw,
								 [In, Out] chmUnitInfo ui,
								 IntPtr buf,
								 UInt64 addr,
								 UInt64 len);
		
		
		

		[DllImport ("libchm.so.0")]
		private static extern int chm_enumerate(IntPtr raw,
							ChmEnumerate what,
							ChmEnumerator e,
							IntPtr context);
		

		
		
		private int GetHtmlFiles(IntPtr chmFile,
					 chmUnitInfo info,
					 IntPtr context) 
		{
			if(info.path.EndsWith(".html"))
				htmlFiles.Add(info.path.Trim());
			
			return 1;
			
		}

		
		
		
		private string  ChmGetString(IntPtr ptr,
					     int offset,
					     int len)
		{
			
			int i = 0;
			string str;
			
			char[]  cadena = new char[len];
		
			for(i =0; i < len ; i++){
				
				cadena[i] = (char) Marshal.ReadByte(ptr,offset + i);
				if(cadena[i] == '\0') 
					break;
			
			}
			
			str = new string (cadena,0,i);
			
			return str;
		
		
		}
		
		private string ChmFileToString(chmUnitInfo ui) 
		{
			
			
			const ulong tmpBufSize = 1025;
			
			StringBuilder strb = new StringBuilder();
			
		
			ulong size = tmpBufSize -1;
			ulong cur = 0;
			
			
			IntPtr raw = Marshal.AllocCoTaskMem ((int)tmpBufSize);
			
			do {
				size = chm_retrieve_object(chmfile,ui,raw,cur,tmpBufSize-1);
				// If I dont create a copy of the string when i free 'raw' the builder data dissapear
				// the last  chunk readed dissapear (mono bug or mi endless stupidity)
				// I'll have to check it out 
				strb.Append(Marshal.PtrToStringAuto(raw,(int)size));
				cur += size;
			}
			while(size == tmpBufSize-1);
			
			Marshal.FreeCoTaskMem (raw);
			return strb.ToString();
			
		}


		private void CleanUp() 
		{
			Marshal.FreeCoTaskMem (chmfile);
			this.loaded = false;
		}
		
		public void Dispose()
		{
			CleanUp();
			GC.SuppressFinalize (this);
						
		}
		
		~ChmFile()
		{
			CleanUp();
			
		}
		
		public ChmFile() 
		{
			
			//this.tocStrings = new ArrayList();
			this.htmlFiles = new ArrayList();
			
			
		}
		
		public bool Load(string path)
		{
			
			//chmUnitInfo ui = new chmUnitInfo() ;
			
			this.chmfile = chm_open(path);
		
			if(this.chmfile == IntPtr.Zero) {
				throw new System.Exception ("Invalid file Type, not a CHM file");	
			}
			this.loaded = true;
			
			bool info = GetArchiveInfo();
			
			/*if(info && HasTopics)
			  BuildTopicsList();*/
		
			
			chm_enumerate(this.chmfile,
				      ChmEnumerate.All,
				      new ChmEnumerator(GetHtmlFiles),
				      IntPtr.Zero);
			
			
			/*		
				foreach(string str in htmlFiles)
				Console.WriteLine(str); */
			
			return info;
			
		}

		/**
		   
		From the #SYSTEM File we are interested in the title (for now).
		
		*/

		private bool SystemInfo() 
		{
			ChmResolve res; 
			ulong  size;
			bool gottitle = false;
			
			
			
			if(!loaded)
				return false;
			
			
			IntPtr buf = Marshal.AllocCoTaskMem (bufSize);
			
			chmUnitInfo ui = new chmUnitInfo() ;
		
			
			res = chm_resolve_object (this.chmfile,"/#SYSTEM", ui);
			
			
			if(res == ChmResolve.Failure)
				return false;
			
			size  =  chm_retrieve_object (this.chmfile, ui,buf, 4, (ulong)bufSize);
			
			int index = 0;
			ushort value = 0;
			long tol = (long)size - 2;
			
			while(index < tol) {
				
				
				value =  (ushort)Marshal.ReadInt16 (buf, index);
				
				if(value == 3) {
					
					
					index += 2;
					ushort len = (ushort)Marshal.ReadInt16 (buf, (int)index);
					
					if(this.title == "") 
						this.title = ChmGetString (buf,index+2, (int)len);
					gottitle = true;
					break;
					
					
				}
				else 
					index += 2;
				
				
				value = (ushort) Marshal.ReadInt16(buf,(int)index);
				
				index += (int)value + 2;
				
			}
			
			Marshal.FreeCoTaskMem (buf);
			return gottitle;
			
		}

		/*
		  TODO:
		  We should trow something like a FileNotFoundException
		*/
		public TextReader GetFile(string path) 
		{

			chmUnitInfo ui = new chmUnitInfo();
			
			if(chm_resolve_object (chmfile,path,ui)  == ChmResolve.Failure) {
				//Console.WriteLine("Fails to Open: {0}",path);
				return new StringReader("");
				

			}
			
			return (new StringReader(ChmFileToString(ui)));
						
		}
		
		
		public TextReader  GetTopicsFile()
		{
			
			if(HasTopics) {
				
				return GetFile(topicsFile);
				
			}
			
			/*Oh Lina, why don't you love me? :P*/			
			return new StringReader("");
			
			
		}
		
		public TextReader GetDefaultFile()
		{
			
			
							
			return GetFile(defaultFile);
				
			
		}
	
		
		
		
		private bool WindowsInfo() 
		{
			
			int  entries;
			int entrySize;
			IntPtr windowsData;
			long size = 0;
			uint block;
			
			const int headerLen = 0x8;
			
		
			IntPtr buf = Marshal.AllocCoTaskMem (bufSize);
			chmUnitInfo ui = new chmUnitInfo ();
			
			
			if(chm_resolve_object (chmfile,"/#WINDOWS",ui) == ChmResolve.Failure)
				return false;
			
			if(chm_retrieve_object (chmfile,ui,buf,0,headerLen) == 0)
				return false;
			
			entries = Marshal.ReadInt32 (buf);
			entrySize = Marshal.ReadInt32 (buf,0x4);
			
			//Console.WriteLine ("entries -> {0}\nsize = {1}",entries,entrySize);
			
			windowsData  = Marshal.AllocCoTaskMem(entries * entrySize);
			
		
			size = (long)chm_retrieve_object (chmfile,
							  ui,
							  windowsData,
							  headerLen,
							  (ulong)(entries * entrySize));
			if(size == 0)
				return false;
			
			size = 0;
			
			
			if(chm_resolve_object (chmfile,"/#STRINGS",ui) == ChmResolve.Failure)
				return false;
			
			
		/*
		  From Pabs' CHM Spec: 
		  "(STRINGS)This file is a list of ANSI/UTF-8 NT strings.
		  The first is just a NIL character so that offsets to this file can specify 
		  zero & get a valid string.
		  The strings are sliced up into blocks that are 4096 bytes in length."
		*/
			
			
			
			for(int i = 0; i < entries; i++) {
				
				int offset = i * entrySize;
				
				
				uint offTitle  = (uint)Marshal.ReadInt32(windowsData,
									 offset + 0x14);
				
				uint offTocFile = (uint)Marshal.ReadInt32(windowsData,
									  offset + 0x60);
				uint offDefaultFile = (uint)Marshal.ReadInt32(windowsData,
									      offset + 0x68);
				
								
				//Console.WriteLine("offTocFile = {0}",offTocFile);
				
				
				block = offTitle / 4096;
				
				
				if(size == 0) 
					size = (long)chm_retrieve_object(chmfile, 
									 ui, 
									 buf, 
									 block * 4096, 
									 (ulong)bufSize);
				
				
				if(size > 0 && offTitle > 0) 
					this.title = ChmGetString(buf,(int)offTitle,4096);
				
				
				if(block != offTocFile / 4096) {
					block = offTocFile / 4096;
					size = (long)chm_retrieve_object(chmfile, 
									 ui, 
									 buf, 
									 block * 4096, 
									 (ulong)bufSize);
					
				}
				
				
				if(size > 0 && offTocFile > 0){ 				
					topicsFile = "/" + ChmGetString(buf,
									(int)offTocFile % 4096 ,
									4096);
					hasTopics = true;
				}
				
				
				if(block != offDefaultFile / 4096) {
					block = offDefaultFile / 4096;
					size = (long)chm_retrieve_object(chmfile, 
									 ui, 
									 buf, 
									 block * 4096, 
									 (ulong)bufSize);
					
				}
				
				
				if(size > 0 && offDefaultFile > 0) 				
					defaultFile = ("/" + ChmGetString(buf,
									  (int)offDefaultFile % 4096 ,
									  4096) );
												
				

			}
		
			Marshal.FreeCoTaskMem (buf);
			Marshal.FreeCoTaskMem (windowsData);
			return true;
			
		}
		
		
		
		private  bool GetArchiveInfo() 
		{
			/*
			  We only get the chm title (if any) from the #SYSTEM file
			  and the Toc strings for now. 		  
			  I'm sure Razvan will feel some kind of Deja Vu.
			*/
			bool rest = false;
			bool resw = false;
			
			
			if(this.loaded) {
			
				resw = WindowsInfo();
				rest = SystemInfo(); 
				
			}
			return (rest || resw);
			
		}
		
		public void ParseContents(ChmHtmlParseFunc Parse)
		{
			
						
			if(this.loaded)
				foreach(string fileName in htmlFiles) {
					chmUnitInfo ui = new chmUnitInfo();
					
					chm_resolve_object(this.chmfile,
							   fileName,
							   ui) ;
					
					
					//Console.WriteLine("Parsing....{0}",ui.path);
					///Logger.Log.Debug("CHMFile: Parsing {0}....",ui.path);
					Parse( new StringReader(ChmFileToString(ui).Trim()) );
					
					
				}
			
			
			
		}
		
		
		
	}
	
	
	

	
#if true
}
#endif