選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

650 行
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. unsigned long int ch;
  301. int x = 0, y = 0, save_x = 0, save_y = 0;
  302. cucul_set_canvas_size(cv, width, height);
  303. if(utf8)
  304. {
  305. grcm.dfg = cucul_attr_to_ansi_fg(cv->curattr);
  306. grcm.dbg = cucul_attr_to_ansi_bg(cv->curattr);
  307. }
  308. else
  309. {
  310. grcm.dfg = CUCUL_LIGHTGRAY;
  311. grcm.dbg = CUCUL_BLACK;
  312. cucul_set_color_ansi(cv, grcm.dfg, grcm.dbg);
  313. }
  314. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  315. for(i = 0; i < size; i += skip)
  316. {
  317. skip = 1;
  318. /* Wrap long lines */
  319. if((unsigned int)x >= 80)
  320. {
  321. x = 0;
  322. y++;
  323. }
  324. if(buffer[i] == '\x1a' && size - i >= 8
  325. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  326. break; /* End before SAUCE data */
  327. if(buffer[i] == '\r')
  328. continue; /* DOS sucks */
  329. if(buffer[i] == '\n')
  330. {
  331. x = 0;
  332. y++;
  333. continue;
  334. }
  335. /* Interpret escape commands, as per Standard ECMA-48 "Control
  336. * Functions for Coded Character Sets", 5.4. Control sequences. */
  337. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  338. {
  339. unsigned int argc = 0, argv[101];
  340. unsigned int param, inter, final;
  341. /* Compute offsets to parameter bytes, intermediate bytes and
  342. * to the final byte. Only the final byte is mandatory, there
  343. * can be zero of the others.
  344. * 0 param=2 inter final final+1
  345. * +-----+------------------+---------------------+-----------------+
  346. * | CSI | parameter bytes | intermediate bytes | final byte |
  347. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  348. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  349. * +-----+------------------+---------------------+-----------------+
  350. */
  351. param = 2;
  352. for(inter = param; i + inter < size; inter++)
  353. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  354. break;
  355. for(final = inter; i + final < size; final++)
  356. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  357. break;
  358. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  359. break; /* Invalid Final Byte */
  360. skip += final;
  361. /* Sanity checks */
  362. if(param < inter && buffer[i + param] >= 0x3c)
  363. {
  364. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  365. final - param + 1, buffer + i + param);
  366. continue; /* Private sequence, skip it entirely */
  367. }
  368. if(final - param > 100)
  369. continue; /* Suspiciously long sequence, skip it */
  370. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  371. * format */
  372. if(param < inter)
  373. {
  374. argv[0] = 0;
  375. for(j = param; j < inter; j++)
  376. {
  377. if(buffer[i + j] == ';')
  378. argv[++argc] = 0;
  379. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  380. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  381. }
  382. argc++;
  383. }
  384. /* Interpret final byte. The code representations are given in
  385. * ECMA-48 5.4: Control sequences, and the code definitions are
  386. * given in ECMA-48 8.3: Definition of control functions. */
  387. switch(buffer[i + final])
  388. {
  389. case 'f': /* CUP - Cursor Position */
  390. case 'H': /* HVP - Character And Line Position */
  391. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  392. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  393. break;
  394. case 'A': /* CUU - Cursor Up */
  395. y -= argc ? argv[0] : 1;
  396. if(y < 0)
  397. y = 0;
  398. break;
  399. case 'B': /* CUD - Cursor Down */
  400. y += argc ? argv[0] : 1;
  401. break;
  402. case 'C': /* CUF - Cursor Right */
  403. x += argc ? argv[0] : 1;
  404. break;
  405. case 'D': /* CUB - Cursor Left */
  406. x -= argc ? argv[0] : 1;
  407. if(x < 0)
  408. x = 0;
  409. break;
  410. case 's': /* Private (save cursor position) */
  411. save_x = x;
  412. save_y = y;
  413. break;
  414. case 'u': /* Private (reload cursor position) */
  415. x = save_x;
  416. y = save_y;
  417. break;
  418. case 'J': /* ED - Erase In Page */
  419. if(argv[0] == 2)
  420. x = y = 0;
  421. break;
  422. case 'K': /* EL - Erase In Line */
  423. if(width < 80)
  424. cucul_set_color_ansi(cv, grcm.dfg, grcm.dbg);
  425. cucul_set_canvas_size(cv, width = 80, height);
  426. for(j = x; j < 80; j++)
  427. cucul_put_char(cv, j, y, ' ');
  428. x = 80;
  429. break;
  430. case 'm': /* SGR - Select Graphic Rendition */
  431. ansi_parse_grcm(cv, &grcm, argc, argv);
  432. break;
  433. default:
  434. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  435. break;
  436. }
  437. continue;
  438. }
  439. /* Get the character we’re going to paste */
  440. if(utf8)
  441. {
  442. unsigned int bytes;
  443. if(i + 6 < size)
  444. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  445. else
  446. {
  447. /* Add a trailing zero to what we're going to read */
  448. char tmp[7];
  449. memcpy(tmp, buffer + i, size - i);
  450. tmp[size - i] = '\0';
  451. ch = cucul_utf8_to_utf32(tmp, &bytes);
  452. }
  453. if(!bytes)
  454. {
  455. /* If the Unicode is invalid, assume it was latin1. */
  456. ch = buffer[i];
  457. bytes = 1;
  458. }
  459. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  460. skip += bytes - 1;
  461. }
  462. else
  463. {
  464. ch = cucul_cp437_to_utf32(buffer[i]);
  465. }
  466. /* Make sure the canvas is big enough. */
  467. if((unsigned int)x + wch > width)
  468. {
  469. cucul_set_color_ansi(cv, grcm.dfg, grcm.dbg);
  470. cucul_set_canvas_size(cv, width = x + wch, height);
  471. }
  472. if((unsigned int)y >= height)
  473. {
  474. cucul_set_color_ansi(cv, grcm.dfg, grcm.dbg);
  475. cucul_set_canvas_size(cv, width, height = y + 1);
  476. }
  477. /* Now paste our character */
  478. cucul_set_color_ansi(cv, grcm.efg, grcm.ebg);
  479. cucul_put_char(cv, x, y, ch);
  480. x += wch;
  481. }
  482. if((unsigned int)y > height)
  483. {
  484. cucul_set_color_ansi(cv, grcm.dfg, grcm.dbg);
  485. cucul_set_canvas_size(cv, width, height = y);
  486. }
  487. return size;
  488. }
  489. /* XXX : ANSI loader helper */
  490. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  491. unsigned int argc, unsigned int const *argv)
  492. {
  493. static uint8_t const ansi2cucul[] =
  494. {
  495. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  496. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  497. };
  498. unsigned int j;
  499. for(j = 0; j < argc; j++)
  500. {
  501. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  502. if(argv[j] >= 30 && argv[j] <= 37)
  503. g->fg = ansi2cucul[argv[j] - 30];
  504. else if(argv[j] >= 40 && argv[j] <= 47)
  505. g->bg = ansi2cucul[argv[j] - 40];
  506. else if(argv[j] >= 90 && argv[j] <= 97)
  507. g->fg = ansi2cucul[argv[j] - 90] + 8;
  508. else if(argv[j] >= 100 && argv[j] <= 107)
  509. g->bg = ansi2cucul[argv[j] - 100] + 8;
  510. else switch(argv[j])
  511. {
  512. case 0: /* default rendition */
  513. g->fg = g->dfg;
  514. g->bg = g->dbg;
  515. g->bold = g->negative = g->concealed = 0;
  516. break;
  517. case 1: /* bold or increased intensity */
  518. g->bold = 1;
  519. break;
  520. case 4: /* singly underlined */
  521. break;
  522. case 5: /* slowly blinking (less then 150 per minute) */
  523. break;
  524. case 7: /* negative image */
  525. g->negative = 1;
  526. break;
  527. case 8: /* concealed characters */
  528. g->concealed = 1;
  529. break;
  530. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  531. g->bold = 0;
  532. break;
  533. case 28: /* revealed characters */
  534. g->concealed = 0;
  535. break;
  536. case 39: /* default display colour (implementation-defined) */
  537. g->fg = g->dfg;
  538. break;
  539. case 49: /* default background colour (implementation-defined) */
  540. g->bg = g->dbg;
  541. break;
  542. default:
  543. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  544. break;
  545. }
  546. }
  547. if(g->concealed)
  548. {
  549. g->efg = g->ebg = CUCUL_TRANSPARENT;
  550. }
  551. else
  552. {
  553. g->efg = g->negative ? g->bg : g->fg;
  554. g->ebg = g->negative ? g->fg : g->bg;
  555. if(g->bold)
  556. {
  557. if(g->efg < 8)
  558. g->efg += 8;
  559. else if(g->efg == CUCUL_DEFAULT)
  560. g->efg = CUCUL_WHITE;
  561. }
  562. }
  563. }