summaryrefslogtreecommitdiff
path: root/src/export
diff options
context:
space:
mode:
Diffstat (limited to 'src/export')
-rw-r--r--src/export/convert_coords.c40
-rw-r--r--src/export/extract_version_2.c131
-rw-r--r--src/export/extract_version_3.c61
-rw-r--r--src/export/map_coords.c62
-rw-r--r--src/export/mystdlib.c56
-rw-r--r--src/export/mystdlib.h32
-rw-r--r--src/export/split_version_2.c42
7 files changed, 424 insertions, 0 deletions
diff --git a/src/export/convert_coords.c b/src/export/convert_coords.c
new file mode 100644
index 0000000..37d780a
--- /dev/null
+++ b/src/export/convert_coords.c
@@ -0,0 +1,40 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
4int main(int argc, char *argv[])
5{
6 double x, y;
7 char buf[64];
8 int in;
9 while( fgets( buf, sizeof(buf), stdin ) )
10 {
11 if( sscanf( buf, "%lf %lf", &x, &y ) == 2 ) {
12 double R = 6365000;
13 double fe = 5200000;
14 double fn = 1200000;
15 double ph0 = 0.5235977; // 30deg
16 double ph1 = 0.7853980; // 45deg
17 double ph2 = 0.9599309; // 55deg
18 double l0 = 0.1745329; // 10deg
19
20 double xs = (x-fe)/R;
21 double ys = (y-fn)/R;
22 double ph0_s = 0.25*M_PI+0.5*ph0;
23 double ph1_s = 0.25*M_PI+0.5*ph1;
24 double ph2_s = 0.25*M_PI+0.5*ph2;
25
26 double n = log(cos(ph1)/cos(ph2))/log(tan(ph2_s)/tan(ph1_s));
27 double F = cos(ph1)*pow(tan(ph1_s),n)/n;
28 double r0 = F / pow(tan(ph0_s),n);
29 double r = sqrt(pow(xs,2)+pow(r0-ys,2));
30 double th = atan(xs/(r0-ys));
31
32 double lon = l0+th/n;
33 double lat = 2.0*atan(pow(F/r,1.0/n))-0.5*M_PI;
34
35 printf("%lf\t%lf\n", lat*180.0/M_PI, lon*180.0/M_PI);
36 } else
37 printf("\t\n");
38 }
39 return 0;
40}
diff --git a/src/export/extract_version_2.c b/src/export/extract_version_2.c
new file mode 100644
index 0000000..fe85252
--- /dev/null
+++ b/src/export/extract_version_2.c
@@ -0,0 +1,131 @@
1#include <fcntl.h>
2#include "mystdlib.h"
3#include <stdlib.h>
4#include <string.h>
5#include <stdint.h>
6#include <unistd.h>
7
8/* lha header:
9
1000 Header length
1101 Header checksum [02-length]
1202 0x2d ('-')
1303 0x6c ('l')
1404 0x68 ('h')
1505 0x?? ('0' or '5') unsure
1606 0x2d ('-')
1707 0x?? LSB of compressed size
1808 0x?? ..
1909 0x00 ..
2010 0x00 MSB of compressed size, i.e. 0
21..
2221 Length of path name
23
24
25*/
26
27static uint8_t mantra_in[] = { 0x68, 0x35, 0x2d, 0x6c };
28
29int main( int args, char **argv )
30{
31 int filenum = 0, run = 1;
32 size_t offset = 0, oldoffset = -1, reported = 0, enc_len = 32;
33 uint8_t mantra[4], id0, id5, *mappedfile;
34 MAP map;
35
36 /* For streets we do have a enc_len of 34 */
37 while( run ) {
38 switch( getopt( args, argv, ":e:" ) ) {
39 case -1 : run = 0; break;
40 case 'e':
41 enc_len = atol( optarg );
42 break;
43 default:
44 fputs( "Syntax: %s [-e encrypted_length (default: 32, for streets 34 or 0)] path-to-teiln.dat", stderr ); exit( 1 );
45 break;
46 }
47 }
48 run = 1;
49
50 if( optind == args )
51 { fputs( "Missing filename.", stderr ); exit( 1 ); }
52
53 map = map_file( argv[optind], 1 );
54 mappedfile = map->addr;
55
56 mantra[0] = mantra_in[0] ^ mappedfile[4];
57 mantra[1] = mantra_in[1] ^ mappedfile[5];
58 mantra[2] = mantra_in[2] ^ mappedfile[2];
59 mantra[3] = mantra_in[3] ^ mappedfile[3];
60
61 id0 = mappedfile[0];
62 id5 = mappedfile[5];
63
64 while( run )
65 {
66 while( ( offset < map->size ) && (
67 ( mappedfile[ offset + 0 ] != id0 ) ||
68 ( mappedfile[ offset + 2 ] != ( '-' ^ mantra[2] )) ||
69 ( mappedfile[ offset + 3 ] != ( 'l' ^ mantra[3] )) ||
70 ( mappedfile[ offset + 4 ] != ( 'h' ^ mantra[0] )) ||
71 ( mappedfile[ offset + 5 ] != id5 ) ||
72 ( mappedfile[ offset + 6 ] != ( '-' ^ mantra[2] ))
73 ) ) offset++;
74
75 // printf( "Found an appropriate offset at: %zd\n", offset );
76
77 if( reported < ( offset * 10 ) / map->size )
78 {
79 reported++;
80 printf( "%zd%% ", 10 * reported );
81 fflush( stdout );
82 }
83
84 if( offset == map->size )
85 run = 0;
86
87 if( oldoffset != -1 )
88 {
89 uint8_t *mf = mappedfile + oldoffset, df[128];
90 size_t filename_len, header_len;
91 char filename_template[32], filename[32];
92 int i;
93
94 /* De-"crypt" obfuscation to our header copy */
95 for( i=0; i<enc_len; ++i)
96 df[i] = mf[i] ^ mantra[i%4];
97
98 /* Get values from LHA header */
99 header_len = df[0] + 2;
100 filename_len = df[21];
101
102 /* Copy rest of header, so we can checksum */
103 for( i=enc_len; i<header_len; ++i)
104 df[i] = mf[i];
105
106 /* Make up new sequental file name */
107 snprintf( filename_template, sizeof(filename_template), "%%0%dd.lha", (int)filename_len );
108 snprintf( filename, sizeof( filename ), filename_template, filenum++ );
109 memcpy( ((uint8_t*)df) + 22, filename, filename_len);
110
111 /* Recalculate checksum with new file name */
112 df[1] = 0; for( i=2; i<header_len; ++i) df[1] += df[i];
113
114 /* Open file and dump our de-"crypted" header and then rest of file */
115 i = open( filename, O_CREAT | O_TRUNC | O_WRONLY, 0644 );
116 if( enc_len > header_len ) {
117 write( i, df, enc_len );
118 write( i, mf + enc_len, offset - oldoffset - enc_len );
119 } else {
120 write( i, df, header_len );
121 write( i, mf + header_len, offset - oldoffset - header_len );
122 }
123 close( i );
124 }
125 oldoffset = offset;
126 offset++;
127 }
128
129 unmap_file( &map );
130 return 0;
131}
diff --git a/src/export/extract_version_3.c b/src/export/extract_version_3.c
new file mode 100644
index 0000000..fef4241
--- /dev/null
+++ b/src/export/extract_version_3.c
@@ -0,0 +1,61 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <string.h>
6#include <zlib.h>
7#include "mystdlib.h"
8
9#define XORLEN (29)
10#define HUGEBLOCK (1024*1024)
11
12int main(int argc, char **argv) {
13 MAP in = map_file( argv[1], 1 );
14
15 unsigned const char xorkey [XORLEN] = "Just for Fun. Linus Torvalds.";
16 unsigned char input [XORLEN];
17 unsigned char output [HUGEBLOCK];
18 char respath[32]; /* file_XXXXX\0 */
19 int i, zres = 0, filenum = 0, resfile;
20 size_t offs = 0, reported = 0;
21
22 z_stream z; memset( &z, 0, sizeof(z));
23
24 while( offs < in->size - XORLEN ) {
25 for( i=0; i<XORLEN; ++i ) input[i] = in->addr[offs+i] ^ xorkey[i];
26 z.next_in = input; z.avail_in = XORLEN;
27 z.next_out = output; z.avail_out = HUGEBLOCK;
28 inflateInit( &z ); zres = inflate( &z, Z_NO_FLUSH );
29 if( (zres != Z_OK) && (zres != Z_STREAM_END) )
30 goto error_continue;
31
32 z.next_in = in->addr + offs + XORLEN; z.avail_in = in->size - offs - XORLEN;
33 while( zres == Z_OK ) zres = inflate( &z, Z_NO_FLUSH );
34
35 if( zres != Z_STREAM_END ) {
36error_continue:
37 inflateEnd(&z); memset( &z, 0, sizeof(z));
38 offs++;
39 continue;
40 }
41
42 sprintf( respath, "file_%05X", filenum++ );
43 resfile = open( respath, O_RDWR | O_CREAT, 0644 );
44 if( resfile < 0 ) {
45 fprintf( stderr, "Could not open output file %s\n", respath );
46 exit(1);
47 }
48 write( resfile, output, z.total_out );
49 close( resfile );
50 offs += z.total_in;
51
52 if( reported < ( offs * 10 ) / in->size ) {
53 reported++;
54 printf( "%zd%% ", 10 * reported );
55 fflush( stdout );
56 }
57
58 inflateEnd(&z); memset( &z, 0, sizeof(z));
59 }
60 unmap_file(&in);
61}
diff --git a/src/export/map_coords.c b/src/export/map_coords.c
new file mode 100644
index 0000000..b46f1cf
--- /dev/null
+++ b/src/export/map_coords.c
@@ -0,0 +1,62 @@
1#define _WITH_GETLINE
2#include "mystdlib.h"
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <ctype.h>
7
8int find_offset( const void *key, const void *line )
9{
10 size_t l = strlen( (char*)key );
11 return strncmp( (char*)key, *(char**)line, l );
12}
13
14int qsort_cmp( const void *a, const void *b )
15{
16 return strcmp( *(char**)a, *(char**)b );
17}
18
19int main( int argc, char ** args )
20{
21 MAP coords = map_file( args[1], 1 );
22 int i, l, lines;
23 char *p, **offsets, *input = malloc(1024);
24 ssize_t ll;
25 size_t input_length = 1024;
26
27 if( !coords ) exit( 111 );
28 p = (char *)coords->addr;
29 for ( i=0, lines=0; i<coords->size; ++i )
30 if( p[i] == 0x00 )
31 ++lines;
32
33 offsets = malloc( lines * sizeof(char*));
34 if( !offsets ) exit( 111 );
35
36 offsets[0] = p; l = 1;
37 for ( i=0; i<coords->size; ++i )
38 if( p[i] == 0x00 )
39 offsets[l++] = p+i+1;
40
41 l--; qsort(offsets, l, sizeof(char*), qsort_cmp );
42
43 while( ( ll = getline( &input, &input_length, stdin ) ) >= 0 )
44 {
45 char **coord_line;
46 input[ll-1]='\t';
47 coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset );
48 if( !coord_line && ll > 2 && isalpha( input[ll-2] ) )
49 {
50 input[ll-2] = '\t'; input[ll-1]=0;
51 ll--;
52 coord_line = bsearch( input, offsets, l, sizeof(char*), find_offset );
53 }
54
55 if( coord_line )
56 printf( "%s\n", *coord_line + ll );
57 else
58 puts( "\t" );
59 }
60
61 return 0;
62}
diff --git a/src/export/mystdlib.c b/src/export/mystdlib.c
new file mode 100644
index 0000000..17f123b
--- /dev/null
+++ b/src/export/mystdlib.c
@@ -0,0 +1,56 @@
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <sys/mman.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9
10#include "mystdlib.h"
11
12MAP map_file( char *filename, int readonly )
13{
14 struct stat fstatus;
15 MAP map = (MAP)malloc( sizeof( *map ));
16
17 if( map )
18 {
19 memset( map, 0, sizeof( *map ));
20
21 if( ( map->fh = open( filename, readonly ? O_RDONLY : O_RDWR ) ) >= 0 )
22 {
23 fstat( map->fh, &fstatus );
24 if( ( map->addr = mmap( NULL, map->size = (size_t)fstatus.st_size,
25 PROT_READ | ( readonly ? 0 : PROT_WRITE), (readonly ? MAP_PRIVATE : MAP_SHARED), map->fh, 0) ) == MAP_FAILED )
26 {
27 fprintf( stderr, "Mapping file '%s' failed\n", filename );
28 close( map->fh ); free( map ); map = NULL;
29 }
30 } else {
31 fprintf( stderr, "Couldn't open file: '%s'\n", filename );
32 free( map ); map = NULL;
33 }
34 } else {
35 fputs( "Couldn't allocate memory", stderr );
36 }
37
38 return map;
39}
40
41void unmap_file ( MAP *pMap )
42{
43 if( !pMap || !*pMap ) return;
44 munmap( (*pMap)->addr, (*pMap)->size);
45 close( (*pMap)->fh);
46 free( *pMap ); *pMap = NULL;
47}
48
49int getfilesize( int fd, unsigned long *size)
50{
51 struct stat sb;
52 int ret;
53 if( fstat( fd, &sb )) return -1;
54 *size = sb.st_size;
55 return 0;
56}
diff --git a/src/export/mystdlib.h b/src/export/mystdlib.h
new file mode 100644
index 0000000..2e9499f
--- /dev/null
+++ b/src/export/mystdlib.h
@@ -0,0 +1,32 @@
1#include <sys/types.h>
2#include <stdio.h>
3
4typedef struct { int fh; unsigned char *addr; size_t size; } *MAP;
5
6/* Mapps a file into memory
7 returns pointer to the mapping struct,
8 containing the file's size, the mapped
9 address and its file handle.
10
11 If readonly is true, the file will be
12 opened and mapped read only. File is
13 opened and mapped writable, if false.
14
15 Returns NULL if memory could not be
16 allocated, file could not be opened or
17 mapped. Gives out an diagnostic message
18 on stderr
19*/
20MAP map_file( char *filename, int readonly );
21
22/* Unmapps a file from memory. NULL pointer
23 checks are being done, so this is safe
24 to be called from cleanup without knowing
25 whether there actually is a map.
26*/
27void unmap_file ( MAP *pMap );
28
29/* Gets file size of open file
30 returns != 0 in case of error */
31inline int getfilesize( int fd, unsigned long *size );
32
diff --git a/src/export/split_version_2.c b/src/export/split_version_2.c
new file mode 100644
index 0000000..bd85775
--- /dev/null
+++ b/src/export/split_version_2.c
@@ -0,0 +1,42 @@
1#include <stdint.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <stdlib.h>
6#include <string.h>
7
8int main( int argc, char **args ) {
9 char table[64], f[1024*1024*16];
10 int outfiles[64], i, off, base = 0;
11 uint32_t fixed_columns = 0;
12
13 if( argc > 1 ) base = atol( args[1] );
14 if( argc > 2 ) fixed_columns = atol( args[2] );
15
16 for( i=0; i<64; ++i ) outfiles[i] = -1;
17 while( fgets( table, sizeof(table), stdin ) ) {
18 int off = ( table[strlen(table)-1] = 0 ); /* fgets sucks */
19 int f_in = open( table, O_RDONLY );
20 size_t s_in = read( f_in, f, sizeof(f));
21 uint32_t *p = (uint32_t*)f;
22 uint32_t count = p[0], columns = fixed_columns ? fixed_columns : p[1] / 4 - 1;
23 unsigned int file, strnr;
24
25 close(f_in);
26
27 for( file=0; file<columns; ++file ) {
28 /* Create outfile, if it is not yet there */
29 if( outfiles[file] == -1 ) {
30 sprintf( table, "%02d_unknown", file+base );
31 outfiles[file] = open( table, O_WRONLY | O_APPEND | O_CREAT, 0644 );
32 if ( outfiles[file] == -1 ) exit(1);
33 }
34 off = p[file+1];
35 /* Look for end of this chunk, which is <count> strings long */
36 for( strnr=0; strnr < count; ++strnr ) { while( f[off++] ) {}; f[off-1] = '\n'; }
37 write( outfiles[file], f + p[file+1], off - p[file+1] );
38 }
39 }
40 for( i=0; i<64; ++i ) close( outfiles[i] );
41 return 0;
42}