1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
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.
27 #define USED_PLUGIN_API_VERSION 8
28 #include <devices/diskimage.h>
29 #include <gadgets/fuelgauge.h>
31 #include <proto/exec.h>
32 #include <proto/dos.h>
33 #include <proto/xpkmaster.h>
35 #include "device_locale.h"
36 #include <SDI_compiler.h>
37 #include "rev/diskimage.device_rev.h"
41 extern struct DiskImagePlugin xpk_plugin
;
43 PLUGIN_TABLE(&xpk_plugin
)
45 BOOL
XPK_Init (struct DiskImagePlugin
*Self
, const struct PluginData
*data
);
46 void XPK_Exit (struct DiskImagePlugin
*Self
);
47 BOOL
XPK_CheckImage (struct DiskImagePlugin
*Self
, BPTR file
, CONST_STRPTR name
, QUAD file_size
,
48 const UBYTE
*test
, LONG testsize
);
49 APTR
XPK_OpenImage (struct DiskImagePlugin
*Self
, APTR unit
, BPTR file
, CONST_STRPTR name
);
50 static ULONG
ProgressHookFunc (REG(a0
, struct Hook
*hook
), REG(a2
, void *unused
),
51 REG(a1
, struct XpkProgress
*xp
));
52 static LONG
XPKError (LONG error
, LONG
*error_string
);
54 struct DiskImagePlugin xpk_plugin
= {
55 PLUGIN_NODE(-1, "XPK"),
74 struct Library
*SysBase
;
75 struct Library
*DOSBase
;
76 static struct DIPluginIFace
*IPlugin
;
77 static struct Library
*XpkBase
;
79 BOOL
XPK_Init (struct DiskImagePlugin
*Self
, const struct PluginData
*data
) {
80 SysBase
= data
->SysBase
;
81 DOSBase
= data
->DOSBase
;
82 IPlugin
= data
->IPlugin
;
86 void XPK_Exit (struct DiskImagePlugin
*Self
) {
87 if (XpkBase
) CloseLibrary(XpkBase
);
90 BOOL
XPK_CheckImage (struct DiskImagePlugin
*Self
, BPTR file
, CONST_STRPTR name
, QUAD file_size
,
91 const UBYTE
*test
, LONG testsize
)
97 XpkBase
= OpenLibrary("xpkmaster.library", 0);
98 if (!XpkBase
) return FALSE
;
101 xf
= XpkAllocObject(XPKOBJ_FIB
, NULL
);
103 if (XpkExamineTags(xf
, XPK_InFH
, file
, TAG_DONE
) == XPKERR_OK
) {
104 if (xf
->xf_Type
== XPKTYPE_PACKED
) is_xpk
= TRUE
;
106 XpkFreeObject(XPKOBJ_FIB
, xf
);
107 ChangeFilePosition(file
, 0, OFFSET_BEGINNING
);
112 APTR
XPK_OpenImage (struct DiskImagePlugin
*Self
, APTR unit
, BPTR file
,
116 LONG error
= NO_ERROR
;
117 LONG error_string
= NO_ERROR_STRING
;
118 IPTR error_args
[4] = {0};
123 CONST_STRPTR tmpname
;
126 XpkBase
= OpenLibrary("xpkmaster.library", 0);
128 error
= ERROR_OBJECT_NOT_FOUND
;
129 error_string
= MSG_REQ
;
130 error_args
[0] = (IPTR
)"xpkmaster.library";
135 ext
= strrchr(FilePart(name
), '.');
138 error
= IPlugin_CreateTempFile(unit
, ext
, &tmpdir
, &tmpname
);
139 if (error
) goto error
;
141 outfile
= IPlugin_OpenTempFile(unit
, MODE_NEWFILE
);
146 struct Hook progresshook
= {0};
149 progresshook
.h_Entry
= ProgressHookFunc
;
150 progresshook
.h_Data
= IPlugin_CreateProgressBar(unit
, TRUE
);
152 xpk_err
= XpkUnpackTags(
155 XPK_ChunkHook
, &progresshook
,
157 if (xpk_err
== XPKERR_NEEDPASSWD
) {
159 passwd
= IPlugin_RequestPassword(unit
);
161 xpk_err
= XpkUnpackTags(
162 XPK_Password
, passwd
,
165 XPK_ChunkHook
, &progresshook
,
169 error
= ERROR_NO_FREE_STORE
;
172 IPlugin_DeleteProgressBar(progresshook
.h_Data
);
176 if (error
!= NO_ERROR
) goto error
;
178 error
= XPKError(xpk_err
, &error_string
);
179 if (error
!= NO_ERROR
) goto error
;
181 outfile
= IPlugin_OpenTempFile(unit
, MODE_OLDFILE
);
188 tmpdir
= CurrentDir(tmpdir
);
189 image
= IPlugin_OpenImage(unit
, outfile
, tmpname
);
196 IPlugin_SetDiskImageErrorA(unit
, error
, error_string
, error_args
);
201 static ULONG
ProgressHookFunc (REG(a0
, struct Hook
*hook
), REG(a2
, void *unused
),
202 REG(a1
, struct XpkProgress
*xp
))
204 APTR pb
= hook
->h_Data
;
206 IPlugin_SetProgressBarAttrs(pb
,
207 FUELGAUGE_Percent
, TRUE
,
209 FUELGAUGE_Level
, xp
->xp_Done
,
212 return IPlugin_ProgressBarInput(pb
) ? 1 : 0;
215 static LONG
XPKError (LONG error
, LONG
*error_string
) {
220 return ERROR_NO_FREE_STORE
;
222 *error_string
= MSG_CANCELED
;
224 case XPKERR_NEEDPASSWD
:
225 *error_string
= MSG_NOPASSWD
;
226 return ERROR_REQUIRED_ARG_MISSING
;
228 *error_string
= MSG_WRONGPASSWD
;
229 return ERROR_REQUIRED_ARG_MISSING
;
231 *error_string
= MSG_XPKERR
;
232 return ERROR_OBJECT_WRONG_TYPE
;