        __  _                        __ _                           _  __
  ______\ \_\\_______________________\///__________________________//_/ /______
  \___\                                                                   /___/
   | .__                                 __                                  |
   | |                   ___  __________/  |________                         |
   |                     \  \/  / ____/\   __\_  __ \                        |
   ;                      >    < <_|  | |  |  |  | \/                        ;
   :                     /__/\_ \__   | |__|  |__|                           :
   .                           \/  |__|                                      .
   .                                                                         .
   :           H/Q Another Droid BBS - andr01d.zapto.org:9999                :
   ;                                                                         ;
   + --- --  -   .     -        ---    ---    ---        -     .    - -- --- +
   :                                                                         :
   |                             Byte Counting...                            |
   :                                                                         :
   ` --- --  -   .     -        ---    ---    ---        -     .    - -- --- ´

lets learn to count! :) in hex editing files, specially save files (files 
that save records of some time), we need to know the length of the various 
fields/variables, cause its crucial. when we edit a binary file, we must 
not add more bytes to it!!! we only overwrite things in the file, so that 
the file size don't change and also wont overlap fields. lets see a small 
example...

we have a record like this:

  [1;32mperson[0;37;40m = [1;36mrecord[0m
    name :[34m [1;36mstring[0;37;40m[30];
    phone :[1;36m string[0;37;40m[20];
  [1;36mend;[0m
  
if we edit the file with a hex editor we will get something like below. 
this is not an actual representation.

[1mxqtr..........................123456789...........[0m

now, if we want to change the name or phone to something else, we 
overwrite that data and make sure that we don't overlap the fields. for 
example:

this is correct:
[1mmamadu........................88888888888888......[0m

but this is not:
[1mmy name is not of your bussiness777777777.........[0m

the string we inserted as name, now overlaps and is occuping space in the 
field we have for the telephone number. when we execute the program that 
uses this file, we will see, that in the area that displays the phone 
number will show something like this: ss777777777

so, all variables have a specific size, which we need to know. but how we 
know it? simple (?)... we look into our programming language variable 
reference. for example the following variables are used in FreePascal:

                                Byte      1
                                Shortint  1
                                Smallint  2
                                Word      2
                                Integer   2 or 4
                                Cardinal  4
                                Longint   4
                                Longword  4
                                Int64     8
                                QWord     8 
                                
each programming language has its own specifications. in hex editing we 
don't care if in pascal a variable type is named as integer and in c is 
named uint16... we care about the bytes! if those two types occupy the 
same size of bytes, we are good to go.

but... (there is always a but... :) there are more complex variable types 
than just numbers or strings... in reality, not even strings are so 
simple.. :( lets to some string math... 

in pascal strings are stored in a different way than other programming 
languages. for example a variable like String[10]... it doesn't occupy 10 
bytes, as expected in the file, but 11! thats because pascal holds the 
size of the string in one byte, before the actual string. for example a 
string[10] variable that holds the value "string10" in hex, it shows like 
this:

.......[1;32m0d  0A 73 74 72 69 6e 67  31 30 0a 00 00 00 00 00[0;37;40m [1m.string10......[0m

the first byte "0A" is the actual size of our string. its not something 
good or bad... its just the way pascal saves string formats, but its not 
compatible with other languages like c. so when we edit a pascal save 
file, we have to know it or if we don't know it in advance, be aware that 
the save file maybe a pascal type.

lets make a practival example. open the file records.112 from mystic bbs 
software and take a look at the records, for example:

  [1;32mRecHistory[0;37;40m = [1;36mRecord[0m
    Date       : [1;36mLongInt;[0m
    Emails     : [1;36mWord;[0m
    Posts      : [1;36mWord;[0m
    Downloads  : [1;36mWord;[0m
    Uploads    : [1;36mWord;[0m
    DownloadKB : [1;36mLongInt;[0m
    UploadKB   : [1;36mLongInt;[0m
    Calls      : [1;36mLongInt;[0m
    NewUsers   : [1;36mWord;[0m
    Telnet     : [1;36mWord;[0m
    FTP        : [1;36mWord;[0m
    POP3       : [1;36mWord;[0m
    SMTP       : [1;36mWord;[0m
    NNTP       : [1;36mWord;[0m
    HTTP       : [1;36mWord;[0m
    Hourly     : [1;36mArray[0;37;40m[0..23][1;36m of Byte[0;37;40m;
    Reserved   : [1;36mArray[0;37;40m[1..2] [1;36mof Byte[0;37;40m;
 [1;36m End;[0m

lets count, how many bytes ocupies this record in a file:

[1;36m  [32mRecHistory[0;37;40m = [1;36mRecord[0m
    Date       : [1;36mLongInt;[13C[0;37;40m  4
    Emails     : [1;36mWord;[16C[0;37;40m  2
    Posts      : [1;36mWord;[16C[0;37;40m  2
    Downloads  : [1;36mWord;[16C[0;37;40m  2
    Uploads    : [1;36mWord;[16C[0;37;40m  2
    DownloadKB : [1;36mLongInt;[13C[0;37;40m  4
    UploadKB   : [1;36mLongInt;[13C[0;37;40m  4
    Calls      : [1;36mLongInt;[13C[0;37;40m  4
    NewUsers   : [1;36mWord;[16C[0;37;40m  2
    Telnet     : [1;36mWord;[16C[0;37;40m  2
    FTP        : [1;36mWord;[16C[0;37;40m  2
    POP3       : [1;36mWord;[16C[0;37;40m  2
    SMTP       : [1;36mWord;[16C[0;37;40m  2
    NNTP       : [1;36mWord;[16C[0;37;40m  2
    HTTP       : [1;36mWord;[16C[0;37;40m  2
    Hourly     : [1;36mArray[0;37;40m[0..23][1;36m of Byte[0;37;40m; 24
    Reserved   : [1;36mArray[0;37;40m[1..2] [1;36mof Byte[0;37;40m;[1C  2
  [1;36mEnd;[0m
  
  Total : 64 bytes
  
if we devide the size of the record with the file size, we will get how 
many records the files stores! which is very useful. or if we know the 
record size and how many records we have, we can calculate the file size.

the important thing is, that we edit the file and for example we want to 
change the number of email a user has we must put a number that occupies 2 
bytes, cause the variable is of type word. you must also have in mind the 
endianess of the system, when writing numbers... remember? from previous 
tutor?

again from the mystic bbs, records.112 file we have the following field:
  
  OptionData :[1;36m Array[0;37;40m[1..10][1;36m of String[0;37;40m[60];
  
can you guess the size? is it 10 x 60 = 600 bytes? not... as we said 
pascal saves strings with one more byte, so a string[60] type has a size
of 61 bytes. so the size of the above array is: 10 x 61= 610 bytes.

if you are not 100% sure or too lazy to calculate it by yourself, you can 
use a programming language to do it for you. each programming language has 
a function that gives the size of a structure/record. in pascal is the 
function "sizeof()". you can write a simple program like this:

[36mProgram count_zero;[0m

[1;32muses[0;37;40m sysutils;

[1;32mtype [0m
  RecHistory =[1;36m Record[0m
    Date       :[1;36m LongInt; [32m   [0;37;40m           
    Emails     :[1;36m Word;    [32m   [0;37;40m           
    Posts      :[1;36m Word;    [32m   [0;37;40m           
    Downloads  :[1;36m Word;    [32m   [0;37;40m           
    Uploads    :[1;36m Word;    [32m   [0;37;40m           
    DownloadKB :[1;36m LongInt; [32m   [0;37;40m           
    UploadKB   :[1;36m LongInt; [32m   [0;37;40m           
    Calls      :[1;36m LongInt; [32m   [0;37;40m           
    NewUsers   :[1;36m Word;    [32m   [0;37;40m           
    Telnet     :[1;36m Word;    [32m   [0;37;40m           
    FTP        :[1;36m Word;[32m       [0;37;40m           
    POP3       :[1;36m Word;[32m       [0;37;40m           
    SMTP       :[1;36m Word;[32m       [0;37;40m           
    NNTP       :[1;36m Word;[0;37;40m                  
    HTTP       :[1;36m Word;[0;37;40m                  
    Hourly     :[1;36m Array[0;37;40m[0..23][1;36m of Byte;[32m [0m
    Reserved   :[1;36m Array[0;37;40m[1..2] [1;36mof Byte; [32m [0;37;40m 
 [1;36m End;[0m

[1;32mvar[0m
  rec : RecHistory;
  
Begin
  [1;36mWriteLn[0;37;40m('Size of record: '+inttostr(sizeof(rec)));
End.

the program will tell us exactly the size of this record/structure. 

but why counting bytes/size is important? for various reasons, but i think 
the most important is to be able to find the exact position of a field 
inside a save file and not looking/changing random numbers. :) specially 
in big files. 

what if we want to find the number of emails, of the 5th record saved in 
the file. we calculate the position like this:

[10C[1mposition_in_file[0;37;40m = [1mvariable_offset[0;37;40m + ([1mstruct_size[0;37;40m *[1m count[0;37;40m)

our variable (email) has an offset of 4 and the structure size is 64, so 
the position is:

[25C[1mposition[0;37;40m = 4 + (64 * 5) = 324

test it and see it yourself. now you can find the position of a structure 
field and change its value.

    + --- --  -   .     -        ---    ---    ---        -     .    - -- --- ´
         _____         _   _              ____          _   _ 
        |  _  |___ ___| |_| |_ ___ ___   |    \ ___ ___|_|_| |        8888
        |     |   | . |  _|   | -_|  _|  |  |  |  _| . | | . |     8 888888 8
        |__|__|_|_|___|_| |_|_|___|_|    |____/|_| |___|_|___|     8888888888
                                                                   8888888888
                DoNt Be aNoTHeR DrOiD fOR tHe SySteM               88 8888 88
                                                                   8888888888
 /: HaM RaDiO   /: ANSi ARt!     /: MySTiC MoDS   /: DooRS         '88||||88'
 /: NeWS        /: WeATheR       /: FiLEs         /: SPooKNet       ''8888"'
 /: GaMeS       /: TeXtFiLeS     /: PrEPardNeSS   /: FsxNet            88
 /: TuTors      /: bOOkS/PdFs    /: SuRVaViLiSM   /: ArakNet    8 8 88888888888
                                                              888 8888][][][888
   TeLNeT : andr01d.zapto.org:9999 [UTC 11:00 - 20:00]          8 888888##88888
   SySoP  : xqtr                   eMAiL: xqtr@gmx.com          8 8888.####.888
   DoNaTe : https://paypal.me/xqtr                              8 8888##88##888
