Recent

Author Topic: TStringList cross platform line size/length consistency?  (Read 1759 times)

Firewrath

  • New Member
  • *
  • Posts: 35
TStringList cross platform line size/length consistency?
« on: May 08, 2019, 05:21:53 pm »
Probably a stupid question, but...
Just out of curiosity, if I did this with a TStringList:
Code: Pascal  [Select][+][-]
  1. StringList.LoadFromFile(project1.exe);
Would the 'lines' be the same size / length across platforms*?
By 'lines', I mean each item on the list, like:
Code: Pascal  [Select][+][-]
  1. StringList.Strings[i]
*IE: All 32 / 64 bit versions of: WindowsXP / Vista / Win7 & 10 / Linux / Mac /etc
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: TStringList cross platform line size/length consistency?
« Reply #1 on: May 08, 2019, 05:29:54 pm »
No, because LineEnding differs and a binary file can have may occurrences of $OD or $OA all over the place.

Bart

Firewrath

  • New Member
  • *
  • Posts: 35
Re: TStringList cross platform line size/length consistency?
« Reply #2 on: May 08, 2019, 06:40:27 pm »
Drats. So much for me being lazy then. ;)
(I was going to toy with 'line by line' encrypting of a file, just for fun.)
Thanks for the reply. ^-^
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: TStringList cross platform line size/length consistency?
« Reply #3 on: May 08, 2019, 09:06:28 pm »
I was going to toy with 'line by line' encrypting of a file, just for fun.

Encryption operates on raw bytes, not on text, and certainly not on lines.  While you can certainly encrypt a file line-by-line, you would be responsible for reading those lines and encrypting them one at a time.  But that would only work for a text file, you certainly would not want to encrypt a binary file that way, you would have to encrypt the whole file as-is instead to preserve its integrity when decrypted.
« Last Edit: May 10, 2019, 09:24:19 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Firewrath

  • New Member
  • *
  • Posts: 35
Re: TStringList cross platform line size/length consistency?
« Reply #4 on: May 08, 2019, 10:11:32 pm »
Hrm, while text is certainly interesting, I just hoped that since I could open a file with a string list, it would save the file (once decrypted) the same as when I opened it and 'The End'. Ahwell. :P

I guess I can't encrypt an 'Array[1..1024] of byte;' either, hmm?
Since it's an array and not a single 'block' ... This is what I get for avoiding my current work to have fun.
(IE: it looks like it's turning into work. ;))

Thanks for the explanation though.
« Last Edit: May 08, 2019, 10:20:57 pm by Firewrath »
Sorry. I currently don't have Internet Access. So my replies might take a week. -_-

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TStringList cross platform line size/length consistency?
« Reply #5 on: May 08, 2019, 10:53:32 pm »
I guess I can't encrypt an 'Array[1..1024] of byte;' either, hmm?
Since it's an array and not a single 'block' ...

If you declare it as: packed array [1..1024] of byte it should ocupy exactly 1024 consecutive bytes on any platform. So yes, you can use it as a buffer for encryption or whatever else that needs that restriction. In fact, you could probably pass a String as an array of bytes if you take the simple precaution of casting it and passing it as String[1].
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: TStringList cross platform line size/length consistency?
« Reply #6 on: May 10, 2019, 09:32:12 pm »
Hrm, while text is certainly interesting, I just hoped that since I could open a file with a string list, it would save the file (once decrypted) the same as when I opened it and 'The End'. Ahwell. :P

A file is just a collection of raw bytes.  A TStringList is a collection of strings in memory.  How you get from bytes to strings requires interpreting the bytes using a charset, which governs how specific bytes are mapped to specific characters.  And since TStringList assumes a file's bytes are textual in nature, that also includes platform-specific line break handling, as some platforms use CR-only, some use LF-only, and some use CR+LF.

So no, you can't rely on a TStringList preserving the original file as-is.

I guess I can't encrypt an 'Array[1..1024] of byte;' either, hmm?

Yes, of course you can.  Why would you think otherwise?

Since it's an array and not a single 'block'

An array is always a single block of contiguous memory.  For encryption purposes, just make sure to use an element type that is a value type, like Byte, Integer, etc, and not a reference type, like String, TObject, Pointer, etc.

If you declare it as: packed array [1..1024] of byte it should ocupy exactly 1024 consecutive bytes on any platform.

You don't need the packed for a byte array,

In fact, you could probably pass a String as an array of bytes if you take the simple precaution of casting it and passing it as String[1].

That really depends on whether String is a ShortString, AnsiString, or UnicodeString.  It is not really a good idea to use a String as a binary buffer.
« Last Edit: May 10, 2019, 09:34:22 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018