1 /***************************************************************************
3 codesets.library - Amiga shared library for handling different codesets
4 Copyright (C) 2001-2005 by Alfonso [alfie] Ranieri <alforan@tin.it>.
5 Copyright (C) 2005-2007 by codesets.library Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 codesets.library project: http://sourceforge.net/projects/codesetslib/
21 ***************************************************************************/
26 #include <exec/resident.h>
27 #include <proto/exec.h>
31 // transforms a define into a string
32 #define STR(x) STR2(x)
35 /****************************************************************************/
37 #define MIN_STACKSIZE 65536
39 // stack cookie for shell v45+
40 static const char USED_VAR stack_size
[] = "$STACK:" STR(MIN_STACKSIZE
) "\n";
42 /****************************************************************************/
44 #if defined(__amigaos4__)
45 struct Library
*SysBase
= NULL
;
46 struct ExecIFace
* IExec
= NULL
;
47 #if defined(__NEWLIB__)
48 struct Library
*NewlibBase
= NULL
;
49 struct NewlibIFace
* INewlib
= NULL
;
52 struct ExecBase
*SysBase
= NULL
;
55 struct LibraryHeader
*CodesetsBase
= NULL
;
57 static const char UserLibName
[] = "codesets.library";
58 static const char UserLibID
[] = "$VER: codesets.library " LIB_REV_STRING CPU
" (" LIB_DATE
") " LIB_COPYRIGHT
;
60 /****************************************************************************/
62 #define libvector LibNull \
63 LFUNC_FA_(CodesetsConvertUTF32toUTF16) \
64 LFUNC_FA_(CodesetsConvertUTF16toUTF32) \
65 LFUNC_FA_(CodesetsConvertUTF16toUTF8) \
66 LFUNC_FA_(CodesetsIsLegalUTF8) \
67 LFUNC_FA_(CodesetsIsLegalUTF8Sequence) \
68 LFUNC_FA_(CodesetsConvertUTF8toUTF16) \
69 LFUNC_FA_(CodesetsConvertUTF32toUTF8) \
70 LFUNC_FA_(CodesetsConvertUTF8toUTF32) \
71 LFUNC_FA_(CodesetsSetDefaultA) \
72 LFUNC_VA_(CodesetsSetDefault) \
73 LFUNC_FA_(CodesetsFreeA) \
74 LFUNC_VA_(CodesetsFree) \
75 LFUNC_FA_(CodesetsSupportedA) \
76 LFUNC_VA_(CodesetsSupported) \
77 LFUNC_FA_(CodesetsFindA) \
78 LFUNC_VA_(CodesetsFind) \
79 LFUNC_FA_(CodesetsFindBestA) \
80 LFUNC_VA_(CodesetsFindBest) \
81 LFUNC_FA_(CodesetsUTF8Len) \
82 LFUNC_FA_(CodesetsUTF8ToStrA) \
83 LFUNC_VA_(CodesetsUTF8ToStr) \
84 LFUNC_FA_(CodesetsUTF8CreateA) \
85 LFUNC_VA_(CodesetsUTF8Create) \
86 LFUNC_FA_(CodesetsEncodeB64A) \
87 LFUNC_VA_(CodesetsEncodeB64) \
88 LFUNC_FA_(CodesetsDecodeB64A) \
89 LFUNC_VA_(CodesetsDecodeB64) \
90 LFUNC_FA_(CodesetsStrLenA) \
91 LFUNC_VA_(CodesetsStrLen) \
92 LFUNC_FA_(CodesetsIsValidUTF8) \
93 LFUNC_FA_(CodesetsFreeVecPooledA) \
94 LFUNC_VA_(CodesetsFreeVecPooled) \
95 LFUNC_FA_(CodesetsConvertStrA) \
96 LFUNC_VA_(CodesetsConvertStr) \
97 LFUNC_FA_(CodesetsListCreateA) \
98 LFUNC_VA_(CodesetsListCreate) \
99 LFUNC_FA_(CodesetsListDeleteA) \
100 LFUNC_VA_(CodesetsListDelete) \
101 LFUNC_FA_(CodesetsListAddA) \
102 LFUNC_VA_(CodesetsListAdd) \
103 LFUNC_FA_(CodesetsListRemoveA) \
104 LFUNC_VA_(CodesetsListRemove)
107 /****************************************************************************/
109 #if defined(__amigaos4__)
111 static struct LibraryHeader
* LIBFUNC
LibInit (struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecIFace
*pIExec
);
112 static BPTR LIBFUNC
LibExpunge (struct LibraryManagerInterface
*Self
);
113 static struct LibraryHeader
* LIBFUNC
LibOpen (struct LibraryManagerInterface
*Self
, ULONG version
);
114 static BPTR LIBFUNC
LibClose (struct LibraryManagerInterface
*Self
);
115 static LONG LIBFUNC
LibNull (void);
117 #elif defined(__MORPHOS__)
119 static struct LibraryHeader
* LIBFUNC
LibInit (struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecBase
*sb
);
120 static BPTR LIBFUNC
LibExpunge(void);
121 static struct LibraryHeader
* LIBFUNC
LibOpen (void);
122 static BPTR LIBFUNC
LibClose (void);
123 static LONG LIBFUNC
LibNull (void);
127 static struct LibraryHeader
* LIBFUNC
LibInit (REG(a0
, BPTR Segment
), REG(d0
, struct LibraryHeader
*lh
), REG(a6
, struct ExecBase
*sb
));
128 static BPTR LIBFUNC
LibExpunge (REG(a6
, struct LibraryHeader
*base
));
129 static struct LibraryHeader
* LIBFUNC
LibOpen (REG(a6
, struct LibraryHeader
*base
));
130 static BPTR LIBFUNC
LibClose (REG(a6
, struct LibraryHeader
*base
));
131 static LONG LIBFUNC
LibNull (void);
135 /****************************************************************************/
138 * The system (and compiler) rely on a symbol named _start which marks
139 * the beginning of execution of an ELF file. To prevent others from
140 * executing this library, and to keep the compiler/linker happy, we
141 * define an empty _start symbol here.
143 * On the classic system (pre-AmigaOS4) this was usually done by
149 #if defined(__amigaos4__)
158 static LONG LIBFUNC
LibNull(VOID
)
163 /****************************************************************************/
165 #if defined(__amigaos4__)
166 /* ------------------- OS4 Manager Interface ------------------------ */
167 STATIC uint32
_manager_Obtain(struct LibraryManagerInterface
*Self
)
170 __asm__
__volatile__(
176 : "r" (&Self
->Data
.RefCount
)
182 STATIC uint32
_manager_Release(struct LibraryManagerInterface
*Self
)
185 __asm__
__volatile__(
191 : "r" (&Self
->Data
.RefCount
)
197 STATIC CONST CONST_APTR lib_manager_vectors
[] =
199 (CONST_APTR
)_manager_Obtain
,
200 (CONST_APTR
)_manager_Release
,
204 (CONST_APTR
)LibClose
,
205 (CONST_APTR
)LibExpunge
,
210 STATIC CONST
struct TagItem lib_managerTags
[] =
212 { MIT_Name
, (Tag
)"__library" },
213 { MIT_VectorTable
, (Tag
)lib_manager_vectors
},
218 /* ------------------- Library Interface(s) ------------------------ */
220 ULONG
LibObtain(UNUSED
struct Interface
*Self
)
225 ULONG
LibRelease(UNUSED
struct Interface
*Self
)
230 STATIC CONST CONST_APTR main_vectors
[] =
232 (CONST_APTR
)LibObtain
,
233 (CONST_APTR
)LibRelease
,
236 (CONST_APTR
)libvector
,
240 STATIC CONST
struct TagItem mainTags
[] =
242 { MIT_Name
, (Tag
)"main" },
243 { MIT_VectorTable
, (Tag
)main_vectors
},
248 STATIC CONST CONST_APTR libInterfaces
[] =
250 (CONST_APTR
)lib_managerTags
,
251 (CONST_APTR
)mainTags
,
255 // Our libraries always have to carry a 68k jump table with it, so
256 // lets define it here as extern, as we are going to link it to
258 #ifndef NO_VECTABLE68K
259 extern CONST APTR VecTable68K
[];
262 STATIC CONST
struct TagItem libCreateTags
[] =
264 { CLT_DataSize
, sizeof(struct LibraryHeader
) },
265 { CLT_InitFunc
, (Tag
)LibInit
},
266 { CLT_Interfaces
, (Tag
)libInterfaces
},
267 #ifndef NO_VECTABLE68K
268 { CLT_Vector68K
, (Tag
)VecTable68K
},
275 STATIC CONST CONST_APTR LibVectors
[] =
278 (CONST_APTR
)FUNCARRAY_32BIT_NATIVE
,
281 (CONST_APTR
)LibClose
,
282 (CONST_APTR
)LibExpunge
,
284 (CONST_APTR
)libvector
,
288 STATIC CONST ULONG LibInitTab
[] =
290 sizeof(struct LibraryHeader
),
298 /****************************************************************************/
300 static const USED_VAR
struct Resident ROMTag
=
303 (struct Resident
*)&ROMTag
,
304 (struct Resident
*)(&ROMTag
+ 1),
305 #if defined(__amigaos4__)
306 RTF_AUTOINIT
|RTF_NATIVE
, // The Library should be set up according to the given table.
307 #elif defined(__MORPHOS__)
308 RTF_AUTOINIT
|RTF_PPC
,
316 (char *)UserLibID
+6, // +6 to skip '$VER: '
317 #if defined(__amigaos4__)
318 (APTR
)libCreateTags
// This table is for initializing the Library.
322 #if defined(__MORPHOS__)
328 #if defined(__MORPHOS__)
330 * To tell the loader that this is a new emulppc elf and not
331 * one for the ppc.library.
334 const USED_VAR ULONG __amigappc__
= 1;
335 const USED_VAR ULONG __abox__
= 1;
337 #endif /* __MORPHOS */
339 /****************************************************************************/
341 #if defined(MIN_STACKSIZE) && !defined(__amigaos4__)
343 /* generic StackSwap() function which calls function() surrounded by
345 extern ULONG
stackswap_call(struct StackSwapStruct
*stack
,
346 ULONG (*function
)(struct LibraryHeader
*),
347 struct LibraryHeader
*arg
);
349 #if defined(__mc68000__)
352 .globl _stackswap_call \n\
354 moveml #0x3022,sp@- \n\
358 movel _SysBase,a6 \n\
365 movel _SysBase,a6 \n\
369 moveml sp@+,#0x440c \n\
371 #elif !defined(__MORPHOS__)
372 ULONG
stackswap_call(struct StackSwapStruct
*stack
,
373 ULONG (*function
)(struct LibraryHeader
*),
374 struct LibraryHeader
*arg
)
376 register ULONG result
;
378 result
= function(arg
);
384 static BOOL
callLibFunction(ULONG (*function
)(struct LibraryHeader
*), struct LibraryHeader
*arg
)
386 BOOL success
= FALSE
;
390 // retrieve the task structure for the
394 #if defined(__MORPHOS__)
395 // In MorphOS we have two stacks. One for PPC code and another for 68k code.
396 // We are only interested in the PPC stack.
397 NewGetTaskAttrsA(tc
, &stacksize
, sizeof(ULONG
), TASKINFOTYPE_STACKSIZE
, NULL
);
399 // on all other systems we query via SPUpper-SPLower calculation
400 stacksize
= tc
->tc_SPUpper
- tc
->tc_SPLower
;
403 // Swap stacks only if current stack is insufficient
404 if(stacksize
< MIN_STACKSIZE
)
406 struct StackSwapStruct
*stack
;
408 if((stack
= AllocVec(sizeof(*stack
), MEMF_PUBLIC
)) != NULL
)
410 if((stack
->stk_Lower
= AllocVec(MIN_STACKSIZE
, MEMF_PUBLIC
)) != NULL
)
412 #if defined(__MORPHOS__)
413 struct PPCStackSwapArgs swapargs
;
416 // perform the StackSwap
417 stack
->stk_Upper
= (ULONG
)stack
->stk_Lower
+ MIN_STACKSIZE
;
418 stack
->stk_Pointer
= (APTR
)stack
->stk_Upper
;
420 // call routine but with embedding it into a [NewPPC]StackSwap()
421 #if defined(__MORPHOS__)
422 swapargs
.Args
[0] = (ULONG
)arg
;
423 success
= NewPPCStackSwap(stack
, function
, &swapargs
);
425 success
= stackswap_call(stack
, function
, arg
);
428 FreeVec(stack
->stk_Lower
);
434 success
= function(arg
);
439 #endif // !__amigaos4__
441 /****************************************************************************/
443 #if defined(__amigaos4__)
444 static struct LibraryHeader
* LibInit(struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecIFace
*pIExec
)
446 struct ExecBase
*sb
= (struct ExecBase
*)pIExec
->Data
.LibBase
;
448 #elif defined(__MORPHOS__)
449 static struct LibraryHeader
* LibInit(struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecBase
*sb
)
452 static struct LibraryHeader
* LIBFUNC
LibInit(REG(a0
, BPTR librarySegment
), REG(d0
, struct LibraryHeader
*base
), REG(a6
, struct ExecBase
*sb
))
458 // make sure that this is really a 68020+ machine if optimized for 020+
459 #if _M68060 || _M68040 || _M68030 || _M68020 || __mc68020 || __mc68030 || __mc68040 || __mc68060
460 if(!(SysBase
->AttnFlags
& AFF_68020
))
464 #if defined(__amigaos4__) && defined(__NEWLIB__)
465 if((NewlibBase
= OpenLibrary("newlib.library", 3)) &&
466 GETINTERFACE(INewlib
, NewlibBase
))
469 BOOL success
= FALSE
;
471 D(DBF_STARTUP
, "LibInit()");
473 // cleanup the library header structure beginning with the
475 base
->libBase
.lib_Node
.ln_Type
= NT_LIBRARY
;
476 base
->libBase
.lib_Node
.ln_Pri
= 0;
477 base
->libBase
.lib_Node
.ln_Name
= (char *)UserLibName
;
478 base
->libBase
.lib_Flags
= LIBF_CHANGED
| LIBF_SUMUSED
;
479 base
->libBase
.lib_Version
= LIB_VERSION
;
480 base
->libBase
.lib_Revision
= LIB_REVISION
;
481 base
->libBase
.lib_IdString
= (char *)(UserLibID
+6);
483 InitSemaphore(&base
->libSem
);
484 InitSemaphore(&base
->poolSem
);
486 base
->sysBase
= (APTR
)SysBase
;
489 base
->systemCodeset
= NULL
;
491 // protect access to initBase()
492 ObtainSemaphore(&base
->libSem
);
494 // set the CodesetsBase
497 // If we are not running on AmigaOS4 (no stackswap required) we go and
498 // do an explicit StackSwap() in case the user wants to make sure we
499 // have enough stack for his user functions
500 #if defined(MIN_STACKSIZE) && !defined(__amigaos4__)
501 success
= callLibFunction(initBase
, base
);
503 success
= initBase(base
);
506 // unprotect initBase()
507 ReleaseSemaphore(&base
->libSem
);
509 // check if everything worked out fine
512 // everything was successfully so lets
513 // set the initialized value and contiue
514 // with the class open phase
515 base
->segList
= librarySegment
;
517 // return the library base as success
523 #if defined(__amigaos4__) && defined(__NEWLIB__)
526 DROPINTERFACE(INewlib
);
527 CloseLibrary(NewlibBase
);
536 /****************************************************************************/
539 #define DeleteLibrary(LIB) \
540 FreeMem((STRPTR)(LIB)-(LIB)->lib_NegSize, (ULONG)((LIB)->lib_NegSize+(LIB)->lib_PosSize))
543 #if defined(__amigaos4__)
544 static BPTR
LibExpunge(struct LibraryManagerInterface
*Self
)
546 struct ExecIFace
*IExec
= (struct ExecIFace
*)(*(struct ExecBase
**)4)->MainInterface
;
547 struct LibraryHeader
*base
= (struct LibraryHeader
*)Self
->Data
.LibBase
;
548 #elif defined(__MORPHOS__)
549 static BPTR
LibExpunge(void)
551 struct LibraryHeader
*base
= (struct LibraryHeader
*)REG_A6
;
553 static BPTR LIBFUNC
LibExpunge(REG(a6
, struct LibraryHeader
*base
))
558 D(DBF_STARTUP
, "LibExpunge(): %ld", base
->libBase
.lib_OpenCnt
);
560 // in case our open counter is still > 0, we have
561 // to set the late expunge flag and return immediately
562 if(base
->libBase
.lib_OpenCnt
> 0)
564 base
->libBase
.lib_Flags
|= LIBF_DELEXP
;
569 // make sure to restore the SysBase
570 SysBase
= (APTR
)base
->sysBase
;
572 // remove the library base from exec's lib list in advance
573 Remove((struct Node
*)base
);
575 // free all our private data and stuff.
576 ObtainSemaphore(&base
->libSem
);
578 // make sure we have enough stack here
579 #if defined(MIN_STACKSIZE) && !defined(__amigaos4__)
580 callLibFunction(freeBase
, base
);
586 ReleaseSemaphore(&base
->libSem
);
588 #if defined(__amigaos4__) && defined(__NEWLIB__)
591 DROPINTERFACE(INewlib
);
592 CloseLibrary(NewlibBase
);
597 // make sure the system deletes the library as well.
599 DeleteLibrary(&base
->libBase
);
605 /****************************************************************************/
607 #if defined(__amigaos4__)
608 static struct LibraryHeader
*LibOpen(struct LibraryManagerInterface
*Self
, ULONG version UNUSED
)
610 struct LibraryHeader
*base
= (struct LibraryHeader
*)Self
->Data
.LibBase
;
611 #elif defined(__MORPHOS__)
612 static struct LibraryHeader
*LibOpen(void)
614 struct LibraryHeader
*base
= (struct LibraryHeader
*)REG_A6
;
616 static struct LibraryHeader
* LIBFUNC
LibOpen(REG(a6
, struct LibraryHeader
*base
))
619 struct LibraryHeader
*res
= base
;
621 D(DBF_STARTUP
, "LibOpen(): %ld", base
->libBase
.lib_OpenCnt
);
623 // LibOpen(), LibClose() and LibExpunge() are called while the system is in
624 // Forbid() state. That means that these functions should be quick and should
625 // not break this Forbid()!! Therefore the open counter should be increased
626 // as the very first instruction during LibOpen(), because a ClassOpen()
627 // which breaks a Forbid() and another task calling LibExpunge() will cause
628 // to expunge this library while it is not yet fully initialized. A crash
629 // is unavoidable then. Even the semaphore does not guarantee 100% protection
630 // against such a race condition, because waiting for the semaphore to be
631 // obtained will effectively break the Forbid()!
633 // increase the open counter ahead of anything else
634 base
->libBase
.lib_OpenCnt
++;
636 // delete the late expunge flag
637 base
->libBase
.lib_Flags
&= ~LIBF_DELEXP
;
642 /****************************************************************************/
644 #if defined(__amigaos4__)
645 static BPTR
LibClose(struct LibraryManagerInterface
*Self
)
647 struct LibraryHeader
*base
= (struct LibraryHeader
*)Self
->Data
.LibBase
;
648 #elif defined(__MORPHOS__)
649 static BPTR
LibClose(void)
651 struct LibraryHeader
*base
= (struct LibraryHeader
*)REG_A6
;
653 static BPTR LIBFUNC
LibClose(REG(a6
, struct LibraryHeader
*base
))
658 D(DBF_STARTUP
, "LibClose(): %ld", base
->libBase
.lib_OpenCnt
);
660 // decrease the open counter
661 base
->libBase
.lib_OpenCnt
--;
663 // in case the opern counter is <= 0 we can
664 // make sure that we free everything
665 if(base
->libBase
.lib_OpenCnt
<= 0)
667 // in case the late expunge flag is set we go and
668 // expunge the library base right now
669 if(base
->libBase
.lib_Flags
& LIBF_DELEXP
)
671 #if defined(__amigaos4__)
672 rc
= LibExpunge(Self
);
673 #elif defined(__MORPHOS__)
676 rc
= LibExpunge(base
);
686 /****************************************************************************/