Google Chrome上的JavaScript和可脚本化NPAPI插件字符串数据 [英] JavaScript and Scriptable NPAPI plugin string data on Google Chrome

查看:222
本文介绍了Google Chrome上的JavaScript和可脚本化NPAPI插件字符串数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的脚本化npapi插件。在JavaScript和FireFox上的插件之间传递字符串效果很好。但是如果字符串包含连字符( - )符号,它会在Google Chrome上生成一些额外的随机字符。例如,在我的JavaScript代码中,我有

plugin.method(ab);



在我的npapi代码中,我有

  bool ScriptablePluginObject :: Invoke(NPIdentifier name,const NPVariant * args,uint32_t argCount,NPVariant * result){

char * outString = args [0] .value.stringValue.UTF8Characters;
char * npOutString =(char *)NPN_MemAlloc(strlen(outString)+ 1);
strcpy(npOutString,outString);
STRINGZ_TO_NPVARIANT(npOutString,* result);

返回true;
}

在Firefox上,它会在Chrome上返回ab,我会看到 ab * [ - ..带有一些额外的随机符号。我尝试将npplugin.dll放在Mozilla下的plugins目录下,或者使用Chrome扩展教程(http://code.google.com/chrome/extensions/npapi.html),这两种方式都给了我同样的奇怪行为。代码使用xulrunner-10.0.2.en-US.win32.sdk编译,使用xulrunner-1.9.0.17.en-US.win32.sdk也有同样的问题。



有没有人有任何线索?

解决方案

你的问题是你假设它是一个标准的NULL结尾的C串。实际上,NPAPI字符串通常是C字符串和NULL终止的,但不能保证这是事实。如果您查看 npruntime.h标头来自npapi-sdk项目的文件,你会看到有一个NPString的UTF8Length成员;这不仅仅是装饰。您应该始终使用它来确定UTF8字符串的长度(以字节为单位)。



要重申下面评论中所述的smorgan,这意味着您无法使用strcpy,strlen或任何其他C字符串函数来访问它;一个NPString是一个字节+长度对,所以你需要使用byte + length方法访问它。



例如:

  bool ScriptablePluginObject :: Invoke(NPIdentifier name,const NPVariant * args,uint32_t argCount,NPVariant * result){

char * outString = args [0 ] .value.stringValue.UTF8Characters;
int outLen = args [0] .value.stringValue.UTF8Length;
char * npOutString =(char *)NPN_MemAlloc(outLen + 1);
memcpy(npOutString,outString,outLen);
//将此NULL终止
npOutString [outLen] = NULL;
STRINGZ_TO_NPVARIANT(npOutString,* result);

返回true;

$ / code>

请注意,您还应该检查以确保NPVariant实际上是一个字符串或这可能都容易炸毁你的脸。


I created a simple scriptable npapi plugin. It works fine to pass string between JavaScript and plugin on FireFox. But it will generate some extra random characters on Google chrome if the string contains hyphen (-) symbol. For example, in my JavaScript code, I have

plugin.method("a-b");

on my npapi code, I have

bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) {

  char* outString = args[0].value.stringValue.UTF8Characters;
  char* npOutString = (char *)NPN_MemAlloc(strlen(outString) + 1);
  strcpy(npOutString, outString);
  STRINGZ_TO_NPVARIANT(npOutString, *result);

  return true;
}

On Firefox, it returns "a-b", on Chrome, I will see "a-b*[-.." with some extra random symbols. I tried with placing npplugin.dll in "plugins" directory under Mozilla or using Chrome extension tutorial(http://code.google.com/chrome/extensions/npapi.html), both way gave me the same strange behavior. The code is compiled with xulrunner-10.0.2.en-US.win32.sdk, using xulrunner-1.9.0.17.en-US.win32.sdk also has the same problem.

Does anyone have any clues?

解决方案

Your problem is that you are assuming that it is a standard NULL terminated C string. In practice, NPAPI strings often are C strings and NULL terminated but there is no guarantee that this will be the case. If you look at the npruntime.h header file from the npapi-sdk project you'll see that there is a UTF8Length member of NPString; this isn't just for decoration. You should always be using that to determine the length (in bytes) of the UTF8 string.

To reiterate what smorgan said in the comment below, this means that you cannot use strcpy, strlen, or any other C string function for accessing it; an NPString is a byte+length pair, so you need to access it using byte+length methods.

For example:

bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) {

  char* outString = args[0].value.stringValue.UTF8Characters;
  int outLen = args[0].value.stringValue.UTF8Length;
  char* npOutString = (char *)NPN_MemAlloc(outLen + 1);
  memcpy(npOutString, outString, outLen);
  // Make this NULL terminated
  npOutString[outLen] = NULL;
  STRINGZ_TO_NPVARIANT(npOutString, *result);

  return true;
}

Note that you should also be checking to make sure that the NPVariant is actually a string or this could all easily blow up in your face.

这篇关于Google Chrome上的JavaScript和可脚本化NPAPI插件字符串数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