Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 62.22 KB | None | 0 0
  1.  
  2. Hack Forums
  3. C# version of SQLiteHandler.vb ? - Printable Version
  4.  
  5. +- Hack Forums (https://hackforums.net)
  6. +-- Forum: Programming, Coding, and Languages (https://hackforums.net/forumdisplay.php?fid=151)
  7. +--- Forum: Visual Basic and .NET Framework (https://hackforums.net/forumdisplay.php?fid=118)
  8. +---- Forum: C# Programming Language (https://hackforums.net/forumdisplay.php?fid=208)
  9. +---- Thread: C# version of SQLiteHandler.vb ? (/showthread.php?tid=4059126)
  10.  
  11. Pages: 1 2
  12.  
  13. C# version of SQLiteHandler.vb ? - DarkN3ss61 - 02-14-2014
  14.  
  15. Hey guys, does anyone know if there is a C# version of this?
  16. http://pastebin.com/QUFMaygS
  17.  
  18. Or am I going to have to manually convert it? Because so far i haven't managed to find it, as well as when I tried to convert it i still get a few errors.
  19.  
  20. -Thanks Dark.
  21.  
  22. * - ShadowCloud - 02-14-2014
  23.  
  24. (02-14-2014, 10:22 AM)DarkN3ss61 Wrote: Hey guys, does anyone know if there is a C# version of this?
  25. http://pastebin.com/QUFMaygS
  26.  
  27. Or am I going to have to manually convert it? Because so far i haven't managed to find it, as well as when I tried to convert it i still get a few errors.
  28.  
  29. -Thanks Dark.
  30.  
  31. I can't recall seeing anything like that in my lifetime. Why the requirement for not having the dependency?
  32.  
  33. * - maarten1012 - 02-14-2014
  34.  
  35. Don't know if this works, but I quickly converted it:
  36. Code
  37. using Microsoft.VisualBasic;
  38. using System;
  39. using System.Collections;
  40. using System.Collections.Generic;
  41. using System.Data;
  42. using System.Diagnostics;
  43. //******
  44. //
  45. // Coded By: RockingWithTheBest
  46. //
  47. // v 0.1
  48. //
  49. //
  50. // A Handler for SQLite without needing the sqlite3.dll!
  51. // You can read every table and value from database, but
  52. // there is no support for input. Also no support for
  53. // journal files and overflow pages, maybe i will add
  54. // that in a later release.
  55. //
  56. //
  57. // Have Fun!
  58. //
  59. // If u want to learn more about or enhance it then visit:
  60. // http://www.sqlite.org/fileformat.html
  61. //
  62. // If you copy or modify it, please add me to credits!
  63. //
  64. // Questions etc. to: peterrlustig@googlemail.com
  65. //
  66. //******
  67. //
  68. // Example:
  69. //
  70. // Dim SQLDatabase = New SQLiteHandler("C:\Temp\Login Data")
  71. // SQLDatabase.ReadTable("logins")
  72. //
  73. // MsgBox(GetValue(SQLDatabase.GetValue(0, "username_value")))
  74. //
  75. //******
  76.  
  77. using System.IO;
  78.  
  79. public class SQLiteHandler
  80. {
  81. private byte[] db_bytes;
  82. private UInt16 page_size;
  83. private UInt64 encoding;
  84.  
  85. private sqlite_master_entry[] master_table_entries;
  86. private byte[] SQLDataTypeSize = new byte[] {
  87. 0,
  88. 1,
  89. 2,
  90. 3,
  91. 4,
  92. 6,
  93. 8,
  94. 8,
  95. 0,
  96. 0
  97. };
  98. private table_entry[] table_entries;
  99.  
  100. private string[] field_names;
  101. private struct record_header_field
  102. {
  103. public Int64 size;
  104. public Int64 type;
  105. }
  106.  
  107. private struct table_entry
  108. {
  109. public Int64 row_id;
  110. public string[] content;
  111. }
  112.  
  113. private struct sqlite_master_entry
  114. {
  115. public Int64 row_id;
  116. public string item_type;
  117. public string item_name;
  118. public string astable_name;
  119. public Int64 root_num;
  120. public string sql_statement;
  121. }
  122.  
  123. //Needs BigEndian
  124. //GetVariableLength
  125. // returns the endindex of an variable length integer
  126. private int GVL(int startIndex)
  127. {
  128. if (startIndex > db_bytes.Length)
  129. return null;
  130.  
  131. for (i = startIndex; i <= startIndex + 8; i += 1) {
  132. if (i > db_bytes.Length - 1) {
  133. return null;
  134. } else if ((db_bytes(i) & 0x80) != 0x80) {
  135. return i;
  136. }
  137. }
  138.  
  139. return startIndex + 8;
  140. }
  141.  
  142. // Eingaberichtung BigEndian
  143. // ConvertVariableLength
  144. private Int64 CVL(int startIndex, int endIndex)
  145. {
  146. endIndex = endIndex + 1;
  147.  
  148. byte[] retus = new byte[8];
  149. dynamic Length = endIndex - startIndex;
  150. bool Bit64 = false;
  151.  
  152. if (Length == 0 | Length > 9)
  153. return null;
  154. if (Length == 1) {
  155. retus(0) = (db_bytes(startIndex) & 0x7f);
  156. return BitConverter.ToInt64(retus, 0);
  157. }
  158.  
  159. if (Length == 9) {
  160. // Ein Byte wird nämlich grad hinzugefügt
  161. Bit64 = true;
  162. }
  163.  
  164. int j = 1;
  165. int k = 7;
  166. int y = 0;
  167.  
  168. if (Bit64) {
  169. retus(0) = db_bytes(endIndex - 1);
  170. endIndex = endIndex - 1;
  171. y = 1;
  172. }
  173.  
  174. for (i = (endIndex - 1); i >= startIndex; i += -1) {
  175. if ((i - 1) >= startIndex) {
  176. retus(y) = ((db_bytes(i) >> (j - 1)) & (0xff >> j)) | (db_bytes(i - 1) << k);
  177. j = j + 1;
  178. y = y + 1;
  179. k = k - 1;
  180. } else {
  181. if (!Bit64)
  182. retus(y) = ((db_bytes(i) >> (j - 1)) & (0xff >> j));
  183. }
  184. }
  185.  
  186. return BitConverter.ToInt64(retus, 0);
  187. }
  188.  
  189. //Checks if a number is odd
  190. private bool IsOdd(Int64 value)
  191. {
  192. return (value & 1) == 1;
  193. }
  194.  
  195. //Big Endian Conversation
  196. private UInt64 ConvertToInteger(int startIndex, int Size)
  197. {
  198. if (Size > 8 | Size == 0)
  199. return null;
  200.  
  201. UInt64 retVal = 0;
  202.  
  203. for (i = 0; i <= Size - 1; i += 1) {
  204. retVal = ((retVal << 8) | db_bytes(startIndex + i));
  205. }
  206.  
  207. return retVal;
  208. }
  209.  
  210. private void ReadMasterTable(UInt64 Offset)
  211. {
  212. //Leaf node
  213. if (db_bytes(Offset) == 0xd) {
  214. //Length for setting the array length for the entries
  215. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  216. int ol = 0;
  217.  
  218. if ((master_table_entries != null)) {
  219. ol = master_table_entries.Length;
  220. Array.Resize(ref master_table_entries, master_table_entries.Length + Length + 1);
  221. } else {
  222. master_table_entries = new sqlite_master_entry[Length + 1];
  223. }
  224.  
  225. UInt64 ent_offset = default(UInt64);
  226.  
  227. for (i = 0; i <= Length; i += 1) {
  228. ent_offset = ConvertToInteger(Offset + 8 + (i * 2), 2);
  229.  
  230. if (Offset != 100)
  231. ent_offset = ent_offset + Offset;
  232.  
  233. //Table Cell auslesen
  234. dynamic t = GVL(ent_offset);
  235. Int64 size = CVL(ent_offset, t);
  236.  
  237. dynamic s = GVL(ent_offset + (t - ent_offset) + 1);
  238. master_table_entries(ol + i).row_id = CVL(ent_offset + (t - ent_offset) + 1, s);
  239.  
  240. //Table Content
  241. //Resetting the offset
  242. ent_offset = ent_offset + (s - ent_offset) + 1;
  243.  
  244. //Now get to the Record Header
  245. t = GVL(ent_offset);
  246. s = t;
  247. Int64 Rec_Header_Size = CVL(ent_offset, t);
  248. //Record Header Length
  249.  
  250. Int64[] Field_Size = new Int64[5];
  251.  
  252. //Now get the field sizes and fill in the Values
  253. for (j = 0; j <= 4; j += 1) {
  254. t = s + 1;
  255. s = GVL(t);
  256. Field_Size(j) = CVL(t, s);
  257.  
  258. if (Field_Size(j) > 9) {
  259. if (IsOdd(Field_Size(j))) {
  260. Field_Size(j) = (Field_Size(j) - 13) / 2;
  261. } else {
  262. Field_Size(j) = (Field_Size(j) - 12) / 2;
  263. }
  264. } else {
  265. Field_Size(j) = SQLDataTypeSize(Field_Size(j));
  266. }
  267. }
  268.  
  269. // Wir lesen nur unbedingt notwendige Sachen aus
  270. if (encoding == 1) {
  271. master_table_entries(ol + i).item_type = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  272. } else if (encoding == 2) {
  273. master_table_entries(ol + i).item_type = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  274. } else if (encoding == 3) {
  275. master_table_entries(ol + i).item_type = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  276. }
  277. if (encoding == 1) {
  278. master_table_entries(ol + i).item_name = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  279. } else if (encoding == 2) {
  280. master_table_entries(ol + i).item_name = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  281. } else if (encoding == 3) {
  282. master_table_entries(ol + i).item_name = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  283. }
  284. //master_table_entries(ol + i).astable_name = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1), Field_Size(2))
  285. master_table_entries(ol + i).root_num = ConvertToInteger(ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2), Field_Size(3));
  286. if (encoding == 1) {
  287. master_table_entries(ol + i).sql_statement = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  288. } else if (encoding == 2) {
  289. master_table_entries(ol + i).sql_statement = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  290. } else if (encoding == 3) {
  291. master_table_entries(ol + i).sql_statement = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  292. }
  293. }
  294. //internal node
  295. } else if (db_bytes(Offset) == 0x5) {
  296. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  297. UInt16 ent_offset = default(UInt16);
  298.  
  299. for (i = 0; i <= Length; i += 1) {
  300. ent_offset = ConvertToInteger(Offset + 12 + (i * 2), 2);
  301.  
  302. if (Offset == 100) {
  303. ReadMasterTable((ConvertToInteger(ent_offset, 4) - 1) * page_size);
  304. } else {
  305. ReadMasterTable((ConvertToInteger(Offset + ent_offset, 4) - 1) * page_size);
  306. }
  307.  
  308. }
  309.  
  310. ReadMasterTable((ConvertToInteger(Offset + 8, 4) - 1) * page_size);
  311. }
  312. }
  313.  
  314. private bool ReadTableFromOffset(UInt64 Offset)
  315. {
  316. //Leaf node
  317. if (db_bytes(Offset) == 0xd) {
  318.  
  319. //Length for setting the array length for the entries
  320. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  321. int ol = 0;
  322.  
  323. if ((table_entries != null)) {
  324. ol = table_entries.Length;
  325. Array.Resize(ref table_entries, table_entries.Length + Length + 1);
  326. } else {
  327. table_entries = new table_entry[Length + 1];
  328. }
  329.  
  330. UInt64 ent_offset = default(UInt64);
  331.  
  332. for (i = 0; i <= Length; i += 1) {
  333. ent_offset = ConvertToInteger(Offset + 8 + (i * 2), 2);
  334.  
  335. if (Offset != 100)
  336. ent_offset = ent_offset + Offset;
  337.  
  338. //Table Cell auslesen
  339. dynamic t = GVL(ent_offset);
  340. Int64 size = CVL(ent_offset, t);
  341.  
  342. dynamic s = GVL(ent_offset + (t - ent_offset) + 1);
  343. table_entries(ol + i).row_id = CVL(ent_offset + (t - ent_offset) + 1, s);
  344.  
  345. //Table Content
  346. //Resetting the offset
  347. ent_offset = ent_offset + (s - ent_offset) + 1;
  348.  
  349. //Now get to the Record Header
  350. t = GVL(ent_offset);
  351. s = t;
  352. Int64 Rec_Header_Size = CVL(ent_offset, t);
  353. //Record Header Length
  354.  
  355. record_header_field[] Field_Size = null;
  356. Int64 size_read = (ent_offset - t) + 1;
  357. dynamic j = 0;
  358.  
  359. //Now get the field sizes and fill in the Values
  360. while (size_read < Rec_Header_Size) {
  361. Array.Resize(ref Field_Size, j + 1);
  362.  
  363. t = s + 1;
  364. s = GVL(t);
  365. Field_Size(j).type = CVL(t, s);
  366.  
  367. if (Field_Size(j).type > 9) {
  368. if (IsOdd(Field_Size(j).type)) {
  369. Field_Size(j).size = (Field_Size(j).type - 13) / 2;
  370. } else {
  371. Field_Size(j).size = (Field_Size(j).type - 12) / 2;
  372. }
  373. } else {
  374. Field_Size(j).size = SQLDataTypeSize(Field_Size(j).type);
  375. }
  376.  
  377. size_read = size_read + (s - t) + 1;
  378. j = j + 1;
  379. }
  380.  
  381. // ERROR: Not supported in C#: ReDimStatement
  382.  
  383. int counter = 0;
  384.  
  385. for (k = 0; k <= Field_Size.Length - 1; k += 1) {
  386. if (Field_Size(k).type > 9) {
  387. if (!IsOdd(Field_Size(k).type)) {
  388. if (encoding == 1) {
  389. table_entries(ol + i).content(k) = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  390. } else if (encoding == 2) {
  391. table_entries(ol + i).content(k) = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  392. } else if (encoding == 3) {
  393. table_entries(ol + i).content(k) = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  394. }
  395. } else {
  396. table_entries(ol + i).content(k) = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  397. }
  398. } else {
  399. table_entries(ol + i).content(k) = Convert.ToString(ConvertToInteger(ent_offset + Rec_Header_Size + counter, Field_Size(k).size));
  400. }
  401.  
  402. counter = counter + Field_Size(k).size;
  403. }
  404. }
  405. //internal node
  406. } else if (db_bytes(Offset) == 0x5) {
  407. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  408. UInt16 ent_offset = default(UInt16);
  409.  
  410. for (i = 0; i <= Length; i += 1) {
  411. ent_offset = ConvertToInteger(Offset + 12 + (i * 2), 2);
  412.  
  413. ReadTableFromOffset((ConvertToInteger(Offset + ent_offset, 4) - 1) * page_size);
  414. }
  415.  
  416. ReadTableFromOffset((ConvertToInteger(Offset + 8, 4) - 1) * page_size);
  417. }
  418.  
  419. return true;
  420. }
  421.  
  422. // Reads a complete table with all entries in it
  423. public bool ReadTable(string TableName)
  424. {
  425. // First loop through sqlite_master and look if table exists
  426. int found = -1;
  427.  
  428. for (i = 0; i <= master_table_entries.Length; i += 1) {
  429. if (master_table_entries(i).item_name.ToLower().CompareTo(TableName.ToLower()) == 0) {
  430. found = i;
  431. break; // TODO: might not be correct. Was : Exit For
  432. }
  433. }
  434.  
  435. if (found == -1)
  436. return false;
  437.  
  438. [] fields = master_table_entries(found).sql_statement.Substring(master_table_entries(found).sql_statement.IndexOf("(") + 1).Split(",");
  439.  
  440. for (i = 0; i <= fields.Length - 1; i += 1) {
  441. fields(i) = Strings.LTrim(fields(i));
  442.  
  443. dynamic index = fields(i).IndexOf(" ");
  444.  
  445. if (index > 0)
  446. fields(i) = fields(i).Substring(0, index);
  447.  
  448. if (fields(i).IndexOf("UNIQUE") == 0) {
  449. break; // TODO: might not be correct. Was : Exit For
  450. } else {
  451. Array.Resize(ref field_names, i + 1);
  452. field_names(i) = fields(i);
  453. }
  454. }
  455.  
  456. return ReadTableFromOffset((master_table_entries(found).root_num - 1) * page_size);
  457. }
  458.  
  459. // Returns the row count of current table
  460. public int GetRowCount()
  461. {
  462. return table_entries.Length;
  463. }
  464.  
  465. // Returns a Value from current table in row row_num with field number field
  466. public string GetValue(int row_num, int field)
  467. {
  468. if (row_num >= table_entries.Length)
  469. return null;
  470. if (field >= table_entries(row_num).content.Length)
  471. return null;
  472.  
  473. return table_entries(row_num).content(field);
  474. }
  475.  
  476. // Returns a Value from current table in row row_num with field name field
  477. public string GetValue(int row_num, string field)
  478. {
  479. int found = -1;
  480.  
  481. for (i = 0; i <= field_names.Length; i += 1) {
  482. if (field_names(i).ToLower().CompareTo(field.ToLower()) == 0) {
  483. found = i;
  484. break; // TODO: might not be correct. Was : Exit For
  485. }
  486. }
  487.  
  488. if (found == -1)
  489. return null;
  490.  
  491. return GetValue(row_num, found);
  492. }
  493.  
  494. // Returns a String-Array with all Tablenames
  495. public string[] GetTableNames()
  496. {
  497. string[] retVal = null;
  498. dynamic arr = 0;
  499.  
  500. for (i = 0; i <= master_table_entries.Length - 1; i += 1) {
  501. if (master_table_entries(i).item_type == "table") {
  502. Array.Resize(ref retVal, arr + 1);
  503. retVal(arr) = master_table_entries(i).item_name;
  504. arr = arr + 1;
  505. }
  506. }
  507.  
  508. return retVal;
  509. }
  510.  
  511. // Constructor
  512. public SQLiteHandler(string baseName)
  513. {
  514. //Page Number n is page_size*(n-1)
  515. if (File.Exists(baseName)) {
  516. FileSystem.FileOpen(1, baseName, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared);
  517. string asi = Strings.Space(FileSystem.LOF(1));
  518. FileSystem.FileGet(1, asi);
  519. FileSystem.FileClose(1);
  520.  
  521. db_bytes = System.Text.Encoding.Default.GetBytes(asi);
  522.  
  523. if (System.Text.Encoding.Default.GetString(db_bytes, 0, 15).CompareTo("SQLite format 3") != 0) {
  524. throw new Exception("Not a valid SQLite 3 Database File");
  525. System.Environment.Exit(0);
  526. }
  527.  
  528. if (db_bytes(52) != 0) {
  529. throw new Exception("Auto-vacuum capable database is not supported");
  530. System.Environment.Exit(0);
  531. } else if (ConvertToInteger(44, 4) >= 4) {
  532. throw new Exception("No supported Schema layer file-format");
  533. System.Environment.Exit(0);
  534. }
  535.  
  536. page_size = ConvertToInteger(16, 2);
  537. encoding = ConvertToInteger(56, 4);
  538.  
  539. if (encoding == 0)
  540. encoding = 1;
  541.  
  542. //Now we read the sqlite_master table
  543. //Offset is 100 in first page
  544. ReadMasterTable(100);
  545. }
  546. }
  547. }
  548.  
  549. //======
  550. //Service provided by Telerik (www.telerik.com)
  551. //Conversion powered by NRefactory.
  552. //Twitter: @telerik
  553. //Facebook: facebook.com/telerik
  554. //======
  555.  
  556.  
  557. * - DarkN3ss61 - 02-14-2014
  558.  
  559. (02-14-2014, 11:14 AM)maarten1012 Wrote: Don't know if this works, but I quickly converted it:
  560. Code
  561. using Microsoft.VisualBasic;
  562. using System;
  563. using System.Collections;
  564. using System.Collections.Generic;
  565. using System.Data;
  566. using System.Diagnostics;
  567. //******
  568. //
  569. // Coded By: RockingWithTheBest
  570. //
  571. // v 0.1
  572. //
  573. //
  574. // A Handler for SQLite without needing the sqlite3.dll!
  575. // You can read every table and value from database, but
  576. // there is no support for input. Also no support for
  577. // journal files and overflow pages, maybe i will add
  578. // that in a later release.
  579. //
  580. //
  581. // Have Fun!
  582. //
  583. // If u want to learn more about or enhance it then visit:
  584. // http://www.sqlite.org/fileformat.html
  585. //
  586. // If you copy or modify it, please add me to credits!
  587. //
  588. // Questions etc. to: peterrlustig@googlemail.com
  589. //
  590. //******
  591. //
  592. // Example:
  593. //
  594. // Dim SQLDatabase = New SQLiteHandler("C:\Temp\Login Data")
  595. // SQLDatabase.ReadTable("logins")
  596. //
  597. // MsgBox(GetValue(SQLDatabase.GetValue(0, "username_value")))
  598. //
  599. //******
  600.  
  601. using System.IO;
  602.  
  603. public class SQLiteHandler
  604. {
  605. private byte[] db_bytes;
  606. private UInt16 page_size;
  607. private UInt64 encoding;
  608.  
  609. private sqlite_master_entry[] master_table_entries;
  610. private byte[] SQLDataTypeSize = new byte[] {
  611. 0,
  612. 1,
  613. 2,
  614. 3,
  615. 4,
  616. 6,
  617. 8,
  618. 8,
  619. 0,
  620. 0
  621. };
  622. private table_entry[] table_entries;
  623.  
  624. private string[] field_names;
  625. private struct record_header_field
  626. {
  627. public Int64 size;
  628. public Int64 type;
  629. }
  630.  
  631. private struct table_entry
  632. {
  633. public Int64 row_id;
  634. public string[] content;
  635. }
  636.  
  637. private struct sqlite_master_entry
  638. {
  639. public Int64 row_id;
  640. public string item_type;
  641. public string item_name;
  642. public string astable_name;
  643. public Int64 root_num;
  644. public string sql_statement;
  645. }
  646.  
  647. //Needs BigEndian
  648. //GetVariableLength
  649. // returns the endindex of an variable length integer
  650. private int GVL(int startIndex)
  651. {
  652. if (startIndex > db_bytes.Length)
  653. return null;
  654.  
  655. for (i = startIndex; i <= startIndex + 8; i += 1) {
  656. if (i > db_bytes.Length - 1) {
  657. return null;
  658. } else if ((db_bytes(i) & 0x80) != 0x80) {
  659. return i;
  660. }
  661. }
  662.  
  663. return startIndex + 8;
  664. }
  665.  
  666. // Eingaberichtung BigEndian
  667. // ConvertVariableLength
  668. private Int64 CVL(int startIndex, int endIndex)
  669. {
  670. endIndex = endIndex + 1;
  671.  
  672. byte[] retus = new byte[8];
  673. dynamic Length = endIndex - startIndex;
  674. bool Bit64 = false;
  675.  
  676. if (Length == 0 | Length > 9)
  677. return null;
  678. if (Length == 1) {
  679. retus(0) = (db_bytes(startIndex) & 0x7f);
  680. return BitConverter.ToInt64(retus, 0);
  681. }
  682.  
  683. if (Length == 9) {
  684. // Ein Byte wird nämlich grad hinzugefügt
  685. Bit64 = true;
  686. }
  687.  
  688. int j = 1;
  689. int k = 7;
  690. int y = 0;
  691.  
  692. if (Bit64) {
  693. retus(0) = db_bytes(endIndex - 1);
  694. endIndex = endIndex - 1;
  695. y = 1;
  696. }
  697.  
  698. for (i = (endIndex - 1); i >= startIndex; i += -1) {
  699. if ((i - 1) >= startIndex) {
  700. retus(y) = ((db_bytes(i) >> (j - 1)) & (0xff >> j)) | (db_bytes(i - 1) << k);
  701. j = j + 1;
  702. y = y + 1;
  703. k = k - 1;
  704. } else {
  705. if (!Bit64)
  706. retus(y) = ((db_bytes(i) >> (j - 1)) & (0xff >> j));
  707. }
  708. }
  709.  
  710. return BitConverter.ToInt64(retus, 0);
  711. }
  712.  
  713. //Checks if a number is odd
  714. private bool IsOdd(Int64 value)
  715. {
  716. return (value & 1) == 1;
  717. }
  718.  
  719. //Big Endian Conversation
  720. private UInt64 ConvertToInteger(int startIndex, int Size)
  721. {
  722. if (Size > 8 | Size == 0)
  723. return null;
  724.  
  725. UInt64 retVal = 0;
  726.  
  727. for (i = 0; i <= Size - 1; i += 1) {
  728. retVal = ((retVal << 8) | db_bytes(startIndex + i));
  729. }
  730.  
  731. return retVal;
  732. }
  733.  
  734. private void ReadMasterTable(UInt64 Offset)
  735. {
  736. //Leaf node
  737. if (db_bytes(Offset) == 0xd) {
  738. //Length for setting the array length for the entries
  739. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  740. int ol = 0;
  741.  
  742. if ((master_table_entries != null)) {
  743. ol = master_table_entries.Length;
  744. Array.Resize(ref master_table_entries, master_table_entries.Length + Length + 1);
  745. } else {
  746. master_table_entries = new sqlite_master_entry[Length + 1];
  747. }
  748.  
  749. UInt64 ent_offset = default(UInt64);
  750.  
  751. for (i = 0; i <= Length; i += 1) {
  752. ent_offset = ConvertToInteger(Offset + 8 + (i * 2), 2);
  753.  
  754. if (Offset != 100)
  755. ent_offset = ent_offset + Offset;
  756.  
  757. //Table Cell auslesen
  758. dynamic t = GVL(ent_offset);
  759. Int64 size = CVL(ent_offset, t);
  760.  
  761. dynamic s = GVL(ent_offset + (t - ent_offset) + 1);
  762. master_table_entries(ol + i).row_id = CVL(ent_offset + (t - ent_offset) + 1, s);
  763.  
  764. //Table Content
  765. //Resetting the offset
  766. ent_offset = ent_offset + (s - ent_offset) + 1;
  767.  
  768. //Now get to the Record Header
  769. t = GVL(ent_offset);
  770. s = t;
  771. Int64 Rec_Header_Size = CVL(ent_offset, t);
  772. //Record Header Length
  773.  
  774. Int64[] Field_Size = new Int64[5];
  775.  
  776. //Now get the field sizes and fill in the Values
  777. for (j = 0; j <= 4; j += 1) {
  778. t = s + 1;
  779. s = GVL(t);
  780. Field_Size(j) = CVL(t, s);
  781.  
  782. if (Field_Size(j) > 9) {
  783. if (IsOdd(Field_Size(j))) {
  784. Field_Size(j) = (Field_Size(j) - 13) / 2;
  785. } else {
  786. Field_Size(j) = (Field_Size(j) - 12) / 2;
  787. }
  788. } else {
  789. Field_Size(j) = SQLDataTypeSize(Field_Size(j));
  790. }
  791. }
  792.  
  793. // Wir lesen nur unbedingt notwendige Sachen aus
  794. if (encoding == 1) {
  795. master_table_entries(ol + i).item_type = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  796. } else if (encoding == 2) {
  797. master_table_entries(ol + i).item_type = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  798. } else if (encoding == 3) {
  799. master_table_entries(ol + i).item_type = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  800. }
  801. if (encoding == 1) {
  802. master_table_entries(ol + i).item_name = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  803. } else if (encoding == 2) {
  804. master_table_entries(ol + i).item_name = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  805. } else if (encoding == 3) {
  806. master_table_entries(ol + i).item_name = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  807. }
  808. //master_table_entries(ol + i).astable_name = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1), Field_Size(2))
  809. master_table_entries(ol + i).root_num = ConvertToInteger(ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2), Field_Size(3));
  810. if (encoding == 1) {
  811. master_table_entries(ol + i).sql_statement = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  812. } else if (encoding == 2) {
  813. master_table_entries(ol + i).sql_statement = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  814. } else if (encoding == 3) {
  815. master_table_entries(ol + i).sql_statement = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  816. }
  817. }
  818. //internal node
  819. } else if (db_bytes(Offset) == 0x5) {
  820. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  821. UInt16 ent_offset = default(UInt16);
  822.  
  823. for (i = 0; i <= Length; i += 1) {
  824. ent_offset = ConvertToInteger(Offset + 12 + (i * 2), 2);
  825.  
  826. if (Offset == 100) {
  827. ReadMasterTable((ConvertToInteger(ent_offset, 4) - 1) * page_size);
  828. } else {
  829. ReadMasterTable((ConvertToInteger(Offset + ent_offset, 4) - 1) * page_size);
  830. }
  831.  
  832. }
  833.  
  834. ReadMasterTable((ConvertToInteger(Offset + 8, 4) - 1) * page_size);
  835. }
  836. }
  837.  
  838. private bool ReadTableFromOffset(UInt64 Offset)
  839. {
  840. //Leaf node
  841. if (db_bytes(Offset) == 0xd) {
  842.  
  843. //Length for setting the array length for the entries
  844. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  845. int ol = 0;
  846.  
  847. if ((table_entries != null)) {
  848. ol = table_entries.Length;
  849. Array.Resize(ref table_entries, table_entries.Length + Length + 1);
  850. } else {
  851. table_entries = new table_entry[Length + 1];
  852. }
  853.  
  854. UInt64 ent_offset = default(UInt64);
  855.  
  856. for (i = 0; i <= Length; i += 1) {
  857. ent_offset = ConvertToInteger(Offset + 8 + (i * 2), 2);
  858.  
  859. if (Offset != 100)
  860. ent_offset = ent_offset + Offset;
  861.  
  862. //Table Cell auslesen
  863. dynamic t = GVL(ent_offset);
  864. Int64 size = CVL(ent_offset, t);
  865.  
  866. dynamic s = GVL(ent_offset + (t - ent_offset) + 1);
  867. table_entries(ol + i).row_id = CVL(ent_offset + (t - ent_offset) + 1, s);
  868.  
  869. //Table Content
  870. //Resetting the offset
  871. ent_offset = ent_offset + (s - ent_offset) + 1;
  872.  
  873. //Now get to the Record Header
  874. t = GVL(ent_offset);
  875. s = t;
  876. Int64 Rec_Header_Size = CVL(ent_offset, t);
  877. //Record Header Length
  878.  
  879. record_header_field[] Field_Size = null;
  880. Int64 size_read = (ent_offset - t) + 1;
  881. dynamic j = 0;
  882.  
  883. //Now get the field sizes and fill in the Values
  884. while (size_read < Rec_Header_Size) {
  885. Array.Resize(ref Field_Size, j + 1);
  886.  
  887. t = s + 1;
  888. s = GVL(t);
  889. Field_Size(j).type = CVL(t, s);
  890.  
  891. if (Field_Size(j).type > 9) {
  892. if (IsOdd(Field_Size(j).type)) {
  893. Field_Size(j).size = (Field_Size(j).type - 13) / 2;
  894. } else {
  895. Field_Size(j).size = (Field_Size(j).type - 12) / 2;
  896. }
  897. } else {
  898. Field_Size(j).size = SQLDataTypeSize(Field_Size(j).type);
  899. }
  900.  
  901. size_read = size_read + (s - t) + 1;
  902. j = j + 1;
  903. }
  904.  
  905. // ERROR: Not supported in C#: ReDimStatement
  906.  
  907. int counter = 0;
  908.  
  909. for (k = 0; k <= Field_Size.Length - 1; k += 1) {
  910. if (Field_Size(k).type > 9) {
  911. if (!IsOdd(Field_Size(k).type)) {
  912. if (encoding == 1) {
  913. table_entries(ol + i).content(k) = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  914. } else if (encoding == 2) {
  915. table_entries(ol + i).content(k) = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  916. } else if (encoding == 3) {
  917. table_entries(ol + i).content(k) = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  918. }
  919. } else {
  920. table_entries(ol + i).content(k) = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  921. }
  922. } else {
  923. table_entries(ol + i).content(k) = Convert.ToString(ConvertToInteger(ent_offset + Rec_Header_Size + counter, Field_Size(k).size));
  924. }
  925.  
  926. counter = counter + Field_Size(k).size;
  927. }
  928. }
  929. //internal node
  930. } else if (db_bytes(Offset) == 0x5) {
  931. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  932. UInt16 ent_offset = default(UInt16);
  933.  
  934. for (i = 0; i <= Length; i += 1) {
  935. ent_offset = ConvertToInteger(Offset + 12 + (i * 2), 2);
  936.  
  937. ReadTableFromOffset((ConvertToInteger(Offset + ent_offset, 4) - 1) * page_size);
  938. }
  939.  
  940. ReadTableFromOffset((ConvertToInteger(Offset + 8, 4) - 1) * page_size);
  941. }
  942.  
  943. return true;
  944. }
  945.  
  946. // Reads a complete table with all entries in it
  947. public bool ReadTable(string TableName)
  948. {
  949. // First loop through sqlite_master and look if table exists
  950. int found = -1;
  951.  
  952. for (i = 0; i <= master_table_entries.Length; i += 1) {
  953. if (master_table_entries(i).item_name.ToLower().CompareTo(TableName.ToLower()) == 0) {
  954. found = i;
  955. break; // TODO: might not be correct. Was : Exit For
  956. }
  957. }
  958.  
  959. if (found == -1)
  960. return false;
  961.  
  962. [] fields = master_table_entries(found).sql_statement.Substring(master_table_entries(found).sql_statement.IndexOf("(") + 1).Split(",");
  963.  
  964. for (i = 0; i <= fields.Length - 1; i += 1) {
  965. fields(i) = Strings.LTrim(fields(i));
  966.  
  967. dynamic index = fields(i).IndexOf(" ");
  968.  
  969. if (index > 0)
  970. fields(i) = fields(i).Substring(0, index);
  971.  
  972. if (fields(i).IndexOf("UNIQUE") == 0) {
  973. break; // TODO: might not be correct. Was : Exit For
  974. } else {
  975. Array.Resize(ref field_names, i + 1);
  976. field_names(i) = fields(i);
  977. }
  978. }
  979.  
  980. return ReadTableFromOffset((master_table_entries(found).root_num - 1) * page_size);
  981. }
  982.  
  983. // Returns the row count of current table
  984. public int GetRowCount()
  985. {
  986. return table_entries.Length;
  987. }
  988.  
  989. // Returns a Value from current table in row row_num with field number field
  990. public string GetValue(int row_num, int field)
  991. {
  992. if (row_num >= table_entries.Length)
  993. return null;
  994. if (field >= table_entries(row_num).content.Length)
  995. return null;
  996.  
  997. return table_entries(row_num).content(field);
  998. }
  999.  
  1000. // Returns a Value from current table in row row_num with field name field
  1001. public string GetValue(int row_num, string field)
  1002. {
  1003. int found = -1;
  1004.  
  1005. for (i = 0; i <= field_names.Length; i += 1) {
  1006. if (field_names(i).ToLower().CompareTo(field.ToLower()) == 0) {
  1007. found = i;
  1008. break; // TODO: might not be correct. Was : Exit For
  1009. }
  1010. }
  1011.  
  1012. if (found == -1)
  1013. return null;
  1014.  
  1015. return GetValue(row_num, found);
  1016. }
  1017.  
  1018. // Returns a String-Array with all Tablenames
  1019. public string[] GetTableNames()
  1020. {
  1021. string[] retVal = null;
  1022. dynamic arr = 0;
  1023.  
  1024. for (i = 0; i <= master_table_entries.Length - 1; i += 1) {
  1025. if (master_table_entries(i).item_type == "table") {
  1026. Array.Resize(ref retVal, arr + 1);
  1027. retVal(arr) = master_table_entries(i).item_name;
  1028. arr = arr + 1;
  1029. }
  1030. }
  1031.  
  1032. return retVal;
  1033. }
  1034.  
  1035. // Constructor
  1036. public SQLiteHandler(string baseName)
  1037. {
  1038. //Page Number n is page_size*(n-1)
  1039. if (File.Exists(baseName)) {
  1040. FileSystem.FileOpen(1, baseName, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared);
  1041. string asi = Strings.Space(FileSystem.LOF(1));
  1042. FileSystem.FileGet(1, asi);
  1043. FileSystem.FileClose(1);
  1044.  
  1045. db_bytes = System.Text.Encoding.Default.GetBytes(asi);
  1046.  
  1047. if (System.Text.Encoding.Default.GetString(db_bytes, 0, 15).CompareTo("SQLite format 3") != 0) {
  1048. throw new Exception("Not a valid SQLite 3 Database File");
  1049. System.Environment.Exit(0);
  1050. }
  1051.  
  1052. if (db_bytes(52) != 0) {
  1053. throw new Exception("Auto-vacuum capable database is not supported");
  1054. System.Environment.Exit(0);
  1055. } else if (ConvertToInteger(44, 4) >= 4) {
  1056. throw new Exception("No supported Schema layer file-format");
  1057. System.Environment.Exit(0);
  1058. }
  1059.  
  1060. page_size = ConvertToInteger(16, 2);
  1061. encoding = ConvertToInteger(56, 4);
  1062.  
  1063. if (encoding == 0)
  1064. encoding = 1;
  1065.  
  1066. //Now we read the sqlite_master table
  1067. //Offset is 100 in first page
  1068. ReadMasterTable(100);
  1069. }
  1070. }
  1071. }
  1072.  
  1073. //======
  1074. //Service provided by Telerik (www.telerik.com)
  1075. //Conversion powered by NRefactory.
  1076. //Twitter: @telerik
  1077. //Facebook: facebook.com/telerik
  1078. //======
  1079.  
  1080. Thanks but it won't work because that's the same website I originally used to convert it and it still had quite a few errors. :/
  1081. So just incase anyone could finish this off for me
  1082. This is where im up to
  1083. Code
  1084. public class SQLiteHandler
  1085. {
  1086. private byte[] db_bytes;
  1087. private UInt16 page_size;
  1088. private UInt64 encoding;
  1089.  
  1090. private sqlite_master_entry[] master_table_entries;
  1091. private byte[] SQLDataTypeSize = new byte[] {0,1,2,3,4,6,8,8,0,0};
  1092. private table_entry[] table_entries;
  1093.  
  1094. private string[] field_names;
  1095. private struct record_header_field
  1096. {
  1097. public Int64 size;
  1098. public Int64 type;
  1099. }
  1100.  
  1101. private struct table_entry
  1102. {
  1103. public Int64 row_id;
  1104. public string[] content;
  1105. }
  1106.  
  1107. private struct sqlite_master_entry
  1108. {
  1109. public Int64 row_id;
  1110. public string item_type;
  1111. public string item_name;
  1112. public string astable_name;
  1113. public Int64 root_num;
  1114. public string sql_statement;
  1115. }
  1116.  
  1117. //Needs BigEndian
  1118. //GetVariableLength
  1119. // returns the endindex of an variable length integer
  1120. private int GVL(int startIndex)
  1121. {
  1122. if (startIndex > db_bytes.Length)
  1123. return 0;
  1124.  
  1125. for (int i = startIndex; i <= startIndex + 8; i += 1)
  1126. {
  1127. if (i > db_bytes.Length - 1)
  1128. {
  1129. return 0;
  1130. }
  1131. else if ((db_bytes[i] & 0x80) != 0x80)
  1132. {
  1133. return i;
  1134. }
  1135. }
  1136.  
  1137. return startIndex + 8;
  1138. }
  1139.  
  1140. // Eingaberichtung BigEndian
  1141. // ConvertVariableLength
  1142. private Int64 CVL(int startIndex, int endIndex)
  1143. {
  1144. endIndex = endIndex + 1;
  1145.  
  1146. byte[] retus = new byte[8];
  1147. dynamic Length = endIndex - startIndex;
  1148. bool Bit64 = false;
  1149.  
  1150. if (Length == 0 | Length > 9)
  1151. return 0;
  1152. if (Length == 1)
  1153. {
  1154. retus[0] = (db_bytes(startIndex) & 0x7f);
  1155. return BitConverter.ToInt64(retus, 0);
  1156. }
  1157.  
  1158. if (Length == 9)
  1159. {
  1160. // Ein Byte wird nämlich grad hinzugefügt
  1161. Bit64 = true;
  1162. }
  1163.  
  1164. int j = 1;
  1165. int k = 7;
  1166. int y = 0;
  1167.  
  1168. if (Bit64)
  1169. {
  1170. retus[0] = db_bytes(endIndex - 1);
  1171. endIndex = endIndex - 1;
  1172. y = 1;
  1173. }
  1174.  
  1175. for (int i = (endIndex - 1); i >= startIndex; i += -1)
  1176. {
  1177. if ((i - 1) >= startIndex)
  1178. {
  1179. retus[y] = ((db_bytes(i) >> (j - 1)) & (0xff >> j)) | (db_bytes(i - 1) << k);
  1180. j = j + 1;
  1181. y = y + 1;
  1182. k = k - 1;
  1183. }
  1184. else
  1185. {
  1186. if (!Bit64)
  1187. retus[y] = ((db_bytes(i) >> (j - 1)) & (0xff >> j));
  1188. }
  1189. }
  1190.  
  1191. return BitConverter.ToInt64(retus, 0);
  1192. }
  1193.  
  1194. //Checks if a number is odd
  1195. private bool IsOdd(Int64 value)
  1196. {
  1197. return (value & 1) == 1;
  1198. }
  1199.  
  1200. //Big Endian Conversation
  1201. private UInt64 ConvertToInteger(int startIndex, int Size)
  1202. {
  1203. if (Size > 8 | Size == 0)
  1204. return 0;
  1205.  
  1206. UInt64 retVal = 0;
  1207.  
  1208. for (int i = 0; i <= Size - 1; i += 1)
  1209. {
  1210. retVal = ((retVal << 8) | db_bytes(startIndex + i));
  1211. }
  1212.  
  1213. return retVal;
  1214. }
  1215.  
  1216. private void ReadMasterTable(UInt64 Offset)
  1217. {
  1218. //Leaf node
  1219. if (db_bytes(Offset) == 0xd)
  1220. {
  1221. //Length for setting the array length for the entries
  1222. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  1223. int ol = 0;
  1224.  
  1225. if ((master_table_entries != null))
  1226. {
  1227. ol = master_table_entries.Length;
  1228. Array.Resize(ref master_table_entries, master_table_entries.Length + Length + 1);
  1229. }
  1230. else
  1231. {
  1232. master_table_entries = new sqlite_master_entry[Length + 1];
  1233. }
  1234.  
  1235. UInt64 ent_offset = default(UInt64);
  1236.  
  1237. for (int i = 0; i <= Length; i += 1)
  1238. {
  1239. ent_offset = ConvertToInteger(Offset + 8 + (i * 2), 2);
  1240.  
  1241. if (Offset != 100)
  1242. ent_offset = ent_offset + Offset;
  1243.  
  1244. //Table Cell auslesen
  1245. dynamic t = GVL(ent_offset);
  1246. Int64 size = CVL(ent_offset, t);
  1247.  
  1248. dynamic s = GVL(ent_offset + (t - ent_offset) + 1);
  1249. master_table_entries(ol + i).row_id = CVL(ent_offset + (t - ent_offset) + 1, s);
  1250.  
  1251. //Table Content
  1252. //Resetting the offset
  1253. ent_offset = ent_offset + (s - ent_offset) + 1;
  1254.  
  1255. //Now get to the Record Header
  1256. t = GVL(ent_offset);
  1257. s = t;
  1258. Int64 Rec_Header_Size = CVL(ent_offset, t);
  1259. //Record Header Length
  1260.  
  1261. Int64[] Field_Size = new Int64[5];
  1262.  
  1263. //Now get the field sizes and fill in the Values
  1264. for (int j = 0; j <= 4; j += 1)
  1265. {
  1266. t = s + 1;
  1267. s = GVL(t);
  1268. Field_Size[j] = CVL(t, s);
  1269.  
  1270. if (Field_Size[j] > 9)
  1271. {
  1272. if (IsOdd(Field_Size[j]))
  1273. {
  1274. Field_Size[j] = (Field_Size[j] - 13) / 2;
  1275. }
  1276. else
  1277. {
  1278. Field_Size[j] = (Field_Size[j] - 12) / 2;
  1279. }
  1280. }
  1281. else
  1282. {
  1283. Field_Size[j] = SQLDataTypeSize(Field_Size[j]);
  1284. }
  1285. }
  1286.  
  1287. // Wir lesen nur unbedingt notwendige Sachen aus
  1288. if (encoding == 1)
  1289. {
  1290. master_table_entries(ol + i).item_type = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  1291. }
  1292. else if (encoding == 2)
  1293. {
  1294. master_table_entries(ol + i).item_type = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  1295. }
  1296. else if (encoding == 3)
  1297. {
  1298. master_table_entries(ol + i).item_type = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size, Field_Size(0));
  1299. }
  1300. if (encoding == 1)
  1301. {
  1302. master_table_entries(ol + i).item_name = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  1303. }
  1304. else if (encoding == 2)
  1305. {
  1306. master_table_entries(ol + i).item_name = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  1307. }
  1308. else if (encoding == 3)
  1309. {
  1310. master_table_entries(ol + i).item_name = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0), Field_Size(1));
  1311. }
  1312. //master_table_entries(ol + i).astable_name = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1), Field_Size(2))
  1313. master_table_entries(ol + i).root_num = ConvertToInteger(ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2), Field_Size(3));
  1314. if (encoding == 1)
  1315. {
  1316. master_table_entries(ol + i).sql_statement = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  1317. }
  1318. else if (encoding == 2)
  1319. {
  1320. master_table_entries(ol + i).sql_statement = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  1321. }
  1322. else if (encoding == 3)
  1323. {
  1324. master_table_entries(ol + i).sql_statement = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + Field_Size(0) + Field_Size(1) + Field_Size(2) + Field_Size(3), Field_Size(4));
  1325. }
  1326. }
  1327. //internal node
  1328. }
  1329. else if (db_bytes(Offset) == 0x5)
  1330. {
  1331. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  1332. UInt16 ent_offset = default(UInt16);
  1333.  
  1334. for (int i = 0; i <= Length; i += 1)
  1335. {
  1336. ent_offset = ConvertToInteger(Offset + 12 + (i * 2), 2);
  1337.  
  1338. if (Offset == 100)
  1339. {
  1340. ReadMasterTable((ConvertToInteger(ent_offset, 4) - 1) * page_size);
  1341. }
  1342. else
  1343. {
  1344. ReadMasterTable((ConvertToInteger(Offset + ent_offset, 4) - 1) * page_size);
  1345. }
  1346.  
  1347. }
  1348.  
  1349. ReadMasterTable((ConvertToInteger(Offset + 8, 4) - 1) * page_size);
  1350. }
  1351. }
  1352.  
  1353. private bool ReadTableFromOffset(UInt64 Offset)
  1354. {
  1355. //Leaf node
  1356. if (db_bytes(Offset) == 0xd)
  1357. {
  1358.  
  1359. //Length for setting the array length for the entries
  1360. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  1361. int ol = 0;
  1362.  
  1363. if ((table_entries != null))
  1364. {
  1365. ol = table_entries.Length;
  1366. Array.Resize(ref table_entries, table_entries.Length + Length + 1);
  1367. }
  1368. else
  1369. {
  1370. table_entries = new table_entry[Length + 1];
  1371. }
  1372.  
  1373. UInt64 ent_offset = default(UInt64);
  1374.  
  1375. for (int i = 0; i <= Length; i += 1)
  1376. {
  1377. ent_offset = ConvertToInteger(Offset + 8 + (i * 2), 2);
  1378.  
  1379. if (Offset != 100)
  1380. ent_offset = ent_offset + Offset;
  1381.  
  1382. //Table Cell auslesen
  1383. dynamic t = GVL(ent_offset);
  1384. Int64 size = CVL(ent_offset, t);
  1385.  
  1386. dynamic s = GVL(ent_offset + (t - ent_offset) + 1);
  1387. table_entries(ol + i).row_id = CVL(ent_offset + (t - ent_offset) + 1, s);
  1388.  
  1389. //Table Content
  1390. //Resetting the offset
  1391. ent_offset = ent_offset + (s - ent_offset) + 1;
  1392.  
  1393. //Now get to the Record Header
  1394. t = GVL(ent_offset);
  1395. s = t;
  1396. Int64 Rec_Header_Size = CVL(ent_offset, t);
  1397. //Record Header Length
  1398.  
  1399. record_header_field[] Field_Size = null;
  1400. Int64 size_read = (ent_offset - t) + 1;
  1401. dynamic j = 0;
  1402.  
  1403. //Now get the field sizes and fill in the Values
  1404. while (size_read < Rec_Header_Size)
  1405. {
  1406. Array.Resize(ref Field_Size, j + 1);
  1407.  
  1408. t = s + 1;
  1409. s = GVL(t);
  1410. Field_Size[j].type = CVL(t, s);
  1411.  
  1412. if (Field_Size[j].type > 9)
  1413. {
  1414. if (IsOdd(Field_Size[j].type))
  1415. {
  1416. Field_Size[j].size = (Field_Size[j].type - 13) / 2;
  1417. }
  1418. else
  1419. {
  1420. Field_Size[j].size = (Field_Size[j].type - 12) / 2;
  1421. }
  1422. }
  1423. else
  1424. {
  1425. Field_Size[j].size = SQLDataTypeSize(Field_Size[j].type);
  1426. }
  1427.  
  1428. size_read = size_read + (s - t) + 1;
  1429. j = j + 1;
  1430. }
  1431.  
  1432. // ERROR: Not supported in C#: ReDimStatement
  1433.  
  1434. int counter = 0;
  1435.  
  1436. for (int k = 0; k <= Field_Size.Length - 1; k += 1)
  1437. {
  1438. if (Field_Size(k).type > 9)
  1439. {
  1440. if (!IsOdd(Field_Size(k).type))
  1441. {
  1442. if (encoding == 1)
  1443. {
  1444. table_entries(ol + i).content(k) = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  1445. }
  1446. else if (encoding == 2)
  1447. {
  1448. table_entries(ol + i).content(k) = System.Text.Encoding.Unicode.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  1449. }
  1450. else if (encoding == 3)
  1451. {
  1452. table_entries(ol + i).content(k) = System.Text.Encoding.BigEndianUnicode.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  1453. }
  1454. }
  1455. else
  1456. {
  1457. table_entries(ol + i).content(k) = System.Text.Encoding.Default.GetString(db_bytes, ent_offset + Rec_Header_Size + counter, Field_Size(k).size);
  1458. }
  1459. }
  1460. else
  1461. {
  1462. table_entries(ol + i).content(k) = Convert.ToString(ConvertToInteger(ent_offset + Rec_Header_Size + counter, Field_Size(k).size));
  1463. }
  1464.  
  1465. counter = counter + Field_Size(k).size;
  1466. }
  1467. }
  1468. //internal node
  1469. }
  1470. else if (db_bytes(Offset) == 0x5)
  1471. {
  1472. UInt16 Length = ConvertToInteger(Offset + 3, 2) - 1;
  1473. UInt16 ent_offset = default(UInt16);
  1474.  
  1475. for (int i = 0; i <= Length; i += 1)
  1476. {
  1477. ent_offset = ConvertToInteger(Offset + 12 + (i * 2), 2);
  1478.  
  1479. ReadTableFromOffset((ConvertToInteger(Offset + ent_offset, 4) - 1) * page_size);
  1480. }
  1481.  
  1482. ReadTableFromOffset((ConvertToInteger(Offset + 8, 4) - 1) * page_size);
  1483. }
  1484.  
  1485. return true;
  1486. }
  1487.  
  1488. // Reads a complete table with all entries in it
  1489. public bool ReadTable(string TableName)
  1490. {
  1491. // First loop through sqlite_master and look if table exists
  1492. int found = -1;
  1493.  
  1494. for (int i = 0; i <= master_table_entries.Length; i += 1) {
  1495. if (master_table_entries[i].item_name.ToLower().CompareTo(TableName.ToLower()) == 0) {
  1496. found = i;
  1497. break; // TODO: might not be correct. Was : Exit For
  1498. }
  1499. }
  1500.  
  1501. if (found == -1)
  1502. return false;
  1503.  
  1504. string fields = master_table_entries[found].sql_statement.Substring(master_table_entries[found].sql_statement.IndexOf("(") + 1).Split(",");
  1505.  
  1506. for (int i = 0; i <= fields.Length - 1; i += 1) {
  1507. fields(i) = Strings.LTrim(fields(i));
  1508.  
  1509. dynamic index = fields(i).IndexOf(" ");
  1510.  
  1511. if (index > 0)
  1512. fields(i) = fields(i).Substring(0, index);
  1513.  
  1514. if (fields(i).IndexOf("UNIQUE") == 0) {
  1515. break; // TODO: might not be correct. Was : Exit For
  1516. } else {
  1517. Array.Resize(ref field_names, i + 1);
  1518. field_names[i] = fields(i);
  1519. }
  1520. }
  1521.  
  1522. return ReadTableFromOffset((master_table_entries(found).root_num - 1) * page_size);
  1523. }
  1524.  
  1525. // Returns the row count of current table
  1526. public int GetRowCount()
  1527. {
  1528. return table_entries.Length;
  1529. }
  1530.  
  1531. // Returns a Value from current table in row row_num with field number field
  1532. public string GetValue(int row_num, int field)
  1533. {
  1534. if (row_num >= table_entries.Length)
  1535. return null;
  1536. if (field >= table_entries(row_num).content.Length)
  1537. return null;
  1538.  
  1539. return table_entries(row_num).content(field);
  1540. }
  1541.  
  1542. // Returns a Value from current table in row row_num with field name field
  1543. public string GetValue(int row_num, string field)
  1544. {
  1545. int found = -1;
  1546.  
  1547. Second part coming
  1548. Code
  1549. for (int i = 0; i <= field_names.Length; i += 1)
  1550. {
  1551. if (field_names(i).ToLower().CompareTo(field.ToLower()) == 0)
  1552. {
  1553. found = i;
  1554. break; // TODO: might not be correct. Was : Exit For
  1555. }
  1556. }
  1557.  
  1558. if (found == -1)
  1559. return null;
  1560. return GetValue(row_num, found);
  1561. }
  1562.  
  1563. // Returns a String-Array with all Tablenames
  1564. public string[] GetTableNames()
  1565. {
  1566. string[] retVal = null;
  1567. dynamic arr = 0;
  1568.  
  1569. for (int i = 0; i <= master_table_entries.Length - 1; i += 1)
  1570. {
  1571. if (master_table_entries(i).item_type == "table")
  1572. {
  1573. Array.Resize(ref retVal, arr + 1);
  1574. retVal(arr) = master_table_entries(i).item_name;
  1575. arr = arr + 1;
  1576. }
  1577. }
  1578.  
  1579. return retVal;
  1580. }
  1581.  
  1582. // Constructor
  1583. public SQLiteHandler(string baseName)
  1584. {
  1585. //Page Number n is page_size*(n-1)
  1586. if (File.Exists(baseName))
  1587. {
  1588. FileSystem.FileOpen(1, baseName, OpenMode.Binary, OpenAccess.Read, OpenShare.Shared);
  1589. string asi = Strings.Space(FileSystem.LOF(1));
  1590. FileSystem.FileGet(1, asi);
  1591. FileSystem.FileClose(1);
  1592.  
  1593. db_bytes = System.Text.Encoding.Default.GetBytes(asi);
  1594.  
  1595. if (System.Text.Encoding.Default.GetString(db_bytes, 0, 15).CompareTo("SQLite format 3") != 0)
  1596. {
  1597. throw new Exception("Not a valid SQLite 3 Database File");
  1598. System.Environment.Exit(0);
  1599. }
  1600.  
  1601. if (db_bytes(52) != 0)
  1602. {
  1603. throw new Exception("Auto-vacuum capable database is not supported");
  1604. System.Environment.Exit(0);
  1605. }
  1606. else if (ConvertToInteger(44, 4) >= 4)
  1607. {
  1608. throw new Exception("No supported Schema layer file-format");
  1609. System.Environment.Exit(0);
  1610. }
  1611.  
  1612. page_size = ConvertToInteger(16, 2);
  1613. encoding = ConvertToInteger(56, 4);
  1614.  
  1615. if (encoding == 0)
  1616. encoding = 1;
  1617.  
  1618. //Now we read the sqlite_master table
  1619. //Offset is 100 in first page
  1620. ReadMasterTable(100);
  1621. }
  1622. }
  1623. }
  1624. }
  1625.  
  1626. And as you can see there are still a few errors left over.
  1627.  
  1628. So if anyone could help finish this, I would <3 you for ever - no homo[/code]
  1629.  
  1630. * - XtremeDev - 02-18-2014
  1631.  
  1632. Hey there, I was bored so I took some time to rewrite it. I used a VB => C# converter for some, and had to do manual coding for other stuff.
  1633.  
  1634. Anyway here is the final result, I have not tested it but it does compile...
  1635.  
  1636. http://pastie.org/private/6yn4zuystojovbsqsvq
  1637.  
  1638. Let me know if you get any weird functionality, I can do further research if you have trouble figuring it out Tongue
  1639.  
  1640. * - DarkN3ss61 - 02-18-2014
  1641.  
  1642. (02-18-2014, 02:31 AM)XtremeDev Wrote: Hey there, I was bored so I took some time to rewrite it. I used a VB => C# converter for some, and had to do manual coding for other stuff.
  1643.  
  1644. Anyway here is the final result, I have not tested it but it does compile...
  1645.  
  1646. http://pastie.org/private/6yn4zuystojovbsqsvq
  1647.  
  1648. Let me know if you get any weird functionality, I can do further research if you have trouble figuring it out Tongue
  1649.  
  1650. Wow thanks so much, thanks for taking the time to do this!
  1651.  
  1652. * - Banksу - 02-18-2014
  1653.  
  1654. I know you're just looking for this to be converted, but I would like to propose an alternative.
  1655.  
  1656. I've used MySQL, MsSQL (Linq to SQL) and more (mostly for Auxilium), and nothing I've used has come close to what I wanted.
  1657.  
  1658. MongoDB is a great alternative. It is a No-SQL database, and integrates VERY easily with C#, to where all you need to do is define a class and mongodb does the rest for you.
  1659.  
  1660. I might make a tutorial soon, because it's (so far) the best alternative I've found to the complicated and pain in the ass that is SQL.
  1661.  
  1662. * - DarkN3ss61 - 02-18-2014
  1663.  
  1664. (02-18-2014, 03:24 AM)Banksу Wrote: I know you're just looking for this to be converted, but I would like to propose an alternative.
  1665.  
  1666. I've used MySQL, MsSQL (Linq to SQL) and more (mostly for Auxilium), and nothing I've used has come close to what I wanted.
  1667.  
  1668. MongoDB is a great alternative. It is a No-SQL database, and integrates VERY easily with C#, to where all you need to do is define a class and mongodb does the rest for you.
  1669.  
  1670. I might make a tutorial soon, because it's (so far) the best alternative I've found to the complicated and pain in the ass that is SQL.
  1671. Thanks for this, i will defiantly look into it! :D
  1672.  
  1673. * - XtremeDev - 02-18-2014
  1674.  
  1675. (02-18-2014, 03:24 AM)Banksу Wrote: I know you're just looking for this to be converted, but I would like to propose an alternative.
  1676.  
  1677. I've used MySQL, MsSQL (Linq to SQL) and more (mostly for Auxilium), and nothing I've used has come close to what I wanted.
  1678.  
  1679. MongoDB is a great alternative. It is a No-SQL database, and integrates VERY easily with C#, to where all you need to do is define a class and mongodb does the rest for you.
  1680.  
  1681. I might make a tutorial soon, because it's (so far) the best alternative I've found to the complicated and pain in the ass that is SQL.
  1682.  
  1683. I would say the benefits of SQL far outweigh the costs of using it. Not only this but with No-SQL you can easily ruin your ability to scale larger if you plan on having relational tables.
  1684. http://mongodbnosql.blogspot.com/2013/01/what-is-advantages-and-disadvantages-of.html
  1685.  
  1686. Relative to "pain in the ass SQL".. have you heard of "LightSpeed" or "EntityFramework" - this sort of gets rid of the "pain in the ass" in SQL Thumbsup LightSpeed works with SQLite and many many other database types with the ease of only changing an enum value to switch backends... and has a pretty designer. Yeye
  1687.  
  1688. http://www.mindscapehq.com/products/lightspeed/key-features
  1689. its free for databases with less than 9 tables.. if you're looking for something free that supports more than 9, check into entity framework.
  1690.  
  1691. http://msdn.microsoft.com/en-us/data/ef.aspx
  1692.  
  1693. Not trying to start a war of NoSQL vs SQL, but from personal experiences I'd stay with SQL Superman
  1694.  
  1695. * - Banksу - 02-18-2014
  1696.  
  1697. (02-18-2014, 03:33 AM)XtremeDev Wrote: I would say the benefits of SQL far outweigh the costs of using it. Not only this but with No-SQL you can easily ruin your ability to scale larger if you plan on having relational tables.
  1698. http://mongodbnosql.blogspot.com/2013/01/what-is-advantages-and-disadvantages-of.html
  1699.  
  1700. Relative to "pain in the ass SQL".. have you heard of "LightSpeed" or "EntityFramework" - this sort of gets rid of the "pain in the ass" in SQL Thumbsup LightSpeed works with SQLite and many many other database types with the ease of only changing an enum value to switch backends... and has a pretty designer. Yeye
  1701.  
  1702. http://www.mindscapehq.com/products/lightspeed/key-features
  1703. its free for databases with less than 9 tables.. if you're looking for something free that supports more than 9, check into entity framework.
  1704.  
  1705. http://msdn.microsoft.com/en-us/data/ef.aspx
  1706.  
  1707. Not trying to start a war of NoSQL vs SQL, but from personal experiences I'd stay with SQL Superman
  1708.  
  1709. What I mean by SQL being a pain in the ass, specifically with MsSQL, is that it's always been (for me anyways) extremely hit or miss as to whether or not it would install correctly the first time or not.
  1710. Figure that in with how large the download is, how long it takes to install, etc, etc, and mongodb just seems like an easier and quicker route.
  1711.  
  1712. * - XtremeDev - 02-18-2014
  1713.  
  1714. (02-18-2014, 03:41 AM)Banksу Wrote: What I mean by SQL being a pain in the ass, specifically with MsSQL, is that it's always been (for me anyways) extremely hit or miss as to whether or not it would install correctly the first time or not.
  1715. Figure that in with how large the download is, how long it takes to install, etc, etc, and mongodb just seems like an easier and quicker route.
  1716.  
  1717. True.. I suppose if it was some sort of client application with a non exhaustive amount of data then it'd be just fine!
  1718.  
  1719. * - DarkN3ss61 - 02-18-2014
  1720.  
  1721. (02-18-2014, 03:44 AM)XtremeDev Wrote: True.. I suppose if it was some sort of client application with a non exhaustive amount of data then it'd be just fine!
  1722.  
  1723. XtremeDev did you get my messages? XD because there are a few errors I am getting when i run it
  1724.  
  1725. * - XtremeDev - 02-18-2014
  1726.  
  1727. (02-18-2014, 03:47 AM)DarkN3ss61 Wrote: XtremeDev did you get my messages? XD because there are a few errors I am getting when i run it
  1728.  
  1729. I did - unfortunately I have misplaced my skype information Glare so hold on I'll make a new account and add you!
  1730.  
  1731. * - DarkN3ss61 - 02-18-2014
  1732.  
  1733. (02-18-2014, 03:49 AM)XtremeDev Wrote: I did - unfortunately I have misplaced my skype information Glare so hold on I'll make a new account and add you!
  1734.  
  1735. Ok cool, thanks so much for helping :D
  1736.  
  1737. * - XtremeDev - 02-18-2014
  1738.  
  1739. (02-18-2014, 04:17 AM)DarkN3ss61 Wrote: Ok cool, thanks so much for helping :D
  1740.  
  1741. I shall accomplish this tomorrow as it is getting late. Expect a friend request sometime in the next several hours, Thanks for understanding. Pinch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement