bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

[原] ftp4j的解析list的bug及解决

Posted by bianbian on 2008-06-19 08:08


本文Tags: , , ,

上次推荐的ftp4j在解析部分FTP站点的目录list的时候遇到了FTPListParseException(也怪FTP协议没有对LIST格式作出标准)。查看源码发现,主要是两个问题:
1)文件权限不只rwx这三个,附加了s、t(详见http://en.wikipedia.org/wiki/File_system_permissions
2)部分ftpd似乎直接调用的“ls -l”输出目录,第一行是“total xxx”
给作者写信了,说不定下个版本就有Sepcial Thanks to bianbian 了。嘿嘿嘿嘿。。。
修正后的代码(省略后面没有变化的部分):
Read the rest of this entry »

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Java, Technology | 4 Comments »

[原] php读取二进制流(C语言结构体struct数据文件)

Posted by bianbian on 2008-04-20 01:08


本文Tags: , , , , , , , ,

尽管php是用C语言开发的,不过令我不解的是php没有提供对结构体struct的直接支持。
不过php提供了pack和unpack函数,用来进行二进制数据(binary data)和php内部数据的互转:

  1. string pack ( string $format [, mixed $args [, mixed $...]] )
  2. //Pack given arguments into binary string according to format.
  3.  
  4. array unpack ( string $format, string $data )
  5. //Unpacks from a binary string into an array according to the given format.

其中,$format跟perl里的pack格式类似,有如下一些(中文是我加的,有不准确的欢迎提出):
a NUL-padded string,即“\0”作为“空字符”的表示形式
A SPACE-padded string,空格作为“空字符”的表示形式
h Hex string, low nibble first,升序位顺序
H Hex string, high nibble first,降序位顺序
c signed char,有符号单字节
C unsigned char,无符号单字节
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x NUL byte,实际使用的时候作为跳过多少字节用,很有用
X Back up one byte,后退1字节
@ NUL-fill to absolute position,实际使用的时候作为从开头跳到某字节用,很有用

实际使用发现:C里的“\0”(即字符串终止符)在php里并不是终止符,而是作为了字符串的一部分。因此,必须对“\0”进行特殊处理,才能进行struct和php内部数据的完美互转。比如 char name[10]; 如果实际数据是“62 69 61 6E 00 62 69 61 6E 00”,在C语言里第5个位置有终止符,name应该是“bian”;而用了unpack转换以后在php里的name却是“bian\0bian\0”。
一开始我用了strpos函数找到“\0”的位置,然后进行substr截取:

  1. $name = substr($name, 0, strpos($name, "\0"));

不过很Faint的事情发生了,不知道是strpos的bug还是substr的bug(其实测试一下就知道,懒得试),有些字符串没问题,有些字符串却只能得到空值(即$name == ”)。很是郁闷,后来找了个strtok函数,这下没有问题了:

  1. $name = strtok($name, "\0");

难为大家看了那么多,下面写个完整的php读取二进制数据流(C语言结构体struct数据)文件的示例代码:
首先是C的struct定义示例,为了演示,我就写个简单点的,实际对照上面那个$format格式表应该没有问题:

  1. struct BIANBIAN {
  2.     char name[10];
  3.     char pass[33];
  4.     int  age;
  5.     unsigned char flag;
  6. };

比如有个“bianbian.org”文件,内容就是上面的N个BIANBIAN结构体构成的。读取的php代码:

  1. //下面根据struct确定$format,注意int类型跟机器环境有关,我的32位Linux是4个长度
  2. $format = 'a10name/a33pass/iage/Cflag';
  3. //确定一个struct占用多少长度字节,如果只是读取单个结构体这是不需要的
  4. $length = 10 + 33 + 4 + 1;
  5. //也可以用fopen + fread + fclose,不过file_get_contents因为可以mmap,效率更高
  6. $data = file_get_contents('bianbian.org', 'r');
  7. for ($i = 0, $c = strlen($data); $i < $c; $i += $length) {
  8.     $bianbian = unpack("@$i/$format", $data);
  9.     //reference传递是php 5才支持的,如果用php4,得用其他办法
  10.     foreach ($bianbian as &$value) {
  11.         if (is_string($value)) {
  12.             $value = strtok($value, "\0");
  13.         }
  14.     }
  15.     print_r($bianbian);
  16. }
  17. //输出为array,即类似:
  18. Array
  19. (
  20.     [name] => 'bianbian'
  21.     [pass] => 'bianbian.org'
  22.     [age]  => 100
  23.     [flag] => 0
  24. )
  25. ...

pack应该跟unpack相反。

