bianbian coding life

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

[原]涂鸦板oekakibbs和Shi-PainterPRO数据格式分析

Posted by bianbian on 2006-12-19 06:33


本文Tags: ,

去年用C语言实现的涂鸦板:http://bbs.nju.edu.cn/pntmain
想当年,一夜没睡,不停地debug、截获网络数据,分析涂鸦板oekakibbs和Shi-PainterPRO的数据格式。
最后保存成功带给我的喜悦我是终身难忘。这也是我喜欢编程的部分原因吧。呵呵
oekakibbs和Shi-PainterPRO是比较有名的涂鸦板程序(实际是Java Applet程序),通过http协议和自定义的数据格式往后台提交数据。解析数据网络上比较多的是php和asp的代码,但是因为和彼此自己的系统结合,所以看起来比较费尽。不过从来没有数据格式说明文档。
今天偶然又翻了一下这些代码,顺便在blog里记一下。要是能给其他人省点力气,也算功德一件。呵呵

  1. /*
  2. 解析涂鸦板传过来的数据并保存图片
  3. Net@Lilybbs, 2005-11-14 7:18
  4. /////////////////////////////
  5. 参数传递:pntupload?临时key_工具名
  6. ====================================
  7. oekakibbs数据格式:
  8. -------------------------------
  9. ...Content-type:string
  10.  
  11. Content-type:string
  12.  
  13. Content-type:image/0
  14.  
  15. {PNG图片数据}
  16. Content-type:animation/
  17.  
  18. {oekakibbs动画数据}
  19. ====================================
  20. Shi-PainterPRO数据格式:
  21. -------------------------------
  22. S0000000000{当前PNG图片的字节数}
  23. {PNG图片数据}{pch长度}{pch文件内容}
  24. -------------------------------
  25. //以下是pch文件内容:
  26. layer_count=3
  27. layer_max=35
  28. layer_last=35
  29. image_width=300
  30. image_height=300
  31. count_lines=25
  32. quality=1
  33.  
  34. {其他信息}
  35. -------------------------------
  36. */
  37. #include "www.h"
  38.  
  39. int pntupload_main()
  40. {
  41.     int n, i, j, key=0;
  42.     char tmp[80], *buf, tool[30], *p;
  43.     FILE *fp;
  44.  
  45.     chdir(BBSHOME);
  46.     http_header(200);
  47.     printf("Content-type: text/html; charset=gb2312\n\n");
  48.     n = atoi(getsenv("CONTENT_LENGTH"));
  49.     if (n<0 || n>3000000)
  50.         http_fatal("too big");
  51.     buf = malloc(n);
  52.     if(buf==0) http_fatal("out of memory");
  53.     fread(buf, 1, n, stdin); //读入buf
  54. /*
  55.     fp = fopen("paint/data", "w");
  56.     fwrite(buf, 1, n, fp);
  57.     fclose(fp);
  58. */
  59.     //-------- 获得_U_KEY
  60.     key = atoi(getsenv("QUERY_STRING"));
  61.     //-------- 获得tool
  62.     p = strchr(getsenv("QUERY_STRING"), '_');
  63.     strsncpy(tool, p+1, 20);
  64.  
  65.     //-------- 根据不同tool, 不同处理数据
  66.     //oekakibbs和oekakibbs2的处理是一样的
  67.     if(!strncmp("oekakibbs", tool, 9))
  68.     {
  69.         //-------- 获得图片
  70.         i = memstr(buf, n, "Content-type:image/0");
  71.         j = memstr(buf+i+24, n, "Content-type:animation/"); //24是"Content-type:image/0\r\n\r\n"的长度
  72.         sprintf(tmp, "paint/tmp/%d.png", key);
  73.         fp = fopen(tmp, "w");
  74.         fwrite(buf + i + 24, 1, j, fp); //j实际是png的长度
  75.         fclose(fp);
  76.         //-------- 获得动画
  77.         j += i+24; //j现在是从buf开始指向Content-type:animation的偏移量
  78.         sprintf(tmp, "paint/tmp/%d.oeb", key);
  79.         fp = fopen(tmp, "w");
  80.         fwrite(buf+j+27, 1, n - j - 27, fp); //p2-buf是长度,27是"Content-type:animation/\r\n\r\n"的长度
  81.         fclose(fp);
  82.         //需要输出success告诉applet保存成功
  83.         printf("success");
  84.     }
  85.     else if(!strcmp("Shi-Painter", tool))
  86.     {
  87.         //png 头:HEX 89 50 4E 47 0D 0A
  88.         //png 尾:HEX 49 45 4E 44 AE 42 60 82
  89.         //unsigned char pngend[] = {0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
  90.         i = memstr(buf, n, "\r\n"); //第2行开始就是PNG图片
  91.         j = atoi(buf+1); //buf[0]固定是S,之后就是PNG的长度
  92.         sprintf(tmp, "paint/tmp/%d.png", key);
  93.         fp = fopen(tmp, "w");
  94.         fwrite(buf+i+2, 1, j, fp);
  95.         fclose(fp);
  96.         //-------- 获得动画
  97.         j += i+2; //j现在是从buf开始指向PNG末尾的偏移量
  98.         i = memstr(buf+j, n, "layer_count="); //这里的i就相当于oekakibbs里面的27:就是PNG末尾到layer_count的长度
  99.         sprintf(tmp, "paint/tmp/%d.oeb", key);
  100.         fp = fopen(tmp, "w");
  101.         fwrite(buf+j+i, 1, n-j-i, fp);
  102.         fclose(fp);
  103.     }
  104.     exit(0);
  105. }
标签: ,

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

相关日志

Posted in C/C++, Technology | No Comments »