/**
* Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
* author: Kaige Li <lkg@rock-chips.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "httppost.h"
#include <curl/curl.h>
#include <string.h>
#include <sys/time.h>
#define POSTURL "http://121.40.93.5:7300/dbzf/router/rest?method=api/picture/upload"
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char * base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return base64;
}
int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )
temp[0]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+1] )
temp[1]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+2] )
temp[2]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+3] )
temp[3]= k;
}
bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
if ( base64[i+2] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
if ( base64[i+3] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
((unsigned char)(temp[3]&0x3F));
}
return j;
}
void encode(FILE * fp_in, FILE * fp_out)
{
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
bytes = fread( bindata, 1, 2049, fp_in );
base64_encode( bindata, base64, bytes );
fprintf( fp_out, "%s", base64 );
}
}
void decode(FILE * fp_in, FILE * fp_out)
{
int i;
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
for ( i = 0 ; i < 2048 ; i ++ )
{
base64[i] = fgetc(fp_in);
if ( base64[i] == EOF )
break;
else if ( base64[i] == '\n' || base64[i] == '\r' )
i --;
}
bytes = base64_decode( base64, bindata );
fwrite( bindata, bytes, 1, fp_out );
}
}
int HttpPostJpegString(char *buf,int buf_len)
{
FILE *pictureSrc;
pictureSrc=fopen("/tmp/webproject/pictureSrc.jpg","w+");
if (!pictureSrc) {
return -1;
}
fwrite(buf, 1, buf_len,pictureSrc);
fseek(pictureSrc,0L,SEEK_SET);
HttpPostJpegFile(pictureSrc);
fclose(pictureSrc);
}
int HttpPostJpegFile(FILE *fp)
{
CURL *curl;
long int flength,first;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";
char *didi;
FILE *pEncode;
pEncode=fopen("/tmp/webproject/pEncode.jpg","w+");
if (!pEncode) {
return -1;
}
fseek(fp,0L,SEEK_SET);
encode(fp, pEncode);
fseek(pEncode,0L,SEEK_SET);
first=ftell(pEncode);
fseek(pEncode,0L,SEEK_END);
flength=ftell(pEncode)-first;
fseek(pEncode,0L,SEEK_SET);
/*read the data and display it*/
didi=(char *) malloc(flength);
if(didi==NULL)
exit(1);
fread(didi,1,flength,pEncode);
fclose(pEncode);
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "imgBase64",
CURLFORM_COPYCONTENTS, didi, CURLFORM_END);
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL,POSTURL);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, flength);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 1000L);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_global_cleanup();
/* free slist */
curl_slist_free_all (headerlist);
}
free(didi);
return 0;
}
* Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
* author: Kaige Li <lkg@rock-chips.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "httppost.h"
#include <curl/curl.h>
#include <string.h>
#include <sys/time.h>
#define POSTURL "http://121.40.93.5:7300/dbzf/router/rest?method=api/picture/upload"
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char * base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return base64;
}
int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )
temp[0]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+1] )
temp[1]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+2] )
temp[2]= k;
}
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i+3] )
temp[3]= k;
}
bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
if ( base64[i+2] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
if ( base64[i+3] == '=' )
break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
((unsigned char)(temp[3]&0x3F));
}
return j;
}
void encode(FILE * fp_in, FILE * fp_out)
{
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
bytes = fread( bindata, 1, 2049, fp_in );
base64_encode( bindata, base64, bytes );
fprintf( fp_out, "%s", base64 );
}
}
void decode(FILE * fp_in, FILE * fp_out)
{
int i;
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
for ( i = 0 ; i < 2048 ; i ++ )
{
base64[i] = fgetc(fp_in);
if ( base64[i] == EOF )
break;
else if ( base64[i] == '\n' || base64[i] == '\r' )
i --;
}
bytes = base64_decode( base64, bindata );
fwrite( bindata, bytes, 1, fp_out );
}
}
int HttpPostJpegString(char *buf,int buf_len)
{
FILE *pictureSrc;
pictureSrc=fopen("/tmp/webproject/pictureSrc.jpg","w+");
if (!pictureSrc) {
return -1;
}
fwrite(buf, 1, buf_len,pictureSrc);
fseek(pictureSrc,0L,SEEK_SET);
HttpPostJpegFile(pictureSrc);
fclose(pictureSrc);
}
int HttpPostJpegFile(FILE *fp)
{
CURL *curl;
long int flength,first;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";
char *didi;
FILE *pEncode;
pEncode=fopen("/tmp/webproject/pEncode.jpg","w+");
if (!pEncode) {
return -1;
}
fseek(fp,0L,SEEK_SET);
encode(fp, pEncode);
fseek(pEncode,0L,SEEK_SET);
first=ftell(pEncode);
fseek(pEncode,0L,SEEK_END);
flength=ftell(pEncode)-first;
fseek(pEncode,0L,SEEK_SET);
/*read the data and display it*/
didi=(char *) malloc(flength);
if(didi==NULL)
exit(1);
fread(didi,1,flength,pEncode);
fclose(pEncode);
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "imgBase64",
CURLFORM_COPYCONTENTS, didi, CURLFORM_END);
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL,POSTURL);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, flength);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 1000L);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_global_cleanup();
/* free slist */
curl_slist_free_all (headerlist);
}
free(didi);
return 0;
}