标签: , , , , , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Technology, php | 4 Comments »

[原] IDL剪贴板绘图在其他语言调用下失效的bug

Posted by bianbian on 2008-03-26 02:32


本文Tags: , , ,

经过运行剪贴板监视器程序,我确认这是IDL的bug:如果用VC等其他语言调用IDL绘图,IDL并不能通过IDLgrClipboard对象将绘图结果保存到系统剪贴板(而在IDL的编辑器环境下运行,剪贴板是能得到绘图结果的)。
下面是英文描述:
IDLgrClipboard not works in callable mode (I use VC++ to execute IDL procedure to draw some plots, and copy the result to system clipboard). After running a system-clipboard-viewer-tool, I conclude it must be a bug (I tested with IDL6.2, IDL7.0).

oClipbrd = OBJ_NEW(’IDLgrClipboard’, DIMENSIONS=windowSize)
oClipbrd->Draw, oView

It works well under IDL-workbench, but when call from VC++, everything else goes OK except clipboard is empty.

解决办法:只好制定FILENAME参数,将结果保存到文件:
Finally, I have no choise but to save the drawing to a file, and it works:

oClipbrd->Draw, oView, FILENAME=’tempout.bmp’

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, ENVI & IDL, Technology | No Comments »

[原] 也许是prototype框架的bug

Posted by bianbian on 2008-03-08 08:03


本文Tags: , , , ,

不知道怎么给prototype提交bug report,就写在这里吧:
其实我没有测试过,只是看prototype源码的时候,觉得是个bug:
对String的原型(prototype)添加toJSON()函数只是转义了引号,未转义”\”:

  1. if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
  2. return "'" + escapedString.replace(/'/g, '\\\'') + "'";

如果用户在input里输入\\,转成string应该是”\\\\”,如果没有转义,再写回去的时候就丢了一个\
应该在转义引号前先转义\:

  1. .replace(/\\/g, '\\\\').replace(/"/g, '\\"')
标签: , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in JavaScript, Technology | No Comments »

[晕] 仙剑4 bug,走到这个点绝对走不出来了

Posted by bianbian on 2008-02-27 04:54


本文Tags: , ,

被翅膀跟尾巴挡住,绝对挂了:
pal4.jpg
要是存档在一万年前,你就哭吧。。。

标签: , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Entertainment, Not IT | 4 Comments »

[晕] 发现gmail的问题

Posted by bianbian on 2008-02-20 04:47


本文Tags: ,

附件里不能发可执行文件。就算用tgz压缩过的,里面有x属性的还是不行。
说什么有可能危害安全。。。。结果发个已编译的开源项目文件也不行。。。
话说,我发什么关你啥事啊?你不是有病毒扫描吗?

标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Technology | No Comments »

[原] PHP bug or Slackware bug? –enable-fastcgi always failed.

Posted by bianbian on 2007-12-29 04:07


本文Tags: , , ,

On Slackware 12.0, compile php to fastcgi alwayes failed, only get CGI:

./configure --enable-fastcgi --enable-force-cgi-redirect

./php --version
PHP x.x.x (cgi)
....

so use spawn-fcgi (lighttpd) always get exit.

OH MY GOD…. I tried from php 4.3.0 - php 5.2.0, only php 4.4.4 can get fastcgi:

./php --version
PHP 4.4.4 (cgi-fcgi) (built: Dec 29 2007 03:55:36)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

why??????????????????????

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Technology, php | No Comments »

[晕] nginx的internal属性的bug

Posted by bianbian on 2007-10-16 02:17


本文Tags: , ,

文档上说,internal用来禁止用户直接通过地址访问。
syntax: internal
default: no
context: location

internal indicates that the matching location can be used only for so called “internal” requests.
For external requests it will return the error “Not found” (404).
Internal requests are the following:
1) requests redirected by the instruction error_page
2) subrequests created by the command include virtual of the “ngx_http_ssi_module” module
3) requests changed by the instruction rewrite of the “ngx_http_rewrite_module” module

An example to prevent clients fetching error pages directly:

  1. error_page   404   /404.html;
  2. location  /404.html {
  3.     internal;
  4. }

实际发现,rewrite的用internal修饰后就挂了。像下面这个配置,如果/realpath/设了internal,用/proxypath/访问就是404。
这个跟本意是违背的(本意只能通过/proxypath/访问,而隐藏/realpath/),应该是nginx的bug(我的版本0.5.32)

  1. location /proxypath/ {
  2.    root     /realpathroot;
  3.    rewrite ^/proxypath/(.+)$  /realpath/$1? last;
  4. }
  5. location /realpath/ {
  6.   internal;
  7. }
标签: , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Linux, Technology | No Comments »

[呜]招行的系统咋那么多问题,这次是ATM存款

Posted by bianbian on 2007-05-13 09:13


本文Tags: ,

可能确实是本命年倒霉,存进去的5K人间蒸发了。。。。
当时也没查余额,凭条也顺手丢了。。。。
晚上打开专业版傻眼了,啥都没有。。。。
95555电话询问,他们似乎见惯了,说明天去柜台登记,查一下ATM的日志就会补给我。

但是。。。。俺对招行的信任正一步步丧失。。。。除了服务态度好,能不能让我放心啊?

标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Uncategorized | 2 Comments »

[原]windows命令Copy合并多文件的bug及解决

Posted by bianbian on 2006-10-25 03:03


本文Tags: , , ,

先来看看copy的语法:

C:\Documents and Settings\Administrator>copy /?
将一份或多份文件复制到另一个位置。

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ …]] [destination [/A | /B]]

source 指定要复制的文件。
/A 表示一个 ASCII 文本文件。
/B 表示一个二进位文件。
/D 允许解密要创建的目标文件
destination 为新文件指定目录和/或文件名。
/V 验证新文件写入是否正确。
/N 复制带有非 8dot3 名称的文件时,
尽可能使用短文件名。
/Y 不使用确认是否要改写现有目标文件
的提示。
/-Y 使用确认是否要改写现有目标文件
的提示。
/Z 用可重新启动模式复制已联网的文件。

命令行开关 /Y 可以在 COPYCMD 环境变量中预先设定。
这可能会被命令行上的 /-Y 替代。除非 COPY
命令是在一个批文件脚本中执行的,默认值应为
在改写时进行提示。

要附加文件,请为目标指定一个文件,为源指定
数个文件(用通配符或 file1+file2+file3 格式)。

其中,file1+file2+file3还是挺好用的,我一般拿来合并JS脚本,比如:
copy bianbian.main.js + bianbian.DOM.js + bianbian.other.js bianbian.src.js /Y
jsmin <bianbian.src.js >bianbian.js
(用jsmin来删除注释缩进等等,让代码小一点)。

不过,现在发现多文件合并有bug:
1)合并以后在文件末尾会产生一个奇怪的字符(十六进制ASCII码为”1A”),如果直接引用这个js,则任何浏览器都会报错。不过这个字符用jsmin了以后就会被删除,所以这个bug倒并不要紧

2)如果这些文件格式是UTF-8的,合并的时候不会删除每个文件的UTF-8头声明,导致解析出错。我们知道UTF-8格式的文件会用“FF FE”开头(可以看做UTF-8声明),而copy只是简单地将所有文件的ASCII码合并,所以合并后的文件正文也会有“FF FE”。这样,会导致每个源文件第一行的脚本解析出错(比如function,会解析成 ?unction,此处问号表示不认识的字符。不过这个解析目前只在Opera下发现出错)
。解决的办法是每个源文件的第一行都留空,不要写脚本。

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Technology, Windows | 1 Comment »

[原] IE在UTF-8下解析JavaScript的bug

Posted by bianbian on 2006-10-20 09:57


本文Tags: , , ,

莫名其妙的脚本问题,脚本肯定没有写错,在IE中就是报错,在Firefox中一切正常:页面是UTF-8的,脚本用了插入页面。
(略去若干抓狂文字。。。。。。)
bianbian.js是ANSI编码的,用记事本保存为UTF-8编码,错误没有了。我觉得是IE的bug:页面是UTF-8编码的,为什么一定要求引用的其他文件也是UTF-8?
下次遇到IE下脚本很奇怪的错误不如看看JavaScript脚本和页面的编码是不是一致。

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in JavaScript, Technology | No Comments »

[原]解决qmail+iGenus webmail中文UTF-8乱码

Posted by bianbian on 2006-10-11 04:23


本文Tags: , , , ,

