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

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