C++使用 cdb_read 在某些读取中返回额外的字符

发布于 2024-08-28 18:47:02 字数 1548 浏览 6 评论 0原文

我正在使用以下函数循环访问几个打开的 CDB 哈希表。有时,给定键的值会与附加字符(特别是 CTRL-P(DLE 字符/0x16/0o020))一起返回。

我已经使用几个不同的实用程序检查了 cdb 键/值对,但它们都没有显示附加到值的任何其他字符。

如果我使用 cdb_read() 或 cdb_getdata() (下面注释掉的代码),我会得到该字符。

如果我不得不猜测,我会说我为从 cdb 函数获取结果而创建的缓冲区做了一些错误。

非常感谢任何建议或帮助。

char* HashReducer::getValueFromDb(const string &id, vector <struct cdb *> &myHashFiles)
{

  unsigned char hex_value[BUFSIZ];
  size_t hex_len;

  //construct a real hex (not ascii-hex) value to use for database lookups
  atoh(id,hex_value,&hex_len);

  char *value = NULL;
  vector <struct cdb *>::iterator my_iter = myHashFiles.begin();
  vector <struct cdb *>::iterator my_end = myHashFiles.end();


  try
  {
    //while there are more databases to search and we have not found a match
    for(; my_iter != my_end && !value ; my_iter++)
    {
      //cerr << "\n looking for this MD5:" << id << " hex(" << hex_value << ") \n";
      if (cdb_find(*my_iter, hex_value, hex_len)){
          //cerr << "\n\nI found the key " << id << " and it is " << cdb_datalen(*my_iter) << " long\n\n";
          value = (char *)malloc(cdb_datalen(*my_iter));
          cdb_read(*my_iter,value,cdb_datalen(*my_iter),cdb_datapos(*my_iter));
          //value = (char *)cdb_getdata(*my_iter);
          //cerr << "\n\nThe value is:" << value << " len is:" << strlen(value)<< "\n\n";
        };

    }
  }
  catch (...){}
  return value;
}

I am using the following function to loop through a couple of open CDB hash tables. Sometimes the value for a given key is returned along with an additional character (specifically a CTRL-P (a DLE character/0x16/0o020)).

I have checked the cdb key/value pairs with a couple of different utilities and none of them show any additional characters appended to the values.

I get the character if I use cdb_read() or cdb_getdata() (the commented out code below).

If I had to guess I would say I am doing something wrong with the buffer I create to get the result from the cdb functions.

Any advice or assistance is greatly appreciated.

char* HashReducer::getValueFromDb(const string &id, vector <struct cdb *> &myHashFiles)
{

  unsigned char hex_value[BUFSIZ];
  size_t hex_len;

  //construct a real hex (not ascii-hex) value to use for database lookups
  atoh(id,hex_value,&hex_len);

  char *value = NULL;
  vector <struct cdb *>::iterator my_iter = myHashFiles.begin();
  vector <struct cdb *>::iterator my_end = myHashFiles.end();


  try
  {
    //while there are more databases to search and we have not found a match
    for(; my_iter != my_end && !value ; my_iter++)
    {
      //cerr << "\n looking for this MD5:" << id << " hex(" << hex_value << ") \n";
      if (cdb_find(*my_iter, hex_value, hex_len)){
          //cerr << "\n\nI found the key " << id << " and it is " << cdb_datalen(*my_iter) << " long\n\n";
          value = (char *)malloc(cdb_datalen(*my_iter));
          cdb_read(*my_iter,value,cdb_datalen(*my_iter),cdb_datapos(*my_iter));
          //value = (char *)cdb_getdata(*my_iter);
          //cerr << "\n\nThe value is:" << value << " len is:" << strlen(value)<< "\n\n";
        };

    }
  }
  catch (...){}
  return value;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳男子 2024-09-04 18:47:02

首先,我不熟悉 CDB,并且我不相信您在这里包含有关您的软件环境的足够详细信息。

但假设它就像我使用过的其他数据库库一样......

这些值可能不必以 NUL 结尾。这意味着转换为 char* 并打印它将不起作用。你应该自己添加一个0字节。

所以 malloc cdb_datalen + 1 并将最后一个字符设置为 0。然后打印它。

更好的是,使用 calloc 它会分配已经设置为零的内存。

First, I am not familiar with CDB and I don't believe you include enough details about your software environment here.

But assuming it is like other database libraries I've used...

The values probably don't have to be NUL-terminated. That means that casting to char* and printing it will not work. You should add a 0 byte yourself.

So malloc cdb_datalen + 1 and set the last character to 0. Then print it.

Better yet, use calloc and it will allocate memory already set to zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文