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

📄 decoder.c

📁 QR Code码的解码程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "decoder.h"
#include "rs.h"

// version format information
static unsigned char vfi[][15] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,0,1,1,0,1,1,1},
{0,0,0,1,0,1,0,0,1,1,0,1,1,1,0},
{0,0,0,1,1,1,1,0,1,0,1,1,0,0,1},
{0,0,1,0,0,0,1,1,1,1,0,1,0,1,1},
{0,0,1,0,1,0,0,1,1,0,1,1,1,0,0},
{0,0,1,1,0,1,1,1,0,0,0,0,1,0,1},
{0,0,1,1,1,1,0,1,0,1,1,0,0,1,0},
{0,1,0,0,0,1,1,1,1,0,1,0,1,1,0},
{0,1,0,0,1,1,0,1,1,1,0,0,0,0,1},
{0,1,0,1,0,0,1,1,0,1,1,1,0,0,0},
{0,1,0,1,1,0,0,1,0,0,0,1,1,1,1},
{0,1,1,0,0,1,0,0,0,1,1,1,1,0,1},
{0,1,1,0,1,1,1,0,0,0,0,1,0,1,0},
{0,1,1,1,0,0,0,0,1,0,1,0,0,1,1},
{0,1,1,1,1,0,1,0,1,1,0,0,1,0,0},
{1,0,0,0,0,1,0,1,0,0,1,1,0,1,1},
{1,0,0,0,1,1,1,1,0,1,0,1,1,0,0},
{1,0,0,1,0,0,0,1,1,1,1,0,1,0,1},
{1,0,0,1,1,0,1,1,1,0,0,0,0,1,0},
{1,0,1,0,0,1,1,0,1,1,1,0,0,0,0},
{1,0,1,0,1,1,0,0,1,0,0,0,1,1,1},
{1,0,1,1,0,0,1,0,0,0,1,1,1,1,0},
{1,0,1,1,1,0,0,0,0,1,0,1,0,0,1},
{1,1,0,0,0,0,1,0,1,0,0,1,1,0,1},
{1,1,0,0,1,0,0,0,1,1,1,1,0,1,0},
{1,1,0,1,0,1,1,0,0,1,0,0,0,1,1},
{1,1,0,1,1,1,0,0,0,0,1,0,1,0,0},
{1,1,1,0,0,0,0,1,0,1,0,0,1,1,0},
{1,1,1,0,1,0,1,1,0,0,1,0,0,0,1},
{1,1,1,1,0,1,0,1,1,0,0,1,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};


// center module of alignment patterns
static unsigned char cm_of_ap[][7] = { {}, {}, { 6, 18 }, { 6, 22 },
			{ 6, 26 }, { 6, 30 }, { 6, 34 }, { 6, 22, 38 }, { 6, 24, 42 },
			{ 6, 26, 46 }, { 6, 28, 50 }, { 6, 30, 54 }, { 6, 32, 58 },
			{ 6, 34, 62 }, { 6, 26, 46, 66 }, { 6, 26, 48, 70 },
			{ 6, 26, 50, 74 }, { 6, 30, 54, 78 }, { 6, 30, 56, 82 },
			{ 6, 30, 58, 86 }, { 6, 34, 62, 90 }, { 6, 28, 50, 72, 94 },
			{ 6, 26, 50, 74, 98 }, { 6, 30, 54, 78, 102 },
			{ 6, 28, 54, 80, 106 }, { 6, 32, 58, 84, 110 },
			{ 6, 30, 58, 86, 114 }, { 6, 34, 62, 90, 118 },
			{ 6, 26, 50, 74, 98, 122 }, { 6, 30, 54, 78, 102, 126 },
			{ 6, 26, 52, 78, 104, 130 }, { 6, 30, 56, 82, 108, 134 },
			{ 6, 34, 60, 86, 112, 138 }, { 6, 30, 58, 86, 114, 142 },
			{ 6, 34, 62, 90, 118, 146 }, { 6, 30, 54, 78, 102, 126, 150 },
			{ 6, 24, 50, 76, 102, 128, 154 }, { 6, 28, 54, 80, 106, 132, 158 },
			{ 6, 32, 58, 84, 110, 136, 162 }, { 6, 26, 54, 82, 110, 138, 166 },
			{ 6, 30, 58, 86, 114, 142, 170 } };

