Main Page   Modules   Data Structures   File List   Data Fields   Globals  

translations.cpp

Go to the documentation of this file.
00001 /*
00002  * translations.cpp
00003  * Created on 25 Feb 2005
00004  * Authors:
00005  *    Wesley Leggette <wleggette@kaylix.net>
00006  * 
00007  * 
00008  * 
00009  * libdarc
00010  * 
00011  * 
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  * 
00026  * $Id: translations_8cpp-source.html 708 2005-03-21 12:30:37Z leggwes $
00027  */
00028 
00034 #ifdef LIBDARC_DEBUG
00035 # define TRANSLATIONS_H_INTERNAL
00036 #endif
00037 
00038 #include "translations.h"
00039 #undef TRANSLATIONS_H_INTERNAL
00040 #include "user_interaction_specific.hpp"
00041 #if LIBDARC_HAVE_LIBDAR_HPP
00042 #include <dar/libdar.hpp>
00043 #endif
00044 
00045 #if HAVE_LIMITS_H
00046 #include <limits.h>
00047 #endif
00048 
00049 #ifdef LIBDARC_DEBUG
00050 char break_buffer[30];
00051 #endif
00052 
00053 NAMESPACE_LIBDAR_START
00054 
00055 
00056 // Wraps char array as a generic_file. Used to read and write binary data
00057 // from infinint objects.
00058 class byte_stream : public generic_file
00059 {
00060 public:
00061     const static size_t ALLOCATE_SIZE = 16;
00062     
00063     
00064     byte_stream(user_interaction & dialog, char* source)
00065     : generic_file( dialog, gf_read_only )
00066     {
00067         buffer = source;
00068         position = 0;
00069         buf_size = 0;
00070     }
00071     
00072     byte_stream(user_interaction & dialog)
00073     : generic_file( dialog, gf_write_only )
00074     {
00075         position = length = 0;
00076         buf_size = ALLOCATE_SIZE;
00077         buffer = (char*)malloc(ALLOCATE_SIZE);
00078         if (buffer == NULL)
00079             throw Ememory("could not allocate string buffer");
00080     }
00081     
00082     ~byte_stream()
00083     {
00084         if (buf_size != 0)
00085             free(buffer);
00086     }
00087     
00088     char* get_string(U_I* store_size)
00089     {
00090         if (store_size != NULL)
00091             *store_size = length;
00092         char* ret = (char*)malloc(length);
00093         if (ret == NULL)
00094             return NULL;  // or out of memory
00095         memcpy(ret, buffer, length);
00096         //int i;
00097         //for (i = 0; i < length; i++)
00098         //  printf("ret[%X] = %X\n", ret + i, *(ret + i));
00099         return ret;
00100     }
00101     
00102     bool skip(const infinint & pos)
00103     {
00104         int tmp = 0;
00105         infinint t(pos);
00106         t.unstack(tmp);
00107         //printf("byte_stream::skip called, skipping %d\n", tmp);
00108         position += tmp;
00109         return true;
00110     }
00111     
00112     bool skip_to_eof()
00113     {
00114         //printf("byte_stream::skip_to_eof called, skipping to %X\n", buffer + position);
00115         position = length;
00116         return true;
00117     }
00118     
00119     bool skip_relative(S_I x)
00120     {
00121         //printf("byte_stream::skip_relative called, skipping %d\n", x);
00122         if (x > 0)
00123             position += x;
00124         else
00125             position -= (unsigned int)(-1 * x);
00126         return true;
00127     }
00128     
00129     infinint get_position()
00130     {
00131         return position;
00132     }
00133     
00134     S_I inherited_read(char* a, size_t size)
00135     {
00136         //printf("byte_stream::inherited_read: size = %d\n", size);
00137         size_t i = 0;
00138         //for (; i < size; i++)
00139         //  printf("byte_stream::inherited_read: buf = %X\n", *(buffer + position + i));
00140         //strncpy(a, (buffer + position), size);
00141         for(i = 0; i < size; i++)
00142             *(a+i) = *(buffer + position + i);
00143         //for (i = 0; i < size; i++)
00144         //  printf("byte_stream::inherited_read: a = %X\n", *(a + i));
00145         //printf("byte_stream::inherited_read: check 1\n");
00146         position += size;
00147         //printf("byte_stream::inherited_read: check 2\n");
00148         return (S_I)size;
00149     }
00150     
00151 
00152     
00153     S_I inherited_write(char* a, size_t size)
00154     {
00155         //printf("byte_stream::inherited_write: size = %d\n", size);
00156         //size_t i = 0;
00157         //for ( i = 0; i < size; i++ )
00158         //  printf("byte_stream::inherited_write: *a = %X\n", *(a+i));
00159         //printf("begin position = %X\n", buffer + position);
00160         //printf("begin length = %d\n", length);
00161         //for (i = 0; i < buf_size; i++)
00162         //  printf("begin *buffer[%X] = %X\n", buffer + i, 
00163         //                           *(buffer + i));
00164         if (size > buf_size - position)
00165         {
00166             //printf("byte_stream::inherited_write: check 1\n");
00167             buffer = (char*)realloc(buffer, buf_size + size + ALLOCATE_SIZE);
00168             //printf("byte_stream::inherited_write: check 2\n");
00169             if (buffer == NULL)
00170                 throw Ememory("could not reallocate string buffer");
00171             //printf("byte_stream::inherited_write: check 3\n");
00172             buf_size += size + ALLOCATE_SIZE;
00173             //printf("byte_stream::inherited_write: check 4\n");
00174             //printf("byte_stream::inherited_write: check 5\n");
00175             memcpy((buffer+position), a, size);
00176             position += size;
00177             length += size;
00178             //printf("byte_stream::inherited_write: check 6\n");
00179         } else {
00180             //printf("byte_stream::inherited_write: check a\n");
00181             //printf("byte_stream::inherited_write: check b\n");
00182             memcpy((buffer+position), a, size);
00183             position += size;
00184             length += size;
00185             //printf("byte_stream::inherited_write: check c\n");
00186         }
00187         //printf("end position = %X\n", buffer + position);
00188         //printf("end length = %d\n", length);
00189         //printf("end buffer = %x\n", buffer);
00190         //for (i = 0; i < buf_size; i++)
00191         //  printf("end *buffer[%X] = %X\n", buffer + i, 
00192         //                           *(buffer + i));
00193         
00194         return size;
00195     }
00196             
00197         
00198 private:
00199     size_t buf_size;
00200     size_t length;
00201     size_t position;
00202     char* buffer;
00203 };
00204 
00205 // convert decimal string to hex string
00206 //char* dtoh( char* deci )
00207 //{
00208     
00209     
00210 
00211 
00212 extern "C" void dar_infinint_destroy(dar_infinint* ref)
00213 {
00214     infinint* tmp = reinterpret_cast<infinint*>( ref );
00215     delete tmp;
00216     return;
00217 }
00218 
00219 extern "C" dar_infinint *dar_infinint_copy(dar_infinint* ref)
00220 {
00221     infinint* tmp = reinterpret_cast<infinint*>(ref);
00222     infinint* ret = new infinint( *tmp );
00223     return (dar_infinint*)ret;
00224 }
00225 
00226 
00227 extern "C" void dar_infinint_add( dar_infinint** a, dar_infinint* b )
00228 {
00229     infinint* ar = reinterpret_cast<infinint*>(*a);
00230     infinint* br = reinterpret_cast<infinint*>( b);
00231     *ar += *br;
00232     //*a = (dar_infinint*)ar;
00233 }
00234 
00235 extern "C" void dar_infinint_sub( dar_infinint** a, dar_infinint* b )
00236 {
00237     infinint* ar = (infinint*)*a;
00238     infinint* br = (infinint*)b;
00239     *ar -= *br;
00240     //*a = (dar_infinint*)ar;
00241 }
00242 
00243 extern "C" void dar_infinint_mult( dar_infinint** a, dar_infinint* b )
00244 {
00245     infinint* ar = (infinint*)*a;
00246     infinint* br = (infinint*)b;
00247     *ar *= *br;
00248     //*a = (dar_infinint*)ar;
00249 }
00250 
00251 extern "C" void dar_infinint_div( dar_infinint** a, dar_infinint* b )
00252 {
00253     infinint* ar = (infinint*)*a;
00254     infinint* br = (infinint*)b;
00255     *ar /= *br;
00256     //*a = (dar_infinint*)ar;
00257 }
00258 
00259 extern "C" void dar_infinint_pow( dar_infinint** a, dar_infinint* b )
00260 {
00261     infinint* ar = (infinint*)*a;
00262     infinint* br = (infinint*)b;
00263     *ar = ar->power(*br);
00264     //*a = (dar_infinint*)ar;
00265 }
00266 
00267 extern "C" void dar_infinint_powi( dar_infinint** a, int exp )
00268 {
00269     infinint* ar = (infinint*)*a;
00270     *ar = ar->power(exp);
00271     //*a = (dar_infinint*)ar;
00272 }
00273 
00274 extern "C" void dar_infinint_shiftl( dar_infinint** a, dar_infinint* b )
00275 {
00276     infinint* ar = (infinint*)*a;
00277         //int i = 0;
00278         //infinint tmp(*ar);
00279         //tmp.unstack(i);
00280         //printf("ar = %d\n", i);
00281     infinint* br = (infinint*)b;
00282         //i = 0;
00283         //tmp = *br;
00284         //tmp.unstack(i);
00285         //printf("br = %d\n", i);
00286     *ar <<= *br;
00287         //i = 0;
00288         //tmp = *ar;
00289         //tmp.unstack(i);
00290         //printf("final ar = %d\n", i);
00291     //*a = (dar_infinint*)ar;   
00292 }
00293 
00294 extern "C" void dar_infinint_shiftli( dar_infinint** a, int bit )
00295 {
00296     infinint* ar = (infinint*)*a;
00297         //int i = 0;
00298         //infinint tmp(*ar);
00299         //tmp.unstack(i);
00300         //printf("ar = %d\n", i);
00301     *ar <<= bit;
00302         //i = 0;
00303         //tmp = *ar;
00304         //tmp.unstack(i);
00305         //printf("final ar = %d\n", i);
00306     //*a = (dar_infinint*)ar;
00307 }
00308 
00309 extern "C" void dar_infinint_shiftr( dar_infinint** a, dar_infinint* b )
00310 {
00311     infinint* ar = (infinint*)*a;
00312     infinint* br = (infinint*)b;
00313     *ar >>= *br;
00314     //*a = (dar_infinint*)ar;   
00315 }
00316 
00317 extern "C" void dar_infinint_shiftri( dar_infinint** a, int bit )
00318 {
00319     infinint* ar = (infinint*)*a;
00320     *ar >>= bit;
00321     //*a = (dar_infinint*)ar;
00322 }
00323 
00324 extern "C" void dar_infinint_mod( dar_infinint** a, dar_infinint* b )
00325 {
00326     infinint* ar = reinterpret_cast<infinint*>(*a);
00327     infinint* br = reinterpret_cast<infinint*>( b);
00328     *ar %= *br;
00329     //*a = (dar_infinint*)ar;
00330 }
00331 
00332 extern "C" void dar_infinint_increment( dar_infinint** a )
00333 {
00334     infinint* ar = (infinint*)*a;
00335         //int i = 0;
00336         //infinint tmp(*ar);
00337         //tmp.unstack(i);
00338         //printf("ar = %d\n", i);
00339     (*ar)++;
00340         //i = 0;
00341         //tmp = *ar;
00342         //tmp.unstack(i);
00343         //printf("final ar = %d\n", i);
00344     //*a = (dar_infinint*)ar;
00345 }
00346 
00347 extern "C" void dar_infinint_decrement( dar_infinint** a )
00348 {
00349     infinint* ar = (infinint*)*a;
00350     (*ar)--;
00351     //*a = (dar_infinint*)ar;
00352 }
00353 
00354 extern "C" void dar_infinint_euclide( dar_infinint* a, dar_infinint* b,
00355                      dar_infinint** q, dar_infinint** r )
00356 {
00357     infinint* ar = (infinint*)a;
00358     infinint* br = (infinint*)b;
00359     infinint* qr = (infinint*)*q;
00360     infinint* rr = (infinint*)*r;
00361     *qr = *ar / *br;
00362     *rr = *ar % *br;
00363 }
00364                      
00365 
00366 extern "C" bool dar_infinint_comp_eq( dar_infinint* a, dar_infinint* b )
00367 {
00368     infinint* ar = (infinint*)a;
00369     infinint* br = (infinint*)b;
00370     return *ar == *br;
00371 }
00372 
00373 extern "C" bool dar_infinint_comp_leq( dar_infinint* a, dar_infinint* b )
00374 {
00375     infinint* ar = (infinint*)a;
00376     infinint* br = (infinint*)b;
00377     return *ar <= *br;
00378 }
00379 
00380 extern "C" bool dar_infinint_comp_l( dar_infinint* a, dar_infinint* b )
00381 {
00382     infinint* ar = (infinint*)a;
00383     infinint* br = (infinint*)b;
00384     return *ar < *br;
00385 }
00386 
00387 extern "C" bool dar_infinint_comp_geq( dar_infinint* a, dar_infinint* b )
00388 {
00389     infinint* ar = (infinint*)a;
00390     infinint* br = (infinint*)b;
00391     return *ar >= *br;
00392 }
00393 
00394 extern "C" bool dar_infinint_comp_g( dar_infinint* a, dar_infinint* b )
00395 {
00396     infinint* ar = (infinint*)a;
00397     infinint* br = (infinint*)b;
00398     return *ar > *br;
00399 }
00400 
00401 extern "C" bool dar_infinint_comp_neq( dar_infinint* a, dar_infinint* b )
00402 {
00403     infinint* ar = (infinint*)a;
00404     infinint* br = (infinint*)b;
00405     return *ar != *br;
00406 }
00407 
00408 
00409 
00410 extern "C" dar_infinint *dar_infpi(int num)
00411 {
00412     infinint* ret = new infinint(num);
00413     return (dar_infinint*)ret;
00414 }
00415 
00416 #if LIBDARC_OS_BITS == 64
00417     extern "C" dar_infinint *dar_infi(U_64 num)
00418     {
00419         infinint* ret = new infinint(num);
00420         return (dar_infinint*)ret;
00421     }
00422     
00423 #else
00424     extern "C" dar_infinint *dar_infi(U_32 num)
00425     {
00426         infinint* ret = new infinint(num);
00427         return (dar_infinint*)ret;
00428     }
00429     
00430 #endif
00431 
00432 extern "C" dar_infinint *dar_infu32(U_32 num)
00433 {
00434     infinint* ret = new infinint(num);
00435     return (dar_infinint*)ret;
00436 }
00437 
00438 extern "C" dar_infinint *dar_infu64(U_64 num)
00439 {
00440     infinint* ret = new infinint(num);
00441     return (dar_infinint*)ret;
00442 }
00443 
00444 
00445 extern "C" dar_infinint *dar_infds(char* num)
00446 {
00447     try {
00448         std::string str(num);
00449         deci d(str);
00450         infinint* ret = new infinint();
00451         *ret = d.computer();
00452         return (dar_infinint*)ret;
00453     } catch (...)
00454     {
00455         return NULL;
00456     }
00457 }
00458 
00459 extern "C" dar_infinint *dar_infhs(char* num)
00460 {
00461     // TODO: This function is not implemented.
00462     infinint* ret = new infinint();
00463     return (dar_infinint*)ret;
00464 }
00465 
00466 extern "C" dar_infinint *dar_infbin(U_8* num)
00467 {
00468     user_interaction_null* ut = new user_interaction_null();
00469     byte_stream stream( *ut, (char*)num );
00470     infinint* ret = new infinint();
00471     try 
00472     {
00473         ret->read( stream );
00474     } catch (...) {
00475         delete ut;
00476         delete ret;
00477         return NULL;
00478     }
00479 //  int tmp = 0;
00480 //  infinint t(*ret);
00481 //  t.unstack(tmp);
00482 //  printf("final infinint = %d\n", tmp);
00483     delete ut;
00484     return (dar_infinint*)ret;
00485 }
00486 
00487 
00488 extern "C" int dar_piinf (bool* overflow, dar_infinint* box)
00489 {
00490     infinint* rb = (infinint*)box;
00491     infinint tmp(*rb);
00492     int ret = 0;
00493     if (overflow != NULL)
00494     {
00495         if ( tmp >= UINT_MAX )
00496             *overflow = true;
00497         else
00498             *overflow = false;
00499     }
00500     tmp.unstack(ret);
00501     return ret;
00502 }
00503 
00504 
00505 #if LIBDARC_OS_BITS == 64
00506     extern "C" U_64 dar_iinf (bool* overflow, dar_infinint* box)
00507     {
00508         infinint* rb = (infinint*)box;
00509         infinint tmp(*rb);
00510         U_64 ret = 0;
00511         if (overflow != NULL)
00512         {
00513             if ( tmp >= U_64_MAX )
00514                 *overflow = true;
00515             else
00516                 *overflow = false;
00517         }
00518         tmp.unstack(ret);
00519         return ret;
00520     }
00521     
00522 #else
00523     extern "C" U_32 dar_iinf (bool* overflow, dar_infinint* box)
00524     {
00525         infinint* rb = (infinint*)box;
00526         infinint tmp(*rb);
00527         U_32 ret = 0;
00528         if (overflow != NULL)
00529         {
00530             if ( tmp >= U_32_MAX )
00531                 *overflow = true;
00532             else
00533                 *overflow = false;
00534         }
00535         tmp.unstack(ret);
00536         return ret;
00537     }
00538     
00539 #endif
00540 
00541 extern "C" U_32 dar_u32inf (bool* overflow, dar_infinint* box)
00542 {
00543     infinint* rb = (infinint*)box;
00544     infinint tmp(*rb);
00545     U_32 ret = 0;
00546     if (overflow != NULL)
00547     {
00548         if ( tmp >= U_32_MAX )
00549             *overflow = true;
00550         else
00551             *overflow = false;
00552     }
00553     tmp.unstack(ret);
00554     return ret;
00555 }
00556 
00557 extern "C" U_64 dar_u64inf (bool* overflow, dar_infinint* box)
00558 {
00559     infinint* rb = (infinint*)box;
00560     infinint tmp(*rb);
00561     U_64 ret = 0;
00562     if (overflow != NULL)
00563     {
00564         if ( tmp >= U_64_MAX )
00565             *overflow = true;
00566         else
00567             *overflow = false;
00568     }
00569     tmp.unstack(ret);
00570     return ret;
00571 }
00572 
00573 extern "C" char* dar_dsinf (U_I* length, dar_infinint* box)
00574 {
00575     infinint* tmp = (infinint*)box;
00576     deci d(*tmp);
00577     std::string str(d.human());
00578     char* ret = (char*)malloc(str.length() + 1);
00579     if (ret == NULL) { return NULL; }
00580     strcpy(ret, str.c_str());
00581     if (length != NULL)
00582         *length = strlen(str.c_str());
00583     return ret;
00584 }
00585 
00586 extern "C" char* dar_hsinf (U_I* length, dar_infinint* box)
00587 {
00588     // TODO: This function is not implemented.
00589     char* ret = (char*)malloc(5);
00590     if (ret == NULL) { return NULL; }
00591     if (length != NULL)
00592         *length = 0;
00593     return ret;
00594 }
00595 
00596 extern "C" U_8* dar_bininf (U_I* length, dar_infinint* box)
00597 {
00598     user_interaction_null* ut = new user_interaction_null();
00599     infinint* b = reinterpret_cast<infinint*>(box);
00600     byte_stream stream( *ut );
00601     try {
00602         b->dump( stream );
00603     } catch ( Egeneric & e ) {
00604         //printf("error = %s", e.get_message().c_str() );
00605         return NULL;
00606     }
00607     delete ut;
00608     return (U_8*)stream.get_string(length);
00609 }
00610 
00611 
00612 
00613 
00614 NAMESPACE_LIBDAR_END
00615