Subversion Repositories filter_foundry

Rev

Rev 210 | Rev 212 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
184 dmarschall 1
/*
207 daniel-mar 2
        This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
3
        Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
        Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
184 dmarschall 5
 
207 daniel-mar 6
        This program is free software; you can redistribute it and/or modify
7
        it under the terms of the GNU General Public License as published by
8
        the Free Software Foundation; either version 2 of the License, or
9
        (at your option) any later version.
184 dmarschall 10
 
207 daniel-mar 11
        This program is distributed in the hope that it will be useful,
12
        but WITHOUT ANY WARRANTY; without even the implied warranty of
13
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
        GNU General Public License for more details.
184 dmarschall 15
 
207 daniel-mar 16
        You should have received a copy of the GNU General Public License
17
        along with this program; if not, write to the Free Software
18
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
184 dmarschall 19
*/
20
 
21
#include <stddef.h>
22
#include <stdint.h>
185 dmarschall 23
#include <assert.h>
184 dmarschall 24
 
25
#include "ff.h"
26
#include "symtab.h"
27
 
210 daniel-mar 28
#include "scripting.h"
184 dmarschall 29
#include "PIActions.h"
30
#include "PITerminology.h"
31
 
32
#include "compat_string.h"
33
 
207 daniel-mar 34
//#define PRINTABLE_HASH_FF16
35
 
184 dmarschall 36
/*
207 daniel-mar 37
Find a printable 4-character key, remembering (see Photoshop API guide):
38
- All IDs starting with an uppercase letter are reserved by Adobe.
39
- All IDs that are all uppercase are reserved by Apple.
40
- All IDs that are all lowercase are reserved by Apple.
41
- This leaves all IDs that begin with a lowercase letter and have at least
42
  one uppercase letter for you and other plug-in developers.
43
Note: It is questionable if "a!!!" or "A!!!" are also reseved by Apple. We don't risk it.
184 dmarschall 44
*/
45
unsigned long printablehash(unsigned long hash) {
207 daniel-mar 46
#ifdef PRINTABLE_HASH_FF16
47
        // FilterFoundry version 1.6 hashing by Toby Thain
48
        // Only accepts upper case at the last character
49
        // 0       = 'a  A'
50
        // 6100899 = 'z~~Z'
51
        unsigned long key = 'a' + (hash % 26);  hash /= 26; // first lower case
52
        key = (key << 8) | (' ' + (hash % 95)); hash /= 95; // any printable
53
        key = (key << 8) | (' ' + (hash % 95)); hash /= 95; // any printable
54
        return  (key << 8) | ('A' + (hash % 26));           // last upper case
55
#else
56
        // FilterFoundry version 1.7 hashing by Daniel Marschall
57
        // Accepts upper case at character 2, 3 or 4
58
        // Spaces are only set the right as padding to make a code shorter
59
        // 0        = 'aA  '
60
        // 13530139 = 'zZZZ'
61
        // The key-space is ~2.22 times larger
62
        long lowlim;
63
        long uplim;
64
        int upperCaseInfo;
65
        int length;
66
        int lastThreeCharInfo;
67
        int firstChar;
68
        unsigned long key;
69
        int found;
70
        int i,j,k;
71
 
72
        uplim = 0;
73
        for (k=1; k<4; k++) {
74
                int mask;
75
                if (k == 1) mask = 1;//0b1;
76
                if (k == 2) mask = 2;//0b11;
77
                if (k == 3) mask = 4;//0b111;
78
                for (i=1; i<=mask; i++) {
79
                        // 'k' characters
80
                        long test = 1;
81
                        for (j=0; j<k; ++j) {
82
                                test *= ((i&(1<<j)) != 0) ? 26 : 94-26;
83
                        }
84
                        uplim += test;
85
                }
86
        }
87
 
88
        lastThreeCharInfo = hash % uplim; hash /= uplim;
89
        firstChar = hash%26; hash /= 26;
90
 
91
        lowlim = 0;
92
        uplim = 0;
93
        found = 0;
94
        length = -1; // avoid compiler warning
95
        upperCaseInfo = -1; // avoid compiler warning
96
        for (k=1; k<4; k++) {
97
                int mask;
98
                if (k == 1) mask = 1;//0b1;
99
                if (k == 2) mask = 2;//0b11;
100
                if (k == 3) mask = 4;//0b111;
101
                if (!found) for (i=1; i<=mask; i++) {
102
                        // 'k' characters
103
                        long test = 1;
104
                        for (j=0; j<k; ++j) {
105
                                test *= ((i&(1<<j)) != 0) ? 26 : 94-26;
106
                        }
107
                        uplim += test;
108
                        if ((lastThreeCharInfo >= lowlim) && (lastThreeCharInfo < uplim)) {
109
                                lastThreeCharInfo -= lowlim;
110
                                found = 1;
111
                                length = k;
112
                                upperCaseInfo = i;
113
                                break;
114
                        }
115
                        lowlim = uplim;
116
                }
117
        }
118
 
119
        key = ('a' + firstChar) << 24; // first char is lower-case
120
        for (i=0; i<length; ++i) {
121
                char res;
122
                if ((upperCaseInfo&(1<<i)) == 0) {
123
                        res = '!' + (lastThreeCharInfo % (94-26));
124
                        if (res >= 'A') res += 26;
125
                        lastThreeCharInfo /= (94-26);
126
                } else {
127
                        res = 'A' + (lastThreeCharInfo % 26);
128
                        lastThreeCharInfo /= 26;
129
                }
130
                key |= res << (8*(i+(4-length-1))); // 2nd, 3rd, 4th char are either upper-case or any printable char
131
        }
132
        if (length == 1) {
133
                key &= 0xFFFF0000;
134
                key |= ' ' << 8;
135
                key |= ' ';
136
        }
137
        if (length == 2) {
138
                key &= 0xFFFFFF00;
139
                key |= ' ';
140
        }
141
        return key;
142
#endif
184 dmarschall 143
}
144
 
