/* * Mimas conversion tools * * Copyright (C) 2010 Benjamin Moody * * 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 3 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, see . */ #include #include #include #include "utils.h" #include "pack.h" static void pack_string(unsigned char **packed, int *length, const char *text, int textlength, const unsigned char *map) { unsigned int c; int i, j = 0, n = 0; *packed = xnew(unsigned char, textlength); for (i = 0; i < textlength; i++) { c = map[(int) (unsigned char) text[i]]; if (c & 0xf0) { if (n) { (*packed)[j - 1] |= c >> 4; (*packed)[j] = c << 4; j++; } else { (*packed)[j] = c; j++; } } else { if (n) { (*packed)[j - 1] |= c; n = 0; } else { (*packed)[j] = c << 4; j++; n = 1; } } } *length = j; } static const unsigned char cmt_map[] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, /* !"#$%&' */ 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, /* ()*+,-./ */ 0xCF, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA, 0xC9, 0xC8, /* 01234567 */ 0xC7, 0xC6, 0xC5, 0xC4, 0xC3, 0xC2, 0xC1, 0xC0, /* 89:;<=>? */ 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, /* @ABCDEFG */ 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, /* HIJKLM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 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3 }; /* Get width of character bitmap after comment packing */ int get_comment_char_width(char c) { unsigned char v = cmt_map[(int) (unsigned char) c]; switch (v) { case 0: return 1; case 0x04: /* i */ case 0xB1: /* ! */ case 0xB7: /* ' */ case 0xBE: /* . */ case 0xC5: /* : */ return 2; case 0x05: /* l */ case 0x09: /* s */ case 0x0A: /* t */ case 0xB0: /* [ */ case 0xB8: /* ( */ case 0xB9: /* ) */ case 0xBC: /* , */ case 0xC4: /* ; */ case 0xE2: /* ] */ case 0xF3: /* f */ return 3; case 0xB6: /* & */ case 0xE1: /* z */ return 5; case 0xB3: /* # */ case 0xB4: /* $ */ case 0xBA: /* * */ case 0xD0: /* @ */ case 0xF8: /* m */ case 0xFD: /* w */ return 6; default: return 4; } } /* Pack a comment string. Input must be in TI-83+ encoding. */ void pack_comment_string(unsigned char **packed, int *length, const char *text) { pack_string(packed, length, text, strlen(text), cmt_map); } static const unsigned char sym_map[] = { 0x0, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, 0x0, 0xB, /* ()*+,-./ */ 0xCF, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA, 0xC9, 0xC8, /* 01234567 */ 0xC7, 0xC6, 0xB, 0xB, 0xB, 0xB, 0xB, 0xB, /* 89:;<=>? */ 0xB, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, /* @ABCDEFG */ 0xD8, 0xD9, 0xDA, 0xDB, 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must be a valid symbol in TI-83+ encoding. */ void pack_symbol_string(unsigned char **packed, int *length, const char *text) { pack_string(packed, length, text, strlen(text) + 1, sym_map); }