i have a problem with the attached code. i wish to replace some delimiters with offsets(the amount of bytes between them). i have only 1 byte and if the offset is larger then 254 i wanna replace it with 255. in this case the next found offset should not be replaced. the replace part works fine... anyhow i can't recognize whether the last delimiter was replaced with 255 last time or not, it is skipped or converted and i dont know why/what am i doing wrong. Maybe anyone could give me any clue. here the code and at the end the output. i have marked (with **) the delimiter, that should not be replaced.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "zlib.h"
unsigned char *process(unsigned char *data, unsigned long size )
{
int i;
unsigned char *lastdel = data+1;
for (i=2;i<size; i++)
{
// if((unsigned char)*lastdel >254 )printf(" %c <#######-------\n", *lastdel);
//if(*lastdel == (unsigned char) 255 )printf(" %c <#######-------\n", *lastdel);
//if (!memcmp(lastdel,(unsigned char) 255, 1))
//printf(" %c <#######-------\n", *lastdel);
//if(*lastdel == (unsigned char) 255 )printf(" %c <#######-------\n", *lastdel);
//if(data[i] !='\x03' && data[i] !='\x1D' && data[i] !='\x1E'&& data[i] !='\x1C')continue;
//if(*lastdel >(unsigned char) 254 )printf(" %c <#######-------\n", *lastdel);
if(*lastdel >(unsigned char) 254 )printf(" %c <++++~~~++++++++#######-------\n", *lastdel);
if(data[i] =='\x03')
{
*lastdel = (&data[i] - lastdel-1 >255) ?(unsigned char) 255 : &data[i] - lastdel-1;
i+=2;
lastdel = &data[i];
continue;
}
if(data[i] =='\x1D')
{
*lastdel = (&data[i] - lastdel-1 >255) ?(unsigned char) 255 : &data[i] - lastdel-1;
i++;
lastdel = &data[i];
continue;
}
if(data[i] =='\x1E'|| data[i] =='\x1C')
{
if(*lastdel >(unsigned char) 254 )printf(" %c <++++~~~++++++++#######-------\n", *lastdel);
*lastdel = (&data[i] - lastdel-1 >255) ? (unsigned char) 255 : &data[i] - lastdel-1;
lastdel = &data[i];
if(*lastdel >(unsigned char) 254 )printf(" %c <++++~~~++++++++#######-------\n", *lastdel);
continue;
}
}
}
int main(void)
{
//readFile("/root/20101202.174715.std");
unsigned long s=290;
printf("hex: %x ", s<<24);
printf(" %x ", s<<16);
printf(" %x ", s<<8);
printf(" %x \n", s);
//exit(1);
int length ;
//unsigned char *bytes =readFile("/root/20101202.174715.std");
unsigned char bytes[] =
"\x31\x02\x00\x00\x67\x10\x00\x00\x00\x53\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x44\x45\x30\x30\x30\x4C\x53\x38\x30\x35\x38\x36\x2E\x4C\x55\x53\x1E\x35\x1C\x38\x2E\x1D\x1E\x32\x38\x1C\x30\x2E\x36\x32\x30\x30\x1E\x33\x30\x1C\x30\x2E\x35\x39\x30\x30\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x03\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x44\x45\x30\x30\x30\x53\x46\x44\x30\x5A\x32\x36\x2E\x4C\x55\x53\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x1E\x35\x1C\x38\x2E\x1D\x1E\x32\x38\x1C\x31\x30\x2E\x30\x35\x38\x30\x1E\x33\x30\x1C\x31\x30\x2E\x30\x32\x38\x30\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x03\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x45\x55\x52\x4E\x4F\x4B\x2E\x46\x58\x56\x57\x44\x1E\x35\x1C\x31\x30\x2E\x1D\x1E\x33\x30\x1C\x37\x2E\x39\x35\x37\x31\x1E\x32\x38\x1C\x37\x2E\x39\x36\x32\x36\x1E\x38\x30\x1C\x37\x2E\x39\x35\x39\x38\x35\x1E\x37\x35\x32\x1C\x5A\x4B\x42\x5A\x1E\x32\x38\x31\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x1E\x36\x34\x1C\x2D\x30\x2E\x30\x33\x39\x32\x1E\x31\x36\x34\x1C\x2D\x2E\x34\x38\x39\x34\x03\x45\x49\x00\x00\x00\x68\x10\x00\x00\x00\x53\x02\x1E\x31\x1C\x36\x33\x1E\x37\x34\x1C\x55\x1E\x38\x1C\x44\x45\x30\x30\x30\x39\x37\x36\x39\x37\x36\x30\x2E\x4C\x55\x53\x1E\x35\x1C\x31\x2E\x1D\x1E\x32\x38\x1C\x31\x30\x30\x2E\x38\x39\x30\x30\x1E\x33\x30\x1C\x31\x30\x30\x2E\x30\x39\x30\x30\x1E\x32\x36\x1C\x31\x35\x3A\x30\x33\x3A\x30\x34\x03\x45"
;
/* Speicher fuer String mit 4294967296 Zeichen reservieren */
unsigned char *data;
if (( data = ( unsigned char *) malloc(1000000000 * sizeof(unsigned char))) == NULL)
{
printf( "Nicht genug Speicher, um den Puffer zu allokieren\n");
exit(1);
}
printf( "Speicher fuer String wurde reserviert!\n" );
int strsize =((sizeof(bytes)/sizeof(*bytes))-1);
int pos =0;
int loop=1;
while(bytes[pos+9]=='S')
{
printf("----------------------------\n");
printf("| LOOP %d, POSITION %d |\n", loop, pos);
printf("----------------------------\n\n");
printf("hex: %x ", bytes[pos+0]<<24);
printf(" %x ", bytes[pos+1]<<16);
printf(" %x ", bytes[pos+2]<<8);
printf(" %x \n", bytes[pos+3]);
unsigned long size= ntohl(bytes[pos+0]<<24| bytes[pos+1]<<16| bytes[pos+2]<<8| bytes[pos+3]);
printf("=> RECOGNIZED SIZE: %lu \n", size );
printf("\n");
int seq = ntohl(bytes[pos+4]<<24| bytes[pos+5]<<16| bytes[pos+6]<<8| bytes[pos+7]);
printf("seq number: %d \n", seq );
printf("compressed: %d \n", bytes[pos+8]);
printf("start in hex: %x, in char: %c \n", bytes[pos+9], b开发者_如何学JAVAytes[pos+9] );
//printf("HeaderFieldIdentifier: %c %c %c \n", bytes[pos+10], bytes[pos+11], bytes[pos+12]);
printf("stop in hex: %x, in char: %c \n\n", bytes[pos+size+10], bytes[pos+size+10] );
/* Array kreieren das nur auf die komprimierten Daten zeigt */
unsigned char *ptr;
ptr = &bytes[pos+10]; // Position des 1. Bytes
bytes[pos+size+10]='\0'; // Ende des Strings festlegen
/* Jetzt entkomprimieren wir die Daten */
int ret=0;
unsigned long size_uncompress = size;//1000000000 * sizeof(unsigned char);
if( bytes[pos+8]=='\x01')
{
size_uncompress = 1000000000 * sizeof(unsigned char);
ret=uncompress(data, &size_uncompress, ptr, size);
data[size_uncompress]='\0';
}
else
data = ptr;
//data[size_uncompress]='\0';
printf("%d|%lu|%lu|%x|%x|%x|%x\n",ret,size,size_uncompress,ptr[0],ptr[1],bytes[pos+10],bytes[pos+11]);
printf("\n");
printf("----------------------------\n");
printf("| Message as String: |\n");
printf("----------------------------\n");
printf("%s\n", data);
int i =0;
printf("\n");
printf("----------------------------\n");
printf("| Message as hexadecimal |\n");
printf("----------------------------\n");
for (i=0;i<size_uncompress ;i++)
{
printf("%x ",data[i] );
}
printf("\n\n");
printf("----------------------------\n");
printf("| Edited message |\n");
printf("----------------------------\n");
process(data, size_uncompress);
for (i=0;i<size_uncompress ;i++)
{
printf("%x ",data[i] );
}
printf("\n");
pos+=(size+11);
loop++;
}
//free(data);
}
Output Orig. message
02 01 31 02 36 33 02 37 34 01 55 01 38 10 44 45 30 30 30 4c 53 38 30 35 38 36 2e 4c 55 53 01 35 02 38 2e 1d 02 32 38 06 30 2e 36 32 30 30 02 33 30 06 30 2e 35 39 30 30 02 32 36 08 31 35 3a 30 33 3a 30 34 03 02 01 31 02 36 33 02 37 34 01 55 01 38 ff 44 45 30 30 30 53 46 44 30 5a 32 36 2e 4c 55 53 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 **01** 35 02 38 2e 1d 02 32 38 07 31 30 2e 30 35 38 30 02 33 30 07 31 30 2e 30 32 38 30 02 32 36 08 31 35 3a 30 33 3a 30 34 03 02 01 31 02 36 33 02 37 34 01 55 01 38 c 45 55 52 4e 4f 4b 2e 46 58 56 57 44 01 35 03 31 30 2e 1d 02 33 30 06 37 2e 39 35 37 31 02 32 38 06 37 2e 39 36 32 36 02 38 30 07 37 2e 39 35 39 38 35 03 37 35 32 4 5a 4b 42 5a 03 32 38 31 8 31 35 3a 30 33 3a 30 34 02 32 36 8 31 35 3a 30 33 3a 30 34 02 36 34 07 2d 30 2e 30 33 39 32 03 31 36 34 06 2d 2e 34 38 39 34 3
Replaced message
02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 44 45 30 30 30 4C 53 38 30 35 38 36 2E 4C 55 53 1E 35 1C 38 2E 1D 1E 32 38 1C 30 2E 36 32 30 30 1E 33 30 1C 30 2E 35 39 30 30 1E 32 36 1C 31 35 3A 30 33 3A 30 34 03 02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 44 45 30 30 30 53 46 44 30 5A 32 36 2E 4C 55 53 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 **1E** 35 1C 38 2E 1D 1E 32 38 1C 31 30 2E 30 35 38 30 1E 33 30 1C 31 30 2E 30 32 38 30 1E 32 36 1C 31 35 3A 30 33 3A 30 34 03 02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 45 55 52 4E 4F 4B 2E 46 58 56 57 44 1E 35 1C 31 30 2E 1D 1E 33 30 1C 37 2E 39 35 37 31 1E 32 38 1C 37 2E 39 36 32 36 1E 38 30 1C 37 2E 39 35 39 38 35 1E 37 35 32 1C 5A 4B 42 5A 1E 32 38 31 1C 31 35 3A 30 33 3A 30 34 1E 32 36 1C 31 35 3A 30 33 3A 30 34 1E 36 34 1C 2D 30 2E 30 33 39 32 1E 31 36 34 1C 2D 2E 34 38 39 34 03 45 49 00 00 00 68 10 00 00 00 53 02 1E 31 1C 36 33 1E 37 34 1C 55 1E 38 1C 44 45 30 30 30 39 37 36 39 37 36 30 2E 4C 55 53 1E 35 1C 31 2E 1D 1E 32 38 1C 31 30 30 2E 38 39 30 30 1E 33 30 1C 31 30 30 2E 30 39 30 30 1E 32 36 1C 31 35 3A 30 33 3A 30 34
First you check if the not yet replaced previous separator (*lastdel) is 255, as it has not been replaced, not wonder the case never occurs. Then you replace *lastdel with 255, and after replacing you change value of lastdel pointer to the next position (not yet changed) and you check if that position was replaced and contains 255...
No wonder the case never occurs. Just put the test at the right place and it should work fine.
精彩评论