understanding 2 functions.

hi guys , i got some problem with understanding 2 functions in C/C++ source.
i'm php programmer and i'm not so good with C/C++ codes . i just know some basic of it .

here are 2 functions i try to figured it out .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static inline unsigned char decode(char** p, unsigned int* l) {
 	  unsigned char c;
 	  if (*l == 0) {
 	    zend_bailout();
 	  }
 	  c = **p;
 	  (*p)++;
 	  (*l)--;
	  return c;
 	}
	
 static inline unsigned int decode32(char** p, unsigned int* l) {
 	  unsigned int i = decode(p, l);
 	  i += ((unsigned int)decode(p, l)) << 8;
	  i += ((unsigned int)decode(p, l)) << 16;
 	  i += ((unsigned int)decode(p, l)) << 24;
 	  return i;
 	}


what does exaclyt these 2 functions do ?

Thanks for any help.
closed account (jy8CpfjN)
double ** are bad practice mostly when not applied to the right projects you have to know what your doing .as for beginners its best not practice double pointers could cause era it is best to use /* *\ for comments.
Last edited on
What? Why are pointers-to-pointers "bad?" (I'm assuming that's what you mean, because he doesn't have any pointers to doubles).

If you've got nothing to say except "double* are bad" (which isn't necessarily try), don't say anything at all.

On topic:
========

Without know what "zend_bailout()" does, I can't really tell. Didn't the original author of that code write a comment at the top, explaining what the function does? I hate lazy people... Personally, whever I write a function, I write a comment before it. Example:
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * die: print a given message to the screen, and then call exit with status @status
 * @message: the message to be printed
 * @status: the status to exit with.
 *
 * die does not return.
 */
void die(const char* message, int status) {
    printf("%s\n", message);

    exit(status);
}
It looks like you have two things here:

1) a stream of bytes
2) a size or 'count' of how many bytes are remaining in that stream

decode does this:
- contrary to what its name implies, it doesn't actually decode anything
- if there are no more bytes remaining in the stream, it calls zend_bailout (whatever that is)
- it returns the next byte in the stream
- it then increments the stream pointer to point to the next byte, and decrements the remaining count so the next call will return the next byte in the stream.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
char buffer[] = {100,50,25};

char* myptr = buffer; // pointer to the buffer
int size = sizeof(buffer);  // number of bytes in the buffer

cout << decode( &myptr, &size ) << "\n"  // outputs '100'
  // myptr is now += 1
  // size is now -= 1

cout << decode( &myptr, &size ) << "\n"  // outputs '50'
cout << decode( &myptr, &size ) << "\n"  // outputs '25'
cout << decode( &myptr, &size ) << "\n"  // calls zend_bailout() (and returns garbage?) 


decode32 simply calls decode 4 times and combines all 4 returned values into one 32-bit number (in little endian form)

Example:

1
2
3
4
5
6
7
8
9
10
11
12
char buffer[] = { 0x11,0x22,0x33,0x44,  0x99,   0x55,0x66,0x77,0x88 };

char* myptr = buffer;
int size = sizeof(buffer);

cout << hex << decode32( &myptr, &size ) << "\n"  // outputs "44332211"

cout << hex << decode( &myptr, &size ) << "\n"  // outputs "99"

cout << hex << decode32( &myptr, &size ) << "\n"  // outputs "88776655"

decode( &myptr, &size );  // zend_bailout() 
Topic archived. No new replies allowed.