revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / diskimage / plugins / iso.c
blobb20978f121af8a2df862486ccb813e86cef3af1c
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
28 ** This plugin handles 2352 bytes per block .iso files as Generic plugin is no
29 ** longer able to handle these
32 #ifdef __AROS__
33 #define __EXEC_NOLIBBASE__
34 #define __DOS_NOLIBBASE__
35 #endif
37 #define USED_PLUGIN_API_VERSION 8
38 #include <devices/diskimage.h>
39 #include <proto/exec.h>
40 #include <proto/dos.h>
41 #include <string.h>
42 #include "device_locale.h"
43 #include <SDI_compiler.h>
44 #include "rev/diskimage.device_rev.h"
46 PLUGIN_VERSTAG("ISO")
48 extern struct DiskImagePlugin iso_plugin;
50 PLUGIN_TABLE(&iso_plugin)
52 struct ISOImage {
53 BPTR file;
54 ULONG block_size;
55 ULONG total_blocks;
56 UQUAD total_bytes;
57 ULONG sector_size;
58 ULONG header_part, ecc_part;
61 BOOL ISO_Init (struct DiskImagePlugin *Self, const struct PluginData *data);
62 BOOL ISO_CheckImage (struct DiskImagePlugin *Self, BPTR file, CONST_STRPTR name, QUAD file_size,
63 const UBYTE *test, LONG testsize);
64 APTR ISO_OpenImage (struct DiskImagePlugin *Self, APTR unit, BPTR file, CONST_STRPTR name);
65 void Generic_CloseImage (struct DiskImagePlugin *Self, APTR image_ptr);
66 LONG Generic_Geometry (struct DiskImagePlugin *Self, APTR image_ptr, struct DriveGeometry *dg);
67 LONG ISO_Read (struct DiskImagePlugin *Self, APTR image_ptr, struct IOStdReq *io);
69 struct DiskImagePlugin iso_plugin = {
70 PLUGIN_NODE(-125, "ISO"),
71 PLUGIN_FLAG_M68K,
72 16,
73 ZERO,
74 NULL,
75 ISO_Init,
76 NULL,
77 ISO_CheckImage,
78 ISO_OpenImage,
79 Generic_CloseImage,
80 Generic_Geometry,
81 ISO_Read,
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 NULL
89 static struct Library *SysBase;
90 static struct Library *DOSBase;
91 static struct DIPluginIFace *IPlugin;
93 BOOL ISO_Init (struct DiskImagePlugin *Self, const struct PluginData *data) {
94 SysBase = data->SysBase;
95 DOSBase = data->DOSBase;
96 IPlugin = data->IPlugin;
97 return TRUE;
100 static const UBYTE sync_header[16] = {
101 0x00, 0xFF, 0xFF, 0xFF,
102 0xFF, 0xFF, 0xFF, 0xFF,
103 0xFF, 0xFF, 0xFF, 0x00,
104 0x00, 0x02, 0x00, 0x01
107 BOOL ISO_CheckImage (struct DiskImagePlugin *Self, BPTR file, CONST_STRPTR name, QUAD file_size,
108 const UBYTE *test, LONG testsize)
110 return testsize >= 16 && !memcmp(test, sync_header, 16);
113 APTR ISO_OpenImage (struct DiskImagePlugin *Self, APTR unit, BPTR file, CONST_STRPTR name) {
114 LONG done = FALSE;
115 LONG error = NO_ERROR;
116 QUAD file_size;
117 struct ISOImage *image = NULL;
119 file_size = GetFileSize(file);
120 if (file_size == -1) {
121 error = IoErr();
122 goto error;
125 image = AllocVec(sizeof(*image), MEMF_CLEAR);
126 if (!image) {
127 error = ERROR_NO_FREE_STORE;
128 goto error;
130 image->file = file;
131 image->total_bytes = file_size;
133 image->block_size = 2048;
134 image->sector_size = 2352;
135 image->header_part = 16;
136 image->ecc_part = image->sector_size - image->header_part - image->block_size;
137 image->total_blocks = image->total_bytes / image->sector_size;
139 done = TRUE;
141 error:
142 if (!done) {
143 if (image) {
144 Plugin_CloseImage(Self, image);
145 image = NULL;
146 } else {
147 Close(file);
149 IPlugin_SetDiskImageError(unit, error, 0);
151 return image;
154 LONG ISO_Read (struct DiskImagePlugin *Self, APTR image_ptr, struct IOStdReq *io) {
155 struct ISOImage *image = image_ptr;
156 BPTR file = image->file;
157 UQUAD offset;
158 UBYTE *buffer;
159 LONG size, read_size;
160 ULONG start_blk;
161 ULONG num_blks;
163 offset = ((UQUAD)io->io_Offset)|((UQUAD)io->io_Actual << 32);
164 buffer = io->io_Data;
165 size = io->io_Length;
166 io->io_Actual = 0;
168 if (offset & 0x7ff) return IOERR_BADADDRESS;
169 if (size & 0x7ff) return IOERR_BADLENGTH;
171 start_blk = offset >> 11;
172 num_blks = size >> 11;
174 if (!ChangeFilePosition(file, start_blk*image->sector_size + image->header_part, OFFSET_BEGINNING)) {
175 return TDERR_SeekError;
178 while (num_blks--) {
179 size = 2048;
180 while (size) {
181 read_size = FRead(file, buffer, 1, size);
182 if (read_size == 0) {
183 return IOERR_BADLENGTH;
184 } else if (read_size == -1) {
185 return IPlugin_DOS2IOErr(IoErr());
187 buffer += read_size;
188 size -= read_size;
189 io->io_Actual += read_size;
191 if (num_blks && !ChangeFilePosition(file, image->ecc_part + image->header_part, OFFSET_CURRENT)) {
192 return TDERR_SeekError;
195 return IOERR_SUCCESS;