🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fr.comp.lang.c
Article #15253

Re: rot13 [Was : Re: conjugaison et autres ...]

#15253
From: Pierre Maurette
Date: Mon, 03 Oct 2005 11:20
160 lines
5455 bytes
Harpo, le 02/10/2005, a �crit :
> Emmanuel Delahaye wrote:
>
>> 
>> Lid�e est de cr�er 2 tables avec tous les caract�res (A-Z) et (a-z),
>> et de chercher dedans la position 'i' du caract�re avec strchr().
>> Ensuite, on va chercher le caract�re � (i + 13) % 26...
>
> Je pense qu'il y a plus simple et plus performant :
>
> On prend une table dont la taille correspond aux valeurs possibles des
> caract�res ( g�n�ralement 256 ), � l'emplacement dont l'offset
> correspond � la valeur d'un caract�re, on place le caract�re par lequel
> il doit �tre traduit. C'est fait une fois � la g�n�ration du logiciel
> pour une architecture donn�e, ou alors on a une table pour l'ASCII, une
> pour l'EBCDIC etc.
> La conversion consiste pour chaque caract�re � le remplacer par la
> valeur de la table dont il est l'index.
Il y a m�me en x86 XLAT/XLATB pour faire �a en une instruction. Je ne 
sais pas si c'est plus rapide, en tout cas MSVC ne l'utilise pas. Faut 
le faire � la  main. Bien entendu, l'optimisation n'a pas de sens dans 
le cas d'une fonction qui crypte un seul caract�re.

Mon tas de sable:

#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include <malloc.h>
#include <stdlib.h>

char* MakeRot13Table1(void);
char* MakeRot13Table2(void);

#define MAX_TAB 200

int main(void)
{
char* Rot13Table1 = NULL;
char* Rot13Table2 = NULL;

char phrase[MAX_TAB]="Preface : The computing world has undergone a 
revolution since the publication of \"The C Programming Language\" in 
1978.";
char brouillon[MAX_TAB];
int i;

if(Rot13Table1 == NULL) Rot13Table1 = MakeRot13Table1();
if(Rot13Table2 == NULL) Rot13Table2 = MakeRot13Table2();

if((Rot13Table1 == NULL) || (Rot13Table2 == NULL)){/* traiter erreur � 
l'initialisation */}

i = 0;
while (phrase[i]!='\0')
{
	if(isalpha(phrase[i]))
	{
		brouillon[i] = Rot13Table1[(size_t)phrase[i]];
	}
	else
	{
		brouillon[i] = phrase[i];
	}
	i++;
}
brouillon[i] = '\0';
printf("%s\n", brouillon);

for(i = 0; i < 200; i++)brouillon[i] = '\0';
i = 0;
while (phrase[i]!='\0')
{
	brouillon[i] = Rot13Table2[(size_t)phrase[i++]];
}
brouillon[i] = '\0';
printf("%s\n", brouillon);

for(i = 0; i < 200; i++)brouillon[i] = '\0';

#ifdef _MSC_VER
__asm
{
#define GARDE_FOU MAX_TAB

	xor ECX, ECX
	mov EBX, Rot13Table2

boucle:
	mov AL, byte ptr phrase[ECX]
	xlat
	mov byte ptr brouillon[ECX], AL
	inc ECX
	cmp ECX, GARDE_FOU
	jg short fin
	test AL, AL
	jne short boucle

fin:
	mov byte ptr brouillon[MAX_TAB - 1], 0
}
printf("%s\n", brouillon);
#else /* _MSC_VER */
#error Visual C++ requis
#endif /* _MSC_VER */

return EXIT_SUCCESS;
}


char* MakeRot13Table1(void)
{
	char* t = NULL;
	size_t max = 0, dummyst;
	int i;
	char dummystring[] = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for(i = 0; (i < 52) & (dummystring[i] != '\0'); i++)
	{
		if(max < ((size_t)dummystring[i])) max = (size_t)dummystring[i];
	}
	if((t = (char*)malloc((max + 1) * sizeof(char*))) != NULL)
	{
		for(i = 0; i < 26; i++)
		{
			t[(size_t)dummystring[i]] = dummystring[dummyst = (i + 13) % 26];
			t[(size_t)dummystring[i + 26]] = dummystring[dummyst + 26];
		}
	}
	return t;
}


char* MakeRot13Table2(void)
{
	size_t max = 0, dummyst;
	int i;
	char dummystring[] = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char* t = (char*)malloc((UCHAR_MAX + 1) * sizeof(char*));

	if(t != NULL)
	{
		for(i = 0; i <= UCHAR_MAX; i++)
		{
			t[i] = (char)i;
		}
		for(i = 0; i < 26; i++)
		{
			t[(size_t)dummystring[i]] = dummystring[dummyst = (i + 13) % 26];
			t[(size_t)dummystring[i + 26]] = dummystring[dummyst + 26];
		}
	}
	return t;
}



-- 
Pierre Maurette
Technical Details
Message-ID: <mn.1b207d5a87a7ade3.31483@laposte.net>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!infeed-1.proxad.net!news18-e.free.fr!not-for-mail
References: <1126342327.583568.21420@g43g2000cwa.googlegroups.com> <4329e5d4$0$7846$8fcfb975@news.wanadoo.fr> <dge4b6$fqa$1@reader1.imaginet.fr> <432ab809$0$1734$8fcfb975@news.wanadoo.fr> <mn.84b07d59016a857b.31483@laposte.net> <433f16ac$0$27406$8fcfb975@news.wanadoo.fr> <mn.10907d5a55119e84.15512@YOURBRAnoos.fr> <433f9cb1$0$7821$8fcfb975@news.wanadoo.fr> <mn.128c7d5aa5238de0.15512@YOURBRAnoos.fr> <43403203$0$5388$8fcfb975@news.wanadoo.fr>

Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,b21b26167a936e28
X-Google-Attributes: gidf9822,public
X-Google-Language: FRENCH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!infeed-1.proxad.net!news18-e.free.fr!not-for-mail
Subject: Re: rot13 [Was : Re: conjugaison et autres ...]
From: "Pierre Maurette" <maurettepierre@wanadoo.fr>
References: <1126342327.583568.21420@g43g2000cwa.googlegroups.com> <4329e5d4$0$7846$8fcfb975@news.wanadoo.fr> <dge4b6$fqa$1@reader1.imaginet.fr> <432ab809$0$1734$8fcfb975@news.wanadoo.fr> <mn.84b07d59016a857b.31483@laposte.net> <433f16ac$0$27406$8fcfb975@news.wanadoo.fr> <mn.10907d5a55119e84.15512@YOURBRAnoos.fr> <433f9cb1$0$7821$8fcfb975@news.wanadoo.fr> <mn.128c7d5aa5238de0.15512@YOURBRAnoos.fr> <43403203$0$5388$8fcfb975@news.wanadoo.fr>
Newsgroups: fr.comp.lang.c
Message-ID: <mn.1b207d5a87a7ade3.31483@laposte.net>
X-Newsreader: MesNews/1.04.00.00
Date: Mon, 03 Oct 2005 13:20:44 +0200
MIME-Version: 1.0
X-Face: cmi$3R1_J{[}5K|8d'LDiJ$SsZ*gn,j*PhJ#KVGt+45,E_N9O}GrhhQ>fb&q<&lz=g%PEv' u"m)N|KrBMux?2:E?N7Uw3L#H>L#GwM}<qH-?#_Y$B9]$q1"~iUGtN#HVy?}7.`m&iKF>gY:.`W.qu z`1'W'h-AXPmB]#7~<+@H.<0M`-jBkCWbmkcT_*LBVD3|p1a?h1H;cHq{$-vbeP@Hdcqp5z~lwU7ZK `~
Reply-To: maurettepierre@wanadoo.fr
Content-Type: text/plain; charset="iso-8859-15"; format=flowed
Content-Transfer-Encoding: 8bit
X-Antivirus: avast! (VPS 0539-6, 02/10/2005), Outbound message
X-Antivirus-Status: Clean
Lines: 162
Organization: Guest of ProXad - France
NNTP-Posting-Date: 03 Oct 2005 13:19:03 MEST
NNTP-Posting-Host: 84.7.244.246
X-Trace: 1128338343 news18-e.free.fr 1285 84.7.244.246:3466
X-Complaints-To: abuse@proxad.net