javadistoglib.c

来自「SRI international 发布的OAA框架软件」· C语言 代码 · 共 115 行

C
115
字号
#include <stdio.h>
#include <string.h>
#include "javadistoglib.h"

/**
 * Read a single Java character from the buffer that starts at buf and ends
 * at end chars away from buf.  *theChar will contain the character.  This
 * assumes ASCII chars.
 */
size_t javadistoglib_readJavaChar(char* buf, size_t end, gchar* theChar)
{
  if(end < 2) {
    return 0;
  }
  memcpy(theChar, buf + 1, sizeof(char));
  return 2;
}

/**
 * Read numToRead characters from the buffer that starts at buf and ends at end.
 */
size_t javadistoglib_readJavaChars(char* buf, size_t end, size_t numToRead, size_t bufSize, gchar* theCharString)
{
  size_t numRead = 0;
  size_t i = 0;

  if(end < (numToRead * 2)) {
    return 0;
  }
  if(bufSize < (numToRead + 1)) {
    fprintf(stderr, "javadistoglib_readJavaChars bufSize is too small\n");
    return 0;
  }
  for(i = 0; i < numToRead; ++i) {
    numRead += javadistoglib_readJavaChar(buf + numRead, end, &(theCharString[i]));
  }
  theCharString[numToRead] = '\0';
  return numRead;
}

size_t javadistoglib_readJavaBytes(char* buf, size_t end, size_t numToRead, size_t bufSize, gchar* bytes)
{
  if(end < numToRead) {
    return 0;
  }
  if(bufSize < numToRead) {
    fprintf(stderr, "javadistoglib_readJavaBytes bufSize is too small\n");
    return 0;
  }
  memcpy(bytes, buf, numToRead);
  return numToRead;
}

/**
 * Read numToRead characters from the buffer that starts at buf and ends at end.
 */
size_t javadistoglib_readJavaData(char* buf, size_t end, size_t numToRead, size_t bufSize, gchar* theCharString)
{
  size_t numRead = 0;
  size_t i = 0;

  if(end < (numToRead * 2)) {
    return 0;
  }
  if(bufSize < numToRead) {
    fprintf(stderr, "javadistoglib_readJavaChars bufSize is too small\n");
    return 0;
  }
  for(i = 0; i < numToRead; ++i) {
    numRead += javadistoglib_readJavaChar(buf + numRead, end, &(theCharString[i]));
  }
  return numRead;
}

size_t javadistoglib_readJavaInt(char* buf, size_t end, gint32* theInt)
{
  if(end < 4) {
    return 0;
  }
  *theInt = (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) |
             ((buf[2] & 0xff) << 8) | (buf[3] & 0xff));
  return 4;
}

size_t javadistoglib_readJavaLong(char* buf, size_t end, gint64* theLong)
{
  if(end < 8) {
    return 0;
  }
  *theLong = (((gint64)(buf[0] & 0xff) << 56) |
             ((gint64)(buf[1] & 0xff) << 48) |
             ((gint64)(buf[2] & 0xff) << 40) |
             ((gint64)(buf[3] & 0xff) << 32) |
             ((gint64)(buf[4] & 0xff) << 24) |
             ((gint64)(buf[5] & 0xff) << 16) |
             ((gint64)(buf[6] & 0xff) << 8) |
             ((gint64)(buf[7] & 0xff)));
  return 8;
}

size_t javadistoglib_readJavaDouble(char* buf, size_t end, gdouble* theDouble)
{
  guint64 bits;

  if(end < sizeof(gdouble)) {
    return 0;
  }

  memcpy(&bits, buf, sizeof(guint64));
  bits = GUINT64_FROM_BE(bits);
  memcpy(theDouble, &bits, sizeof(guint64));

  return sizeof(gdouble);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?