summaryrefslogtreecommitdiff
path: root/unsorted/uLibImporter.c
blob: 3cf347562e97daacbf1f3a4ac704ada2bb41c6c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "cw_common.h"
#include "cos.h"

static CWPluginContext compiler_context;

static void pstrcpy(StringPtr dst, ConstStringPtr src) {
    short len = src[0];
    do {
        *(dst++) = *(src++);
    } while (len-- > 0);
}

static void ReportError(int index, int osErr) {
    char buf[256];
    COS_GetString(buf, 911, index);
    if (osErr)
        CWOSErrorMessage(compiler_context, buf, osErr);
    else
        CWReportMessage(compiler_context, NULL, buf, NULL, messagetypeError, 0);
}

static CWMemHandle ImportObjectLibrary(CWFileSpec *spec, SInt32 *codesize, SInt32 *udatasize, SInt32 *idatasize) {
    int err;
    short ref;
    CWMemHandle mh;
    UInt32 *ptr;
    SInt32 fsize;

    err = COS_FileOpen(spec, &ref);
    if (err) {
        ReportError(1, err);
        return NULL;
    }

    err = COS_FileGetSize(ref, &fsize);
    if (err) {
        COS_FileClose(ref);
        ReportError(1, err);
        return NULL;
    }

    err = CWAllocMemHandle(compiler_context, 8, 1, &mh);
    if (err) {
        COS_FileClose(ref);
        ReportError(1, err);
        return NULL;
    }

    err = CWLockMemHandle(compiler_context, mh, 0, (void **) &ptr);
    if (err) {
        COS_FileClose(ref);
        CWFreeMemHandle(compiler_context, mh);
        ReportError(1, err);
        return NULL;
    }

    err = COS_FileRead(ref, ptr, 8);
    if (err != (short) err) {
        ReportError(1, err);
        return NULL;
    }

    COS_FileClose(ref);

    if (ptr[0] != 0xFEEDFACE && ptr[0] != 0xCAFEBABE && ptr[0] != CWFOURCHAR('!','<','a','r') && ptr[1] != CWFOURCHAR('c','h','>','\n')) {
        CWFreeMemHandle(compiler_context, mh);
        ReportError(2, 0);
        return NULL;
    }

    CWUnlockMemHandle(compiler_context, mh);
    *codesize = fsize;
    *udatasize = 0;
    *idatasize = 0;
    return mh;
}

CWPLUGIN_ENTRY(PPCLibImport_main)(CWPluginContext context) {
    SInt32 request;
    CWResult result;
    CWFileSpec fss;
    SInt32 fileNumber;
    CWObjectData od;

    result = cwNoErr;

    CWGetPluginRequest(context, &request);
    switch (request) {
        case reqInitialize:
        case reqTerminate:
            break;
        case 0:
            compiler_context = context;
            CWGetMainFileSpec(context, &fss);
            CWGetMainFileNumber(context, &fileNumber);
            memset(&od, 0, sizeof(od));
            od.objectdata = ImportObjectLibrary(&fss, &od.codesize, &od.udatasize, &od.idatasize);
            result = CWDonePluginRequest(context, od.objectdata ? CWStoreObjectData(context, fileNumber, &od) : cwErrRequestFailed);
            break;
    }

    return result;
}