Exemplo n.º 1
0
DLLEXPORT_TEST_FUNCTION short Base64Decode(char *source, char *target, int targetLen)
{
	char *src, *tmpptr;
    char quadruple[4], tmpresult[3];
    int i, tmplen = 3;
    size_t converted = 0;

    /* concatinate '===' to the source to handle unpadded base64 data */
	src = (char *)MemoryManager_Alloc(strlen(source)+5);
    if (src == NULL)
	{
		return FALSE;	
	}	
    strcpy(src, source);
    strcat(src, "====");
    tmpptr = src;

    /* convert as long as we get a full result */
    while (tmplen == 3)
    {
		/* get 4 characters to convert */
		for (i=0; i<4; i++)
		{
			/* skip invalid characters - we won't reach the end */
			while (*tmpptr != '=' && _base64_char_value(*tmpptr)<0)
			{
				tmpptr++;
			}

			quadruple[i] = *(tmpptr++);
		}

		/* convert the characters */
		tmplen = _base64_decode_triple(quadruple, tmpresult);

		/* check if the fit in the result buffer */
		if (targetLen < tmplen)
		{
			MemoryManager_Free(src);
			return FALSE;
		}

		/* put the partial result in the result buffer */		
		MemoryManager_MemCpy(target, tmpresult, tmplen);
		target += tmplen;
		targetLen -= tmplen;
		converted += tmplen;
    }

	MemoryManager_Free(src);
    return TRUE;
	
}
Exemplo n.º 2
0
/**
 * decode base64 encoded data
 *
 * @param source the encoded data (zero terminated)
 * @param target pointer to the target buffer
 * @param targetlen length of the target buffer
 * @return length of converted data on success, -1 otherwise
 */
size_t base64_decode(char *source, unsigned char **target, size_t targetlen)
{
  char *src, *tmpptr;
  unsigned char *target_walk = *target;
  char quadruple[4], tmpresult[3];
  unsigned int i, tmplen = 3;
  size_t converted = 0;

  /* concatinate '===' to the source to handle unpadded base64 data */
  src = (char *)malloc(strlen(source)+5);
  if (src == NULL)
    return -1;
  strcpy(src, source);
  strcat(src, "====");
  tmpptr = src;

  /* convert as long as we get a full result */
  while (tmplen == 3)
  {
    /* get 4 characters to convert */
    for (i=0; i<4; i++)
    {
      /* skip invalid characters - we won't reach the end */
      while (*tmpptr != '=' && _base64_char_value(*tmpptr)<0)
        tmpptr++;

      quadruple[i] = *(tmpptr++);
    }

    /* convert the characters */
    tmplen = _base64_decode_triple(quadruple, (unsigned char *)tmpresult);

    /* check if the fit in the result buffer */
    if (targetlen < tmplen) {
      free(src);
      return -1;
    }

    /* put the partial result in the result buffer */
    memcpy(target_walk, tmpresult, tmplen);
    target_walk += tmplen;
    targetlen -= tmplen;
    converted += tmplen;
  }

  *target = realloc(*target, converted);

  free(src);
  return converted;
}