⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64.c

📁 Zoran V966 DVD 解码 Soc芯片的源程序
💻 C
字号:
// $Header: /I76/I76_Common/I76_Reference/Playcore/Nav_Clips/AviDrm/LibDrmCommon/base64.c 3     2/15/04 7:41p Lotan $
// Copyright (c) 2003 DivXNetworks, Inc. http://www.divxnetworks.com
// All rights reserved.
//
// This software is the confidential and proprietary information of DivxNetworks
// Inc. ("Confidential Information").  You shall not disclose such Confidential
// Information and shall use it only in accordance with the terms of the license
// agreement you entered into with DivXNetworks, Inc.

#include "Playcore\Nav_Clips\AviDrm\libDrmCommon\base64.h"
#include <string.h>
#include "Services\Include\_heap.h"

#ifdef AVI_DRM_SUPPORT


static CONST
char _base64Chars[]= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
				     'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
			         '0','1','2','3','4','5','6','7','8','9',
			         '-','_' };


int base64CharValue(char c)
{
    int i;

    for(i=0; i < 64; i++)
    {
        if(_base64Chars[i] == c)
        {
            return i;
        }
    }

    return -1;
}

void base64EncodeBlock(char *block, char *dst, int size)
{
	dst[0] = '=';
	dst[1] = '=';
	dst[2] = '=';
	dst[3] = '=';

    switch(size)
    {
    case 3:
        {
            int b1 = ((block[0] & 0xFC) >> 2);
            int b2 = ((block[0] & 0x03) << 4) | ((block[1] & 0xF0) >> 4);
            int b3 = ((block[1] & 0x0F) << 2) | ((block[2] & 0xC0) >> 6);
            int b4 = ((block[2] & 0x3F));

            dst[0] = _base64Chars[b1]; 
            dst[1] = _base64Chars[b2]; 
            dst[2] = _base64Chars[b3]; 
            dst[3] = _base64Chars[b4]; 

        }
        break;
    
    case 2:
        {
            int b1 = ((block[0] & 0xFC) >> 2);
            int b2 = ((block[0] & 0x03) << 4) | ((block[1] & 0xF0) >> 4);
            int b3 = ((block[1] & 0x0F) << 2);

            dst[0] = _base64Chars[b1]; 
            dst[1] = _base64Chars[b2]; 
            dst[2] = _base64Chars[b3]; 
            
        }
        break;
    
    case 1:
        {
            int b1 = ((block[0] & 0xFC) >> 2);
            int b2 = ((block[0] & 0x03) << 4);
            
            dst[0] = _base64Chars[b1]; 
            dst[1] = _base64Chars[b2]; 
        }
        break;
    }
}


void base64DecodeBlock(char *block, char *out)
{
    if((block[3] == '=') && (block[2] == '='))
    {
		int v1;
        int c1 = base64CharValue(block[0]);
        int c2 = base64CharValue(block[1]);

        if((c1 == -1) || (c2 == -1))
        {
            return;
        }

        v1 = (c1 << 2) | (c2 >> 4); 
        
		out[0] = (char) v1;
        out[1] = '\0';
    }
    else
    {
        if(block[3] == '=')
        {
			int v1, v2;
            int c1 = base64CharValue(block[0]);
            int c2 = base64CharValue(block[1]);
            int c3 = base64CharValue(block[2]);

            if((c1 == -1) || 
               (c2 == -1) ||
               (c3 == -1))
            {
                return;
            }

            v1 = (c1 << 2) | ((c2 & 0x30) >> 4); 
            v2 = ((c2 & 0x0F)<< 4) | ((c3 & 0x3C) >> 2); 
            
            out[0] = (char) v1;
            out[1] = (char) v2;
            out[2] = '\0';
        }
        else
        {
			int v1, v2, v3;
            int c1 = base64CharValue(block[0]);
            int c2 = base64CharValue(block[1]);
            int c3 = base64CharValue(block[2]);
            int c4 = base64CharValue(block[3]);

            if((c1 == -1) || 
               (c2 == -1) ||
               (c3 == -1) ||
               (c4 == -1))
            {
                return;
            }

            v1 = (c1 << 2) | ((c2 & 0x30) >> 4); 
            v2 = ((c2 & 0x0F)<< 4) | ((c3 & 0x3C) >> 2); 
            v3 = ((c3 & 0x03)<< 6) | ((c4 & 0x3F)); 

            out[0] = (char) v1;
            out[1] = (char) v2;
            out[2] = (char) v3;
        }
    }
}


