Is it possible to read a png image as if it were a normal text file?

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Is it possible to read a png image as if it were a normal text file?

Post by skinkairewalker »

Hello everyone, I'm trying to encrypt an image inside a lzma package, to then read it as if it were a text file (obviously unreadable), but I need it to be read as if it were a text file, below is the excerpt from as I'm doing... normal text files, work perfectly, but png files, this is returned >:

Code: Select all

�PNG 

 size : 85098
code snippet

Code: Select all


  Procedure.s GetFileText(PackName.s,FileName$)

    If (OpenPack(0,PacksList(PackName)\PackPath,#PB_PackerPlugin_BriefLZ) <> 0)
      
      *TempFileUncompressed = ReAllocateMemory(*TempFileUncompressed,PreFiles(FileName$)\FileSize)
      
      UncompressPackMemory(0, *TempFileUncompressed, PreFiles(FileName$)\FileSize,FileName$)
      
      *DecipheredFile = ReAllocateMemory(*DecipheredFile,PreFiles(FileName$)\FileSize)
      
      AESDecoder(*TempFileUncompressed, *DecipheredFile, PreFiles(FileName$)\FileSize, @Password , 256, 0, #PB_Cipher_ECB)
      
      ResultFileText$ = PeekS(*DecipheredFile,PreFiles(FileName$)\FileSize,#PB_UTF8|#PB_ByteLength)
      Debug ResultFileText$ + " size : "+ Str(PreFiles(FileName$)\FileSize)
    ClosePack(0) 
      *TempFileUncompressed = ReAllocateMemory(*TempFileUncompressed,1024)
      *DecipheredFile = ReAllocateMemory(*DecipheredFile,1024)
      ProcedureReturn ResultFileText$
    Else
      ProcedureReturn ""
    EndIf
      
EndProcedure  


does anyone know how to solve this so that it continues reading normal text files, however, it reads images and any other files as if it were a text file even though it is not?
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is it possible to read a png image as if it were a normal text file?

Post by infratec »

You never can read a binary file as a text, because a text line has always an EOL.
If your binary file contains a EOL (CR or LF), then this line is finished and the next starts, but you loose the EOL marker and if you reach EOF you get also in trouble, because you don't know if there was an EOL marker or not.

But you can detect the PNG marker, read the file into a buffer and convert it with Base64 to a text if you need it as text.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: Is it possible to read a png image as if it were a normal text file?

Post by skinkairewalker »

Hi, Thanks by your answer infrated !

let me explain the problem I'm facing, I'm creating a webserver with atomic webserver, however, I'm packaging the .html and .img files to prevent any type of person from simply copying and pasting the files... is there any way to read the image so I can send the images to the user's browser without having to make it base64?
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Is it possible to read a png image as if it were a normal text file?

Post by blueb »

You might find something useful here on JHPJHP's web page:

LSB Image Steganography..
LSB (Least Significant BIT):
- set a Header password and BIT password (each BIT can have a different password)
- load an image container of the following type: jpg, png, or bmp
- embed files and folders into the image container (restricted by size / BIT spanning)
- files and folders are compressed and encrypted before being embedded
- save the image container as a PNG (Portable Network Graphics); lossless compression
- load the image container to extract the embedded files and folders
- embedded files and folders can be overwritten at anytime

viewtopic.php?p=476500#p476500
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is it possible to read a png image as if it were a normal text file?

Post by infratec »

You can serve directly the binary.

You need to set

"content-type": "image/png"

You definately need to set the length

"content-length": xxxx

See also:

https://dev.to/sebastienfilion/building ... pload-438d

You can use the developer tools from a webbrowser to see what a server sends.
Last edited by infratec on Fri Mar 31, 2023 9:40 pm, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Is it possible to read a png image as if it were a normal text file?

Post by netmaestro »

You can disable right-clicking for a page, just google how to do that. But nothing can prevent a user from getting an image shown on his screen by hitting the printscreen key. If the image provenance is important to you your only recourse is a watermark which you can easily apply by creating a transparent image with your watermark on it rendered in a low alpha value and drawing it on top of your image to be protected. Shutterstock does this for their images and it works well for them.
Last edited by netmaestro on Fri Mar 31, 2023 9:54 pm, edited 1 time in total.
BERESHEIT
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is it possible to read a png image as if it were a normal text file?

Post by infratec »

Have already looked at this:

viewtopic.php?t=77311

:?:
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Is it possible to read a png image as if it were a normal text file?

Post by idle »

he could also use civet server, The whole website can be run from in memory from a data section, you just use easypack to pack the static assets into ww.ezp and include the binary in your exe. See civet_server_in_memSSL.pb for use
https://github.com/idle-PB/pb_civet_server
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: Is it possible to read a png image as if it were a normal text file?

Post by skinkairewalker »

Guys, this is my code, I made my own package manager and I'm using Atomic Web Server v2 base, text files works 100%, but I tried everything to make images be sent to the user's browser, but my knowledge is still limited
thank you all for the immense help, you are the guys !!

https://drive.google.com/file/d/1wC3Vhb ... sp=sharing

Can someone analyze my code and see what is possible to do so that the Atomic WebServer can load images or any other type of file that is not ( html, js and css ) too?
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is it possible to read a png image as if it were a normal text file?

Post by infratec »

Code: Select all

Procedure ProcessRequest()
  Protected RequestedFile.s, FileLength, MaxPosition, Position, ContentType.s
  ;   Protected *FileBuffer
  ;   Protected BufferOffset.s, *BufferOffset
  
  Protected *FileBuffer2
  Protected BufferOffset2.s
  Protected TmpPage.s
  Protected *TmpBuffer
  
  If Left(Buffer, 3) = "GET"    
    ;Extract page html from GET /yourpage.html HTTP/1.1
    MaxPosition = FindString(Buffer, Chr(13), 5)
    Position = FindString(Buffer, " ", 5)
    If Position < MaxPosition
      RequestedFile = Mid(Buffer, 6, Position-5)      ; Automatically remove the leading '/'
      RequestedFile = RTrim(RequestedFile)
    Else
      RequestedFile = Mid(Buffer, 6, MaxPosition-5)   ; When a command like 'GET /' is sent..
    EndIf
    
    If RequestedFile = ""
      RequestedFile = WWWIndex      
    EndIf
    
    ;Mise à jour du log / UPdated log
    If GetGadgetState(1) = #PB_Checkbox_Checked
      AddGadgetItem(0, -1, "Client IP " + IPString(GetClientIP(ClientID)) + " load " + RequestedFile) 
    EndIf   
    
    ;Envoyer la page HTML au client / Send the HTML page to the client
    If (ColdBasicPacker::CheckFileExists("Pack","/"+RequestedFile))
      ;Definition du content-type / Setup content-type
      ;Ref : https://fr.wikipedia.org/wiki/Type_MIME
      Select GetExtensionPart(RequestedFile)
        Case "css" : ContentType = "text/css"
          TmpPage = ColdBasicPacker::GetFileText("Pack","/"+RequestedFile)
        Case "js"  : ContentType = "application/javascript" 
          TmpPage = ColdBasicPacker::GetFileText("Pack","/"+RequestedFile)
        Case "gif" : ContentType = "image/gif"
          *TmpBuffer =  ColdBasicPacker::GetFileImg("Pack","/"+RequestedFile)
        Case "jpg" : ContentType = "image/jpeg"
          *TmpBuffer =  ColdBasicPacker::GetFileImg("Pack","/"+RequestedFile)
        Case "png" : ContentType = "image/png"
          *TmpBuffer = ColdBasicPacker::GetFileImg("Pack","/"+RequestedFile)
        Case "txt" : ContentType = "text/plain"
          TmpPage = ColdBasicPacker::GetFileText("Pack","/"+RequestedFile)
        Case "zip" : ContentType = "application/zip"
          TmpPage = ColdBasicPacker::GetFileText("Pack","/"+RequestedFile)
        Case "pdf" : ContentType = "application/pdf"
          TmpPage = ColdBasicPacker::GetFileText("Pack","/"+RequestedFile)
          
        Default
          ContentType = "text/html" 
          TmpPage = ColdBasicPacker::GetFileText("Pack","/"+RequestedFile)
      EndSelect      
      
    Else
      
      ;Affichage de la page d'erreur si url inexistant / Display error page if url nonexistent
      
      ContentType = "text/html"        
      
      TmpPage = ColdBasicPacker::GetFileText("Pack","/"+WWWError)
      
    EndIf
       
    If *TmpBuffer
      BufferOffset2 = BuildRequestHeader2(MemorySize(*TmpBuffer), ContentType)
      *FileBuffer2 = AllocateMemory(MemorySize(*TmpBuffer) + StringByteLength(BufferOffset2, #PB_UTF8), #PB_Memory_NoClear)
      PokeS(*FileBuffer2, BufferOffset2, -1, #PB_UTF8)
      CopyMemory(*TmpBuffer, *FileBuffer2 + StringByteLength(BufferOffset2, #PB_UTF8), MemorySize(*TmpBuffer))
      FreeMemory(*TmpBuffer)
    Else
      BufferOffset2 = BuildRequestHeader2(StringByteLength(TmpPage,#PB_UTF8), ContentType) + TmpPage
      *FileBuffer2 = AllocateMemory(StringByteLength(BufferOffset2, #PB_UTF8) + 1)
      PokeS(*FileBuffer2, BufferOffset2, -1, #PB_UTF8)
    EndIf
    
    SendNetworkData(ClientID, *FileBuffer2, MemorySize(*FileBuffer2))
    FreeMemory(*FileBuffer2)
    
  EndIf  
EndProcedure

Code: Select all

Procedure.i GetFileImg(PackName.s,FileName$)
    
    Protected *DecipheredFile
    
    If OpenPack(0,PacksList(PackName)\PackPath,#PB_PackerPlugin_BriefLZ) <> 0
      
      *TempFileUncompressed = ReAllocateMemory(*TempFileUncompressed,PreFiles(FileName$)\FileSize)
      Debug "meu saco = "+Str(PreFiles(FileName$)\FileSize)
      UncompressPackMemory(0, *TempFileUncompressed, PreFiles(FileName$)\FileSize,FileName$)
      
      *DecipheredFile = ReAllocateMemory(*DecipheredFile,PreFiles(FileName$)\FileSize)
      
      AESDecoder(*TempFileUncompressed, *DecipheredFile, PreFiles(FileName$)\FileSize, @Password , 256, 0, #PB_Cipher_ECB)
      
      Debug "Image Size Memory = "+MemorySize(*DecipheredFile)
      
      ClosePack(0)
      *TempFileUncompressed = ReAllocateMemory(*TempFileUncompressed,1024)
      ;*DecipheredFile = ReAllocateMemory(*DecipheredFile,1024)
      ProcedureReturn *DecipheredFile
    Else
      ProcedureReturn #Null
    EndIf
    
  EndProcedure
Maybe it is better to return always a pointer and never a string.

You also should remove the fixed 0 for opening something, or you are never allowed to thread the server.

And ... I added no checks for success of something.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: Is it possible to read a png image as if it were a normal text file?

Post by skinkairewalker »

Awesome !!!
I even replaced the text function the way you did, it was very efficient !!
thank you so much for your help, I appreciate it all!!

I love this community... I don't know better.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is it possible to read a png image as if it were a normal text file?

Post by infratec »

I also replaced this reallocate stuff, because it takes much longer and is not efficent.
So I can also use the NoClear flag which also gains speed.

Code: Select all

Procedure.i GetFileImg(PackName.s, FileName$)
    
    Protected.i Pack, Result
    Protected *TempFileUncompressed, *DecipheredFile
    
    
    Pack = OpenPack(#PB_Any, PacksMap(PackName)\PackPath, #PB_PackerPlugin_BriefLZ)
    If Pack
      
      *TempFileUncompressed = AllocateMemory(PreFilesMap(FileName$)\FileSize, #PB_Memory_NoClear)
      If *TempFileUncompressed
        
        Debug "meu saco = " + Str(PreFilesMap(FileName$)\FileSize)
        If UncompressPackMemory(Pack, *TempFileUncompressed, PreFilesMap(FileName$)\FileSize, FileName$) > 0
          
          *DecipheredFile = AllocateMemory(PreFilesMap(FileName$)\FileSize, #PB_Memory_NoClear)
          If *DecipheredFile
            
            If AESDecoder(*TempFileUncompressed, *DecipheredFile, PreFilesMap(FileName$)\FileSize, @Password , 256, 0, #PB_Cipher_ECB)
              
              Debug "Image Size Memory = " + Str(MemorySize(*DecipheredFile))
              
              Result = *DecipheredFile
              
            EndIf
            
          EndIf
          
        EndIf
        
        FreeMemory(*TempFileUncompressed)
      EndIf
      
      ClosePack(Pack)
      
    EndIf
    
    ProcedureReturn Result
    
  EndProcedure
I added also more checks.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: Is it possible to read a png image as if it were a normal text file?

Post by skinkairewalker »

I need to study a lot to get to make algorithms of this level of fluidity.
wonderful ...

I greatly appreciate everyone who helped, and thank you for the algorithms @infratec @idle @netmaestro @blueb
Post Reply