filetypes.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Hey EMACS -*- linux-c -*- */
  2. /* $Id: typesxx.c 912 2005-03-30 20:49:06Z roms $ */
  3. /* libtifiles - file format library, a part of the TiLP project
  4. * Copyright (C) 1999-2005 Romain Liévin
  5. * Copyright (C) 2007 Kevin Kofler
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #define g_ascii_strcasecmp strcasecmp
  26. #include "tifiles.h"
  27. #include "error.h"
  28. #include "rwfile.h"
  29. #include "sysdep.h"
  30. /****************/
  31. /* Global types */
  32. /****************/
  33. #define NCALCS FILES_NCALCS
  34. static const char GROUP_FILE_EXT[NCALCS + 1][4] =
  35. {
  36. "XxX",
  37. "73g", "82g", "83g", "8Xg", "8Xg", "85g", "86g",
  38. "89g", "89g", "92g", "9Xg", "V2g", "8Xg", "89g",
  39. };
  40. static const char BACKUP_FILE_EXT[NCALCS + 1][4] =
  41. {
  42. "XxX",
  43. "73b", "82b", "83b", "8Xb", "8Xb", "85b", "86b",
  44. "89g", "89g", "92b", "9Xg", "V2g", "8Xg", "89g",
  45. };
  46. static const char FLASH_APP_FILE_EXT[NCALCS + 1][4] =
  47. {
  48. "XxX",
  49. "73k", "???", "???", "8Xk", "8Xk", "???", "???",
  50. "89k", "89k", "???", "9Xk", "V2k", "8Xk", "89k",
  51. };
  52. static const char FLASH_OS_FILE_EXT[NCALCS + 1][4] =
  53. {
  54. "XxX",
  55. "73u", "???", "???", "8Xu", "8Xu", "???", "???",
  56. "89u", "89u", "???", "9Xu", "V2u", "8Xu", "89u",
  57. };
  58. static const char CERTIF_FILE_EXT[NCALCS + 1][4] =
  59. {
  60. "XxX",
  61. "73q", "???", "???", "8Xq", "8Xq", "???", "???",
  62. "89q", "89q", "???", "9Xq", "V2q", "8Xq", "89q",
  63. };
  64. /*******************/
  65. /* File extensions */
  66. /*******************/
  67. /**
  68. * tifiles_fext_get:
  69. * @filename: a filename as string.
  70. *
  71. * Returns file extension part.
  72. *
  73. * Return value: a file extension without dot as string (like "89g").
  74. **/
  75. TIEXPORT2 const char *TICALL tifiles_fext_get(const char *filename)
  76. {
  77. char *d = NULL;
  78. d = strrchr(filename, '.');
  79. if (d == NULL)
  80. return "";
  81. return (++d);
  82. }
  83. /**********************/
  84. /* Signature checking */
  85. /**********************/
  86. static int tifiles_file_has_ti_header(const char *filename)
  87. {
  88. FILE *f;
  89. char buf[9];
  90. char *p;
  91. f = gfopen(filename, "rb");
  92. if (f == NULL)
  93. return 0;
  94. fread_8_chars(f, buf);
  95. for(p = buf; *p != '\0'; p++)
  96. *p = toupper(*p);
  97. if (!strcmp(buf, "**TI73**") || !strcmp(buf, "**TI82**") ||
  98. !strcmp(buf, "**TI83**") || !strcmp(buf, "**TI83F*") ||
  99. !strcmp(buf, "**TI85**") || !strcmp(buf, "**TI86**") ||
  100. !strcmp(buf, "**TI89**") || !strcmp(buf, "**TI92**") ||
  101. !strcmp(buf, "**TI92P*") || !strcmp(buf, "**V200**") ||
  102. !strcmp(buf, "**TIFL**")) {
  103. fclose(f);
  104. return !0;
  105. }
  106. fclose(f);
  107. return 0;
  108. }
  109. #define TIB_SIGNATURE "Advanced Mathematics Software"
  110. static int tifiles_file_has_tib_header(const char *filename)
  111. {
  112. FILE *f;
  113. char str[128];
  114. const char *e = tifiles_fext_get(filename);
  115. if (!strcmp(e, ""))
  116. return 0;
  117. if(g_ascii_strcasecmp(e, "tib"))
  118. return 0;
  119. f = gfopen(filename, "rb");
  120. if(f == NULL)
  121. return 0;
  122. fread_n_chars(f, 22, str);
  123. fread_n_chars(f, strlen(TIB_SIGNATURE), str);
  124. str[strlen(TIB_SIGNATURE)] = '\0';
  125. if(!strcmp(str, TIB_SIGNATURE))
  126. {
  127. fclose(f);
  128. return !0;
  129. }
  130. return 0;
  131. }
  132. /**************/
  133. /* File types */
  134. /**************/
  135. #ifndef __WIN32__
  136. #include <sys/types.h>
  137. #include <sys/stat.h>
  138. #include <unistd.h>
  139. #endif
  140. static int is_regfile(const char *filename ATTRIBUTE_UNUSED)
  141. {
  142. #ifndef __WIN32__
  143. struct stat buf;
  144. if (stat(filename, &buf) < 0)
  145. return 0;
  146. if (S_ISREG(buf.st_mode))
  147. return !0;
  148. else
  149. return 0;
  150. #else
  151. return !0;
  152. #endif
  153. }
  154. /**
  155. * tifiles_file_is_ti:
  156. * @filename: a filename as string.
  157. *
  158. * Check whether file is a TI file by checking the signature.
  159. *
  160. * Return value: a boolean value.
  161. **/
  162. TIEXPORT2 int TICALL tifiles_file_is_ti(const char *filename)
  163. {
  164. // bug: check that file is not a FIFO
  165. if (!is_regfile(filename))
  166. return 0;
  167. if(tifiles_file_has_ti_header(filename))
  168. return !0;
  169. if(tifiles_file_has_tib_header(filename))
  170. return !0;
  171. return 0;
  172. }
  173. /**
  174. * tifiles_file_is_os:
  175. * @filename: a filename as string.
  176. *
  177. * Check whether file is a FLASH OS file (tib or XXu)
  178. *
  179. * Return value: a boolean value.
  180. **/
  181. TIEXPORT2 int TICALL tifiles_file_is_os(const char *filename)
  182. {
  183. int i;
  184. const char *e = tifiles_fext_get(filename);
  185. if (!strcmp(e, ""))
  186. return 0;
  187. if (!tifiles_file_is_ti(filename))
  188. return 0;
  189. if(tifiles_file_is_tib(filename))
  190. return !0;
  191. for (i = 1; i < NCALCS + 1; i++)
  192. {
  193. if (!g_ascii_strcasecmp(e, FLASH_OS_FILE_EXT[i]))
  194. return !0;
  195. }
  196. return 0;
  197. }
  198. /**
  199. * tifiles_file_is_app:
  200. * @filename: a filename as string.
  201. *
  202. * Check whether file is a FLASH app file
  203. *
  204. * Return value: a boolean value.
  205. **/
  206. TIEXPORT2 int TICALL tifiles_file_is_app(const char *filename)
  207. {
  208. int i;
  209. const char *e = tifiles_fext_get(filename);
  210. if (!strcmp(e, ""))
  211. return 0;
  212. if (!tifiles_file_is_ti(filename))
  213. return 0;
  214. for (i = 1; i < NCALCS + 1; i++)
  215. {
  216. if (!g_ascii_strcasecmp(e, FLASH_APP_FILE_EXT[i]))
  217. return !0;
  218. }
  219. return 0;
  220. }
  221. /**
  222. * tifiles_file_is_flash:
  223. * @filename: a filename as string.
  224. *
  225. * Check whether file is a FLASH file (os or app).
  226. *
  227. * Return value: a boolean value.
  228. **/
  229. TIEXPORT2 int TICALL tifiles_file_is_flash(const char *filename)
  230. {
  231. return tifiles_file_is_os(filename) || tifiles_file_is_app(filename);
  232. }
  233. /**
  234. * tifiles_file_is_tib:
  235. * @filename: a filename as string.
  236. *
  237. * Check whether file is a TIB formatted file.
  238. *
  239. * Return value: a boolean value.
  240. **/
  241. TIEXPORT2 int TICALL tifiles_file_is_tib(const char *filename)
  242. {
  243. return tifiles_file_has_tib_header(filename);
  244. }
  245. /********/
  246. /* Misc */
  247. /********/
  248. /* Note: a better way should be to open the file and read the signature */
  249. /**
  250. * tifiles_file_get_model:
  251. * @filename: a filename as string.
  252. *
  253. * Returns the calculator model targetted for this file.
  254. *
  255. * Return value: a model taken in #CalcModel.
  256. **/
  257. TIEXPORT2 CalcModel TICALL tifiles_file_get_model(const char *filename)
  258. {
  259. const char *ext = tifiles_fext_get(filename);
  260. int type = CALC_NONE;
  261. char str[3];
  262. if (!strcmp(ext, ""))
  263. return CALC_NONE;
  264. strncpy(str, ext, 2);
  265. str[2] = '\0';
  266. if (!g_ascii_strcasecmp(str, "73"))
  267. type = CALC_TI73;
  268. else if (!g_ascii_strcasecmp(str, "82"))
  269. type = CALC_TI82;
  270. else if (!g_ascii_strcasecmp(str, "83"))
  271. type = CALC_TI83;
  272. else if (!g_ascii_strcasecmp(str, "8x"))
  273. type = CALC_TI83P;
  274. else if (!g_ascii_strcasecmp(str, "85"))
  275. type = CALC_TI85;
  276. else if (!g_ascii_strcasecmp(str, "86"))
  277. type = CALC_TI86;
  278. else if (!g_ascii_strcasecmp(str, "89"))
  279. type = CALC_TI89;
  280. else if (!g_ascii_strcasecmp(str, "92"))
  281. type = CALC_TI92;
  282. else if (!g_ascii_strcasecmp(str, "9X"))
  283. type = CALC_TI92P;
  284. else if (!g_ascii_strcasecmp(str, "V2"))
  285. type = CALC_V200;
  286. //else if (!g_ascii_strcasecmp(str, "tib"))
  287. //type = CALC_TI89; // consider .tib as TI89
  288. else
  289. type = CALC_NONE;
  290. return type;
  291. }