Re: [sqlite] BLOB question

2007-02-23 Thread Cesar Rodas

good idea!! i didn't think in this!!

On 23/02/07, Martin Jenkins <[EMAIL PROTECTED]> wrote:

Cesar Rodas wrote:
>while ( (n=fread(buff,Buffsize,1,file)) > 0)
>{
>if (i>0)
>*value = realloc(*value, (i+1) * Buffsize);
>memcpy(*value + (i * Buffsize), buff,  Buffsize);
>*len += n;
>i++;
>}

An afterthought, why don't you just stat the file and malloc the right
sized buffer from the outset? Much easier.

Martin

-
To unsubscribe, send email to [EMAIL PROTECTED]
-





--
Cesar Rodas
http://www.sf.net/projects/pagerank (The PageRank made easy...)
http://www.sf.net/projects/fastfs ( The Fast File System)
Mobile Phone: 595 961 974165
Phone: 595 21 645590
[EMAIL PROTECTED]
[EMAIL PROTECTED]

-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] BLOB question

2007-02-23 Thread John Stanton
I would make your routine more like this.  It avoids the needless buffer 
shadowing you get by using fopen and also avoids unecessary mallocs.


I haven't tested the code, it just presents the method.

  unsigned char *bp;
  int   fd;
  long  fsize;

  fd = open(path, O_RDWR);  /*Get handle to file.*/
  if (fd < 0) return E_IO_COULDNT_OPEN;

  fsize = lseek(fd, 0, SEEK_END);   /*Gives file size.*/

  madvise(bp, fsize, MADV_SEQUENTIAL);  /*May make it run better.*/

  /*Map file into local addressing space.*/
  bp = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd,  0);

  close(fd);   /*Don't need the handle noe.*/

  /*Place bind of bp to BLOB here usinjg sqlite3_bind_blob.*/
  /**/

  /*Execute SQL here using sqlite3_step.*/
  /**/

  /*Reset sql statement.*/
  /**/

  unmap(bp); /*Release file from local addressing space.*/

>
> uchar * value;
> char *query;
> long len;
>
> if ( LoadFileContent("file.jpeg",, ) != 0)
> exit();
>
> query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value); /* 
here i

> have a segmentation fault! */
>
Cesar Rodas wrote:

#define E_IO_COULDNT_OPEN - 1
#define E_IO_ERRO -2
#define uchar unsigned char *

int LoadFileContent(uchar *path, uchar ** value, long * len)
{
   #define Buffsize 1024*4

   int n;
   long i=0;
   FILE *file;
   unsigned char buff[Buffsize];

   if ( (file=fopen(path,"rb"))==0)
   {
   return E_IO_COULDNT_OPEN;
   }

   *value = malloc(Buffsize);
   *len = 0;
   fseek(file,0L,SEEK_SET);
   while ( (n=fread(buff,Buffsize,1,file)) > 0)
   {
   if (i>0)
   *value = realloc(*value, (i+1) * Buffsize);
   memcpy(*value + (i * Buffsize), buff,  Buffsize);
   *len += n;
   i++;
   }
   if (n==-1)
   {
   fclose(file);
   return E_IO_ERRO;
   }

   fclose(file);
   return 0;
}


uchar * value;
char *query;
long len;

if ( LoadFileContent("file.jpeg",, ) != 0)
exit();

query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value); /* here i
have a segmentation fault! */

/** Here I compile in debug mode there segmentation 
fault is

here */
sqlite3/printf.c LINE 608

for(i=n=0; (ch=escarg[i])!=0; i++){
escarg is NULL;

I know that is not an SQLite bug. I know that that is my fault.

Thanks.






-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] BLOB question

2007-02-23 Thread Martin Jenkins

Cesar Rodas wrote:

   while ( (n=fread(buff,Buffsize,1,file)) > 0)
   {
   if (i>0)
   *value = realloc(*value, (i+1) * Buffsize);
   memcpy(*value + (i * Buffsize), buff,  Buffsize);
   *len += n;
   i++;
   }


An afterthought, why don't you just stat the file and malloc the right 
sized buffer from the outset? Much easier.


Martin

-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] BLOB question

2007-02-23 Thread Cesar Rodas

Sorry I copy bad

when I wanted to write "#define uchar unsigned char" but I did
"#define uchar unsigned char *"

On 23/02/07, Martin Jenkins <[EMAIL PROTECTED]> wrote:

Cesar Rodas wrote:

 > #define uchar unsigned char *

suggests it's a char but is in fact a pointer to a char so the signature
of LoadFileContent is actually

int LoadFileContent(unsigned char **path, unsigned char ***value, ...

and that triggers my "three levels of indirection is usually an error"
alarm. You want

 > #define uchar unsigned char /* no asterisk here */

or even better

typedef unsigned char uchar;

Martin

-
To unsubscribe, send email to [EMAIL PROTECTED]
-





--
Cesar Rodas
http://www.sf.net/projects/pagerank (The PageRank made easy...)
http://www.sf.net/projects/fastfs ( The Fast File System)
Mobile Phone: 595 961 974165
Phone: 595 21 645590
[EMAIL PROTECTED]
[EMAIL PROTECTED]

-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] BLOB question

2007-02-23 Thread Cesar Rodas

I found the answer!! http://www.sqlite.org/cvstrac/wiki?p=BlobExample

thanks for you help too Wesley

On 23/02/07, Cesar Rodas <[EMAIL PROTECTED]> wrote:


