bin2calc.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. // This structure comes from ttools.
  5. typedef struct {
  6. char signature[8]; // "**TI92P*" or "**TI89**"
  7. unsigned char fill1[2]; // 01 00
  8. char folder[8]; // folder name
  9. char desc[40]; // ---- not used ----
  10. unsigned char fill2[6]; // 01 00 52 00 00 00
  11. char name[8]; // varname
  12. unsigned char type[4]; // 0C 00 00 00
  13. unsigned char size[4]; // complete file size (including checksum)
  14. unsigned char fill3[6]; // A5 5A 00 00 00 00
  15. unsigned char datasize[2]; // data size
  16. } TI_FILE_HDR;
  17. static void put_little_endian(unsigned char *dest, unsigned long val, int len) {
  18. while (len--) {
  19. *dest++ = (unsigned char)(val&0xFF);
  20. val >>= 8;
  21. }
  22. }
  23. static void put_big_endian(unsigned char *dest, unsigned long val, int len) {
  24. dest += len;
  25. while (len--) {
  26. *--dest = (unsigned char)(val&0xFF);
  27. val >>= 8;
  28. }
  29. }
  30. char *create_ti_file(int calc,int tigl_type,char *name,char *folder,char *content,unsigned long content_size,unsigned long *ti_file_size) {
  31. TI_FILE_HDR h;
  32. memset(&h, 0, sizeof h);
  33. memcpy(h.signature, "********", 8);
  34. #define sig(x) memcpy(h.signature+2,x,sizeof(x)-1)
  35. switch (calc) {
  36. case 0:
  37. sig("TI89");
  38. break;
  39. case 1:
  40. sig("TI92P");
  41. break;
  42. case 2:
  43. sig("TI92P");
  44. break;
  45. default:
  46. fatal("unknown calc type");
  47. }
  48. #undef sig
  49. h.fill1[0] = 1;
  50. strncpy(h.folder, folder?folder:"main", 8);
  51. h.fill2[0] = 1; h.fill2[2] = 0x52;
  52. strncpy(h.name, name, 8);
  53. h.type[0] = tigl_type; h.type[2] = 3;
  54. h.fill3[0] = 0xA5; h.fill3[1] = 0x5A;
  55. {
  56. unsigned long oncalc_size = 88+content_size+2;
  57. put_little_endian(h.size, oncalc_size, 4);
  58. }
  59. put_big_endian(h.datasize, content_size, 2);
  60. if (content_size>=65520)
  61. fatal("TIOS variables can't be more than 65520 bytes long.");
  62. {
  63. char *ret = malloc(sizeof(h)+content_size+2);
  64. if (!ret)
  65. fatal("Memory");
  66. if (ti_file_size)
  67. *ti_file_size = sizeof(h)+content_size+2;
  68. memcpy(ret, &h, sizeof(h));
  69. memcpy(ret+sizeof(h), content, content_size);
  70. {
  71. unsigned int crc = h.datasize[0]+h.datasize[1];
  72. unsigned long n = content_size;
  73. while (n--)
  74. crc += 0xff & (unsigned char)*content++;
  75. put_little_endian(ret+sizeof(h)+content_size, crc, 2);
  76. }
  77. return ret;
  78. }
  79. }