file.c revision a96d7823
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 "libxfontint.h"
28#include <string.h>
29#include "builtin.h"
30
31typedef struct _BuiltinIO {
32    int		    offset;
33    BuiltinFilePtr  file;
34} BuiltinIORec, *BuiltinIOPtr;
35
36static int
37BuiltinFill (BufFilePtr	f)
38{
39    int	    left, len;
40    BuiltinIOPtr    io = ((BuiltinIOPtr) f->private);
41
42    left = io->file->len - io->offset;
43    if (left <= 0)
44    {
45	f->left = 0;
46	return BUFFILEEOF;
47    }
48    len = BUFFILESIZE;
49    if (len > left)
50	len = left;
51    memcpy (f->buffer, io->file->bits + io->offset, len);
52    io->offset += len;
53    f->left = len - 1;
54    f->bufp = f->buffer + 1;
55    return f->buffer[0];
56}
57
58static int
59BuiltinSkip (BufFilePtr	f, int count)
60{
61    BuiltinIOPtr    io = ((BuiltinIOPtr) f->private);
62    int	    curoff;
63    int	    fileoff;
64    int	    todo;
65
66    curoff = f->bufp - f->buffer;
67    fileoff = curoff + f->left;
68    if (curoff + count <= fileoff) {
69	f->bufp += count;
70	f->left -= count;
71    } else {
72	todo = count - (fileoff - curoff);
73	io->offset += todo;
74	if (io->offset > io->file->len)
75	    io->offset = io->file->len;
76	if (io->offset < 0)
77	    io->offset = 0;
78	f->left = 0;
79    }
80    return count;
81}
82
83static int
84BuiltinClose (BufFilePtr f, int unused)
85{
86    BuiltinIOPtr    io = ((BuiltinIOPtr) f->private);
87
88    free (io);
89    return 1;
90}
91
92
93FontFilePtr
94BuiltinFileOpen (const char *name)
95{
96    int		    i;
97    BuiltinIOPtr    io;
98    BufFilePtr	    raw, cooked;
99
100    if (*name == '/') name++;
101    for (i = 0; i < builtin_files_count; i++)
102	if (!strcmp (name, builtin_files[i].name))
103	    break;
104    if (i == builtin_files_count)
105	return NULL;
106    io = malloc (sizeof (BuiltinIORec));
107    if (!io)
108	return NULL;
109    io->offset = 0;
110    io->file = (void *) &builtin_files[i];
111    raw = BufFileCreate ((char *) io, BuiltinFill, 0, BuiltinSkip, BuiltinClose);
112    if (!raw)
113    {
114	free (io);
115	return NULL;
116    }
117    if ((cooked = BufFilePushZIP (raw)))
118	raw = cooked;
119    else
120    {
121	raw->left += raw->bufp - raw->buffer;
122	raw->bufp = raw->buffer;
123    }
124    return (FontFilePtr) raw;
125}
126
127int
128BuiltinFileClose (FontFilePtr f, int unused)
129{
130    return BufFileClose ((BufFilePtr) f, TRUE);
131}
132