#define E_IO_COULDNT_OPEN - 1
#define E_IO_ERRO -2
#define uchar unsigned char *

int LoadFileContent(uchar *path, uchar ** value, long * len)
{
#define Buffsize 1024*4

int n;
long i=0;
FILE *file;
unsigned char buff[Buffsize];

if ( (file=fopen(path,"rb"))==0)
{
return E_IO_COULDNT_OPEN;
}

*value = malloc(Buffsize);
*len = 0;
fseek(file,0L,SEEK_SET);
while ( (n=fread(buff,Buffsize,1,file)) > 0)
{
if (i>0)
*value = realloc(*value, (i+1) * Buffsize);
memcpy(*value + (i * Buffsize), buff,  Buffsize);
*len += n;
i++;
}
if (n==-1)
{
fclose(file);
return E_IO_ERRO;
}

fclose(file);
return 0;
}


uchar * value;
char *query;
long len;

if ( LoadFileContent("file.jpeg",, ) != 0)
exit();

query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value); /* here
i have a segmentation fault! */

/** Here I compile in debug mode there segmentation fault
is here */
sqlite3/printf.c LINE 608

for(i=n=0; (ch=escarg[i])!=0; i++){
escarg is NULL;

I know that is not an SQLite bug. I know that that is my fault.

Thanks.



--
Cesar Rodas
http://www.sf.net/projects/pagerank (The PageRank made easy...)
http://www.sf.net/projects/fastfs ( The Fast File System)
Mobile Phone: 595 961 974165
Phone: 595 21 645590
[EMAIL PROTECTED]
[EMAIL PROTECTED]





--
Cesar Rodas
http://www.sf.net/projects/pagerank (The PageRank made easy...)
http://www.sf.net/projects/fastfs ( The Fast File System)
Mobile Phone: 595 961 974165
Phone: 595 21 645590
[EMAIL PROTECTED]
[EMAIL PROTECTED]


Re: [sqlite] BLOB question

2007-02-23 Thread Martin Jenkins

Cesar Rodas wrote:

> #define uchar unsigned char *

suggests it's a char but is in fact a pointer to a char so the signature 
of LoadFileContent is actually


int LoadFileContent(unsigned char **path, unsigned char ***value, ...

and that triggers my "three levels of indirection is usually an error" 
alarm. You want


> #define uchar unsigned char /* no asterisk here */

or even better

typedef unsigned char uchar;

Martin

-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] BLOB question

2007-02-23 Thread Wesley W. Terpstra

A couple of comments.

On Feb 23, 2007, at 3:03 PM, Cesar Rodas wrote:

   while ( (n=fread(buff,Buffsize,1,file)) > 0)
   {
   if (i>0)
   *value = realloc(*value, (i+1) * Buffsize);
   memcpy(*value + (i * Buffsize), buff,  Buffsize);
   *len += n;
   i++;
   }


You are growing the array in size at each append. This is usually a  
bad idea; it leads to n^2 complexity. The standard approach is to  
double the buffer each time it fills up. This is 2*n complexity.  
However, it's not your bug.



query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value);


This segfaults because there is no null terminator on the value  
string. The way SQLite3 reads 'value' is by scanning it until it  
finds a '\0'. If there is no such character, it scans past the end of  
the array, leading to the crash you are seeing.


A simple solution is to append this character to the end of the  
string. However, it the file itself contains a '\0' character, this  
will lead to the file being truncated in the database. A better  
solution would be:


file_content = ... your loading code, allocated by malloc ...
file_length = ... length of the file ...;
query = "INSERT INTO blob VALUES(?);";
sqlite3_prepare_v2(db, query, -1, , );
sqlite3_bind_block(qhandle, 1, file_content, file_length, free);


-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] BLOB question

2007-02-23 Thread Cesar Rodas

#define E_IO_COULDNT_OPEN - 1
#define E_IO_ERRO -2
#define uchar unsigned char *

int LoadFileContent(uchar *path, uchar ** value, long * len)
{
   #define Buffsize 1024*4

   int n;
   long i=0;
   FILE *file;
   unsigned char buff[Buffsize];

   if ( (file=fopen(path,"rb"))==0)
   {
   return E_IO_COULDNT_OPEN;
   }

   *value = malloc(Buffsize);
   *len = 0;
   fseek(file,0L,SEEK_SET);
   while ( (n=fread(buff,Buffsize,1,file)) > 0)
   {
   if (i>0)
   *value = realloc(*value, (i+1) * Buffsize);
   memcpy(*value + (i * Buffsize), buff,  Buffsize);
   *len += n;
   i++;
   }
   if (n==-1)
   {
   fclose(file);
   return E_IO_ERRO;
   }

   fclose(file);
   return 0;
}


uchar * value;
char *query;
long len;

if ( LoadFileContent("file.jpeg",, ) != 0)
exit();

query = sqlite3_mprintf("INSERT INTO blob VALUES('%Q') ", value); /* here i
have a segmentation fault! */

/** Here I compile in debug mode there segmentation fault is
here */
sqlite3/printf.c LINE 608

for(i=n=0; (ch=escarg[i])!=0; i++){
escarg is NULL;

I know that is not an SQLite bug. I know that that is my fault.

Thanks.



--
Cesar Rodas
http://www.sf.net/projects/pagerank (The PageRank made easy...)
http://www.sf.net/projects/fastfs ( The Fast File System)
Mobile Phone: 595 961 974165
Phone: 595 21 645590
[EMAIL PROTECTED]
[EMAIL PROTECTED]