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.
 
 
 
 
 
 

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