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