I've been trying to understand the following Python program to translate it in C, but I meet some difficulties, here it is :
ROB = '\x00\xA1\x0F\xC0'
def my_function(f):
for c in range(0,len(f),4):
if f[c:c+4] == ROB:
f = f[:c] + f[c+4:] # What does it even means ???
print '%p' % c
return f
print 'ERR'
sys.exit(-1)
I mean, I understood the most of the function but one of these lines is literally hard to translate (I talk about f[:c] + f[c+4:]
) ...
Does someone know what this line is doing or how it should looks like in C ?
Those are "slices", and the +
simply concatenates them. This is the simplest case of slicing, [start:end]
creates a subsequence of a sequence, starting from position start
inclusive, ending at position end
, exclusive. If one of them is missing (but the :
is there), it is considered to be the beginning/end of the complete sequence. Indices are zero based.
Examples with string:
'Hello World'[1:5]
: 'ello'
'Hello World'[:5]
: 'Hello'
'Hello World'[1:]
: 'ello World'
'Hello World'[:1]+'Hello World'[5:]
: 'H World'
Your loop steps through your data in 4-byte steps, comparing the 4-byte chunk at the current position with the sequence you have in ROB
, and it returns a new sequence without it when it is found (so the f=...
part does not actually modify the existing sequence).
In C you could do that with memcmp
, then malloc
and a pair of memcpy
calls, as you correctly noticed that this data is binary (contains null-characters), so string functions would likely break it.
Hopefully I did not messed up the indices, and then it could look like this:
void* my_function(const void* f, int len){
for(int i=0;i<len;i+=4)
if(memcmp(f+i,ROB,4)==0){
void* ret=malloc(len-4);
memcpy(ret,f,i);
memcpy(ret+i,f+i+4,len-i-4);
return ret;
}
return NULL; // or have it die somehow
}