TNS File Format

From Hackspire
Revision as of 19:46, 25 June 2009 by Smarter (talk | contribs) (typo)
Jump to navigation Jump to search

TNS File Format

.tns is the file format of the TI N-Spire documents. It contains several XML files, for more informations on its content see: Document management. This page only discusses the .TNS file format itself.

History

The first tns files were standard zip files. At some point(which firmware version?), the format changed and the files stopped to be openable by zip utilities. If you are unfamiliar with the zip format, please refer to the official specification to better understand the following text.

The new format

It appears that the new format is just a zip file in disguise. It contains two differences which prevent utilities like the "file" command from detecting it.

Magic header

Instead of:

PK\003\004

The header is:

*TIMLP

Followed by what seems to be a version number. Known version numbers are 0120, 0200, 0300. No difference could be found between the files in these formats, further investigation is probably needed.

End of central directory

Instead of:

PK\005\006

The end of central directory also known as digital signature is:

TIPD

Converting to a standard zip file

You can use the following Perl script to do the conversion:

#!/usr/bin/perl
## Convert .TNS files to standard .ZIP files
## Adapted from http://forums.devshed.com/perl-programming-6/perl-searching-and-replacing-an-hex-pattern-514905.html  

use strict;

  if( $#ARGV < 0 ){
    print "This script converts TI N-Spire .TNS files to standard .ZIP files\n";
    print "USAGE: $0 input.tns output.zip\n";
    exit(0);
}

my $hex_in = $ARGV[0];
my $hex_out = $ARGV[1];

open (HEX_IN, "$hex_in") or die("open failed for $hex_in : $!");
open (HEX_OUT, ">$hex_out") or die("open failed for $hex_out : $!");
binmode(HEX_IN);
binmode(HEX_OUT);
local $/;
my $string = <HEX_IN>;

$string =~ s/\*TIMLP[0-9]{4}(.*)/PK\x03\x04$1/gi;

$string =~ s/(.*)TIPD(.*)/$1PK\x05\x06$2/gi;

## FIXME: Make sure that the actual size of the data and the size indicated in the zip file are the same

print HEX_OUT $string;

close(HEX_IN);
close(HEX_OUT);

The output file will be an (almost, as the number of bytes have changed, zip utilities will complain) valid zip file. Trying to decompress it using the "unzip" utility gives the following result:

Archive:  output.zip
error [output.zip]:  missing 6 bytes in zipfile
  (attempting to process anyway)
   skipping: Document.xml            unsupported compression method 13
   skipping: Problem1.xml            unsupported compression method 13

The files in the zip files are detected, but can't be opened due to the use of an unsupported compression method.

TI Compression Method 13

According to the zip specification, there is no compression method 13 at the moment. Trying to change the compression method bytes to something else doesn't work either, so it appears that TI has created its own compression method. There is currently no known way to get data from it, further investigations are required.

A magic number ?

All the files tested so far contain the following hexadecimal numbers right at the beginning of the file data field(see the zip specification):

OF CE D8 D2 81 06 86 5B

No known compression method seem to use this magic number, finding its origin might help in understanding TI compression method.

Reasons for the use of this compression method

If we can understand why TI choosed to change the compression method used, we might understand how it work too.

  • Prevent people from fiddling with the XML inside? This could mean that there is a way to use it to hack the calculator.
  • Prevent people from using different tools that the official nspire software for editing documents? Not sure if it's a big source of incomes for them.
  • Make smaller documents(this could be easily checked)? After all, this is the main use of compression, and size on the calculator is limited, but are the files stored in the same format in the calculator?
  • Provide faster decompression? Standard compression methods already provide a wide range of choice, but who knows.