00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include "javadistoglib.h"
00024
00030 size_t javadistoglib_readJavaChar(char* buf, size_t end, gchar* theChar)
00031 {
00032 if(end < 2) {
00033 return 0;
00034 }
00035 memcpy(theChar, buf + 1, sizeof(char));
00036 return 2;
00037 }
00038
00042 size_t javadistoglib_readJavaChars(char* buf, size_t end, size_t numToRead, size_t bufSize, gchar* theCharString)
00043 {
00044 size_t numRead = 0;
00045 size_t i = 0;
00046
00047 if(end < (numToRead * 2)) {
00048 return 0;
00049 }
00050 if(bufSize < (numToRead + 1)) {
00051 fprintf(stderr, "javadistoglib_readJavaChars bufSize is too small\n");
00052 return 0;
00053 }
00054 for(i = 0; i < numToRead; ++i) {
00055 numRead += javadistoglib_readJavaChar(buf + numRead, end, &(theCharString[i]));
00056 }
00057 theCharString[numToRead] = '\0';
00058 return numRead;
00059 }
00060
00061 size_t javadistoglib_readJavaBytes(char* buf, size_t end, size_t numToRead, size_t bufSize, gchar* bytes)
00062 {
00063 if(end < numToRead) {
00064 return 0;
00065 }
00066 if(bufSize < numToRead) {
00067 fprintf(stderr, "javadistoglib_readJavaBytes bufSize is too small\n");
00068 return 0;
00069 }
00070 memcpy(bytes, buf, numToRead);
00071 return numToRead;
00072 }
00073
00077 size_t javadistoglib_readJavaData(char* buf, size_t end, size_t numToRead, size_t bufSize, gchar* theCharString)
00078 {
00079 size_t numRead = 0;
00080 size_t i = 0;
00081
00082 if(end < (numToRead * 2)) {
00083 return 0;
00084 }
00085 if(bufSize < numToRead) {
00086 fprintf(stderr, "javadistoglib_readJavaChars bufSize is too small\n");
00087 return 0;
00088 }
00089 for(i = 0; i < numToRead; ++i) {
00090 numRead += javadistoglib_readJavaChar(buf + numRead, end, &(theCharString[i]));
00091 }
00092 return numRead;
00093 }
00094
00095 size_t javadistoglib_readJavaInt(char* buf, size_t end, gint32* theInt)
00096 {
00097 if(end < 4) {
00098 return 0;
00099 }
00100 *theInt = (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) |
00101 ((buf[2] & 0xff) << 8) | (buf[3] & 0xff));
00102 return 4;
00103 }
00104
00105 size_t javadistoglib_readJavaLong(char* buf, size_t end, gint64* theLong)
00106 {
00107 if(end < 8) {
00108 return 0;
00109 }
00110 *theLong = (((gint64)(buf[0] & 0xff) << 56) |
00111 ((gint64)(buf[1] & 0xff) << 48) |
00112 ((gint64)(buf[2] & 0xff) << 40) |
00113 ((gint64)(buf[3] & 0xff) << 32) |
00114 ((gint64)(buf[4] & 0xff) << 24) |
00115 ((gint64)(buf[5] & 0xff) << 16) |
00116 ((gint64)(buf[6] & 0xff) << 8) |
00117 ((gint64)(buf[7] & 0xff)));
00118 return 8;
00119 }
00120
00121 size_t javadistoglib_readJavaDouble(char* buf, size_t end, gdouble* theDouble)
00122 {
00123 guint64 bits;
00124
00125 if(end < sizeof(gdouble)) {
00126 return 0;
00127 }
00128
00129 memcpy(&bits, buf, sizeof(guint64));
00130 bits = GUINT64_FROM_BE(bits);
00131 memcpy(theDouble, &bits, sizeof(guint64));
00132
00133 return sizeof(gdouble);
00134 }