source: libcaca/trunk/cucul/import.c @ 1302

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