Subversion Repositories filter_foundry

Rev

Rev 216 | Rev 229 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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