145
long roundToNext4(long x) {
207 daniel-mar 146
        int pad = 4 - (x % 4);
147
        if (pad == 0) pad = 4;
148
        return x + pad;
184 dmarschall 149
}
150
 
208 daniel-mar 151
size_t fixpipl(PIPropertyList *pipl, size_t origsize, StringPtr title, long *event_id) {
207 daniel-mar 152
        PIProperty *prop;
153
        char *p;
154
        struct hstm_data {
155
                /* this structure must be 14+1 bytes long, to match PiPL structure */
156
                long version; /* = 0 */
157
                long class_id;
158
                long event_id;
159
                short aete_resid;
160
                char scope[1];
161
        };
162
        struct hstm_data *hstm;
163
        int scopelen;
164
        unsigned long hash;
184 dmarschall 165
 
207 daniel-mar 166
        pipl->count += 3; // 3 more keys in PiPL: name, catg, hstm
184 dmarschall 167
 
207 daniel-mar 168
        p = (char*)pipl + origsize;
169
        prop = (PIProperty*)p;
184 dmarschall 170
 
207 daniel-mar 171
        /* add Title/Name property key */
184 dmarschall 172
 
207 daniel-mar 173
        prop->vendorID = kPhotoshopSignature;
174
        prop->propertyKey = PINameProperty;
175
        prop->propertyID = 0;
176
        prop->propertyLength = roundToNext4(title[0] + 1);
177
        PLstrcpy((StringPtr)prop->propertyData, title);
184 dmarschall 178
 
207 daniel-mar 179
        // skip past new property record, and any padding
180
        p += offsetof(PIProperty, propertyData) + prop->propertyLength;
181
        prop = (PIProperty*)p;
184 dmarschall 182
 
207 daniel-mar 183
        /* add Category property key */
184 dmarschall 184
 
207 daniel-mar 185
        prop->vendorID = kPhotoshopSignature;
186
        prop->propertyKey = PICategoryProperty;
187
        prop->propertyID = 0;
188
        prop->propertyLength = roundToNext4(gdata->parm.category[0] + 1);
189
        PLstrcpy((StringPtr)prop->propertyData, gdata->parm.category);
184 dmarschall 190
 
207 daniel-mar 191
        p += offsetof(PIProperty, propertyData) + prop->propertyLength;
192
        prop = (PIProperty*)p;
184 dmarschall 193
 
207 daniel-mar 194
        /* add HasTerminology property key */
184 dmarschall 195
 
207 daniel-mar 196
        /* construct scope string by concatenating Category and Title - hopefully unique! */
210 daniel-mar 197
        // Note: In doresources() we malloc'ed 300h additional bytes,
198
        // and in the build dialog, title and category are size-limited,
199
        // so we can write here without malloc.
207 daniel-mar 200
        hstm = (struct hstm_data*)prop->propertyData;
201
        scopelen = sprintf(hstm->scope, "%s %s",
202
                INPLACEP2CSTR(gdata->parm.category),
203
                INPLACEP2CSTR(title));
184 dmarschall 204
 
207 daniel-mar 205
        /* make up a new event ID for this aete, based on printable base-95 hash of scope */
206
        hash = djb2(hstm->scope);
208 daniel-mar 207
        *event_id = printablehash(hash); /* this is used by aete_generate() later... */
184 dmarschall 208
 
207 daniel-mar 209
        prop->vendorID = kPhotoshopSignature;
210
        prop->propertyKey = PIHasTerminologyProperty;
211
        prop->propertyID = 0;
212
        prop->propertyLength = roundToNext4(offsetof(struct hstm_data, scope) + scopelen);
184 dmarschall 213
 
207 daniel-mar 214
        hstm->version = 0;
215
        hstm->class_id = plugInClassID;
209 daniel-mar 216
        hstm->event_id = *event_id;
207 daniel-mar 217
        hstm->aete_resid = AETE_ID;
184 dmarschall 218
 
207 daniel-mar 219
        p += offsetof(PIProperty, propertyData) + prop->propertyLength;
184 dmarschall 220
 
207 daniel-mar 221
        return p - (char*)pipl;  // figure how many bytes were added
184 dmarschall 222
}
223
 
187 dmarschall 224
void _aete_write_byte(void** aeteptr, uint8_t val) {
207 daniel-mar 225
        uint8_t* tmp = *((uint8_t**)aeteptr);
226
        *tmp = val;
227
        *aeteptr = (void*)((unsigned char*)tmp + 1);
187 dmarschall 228
}
229
#define AETE_WRITE_BYTE(i) _aete_write_byte(&aeteptr, (i));
184 dmarschall 230
 
187 dmarschall 231
void _aete_write_word(void** aeteptr, uint16_t val) {
207 daniel-mar 232
        uint16_t* tmp = *((uint16_t**)aeteptr);
233
        *tmp = val;
234
        *aeteptr = (void*)((unsigned char*)tmp + 2);
187 dmarschall 235
}
236
#define AETE_WRITE_WORD(i) _aete_write_word(&aeteptr, (i));
237
 
238
void _aete_write_dword(void** aeteptr, uint32_t val) {
207 daniel-mar 239
        uint32_t* tmp = *((uint32_t**)aeteptr);
240
        *tmp = val;
241
        *aeteptr = (void*)((unsigned char*)tmp + 4);
187 dmarschall 242
}
243
#define AETE_WRITE_DWORD(i) _aete_write_dword(&aeteptr, (i));
244
 
245
void _aete_write_pstr(void** aeteptr, char* str) {
207 daniel-mar 246
        char* tmp;
187 dmarschall 247
 
207 daniel-mar 248
        assert(strlen(str) <= 255);
187 dmarschall 249
 
207 daniel-mar 250
        _aete_write_byte(aeteptr, (uint8_t)strlen(str));
187 dmarschall 251
 
207 daniel-mar 252
        tmp = *((char**)aeteptr);
253
        strcpy(tmp, str);
254
        *aeteptr = (void*)((unsigned char*)tmp + strlen(str));
187 dmarschall 255
}
256
#define AETE_WRITE_PSTR(s) _aete_write_pstr(&aeteptr, (s));
257
 
258
void _aete_align_word(void** aeteptr) {
207 daniel-mar 259
        #ifdef MAC_ENV
260
        unsigned char* tmp = *((unsigned char**)aeteptr);
261
        tmp += (intptr_t)tmp & 1;
262
        *aeteptr = (void*)tmp;
263
        #endif
187 dmarschall 264
}
265
#define AETE_ALIGN_WORD() _aete_align_word(&aeteptr);
266
 
185 dmarschall 267
void* _aete_property(void* aeteptr, PARM_T *pparm, int ctlidx, int mapidx, OSType key) {
207 daniel-mar 268
        char tmp[256];
185 dmarschall 269
 
207 daniel-mar 270
        if (pparm->ctl_used[ctlidx] || pparm->map_used[mapidx]) {
271
                if (pparm->map_used[mapidx]) {
272
                        if (ctlidx & 1) {
273
                                sprintf(tmp, "... %s", (char*)pparm->map[mapidx]);
274
                        } else {
275
                                sprintf(tmp, "%s ...", (char*)pparm->map[mapidx]);
276
                        }
277
                        AETE_WRITE_PSTR(tmp);
278
                } else {
279
                        AETE_WRITE_PSTR((char*)pparm->ctl[ctlidx]);
280
                }
281
                AETE_ALIGN_WORD();
282
                AETE_WRITE_DWORD(key);
283
                AETE_WRITE_DWORD(typeSInt32);
284
                AETE_WRITE_PSTR(_strdup(""));
285
                AETE_ALIGN_WORD();
286
                AETE_WRITE_WORD(0x8000); /* FLAGS_1_OPT_PARAM / flagsOptionalSingleParameter */
287
        }
185 dmarschall 288
 
207 daniel-mar 289
        return aeteptr;
185 dmarschall 290
}
291
 
208 daniel-mar 292
size_t aete_generate(void* aeteptr, PARM_T *pparm, long event_id) {
207 daniel-mar 293
        int numprops;
294
        void *beginptr = aeteptr;
210 daniel-mar 295
 
207 daniel-mar 296
        // Attention!
297
        // - On some systems (e.g. ARM based CPUs) this will cause an unaligned memory access exception.
298
        //   For X86, memory access just becomes slower.
299
        // - If you change something here, please also change it in Scripting.rc (Windows) and scripting.r (Mac OS)
184 dmarschall 300
 
207 daniel-mar 301
        // Note:
302
        // - The 'aete' resource for Mac OS has word alignments after strings (but not if the next element is also a string)
303
        //   see https://developer.apple.com/library/archive/documentation/mac/pdf/Interapplication_Communication/AE_Term_Resources.pdf page 8-9
184 dmarschall 304
 
305
#ifdef WIN_ENV
207 daniel-mar 306
        AETE_WRITE_WORD(0x0001); /* Reserved (for Photoshop) */
184 dmarschall 307
#endif
207 daniel-mar 308
        AETE_WRITE_BYTE(0x01); /* aete version */
309
        AETE_WRITE_BYTE(0x00); /* aete version */
310
        AETE_WRITE_WORD(english); /* language specifiers */
311
        AETE_WRITE_WORD(roman);
312
        AETE_WRITE_WORD(1); /* 1 suite */
313
        {
314
                AETE_WRITE_PSTR(/*"Telegraphics"*/(char*)pparm->author); /* vendor suite name */
315
                AETE_WRITE_PSTR(_strdup("")); /* optional description */
316
                AETE_ALIGN_WORD();
317
                AETE_WRITE_DWORD(plugInSuiteID); /* suite ID */
318
                AETE_WRITE_WORD(1); /* suite code, must be 1. Attention: Filters like 'Pointillize' have set this to 0! */
319
                AETE_WRITE_WORD(1); /* suite level, must be 1. Attention: Filters like 'Pointillize' have set this to 0! */
320
                AETE_WRITE_WORD(1); /* 1 event (structure for filters) */
321
                {
322
                        AETE_WRITE_PSTR(/*"FilterFoundry"*/(char*)pparm->title); /* event name */
323
                        AETE_WRITE_PSTR(_strdup("")); /* event description */
324
                        AETE_ALIGN_WORD();
325
                        AETE_WRITE_DWORD(plugInClassID); /* event class */
326
                        AETE_WRITE_DWORD(/*plugInEventID*/event_id); /* event ID */
327
                        /* NO_REPLY: */
328
                        AETE_WRITE_DWORD(noReply); /* noReply='null' */
329
                        AETE_WRITE_PSTR(_strdup("")); /* reply description */
330
                        AETE_ALIGN_WORD();
331
                        AETE_WRITE_WORD(0);
332
                        /* IMAGE_DIRECT_PARAM: */
333
                        AETE_WRITE_DWORD(typeImageReference); /* typeImageReference='#ImR' */
334
                        AETE_WRITE_PSTR(_strdup("")); /* direct parm description */
335
                        AETE_ALIGN_WORD();
336
                        AETE_WRITE_WORD(0xB000);
184 dmarschall 337
 
207 daniel-mar 338
                        numprops = 0;
339
                        if (pparm->ctl_used[0] || pparm->map_used[0]) numprops++;
340
                        if (pparm->ctl_used[1] || pparm->map_used[0]) numprops++;
341
                        if (pparm->ctl_used[2] || pparm->map_used[1]) numprops++;
342
                        if (pparm->ctl_used[3] || pparm->map_used[1]) numprops++;
343
                        if (pparm->ctl_used[4] || pparm->map_used[2]) numprops++;
344
                        if (pparm->ctl_used[5] || pparm->map_used[2]) numprops++;
345
                        if (pparm->ctl_used[6] || pparm->map_used[3]) numprops++;
346
                        if (pparm->ctl_used[7] || pparm->map_used[3]) numprops++;
347
                        AETE_WRITE_WORD(numprops);
348
                        {
349
                                // Standalone filters don't need RGBA expressions
350
                                /*
351
                                AETE_WRITE_PSTR("R");
352
                                AETE_ALIGN_WORD();
353
                                AETE_WRITE_DWORD(PARAM_R_KEY);
354
                                AETE_WRITE_DWORD(typeText);
355
                                AETE_WRITE_PSTR("R channel expression");
356
                                AETE_ALIGN_WORD();
357
                                AETE_WRITE_WORD(0x8000);
184 dmarschall 358
 
207 daniel-mar 359
                                AETE_WRITE_PSTR("G");
360
                                AETE_ALIGN_WORD();
361
                                AETE_WRITE_DWORD(PARAM_G_KEY);
362
                                AETE_WRITE_DWORD(typeText);
363
                                AETE_WRITE_PSTR("G channel expression");
364
                                AETE_ALIGN_WORD();
365
                                AETE_WRITE_WORD(0x8000);
184 dmarschall 366
 
207 daniel-mar 367
                                AETE_WRITE_PSTR("B");
368
                                AETE_ALIGN_WORD();
369
                                AETE_WRITE_DWORD(PARAM_B_KEY);
370
                                AETE_WRITE_DWORD(typeText);
371
                                AETE_WRITE_PSTR("B channel expression");
372
                                AETE_ALIGN_WORD();
373
                                AETE_WRITE_WORD(0x8000);
184 dmarschall 374
 
207 daniel-mar 375
                                AETE_WRITE_PSTR("A");
376
                                AETE_ALIGN_WORD();
377
                                AETE_WRITE_DWORD(PARAM_A_KEY);
378
                                AETE_WRITE_DWORD(typeText);
379
                                AETE_WRITE_PSTR("A channel expression");
380
                                AETE_ALIGN_WORD();
381
                                AETE_WRITE_WORD(0x8000);
382
                                */
184 dmarschall 383
 
210 daniel-mar 384
                                aeteptr = _aete_property(aeteptr, pparm, 0, 0, getAeteKey('0', pparm));
385
                                aeteptr = _aete_property(aeteptr, pparm, 1, 0, getAeteKey('1', pparm));
386
                                aeteptr = _aete_property(aeteptr, pparm, 2, 1, getAeteKey('2', pparm));
387
                                aeteptr = _aete_property(aeteptr, pparm, 3, 1, getAeteKey('3', pparm));
388
                                aeteptr = _aete_property(aeteptr, pparm, 4, 2, getAeteKey('4', pparm));
389
                                aeteptr = _aete_property(aeteptr, pparm, 5, 2, getAeteKey('5', pparm));
390
                                aeteptr = _aete_property(aeteptr, pparm, 6, 3, getAeteKey('6', pparm));
391
                                aeteptr = _aete_property(aeteptr, pparm, 7, 3, getAeteKey('7', pparm));
207 daniel-mar 392
                        }
393
                }
184 dmarschall 394
 
207 daniel-mar 395
                /* non-filter plug-in class here */
396
                AETE_WRITE_WORD(0); /* 0 classes */
397
                {}
398
                AETE_WRITE_WORD(0); /* 0 comparison ops (not supported) */
399
                {}
400
                AETE_WRITE_WORD(0); /* 0 enumerations */
401
                {}
402
        }
403
        AETE_WRITE_DWORD(0); /* padding (FIXME: do we need that? Adobe's Windows filters don't) */
184 dmarschall 404
 
207 daniel-mar 405
        return (unsigned char*)aeteptr - (unsigned char*)beginptr; // length of stuff written
184 dmarschall 406
}
407
 
408
void obfusc(unsigned char *pparm, size_t size) {
207 daniel-mar 409
        size_t i;
410
        unsigned char *p;
411
        uint32_t x32;
184 dmarschall 412
 
207 daniel-mar 413
        x32 = 0x95D4A68F; // Hardcoded seed
414
        for (i = size, p = pparm; i--;) {
415
                // https://de.wikipedia.org/wiki/Xorshift
416
                x32 ^= x32 << 13;
417
                x32 ^= x32 >> 17;
418
                x32 ^= x32 << 5;
419
                *p++ ^= x32;
420
        }
184 dmarschall 421
}
211 daniel-mar 422
 
423
void deobfusc(unsigned char* pparm, size_t size) {
424
    obfusc(pparm, size);
425
}