Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Created August 29, 2012 12:01
Show Gist options
  • Save deoxxa/3511525 to your computer and use it in GitHub Desktop.
Save deoxxa/3511525 to your computer and use it in GitHub Desktop.
AniDB Database Stuff
#!/usr/bin/env node
var binary = require("binary"); // https://github.com/substack/node-binary
var str = "646174610900000058000000000100040800000000ffffffbbffffffe4000100000000000000000200041400000003ffffffbbffffffe40041006e0069006d00650068006100760065006e00000300040400000003ffffffbbffffffe40041004800000600040d00000002ffffffbbffffffe4006972632e72697a6f6e2e6e6574000700040b00000002ffffffbbffffffe40023616e696d65686176656e000a00040800000000ffffffbbffffffe4001affffffc5ffffffc23c00000000000b00040800000000ffffffbbffffffe400ffffffb36b594700000000000c00040800000000ffffffbbffffffe400ffffffdf01000000000000000d00040800000000ffffffbbffffffe4004700000000000000".replace(/ffffff/g, "");
var b = new Buffer(str, "hex");
binary(b).buffer("magic", 4).tap(function(vars) {
if (vars.magic.toString() !== "data") {
return console.warn("invalid header");
}
this.word32lu("count").word32lu("length").tap(function(vars) {
var count = vars.count, i = 0;
this.loop(function(end, vars) {
if (i++ >= count) {
return end();
}
this.into("element_" + i, function(vars) {
this.word16bu("id").word16bu("magic").word32lu("length").word8("type").buffer("junk", 3).buffer("data", "length");
});
});
});
}).tap(function(vars) {
console.log(vars);
});
/*
{ magic: <Buffer 64 61 74 61>,
count: 9,
length: 88,
element_1:
{ id: 1,
magic: 4,
length: 8,
type: 0,
junk: <Buffer bb e4 00>,
data: <Buffer 01 00 00 00 00 00 00 00> },
element_2:
{ id: 2,
magic: 4,
length: 20,
type: 3,
junk: <Buffer bb e4 00>,
data: <Buffer 41 00 6e 00 69 00 6d 00 65 00 68 00 61 00 76 00 65 00 6e 00> },
element_3:
{ id: 3,
magic: 4,
length: 4,
type: 3,
junk: <Buffer bb e4 00>,
data: <Buffer 41 00 48 00> },
element_4:
{ id: 6,
magic: 4,
length: 13,
type: 2,
junk: <Buffer bb e4 00>,
data: <Buffer 69 72 63 2e 72 69 7a 6f 6e 2e 6e 65 74> },
element_5:
{ id: 7,
magic: 4,
length: 11,
type: 2,
junk: <Buffer bb e4 00>,
data: <Buffer 23 61 6e 69 6d 65 68 61 76 65 6e> },
element_6:
{ id: 10,
magic: 4,
length: 8,
type: 0,
junk: <Buffer bb e4 00>,
data: <Buffer 1a c5 c2 3c 00 00 00 00> },
element_7:
{ id: 11,
magic: 4,
length: 8,
type: 0,
junk: <Buffer bb e4 00>,
data: <Buffer b3 6b 59 47 00 00 00 00> },
element_8:
{ id: 12,
magic: 4,
length: 8,
type: 0,
junk: <Buffer bb e4 00>,
data: <Buffer df 01 00 00 00 00 00 00> },
element_9:
{ id: 13,
magic: 4,
length: 8,
type: 0,
junk: <Buffer bb e4 00>,
data: <Buffer 47 00 00 00 00 00 00 00> } }
*/
646174610900000058000000000100040800000000ffffffbbffffffe4000100000000000000000200041400000003ffffffbbffffffe40041006e0069006d00650068006100760065006e00000300040400000003ffffffbbffffffe40041004800000600040d00000002ffffffbbffffffe4006972632e72697a6f6e2e6e6574000700040b00000002ffffffbbffffffe40023616e696d65686176656e000a00040800000000ffffffbbffffffe4001affffffc5ffffffc23c00000000000b00040800000000ffffffbbffffffe400ffffffb36b594700000000000c00040800000000ffffffbbffffffe400ffffffdf01000000000000000d00040800000000ffffffbbffffffe4004700000000000000
646174610b000000ffffffa0000000000100040800000000ffffffbbffffffe4000200000000000000000200041e00000003ffffffbbffffffe40041006e0069006d00650043006f006c006c0065006300740069006f006e00000300040600000003ffffffbbffffffe400410043004c00000400041d00000002ffffffbbffffffe400687474703a2f2f7777772e616e696d65636f6c6c656374696f6e2e6465000500041a00000002ffffffbbffffffe4006c6f72646c696340616e696d65636f6c6c656374696f6e2e6465000600040d00000002ffffffbbffffffe4006972632e65756972632e6e6574000700041000000002ffffffbbffffffe40023616e696d65636f6c6c656374696f6e000a00040800000000ffffffbbffffffe400ffffffe5ffffffe0ffffffc23c00000000000b00040800000000ffffffbbffffffe40022ffffffe3ffffffaf3f00000000000c00040800000000ffffffbbffffffe400ffffffe001000000000000000d00040800000000ffffffbbffffffe4000d00000000000000
64617461090000005b000000000100040800000000ffffffbbffffffe4000300000000000000000200040c00000003ffffffbbffffffe40061002e0066002e006b002e00000300040c00000003ffffffbbffffffe40061002e0066002e006b002e00000600041400000002ffffffbbffffffe4006972632e656e74657274686567616d652e636f6d000700040700000002ffffffbbffffffe40023612e662e6b2e000a00040800000000ffffffbbffffffe4001dffffffe3ffffffc23c00000000000b00040800000000ffffffbbffffffe4001e37ffffffd63f00000000000c00040800000000ffffffbbffffffe400fffffff802000000000000000d00040800000000ffffffbbffffffe4006a06000000000000
646174610700000048000000000100040800000000ffffffbbffffffe4000400000000000000000200041c00000003ffffffbbffffffe40041006e0069006d00650043006f0061006c006900740069006f006e00000300040400000003ffffffbbffffffe40041004300000a00040800000000ffffffbbffffffe400ffffffe2ffffffe8ffffffc23c00000000000b00040800000000ffffffbbffffffe4000a33ffffffe74f00000000000c00040800000000ffffffbbffffffe4000102000000000000000d00040800000000ffffffbbffffffe400ffffffa300000000000000
646174610a000000ffffff84000000000100040800000000ffffffbbffffffe4000500000000000000000200041a00000003ffffffbbffffffe40041006e0069006d0065002000440061006900730075006b006900000300040600000003ffffffbbffffffe40041002d004400000400042100000002ffffffbbffffffe400687474703a2f2f7777772e6f72616e67656461792e6e65742f64616973756b692f000600040d00000002ffffffbbffffffe4006972632e72697a6f6e2e6e6574000700040e00000002ffffffbbffffffe40023616e696d655f64616973756b69000a00040800000000ffffffbbffffffe4001dffffffe9ffffffc23c00000000000b00040800000000ffffffbbffffffe400ffffff8cffffffd34d4000000000000c00040800000000ffffffbbffffffe4006602000000000000000d00040800000000ffffffbbffffffe4000f00000000000000
646174610a0000007f000000000100040800000000ffffffbbffffffe4000600000000000000000200041800000003ffffffbbffffffe40041006e0069006d0065002d0045006d007000690072006500000300040600000003ffffffbbffffffe40041002d004500000400041b00000002ffffffbbffffffe400687474703a2f2f7777772e616e696d652d656d706972652e6e6574000600041100000002ffffffbbffffffe4006972632e636861747370696b652e6e6574000700040d00000002ffffffbbffffffe40023416e696d652d456d70697265000a00040800000000ffffffbbffffffe4005bffffffe9ffffffc23c00000000000b00040800000000ffffffbbffffffe4000affffffd6ffffffb64600000000000c00040800000000ffffffbbffffffe400ffffffef02000000000000000d00040800000000ffffffbbffffffe4005304000000000000
646174610a00000022010000000100040800000000ffffffbbffffffe4000700000000000000000200041a00000003ffffffbbffffffe40041006e0069006d006500200046006100630074006f0072007900000300040400000003ffffffbbffffffe40041004600000600040f00000002ffffffbbffffffe4006972632e6c696e6b6972632e6e6574000700040d00000002ffffffbbffffffe40023416e696d65666163746f727900090004ffffffc000000003ffffffbbffffffe40046006f0072006d0065006400200073006f006d006500740069006d006500200069006e00200031003900390039002e00200049007300200061006d006f006e0067007300740020007400680065002000660069007200730074002000670072006f00750070007300200074006f002000720065006c00650061007300650020006400690067006900740061006c002000660061006e0073007500620073002e003c006200720020002f003e00440069007300620061006e006400650064002e00000a00040800000000ffffffbbffffffe400ffffff98ffffffe9ffffffc23c00000000000b00040800000000ffffffbbffffffe400ffffffd8ffffffd9ffffffb64600000000000c00040800000000ffffffbbffffffe4002001000000000000000d00040800000000ffffffbbffffffe4001400000000000000
646174610a00000077000000000100040800000000ffffffbbffffffe4000800000000000000000200040e00000003ffffffbbffffffe40053006f006c006400610074007300000300040e00000003ffffffbbffffffe40053006f006c006400610074007300000400041700000002ffffffbbffffffe400687474703a2f2f7777772e736f6c646174732e6e65742f000600041400000002ffffffbbffffffe4006972632e656e74657274686567616d652e636f6d000700040800000002ffffffbbffffffe40023736f6c64617473000a00040800000000ffffffbbffffffe400ffffffacffffffedffffffc23c00000000000b00040800000000ffffffbbffffffe40023ffffffdbffffffb64600000000000c00040800000000ffffffbbffffffe4005e02000000000000000d00040800000000ffffffbbffffffe4004901000000000000
646174610800000062000000000100040800000000ffffffbbffffffe4000900000000000000000200041400000003ffffffbbffffffe40041006e0069006d00650066006f00720063006500000300040a00000003ffffffbbffffffe40046004f00520043004500000900041c00000003ffffffbbffffffe400470072006f0075007000200069007300200064006500610064002e00000a00040800000000ffffffbbffffffe4004afffffff1ffffffc23c00000000000b00040800000000ffffffbbffffffe4007e20374300000000000c00040800000000ffffffbbffffffe400fffffffb00000000000000000d00040800000000ffffffbbffffffe4002900000000000000
646174610a00000075000000000100040800000000ffffffbbffffffe4000a00000000000000000200041400000003ffffffbbffffffe40041006e0069006d0065002d004b00650065007000000300040c00000003ffffffbbffffffe40041002d004b00650065007000000400041500000002ffffffbbffffffe400687474703a2f2f616e696d652d6b6565702e6f7267000600040d00000002ffffffbbffffffe4006972632e72697a6f6e2e6e6574000700040b00000002ffffffbbffffffe40023616e696d652d6b656570000a00040800000000ffffffbbffffffe400ffffff8efffffff1ffffffc23c00000000000b00040800000000ffffffbbffffffe400ffffffaeffffffebfffffff44100000000000c00040800000000ffffffbbffffffe400ffffffd002000000000000000d00040800000000ffffffbbffffffe4003805000000000000
/**
* Link against `aomdbi4.dll`. For headers I used the `sqlite3.h` from OS X Lion,
* you can use whatever you like. It just has to provide `sqlite3_key`.
*
* > gcc.exe -o sqlite-thing.exe sqlite-thing.c c:\path\to\aomdbi4.dll
* > sqlite-thing.exe c:\path\to\database.data > data.dump
*
* Windows is silly.
*/
#include <stdio.h>
#include "sqlite3.h"
int main(int argc, char** argv) {
// Supplying this key is left as an exercise for the reader.
char key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
char sql[] = "SELECT * FROM dataTable";
int rc = 0;
sqlite3* dbh = NULL;
char* err = NULL;
sqlite3_stmt* sth = NULL;
int cols = 0, i = 0, type = 0, rows = 0;
const char* d = NULL;
int j = 0, k = 0;
rc = sqlite3_open(argv[1], &dbh);
#ifdef _DEBUG
fprintf(stderr, "res: %d\n", rc);
#endif
if (rc != SQLITE_OK) { return -1; }
#ifdef _DEBUG
fprintf(stderr, "dbh: %p\n", dbh);
#endif
rc = sqlite3_key(dbh, key, sizeof(key));
#ifdef _DEBUG
fprintf(stderr, "res: %d\n", rc);
#endif
if (rc != SQLITE_OK) { return -1; }
rc = sqlite3_prepare(dbh, sql, sizeof(sql) - 1, &sth, NULL);
#ifdef _DEBUG
fprintf(stderr, "res: %d\n", rc);
#endif
if (rc != SQLITE_OK) { return -1; }
#ifdef _DEBUG
fprintf(stderr, "sth: %p\n", sth);
#endif
while (1) {
rc = sqlite3_step(sth);
if (rc == SQLITE_ROW) {
cols = sqlite3_column_count(sth);
rows++;
for (i=0;i<cols;++i) {
type = sqlite3_column_type(sth, i);
switch (type) {
case SQLITE_INTEGER:
#ifdef _DEBUG
fprintf(stderr, "(int) %d\n", sqlite3_column_int(sth, i));
#endif
break;
case SQLITE_FLOAT:
#ifdef _DEBUG
fprintf(stderr, "(float) %f\n", sqlite3_column_double(sth, i));
#endif
break;
case SQLITE_TEXT:
#ifdef _DEBUG
fprintf(stderr, "(text) %d\n", sqlite3_column_bytes(sth, i));
#endif
break;
case SQLITE_BLOB:
#ifdef _DEBUG
fprintf(stderr, "(blob) %d\n", sqlite3_column_bytes(sth, i));
#endif
d = sqlite3_column_blob(sth, i);
k = sqlite3_column_bytes(sth, i);
for (j=0;j<k;++j) {
fprintf(stdout, "%02x", d[j]);
}
printf("\n");
break;
case SQLITE_NULL:
#ifdef _DEBUG
fprintf(stderr, "(null)\n");
#endif
break;
default:
return -1;
}
}
} else if (rc == SQLITE_DONE) {
break;
} else {
return -1;
}
}
rc = sqlite3_finalize(sth);
#ifdef _DEBUG
fprintf(stderr, "res: %d\n", rc);
#endif
if (rc != SQLITE_OK) { return -1; }
rc = sqlite3_close(dbh);
#ifdef _DEBUG
fprintf(stderr, "res: %d\n", rc);
#endif
if (rc != SQLITE_OK) { return -1; }
fprintf(stderr, "Done. Exported %d records.\n", rows);
return 0;
}
64617461 // "data"
0a000000 // 10 (elements?)
7f000000 // 127 (somehow related to length...?)
000100040800000000 // no idea
ffffffbb // ffffffbb shows up 10 times, same as the "element count" value above
ffffffe4 // seems to mark the beginning of a field? maybe?
000600000000000000000200041800000003
ffffffbb
ffffffe4
0041006e0069006d0065002d0045006d00700069007200650000 // ucs2-be/utf16-be string terminated with a null character
0300040600000003
ffffffbb
ffffffe4
0041002d00450000 // another string - "A-E"
0400041b00000002
ffffffbb
ffffffe4
00687474703a2f2f7777772e616e696d652d656d706972652e6e6574000600041100000002
ffffffbb
ffffffe4
006972632e636861747370696b652e6e6574000700040d00000002
ffffffbb
ffffffe4
0023416e696d652d456d70697265000a00040800000000
ffffffbb
ffffffe4
005b
ffffffe9
ffffffc2
3c00000000000b00040800000000
ffffffbb
ffffffe4
000a
ffffffd6
ffffffb6
4600000000000c00040800000000
ffffffbb
ffffffe4
00
ffffffef
02000000000000000d00040800000000
ffffffbb
ffffffe4
005304000000000000
@deoxxa
Copy link
Author

deoxxa commented May 15, 2018

Yikes, this is six years old. I'd be very surprised if any of this is relevant anymore.

Getting the key is your responsibility though, if this stuff does still work. Shouldn't take you too long if you can operate a debugger :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment