如何将数据从内核写入二进制文件

时间:2022-12-20 19:38:10

I used an API to get data from kernel which API provides part info as follows.

我使用API​​从内核获取数据,API提供如下部分信息。

#API INPUT:
#      size = 0x100 (User/driver will need to allocate buf of size*4,
             #                     since driver dump data in %02x format)

# API OUTPUT:
             #      buf = data copied to this buffer
             #      size = 0x100 * 4
             #      rv = return value.      None zero indicates error

Then i want to save these data from kernel into a binary file.

然后我想将这些数据从内核保存到二进制文件中。

char *buffer = malloc(0x100 *4);
FILE *fd;
fd = fopen("binfile", "wb");
...
fwrite(buffer, 1, 0x100 *4, fd);
...

But i found the content in this binfile are text. I can use 'cat binfile' to see the content and output as follows.

但是我发现这个binfile中的内容是文本。我可以使用'cat binfile'来查看内容和输出如下。

03 04 07 10 00 00 00 00 00 00 00 06 67 00 00 00 08 03 00 1e 46 49 4e 49 53 41 52 20 43 4f 52 50 2e 20 20 20 00 00 90 65 46 54 4c 58 38 35 37 31 44 33 42 4e 4c 2d 45 35 42 20 20 20 03 52 00 9b 00 1a 00 00 41 4d 51 30 43 36 31 20 20 20 20 20 20 20 20 20 31 32 30 36 31 34 20 20 68 f0 03 bc 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

And use 'hexdump binfile', output is as follows.

并使用'hexdump binfile',输出如下。

0000000 3330 3020 2034 3730 3120 2030 3030 3020
0000010 2030 3030 3020 2030 3030 3020 2030 3030
0000020 3020 2036 3736 3020 2030 3030 3020 2030
0000030 3830 3020 2033 3030 3120 2065 3634 3420
0000040 2039 6534 3420 2039 3335 3420 2031 3235
0000050 3220 2030 3334 3420 2066 3235 3520 2030
0000060 6532 3220 2030 3032 3220 2030 3030 3020
0000070 2030 3039 3620 2035 3634 3520 2034 6334
0000080 3520 2038 3833 3320 2035 3733 3320 2031
0000090 3434 3320 2033 3234 3420 2065 6334 3220
00000a0 2064 3534 3320 2035 3234 3220 2030 3032
00000b0 3220 2030 3330 3520 2032 3030 3920 2062
00000c0 3030 3120 2061 3030 3020 2030 3134 3420
00000d0 2064 3135 3320 2030 3334 3320 2036 3133
00000e0 3220 2030 3032 3220 2030 3032 3220 2030
00000f0 3032 3220 2030 3032 3220 2030 3133 3320
...
00002c0 3020 2030 3030 3020 2030 3030 3020 2030
00002d0 3030 3020 2030 3030 3020 2030 3030 3020
00002e0 2030 3030 3020 2030 3030 3020 2030 3030
00002f0 3020 2030 3030 3020 2030 3030 3020 2030
0000300 0000 0000 0000 0000 0000 0000 0000 0000
*
0000400 >>> it is 0x400

But my expected content of binary file should be as follows.

但我预期的二进制文件内容应如下所示。

0000000 0403 0007 0000 4000 0440 0670 0a8c 0000
0000010 0003 0a00 4946 494e 4153 2052 4f43 5052
0000020 202e 2020 0000 6590 5446 464c 3538 3932
0000030 3350 4e42 2d56 3545 3141 2020 5203 a900
0000040 3a00 0000 5155 3036 3030 2048 2020 2020
0000050 2020 2020 3331 3830 3430 2020 fa68 e505
0000060 0000 0000 0000 0000 0000 0000 0000 0000
*
0000100 >>> only 0x100

1 个解决方案

#1


0  

Note the following from the comment on your API:

请注意您对API的评论中的以下内容:

(User/driver will need to allocate buf of size*4, since driver dump data in %02x format)

(用户/驱动程序需要分配大小为* 4的buf,因为驱动程序转储数据为%02x格式)

You're not accidentally converting it to text; that's how the API gives it to you. You need to convert it to binary yourself. Once you do that, you can just write it the way you are now and it will stay binary.

你不小心将它转换为文本;这就是API给你的方式。您需要自己将其转换为二进制。一旦你这样做,你可以按照现在的方式编写它,它将保持二进制。

#1


0  

Note the following from the comment on your API:

请注意您对API的评论中的以下内容:

(User/driver will need to allocate buf of size*4, since driver dump data in %02x format)

(用户/驱动程序需要分配大小为* 4的buf,因为驱动程序转储数据为%02x格式)

You're not accidentally converting it to text; that's how the API gives it to you. You need to convert it to binary yourself. Once you do that, you can just write it the way you are now and it will stay binary.

你不小心将它转换为文本;这就是API给你的方式。您需要自己将其转换为二进制。一旦你这样做,你可以按照现在的方式编写它,它将保持二进制。