Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

556 строки
16 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 "text": import ASCII text files.
  61. * - \c "ansi": import ANSI files.
  62. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  63. * - \c "caca": import native libcaca files.
  64. *
  65. * If an error occurs, -1 is returned and \b errno is set accordingly:
  66. * - \c ENOMEM Not enough memory to allocate canvas.
  67. * - \c EINVAL Invalid format requested.
  68. *
  69. * \param A libcucul canvas in which to import the file.
  70. * \param buffer A \e libcucul buffer containing the data to be loaded
  71. * into a canvas.
  72. * \param format A string describing the input format.
  73. * \return The number of bytes read, or -1 if an error occurred.
  74. */
  75. long int cucul_import(cucul_canvas_t *cv, unsigned char const *buf,
  76. unsigned long int len, char const *format)
  77. {
  78. if(!strcasecmp("caca", format))
  79. return import_caca(cv, buf, len);
  80. if(!strcasecmp("utf8", format))
  81. return import_ansi(cv, buf, len, 1);
  82. if(!strcasecmp("text", format))
  83. return import_text(cv, buf, len);
  84. if(!strcasecmp("ansi", format))
  85. return import_ansi(cv, buf, len, 0);
  86. /* Autodetection */
  87. if(!strcasecmp("", format))
  88. {
  89. unsigned int i;
  90. /* If 4 first bytes are 0xcaca + 'CV' */
  91. if(len >= 4 && buf[0] == 0xca &&
  92. buf[1] == 0xca && buf[2] == 'C' && buf[3] == 'V')
  93. return import_caca(cv, buf, len);
  94. /* If we find ESC[ argv, we guess it's an ANSI file */
  95. for(i = 0; i + 1 < len; i++)
  96. if((buf[i] == 0x1b) && (buf[i + 1] == '['))
  97. return import_ansi(cv, buf, len, 0);
  98. /* Otherwise, import it as text */
  99. return import_text(cv, buf, len);
  100. }
  101. #if defined(HAVE_ERRNO_H)
  102. errno = EINVAL;
  103. #endif
  104. return -1;
  105. }
  106. /** \brief Get available import formats
  107. *
  108. * Return a list of available import formats. The list is a NULL-terminated
  109. * array of strings, interleaving a string containing the internal value for
  110. * the import format, to be used with cucul_import_canvas(), and a string
  111. * containing the natural language description for that import format.
  112. *
  113. * This function never fails.
  114. *
  115. * \return An array of strings.
  116. */
  117. char const * const * cucul_get_import_list(void)
  118. {
  119. static char const * const list[] =
  120. {
  121. "", "autodetect",
  122. "text", "plain text",
  123. "caca", "native libcaca format",
  124. "ansi", "ANSI coloured text",
  125. NULL, NULL
  126. };
  127. return list;
  128. }
  129. /*
  130. * XXX: the following functions are local.
  131. */
  132. static long int import_caca(cucul_canvas_t *cv,
  133. void const *data, unsigned int size)
  134. {
  135. uint8_t const *buf = (uint8_t const *)data;
  136. unsigned int control_size, data_size, full_size, frames, f, n;
  137. uint16_t version, flags;
  138. cucul_set_canvas_size(cv, 0, 0);
  139. if(size < 20)
  140. goto invalid_caca;
  141. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  142. goto invalid_caca;
  143. control_size = sscanu32(buf + 4);
  144. data_size = sscanu32(buf + 8);
  145. version = sscanu16(buf + 12);
  146. frames = sscanu32(buf + 14);
  147. flags = sscanu16(buf + 18);
  148. if(size != 4 + control_size + data_size)
  149. goto invalid_caca;
  150. if(control_size < 16 + frames * 24)
  151. goto invalid_caca;
  152. for(full_size = 0, f = 0; f < frames; f++)
  153. {
  154. unsigned int width, height, duration;
  155. uint32_t attr;
  156. int x, y;
  157. width = sscanu32(buf + 4 + 16 + f * 24);
  158. height = sscanu32(buf + 4 + 16 + f * 24 + 4);
  159. duration = sscanu32(buf + 4 + 16 + f * 24 + 8);
  160. attr = sscanu32(buf + 4 + 16 + f * 24 + 12);
  161. x = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 16);
  162. y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20);
  163. full_size += width * height * 8;
  164. }
  165. if(full_size != data_size)
  166. goto invalid_caca;
  167. /* FIXME: read all frames, not only the first one */
  168. cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16),
  169. sscanu32(buf + 4 + 16 + 4));
  170. /* FIXME: check for return value */
  171. for(n = sscanu32(buf + 4 + 16) * sscanu32(buf + 4 + 16 + 4); n--; )
  172. {
  173. cv->chars[n] = sscanu32(buf + 4 + control_size + 8 * n);
  174. cv->attrs[n] = sscanu32(buf + 4 + control_size + 8 * n + 4);
  175. }
  176. cv->curattr = sscanu32(buf + 4 + 16 + 12);
  177. return size;
  178. invalid_caca:
  179. #if defined(HAVE_ERRNO_H)
  180. errno = EINVAL;
  181. #endif
  182. return -1;
  183. }
  184. static long int import_text(cucul_canvas_t *cv,
  185. void const *data, unsigned int size)
  186. {
  187. char const *text = (char const *)data;
  188. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  189. cucul_set_canvas_size(cv, width, height);
  190. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  191. for(i = 0; i < size; i++)
  192. {
  193. unsigned char ch = *text++;
  194. if(ch == '\r')
  195. continue;
  196. if(ch == '\n')
  197. {
  198. x = 0;
  199. y++;
  200. continue;
  201. }
  202. if(x >= width || y >= height)
  203. {
  204. if(x >= width)
  205. width = x + 1;
  206. if(y >= height)
  207. height = y + 1;
  208. cucul_set_canvas_size(cv, width, height);
  209. }
  210. cucul_putchar(cv, x, y, ch);
  211. x++;
  212. }
  213. if(y > height)
  214. cucul_set_canvas_size(cv, width, height = y);
  215. return size;
  216. }
  217. static long int import_ansi(cucul_canvas_t *cv,
  218. void const *data, unsigned int size, int utf8)
  219. {
  220. struct ansi_grcm grcm;
  221. unsigned char const *buffer = (unsigned char const*)data;
  222. unsigned int i, j, skip, dummy = 0;
  223. unsigned int width = 0, height = 0, wch = 1;
  224. unsigned long int ch;
  225. int x = 0, y = 0, save_x = 0, save_y = 0;
  226. cucul_set_canvas_size(cv, width, height);
  227. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  228. for(i = 0; i < size; i += skip)
  229. {
  230. skip = 1;
  231. /* Wrap long lines */
  232. if((unsigned int)x >= 80)
  233. {
  234. x = 0;
  235. y++;
  236. }
  237. if(buffer[i] == '\x1a' && size - i >= 8
  238. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  239. break; /* End before SAUCE data */
  240. if(buffer[i] == '\r')
  241. continue; /* DOS sucks */
  242. if(buffer[i] == '\n')
  243. {
  244. x = 0;
  245. y++;
  246. continue;
  247. }
  248. /* Interpret escape commands, as per Standard ECMA-48 "Control
  249. * Functions for Coded Character Sets", 5.4. Control sequences. */
  250. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  251. {
  252. unsigned int argc = 0, argv[101];
  253. unsigned int param, inter, final;
  254. /* Compute offsets to parameter bytes, intermediate bytes and
  255. * to the final byte. Only the final byte is mandatory, there
  256. * can be zero of the others.
  257. * 0 param=2 inter final final+1
  258. * +-----+------------------+---------------------+-----------------+
  259. * | CSI | parameter bytes | intermediate bytes | final byte |
  260. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  261. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  262. * +-----+------------------+---------------------+-----------------+
  263. */
  264. param = 2;
  265. for(inter = param; i + inter < size; inter++)
  266. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  267. break;
  268. for(final = inter; i + final < size; final++)
  269. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  270. break;
  271. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  272. break; /* Invalid Final Byte */
  273. skip += final;
  274. /* Sanity checks */
  275. if(param < inter && buffer[i + param] >= 0x3c)
  276. {
  277. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  278. final - param + 1, buffer + i + param);
  279. continue; /* Private sequence, skip it entirely */
  280. }
  281. if(final - param > 100)
  282. continue; /* Suspiciously long sequence, skip it */
  283. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  284. * format */
  285. if(param < inter)
  286. {
  287. argv[0] = 0;
  288. for(j = param; j < inter; j++)
  289. {
  290. if(buffer[i + j] == ';')
  291. argv[++argc] = 0;
  292. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  293. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  294. }
  295. argc++;
  296. }
  297. /* Interpret final byte. The code representations are given in
  298. * ECMA-48 5.4: Control sequences, and the code definitions are
  299. * given in ECMA-48 8.3: Definition of control functions. */
  300. switch(buffer[i + final])
  301. {
  302. case 'f': /* CUP - Cursor Position */
  303. case 'H': /* HVP - Character And Line Position */
  304. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  305. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  306. break;
  307. case 'A': /* CUU - Cursor Up */
  308. y -= argc ? argv[0] : 1;
  309. if(y < 0)
  310. y = 0;
  311. break;
  312. case 'B': /* CUD - Cursor Down */
  313. y += argc ? argv[0] : 1;
  314. break;
  315. case 'C': /* CUF - Cursor Right */
  316. x += argc ? argv[0] : 1;
  317. break;
  318. case 'D': /* CUB - Cursor Left */
  319. x -= argc ? argv[0] : 1;
  320. if(x < 0)
  321. x = 0;
  322. break;
  323. case 's': /* Private (save cursor position) */
  324. save_x = x;
  325. save_y = y;
  326. break;
  327. case 'u': /* Private (reload cursor position) */
  328. x = save_x;
  329. y = save_y;
  330. break;
  331. case 'J': /* ED - Erase In Page */
  332. if(argv[0] == 2)
  333. x = y = 0;
  334. break;
  335. case 'K': /* EL - Erase In Line */
  336. if(width < 80)
  337. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  338. cucul_set_canvas_size(cv, width = 80, height);
  339. for(j = x; j < 80; j++)
  340. cucul_putchar(cv, j, y, ' ');
  341. x = 80;
  342. break;
  343. case 'm': /* SGR - Select Graphic Rendition */
  344. ansi_parse_grcm(cv, &grcm, argc, argv);
  345. break;
  346. default:
  347. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  348. break;
  349. }
  350. continue;
  351. }
  352. /* Get the character we’re going to paste */
  353. if(utf8)
  354. {
  355. unsigned int bytes;
  356. if(i + 6 < size)
  357. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  358. else
  359. {
  360. /* Add a trailing zero to what we're going to read */
  361. char tmp[7];
  362. memcpy(tmp, buffer + i, size - i);
  363. tmp[size - i] = '\0';
  364. ch = cucul_utf8_to_utf32(tmp, &bytes);
  365. }
  366. if(!bytes)
  367. {
  368. /* If the Unicode is invalid, assume it was latin1. */
  369. ch = buffer[i];
  370. bytes = 1;
  371. }
  372. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  373. skip += bytes - 1;
  374. }
  375. else
  376. {
  377. ch = cucul_cp437_to_utf32(buffer[i]);
  378. }
  379. /* Make sure the canvas is big enough. */
  380. if((unsigned int)x + wch > width)
  381. {
  382. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  383. cucul_set_canvas_size(cv, width = x + wch, height);
  384. }
  385. if((unsigned int)y >= height)
  386. {
  387. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  388. cucul_set_canvas_size(cv, width, height = y + 1);
  389. }
  390. /* Now paste our character */
  391. cucul_set_color_ansi(cv, grcm.efg, grcm.ebg);
  392. cucul_putchar(cv, x, y, ch);
  393. x += wch;
  394. }
  395. if((unsigned int)y > height)
  396. {
  397. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  398. cucul_set_canvas_size(cv, width, height = y);
  399. }
  400. return size;
  401. }
  402. /* XXX : ANSI loader helper */
  403. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  404. unsigned int argc, unsigned int const *argv)
  405. {
  406. static uint8_t const ansi2cucul[] =
  407. {
  408. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  409. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  410. };
  411. unsigned int j;
  412. for(j = 0; j < argc; j++)
  413. {
  414. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  415. if(argv[j] >= 30 && argv[j] <= 37)
  416. g->fg = ansi2cucul[argv[j] - 30];
  417. else if(argv[j] >= 40 && argv[j] <= 47)
  418. g->bg = ansi2cucul[argv[j] - 40];
  419. else if(argv[j] >= 90 && argv[j] <= 97)
  420. g->fg = ansi2cucul[argv[j] - 90] + 8;
  421. else if(argv[j] >= 100 && argv[j] <= 107)
  422. g->bg = ansi2cucul[argv[j] - 100] + 8;
  423. else switch(argv[j])
  424. {
  425. case 0: /* default rendition */
  426. g->fg = CUCUL_DEFAULT;
  427. g->bg = CUCUL_TRANSPARENT;
  428. g->bold = g->negative = g->concealed = 0;
  429. break;
  430. case 1: /* bold or increased intensity */
  431. g->bold = 1;
  432. break;
  433. case 4: /* singly underlined */
  434. break;
  435. case 5: /* slowly blinking (less then 150 per minute) */
  436. break;
  437. case 7: /* negative image */
  438. g->negative = 1;
  439. break;
  440. case 8: /* concealed characters */
  441. g->concealed = 1;
  442. break;
  443. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  444. g->bold = 0;
  445. break;
  446. case 28: /* revealed characters */
  447. g->concealed = 0;
  448. break;
  449. case 39: /* default display colour (implementation-defined) */
  450. g->fg = CUCUL_DEFAULT;
  451. break;
  452. case 49: /* default background colour (implementation-defined) */
  453. g->bg = CUCUL_TRANSPARENT;
  454. break;
  455. default:
  456. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  457. break;
  458. }
  459. }
  460. if(g->concealed)
  461. {
  462. g->efg = g->ebg = CUCUL_TRANSPARENT;
  463. }
  464. else
  465. {
  466. g->efg = g->negative ? g->bg : g->fg;
  467. g->ebg = g->negative ? g->fg : g->bg;
  468. if(g->bold)
  469. {
  470. if(g->efg < 8)
  471. g->efg += 8;
  472. else if(g->efg == CUCUL_DEFAULT)
  473. g->efg = CUCUL_WHITE;
  474. }
  475. }
  476. }