Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1185 connectés 

  FORUM HardWare.fr
  Programmation
  C

  portabilité linux vers windows

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

portabilité linux vers windows

n°1240575
neotunner
Y'a un début à tout
Posté le 07-11-2005 à 16:32:16  profilanswer
 

Bonjour,
Pour un projet de mon stage, je fais une petite appli en C qui calcule le hash MD4 (version eDonkey) d'un fichier passé en paramètre et le sort dans une chaine de caractère, associé à cette fonctions, il ya bien sur les fonctions de calcul de hash que j'ai récupéré sur le net et un petit main histoire de donner le fichier à chercher et afficher le hash en sortie. Sous linux, ça compile et ça marche nickel, je doit faire cette appli pour qu'elle fonctionne sous windows (compilateur de visual studio (Vc++ 7.0.9955) après quq modif d'appel de librairies et fonctions basique de lecture (read -> _read, open ->_open), ça compile, ça s'exécute mais le hash deviens n'importe quoi!!!
En fait je me retrouve avec un hash de la sorte:
ABCDABCDEFGHEFGH (les 4 premier octets sont répétés une fois et les octets 9 à 12 sont répétés sur les 4 derniers)
ce pseudo hash est pris directe à au retour des fonctions de hashage
 
Je n'ai pas touché au code des fonctions de hashage
 
D'ou viendrais ce soucis? des librairies appelées ds le prgm st pas les mêmes sous vc++ et sous linux?
 
Mon linux: Mandrake 9.2 avec gcc 3.3.1
 
librairies appelées sous linux:
errno.h;fcntl.h;stdio.h,unistd.h,stdlib.h,string.h,sys/types.h,sys/stat.h
 
librairies appelées sous windows:
errno.h;fcntl.h;stdio.h,io.h,stdlib.h,string.h,sys/types.h,sys/stat.h
En fait ce sont les même sauf unistd remplacée par io
 
merki pour vos réponses


---------------
Vive le tunning
mood
Publicité
Posté le 07-11-2005 à 16:32:16  profilanswer
 

n°1240835
karlkox
Posté le 07-11-2005 à 21:40:39  profilanswer
 

Essaye de passer  pas les API (CreateFile, ReadFile ...).

n°1240842
chrisbk
-
Posté le 07-11-2005 à 21:51:19  profilanswer
 

CreateFile sous linux [:pingouino]

n°1240843
chrisbk
-
Posté le 07-11-2005 à 21:52:01  profilanswer
 

pkoi t'utilise pas fread&consort, deja ?

n°1240859
karlkox
Posté le 07-11-2005 à 22:12:58  profilanswer
 

chrisbk a écrit :

CreateFile sous linux [:pingouino]


 
Ca fonctionne sous Linux pas sous Windows son code.  :na:

n°1240864
Taz
bisounours-codeur
Posté le 07-11-2005 à 22:21:09  profilanswer
 

ANSI + glib

n°1240865
chrisbk
-
Posté le 07-11-2005 à 22:21:38  profilanswer
 

ouais, enfin en pratique jtrouve fread moins relou a utiliser :d (hop, je detourne finement la conversation et gagne deux points pommes)

n°1240866
karlkox
Posté le 07-11-2005 à 22:22:25  profilanswer
 

Je plussois le fread (technique de brossage de dos à mon avantage ^_^)

n°1241008
neotunner
Y'a un début à tout
Posté le 08-11-2005 à 09:11:27  profilanswer
 

oki, merki bien pour vos propals, je looké ça je vous tiens, au couant, si amais vous avez d'autres idées, n'hésitez pas


---------------
Vive le tunning
n°1241093
neotunner
Y'a un début à tout
Posté le 08-11-2005 à 11:41:58  profilanswer
 

bon, aucune nouveauté avec fread et fopen....dommage


---------------
Vive le tunning
mood
Publicité
Posté le 08-11-2005 à 11:41:58  profilanswer
 

n°1241121
Elmoricq
Modérateur
Posté le 08-11-2005 à 12:07:46  profilanswer
 

neotunner a écrit :

bon, aucune nouveauté avec fread et fopen....dommage


 
Qu'est-ce que tu veux dire par là ?
 
Ces fonctions sont on ne peut plus standard.

n°1241125
neotunner
Y'a un début à tout
Posté le 08-11-2005 à 12:12:14  profilanswer
 

