/* cdpreader -- Trainz CDP file reader/analyser * * Dan Horák * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * (C) Copyright 2010 Daniel Horák */ #include #include #include #include #include struct CDPHeader { char header[4]; int32_t version; int32_t val1; int32_t length; }; int readblock(int f) { int32_t block_length; int8_t name_length, value_type; char name[256]; int32_t int_value, kuid1, kuid2; float float_value; char char_value; off_t filepos; int value_length; filepos = lseek(f, 0, SEEK_CUR); if (read(f, &block_length, sizeof(block_length)) <= 0) return -1; if (read(f, &name_length, sizeof(name_length)) < 0) return -1; if (read(f, name, name_length) < 0) return -1; if (read(f, &value_type, sizeof(value_type)) < 0) return -1; value_length = block_length - 1 - name_length - 1; printf("block: start=0x%08x length=%ld name=%s value_type=0x%02x value_length=%d\n", filepos, block_length, name, value_type, value_length); switch (value_type) { case 0x00: return readblock(f); break; case 0x01: while (value_length > 0) { read(f, &int_value, sizeof(int32_t)); printf("int=%d\n", int_value); value_length -= 4; } break; case 0x02: while (value_length > 0) { read(f, &float_value, sizeof(float)); printf("float=%f\n", float_value); value_length -= 4; } break; case 0x03: printf("string="); while (value_length-- > 0) { read(f, &char_value, 1); if (char_value) printf("%c", char_value); } printf("\n"); break; case 0x04: lseek(f, value_length, SEEK_CUR); printf("skipping content\n"); break; case 0x0d: read(f, &kuid1, sizeof(int32_t)); read(f, &kuid2, sizeof(int32_t)); printf("kuid=%d:%d\n", kuid1, kuid2); break; default: lseek(f, value_length, SEEK_CUR); printf("unknown type 0x%02x, skipping content\n", value_type); break; } return 0; } int main(int argc, char **argv) { int f; char *file; struct CDPHeader cdp; int res; if (argc != 2) { printf("Usage: %s \n", argv[0]); return 1; } file = argv[1]; if ((f = open(argv[1], O_RDONLY)) == -1) { printf("can't open %s\n", file); return 2; } printf("checking file: %s\n", file); if (read(f, &cdp, sizeof(cdp)) == -1) { printf("can't read %s\n", file); close(f); return 2; } if (strncmp(cdp.header, "ACS$", 4) != 0) { printf("file %s is not CDP\n", file); } printf("version=%d val1=%d lenght=%ld\n", cdp.version, cdp.val1, cdp.length); while (res == 0) { res = readblock(f); } close(f); return 0; }