November 28, 2020, 02:01:33 AM

Author Topic: [bb] Read/Write PureText Strings by _PJ_ [ 1+ years ago ]  (Read 599 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Read/Write PureText Strings by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Read/Write PureText Strings
Author : _PJ_
Posted : 1+ years ago

Description : Not sure exactly how practical this is, since it was part of something else I've been working towards.
Essentially, it simplifies the bytes of string data allowing standard alphanumeric characters (plus 1 or two others) to be read/written to files with 64 bytes each character.
Making use of the Bit for 128, I incorporated a very simple randomisation to 'encrypt' the data, making it harder to read by an end user direct from the output. The Read functionality identifies this value.
Limitations & Notes:
I built in some formatting to remove extraneous spaces.
Commas and full-stops (period US) / decimal points are the only accepted punctuation. It's possible to alter these, but only 3 such non-alphanumeric chars are permitted (including SPACE).
String length is limited to 65535 chars, should be enough for anyoine. This limit can be altered if really necessary, so long as Length is read/written as Int for 4-Byte values or perhaps Byte for 255-length strings rather than Short (16-Bit, 65535 limit).
Encryption is very simply substitution and reversal with the cypher given at the top of the code. (Also the byte format for the chars) - whilst it may not be a preferred form of security, it should be adequate in theory for general purposes due to the application of randomised encrypted characters.


Code :
Code: BlitzBasic
  1. ;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,
  2.  
  3. ;------------------------------------------------------------------------------------------------
  4. ;EXAMPLE:
  5. ;------------------------------------------------------------------------------------------------
  6. f=WriteFile("test.txt")
  7.  
  8. WritePureTextString(f,"Hello World",True)
  9.  
  10. CloseFile f
  11.  
  12. f=ReadFile("test.txt")
  13.  
  14. Print ReadPureTextString(f)
  15.  
  16. CloseFile f
  17.  
  18.  
  19. ;------------------------------------------------------------------------------------------------
  20. ;FUNCTIONS:
  21. ;------------------------------------------------------------------------------------------------
  22.  
  23.  
  24. Function ReadPureTextString$(FileStream)
  25.         If (Not(Filestream)) Then Return ""
  26.        
  27.         Local Length%=ReadShort(FileStream)
  28.         If (Not (Length)) Then Return ""
  29.        
  30.         Local sString$=""
  31.         Local IterChar%
  32.         Local Char$
  33.         Local Byte%
  34.         For IterChar= 1 To Length
  35.                 Byte=ReadByte(Filestream)
  36.                 If (Byte>64) Then Byte=Byte-128
  37.                 Char=ConvertByteToChar(Byte)
  38.                 sString=sString+Char
  39.         Next
  40.        
  41.         sString=Trim(sString)
  42.         While Instr(sString,"  ")
  43.                 sString=Replace(sString,"  "," ")
  44.         Wend
  45.        
  46.         Return sString
  47. End Function
  48.  
  49. Function WritePureTextString(Filestream%,sString$,Encrypt=False)
  50.         If (Not(Filestream)) Then Return
  51.        
  52.         Local Length=Len(sString)
  53.  
  54.         ; Zero Length string   
  55.         If (Not (Length))
  56.                 WriteShort Filestream,0
  57.                 Return
  58.         End If
  59.        
  60.         Local IterChar%
  61.         Local Char$
  62.         Local Byte%
  63.                
  64.         ;Convert string
  65.         For IterChar=1 To Length
  66.                 Char=Mid(sString,IterChar,1)
  67.                 Byte=ConvertCharToByte(Char)
  68.                 Replace(sString,Chr(Char),ConvertByteToChar(Char))
  69.         Next
  70.  
  71.         ;Consecutive spaces count as 1. No leading or trailing spaces.
  72.         sString=Trim(sString)
  73.         While Instr(sString,"  ")
  74.                 sString=Replace(sString,"  "," ")
  75.         Wend
  76.  
  77.         Length=Len(sString)
  78.  
  79.         ; Zero Length string   
  80.         If (Not (Length))
  81.                 WriteShort Filestream,0
  82.                 Return
  83.         End If
  84.        
  85.         ;Truncate long strings
  86.         If (Length>65535)
  87.                 Length=65535
  88.                 sString=Left(sString,65535)
  89.         End If
  90.  
  91.         ;Consecutive spaces count as 1. No leading or trailing spaces.
  92.         sString=Trim(sString)
  93.         While Instr(sString,"  ")
  94.                 sString=Replace(sString,"  "," ")
  95.         Wend
  96.  
  97.         Length%=Len(sString)
  98.  
  99.         ;Write Length as short
  100.         WriteShort FileStream,Length
  101.                        
  102.         For IterChar=1 To Length
  103.                 Char=Mid(sString,IterChar,1)
  104.                 Byte=ConvertCharToByte(Char)
  105.                 If (Encrypt)
  106.                         SeedRnd MilliSecs()*RndSeed()
  107.                         If Rand(0,1) Then Byte=Byte+128
  108.                 End If
  109.                 WriteByte Filestream,Byte
  110.         Next
  111. End Function
  112.        
  113.  
  114. Function ConvertCharToByte%(Char$)
  115.  
  116.         Local Ascii%=Asc(Char)
  117.  
  118.         If ((Ascii>47) And (Ascii<58))
  119.                 ; NUMERAL
  120.                 Return (Ascii-48)
  121.         Else
  122.  
  123.                 If ((Ascii>64) And (Ascii<91))
  124.                         ; UPPER ALPHA
  125.                         Return (Ascii-54)
  126.        
  127.                 Else
  128.        
  129.                         If (Ascii>96) And (Ascii<123)
  130.                                 ; LOWER ALPHA
  131.                         Return (Ascii-61)
  132.                
  133.                         End If
  134.                 End If
  135.         End If 
  136.        
  137.         If Char="." Then Return 62
  138.         If Char="," Then Return 63
  139.        
  140.         ; For everything else, there's mastercard
  141.         Return 64
  142.                
  143. End Function
  144.  
  145. Function ConvertByteToChar$(Byte)
  146.  
  147.         If (Byte<10)
  148.                 ;NUMERAL
  149.                 Return Chr(Byte+48)
  150.        
  151.         Else
  152.                 If ((Byte>9) And (Byte<37))
  153.                         ; UPPER ALPHA
  154.                         Return Chr(Byte+54)
  155.                 Else
  156.                         If ((Byte>36) And (Byte<62))
  157.                                 ; LOWER ALPHA
  158.                                 Return Chr(Byte+61)
  159.                
  160.                         End If
  161.                 End If
  162.         End If 
  163.        
  164.         If Byte=62 Then Return "."
  165.         If Byte=63 Then Return ","     
  166.         Return " "
  167. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal