PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/libchmsharp2/System.cs

http://github.com/MindTouch/libchmsharp2
C# | 121 lines | 84 code | 18 blank | 19 comment | 4 complexity | c680ff264d31959b40ec678b89da218e MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * libchmsharp2 - a C# port of chmlib
  3. * Copyright (C) 2011 MindTouch, Inc.
  4. * www.mindtouch.com oss@mindtouch.com
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Runtime.InteropServices;
  24. using System.Text;
  25. namespace CHMsharp
  26. {
  27. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  28. public struct SystemFileEntry
  29. {
  30. public UInt16 code; /* 0x00 WORD code */
  31. public UInt16 length; /* 0x02 WORD length */
  32. public byte[] data; /* 0x04 BYTE data */
  33. }
  34. public enum Compatibility
  35. {
  36. Version_Unknown = 0,
  37. Version_1_0 = 2,
  38. Version_1_1 = 3
  39. }
  40. public sealed class SystemFile
  41. {
  42. public const UInt16 CODE_CONTENTS_FILE = 0;
  43. public const UInt16 CODE_INDEX_FILE = 1;
  44. public const UInt16 CODE_DEFAULT_TOPIC = 2;
  45. public const UInt16 CODE_TITLE = 3;
  46. public const UInt16 CODE_OPTIONS_BITFIELD = 4;
  47. public const UInt16 CODE_DEFAULT_WINDOW = 5;
  48. public const UInt16 CODE_COMPILED_FILE = 6;
  49. public const UInt16 CODE_UKNOWN_BINARY_INDEX = 7;
  50. public const UInt16 CODE_UKNOWN_STRINGS_PTR = 8;
  51. public const UInt16 CODE_GENERATOR_VERSION = 9;
  52. public const UInt16 CODE_TIMESTAMP = 10;
  53. public const UInt16 CODE_UNKNOWN_BINARY_TOC = 11;
  54. public const UInt16 CODE_NUMBER_OF_INFO_TYPES = 12;
  55. public const UInt16 CODE_UKNOWN_IDX_HEADER = 13;
  56. public const UInt16 CODE_UKNOWN_MSO_EXTENSIONS = 14;
  57. public const UInt16 CODE_INFO_TYPE_CHECKSUM = 15;
  58. public const UInt16 CODE_DEFAULT_FONT = 16;
  59. private Compatibility _compat;
  60. private SystemFileEntry[] _entries;
  61. private SystemFile(int compat, SystemFileEntry[] entries)
  62. {
  63. _compat = Enum.IsDefined(typeof(Compatibility), compat) ?
  64. (Compatibility)compat :
  65. Compatibility.Version_Unknown;
  66. _entries = entries;
  67. }
  68. public static SystemFile Read(ChmFile f)
  69. {
  70. List<SystemFileEntry> sfelst = new List<SystemFileEntry>();
  71. ChmUnitInfo ui = new ChmUnitInfo();
  72. byte[] buf;
  73. uint pos = 0, remaining = 0;
  74. if (!f.ResolveObject("/#SYSTEM", ref ui))
  75. throw new InvalidOperationException("Could not find SYSTEM file in CHM!");
  76. buf = new byte[ui.length];
  77. remaining = (uint)buf.Length;
  78. if (f.RetrieveObject(ui, ref buf, 0, (long)ui.length) == 0)
  79. throw new InvalidOperationException("Could not read SYSTEM file in CHM!");
  80. Int32 version = 0;
  81. Unmarshal.ToInt32(ref buf, ref pos, ref remaining, ref version);
  82. for ( ; pos < buf.Length; ) {
  83. SystemFileEntry sfe = new SystemFileEntry();
  84. Unmarshal.ToUInt16(ref buf, ref pos, ref remaining, ref sfe.code);
  85. Unmarshal.ToUInt16(ref buf, ref pos, ref remaining, ref sfe.length);
  86. sfe.data = new byte[sfe.length];
  87. Array.Copy(buf, pos, sfe.data, 0, sfe.length);
  88. pos += sfe.length;
  89. remaining -= sfe.length;
  90. sfelst.Add(sfe);
  91. }
  92. return new SystemFile(version, sfelst.ToArray());
  93. }
  94. public SystemFileEntry[] Entries
  95. {
  96. get { return _entries; }
  97. }
  98. public Compatibility Version
  99. {
  100. get { return _compat; }
  101. }
  102. }
  103. }