char *base64Encode(char *buffer, 
				   unsigned int length, 
				   unsigned int *outlength)
{
	char *result;

	int i;
    int blocks = length / 3;
    int left   = length % 3;

	int size   = blocks*4 + (left != 0 ? 4 : 0);

	result = (char *) MEM_Allocate(SC_POOL, size);

    for(i=0; i < blocks; i++)
    {
		base64EncodeBlock(buffer + i*3, result + i*4, 3);
    }

    if(left > 0)
    {
		base64EncodeBlock(buffer + (blocks)*3, 
						  result + (blocks)*4,
						  left);
    }

	*outlength = size;

	return result;
}

void base64Decode(char *str,
				   unsigned int length,
                  unsigned int* outlength,
                  char* result)
{
    int i;
    int blocks   = length/4;
    int outSize  = blocks*3;
    int realSize = outSize;

    if(((length % 4) > 0) || 
	   ((length == 0)   ))
    {
        return;
    }

    if(str[length - 1] == '=')
    {
        realSize--;

        if(str[length - 2] == '=')
        {
            realSize--;
        }
    }

     for(i=0; i < blocks; i++)
    {
        base64DecodeBlock(str + 4*i, result + i*3);
    }

	*outlength = realSize;
   return;

}

//
//
//


static CONST  char base32[32] = {
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'M',
    'N',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z'
};

void base32Encode40Bits(unsigned char *input, char *output)
{
	unsigned char current = 0;
	unsigned char number = 0;
	unsigned char bits[5] = {0};
	unsigned char i = 0;
	unsigned char j = 0;
	unsigned char k = 0;
	unsigned char m = 0;

	current = input[0];

	for (i = 1; i < 41; i++) /* Index from 1 to make code easier. */
	{
		/* Peel off one bit, and store in array. */
		bits[m++] = current & 0x80;
		current = current << 1;

		/* Check to get new input character. */
		if (0 == (i % 8))
		{
			j++;
			if (j < 5) /* Handle boundary case, do not copy in new value at end. */
			{
				current = input[j];
			}
		}

		/* Check to get new output character */
		if (0 == (i % 5))
		{
			number = ((bits[0] >> 0) |
				      (bits[1] >> 1) |
					  (bits[2] >> 2) |
					  (bits[3] >> 3) |
					  (bits[4] >> 4)) >> 3;
			output[k] = base32[number];
			memset(bits, 0, 5);
			k++;
			m = 0;
		}
	}

	output[8] = '\0';
}

void base32Decode40Bits(char *input, unsigned char *output)
{
	unsigned char number = 0;
	unsigned char bits[40] = {0};
	unsigned char i = 0;
	char j = 0;
	unsigned char k = 0;
	unsigned char m = 0;

	for (i = 0; i < 8; i++)
	{
		for (j = 0; j < 32; j++)
		{
			if (base32[j] == input[i])
			{
				number = j;
				number = number << 3;
				for (k = 0; k < 5; k++)
				{
					if ( (number & 0x80) == 0x80)
					{
						bits[m] = 0x01;
					}
					m++;
					number = number << 1;
				}

				break;
			}
		}
	
	}

	m = 0;
	number = 0;
	for (i = 0; i < 5; i++)
	{
		for (j = 7; j >= 0; j--)
		{
			number = number | (bits[m++] << j);
		}
		output[i] = number;
		number = 0;
	}
}

#endif // AVI_DRM_SUPPORT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -