All pastes #481616 Raw Edit

Untitled

public text v1 · immutable
·published 2007-05-10 22:28 UTC
rendered paste body
void hextobin(unsigned char *ctext, unsigned char *btext)
{
   /* define buffers */
  unsigned char *itbuff;	/* temp buffer pointer, so we don't destroy ibuff */
  unsigned char *otbuff;	/* temp buffer pointer, so we don't destroy obuff */
  int i = 0;
  char *hexstring;			/* temp string used for strtol */
  int x = 0;

  /* allocate buffer memory and zero out */
  itbuff = calloc(sizeof(ctext)-1, sizeof(char));	/* set all pointers to start memory */
  otbuff = calloc(sizeof(btext)-1, sizeof(char));	/* set all pointers to start memory */
  hexstring = calloc(4, sizeof(char));

  strncpy(itbuff, ctext, 256);
  strncpy(hexstring, "0x", 2);	/* setup for hex conversion */

  printf("itbuff in conv: %s\n", itbuff);
  printf("itbuff len: %d\n", strlen(itbuff));

  /* convert ibuff to obuff, using itbuff and otbuff */
  for (i = 0 ; i < 128 ; i++) {
    hexstring[2] = itbuff[0];				/* grab first char */
    hexstring[3] = itbuff[1];				/* grab second char */
    sprintf(otbuff, "%c", strtol(hexstring, NULL, 16));	/* convert to long, then put it in obuff as char */
    (itbuff) += 2;					/* walk input buffer, 2 chars at a time */
    (otbuff)++;						/* walk output buffer, 1 char at a time */
  }

  /* copy the results */
  strcpy(btext, otbuff);
  printf("btext: %s\n", btext);

  /* make sure we do proper memory management! */
  itbuff = otbuff = NULL;
  free(otbuff);
  free(itbuff);
  free(hexstring);
}