xref: /plan9/sys/src/cmd/gs/src/gsiorom.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2005 artofcode LLC.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gsiorom.c,v 1.2 2005/10/04 06:30:02 ray Exp $ */
18 /* %rom% IODevice implementation for a compressed in-memory filesystem */
19 
20 /*
21  * This file implements a special %rom% IODevice designed for embedded
22  * use. It accesses a compressed filesytem image which may be stored
23  * in literal ROM, or more commonly is just static data linked directly
24  * into the executable. This can be used for storing postscript library
25  * files, fonts, Resources or other data files that Ghostscript needs
26  * to run.
27  */
28 
29 #include "std.h"
30 #include "string_.h"
31 #include "gx.h"
32 #include "gserrors.h"
33 #include "gsstruct.h"
34 #include "gxiodev.h"
35 #include "stream.h"
36 
37 /* device method prototypes */
38 private iodev_proc_init(iodev_rom_init);
39 private iodev_proc_open_file(iodev_rom_open_file);
40 /* close is handled by stream closure */
41 
42 /* device definition */
43 const gx_io_device gs_iodev_rom =
44 {
45     "%rom%", "FileSystem",
46     {iodev_rom_init, iodev_no_open_device,
47      iodev_rom_open_file,
48      iodev_no_fopen, iodev_no_fclose,
49      iodev_no_delete_file, iodev_no_rename_file,
50      iodev_no_file_status,
51      iodev_no_enumerate_files, NULL, NULL,
52      iodev_no_get_params, iodev_no_put_params
53     }
54 };
55 
56 /* internal state for our device */
57 typedef struct romfs_state_s {
58     char *image;
59 } romfs_state;
60 
61 gs_private_st_simple(st_romfs_state, struct romfs_state_s, "romfs_state");
62 /* we don't need to track the image ptr in the descriptors because
63    this is by definition static data */
64 
65 private int
iodev_rom_init(gx_io_device * iodev,gs_memory_t * mem)66 iodev_rom_init(gx_io_device *iodev, gs_memory_t *mem)
67 {
68     romfs_state *state = gs_alloc_struct(mem, romfs_state,
69                                               &st_romfs_state,
70                                               "iodev_rom_init(state)");
71     if (!state)
72 	return gs_error_VMerror;
73 
74     state->image = NULL;
75     return 0;
76 }
77 
78 private int
iodev_rom_open_file(gx_io_device * iodev,const char * fname,uint namelen,const char * access,stream ** ps,gs_memory_t * mem)79 iodev_rom_open_file(gx_io_device *iodev, const char *fname, uint namelen,
80     const char *access, stream **ps, gs_memory_t *mem)
81 {
82     const char* dummy = "this came from the compressed romfs.";
83     byte *buf;
84 
85     /* return an empty stream on error */
86     *ps = NULL;
87 
88     /* fake by returning the contents of a string */
89     buf = gs_alloc_string(mem, strlen(dummy), "romfs buffer");
90     if (buf == NULL) {
91 	if_debug0('s', "%rom%: could not allocate buffer\n");
92 	return_error(gs_error_VMerror);
93     }
94     memcpy(buf, dummy, strlen(dummy));
95     *ps = s_alloc(mem, "romfs");
96     sread_string(*ps, buf, strlen(dummy));
97 
98     /* return success */
99     return 0;
100 }
101