bin2calc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * GTools C compiler
  3. * =================
  4. * source file :
  5. * binary to TI calculator format conversion
  6. *
  7. * Copyright 2001-2004 Paul Froissart.
  8. * Credits to Christoph van Wuellen and Matthew Brandt.
  9. * All commercial rights reserved.
  10. *
  11. * This compiler may be redistributed as long there is no
  12. * commercial interest. The compiler must not be redistributed
  13. * without its full sources. This notice must stay intact.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "define.h"
  19. #include "gtpack/gtpack.c"
  20. // This structure comes from ttools.
  21. typedef struct {
  22. char signature[8]; // "**TI92P*" or "**TI89**"
  23. unsigned char fill1[2]; // 01 00
  24. char folder[8]; // folder name
  25. char desc[40]; // ---- not used ----
  26. unsigned char fill2[6]; // 01 00 52 00 00 00
  27. char name[8]; // varname
  28. unsigned char type[4]; // 0C 00 00 00
  29. unsigned char size[4]; // complete file size (including checksum)
  30. unsigned char fill3[6]; // A5 5A 00 00 00 00
  31. unsigned char datasize[2]; // data size
  32. } TI_FILE_HDR;
  33. static void put_little_endian(unsigned char *dest, unsigned long val, int len) {
  34. while (len--) {
  35. *dest++ = (unsigned char)(val&0xFF);
  36. val >>= 8;
  37. }
  38. }
  39. static void put_big_endian(unsigned char *dest, unsigned long val, int len) {
  40. dest += len;
  41. while (len--) {
  42. *--dest = (unsigned char)(val&0xFF);
  43. val >>= 8;
  44. }
  45. }
  46. char *create_ti_file(int calc,int tigl_type,char *name,char *folder,char *content,unsigned long content_size,unsigned long *ti_file_size) {
  47. TI_FILE_HDR h;
  48. memset(&h, 0, sizeof h);
  49. memcpy(h.signature, "********", 8);
  50. #define sig(x) memcpy(h.signature+2,x,sizeof(x)-1)
  51. switch (calc) {
  52. case 0:
  53. sig("TI89");
  54. break;
  55. case 1:
  56. sig("TI92P");
  57. break;
  58. case 2:
  59. sig("TI92P");
  60. break;
  61. default:
  62. fatal("unknown calc type");
  63. }
  64. #undef sig
  65. h.fill1[0] = 1;
  66. strncpy(h.folder, folder?folder:"main", 8);
  67. h.fill2[0] = 1; h.fill2[2] = 0x52;
  68. strncpy(h.name, name, 8);
  69. h.type[0] = tigl_type; h.type[2] = 3;
  70. h.fill3[0] = 0xA5; h.fill3[1] = 0x5A;
  71. {
  72. unsigned long oncalc_size = 88+content_size+2;
  73. put_little_endian(h.size, oncalc_size, 4);
  74. }
  75. put_big_endian(h.datasize, content_size, 2);
  76. if (content_size>=65520)
  77. fatal("TIOS variables can't be more than 65520 bytes long.");
  78. {
  79. char *ret = malloc(sizeof(h)+content_size+2);
  80. if (!ret)
  81. fatal("Memory");
  82. if (ti_file_size)
  83. *ti_file_size = sizeof(h)+content_size+2;
  84. memcpy(ret, &h, sizeof(h));
  85. memcpy(ret+sizeof(h), content, content_size);
  86. {
  87. unsigned int crc = h.datasize[0]+h.datasize[1];
  88. unsigned long n = content_size;
  89. while (n--)
  90. crc += 0xff & (unsigned char)*content++;
  91. put_little_endian(ret+sizeof(h)+content_size, crc, 2);
  92. }
  93. return ret;
  94. }
  95. }