小编Aus*_*tin的帖子

将 Lua 字符串的前两个字节(bigendian 格式)转换为无符号短数字

我想要一个带有字符串参数的 lua 函数。字符串有 N+2 字节的数据。前两个字节具有双端格式的长度,其余 N 个字节包含数据。

假设数据是“abcd”
,那么字符串是 0x00 0x04 abcd

在 Lua 函数中,这个字符串是我的输入参数。我怎样才能计算长度最佳方式。

到目前为止我已经尝试过下面的代码

function calculate_length(s)
    len = string.len(s)
    if(len >= 2) then
        first_byte = s:byte(1);
        second_byte = s:byte(2);
        //len = ((first_byte & 0xFF) << 8) or (second_byte & 0xFF)
        len = second_byte
    else
        len = 0
    end
    return len
end
Run Code Online (Sandbox Code Playgroud)

请参阅注释行(我在 C 中会如何做)。

在Lua中我如何实现注释行。

lua

2
推荐指数
1
解决办法
1847
查看次数

在C语言中,如何调用我的函数而不是系统库函数

平台-Linux

我有一个典型的要求,即我需要给我的功能比系统提供的功能更高的优先级。

可以说我有第三方库或可执行文件。它调用了许多系统函数,例如strcpy或strlen等。由于无法访问代码,因此无法修改可执行文件/库。

现在,我已经实现了自己的strlen或strcpy函数。当第三方库或可执行文件正在执行时,应调用我的函数而不是系统库函数。

可能吗。如果是,可以有人指导我如何执行此操作。

LD_PRELOAD试用结果

根据这里的建议,我尝试使用LD_PRELOAD。我试图覆盖malloc函数。

    #include <string.h>

    void *malloc(int size)
    {
        void *ptr = NULL;
        printf("Inside my malloc function\n");
        ptr = malloc(size);
        return ptr;
    }
Run Code Online (Sandbox Code Playgroud)

我将上面的代码放在ac文件中,并构建了mem.so文件,并在命令下面执行。

导出LD_PRELOAD =。/ mem.so

所以现在我已经覆盖了malloc函数。

如果执行lsclear命令,我将得到以下结果,这是合乎逻辑的。

Inside my malloc function
Inside my malloc function
Inside my malloc function
Inside my malloc function
Inside my malloc function
Run Code Online (Sandbox Code Playgroud)

看起来我的重载malloc函数是递归调用的。我只需要打一次电话。我该如何实现。

c linux

1
推荐指数
1
解决办法
670
查看次数

标签 统计

c ×1

linux ×1

lua ×1