static unsigned char P [] = { 
		3, 2,  1,  1,  2,  0,  0,  0, 1,  0,  0,  0
};

static unsigned char num_of_ecb [160][2]  = {
			{ 1 }, { 1 }, { 1 }, { 1 },
			{ 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 1 }, { 2 }, { 2 }, { 1 },
			{ 2 }, { 2 }, { 4 }, { 1 }, { 2 }, { 2, 2 }, { 2, 2 }, { 2 },
			{ 4 }, { 4 }, { 4 }, { 2 }, { 4 }, { 2, 4 }, { 4, 1 }, { 2 },
			{ 2, 2 }, { 4, 2 }, { 4, 2 }, { 2 }, { 3, 2 }, { 4, 4 }, { 4, 4 },
			{ 2, 2 }, { 4, 1 }, { 6, 2 }, { 6, 2 }, { 4 }, { 1, 4 }, { 4, 4 },
			{ 3, 8 }, { 2, 2 }, { 6, 2 }, { 4, 6 }, { 7, 4 }, { 4 }, { 8, 1 },
			{ 8, 4 }, { 12, 4 }, { 3, 1 }, { 4, 5 }, { 11, 5 }, { 11, 5 },
			{ 5, 1 }, { 5, 5 }, { 5, 7 }, { 11, 7 }, { 5, 1 }, { 7, 3 },
			{ 15, 2 }, { 3, 13 }, { 1, 5 }, { 10, 1 }, { 1, 15 }, { 2, 17 },
			{ 5, 1 }, { 9, 4 }, { 17, 1 }, { 2, 19 }, { 3, 4 }, { 3, 11 },
			{ 17, 4 }, { 9, 16 }, { 3, 5 }, { 3, 13 }, { 15, 5 }, { 15, 10 },
			{ 4, 4 }, { 17 }, { 17, 6 }, { 19, 6 }, { 2, 7 }, { 17 },
			{ 7, 16 }, { 34 }, { 4, 5 }, { 4, 14 }, { 11, 14 }, { 16, 14 },
			{ 6, 4 }, { 6, 14 }, { 11, 16 }, { 30, 2 }, { 8, 4 }, { 8, 13 },
			{ 7, 22 }, { 22, 13 }, { 10, 2 }, { 19, 4 }, { 28, 6 }, { 33, 4 },
			{ 8, 4 }, { 22, 3 }, { 8, 26 }, { 12, 28 }, { 3, 10 }, { 2, 23 },
			{ 4, 31 }, { 11, 31 }, { 7, 7 }, { 21, 7 }, { 1, 37 }, { 19, 26 },
			{ 5, 10 }, { 19, 10 }, { 15, 25 }, { 23, 25 }, { 13, 3 },
			{ 2, 29 }, { 42, 1 }, { 23, 28 }, { 17 }, { 10, 23 }, { 10, 35 },
			{ 19, 35 }, { 17, 1 }, { 14, 21 }, { 29, 19 }, { 11, 46 },
			{ 13, 6 }, { 14, 23 }, { 44, 7 }, { 59, 1 }, { 12, 7 }, { 12, 26 },
			{ 39, 14 }, { 22, 41 }, { 6, 14 }, { 6, 34 }, { 46, 10 },
			{ 2, 64 }, { 17, 4 }, { 29, 14 }, { 49, 10 }, { 24, 46 },
			{ 4, 18 }, { 13, 32 }, { 48, 14 }, { 42, 32 }, { 20, 4 },
			{ 40, 7 }, { 43, 22 }, { 10, 67 }, { 19, 6 }, { 18, 31 },
			{ 34, 34 }, { 20, 61 }
			};
static unsigned char dae_per_block [160][4] = { { 26, 19 }, { 26, 16 },
			{ 26, 13 }, { 26, 9 }, { 44, 34 }, { 44, 28 }, { 44, 22 },
			{ 44, 16 }, { 70, 55 }, { 70, 44 }, { 35, 17 }, { 35, 13 },
			{ 100, 80 }, { 50, 32 }, { 50, 24 }, { 25, 9 }, { 134, 108 },
			{ 67, 43 }, { 33, 15, 34, 16 }, { 33, 11, 34, 12 }, { 86, 68 },
			{ 43, 27 }, { 43, 19 }, { 43, 15 }, { 98, 78 }, { 49, 31 },
			{ 32, 14, 33, 15 }, { 39, 13, 40, 14 }, { 121, 97 },
			{ 60, 38, 61, 39 }, { 40, 18, 41, 19 }, { 40, 14, 41, 15 },
			{ 146, 116 }, { 58, 36, 59, 37 }, { 36, 16, 37, 17 },
			{ 36, 12, 37, 13 }, { 86, 68, 87, 69 }, { 69, 43, 70, 44 },
			{ 43, 19, 44, 20 }, { 43, 15, 44, 16 }, { 101, 81 },
			{ 80, 50, 81, 51 }, { 50, 22, 51, 23 }, { 36, 12, 37, 13 },
			{ 116, 92, 117, 93 }, { 58, 36, 59, 37 }, { 46, 20, 47, 21 },
			{ 42, 14, 43, 15 }, { 133, 107 }, { 59, 37, 60, 38 },
			{ 44, 20, 45, 21 }, { 33, 11, 34, 12 }, { 145, 115, 146, 116 },
			{ 64, 40, 65, 41 }, { 36, 16, 37, 17 }, { 36, 12, 37, 13 },
			{ 109, 87, 110, 88 }, { 65, 41, 66, 42 }, { 54, 24, 55, 25 },
			{ 36, 12, 37, 13 }, { 122, 98, 123, 99 }, { 73, 45, 74, 46 },
			{ 43, 19, 44, 20 }, { 45, 15, 46, 16 }, { 135, 107, 136, 108 },
			{ 74, 46, 75, 47 }, { 50, 22, 51, 23 }, { 42, 14, 43, 15 },
			{ 150, 120, 151, 121 }, { 69, 43, 70, 44 }, { 50, 22, 51, 23 },
			{ 42, 14, 43, 15 }, { 141, 113, 142, 114 }, { 70, 44, 71, 45 },
			{ 47, 21, 48, 22 }, { 39, 13, 40, 14 }, { 135, 107, 136, 108 },
			{ 67, 41, 68, 42 }, { 54, 24, 55, 25 }, { 43, 15, 44, 16 },
			{ 144, 116, 145, 117 }, { 68, 42 }, { 50, 22, 51, 23 },
			{ 46, 16, 47, 17 }, { 139, 111, 140, 112 }, { 74, 46 },
			{ 54, 24, 55, 25 }, { 37, 13 }, { 151, 121, 152, 122 },
			{ 75, 47, 76, 48 }, { 54, 24, 55, 25 }, { 45, 15, 46, 16 },
			{ 147, 117, 148, 118 }, { 73, 45, 74, 46 }, { 54, 24, 55, 25 },
			{ 46, 16, 47, 17 }, { 132, 106, 133, 107 }, { 75, 47, 76, 48 },
			{ 54, 24, 55, 25 }, { 45, 15, 46, 16 }, { 142, 114, 143, 115 },
			{ 74, 46, 75, 47 }, { 50, 22, 51, 23 }, { 46, 16, 47, 17 },
			{ 152, 122, 153, 123 }, { 73, 45, 74, 46 }, { 53, 23, 54, 24 },
			{ 45, 15, 46, 16 }, { 147, 117, 148, 118 }, { 73, 45, 74, 46 },
			{ 54, 24, 55, 25 }, { 45, 15, 46, 16 }, { 146, 116, 147, 117 },
			{ 73, 45, 74, 46 }, { 53, 23, 54, 24 }, { 45, 15, 46, 16 },
			{ 145, 115, 146, 116 }, { 75, 47, 76, 48 }, { 54, 24, 55, 25 },
			{ 45, 15, 46, 16 }, { 145, 115, 146, 116 }, { 74, 46, 75, 47 },
			{ 54, 24, 55, 25 }, { 45, 15, 46, 16 }, { 145, 115 },
			{ 74, 46, 75, 47 }, { 54, 24, 55, 25 }, { 45, 15, 46, 16 },
			{ 145, 115, 146, 116 }, { 74, 46, 75, 47 }, { 54, 24, 55, 25 },
			{ 45, 15, 46, 16 }, { 145, 115, 146, 116 }, { 74, 46, 75, 47 },
			{ 54, 24, 55, 25 }, { 46, 16, 47, 17 }, { 151, 121, 152, 122 },
			{ 75, 47, 76, 48 }, { 54, 24, 55, 25 }, { 45, 15, 46, 16 },
			{ 151, 121, 152, 122 }, { 75, 47, 76, 48 }, { 54, 24, 55, 25 },
			{ 45, 15, 46, 16 }, { 152, 122, 153, 123 }, { 74, 46, 75, 47 },
			{ 54, 24, 55, 25 }, { 45, 15, 46, 16 }, { 152, 122, 153, 123 },
			{ 74, 46, 75, 47 }, { 54, 24, 55, 25 }, { 45, 15, 46, 16 },
			{ 147, 117, 148, 118 }, { 75, 47, 76, 48 }, { 54, 24, 55, 25 },
			{ 45, 15, 46, 16 }, { 148, 118, 149, 119 }, { 75, 47, 76, 48 },
			{ 54, 24, 55, 25 }, { 45, 15, 46, 16 } };

