0

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 ?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 1
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – ChrisGPT was on strike Feb 15 '20 at 13:11
  • Not directly answering to your question, have you tried Python-C transpiler or compiler? For example, I use Nuitka and it gives a good performance boost in most cases. – DumTux Feb 15 '20 at 13:12
  • You cannot rewrite directly into C, because C has no "counted strings" (and the 4-bye fragment contains a NUL). So you'll need to keep track of the actual size-used of the char[] object. – wildplasser Feb 15 '20 at 13:50
  • `f = f[:c] + f[c+4:]` just erases substring "ROB". To do this in C, simple copy both parts into another string with enough size. Like `oldstr[c]=0; snprintf(newstr, size, "%s%s", oldstr, oldstr+4);` – Eddy_Em Feb 15 '20 at 14:26
  • If I understood right, it should be : `char *var[c] = 0; snprintf(f, sizeof(f), "%s%s", var, var+4);` ? (I wonder if `memcpy()` wouldn't be useful too ?) –  Feb 15 '20 at 14:56

1 Answers1

1

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
}
tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • @Lisbakc `memcpy(f, ROB, 4);` in particular would copy the first 4 bytes of `ROB` at the beginning of `f`. That may or may not be what you need, I can't tell. – tevemadar Feb 15 '20 at 16:55
  • A side comment: if `len` (length of the original sequence) may be something not divisible by 4, `for(int i=0;i – tevemadar Feb 15 '20 at 16:57