最近帮忙解决了一个问题(下面的全凭回忆输入,可能拼写上有问题):很古老的qmail+iGenus webmail系统,邮件若是UTF-8编码则显示乱码(缺省编码为GB2312)。主要解决办法就是利用iconv进行转码:
看看有没有iconv模块,如果没有,需要重新编译php(如果iconv系统里没有装,得先安装iconv:去 http://www.gnu.org/software/libiconv/ 下载,./configure,make,make install),编译php的时候加上 –with-iconv
如果php是静态模块方式和apache绑定的,还得把apache重新编译一下:./configure –activate-module=src/modules/php4/libphp4.a,make clean,make,make install

然后打开iGenus include目录下的Fun_inc.php,找到Decode_mime()函数,在B和Q解码以后加上

  1. if(strtolower($Charset)=='utf-8') $Text = iconv('UTF-8', 'GB2312', $Text);

还有一个地方是正文的编码,打开include目录下的Prev_inc.php,找到Decode_text()函数,为了防止出现错误,复制一下Decode_text(),比如Decode_text_my(),并增加一个参数:$Charset。在函数内解析完数据准备写到文件之前转码:

  1. if(strtolower($Charset)=='utf-8') $buff = iconv('UTF-8', 'GB2312', $buff);

打开iGenus主目录下的prev.php,找到调用Decode_text()的两个地方,改成Decode_text_my(),并增加传递一个参数:$Charset

总结:我看的那个iGenus版本php写得很是糟糕,在那个上面改东西有一种想死的冲动……

标签: , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Technology, php | 3 Comments »

[原] 解决UltraEdit在UTF-8编码上的bug

Posted by bianbian on 2006-09-06 04:50


本文Tags: , , , ,

我一直喜欢用UltraEdit,包括写JavaScript、HTML、python、C、JSP等等。不过UltraEdit在UTF-8的处理上有个奇怪的bug。不信你可以试一下:在记事本里输入

  1. <%@page language="java" contentType="text/html;charset=UTF-8">
  2. 测试一下中文,English,呵呵
  3. welcome to <a href="http://bianbian.org">http://bianbian.org</a>

保存,比如test.jsp。上面是很正常的一个JSP文件,现在用UltraEdit打开,你发现什么了?
我试了各种各样版本的UltraEdit,中文都是乱码。这是怎么回事呢?
后来我想了一下,用记事本保存的文件默认是ANSI格式的,也就是我们的test.jsp实际并不是UTF-8格式的;而第一行的“charset=UTF-8”只是告诉java我们输出的是UTF-8格式。可怜的UltraEdit看了这行字以为文件也是UTF-8格式的,所以显示都是乱码。我觉得很搞笑,文件格式当然要根据实际文件存储格式来确定,怎么可以反过来呢?所以我觉得是UltraEdit的bug,并写信给UltraEdit。他们的效率真高,我第二天早上就收到回信了,并解决了我这个问题(虽然我觉得他们不默认提供这个设置真是搞笑)。冲着他们的效率,我决定以后买个正版。。。。嗯,跑题了

解决办法就是打开UltraEdit安装路径下的Uedit32.ini(如果没有这个文件,那说明你的UltraEdit版本的ini不是放在安装路径下的,得去C:\Documents and Settings\(登录用户名,默认是Administrator)\Application Data\IDMComp\UltraEdit里面找一下),在[Settings]里加上一句“Detect UTF-8 String=0”即可(bianbian补充:在UltraEdit某版本之后,这个字符串改成了“Auto Detect UTF-8 String=0”;你可以两个都试一下,或者都填上去),意思是禁止UltraEdit检测可能标记UTF-8的字符串,这个选项在“配置”里是没有的(不然我也不会去找他们了,呵呵)。我也建议他们以后在配置里加上这个选项,他们说会考虑,不知道现在的版本里是否已经有了,呵呵。附信件:

Dear Yuelinniao,

Thanks for your message. If you don’t want the charset declaration to be
used to determine whether or not a file is in UTF-8 format there is a
setting you can add to your uedit32.ini file to help with this. If you
open the uedit32.ini file you may add this under the [Settings] section:

Detect UTF-8 String = 0

This will prevent the referenced line from determining if a file is
recognized as being in UTF-8 format. I believe this will help.

Thanks, Troy

bianbian wrote:

> Dear support,
>
> I write to report a bug, which is making me inconvenient.
>
> When I edit a jsp file, first line is like this:
> <%@page language="java" contentType="text/html;charset=UTF-8">
> ~~~~~~
> but the file is saved on disk in ANSI format. Ultraedit maybe thinks
> the file is in UTF-8 format, and all the Chinese words of the file is
> in a state of disorder.
>
> When I take the property “Auto recognize UTF-8 format” off, the file
> is showed correctly. But when I edit another really UTF-8 format file(
> the file
> saved on disk in UTF-8 format), it is wrong again.
>
> So maybe there is a bug when Ultraedit recognizing UTF-8 format (not
> depending on the really format saved on disk)?
>
> Sorry for my poor English.
>
> Best wishes.
> Yours,
> yuelinniao

标签: , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Technology, Windows | 14 Comments »