Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. #define bool int
  7. #define true 1
  8. #define false 0
  9.  
  10. #include <npapi.h>
  11. #include <npruntime.h>
  12. #include <npfunctions.h>
  13.  
  14. static NPObject        *so       = NULL;
  15. NPNetscapeFuncs        *npnfuncs = NULL;
  16. static NPP             inst      = NULL;
  17.  
  18. #define _log(MSG, ...) do { \
  19.     char buf[300]; \
  20.     sprintf(buf, "echo \"%s: " MSG "\" >> /home/ivan/log.txt", __func__, ## __VA_ARGS__); \
  21.     system(buf); \
  22.     } while (0);
  23.  
  24.  
  25. #ifndef _WIN32
  26. static bool
  27. hasMethod(NPObject *obj, NPIdentifier methodName) {
  28.     _log("methodName %s", npnfuncs->utf8fromidentifier(methodName));
  29.  
  30.     return false;
  31. }
  32. #else
  33. static bool
  34. hasMethod(NPObject *obj, NPIdentifier methodName) {
  35.     return false;
  36. }
  37.  
  38. #endif
  39.  
  40. // Funcs
  41.  
  42. static bool
  43. invokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result) {
  44.     result->type = NPVariantType_Int32;
  45.     result->value.intValue = 42;
  46.     return true;
  47. }
  48.  
  49. static bool
  50. invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
  51.     char *name;
  52.  
  53.     name = npnfuncs->utf8fromidentifier(methodName);
  54.     _log("%s called", name);
  55.     npnfuncs->setexception(obj, "function not found");
  56.     return false;
  57. }
  58.  
  59. enum { FSTRING, FINT };
  60.  
  61. typedef struct {
  62.     int type;
  63.     char *name, *value;
  64. } tprop;
  65.  
  66.  
  67. static tprop props[] = {
  68.     { .name = "example_prop", .value = "SKJY1107", .type = FSTRING, },
  69.     { .name = "proptwo", .value = "e8:5b:5b:24:76:11", .type = FSTRING, },
  70.     //
  71.     { .name = NULL, .value = NULL, .type = FINT, },
  72. };
  73.  
  74. #define FORE_PROP(IT) \
  75.     tprop *IT = (tprop*) props; \
  76.     for(; IT->name != NULL; IT++)
  77.  
  78. static bool
  79. hasProperty(NPObject *obj, NPIdentifier propertyName) {
  80.     char *name;
  81.     int i;
  82.     int cont;
  83.  
  84.     name = npnfuncs->utf8fromidentifier(propertyName);
  85.     cont = 1;
  86.     _log("hasProperty %s", name);
  87.  
  88.     FORE_PROP(prop) {
  89.         cont = strcmp(prop->name, name);
  90.         if (!cont)
  91.             break;
  92.     }
  93.  
  94.     return !cont;
  95. }
  96.  
  97. static bool
  98. getProperty(NPObject *obj, NPIdentifier propertyName, NPVariant *result) {
  99.     char *name;
  100.  
  101.     name = npnfuncs->utf8fromidentifier(propertyName);
  102.  
  103.     FORE_PROP(prop) {
  104.         if (!strcmp(prop->name, name)) {
  105.             if (prop->type == FSTRING) {
  106.                 STRINGZ_TO_NPVARIANT(strdup(prop->value), *result);
  107.             }
  108.             return true;
  109.         }
  110.     }
  111.  
  112.     return false;
  113. }
  114. //
  115.  
  116. static NPClass npcRefObject = {
  117.     NP_CLASS_STRUCT_VERSION,
  118.     0,
  119.     0,
  120.     0,
  121.     hasMethod,
  122.     invoke,
  123.     invokeDefault,
  124.     hasProperty,
  125.     getProperty,
  126.     0,
  127.     0,
  128. };
  129.  
  130. static NPError
  131. plugin_new(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved) {
  132.     inst = instance;
  133.     return NPERR_NO_ERROR;
  134. }
  135.  
  136. static NPError
  137. plugin_destroy(NPP instance, NPSavedData **save) {
  138.     if (so)
  139.         npnfuncs->releaseobject(so);
  140.     so = 0;
  141.     return NPERR_NO_ERROR;
  142.  
  143. }
  144.  
  145. static NPError
  146. plugin_getValue(NPP instance, NPPVariable variable, void *value) {
  147.     inst = instance;
  148.     switch (variable) {
  149.         default:
  150.             return NPERR_GENERIC_ERROR;
  151.         case NPPVpluginNameString:
  152.             *((char **)value) = "Example Plugin";
  153.             break;
  154.         case NPPVpluginDescriptionString:
  155.             *((char **)value) = "Plugin for Firefox";
  156.             break;
  157.         case NPPVpluginScriptableNPObject:
  158.             if(!so)
  159.                 so = npnfuncs->createobject(instance, &npcRefObject);
  160.             npnfuncs->retainobject(so);
  161.             *(NPObject **)value = so;
  162.             break;
  163.     }
  164.     return NPERR_NO_ERROR;
  165. }
  166.  
  167. static NPError
  168. plugin_handleEvent(NPP instance, void *ev) {
  169.     inst = instance;
  170.     return NPERR_NO_ERROR;
  171. }
  172.  
  173. static NPError
  174. plugin_setWindow(NPP instance, NPWindow *pNPWindow) {
  175.     inst = instance;
  176.     return NPERR_NO_ERROR;
  177. }
  178.  
  179. // Define callbacks do plugin
  180. NPError OSCALL
  181. NP_GetEntryPoints(NPPluginFuncs *nppFuncs) {
  182.     nppFuncs->version   = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
  183.     nppFuncs->newp      = plugin_new;
  184.     nppFuncs->destroy   = plugin_destroy;
  185.     nppFuncs->getvalue  = plugin_getValue;
  186.     nppFuncs->event     = plugin_handleEvent;
  187.     nppFuncs->setwindow = plugin_setWindow;
  188.  
  189.     return NPERR_NO_ERROR;
  190. }
  191.  
  192. #ifndef _WIN32
  193. #define HIBYTE(x) ((((uint32_t)(x)) & 0xff00) >> 8)
  194. #endif
  195.  
  196. #ifndef _WIN32
  197. NPError OSCALL
  198. NP_Initialize(NPNetscapeFuncs *npnf, NPPluginFuncs *nppfuncs) {
  199.     if (npnf == 0)
  200.         return NPERR_INVALID_FUNCTABLE_ERROR;
  201.     if (HIBYTE(npnf->version) > NP_VERSION_MAJOR)
  202.         return NPERR_INVALID_FUNCTABLE_ERROR;
  203.     npnfuncs = npnf;
  204.     NP_GetEntryPoints(nppfuncs);
  205.     return NPERR_NO_ERROR;
  206. }
  207. #else
  208. NPError OSCALL NP_Initialize(NPNetscapeFuncs* bFuncs) {
  209.     npnfuncs = bFuncs;
  210. }
  211. #endif
  212.  
  213. NPError OSCALL
  214. NP_Shutdown() {
  215.     return NPERR_NO_ERROR;
  216. }
  217.  
  218. const char *
  219. NP_GetMIMEDescription(void) {
  220.     return "application/x-example-info:.exampleplugin:example.com";
  221. }
  222.  
  223. NPError OSCALL
  224. NP_GetValue(void *npp, NPPVariable variable, void *value) {
  225.     inst = (NPP) npp;
  226.     return plugin_getValue((NPP) npp, variable, value);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement