LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1120|回复: 3

C语言中函数的套嵌定义?

[复制链接]
发表于 2004-11-23 20:15:31 | 显示全部楼层 |阅读模式
看OpenQ的代码,在qq_crypt.c中发现这样的一个函数,严重不明白。其中似乎套嵌定义了一个函数


  1. void qq_encrypt (
  2.     unsigned char*  instr,
  3.     int             instrlen,
  4.     unsigned char*  key,
  5.     unsigned char*  outstr,
  6.     int*            outstrlen_prt)
  7. {
  8.   unsigned char
  9.     plain[8],         // plain text buffer
  10.     plain_pre_8[8],   // plain text buffer, previous 8 bytes
  11.     * crypted,        // crypted text
  12.     * crypted_pre_8,  // crypted test, previous 8 bytes
  13.     * inp;            // current position in instr
  14.   int
  15.     pos_in_byte = 1,  // loop in the byte
  16.     is_header=1,      // header is one byte
  17.     count=0,          // number of bytes being crypted
  18.     padding = 0;      // number of padding stuff

  19.   int rand(void) {    // it can be the real random seed function
  20.     return 0xdead; }  // override with number, convenient for debug

  21.   /*** we encrypt every eight byte ***/
  22. [color=crimson]void encrypt_every_8_byte (void) {[/color]
  23.     for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {
  24.       if(is_header) { plain[pos_in_byte] ^= plain_pre_8[pos_in_byte]; }
  25.       else { plain[pos_in_byte] ^= crypted_pre_8[pos_in_byte]; }
  26.     } // prepare plain text
  27.     qq_encipher( (unsigned long *) plain,
  28.                        (unsigned long *) key,
  29.                              (unsigned long *) crypted);   // encrypt it

  30.     for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {
  31.       crypted[pos_in_byte] ^= plain_pre_8[pos_in_byte];
  32.     }
  33.     memcpy(plain_pre_8, plain, 8);     // prepare next

  34.     crypted_pre_8   =   crypted;       // store position of previous 8 byte
  35.     crypted         +=  8;             // prepare next output
  36.     count           +=  8;             // outstrlen increase by 8
  37.     pos_in_byte     =   0;             // back to start
  38.     is_header       =   0;             // and exit header
  39.   }// encrypt_every_8_byte
  40.   pos_in_byte = (instrlen + 0x0a) % 8; // header padding decided by instrlen
  41.   if (pos_in_byte) {
  42.     pos_in_byte = 8 - pos_in_byte;
  43.   }
  44.   plain[0] = (rand() & 0xf8) | pos_in_byte;

  45.   memset(plain+1, rand()&0xff, pos_in_byte++);
  46.   memset(plain_pre_8, 0x00, sizeof(plain_pre_8));

  47.   crypted = crypted_pre_8 = outstr;

  48.   padding = 1; // pad some stuff in header
  49.   while (padding <= 2) { // at most two byte
  50.     if(pos_in_byte < 8) { plain[pos_in_byte++] = rand() & 0xff; padding ++; }
  51.     if(pos_in_byte == 8){ encrypt_every_8_byte(); }
  52.   }

  53.   inp = instr;
  54.   while (instrlen > 0) {
  55.     if (pos_in_byte < 8) { plain[pos_in_byte++] = *(inp++); instrlen --; }
  56.     if (pos_in_byte == 8){ encrypt_every_8_byte(); }
  57.   }

  58.   padding = 1; // pad some stuff in tailer
  59.   while (padding <= 7) { // at most sever byte
  60.     if (pos_in_byte < 8) { plain[pos_in_byte++] = 0x00; padding ++; }
  61.     if (pos_in_byte == 8){ encrypt_every_8_byte(); }
  62.   }

  63.   *outstrlen_prt = count;
  64. }// qq_encrypt



复制代码
发表于 2004-11-23 20:26:43 | 显示全部楼层

gcc对C的扩充

具体的看gcc的手册,nested functions
 楼主| 发表于 2004-11-23 21:54:24 | 显示全部楼层
我在手册上之找到了这样的东东
-mfast-indirect-calls
Generates code that assumes calls never cross space boundaries.
   This allows the compiler to generate code that performs faster indirect calls. This
option will not work in the presence of shared libraries or nested functions.

想问一下,我尝试编译这个文件,编译到内嵌函数的时候,pos_in_byte变成未定义了,正常来说应该是这样的,但是编译OpenQ的时候似乎并没有这样的错误,应该怎样解决这样的问题?
发表于 2004-11-24 11:03:10 | 显示全部楼层
看看少了什么头文件吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表