javadistoglib.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006  SRI International
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  * SRI International: 333 Ravenswood Ave, Menlo Park, CA 94025
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 }

Generated on Wed May 23 17:20:10 2007 using doxygen 1.5.2