You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

637 lines
19 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains various import functions.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined __KERNEL__
  19. # if defined HAVE_ERRNO_H
  20. # include <errno.h>
  21. # endif
  22. # include <stdio.h>
  23. # include <stdlib.h>
  24. # include <string.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. static inline uint32_t sscanu32(void const *s)
  29. {
  30. uint32_t x;
  31. memcpy(&x, s, 4);
  32. return hton32(x);
  33. }
  34. static inline uint16_t sscanu16(void const *s)
  35. {
  36. uint16_t x;
  37. memcpy(&x, s, 2);
  38. return hton16(x);
  39. }
  40. /* ANSI Graphic Rendition Combination Mode */
  41. struct ansi_grcm
  42. {
  43. uint8_t fg, bg; /* ANSI-context fg/bg */
  44. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  45. uint8_t bold, negative, concealed;
  46. };
  47. static long int import_caca(cucul_canvas_t *, void const *, unsigned int);
  48. static long int import_text(cucul_canvas_t *, void const *, unsigned int);
  49. static long int import_ansi(cucul_canvas_t *, void const *, unsigned int, int);
  50. static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
  51. unsigned int, unsigned int const *);
  52. /** \brief Import a memory buffer into a canvas
  53. *
  54. * Import a memory buffer into the given libcucul canvas's current
  55. * frame. The current frame is resized accordingly and its contents are
  56. * replaced with the imported data.
  57. *
  58. * Valid values for \c format are:
  59. * - \c "": attempt to autodetect the file format.
  60. * - \c "caca": import native libcaca files.
  61. * - \c "text": import ASCII text files.
  62. * - \c "ansi": import ANSI files.
  63. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  64. *
  65. * The number of bytes read is returned. If the file format is valid, but
  66. * not enough data was available, 0 is returned.
  67. *
  68. * If an error occurs, -1 is returned and \b errno is set accordingly:
  69. * - \c ENOMEM Not enough memory to allocate canvas.
  70. * - \c EINVAL Invalid format requested.
  71. *
  72. * \param cv A libcucul canvas in which to import the file.
  73. * \param data A memory area containing the data to be loaded into the canvas.
  74. * \param len The size in bytes of the memory area.
  75. * \param format A string describing the input format.
  76. * \return The number of bytes read, or 0 if there was not enough data,
  77. * or -1 if an error occurred.
  78. */
  79. long int cucul_import_memory(cucul_canvas_t *cv, void const *data,
  80. unsigned long int len, char const *format)
  81. {
  82. if(!strcasecmp("caca", format))
  83. return import_caca(cv, data, len);
  84. if(!strcasecmp("utf8", format))
  85. return import_ansi(cv, data, len, 1);
  86. if(!strcasecmp("text", format))
  87. return import_text(cv, data, len);
  88. if(!strcasecmp("ansi", format))
  89. return import_ansi(cv, data, len, 0);
  90. /* Autodetection */
  91. if(!strcasecmp("", format))
  92. {
  93. unsigned char const *str = data;
  94. unsigned int i;
  95. /* If 4 first bytes are 0xcaca + 'CV' */
  96. if(len >= 4 && str[0] == 0xca &&
  97. str[1] == 0xca && str[2] == 'C' && str[3] == 'V')
  98. return import_caca(cv, data, len);
  99. /* If we find ESC[ argv, we guess it's an ANSI file */
  100. for(i = 0; i + 1 < len; i++)
  101. if((str[i] == 0x1b) && (str[i + 1] == '['))
  102. return import_ansi(cv, data, len, 0);
  103. /* Otherwise, import it as text */
  104. return import_text(cv, data, len);
  105. }
  106. #if defined HAVE_ERRNO_H
  107. errno = EINVAL;
  108. #endif
  109. return -1;
  110. }
  111. /** \brief Import a file into a canvas
  112. *
  113. * Import a file into the given libcucul canvas's current frame. The
  114. * current frame is resized accordingly and its contents are replaced
  115. * with the imported data.
  116. *
  117. * Valid values for \c format are:
  118. * - \c "": attempt to autodetect the file format.
  119. * - \c "caca": import native libcaca files.
  120. * - \c "text": import ASCII text files.
  121. * - \c "ansi": import ANSI files.
  122. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  123. *
  124. * The number of bytes read is returned. If the file format is valid, but
  125. * not enough data was available, 0 is returned.
  126. *
  127. * If an error occurs, -1 is returned and \b errno is set accordingly:
  128. * - \c ENOSYS File access is not implemented on this system.
  129. * - \c ENOMEM Not enough memory to allocate canvas.
  130. * - \c EINVAL Invalid format requested.
  131. * cucul_import_file() may also fail and set \b errno for any of the
  132. * errors specified for the routine fopen().
  133. *
  134. * \param cv A libcucul canvas in which to import the file.
  135. * \param filename The name of the file to load.
  136. * \param format A string describing the input format.
  137. * \return The number of bytes read, or 0 if there was not enough data,
  138. * or -1 if an error occurred.
  139. */
  140. long int cucul_import_file(cucul_canvas_t *cv, char const *filename,
  141. char const *format)
  142. {
  143. #if defined __KERNEL__
  144. # if defined HAVE_ERRNO_H
  145. errno = ENOSYS;
  146. # endif
  147. return -1;
  148. #else
  149. FILE *fp;
  150. void *data;
  151. long int size;
  152. int ret;
  153. fp = fopen(filename, "rb");
  154. if(!fp)
  155. return -1; /* fopen already set errno */
  156. fseek(fp, 0, SEEK_END);
  157. size = ftell(fp);
  158. data = malloc(size);
  159. if(!data)
  160. {
  161. fclose(fp);
  162. # if defined HAVE_ERRNO_H
  163. errno = ENOMEM;
  164. # endif
  165. return -1;
  166. }
  167. fseek(fp, 0, SEEK_SET);
  168. fread(data, size, 1, fp);
  169. fclose(fp);
  170. ret = cucul_import_memory(cv, data, size, format);
  171. free(data);
  172. return ret;
  173. #endif
  174. }
  175. /** \brief Get available import formats
  176. *
  177. * Return a list of available import formats. The list is a NULL-terminated
  178. * array of strings, interleaving a string containing the internal value for
  179. * the import format, to be used with cucul_import_canvas(), and a string
  180. * containing the natural language description for that import format.
  181. *
  182. * This function never fails.
  183. *
  184. * \return An array of strings.
  185. */
  186. char const * const * cucul_get_import_list(void)
  187. {
  188. static char const * const list[] =
  189. {
  190. "", "autodetect",
  191. "caca", "native libcaca format",
  192. "text", "plain text",
  193. "ansi", "ANSI coloured text",
  194. "utf8", "UTF-8 files with ANSI colour codes",
  195. NULL, NULL
  196. };
  197. return list;
  198. }
  199. /*
  200. * XXX: the following functions are local.
  201. */
  202. static long int import_caca(cucul_canvas_t *cv,
  203. void const *data, unsigned int size)
  204. {
  205. uint8_t const *buf = (uint8_t const *)data;
  206. unsigned int control_size, data_size, expected_size, frames, f, n;
  207. uint16_t version, flags;
  208. if(size < 20)
  209. return 0;
  210. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  211. goto invalid_caca;
  212. control_size = sscanu32(buf + 4);
  213. data_size = sscanu32(buf + 8);
  214. version = sscanu16(buf + 12);
  215. frames = sscanu32(buf + 14);
  216. flags = sscanu16(buf + 18);
  217. if(size < 4 + control_size + data_size)
  218. return 0;
  219. if(control_size < 16 + frames * 32)
  220. goto invalid_caca;
  221. for(expected_size = 0, f = 0; f < frames; f++)
  222. {
  223. unsigned int width, height, duration;
  224. uint32_t attr;
  225. int x, y, handlex, handley;
  226. width = sscanu32(buf + 4 + 16 + f * 24);
  227. height = sscanu32(buf + 4 + 16 + f * 24 + 4);
  228. duration = sscanu32(buf + 4 + 16 + f * 24 + 8);
  229. attr = sscanu32(buf + 4 + 16 + f * 24 + 12);
  230. x = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 16);
  231. y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20);
  232. handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 24);
  233. handley = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 28);
  234. expected_size += width * height * 8;
  235. }
  236. if(expected_size != data_size)
  237. goto invalid_caca;
  238. /* FIXME: read all frames, not only the first one */
  239. cucul_set_canvas_size(cv, 0, 0);
  240. cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16),
  241. sscanu32(buf + 4 + 16 + 4));
  242. /* FIXME: check for return value */
  243. for(n = sscanu32(buf + 4 + 16) * sscanu32(buf + 4 + 16 + 4); n--; )
  244. {
  245. cv->chars[n] = sscanu32(buf + 4 + control_size + 8 * n);
  246. cv->attrs[n] = sscanu32(buf + 4 + control_size + 8 * n + 4);
  247. }
  248. cv->curattr = sscanu32(buf + 4 + 16 + 12);
  249. cv->frames[0].x = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 16);
  250. cv->frames[0].y = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 20);
  251. cv->frames[0].handlex = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 24);
  252. cv->frames[0].handley = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 28);
  253. return 4 + control_size + data_size;
  254. invalid_caca:
  255. #if defined HAVE_ERRNO_H
  256. errno = EINVAL;
  257. #endif
  258. return -1;
  259. }
  260. static long int import_text(cucul_canvas_t *cv,
  261. void const *data, unsigned int size)
  262. {
  263. char const *text = (char const *)data;
  264. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  265. cucul_set_canvas_size(cv, width, height);
  266. for(i = 0; i < size; i++)
  267. {
  268. unsigned char ch = *text++;
  269. if(ch == '\r')
  270. continue;
  271. if(ch == '\n')
  272. {
  273. x = 0;
  274. y++;
  275. continue;
  276. }
  277. if(x >= width || y >= height)
  278. {
  279. if(x >= width)
  280. width = x + 1;
  281. if(y >= height)
  282. height = y + 1;
  283. cucul_set_canvas_size(cv, width, height);
  284. }
  285. cucul_put_char(cv, x, y, ch);
  286. x++;
  287. }
  288. if(y > height)
  289. cucul_set_canvas_size(cv, width, height = y);
  290. return size;
  291. }
  292. static long int import_ansi(cucul_canvas_t *cv,
  293. void const *data, unsigned int size, int utf8)
  294. {
  295. struct ansi_grcm grcm;
  296. unsigned char const *buffer = (unsigned char const*)data;
  297. unsigned int i, j, skip, dummy = 0;
  298. unsigned int width = 0, height = 0, wch = 1;
  299. unsigned long int ch;
  300. int x = 0, y = 0, save_x = 0, save_y = 0;
  301. cucul_set_canvas_size(cv, width, height);
  302. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  303. for(i = 0; i < size; i += skip)
  304. {
  305. skip = 1;
  306. /* Wrap long lines */
  307. if((unsigned int)x >= 80)
  308. {
  309. x = 0;
  310. y++;
  311. }
  312. if(buffer[i] == '\x1a' && size - i >= 8
  313. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  314. break; /* End before SAUCE data */
  315. if(buffer[i] == '\r')
  316. continue; /* DOS sucks */
  317. if(buffer[i] == '\n')
  318. {
  319. x = 0;
  320. y++;
  321. continue;
  322. }
  323. /* Interpret escape commands, as per Standard ECMA-48 "Control
  324. * Functions for Coded Character Sets", 5.4. Control sequences. */
  325. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  326. {
  327. unsigned int argc = 0, argv[101];
  328. unsigned int param, inter, final;
  329. /* Compute offsets to parameter bytes, intermediate bytes and
  330. * to the final byte. Only the final byte is mandatory, there
  331. * can be zero of the others.
  332. * 0 param=2 inter final final+1
  333. * +-----+------------------+---------------------+-----------------+
  334. * | CSI | parameter bytes | intermediate bytes | final byte |
  335. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  336. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  337. * +-----+------------------+---------------------+-----------------+
  338. */
  339. param = 2;
  340. for(inter = param; i + inter < size; inter++)
  341. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  342. break;
  343. for(final = inter; i + final < size; final++)
  344. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  345. break;
  346. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  347. break; /* Invalid Final Byte */
  348. skip += final;
  349. /* Sanity checks */
  350. if(param < inter && buffer[i + param] >= 0x3c)
  351. {
  352. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  353. final - param + 1, buffer + i + param);
  354. continue; /* Private sequence, skip it entirely */
  355. }
  356. if(final - param > 100)
  357. continue; /* Suspiciously long sequence, skip it */
  358. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  359. * format */
  360. if(param < inter)
  361. {
  362. argv[0] = 0;
  363. for(j = param; j < inter; j++)
  364. {
  365. if(buffer[i + j] == ';')
  366. argv[++argc] = 0;
  367. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  368. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  369. }
  370. argc++;
  371. }
  372. /* Interpret final byte. The code representations are given in
  373. * ECMA-48 5.4: Control sequences, and the code definitions are
  374. * given in ECMA-48 8.3: Definition of control functions. */
  375. switch(buffer[i + final])
  376. {
  377. case 'f': /* CUP - Cursor Position */
  378. case 'H': /* HVP - Character And Line Position */
  379. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  380. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  381. break;
  382. case 'A': /* CUU - Cursor Up */
  383. y -= argc ? argv[0] : 1;
  384. if(y < 0)
  385. y = 0;
  386. break;
  387. case 'B': /* CUD - Cursor Down */
  388. y += argc ? argv[0] : 1;
  389. break;
  390. case 'C': /* CUF - Cursor Right */
  391. x += argc ? argv[0] : 1;
  392. break;
  393. case 'D': /* CUB - Cursor Left */
  394. x -= argc ? argv[0] : 1;
  395. if(x < 0)
  396. x = 0;
  397. break;
  398. case 's': /* Private (save cursor position) */
  399. save_x = x;
  400. save_y = y;
  401. break;
  402. case 'u': /* Private (reload cursor position) */
  403. x = save_x;
  404. y = save_y;
  405. break;
  406. case 'J': /* ED - Erase In Page */
  407. if(argv[0] == 2)
  408. x = y = 0;
  409. break;
  410. case 'K': /* EL - Erase In Line */
  411. if(width < 80)
  412. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  413. cucul_set_canvas_size(cv, width = 80, height);
  414. for(j = x; j < 80; j++)
  415. cucul_put_char(cv, j, y, ' ');
  416. x = 80;
  417. break;
  418. case 'm': /* SGR - Select Graphic Rendition */
  419. ansi_parse_grcm(cv, &grcm, argc, argv);
  420. break;
  421. default:
  422. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  423. break;
  424. }
  425. continue;
  426. }
  427. /* Get the character we’re going to paste */
  428. if(utf8)
  429. {
  430. unsigned int bytes;
  431. if(i + 6 < size)
  432. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  433. else
  434. {
  435. /* Add a trailing zero to what we're going to read */
  436. char tmp[7];
  437. memcpy(tmp, buffer + i, size - i);
  438. tmp[size - i] = '\0';
  439. ch = cucul_utf8_to_utf32(tmp, &bytes);
  440. }
  441. if(!bytes)
  442. {
  443. /* If the Unicode is invalid, assume it was latin1. */
  444. ch = buffer[i];
  445. bytes = 1;
  446. }
  447. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  448. skip += bytes - 1;
  449. }
  450. else
  451. {
  452. ch = cucul_cp437_to_utf32(buffer[i]);
  453. }
  454. /* Make sure the canvas is big enough. */
  455. if((unsigned int)x + wch > width)
  456. {
  457. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  458. cucul_set_canvas_size(cv, width = x + wch, height);
  459. }
  460. if((unsigned int)y >= height)
  461. {
  462. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  463. cucul_set_canvas_size(cv, width, height = y + 1);
  464. }
  465. /* Now paste our character */
  466. cucul_set_color_ansi(cv, grcm.efg, grcm.ebg);
  467. cucul_put_char(cv, x, y, ch);
  468. x += wch;
  469. }
  470. if((unsigned int)y > height)
  471. {
  472. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  473. cucul_set_canvas_size(cv, width, height = y);
  474. }
  475. return size;
  476. }
  477. /* XXX : ANSI loader helper */
  478. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  479. unsigned int argc, unsigned int const *argv)
  480. {
  481. static uint8_t const ansi2cucul[] =
  482. {
  483. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  484. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  485. };
  486. unsigned int j;
  487. for(j = 0; j < argc; j++)
  488. {
  489. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  490. if(argv[j] >= 30 && argv[j] <= 37)
  491. g->fg = ansi2cucul[argv[j] - 30];
  492. else if(argv[j] >= 40 && argv[j] <= 47)
  493. g->bg = ansi2cucul[argv[j] - 40];
  494. else if(argv[j] >= 90 && argv[j] <= 97)
  495. g->fg = ansi2cucul[argv[j] - 90] + 8;
  496. else if(argv[j] >= 100 && argv[j] <= 107)
  497. g->bg = ansi2cucul[argv[j] - 100] + 8;
  498. else switch(argv[j])
  499. {
  500. case 0: /* default rendition */
  501. g->fg = CUCUL_DEFAULT;
  502. g->bg = CUCUL_TRANSPARENT;
  503. g->bold = g->negative = g->concealed = 0;
  504. break;
  505. case 1: /* bold or increased intensity */
  506. g->bold = 1;
  507. break;
  508. case 4: /* singly underlined */
  509. break;
  510. case 5: /* slowly blinking (less then 150 per minute) */
  511. break;
  512. case 7: /* negative image */
  513. g->negative = 1;
  514. break;
  515. case 8: /* concealed characters */
  516. g->concealed = 1;
  517. break;
  518. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  519. g->bold = 0;
  520. break;
  521. case 28: /* revealed characters */
  522. g->concealed = 0;
  523. break;
  524. case 39: /* default display colour (implementation-defined) */
  525. g->fg = CUCUL_DEFAULT;
  526. break;
  527. case 49: /* default background colour (implementation-defined) */
  528. g->bg = CUCUL_TRANSPARENT;
  529. break;
  530. default:
  531. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  532. break;
  533. }
  534. }
  535. if(g->concealed)
  536. {
  537. g->efg = g->ebg = CUCUL_TRANSPARENT;
  538. }
  539. else
  540. {
  541. g->efg = g->negative ? g->bg : g->fg;
  542. g->ebg = g->negative ? g->fg : g->bg;
  543. if(g->bold)
  544. {
  545. if(g->efg < 8)
  546. g->efg += 8;
  547. else if(g->efg == CUCUL_DEFAULT)
  548. g->efg = CUCUL_WHITE;
  549. }
  550. }
  551. }