ouais, krément je m'en doute qu'elles sont std, l'utilisation de ces fonctions sous linux et windows st exactement pareil, mais j'ai tjs ma sortie fantaisiste, cela signifie que le souci ne viens pas de là, je me posais la question des headers (les headers linux sont différents des headers windows, ça peux jouer?) ou bien il ne reste plus que les fonctions de hash qui ne se comporte pas correctement sous windows, je n'ai rien touché à ces fonctions...je sais pas ce qui se passe dites donc


---------------
Vive le tunning
n°1241348
Emmanuel D​elahaye
C is a sharp tool
Posté le 08-11-2005 à 15:31:49  profilanswer
 

neotunner a écrit :

l'utilisation de ces fonctions sous linux et windows st exactement pareil, mais j'ai tjs ma sortie fantaisiste, cela signifie que le souci ne viens pas de là, je me posais la question des headers (les headers linux sont différents des headers windows, ça peux jouer?) ou bien il ne reste plus que les fonctions de hash qui ne se comporte pas correctement sous windows, je n'ai rien touché à ces fonctions...je sais pas ce qui se passe dites donc


Poste ton code ou donne un lien. Tu fais peut être des choses pas portables...


---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
n°1241382
neotunner
Y'a un début à tout
Posté le 08-11-2005 à 16:05:33  profilanswer
 

oki, je vais mettre le code, tout ce qui est fonction de hash, je l'ai chopé sur le net, en fait j'ai juste arrangé la fonction calchash et posé un main pour balancer un fichier à la fonction calchash et pour récupéré le hash renvoyé par calchash:

Code :
  1. /* $Id: ed2k.c,v 1.4 2004/06/13 17:42:25 sdalu Exp $ */
  2. /**
  3. * @file ed2k.c
  4. * @date 2002/09/29 13:47:05
  5. * @author Stephane D'Alu
  6. *
  7. * $Revision: 1.4 $
  8. * $Date: 2004/06/13 17:42:25 $
  9. *
  10. * The code for the MD4 subroutines are from RSA Data Security
  11. *  See the copyright below ( look for the string: ==> )
  12. *
  13. * The remaining of the code is placed under public domain.
  14. *
  15. * WWW: http://www.sdalu.com/software/
  16. *
  17. * Credit:  
  18. *   - edonkey computation id: http://xpoop.free.fr/desc.htm
  19. *
  20. * Compile:
  21. *   - just type: make ed2k
  22. */
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. /* #define OFF_CONV "%ld"   off_t is long      (Linux)   */
  32. /* #define OFF_CONF "%lld"   off_t is long long (FreeBSD) */
  33. #ifndef OFF_CONV
  34. //#  warning "You should define OFF_CONV (see the file)"
  35. #  define OFF_CAST (long)
  36. #  define OFF_CONV "%ld"
  37. #else
  38. #  define OFF_CAST
  39. #endif
  40. #define BLOCKSIZE 4096
  41. #define EDK_BLOCK 2375
  42. typedef unsigned char md4_t[16];
  43. typedef struct {
  44.     u_int32_t  state[4];
  45.     u_int32_t  count[2];
  46.     unsigned char buffer[64];
  47. } md4_ctx_t;
  48. void md4_init(md4_ctx_t *);
  49. void md4_update(md4_ctx_t *, unsigned char *, unsigned int);
  50. void md4_finish(md4_ctx_t *, md4_t);
  51. void hash_fd(int fd, md4_t digest) {
  52.     unsigned char buf[BLOCKSIZE];
  53.     int           len   = -1;
  54.     int    count =  0;
  55.     md4_ctx_t   ctx_i;
  56.     md4_ctx_t   ctx_f;
  57.     md4_init(&ctx_f);
  58.     do {
  59. int eb = EDK_BLOCK;
  60. md4_init(&ctx_i);
  61. while ((eb-- > 0) && ((len = read(fd, buf, BLOCKSIZE)) > 0))
  62.     md4_update(&ctx_i, buf, len);
  63. md4_finish(&ctx_i, digest);
  64. count++;
  65. md4_update(&ctx_f, digest, sizeof(md4_t));
  66.     } while (len > 0);
  67.    
  68.     if (count > 1)
  69. md4_finish(&ctx_f, digest);
  70. }
  71. static __inline__ char hdigit(q) {
  72.     return ((q >= 10) ? 'W' : '0') + q;
  73. }
  74. char *digest_str(void *digest) {
  75.     static char internal_str[sizeof(md4_t)*2 + 1];
  76.     char *str   = internal_str;
  77.     int   dsize = sizeof(md4_t);
  78.     while (dsize--) {
  79. unsigned char d = *((unsigned char *)digest)++;
  80. *(str++) = hdigit(d >> 4 );
  81. *(str++) = hdigit(d & 0xf);
  82.     }
  83.     *str = '\0';
  84.     return internal_str;
  85. }


voila la fonction calchash que j'ai mis à ma sauce, et le main pour l'utiliser et voir le résultat...

Code :
  1. char *calculhash(char *adresse)
  2. {int file;
  3. md4_t buffer;
  4. char *sortie=(char *)malloc(33);
  5. file=open(adresse,O_RDONLY);
  6. if (file!=-1) hash_fd(file,buffer);
  7. sortie=digest_str(buffer);
  8. close(file);
  9. return sortie;
  10. }
  11. int main()
  12. {char *retour=(char *)malloc(33);
  13. char *fic="./Kyo - 300 Lesions - Sarah.mp3";
  14. retour=calculhash(fic);
  15. printf("Le hash de Kyo: %s\n",retour);
  16. free(retour);
  17. return 0;
  18. }
  19. /*==> RSA Data Security, Inc. MD4 Message-Digest Algorithm =============*/
  20. /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
  21.    License to copy and use this software is granted provided that it
  22.    is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  23.    Algorithm" in all material mentioning or referencing this software
  24.    or this function.
  25.    License is also granted to make and use derivative works provided
  26.    that such works are identified as "derived from the RSA Data
  27.    Security, Inc. MD4 Message-Digest Algorithm" in all material
  28.    mentioning or referencing the derived work.
  29.    RSA Data Security, Inc. makes no representations concerning either
  30.    the merchantability of this software or the suitability of this
  31.    software for any particular purpose. It is provided "as is"
  32.    without express or implied warranty of any kind.
  33.    These notices must be retained in any copies of any part of this
  34.    documentation and/or software.
  35. */
  36. static unsigned char md4_padding[64] = {
  37.     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  38.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  40. };
  41. #define FF(a, b, c, d, x, s) {      \
  42.     (a) += (((b) & (c)) | ((~b) & (d))) + (x);    \
  43.     (a)  = ((a) << (s)) | ((a) >> (32-(s)));    \
  44. }
  45. #define GG(a, b, c, d, x, s) {      \
  46.     (a) += (((b)&(c))|((b)&(d))|((c)&(d))) + (x) + (u_int32_t)0x5a827999; \
  47.     (a)  = ((a) << (s)) | ((a) >> (32-(s)));    \
  48. }
  49. #define HH(a, b, c, d, x, s) {      \
  50.     (a) += ((b) ^ (c) ^ (d)) + (x) + (u_int32_t)0x6ed9eba1;  \
  51.     (a)  = ((a) << (s)) | ((a) >> (32-(s)));    \
  52. }
  53. static void md4_transform (u_int32_t state[4], unsigned char block[64]) {
  54.     unsigned int i, j;
  55.     u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  56.    
  57.     for (i = 0, j = 0 ; j < 64 ; i++, j += 4)
  58. x[i] = ((u_int32_t)block[j]) | (((u_int32_t)block[j+1]) << 8) |
  59.     (((u_int32_t)block[j+2]) << 16) | (((u_int32_t)block[j+3]) << 24);
  60.     FF (a, b, c, d, x[ 0],   3); FF (d, a, b, c, x[ 1],   7);
  61.     FF (c, d, a, b, x[ 2],  11); FF (b, c, d, a, x[ 3],  19);
  62.     FF (a, b, c, d, x[ 4],   3); FF (d, a, b, c, x[ 5],   7);
  63.     FF (c, d, a, b, x[ 6],  11); FF (b, c, d, a, x[ 7],  19);
  64.     FF (a, b, c, d, x[ 8],   3); FF (d, a, b, c, x[ 9],   7);
  65.     FF (c, d, a, b, x[10],  11); FF (b, c, d, a, x[11],  19);
  66.     FF (a, b, c, d, x[12],   3); FF (d, a, b, c, x[13],   7);
  67.     FF (c, d, a, b, x[14],  11); FF (b, c, d, a, x[15],  19);
  68.     GG (a, b, c, d, x[ 0],   3); GG (d, a, b, c, x[ 4],   5);
  69.     GG (c, d, a, b, x[ 8],   9); GG (b, c, d, a, x[12],  13);
  70.     GG (a, b, c, d, x[ 1],   3); GG (d, a, b, c, x[ 5],   5);
  71.     GG (c, d, a, b, x[ 9],   9); GG (b, c, d, a, x[13],  13);
  72.     GG (a, b, c, d, x[ 2],   3); GG (d, a, b, c, x[ 6],   5);
  73.     GG (c, d, a, b, x[10],   9); GG (b, c, d, a, x[14],  13);
  74.     GG (a, b, c, d, x[ 3],   3); GG (d, a, b, c, x[ 7],   5);
  75.     GG (c, d, a, b, x[11],   9); GG (b, c, d, a, x[15],  13);
  76.     HH (a, b, c, d, x[ 0],   3); HH (d, a, b, c, x[ 8],   9);
  77.     HH (c, d, a, b, x[ 4],  11); HH (b, c, d, a, x[12],  15);
  78.     HH (a, b, c, d, x[ 2],   3); HH (d, a, b, c, x[10],   9);
  79.     HH (c, d, a, b, x[ 6],  11); HH (b, c, d, a, x[14],  15);
  80.     HH (a, b, c, d, x[ 1],   3); HH (d, a, b, c, x[ 9],   9);
  81.     HH (c, d, a, b, x[ 5],  11); HH (b, c, d, a, x[13],  15);
  82.     HH (a, b, c, d, x[ 3],   3); HH (d, a, b, c, x[11],   9);
  83.     HH (c, d, a, b, x[ 7],  11); HH (b, c, d, a, x[15],  15);
  84.     state[0] += a;    state[1] += b;
  85.     state[2] += c;    state[3] += d;
  86. }
  87. static void md4_encode(unsigned char *output,
  88.         u_int32_t *input, unsigned int len) {
  89.     unsigned int i, j;
  90.    
  91.     for (i = 0, j = 0; j < len; i++, j += 4) {
  92. output[j]   = (unsigned char) (input[i]        & 0xff);
  93. output[j+1] = (unsigned char)((input[i] >> 8)  & 0xff);
  94. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  95. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  96.     }
  97. }
  98. void md4_init(md4_ctx_t *context) {
  99.     context->count[0] = context->count[1] = 0;
  100.     context->state[0] = 0x67452301;
  101.     context->state[1] = 0xefcdab89;
  102.     context->state[2] = 0x98badcfe;
  103.     context->state[3] = 0x10325476;
  104. }
  105. void md4_update(md4_ctx_t *context, unsigned char *input, unsigned int len) {
  106.     unsigned int i, index, partLen;
  107.     index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  108.     if ((context->count[0] += ((u_int32_t)len << 3)) < ((u_int32_t)len << 3))
  109. context->count[1]++;
  110.     context->count[1] += ((u_int32_t)len >> 29);
  111.    
  112.     partLen = 64 - index;
  113.    
  114.     if (len >= partLen) {
  115. memcpy(&context->buffer[index], input, partLen);
  116. md4_transform(context->state, context->buffer);
  117. for (i = partLen; i + 63 < len; i += 64)
  118.     md4_transform(context->state, &input[i]);
  119. index = 0;
  120.     } else {
  121. i = 0;
  122.     }
  123.     memcpy(&context->buffer[index], &input[i], len-i);
  124. }
  125. void md4_finish (md4_ctx_t *context, md4_t digest) {
  126.     unsigned char bits[8];
  127.     unsigned int index, padLen;
  128.     md4_encode (bits, context->count, 8);
  129.    
  130.     index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  131.     padLen = (index < 56) ? (56 - index) : (120 - index);
  132.     md4_update (context, md4_padding, padLen);
  133.    
  134.     md4_update (context, bits, 8);
  135.     md4_encode (digest, context->state, 16);
  136. }


c'est la version qui tourne impeccable sous linux avec des petits et gros fichier
Voila, voila


Message édité par neotunner le 08-11-2005 à 16:08:56

---------------
Vive le tunning

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  portabilité linux vers windows

 

Sujets relatifs
Créer un service windows Compte LocalSystem[perl] lancer un process windows en perl
Migration Crystal 8.5 vers XIUn lien vers un fichier sur mon reseau
php : Indexeur sous Windows[perl dédié linux putty]lancer perl en tâche de fond
__FILE__ avec Path sous linuxListe des utilisateurs sous Linux
[débutant] différence Windows Forms et asp.netQuestions linux et administration .. big brother ?
Plus de sujets relatifs à : portabilité linux vers windows


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR