void cb(struct evhttp_request *req, struct evbuffer *evb,void *ctx) { int ret, fd; struct stat st; void *pngdata; const char *txt; struct evkeyvalq args; evhttp_parse_query(req->uri, &args); txt = evhttp_find_header(&args, "txt"); ret = qrencode(txt); if (ret == 0) { fd = fileno(fp); fstat(fd, &st); pngdata = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); evhttp_add_header(req->output_headers, "content-type", "image/png"); evbuffer_add(evb, pngdata, st.st_size); evhttp_send_reply(req, HTTP_OK, "OK", evb); munmap(pngdata, st.st_size); } else { evhttp_send_reply(req, HTTP_SERVUNAVAIL, "ERROR", evb); } }
int benchmark (void) { initeccsize(1, size); memcpy(strinbuf, encode, size); initframe(); qrencode(); freeframe(); freeecc(); return 0; }
void QRcode::drawQRCode(int16_t xOffset, int16_t yOffset, String message) { // create QR code message.toCharArray((char *)strinbuf,260); qrencode(); // print QR Code for (byte x = 0; x < WD; x+=2) { for (byte y = 0; y < WD; y++) { if ( QRBIT(x,y) && QRBIT((x+1),y)) { // black square on top of black square render(x * scale + xOffset, y * scale + yOffset, foregroundColor); render((x+1) * scale + xOffset, y * scale + yOffset, foregroundColor); } if (!QRBIT(x,y) && QRBIT((x+1),y)) { // white square on top of black square render(x * scale + xOffset, y * scale + yOffset, backgroundColor); render((x+1) * scale + xOffset, y * scale + yOffset, foregroundColor); } if ( QRBIT(x,y) && !QRBIT((x+1),y)) { // black square on top of white square render(x * scale + xOffset, y * scale + yOffset, foregroundColor); render((x+1) * scale + xOffset, y * scale + yOffset, backgroundColor); } if (!QRBIT(x,y) && !QRBIT((x+1),y)) { // white square on top of white square render(x * scale + xOffset, y * scale + yOffset, backgroundColor); render((x+1) * scale + xOffset, y * scale + yOffset, backgroundColor); } } Serial.println(); } }
int main(int argc, char **argv) { int opt, lindex = -1; char *outfile = NULL; unsigned char *intext = NULL; int length = 0; while((opt = getopt_long(argc, argv, optstring, options, &lindex)) != -1) { switch(opt) { case 'h': if(lindex == 0) { usage(1, 1); } else { usage(1, 0); } exit(EXIT_SUCCESS); break; case 'o': outfile = optarg; break; case 's': size = atoi(optarg); if(size <= 0) { fprintf(stderr, "Invalid size: %d\n", size); exit(EXIT_FAILURE); } break; case 'v': version = atoi(optarg); if(version < 0) { fprintf(stderr, "Invalid version: %d\n", version); exit(EXIT_FAILURE); } break; case 'l': switch(*optarg) { case 'l': case 'L': level = QR_ECLEVEL_L; break; case 'm': case 'M': level = QR_ECLEVEL_M; break; case 'q': case 'Q': level = QR_ECLEVEL_Q; break; case 'h': case 'H': level = QR_ECLEVEL_H; break; default: fprintf(stderr, "Invalid level: %s\n", optarg); exit(EXIT_FAILURE); break; } break; case 'm': margin = atoi(optarg); if(margin < 0) { fprintf(stderr, "Invalid margin: %d\n", margin); exit(EXIT_FAILURE); } break; case 'd': dpi = atoi(optarg); if( dpi < 0 ) { fprintf(stderr, "Invalid DPI: %d\n", dpi); exit(EXIT_FAILURE); } break; case 't': if(strcasecmp(optarg, "png") == 0) { image_type = PNG_TYPE; } else if(strcasecmp(optarg, "eps") == 0) { image_type = EPS_TYPE; } else if(strcasecmp(optarg, "svg") == 0) { image_type = SVG_TYPE; } else if(strcasecmp(optarg, "ansi") == 0) { image_type = ANSI_TYPE; } else if(strcasecmp(optarg, "ansi256") == 0) { image_type = ANSI256_TYPE; } else if(strcasecmp(optarg, "asciii") == 0) { image_type = ASCIIi_TYPE; } else if(strcasecmp(optarg, "ascii") == 0) { image_type = ASCII_TYPE; } else if(strcasecmp(optarg, "utf8") == 0) { image_type = UTF8_TYPE; } else if(strcasecmp(optarg, "ansiutf8") == 0) { image_type = ANSIUTF8_TYPE; } else { fprintf(stderr, "Invalid image type: %s\n", optarg); exit(EXIT_FAILURE); } break; case 'S': structured = 1; break; case 'k': hint = QR_MODE_KANJI; break; case 'c': casesensitive = 1; break; case 'i': casesensitive = 0; break; case '8': eightbit = 1; break; case 'M': micro = 1; break; case 'f': if(color_set(fg_color, optarg)) { fprintf(stderr, "Invalid foreground color value.\n"); exit(EXIT_FAILURE); } break; case 'b': if(color_set(bg_color, optarg)) { fprintf(stderr, "Invalid background color value.\n"); exit(EXIT_FAILURE); } break; case 'V': usage(0, 0); exit(EXIT_SUCCESS); break; case 0: break; default: fprintf(stderr, "Try `qrencode --help' for more information.\n"); exit(EXIT_FAILURE); break; } } if(argc == 1) { usage(1, 0); exit(EXIT_SUCCESS); } if(outfile == NULL && image_type == PNG_TYPE) { fprintf(stderr, "No output filename is given.\n"); exit(EXIT_FAILURE); } if(optind < argc) { intext = (unsigned char *)argv[optind]; length = strlen((char *)intext); } if(intext == NULL) { intext = readStdin(&length); } if(micro && version > MQRSPEC_VERSION_MAX) { fprintf(stderr, "Version should be less or equal to %d.\n", MQRSPEC_VERSION_MAX); exit(EXIT_FAILURE); } else if(!micro && version > QRSPEC_VERSION_MAX) { fprintf(stderr, "Version should be less or equal to %d.\n", QRSPEC_VERSION_MAX); exit(EXIT_FAILURE); } if(margin < 0) { if(micro) { margin = 2; } else { margin = 4; } } if(micro) { if(version == 0) { fprintf(stderr, "Version must be specified to encode a Micro QR Code symbol.\n"); exit(EXIT_FAILURE); } if(structured) { fprintf(stderr, "Micro QR Code does not support structured symbols.\n"); exit(EXIT_FAILURE); } } if(structured) { if(version == 0) { fprintf(stderr, "Version must be specified to encode structured symbols.\n"); exit(EXIT_FAILURE); } qrencodeStructured(intext, length, outfile); } else { qrencode(intext, length, outfile); } return 0; }
void qrpng_internal (HV * options) { char * text; unsigned text_length; qr_t qr = {0}; qrpng_t qrpng = {0}; SV ** sv_ptr; qrpng_status_t qrpng_status; SV ** size_ptr; /* Get the text. This is assumed to exist. */ HASH_FETCH_PV (options, text); qr.input = text; qr.input_length = text_length; qr.level = 1; sv_ptr = hv_fetch (options, "level", strlen ("level"), 0); if (sv_ptr) { qr.level = SvUV (* sv_ptr); } if (qr.level < 1 || qr.level > 4) { croak ("Bad level %d; this is between 1 and 4", qr.level); } sv_ptr = hv_fetch (options, "version", strlen ("version"), 0); if (sv_ptr) { qr.version = SvUV (* sv_ptr); if (qr.version < 1 || qr.version > 40) { croak ("Bad version %d; this is between 1 and 40", qr.version); } initecc (& qr); } else { initeccsize (& qr); } initframe(& qr); qrencode (& qr); sv_ptr = hv_fetch (options, "quiet", strlen ("quiet"), 0); if (sv_ptr) { SV * quiet_sv; quiet_sv = * sv_ptr; qrpng.quietzone = SvUV (quiet_sv); } else { qrpng.quietzone = QUIETZONE; } sv_ptr = hv_fetch (options, "scale", strlen ("scale"), 0); if (sv_ptr) { SV * scale_sv; scale_sv = * sv_ptr; qrpng.scale = SvUV (scale_sv); } else { qrpng.scale = 3; } qrpng_status = qrpng_make_png (& qr, & qrpng); if (qrpng_status != qrpng_ok) { croak ("bad status %d from qrpng_make_png", qrpng_status); } sv_ptr = hv_fetch (options, "out_sv", strlen ("out_sv"), 0); if (sv_ptr) { /* Write it as a scalar. The code is copied out of Image::PNG::Libpng, but we don't depend on that. */ scalar_as_image_t si = {0}; png_set_write_fn (qrpng.png, & si, perl_png_scalar_write, 0 /* No flush function */); /* Write using our function. */ png_write_png (qrpng.png, qrpng.info, PNG_TRANSFORM_INVERT_MONO, NULL); /* Put the data into %options as $options{png_data}. */ (void) hv_store (options, "png_data", strlen ("png_data"), si.png_image, 0); } else { char * out; unsigned int out_length; HASH_FETCH_PV (options, out); qrpng.filename = out; qrpng_write (& qrpng); } size_ptr = hv_fetch (options, "size", strlen ("size"), 0); if (size_ptr) { // fprintf (stderr, "%s:%d: OK baby.\n", __FILE__, __LINE__); if (SvROK (* size_ptr) && SvTYPE (SvRV (* size_ptr)) < SVt_PVAV) { SV * sv = SvRV (* size_ptr); // fprintf (stderr, "%s:%d: OK baby.\n", __FILE__, __LINE__); sv_setuv (sv, (UV) qrpng.img_size); } } qrfree (& qr); qrpng_free (& qrpng); }
int main(int argc, char **argv) { int opt, lindex = -1; char *outfile = NULL; char *intext = NULL; while((opt = getopt_long(argc, argv, optstring, options, &lindex)) != -1) { switch(opt) { case 'h': if(lindex == 0) { usage(1, 1); } else { usage(1, 0); } exit(0); break; case 'o': outfile = optarg; break; case 's': size = atoi(optarg); if(size <= 0) { fprintf(stderr, "Invalid size: %d\n", size); exit(EXIT_FAILURE); } break; case 'v': version = atoi(optarg); if(version < 0) { fprintf(stderr, "Invalid version: %d\n", version); exit(EXIT_FAILURE); } break; case 'l': switch(*optarg) { case 'l': case 'L': level = QR_ECLEVEL_L; break; case 'm': case 'M': level = QR_ECLEVEL_M; break; case 'q': case 'Q': level = QR_ECLEVEL_Q; break; case 'h': case 'H': level = QR_ECLEVEL_H; break; default: fprintf(stderr, "Invalid level: %s\n", optarg); exit(EXIT_FAILURE); break; } break; case 'm': margin = atoi(optarg); if(margin < 0) { fprintf(stderr, "Invalid margin: %d\n", margin); exit(EXIT_FAILURE); } break; case 'S': structured = 1; case 'k': hint = QR_MODE_KANJI; break; case 'c': casesensitive = 1; break; case 'i': casesensitive = 0; break; case '8': eightbit = 1; break; case 'V': usage(0, 0); exit(0); break; default: fprintf(stderr, "Try `qrencode --help' for more information.\n"); exit(EXIT_FAILURE); break; } } if(argc == 1) { usage(1, 0); exit(0); } if(outfile == NULL) { fprintf(stderr, "No output filename is given.\n"); exit(EXIT_FAILURE); } if(optind < argc) { intext = argv[optind]; } if(intext == NULL) { intext = readStdin(); } if(structured) { if(version == 0) { fprintf(stderr, "Version must be specified to encode structured symbols.\n"); exit(EXIT_FAILURE); } qrencodeStructured(intext, outfile); } else { qrencode(intext, outfile); } return 0; }
int generate_key (char ** const result, const unsigned char qflag, const unsigned char rflag, const unsigned char eflag, const unsigned char uflag, const unsigned char sflag, const char * dvalue, const char * ovalue, const char * tvalue, const char * pvalue, const char * kvalue) { int ret = 0; int i = 0; unsigned char tvalue_uchar = 0; if (tvalue==0) { tvalue_uchar=0; } else { tvalue_uchar=(unsigned char)(atoi(tvalue)); } if (uflag==1) { publen = 65; } result[0] = (char *)malloc(200); result[1] = (char *)malloc(100); strcpy(result[0],""); strcpy(result[1],""); if (rflag == 1) { if (eflag != 1) { strcpy(result[0],"A private key is needed to decrypt\n"); return(1); } unsigned char * privkey = malloc(privlen); int randomData = open("/dev/random", O_RDONLY); size_t randomDataLen = 0; while (randomDataLen < privlen) { ssize_t res = read(randomData, privkey + randomDataLen, privlen - randomDataLen); if (res < 0) { strcpy(result[0],"Cannot read /dev/random\n"); return(1); } randomDataLen += res; } close(randomData); /* Get the address twice and compare */ char ** bc_address = (char **)malloc(sizeof(char *)*2); char ** pubkey_bc = (char **)malloc(sizeof(char *)*2); for (i=0; i<2; i++) { unsigned char * pubkey = malloc(publen); ret = priv_to_pub(pubkey,publen,privkey,privlen); pubkey_bc[i] = malloc(35); ret = pubkey_to_bc_format(pubkey_bc[i],pubkey,publen,tvalue_uchar); if (ret==-1) { strcpy(result[0],"Cannot convert public key to address\n"); return(1); } bc_address[i] = pubkey_bc[i] + ret; free(pubkey); } i = 0; do { if (bc_address[0][i] != bc_address[1][i]) { strcpy(result[0],"Address generations inconsistent\n"); return(1); } i++; } while (bc_address[0][i] != '\0'); strcpy(result[0],bc_address[0]); if (pvalue != 0) { int passlen = strlen(pvalue); if (passlen > 42) { strcpy(result[0],"The passphrase cannot be longer than 42 characters\n"); return(1); } if (passlen==0) { strcpy(result[0],"The passphrase cannot be empty\n"); return(1); } for (i=0;i<passlen;i++) { if (indexof(pvalue[i],b64chars)==-1) { strcpy(result[0],"The passphrase may only contain letters, numbers, spaces, and hyphens\n"); return(1); } } if (pvalue[0] == ' ') { strcpy(result[0],"The first character of the passphrase cannot be the space character\n"); return(1); } unsigned char * password_uchar = (unsigned char *)malloc(privlen+1); ret = b64_to_uchar(password_uchar,privlen,pvalue,strlen(pvalue),1); if (ret < 0) { strcpy(result[0],"Cannot parse passphrase\n"); return(1); } unsigned char * privkey_encrypted= (unsigned char *)malloc(privlen); ret = add_uchars(privkey_encrypted,privkey,password_uchar,privlen); unsigned char * privkey_decrypted = (unsigned char *)malloc(privlen); ret = subtract_uchars(privkey_decrypted,privkey_encrypted,password_uchar,privlen); for (i=0;i<privlen;i++) { if (privkey_decrypted[i] != privkey[i]) { strcpy(result[0],"Cannot encrypt private key\n"); return(1); } } free(privkey_decrypted); free(password_uchar); free(privkey); privkey = privkey_encrypted; } /* Get the private key twice and compare */ char ** bc_privkey = (char **)malloc(sizeof(char *)*2); char ** privkey_bc = (char **)malloc(sizeof(char *)*2); for (i=0; i<2; i++) { privkey_bc[i] = (char *)malloc(53); ret = privkey_to_bc_format(privkey_bc[i],privkey,privlen,0,0); if (ret==-1) { strcpy(result[0],"Cannot convert private key to the Bitcoin format\n"); return(1); } else if (ret==-2) { strcpy(result[0],"Private key does not match public key\n"); return(1); } bc_privkey[i] = privkey_bc[i] + ret; } i = 0; do { if (bc_privkey[0][i] != bc_privkey[1][i]) { strcpy(result[0],"Private key generations inconsistent\n"); return(1); } i++; } while (bc_privkey[0][i] != '\0'); strcpy(result[1],bc_privkey[0]); if (qflag == 1) { ret = qrencode(bc_address[0],bc_privkey[0],dvalue,ovalue,pvalue,eflag,sflag); if (ret!=0) { strcpy(result[0],"Cannot qr-encode\n"); return(1); } } free(privkey); free(pubkey_bc[0]); free(pubkey_bc[1]); free(pubkey_bc); free(privkey_bc[0]); free(privkey_bc[1]); free(privkey_bc); free(bc_address); free(bc_privkey); } else if (kvalue != 0) { unsigned char * privkey_1_full = malloc(privlen+7); int privkey_offset = b58_to_uchar(privkey_1_full,privlen+6,kvalue,strlen(kvalue),1); if (privkey_offset < 0) { strcpy(result[0],"Cannot parse private key\n"); return(1); } unsigned char * privkey_1 = privkey_1_full + privkey_offset; unsigned char * hash1 = malloc(32); SHA256(privkey_1,privlen+1+1-privkey_offset,hash1); unsigned char * hash2 = malloc(32); SHA256(hash1,32,hash2); privkey_1 += 1; unsigned char * privkey_1_hash = privkey_1+privlen+1-privkey_offset; for (i=0;i<4;i++) { if (privkey_1_hash[i]!=hash2[i]) { strcpy(result[0],"Invalid private key\n"); return(1); } } unsigned char * privkey_2 = (unsigned char *)malloc(privlen); if (pvalue == 0) { memcpy(privkey_2,privkey_1,privlen); for (i=0; i<privlen; i++) { if (privkey_2[i] != privkey_1[i]) { strcpy(result[0],"Cannot copy private key\n"); return(1); } } } else { int passlen = strlen(pvalue); if (passlen > 42) { strcpy(result[0],"The passphrase cannot be longer than 42 characters\n"); return(1); } if (passlen==0) { strcpy(result[0],"The passphrase cannot be empty\n"); return(1); } for (i=0;i<passlen;i++) { if (indexof(pvalue[i],b64chars)==-1) { strcpy(result[0],"The passphrase may only contain letters, numbers, spaces, and hyphens\n"); return(1); } } if (pvalue[0] == ' ') { strcpy(result[0],"The first character of the passphrase cannot be a space\n"); return(1); } unsigned char * password_uchar = (unsigned char *)malloc(privlen+1); ret = b64_to_uchar(password_uchar,privlen,pvalue,strlen(pvalue),1); if (ret < 0) { strcpy(result[0],"Cannot parse passphrase\n"); return(1); } unsigned char * privkey_1_check = (unsigned char *)malloc(privlen); if (eflag == 1) { ret = add_uchars(privkey_2,privkey_1,password_uchar,privlen); ret = subtract_uchars(privkey_1_check,privkey_2,password_uchar,privlen); for (i=0; i<privlen; i++) { if (privkey_1[i] != privkey_1_check[i]) { strcpy(result[0],"Cannot add passphrase to private key\n"); return(1); } } } else { ret = subtract_uchars(privkey_2,privkey_1,password_uchar,privlen); ret = add_uchars(privkey_1_check,privkey_2,password_uchar,privlen); for (i=0; i<privlen; i++) { if (privkey_1[i] != privkey_1_check[i]) { strcpy(result[0],"Cannot subtract passphrase from private key\n"); return(1); } } } free(privkey_1_check); free(password_uchar); } if (privkey_offset == 1) { publen = 65; } else { publen = 33; } /* Get the address twice and compare */ char ** bc_address = (char **)malloc(sizeof(char *)*2); char ** pubkey_bc = (char **)malloc(sizeof(char *)*2); for (i=0; i<2; i++) { unsigned char * pubkey = malloc(publen); if (eflag == 1) { ret = priv_to_pub(pubkey,publen,privkey_1,privlen); } else { ret = priv_to_pub(pubkey,publen,privkey_2,privlen); } pubkey_bc[i] = malloc(35); ret = pubkey_to_bc_format(pubkey_bc[i],pubkey,publen,tvalue_uchar); if (ret==-1) { strcpy(result[0],"Cannot convert public key to address\n"); return(1); } bc_address[i] = pubkey_bc[i] + ret; free(pubkey); } i = 0; do { if (bc_address[0][i] != bc_address[1][i]) { strcpy(result[0],"Address generations inconsistent\n"); return(1); } i++; } while (bc_address[0][i] != '\0'); strcpy(result[0],bc_address[0]); /* Get the private key twice and compare */ char ** bc_privkey = (char **)malloc(sizeof(char *)*2); char ** privkey_bc = (char **)malloc(sizeof(char *)*2); for (i=0; i<2; i++) { privkey_bc[i] = (char *)malloc(53); ret = privkey_to_bc_format(privkey_bc[i],privkey_2,privlen,0,0); if (ret==-1) { strcpy(result[0],"Cannot convert private key to the Bitcoin format\n"); return(1); } else if (ret==-2) { strcpy(result[0],"Private key does not match public key\n"); return(1); } bc_privkey[i] = privkey_bc[i] + ret; } do { if (bc_privkey[0][i] != bc_privkey[1][i]) { strcpy(result[0],"Private key generations inconsistent\n"); return(1); } i++; } while (bc_privkey[0][i] != '\0'); strcpy(result[1],bc_privkey[0]); if (qflag == 1) { ret = qrencode(bc_address[0],bc_privkey[0],dvalue,ovalue,pvalue,eflag,sflag); if (ret!=0) { strcpy(result[0],"Cannot qr-encode\n"); return(1); } } free(privkey_1_full); free(privkey_2); free(hash1); free(hash2); free(pubkey_bc[0]); free(pubkey_bc[1]); free(pubkey_bc); free(privkey_bc[0]); free(privkey_bc[1]); free(privkey_bc); free(bc_address); free(bc_privkey); } return(0); }
int main(int argc, char *argv[]) { unsigned char x, y, v = 0, l = 1; char *c; unsigned width, height, j, k; int argp = 0; if( argc < 2 ) { printf( "Usage:\n\nqrjpeg [-v VERS_1_40] [-l ECCLVL_1_4] \"text to encode\" >output.jpg\n" ); printf( "version defaults to auto, same as \"-v 0\"\necc level defaults to 1\n" ); return 1; } c = "Test Message"; while( ++argp < argc ) { if( argv[argp][0] == '-' ) { if( argv[argp][1] == 'v' ) v = atoi( argv[++argp]); else if( argv[argp][1] == 'l' ) l = atoi( argv[++argp]); else { printf( "Usage:\n\nqrjpeg [-v VERS_1_40] [-l ECCLVL_1_4] \"text to encode\" >output.jpg\n" ); printf( "version defaults to auto, same as \"-v 0\"\necc level defaults to 1\n" ); return 1; } } else c = argv[argp]; } if( v > 40 ) { fprintf( stderr, "Bad version (size) parameter (should be 0 (auto) to 40)\n" ); return -1; } if( l < 1 || l > 4 ) { fprintf( stderr, "Bad ECC level parameter (should be 1 to 4)\n" ); return -1; } if( v ) k = initecc(l, v); else k = initeccsize( l, strlen(c)); initframe(); strcpy((char *)strinbuf, c ); qrencode(); width = height = WD+8; // set height and width in header jpeg0[0x5e] = width >> 5; jpeg0[0x5f] = width << 3; jpeg0[0x60] = width >> 5; jpeg0[0x61] = width << 3; // write out header fwrite(jpeg0, 1, sizeof(jpeg0), stdout); // put half full scale, 3e for white, 40 for black putchar(0x40); for (j = 0; j < width * 4 + 3; j++) putchar(0x80); for (y = 0; y < WD; y++) { k = 0; for (x = 0; x < WD; x++) { j = QRBIT(x, y); if (k == j) { putchar(0x80); // code for no change continue; } putchar(j ? 0 : 0x7e); // full scale flip k = j; } if (k != 0) putchar(0x7e); else putchar(0x80); for (j = 0; j < 7; j++) putchar(0x80); } for (j = 0; j < width * 4 - 4; j++) putchar(0x80); putchar(0x80); // one last for EOF putchar(0xFF); // end marker putchar(0xd9); return 0; }