static char tAlpha[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";

static char code[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+-";

static unsigned char derotor[] = { 19, 60, 31, 1, 58, 2, 13, 18, 4, 52, 63,
			48, 20, 49, 15, 22, 9, 44, 8, 34, 29, 54, 51, 47, 46, 10, 11, 50,
			45, 32, 62, 36, 55, 61, 14, 25, 56, 12, 57, 6, 35, 27, 53, 16, 40,
			37, 0, 28, 42, 17, 7, 39, 38, 33, 5, 26, 3, 41, 43, 24, 21, 59, 23,
			30 };

void Cstr_init(Cstr* cs){
     cs->length = 0;
     cs->size = 30;
     cs->num = 1;
     cs->str = (char*)malloc(SCHAR * cs->size);
	 memset(cs->str, 0, cs->size);
};

void Cstr_appends(Cstr* cs ,char* str){
     int l=strlen(str);
     if((cs->length+l) >= (cs->size * cs->num)){
         cs->num = (cs->length+l)/cs->size+1;
         char *nstr = (char*)malloc(SCHAR * cs->size * cs->num);
         memset(nstr, '\0', cs->size * cs->num);
         strcpy(nstr, cs->str);
         strcat(nstr, str);
         free(cs->str);
         cs->str=nstr;
         cs->length = cs->length + l;
     }
     else{
         strcat(cs->str,str);
         cs->length = cs->length + l;
     }
};

void Cstr_appendc(Cstr* cs ,char c){
     if((cs->length+1) >= (cs->size * cs->num)){
         cs->num++;
         char *nstr = (char*)malloc(SCHAR * cs->size * cs->num);
         memset(nstr, '\0', cs->size * cs->num);
         strcpy(nstr, cs->str);
         *(nstr+cs->length)=c;
         *(nstr+cs->length+1)='\0';
         free(cs->str);
         cs->str=nstr;
         cs->length++;
     }
     else{
         *(cs->str+cs->length)=c;
         *(cs->str+cs->length+1)='\0';
         cs->length++;
     }
};

void Cstr_cleanup(Cstr* cs){
     free(cs->str);
}

int find_first(char* str ,char c){
    int i=0, l=strlen(str);
    for(i=0;i<l;i++){
        if(*(str+i)==c)
            return i;
    }
    return -1;
}

//10进制0-63 到 6位2进制 
void int64tobinary(char* bin, int data){
	int i;
	for(i=5;i>=0;i--){
		*(bin+i) = (char)((data&1) + 48);
		data = data >> 1;
	}
	*(bin+6)='\0';
//    printf("%s\n",bin);
}

//2位16进制 到 10进制 
char hextoint(char* hexString){
	char hex[] = "0123456789abcdef";
	char data = 0;
	data += find_first(hex, hexString[0]) << 4;
	data += find_first(hex, hexString[1]);
	return data;
}

//4n位2进制 到 16进制 
int binarytohex(Cstr* hexs ,char* bString) {
	int l = strlen(bString);
	if (l<1 || l % 4 != 0)
	    return 0;
	char hex[] = "0123456789abcdef";
	int iTmp=0;
	int i=0;
	for (i = 0; i < l; i += 4) {
		iTmp = 0;
		int j=0;
		for (j = 0; j < 4; j++) {
			iTmp += (*(bString+i+j) - 48)  << (4 - j - 1);
		}
		Cstr_appendc(hexs, hex[iTmp]);
	}
	return 1;
}

int getbit(unsigned char bytee, int bit){
	switch(bit){
	case 0:
		return bytee >> 7 & 1;
	case 1:
		return bytee >> 6 & 1;
	case 2:
		return bytee >> 5 & 1;
	case 3:
		return bytee >> 4 & 1;
	case 4:
		return bytee >> 3 & 1;
	case 5:
		return bytee >> 2 & 1;
	case 6:
		return bytee >> 1 & 1;
	case 7:
		return bytee & 1;
	}
	return 0;
}

int maskPattern(int y, int x, int maskpr) {
	switch (maskpr) {
	case 0:
		return ((x + y) % 2 == 0 ? 1 : 0);
	case 1:
		return (y % 2 == 0 ? 1 : 0);
	case 2:
		return (x % 3 == 0 ? 1 : 0);
	case 3:
		return ((x + y) % 3 == 0 ? 1 : 0);
	case 4:
		return ((y / 2 + x / 3) % 2 == 0 ? 1 : 0);
	case 5:
		return ((x * y) % 2 + (x * y) % 3 == 0 ? 1 : 0);
	case 6:
		return (((x * y) % 2 + (x * y) % 3) % 2 == 0 ? 1 : 0);
	case 7:
		return (((x + y) % 2 + (x * y) % 3) % 2 == 0 ? 1 : 0);
	}
	return 0;
}

void fillRect(char* iData, int iWidth, int cy, int cx, int n) {
	cx -= n / 2;
	cy -= n / 2;
	int i; 
	for (i = 0; i < n; i++) {
	    int j;
		for (j = 0; j < n; j++) {
			*(iData+(cy + i)*iWidth+(cx + j)) = -1;
		}
	}	
}

int isEncrypted(Cstr* text, char* pass) {
    char* ciphertext = text->str;
    int l=text->length;
	if (l < 2)
		return 0;
    int key = find_first(code, *ciphertext);
    if(key==-1) return 0;
    int bu = find_first(code, *(ciphertext+1));
    if(bu==-1) return 0;
    int i;
    char* btmp = (char*)malloc(SCHAR*l*6);
    memset(btmp, '\0', l*6);
    for (i = 2; i < l; i++) {
        int ciphercode = find_first(code, *(ciphertext+i));
	    if(ciphercode==-1) break;
	    char plaincode = (derotor[ciphercode] - key * (i - 2)) % 64;
	    if (plaincode < 0) plaincode += 64;
	    char bin[7];
	    int64tobinary(bin, plaincode);
	    strcat(btmp, bin);
    }
    if(i<l){
        free(btmp);
        return 0;
    }
    l=strlen(btmp+bu);
    if (l<1 || l%4!=0){
        free(btmp);
	    return 0;
	}
    Cstr ttmp;
    Cstr_init(&ttmp);
    binarytohex(&ttmp, btmp+bu);
    free(btmp);
	// password length
	l=strlen(ttmp.str);
	unsigned char pl = (*ttmp.str) - 48;
	if(pl<1||pl>6||1+pl>=l){
	    Cstr_cleanup(&ttmp);
	    return 0;
    }
	strncpy(pass, ttmp.str+1, pl);
	for(i=0;i<pl;i++)
	    if(!isdigit(*(pass+i))){
	        Cstr_cleanup(&ttmp);
	        pass[0]='\0';
	        return 0;
	    }
	// 检查2n位16进制
	l=strlen(ttmp.str+pl+1);
	if(l%2){
	    Cstr_cleanup(&ttmp);
	    pass[0]='\0';
	    return 0;
	}
    // text
	Cstr_init(text);
	l=ttmp.length;
	for (i = pl + 1; i < l; i += 2) {
		char bytee = hextoint(ttmp.str+i);
		Cstr_appendc(text, bytee);

⌨️ 快捷键说明

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