1/*
2 * Copyright 1999 SuSE, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of SuSE not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission.  SuSE makes no representations about the
11 * suitability of this software for any purpose.  It is provided "as is"
12 * without express or implied warranty.
13 *
14 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
16 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
18 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 * Author:  Keith Packard, SuSE, Inc.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27#include <string.h>
28#include "builtin.h"
29
30typedef struct _BuiltinIO {
31    int		    offset;
32    BuiltinFilePtr  file;
33} BuiltinIORec, *BuiltinIOPtr;
34
35static int
36BuiltinFill (BufFilePtr	f)
37{
38    int	    left, len;
39    BuiltinIOPtr    io = ((BuiltinIOPtr) f->private);
40
41    left = io->file->len - io->offset;
42    if (left <= 0)
43    {
44	f->left = 0;
45	return BUFFILEEOF;
46    }
47    len = BUFFILESIZE;
48    if (len > left)
49	len = left;
50    memcpy (f->buffer, io->file->bits + io->offset, len);
51    io->offset += len;
52    f->left = len - 1;
53    f->bufp = f->buffer + 1;
54    return f->buffer[0];
55}
56
57static int
58BuiltinSkip (BufFilePtr	f, int count)
59{
60    BuiltinIOPtr    io = ((BuiltinIOPtr) f->private);
61    int	    curoff;
62    int	    fileoff;
63    int	    todo;
64
65    curoff = f->bufp - f->buffer;
66    fileoff = curoff + f->left;
67    if (curoff + count <= fileoff) {
68	f->bufp += count;
69	f->left -= count;
70    } else {
71	todo = count - (fileoff - curoff);
72	io->offset += todo;
73	if (io->offset > io->file->len)
74	    io->offset = io->file->len;
75	if (io->offset < 0)
76	    io->offset = 0;
77	f->left = 0;
78    }
79    return count;
80}
81
82static int
83BuiltinClose (BufFilePtr f, int unused)
84{
85    BuiltinIOPtr    io = ((BuiltinIOPtr) f->private);
86
87    free (io);
88    return 1;
89}
90
91
92FontFilePtr
93BuiltinFileOpen (const char *name)
94{
95    int		    i;
96    BuiltinIOPtr    io;
97    BufFilePtr	    raw, cooked;
98
99    if (*name == '/') name++;
100    for (i = 0; i < builtin_files_count; i++)
101	if (!strcmp (name, builtin_files[i].name))
102	    break;
103    if (i == builtin_files_count)
104	return NULL;
105    io = malloc (sizeof (BuiltinIORec));
106    if (!io)
107	return NULL;
108    io->offset = 0;
109    io->file = (void *) &builtin_files[i];
110    raw = BufFileCreate ((char *) io, BuiltinFill, 0, BuiltinSkip, BuiltinClose);
111    if (!raw)
112    {
113	free (io);
114	return NULL;
115    }
116    if ((cooked = BufFilePushZIP (raw)))
117	raw = cooked;
118    else
119    {
120	raw->left += raw->bufp - raw->buffer;
121	raw->bufp = raw->buffer;
122    }
123    return (FontFilePtr) raw;
124}
125
126int
127BuiltinFileClose (FontFilePtr f, int unused)
128{
129    return BufFileClose ((BufFilePtr) f, TRUE);
130}
131