home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / pxtol012.zip / doc / paradox4.txt next >
Text File  |  2000-09-05  |  57KB  |  1,747 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.                               PARADOX 4.x FILE FORMATS
  32.  
  33.                                      Revision 1
  34.                                     May 11, 1996
  35.  
  36.                                Author: Kevin Mitchell
  37.  
  38.  
  39.  
  40.  
  41.  
  42.                               PARADOX 4.x FILE FORMATS
  43.  
  44.  
  45.           Preface
  46.           -------
  47.  
  48.           This document is released to the public domain.
  49.  
  50.           You may do anything you want with it.
  51.  
  52.           IMPORTANT:     YOU USE THE INFORMATION CONTAINED IN THIS DOCUMENT
  53.                          AT YOUR OWN RISK. I CANNOT BE HELD RESPONSIBLE FOR
  54.                          ANY DAMAGES THAT RESULT FROM YOUR USE OF THIS
  55.                          INFORMATION.
  56.  
  57.           If you modify the document, please remove my name from it.
  58.  
  59.  
  60.           There are a couple of illustrations that would have looked better
  61.           if I had used line draw characters. I avoided using line draw
  62.           characters because non-US character sets do not display the
  63.           characters correctly.
  64.  
  65.  
  66.           Kevin Mitchell
  67.           May 11, 1996
  68.  
  69.           Send questions and comments via E-Mail.
  70.  
  71.           My Compuserve Id is 70717,475
  72.  
  73.  
  74.           I specialize in programming for Paradox/DOS.
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.           Revision 1.0                    i                    May 11, 1996
  96.  
  97.  
  98.  
  99.  
  100.  
  101.                               PARADOX 4.x FILE FORMATS
  102.  
  103.  
  104.           Introduction
  105.           ------------
  106.  
  107.           This document describes the internal formats of the Paradox data
  108.           and index files:
  109.  
  110.           DB - Contains the data records for a table. The header contains
  111.           the field names and types and other useful information.
  112.  
  113.           PX - Contains the primary index for a table. If the table is
  114.           unkeyed, there is no PX file for the table.
  115.  
  116.           MB - Contains BLOB (Binary Large Object) data. For example, type
  117.           M and B fields are blobs and the MB file contains the data for M
  118.           and B fields.
  119.  
  120.           X** - Contains a secondary index and has the same basic format as
  121.           a DB file.
  122.  
  123.           Y** - Contains the index for a secondary index (X**) And has the
  124.           same basic format as a PX file.
  125.  
  126.  
  127.           Some of the information in this document may apply to tables
  128.           created by other Paradox versions, but the information was
  129.           derived by examining version 4.5 tables.
  130.  
  131.           Standard reverse engineering techniques were used to obtain the
  132.           information, i.e., view a file with a hex editor, make a change
  133.           in Paradox, and then look at the file again to see what changed.
  134.  
  135.           Note: I tried about eight different shareware hex editors. My
  136.                favorite was Hex Workshop 2.10 from BreakPoint Software. It
  137.                is a Windows application and is available in 16 and 32-bit
  138.                versions. It can be found in Lib 4 of the PCUTIL forum. The
  139.                filename is HW16V210.ZIP. It's about 300k so it only takes
  140.                about 3 minutes to download at 14.4 kbps. I am in no way
  141.                affiliated with the authors of Hex Workshop.
  142.  
  143.  
  144.           Not all of the DB and PX header fields are documented. However,
  145.           there is enough information to perform the following tasks.
  146.  
  147.           *    Write a program to retrieve a record and its memo fields, if
  148.                any.
  149.  
  150.                Write a program to browse the table in forward or reverse          *
  151.                primary key sequence.
  152.  
  153.  
  154.           Revision 1.0                    1                    May 11, 1996
  155.  
  156.  
  157.  
  158.  
  159.  
  160.                               PARADOX 4.x FILE FORMATS
  161.  
  162.  
  163.                Write a program to use the primary index to locate a record.           *
  164.  
  165.           *    Write a program to use the secondary index to locate
  166.                records.
  167.  
  168.           *    If Tutility can't rebuild a table, you might be able to use
  169.                a hex editor to make enough repairs so that Tutility can
  170.                proceed.
  171.  
  172.           It would be foolhardy to attempt to write a program to update a
  173.           table based on the information in this document. It might be
  174.           possible to do it but it would be dangerous when you consider the
  175.           number of undocumented header fields. Paradox might be quite
  176.           sensitive to the content of some of the undocumented fields.
  177.  
  178.  
  179.           Paradox Field Types and Lengths
  180.           -------------------------------
  181.  
  182.           The following table shows the Paradox 4.x field types and the
  183.           number of bytes occupied by each type in a data record.
  184.  
  185.           Type   Length
  186.           ----   ------
  187.           N      8
  188.           $      8
  189.           D      4
  190.           S      2
  191.           Ann    nn     1 <= nn <= 255
  192.           Mnn    nn+10  1 <= nn <= 240
  193.           Bnn    nn+10  0 <= nn <= 240
  194.           Unn    nn+10  0 <= nn <= 240 (Only created during IMPORT)
  195.  
  196.           N and $ fields are stored as double-precision floating point
  197.           numbers and are identical except that Paradox displays $ fields
  198.           differently. Paradox automatically rounds $ fields to 2 decimal
  199.           places, uses separators (commas) between three-digit groups, and
  200.           puts parentheses around negative numbers.
  201.  
  202.           IMPORTANT: the rounding for $ fields is for display only. The
  203.           stored value is NOT rounded.
  204.  
  205.           D fields are stored as a signed long integer.
  206.  
  207.           Ann fields are stored as fixed-length character strings. Unused
  208.           positions on the right are filled with nulls (binary zero).
  209.  
  210.  
  211.  
  212.  
  213.           Revision 1.0                    2                    May 11, 1996
  214.  
  215.  
  216.  
  217.  
  218.  
  219.                               PARADOX 4.x FILE FORMATS
  220.  
  221.  
  222.           M, B, and U fields are variable length and are called BLOB
  223.           fields. The length shown here is the fixed-length leader that is
  224.           stored in the record in the DB file.
  225.  
  226.           A blob uses an extra 10 bytes in the DB record. The extra space
  227.           is used to hold the length of the blob, the location of the blob
  228.           in the MB file, and a modification number (used internally by
  229.           Paradox).
  230.  
  231.           Blob data is stored in the MB file. The leader in the DB record
  232.           contains a copy of the first part of the data in MB.
  233.  
  234.           A special case occurs when the entire Blob will fit in the
  235.           leader. In this case the blob is stored in the leader and is not
  236.           written to the MB file.
  237.  
  238.           In general, memo fields should be defined as M1 to minimize
  239.           record size. An M1 field takes 11 bytes in the record. A larger
  240.           memo field, Mnn, can improve performance if all of the following
  241.           conditions are met.
  242.  
  243.            - Most records have non-blank memos
  244.            - Most memos have a length less than or equal to nn
  245.            - Most time-critical operations look at the memo fields
  246.  
  247.  
  248.           Numeric Formats Supported by the 80x86/7
  249.           ----------------------------------------
  250.  
  251.           A byte can store a value between 0 and 255 if the value is
  252.           treated as unsigned. If the byte is treated as a signed value,
  253.           the value can range from -128 to 127.
  254.  
  255.           Short integers are 16 bits long (2 bytes). Unsigned range: 0 to
  256.           65,535. Signed range: -32768 to 32767.
  257.  
  258.           Long integers are 32 bits long (4 bytes). Unsigned range: 0 to
  259.           4,294,967,295. Signed range: -2,147,483,648 to 2,147,483,547.
  260.  
  261.           Double precision floating point numbers are 64 bits long (8
  262.           bytes). Floating point numbers are always interpreted as signed
  263.           values. They provide approximately 15 decimal digits of precision
  264.           with a decimal exponent in the range -307 to 308.
  265.  
  266.           Note: Single precision (32-bits) and extended precision (80-bits)
  267.                numbers are also supported but are not used by Paradox.
  268.                Single precision is pretty useless because it only provides
  269.                for 6 decimal digits of precision. Extended precision is not
  270.  
  271.  
  272.           Revision 1.0                    3                    May 11, 1996
  273.  
  274.  
  275.  
  276.  
  277.  
  278.                               PARADOX 4.x FILE FORMATS
  279.  
  280.  
  281.                used because it is actually intended only for intermediate
  282.                results during computations. Internally, the 80x86/7 uses
  283.                extended precision for all computations.
  284.  
  285.  
  286.           Floating Point
  287.           --------------
  288.  
  289.           Paradox uses double precision floating point (64 bits) for type N
  290.           and $ fields.
  291.  
  292.           This floating point format has a sign bit, 11 bits for the
  293.           exponent, and 52 bits for the significand.
  294.  
  295.           In order to avoid having two sign bits (one for the number and
  296.           one for the exponent), the 80x86 (or 80x87) uses a bias for the
  297.           exponent. The bias is subtracted from the exponent value to
  298.           obtain the true exponent. The true exponent is the power of 2
  299.           that the significand must be multiplied by.
  300.  
  301.           The largest unsigned number that can be expressed with 11 bits is
  302.           2047. The smallest is zero. The 80x86 disallows exponents with
  303.           all bits set to either 0 or 1 (there are a few exceptions to this
  304.           rule). Thus, the 11-bit exponent can range from 1 to 2046. The
  305.           80x86 uses a bias of 1023. This means that numbers with binary
  306.           exponents between -1022 and +1023 can be represented. This is a
  307.           big enough range for most applications.
  308.  
  309.           The significand is always normalized. This means that the binary
  310.           point (binary equivalent of a decimal point) is placed
  311.           immediately to the right of the most significant "1" bit in the
  312.           number. Since normalization forces all numbers to have a 1 to the
  313.           left of the binary point, the 1 is not stored in the field - it
  314.           is assumed to be there. Effectively, this increases the
  315.           significand length to 53.
  316.  
  317.           Consider the following floating point number (the "h" after the
  318.           number denotes hexadecimal notation):
  319.  
  320.           4059200000000000h
  321.  
  322.           The sign bit is zero.
  323.  
  324.           The exponent is 405h = 1029. We subtract the bias to get 6 as the
  325.           true binary exponent.
  326.  
  327.           The significand is 92h (We can ignore trailing zeros). In binary
  328.           this is: 10010010.
  329.  
  330.  
  331.           Revision 1.0                    4                    May 11, 1996
  332.  
  333.  
  334.  
  335.  
  336.  
  337.                               PARADOX 4.x FILE FORMATS
  338.  
  339.  
  340.           When we put back the assumed 1 and the binary point we get:
  341.  
  342.           1.10010010
  343.  
  344.           We multiply this by 2 raised to the 6th power. This is the same
  345.           as shifting the binary point 6 places to the right.
  346.  
  347.           1100100.10
  348.  
  349.           In decimal this is 100 (1100100) + .5 (.10) = 100.5
  350.  
  351.           Note: Binary digits to the right of the binary point correspond
  352.           to negative powers of 2. The following table shows some examples.
  353.  
  354.           Binary Decimal
  355.           ------ ----------
  356.           .1     .5 (1/2)
  357.           .01    .25 (1/4)
  358.           .001   .125 (1/8)
  359.           .0001  .0625 (1/16)
  360.  
  361.           For example, decimal .75 is written in binary as .11
  362.  
  363.  
  364.           A floating point number is treated as zero if its exponent and
  365.           significand bits are all zero. This is an exception to the rule
  366.           that the exponent cannot be all "0" or all "1".
  367.  
  368.           Certain operations can cause the exponent to be all "0" or all
  369.           "1". The special handling that the 80x86/7 uses for these numbers
  370.           is beyond the scope of this document.
  371.  
  372.  
  373.           Date Format
  374.           -----------
  375.  
  376.           Paradox stores a date as a long integer. The integer contains the
  377.           date expressed as the number of days since January 1, 1 (the year
  378.           1 A.D.).
  379.  
  380.           Although dates are expressed (internally) as the number of days
  381.           since 1/1/1, the lowest year that Paradox allows you to enter is
  382.           100. If a value less than 100 is entered, it is treated as 19xx,
  383.           where xx is the value.
  384.  
  385.           The internal representation of 1/1/100 (the lowest valid date) is
  386.           36,160 (00008D40h). The internal representation of January 2, 100
  387.  
  388.  
  389.  
  390.           Revision 1.0                    5                    May 11, 1996
  391.  
  392.  
  393.  
  394.  
  395.  
  396.                               PARADOX 4.x FILE FORMATS
  397.  
  398.  
  399.           is 36,161. The internal representation of May 4, 1996 is decimal
  400.           728,783 (000B1ECFh).
  401.  
  402.           Paradox accepts dates between Jan 1, 100 and Dec 31, 9999.
  403.  
  404.  
  405.           Blob Fields
  406.           -----------
  407.  
  408.           Type M (Memo) and B (Binary) fields are blob fields.
  409.  
  410.           In a DB record, a blob field is stored as a fixed-length data
  411.           field (called the leader) followed by 10 bytes with the following
  412.           fields:
  413.  
  414.                An unsigned long integer (32 bits) that contains the offset          *
  415.                of the blob's data block in the MB file and an index value.
  416.  
  417.                An unsigned long integer that contains the length of the          *
  418.                blob.
  419.  
  420.           *    An unsigned short integer (16 bits) that contains the
  421.                modification number from the MB file header.
  422.  
  423.           The length of the leader may be zero for type B fields. The
  424.           leader for a type M field must be at least one byte.
  425.  
  426.           If you define a memo field as M40, then the length of the leader
  427.           is 40. If the memo data is over 40 bytes long, then the entire
  428.           memo is stored in the MB file and the leader contains a copy of
  429.           the first 40 bytes. If the memo data is less than 41 bytes long,
  430.           then the leader contains all of the data and nothing is stored in
  431.           the MB file.
  432.  
  433.           The MB file is described later in this document.
  434.  
  435.           Although the numeric data in a DB record data is stored in
  436.           modified big endian format, the 10 bytes of blob information are
  437.           stored in little endian (native 80x86) format.
  438.  
  439.           We'll refer to the first four bytes after the leader as
  440.           MB_Offset. MB_Offset is used to locate the blob data.
  441.  
  442.           If MB_Offset = 0 then the entire blob is contained in the leader.
  443.  
  444.           Take the low-order byte from MB_Offset and call it MB_Index.
  445.           Change the low-order byte of MB_Offset to zero.
  446.  
  447.  
  448.  
  449.           Revision 1.0                    6                    May 11, 1996
  450.  
  451.  
  452.  
  453.  
  454.  
  455.                               PARADOX 4.x FILE FORMATS
  456.  
  457.  
  458.           If MB_Index is FFh, then MB_Offset contains the offset of a type
  459.           02 block in the MB file.
  460.  
  461.           Otherwise, MB_Offset contains the offset of a type 03 block in
  462.           the MB file. MB_Index contains the index of an entry in the Blob
  463.           Pointer Array in the type 03 block.
  464.  
  465.           Refer to the MB file description for block formats.
  466.  
  467.  
  468.           Big and Little Endians
  469.           ----------------------
  470.  
  471.           80x86 processors normally store numeric fields in "little endian"
  472.           format. This means the least significant byte of the number has
  473.           the lowest address (the little end comes first).
  474.  
  475.           For example, a short integer containing decimal 10 has the
  476.           following hexadecimal representation: 000Ah (the lower case h
  477.           after the number means it is a hex number). An 80x86 processor
  478.           would store this as 0A00. The least significant byte has the
  479.           lowest address.
  480.  
  481.           Many processors (like the Motorola processors used in Macintosh
  482.           computers) store numbers is "big endian" format. The most
  483.           significant byte has the lowest address (the big end comes
  484.           first).
  485.  
  486.           Processors that use big endian format store 000Ah as 000A.
  487.  
  488.           Little endian format causes some complications when you attempt
  489.           to sort a number as a string. 256, expressed as a short integer,
  490.           is 0100h. This is stored (little endian) as 0001. If you sort a
  491.           file that contains 10 and 100, then 10 (0A00) sorts after 256
  492.           (0001).
  493.  
  494.           Big endian notation is a partial solution to this problem. 10
  495.           (000A) will clearly sort before 256 (0100). Sorts using big
  496.           endian notation fail to work correctly when signed numbers are
  497.           used. -1 is expressed as FFFFh and will sort higher than any
  498.           other number.
  499.  
  500.           If all numbers are signed and all floating point numbers are
  501.           normalized, then there is a simple modification to big endian
  502.           format that makes sorting work correctly. The sign bit (leftmost
  503.           bit) is complemented (reversed) when numbers are stored. (It is
  504.           complemented again before the numbers are used in computations).
  505.  
  506.  
  507.  
  508.           Revision 1.0                    7                    May 11, 1996
  509.  
  510.  
  511.  
  512.  
  513.  
  514.                               PARADOX 4.x FILE FORMATS
  515.  
  516.  
  517.           -2 becomes 7FFE
  518.           -1 becomes 7FFF
  519.            1 becomes 8001
  520.            2 becomes 8002
  521.  
  522.           Note that negative numbers will sort before positive numbers.
  523.           "Larger" negative numbers will sort before "smaller" negative
  524.           numbers.
  525.  
  526.           Since double-precision floating point numbers on the 80x86/7 are
  527.           ALWAYS signed and normalized, they will also sort correctly.
  528.  
  529.           Because it is important, I will repeat that this modification to
  530.           big endian format only makes sorting work correctly if all
  531.           numbers are signed. This is probably why Paradox doesn't support
  532.           unsigned fields.
  533.  
  534.           Paradox uses little endian format (the natural 80x86 format) for
  535.           control structures like file headers and block headers. It uses
  536.           modified big endian format for numeric data, i.e., type N, $, D,
  537.           and S fields in data records.
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.           Revision 1.0                    8                    May 11, 1996
  568.  
  569.  
  570.  
  571.  
  572.  
  573.                               PARADOX 4.x FILE FORMATS
  574.  
  575.  
  576.           The DB File
  577.           -----------
  578.  
  579.           The DB file contains the data records for a table. The first
  580.           block in the DB file is the table header. The table header is
  581.           followed by the data blocks.
  582.  
  583.           The DB file has the following logical structure.
  584.  
  585.           +--------------+
  586.           | Table Header |
  587.           |              |         
  588.           |First Block   |-------------------+
  589.           |Last Block    |----+              |
  590.           |Free Blocks   |--+ |              |
  591.           +--------------+  | |              |
  592.                             | |              |
  593.               +-------------+ |              V
  594.               V               |          +-------------+  Next
  595.           +------+ Next       |  +------>|Data block 1 |-------+
  596.           | Free |-----+      |  |       +-------------+       |
  597.           +------+     |      |  |                             |
  598.                        |      |  |       +-------------+       |
  599.           +------+     |      |  +-------|Data block 2 |<------+
  600.           | Free |<----+      |   Prev   +-------------+   
  601.           +------+            |                
  602.              |                |               ...
  603.              |Next            |  +--->
  604.              |                |  |
  605.              V                |  |       +-------------+
  606.                               |  +-------|Data block n |
  607.             ...               |   Prev   +-------------+
  608.                               |              ^
  609.                               |              |
  610.                               +--------------+
  611.  
  612.  
  613.           The table header contains the block number of the first data
  614.           block, the last data block, and the first free block.
  615.  
  616.           Blocks are numbered. The first block after the table header is
  617.           block 1.
  618.  
  619.           All blocks (except the table header) are the same size. This
  620.           means the block number can be used to compute the offset of the
  621.           block within the file:
  622.  
  623.  
  624.  
  625.  
  626.           Revision 1.0                    9                    May 11, 1996
  627.  
  628.  
  629.  
  630.  
  631.  
  632.                               PARADOX 4.x FILE FORMATS
  633.  
  634.  
  635.           block offset = block length * ( block number - 1 ) + table header
  636.           length
  637.  
  638.           Data blocks are organized as a bi-directional linked list, i.e.,
  639.           the block header in each block contains the block number of the
  640.           next and previous blocks in the linked list. The blocks are
  641.           linked in ascending key sequence based on the first record in
  642.           each block. 
  643.  
  644.           Within a block, records are stored in ascending sequence. The
  645.           block header contains the offset of the last record in the block.
  646.           When a record is deleted, any records that follow it "move up" to
  647.           overwrite it and the record length is subtracted from the last
  648.           record offset in the block header.
  649.  
  650.           Free blocks are organized as a linked list. A free block contains
  651.           the block number of the next free block but does not contain the
  652.           block number of the previous free block.
  653.  
  654.           A block is added to the free block list when all of the records
  655.           in the block are deleted. When records are inserted into the
  656.           table and a new block must be allocated, the first free block is
  657.           plucked from the list. The next free block becomes the new first
  658.           block.
  659.  
  660.           If a new block is needed and there are no free blocks, then a new
  661.           block is added to the end of the file.
  662.  
  663.           Note: Block 1 is never allowed to be a free block. If block 1 is
  664.                emptied, then the data from the next block in the linked
  665.                list is copied to block 1. The block whose data was copied
  666.                to block 1 is then added to the free list.
  667.  
  668.           When you add a record, Paradox tries to put it in the data block
  669.           that contains the record that is just before it in key sequence.
  670.           If there is no room in the block, then:
  671.  
  672.           *    If you are inserting a record in the last data block, a new
  673.                block is allocated and the existing block does not split.
  674.  
  675.           *    Otherwise, the block is split. Half of the records remain in
  676.                the original block. A new block is created for the remaining
  677.                records.
  678.  
  679.           Of course, linked list pointers are adjusted whenever a new block
  680.           is inserted. The indexes will also be updated. The primary index
  681.           contains one record for each data block. The key value stored in
  682.  
  683.  
  684.  
  685.           Revision 1.0                   10                    May 11, 1996
  686.  
  687.  
  688.  
  689.  
  690.  
  691.                               PARADOX 4.x FILE FORMATS
  692.  
  693.  
  694.           the index is the key of the first record in the block. (More
  695.           about this later.)
  696.  
  697.  
  698.           Table Header
  699.           ------------
  700.  
  701.           The table header is the first block in the DB file. Some of the
  702.           fields in the header are described below. (The list is far from
  703.           complete.)
  704.  
  705.           Field type codes are: UB = Unsigned byte. US = Unsigned Short
  706.           integer. UL = Unsigned Long integer.
  707.  
  708.            Hex   Field
  709.           Offset Type  Description
  710.           ------ ----- --------------------------------------------------
  711.           000000 US    Record length
  712.           000002 US    Length of the header block.
  713.                        Usually 2k (even if the data block is not 2k).
  714.                        The size may increase if there are a lot of fields
  715.                        with long field names.
  716.                        Worst case: 10k for 255 fields with 25-character
  717.                        names.
  718.           000004 UB    File type
  719.                        00 - DB file for an keyed table
  720.                        02 - DB file for an unkeyed table
  721.           000005 UB    Data block size code
  722.                        01 - Block size is 1k
  723.                        02 - Block size is 2k
  724.                        03 - Block size is 3k (not used in 4.5)
  725.                        04 - Block size is 4k
  726.           000006 UL    Number of records in DB
  727.           00000A US    Number of blocks in use
  728.           00000C US    Total blocks in file
  729.           00000E US    First data block (always 1)
  730.           000010 US    Last block in use
  731.           000021 UB    Number of fields
  732.           000023 UB    Number of key fields
  733.           00004D US    Block number of first free block
  734.           000078       Start of field description array
  735.  
  736.           The field description array contains two bytes for each field. 
  737.  
  738.           The first byte contains the field type code.
  739.  
  740.  
  741.  
  742.  
  743.  
  744.           Revision 1.0                   11                    May 11, 1996
  745.  
  746.  
  747.  
  748.  
  749.  
  750.                               PARADOX 4.x FILE FORMATS
  751.  
  752.  
  753.           Code Field Type
  754.           ---- ------------
  755.            01  A
  756.            02  D
  757.            03  S
  758.            05  $
  759.            06  N
  760.            0C  M
  761.            0D  B
  762.  
  763.           The second byte contains the field length.
  764.  
  765.           The length for $ and N is always 8.
  766.           The length for D is always 4.
  767.           The length for S is always 2.
  768.           The length for A ranges from 1 to 255 (01h to FFh).
  769.           The length for M ranges from 11 to 250 (0Bh to FAh).
  770.           The length for B ranges from 10 to 250 (0Ah to FAh).
  771.  
  772.           The length for a B or M (blob) field includes 10 bytes used to
  773.           hold the blob's length and its location in the MB file.
  774.  
  775.           Field names start at offset 120 (78h) plus 83 plus 6 times the
  776.           number of fields. Field names are in field number sequence. Each
  777.           field name is a null-terminated string (00h marks the end of the
  778.           string).
  779.  
  780.           No data records are stored in the table header block.
  781.  
  782.  
  783.           DB Data Blocks
  784.           --------------
  785.  
  786.           The following table describes the format of a DB data block.
  787.  
  788.            Hex   Field
  789.           Offset Type  Description
  790.           ------ ----- --------------------------------------------------
  791.           000000 US    Next block number (Zero if last block)
  792.           000002 US    Previous block number (Zero if first block)
  793.           000004 US    Offset of last record in block.
  794.           000006       First data record.
  795.  
  796.           Records are stored contiguously. There are no gaps between
  797.           records. Records contain no slack bytes between fields.
  798.  
  799.           The last record offset is relative to the end of the header. Add
  800.           6 to calculate the offset from the start of the block.
  801.  
  802.  
  803.           Revision 1.0                   12                    May 11, 1996
  804.  
  805.  
  806.  
  807.  
  808.  
  809.                               PARADOX 4.x FILE FORMATS
  810.  
  811.  
  812.           If the block is empty, the offset is set to 0 minus record
  813.           length.
  814.  
  815.           A zero in offset means that the block contains one record.
  816.  
  817.           Since Paradox knows the record length and the block length, it
  818.           can use the last record offset to compute the number of records
  819.           in the block and the amount of free space in the block.
  820.  
  821.           If you use a hex editor to look at the data, remember that the
  822.           fields in the block header are in little endian format but any
  823.           numeric data fields are in modified big endian format.
  824.  
  825.           If a record is deleted, records after it move up in the block and
  826.           the record length is subtracted from the last record offset in
  827.           the block header.
  828.  
  829.           Records are stored in key sequence. If a record is inserted in
  830.           the block, then records with higher keys "move down" to make room
  831.           for the new record. Record length is added to the last record
  832.           offset in the block header.
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.           Revision 1.0                   13                    May 11, 1996
  863.  
  864.  
  865.  
  866.  
  867.  
  868.                               PARADOX 4.x FILE FORMATS
  869.  
  870.  
  871.           The PX File
  872.           -----------
  873.  
  874.           The PX file contains the primary index records for a keyed table.
  875.           The first block in the PX file is the index header. The index
  876.           header is followed by the index data blocks.
  877.  
  878.           The format of the PX file is very similar to the format of the DB
  879.           file. Index blocks are chained in a bi-directional linked list
  880.           and free blocks are chained in a linked list.
  881.  
  882.           In addition to the list structure, the index blocks are organized
  883.           into a hierarchical (tree) structure. The tree structure is the
  884.           primary structure used when accessing the index.
  885.  
  886.           A tree is a typical index structure. Almost any book about data
  887.           bases will describe the creation and maintenance of a tree-
  888.           structured index, so I won't go into too many details here.
  889.           However, I will show a simple example.
  890.  
  891.           In this example we will assume that we have a set of 10,000
  892.           records sorted in key sequence. The key is an integer between 1
  893.           and 10,000.
  894.  
  895.           We will also assume that a data block holds 10 data records and
  896.           an index block holds 10 index records. (This is unrealistic - the
  897.           index block would actually hold many more index records. However,
  898.           assuming 10 indexes per block simplifies the example.)
  899.  
  900.           There is one index record per data block and the index record
  901.           contains the key of the first record in the data block.
  902.  
  903.           When we insert the first data record in the DB file, an index
  904.           record is created in the PX file. The index record contains the
  905.           block number of the first data block (block 1 in DB) and the key
  906.           of the first record in that data block (key = 1).
  907.  
  908.           When records 2 through 10 are inserted, they are placed in the
  909.           first data block in DB. No additional index records are
  910.           generated.
  911.  
  912.           When record 11 is inserted, a new data block is created. We must
  913.           insert an index record containing the key of the first record in
  914.           the block (key = 11) and the block number of the second data
  915.           block. The index now contains two records.
  916.  
  917.           If we continue inserting records, then we will eventually insert
  918.           the record whose key is 101.
  919.  
  920.  
  921.           Revision 1.0                   14                    May 11, 1996
  922.  
  923.  
  924.  
  925.  
  926.  
  927.                               PARADOX 4.x FILE FORMATS
  928.  
  929.  
  930.           When data record 101 is inserted, there is no room for the index
  931.           record in the first index block. A new index block is created to
  932.           hold the index record for 101. We now have two index blocks.
  933.  
  934.           An index block is created to index the two index blocks. We will
  935.           refer to this as the level 2 index and refer to the first two
  936.           index blocks as the level 1 index. The level 2 index contains the
  937.           first key from each level 1 index block. We now have a structure
  938.           that looks like this:
  939.  
  940.  
  941.           Level 2 Index      Level 1 Index
  942.  
  943.           Key                Keys in Block
  944.           ----               ----
  945.              1 ----------->  1, 11,21, 31, ..., 91
  946.            101 ----------->  101
  947.  
  948.           The level 2 record points to a block in the level 1 index. The
  949.           "pointer" is the block number of the level 1 index block.
  950.  
  951.           When record 1001 is inserted, a second level 2 index block will
  952.           be created and a level 3 index block will be created to index the
  953.           level 2 blocks.
  954.  
  955.           After 10,000 records have been inserted, we will have a structure
  956.           like the figure on the next page.
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.           Revision 1.0                   15                    May 11, 1996
  981.  
  982.  
  983.  
  984.  
  985.  
  986.                               PARADOX 4.x FILE FORMATS
  987.  
  988.  
  989.           Index Structure for 10,000 Records
  990.           ----------------------------------
  991.  
  992.  
  993.           +-----+            Level 3 (Index root)
  994.           |    1|--+
  995.           | 1001|  |
  996.           | 2001|  |
  997.           | 3001|  |
  998.           | 4001|  |
  999.           | 5001|  |
  1000.           | 6001|  |
  1001.           | 7001|  |
  1002.           | 8001|  |
  1003.           | 9001|--|--------------------+
  1004.           +-----+  |                    |
  1005.                    V                    V
  1006.                +-----+        ...     +-----+        Level 2
  1007.                |    1|--+             | 9001|
  1008.                |  101|--|----------+  | 9101|
  1009.                |  201|  |          |  | 9201|
  1010.                |  301|  |          |  | 9301|
  1011.                |  401|  |          |  | 9401|
  1012.                |  501|  |          |  | 9501|
  1013.                |  601|  |          |  | 9601|
  1014.                |  701|  |          |  | 9701|
  1015.                |  801|  |          |  | 9801|
  1016.                |  901|  |          |  | 9901|------+
  1017.                +-----+  |          |  +-----+      |
  1018.                         V          V               V
  1019.                     +-----+    +-----+   ...   +-----+   Level 1
  1020.                     |    1|--+ |  101|         | 9901|
  1021.                     |   11|  | |  111|         | 9911|
  1022.                     |   21|  | |  121|         | 9921|
  1023.                     |   31|  | |  131|         | 9931|
  1024.                     |   41|  | |  141|         | 9941|
  1025.                     |   51|  | |  151|         | 9951|
  1026.                     |   61|  | |  161|         | 9961|
  1027.                     |   71|  | |  171|         | 9971|
  1028.                     |   81|  | |  181|         | 9981|
  1029.                     |   91|  | |  191|         | 9991|----+
  1030.                     +-----+  | +-----+         +-----+    |
  1031.                              |                            |
  1032.                              |                            |
  1033.                              V                            V
  1034.                        Data block that              Data block that
  1035.                        contains records             contains records 
  1036.                        1 through 10                 9,991 through 10,000
  1037.  
  1038.  
  1039.           Revision 1.0                   16                    May 11, 1996
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.                               PARADOX 4.x FILE FORMATS
  1046.  
  1047.  
  1048.           To find a record via the index, we start at the root (level 3)
  1049.           index and, at each index level, pick the entry that has the
  1050.           highest key that is less than or equal to the key we are trying
  1051.           to locate. Keep in mind that the block number we get from index
  1052.           records above level 1 will refer to a block in the PX file. The
  1053.           block number we get from the level 1 index refers to a block in
  1054.           the DB file.
  1055.  
  1056.           For example, to find the data record with key 185 we proceed as
  1057.           follows:
  1058.  
  1059.                From the root index we pick 1. This points to the level 2          *
  1060.                index that contains 1, 101, 201, ...
  1061.  
  1062.                From the level 2 index we pick 101. This points to the          *
  1063.                level 1 index that contains 101, 111, 121, ...
  1064.  
  1065.           *    From the level 1 index we pick 181. This points to the DB
  1066.                block that contains data records 181 through 190.
  1067.  
  1068.                Find the record with key 185 in the DB block. If the record          *
  1069.                has not been deleted, we will find it.
  1070.  
  1071.  
  1072.           Since the entries in all blocks (PX and DB) are stored in key
  1073.           sequence, a binary search can be used to locate the desired index
  1074.           or data record in a block.
  1075.  
  1076.  
  1077.           Statistics
  1078.           ----------
  1079.  
  1080.           Paradox keeps statistics that are (probably) used to optimize
  1081.           queries. The statistics are stored in the index and are updated
  1082.           as records are inserted and deleted.
  1083.  
  1084.           A record in the level 1 index contains the number of records in
  1085.           the DB block that it points to.
  1086.  
  1087.           A record in the level 2 index contains the sum of the statistics
  1088.           (DB record counts) from the level 1 index block that it points
  1089.           to.
  1090.  
  1091.           A record in the level n index contains the sum of the statistics
  1092.           from the level n-1 index block that it points to.
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.           Revision 1.0                   17                    May 11, 1996
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.                               PARADOX 4.x FILE FORMATS
  1105.  
  1106.  
  1107.           For example, in the sample structure each level 1 index record
  1108.           contains 10. Each level 2 record contains 100. Each level 3
  1109.           record contains 1000.
  1110.  
  1111.           If the record with key = 128 is deleted, then the level 1 record
  1112.           with key 121 contains 9. The level 2 record with key 101 contains
  1113.           99. The level 3 index with key 1 contains 999.
  1114.  
  1115.  
  1116.           Index Header
  1117.           ------------
  1118.  
  1119.           The index header is the first block in the PX file. Some of the
  1120.           fields in the header are described below. (The list is far from
  1121.           complete.)
  1122.  
  1123.  
  1124.            Hex   Field
  1125.           Offset Type  Description
  1126.           ------ ----- --------------------------------------------------
  1127.           000000 US    Index record length
  1128.           000002 US    Length of index header size (2k)
  1129.           000004 UB    File type
  1130.                        01 - PX file
  1131.           000005 UB    Index block size code
  1132.                        01 - Block size is 1k
  1133.                        02 - Block size is 2k
  1134.                        03 - Block size is 3k (not used in 4.5)
  1135.                        04 - Block size is 4k
  1136.           000006 UL    Number of records in PX
  1137.           00000A US    Number of blocks in use
  1138.           00000C US    Total blocks in file
  1139.           00000E US    First index data block (always 1)
  1140.           000010 US    Last block in use
  1141.           00001E US    Block number of index root
  1142.           000020 UB    Number of index levels
  1143.           000021 UB    Number of fields in index
  1144.  
  1145.           The index record length is six greater than the sum of the
  1146.           lengths of the key fields.
  1147.  
  1148.           The number of fields in the index is the same as the number of
  1149.           key fields for the table.
  1150.  
  1151.           Most of the block is filled with nulls.
  1152.  
  1153.           No index records are stored in this block.
  1154.  
  1155.  
  1156.  
  1157.           Revision 1.0                   18                    May 11, 1996
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.                               PARADOX 4.x FILE FORMATS
  1164.  
  1165.  
  1166.           Index Blocks
  1167.           ------------
  1168.  
  1169.           Within each PX block the records (primary key values) are stored
  1170.           in ascending sequence. Records following a deleted record "move
  1171.           up".
  1172.  
  1173.           The block header is just like the one used in the DB file.
  1174.  
  1175.            Hex   Field
  1176.           Offset Type  Description
  1177.           ------ ----- --------------------------------------------------
  1178.           000000 US    Next block number (Zero if last block)
  1179.           000002 US    Previous block number (Zero if first block)
  1180.           000004 US    Offset of last record in block.
  1181.           000006       First index record.
  1182.  
  1183.           An index record has the format:
  1184.  
  1185.           <primary key fields> followed by six bytes used as follows:
  1186.  
  1187.           Bytes Contents
  1188.           ----- -------------------------------------------------
  1189.           1-2   Unsigned short integer that contains the block number
  1190.                 associated with the key field.
  1191.                 For a level 1 block, this is a DB file block number.
  1192.                 For a block above level 1, this is a PX block number.
  1193.  
  1194.           3-4   Unsigned short integer containing statistics.
  1195.  
  1196.           5-6   Unsigned short integer.
  1197.                 Purpose unknown. Usually contains zero.
  1198.  
  1199.           If you use a hex editor to look at the data, remember that the
  1200.           fields in the block header are in little endian format.
  1201.  
  1202.           An index record is treated as data. Numeric fields, including the
  1203.           data block number and statistics, are stored in modified big
  1204.           endian format.
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.           Revision 1.0                   19                    May 11, 1996
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.                               PARADOX 4.x FILE FORMATS
  1223.  
  1224.  
  1225.           The MB File
  1226.           -----------
  1227.  
  1228.           The MB file is used to store BLOB (Binary Large Object) data.
  1229.  
  1230.           The format of the MB file is very different from the format of
  1231.           the DB and PX files. The blocks are not chained and the block
  1232.           length varies. The header is always 4k (1000h) long. Blocks that
  1233.           follow the header have a length that is a multiple of 4k.
  1234.  
  1235.           Each block has the following information in the first three
  1236.           bytes.
  1237.  
  1238.           UB - Record type
  1239.                00 - Header block
  1240.                02 - Single blob block
  1241.                03 - Suballocated block
  1242.                04 - Free block
  1243.           US - Number of 4k chunks in this block.
  1244.                The maximum size is FFFFh x 1000h = 65,535 x 4096.
  1245.                This is 256 megabytes (the maximum length of a blob).
  1246.  
  1247.  
  1248.           Two methods are used to allocate space for blobs.
  1249.  
  1250.                A separate block (record type 02) is allocated for a blob          *
  1251.                over 2k bytes long. The length of the data block is the
  1252.                smallest multiple of 4k that is larger than the blob.
  1253.  
  1254.                One 4k block may be suballocated (record type 03) to hold up          *
  1255.                to 64 small (under 2k) blobs.
  1256.  
  1257.  
  1258.           The Blob Header
  1259.           ---------------
  1260.  
  1261.           The blob header is the first block in the MB file. It is 4096
  1262.           (4k) bytes long. The block contains the following fields.
  1263.  
  1264.            Hex   Field
  1265.           Offset Type  Description
  1266.           ------ ----- --------------------------------------------------
  1267.           000000 UB    Record type = 00h (Header block)
  1268.           000001 US    Size of block divided by 4k
  1269.                        1 because the header is 4k
  1270.           000003 US    Modification count
  1271.                        This is reset to 1 by a table restructure.
  1272.                        Every time a blob is updated, this field is
  1273.  
  1274.  
  1275.           Revision 1.0                   20                    May 11, 1996
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.                               PARADOX 4.x FILE FORMATS
  1282.  
  1283.  
  1284.                        incremented. I don't know why this is done.
  1285.                        The mod number is stored with the blob data.
  1286.                        Again, I don't know why.
  1287.  
  1288.                        *** ALL OF THE FOLLOWING ARE GUESSES ***
  1289.  
  1290.           00000B US    Base size of data blocks (1000h).
  1291.           00000D US    Size of suballocated data blocks (1000h).
  1292.           000010 UB    Suballocation chunk size (10h)
  1293.           000011 US    Number of suballocations per block (00040h)
  1294.           000013 US    Suballocation threshold (0800h)
  1295.                        The border line between "big" and "small" blobs.
  1296.                        Big blobs get their own blocks.
  1297.                        Several small blobs may be stored in one block.
  1298.  
  1299.           Note: I can't even guess at the rest of the header block's
  1300.                fields. I assume that there's some kind of garbage
  1301.                collection scheme that saves data in the header block, but I
  1302.                haven't been able to figure it out.
  1303.  
  1304.  
  1305.           Single Blob Block (Type 02)
  1306.           ---------------------------
  1307.  
  1308.           A single blob block can appear anywhere in the MB file.
  1309.  
  1310.           A "long" blob is stored in this kind of block.
  1311.  
  1312.           The block length is the smallest multiple of 4k that is greater
  1313.           than or equal to the length of the blob.
  1314.  
  1315.           The block header contains the following fields.
  1316.  
  1317.            Hex   Field
  1318.           Offset Type  Description
  1319.           ------ ----- --------------------------------------------------
  1320.           000000 UB    Record type = 02h (block contains one blob)
  1321.           000001 US    Size of block divided by 4k
  1322.           000003 UL    Length of the blob.
  1323.           000007 US    Modification number
  1324.                        This is reset to 1 by a table restructure.
  1325.           000009       Blob data starts here
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.           Revision 1.0                   21                    May 11, 1996
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.                               PARADOX 4.x FILE FORMATS
  1341.  
  1342.  
  1343.           Suballocated Block (Type 03)
  1344.           ----------------------------
  1345.  
  1346.           A suballocated block can appear anywhere in the MB file.
  1347.  
  1348.           Up to 64 short blobs may be stored in this type of block.
  1349.  
  1350.           A suballocated block is 4k bytes long. It has a 12-byte header
  1351.           followed by an array of up to 64 5-byte blob pointers.
  1352.  
  1353.           The DB field that "owns" a blob contains the offset of the block
  1354.           (from the start of the MB file) and the index of one of the
  1355.           entries in the blob pointer array. The array entry points to the
  1356.           blob data.
  1357.  
  1358.           This method of using indirect pointers (a pointer to a pointer)
  1359.           is quite common. It simplifies garbage collection. Paradox can
  1360.           move data around within the block to consolidate non-contiguous
  1361.           chunks of free space. All it has to do is update the pointer
  1362.           array within the block.
  1363.  
  1364.           The 12-byte block header contains the following fields.
  1365.  
  1366.            Hex   Field
  1367.           Offset Type  Description
  1368.           ------ ----- --------------------------------------------------
  1369.           000000 UB    Record type = 03h (Suballocated block)
  1370.           000001 US    Size of block divided by 4k
  1371.                        1 because the block size is 4k
  1372.  
  1373.           Note: There are nine more bytes in the header. I have no idea
  1374.                what they contain.
  1375.  
  1376.           The blob pointer array follows the header. The array has 64
  1377.           entries numbered from 00h to 3Fh. Entries are used in reverse
  1378.           order. The 3Fh entry is used first. Then the 3Eh entry, then 3D,
  1379.           and so on ..
  1380.  
  1381.           The offset (from the start of the block) of the entry indexed by
  1382.           i is calculated as: offset = 12 + ( 5 * i )
  1383.  
  1384.           Each entry is 5 bytes long and has the following format.
  1385.  
  1386.  
  1387.            Hex   Field
  1388.           Offset Type  Description
  1389.           ------ ----- --------------------------------------------------
  1390.           000000 UB    Data offset divided by 16
  1391.  
  1392.  
  1393.           Revision 1.0                   22                    May 11, 1996
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.                               PARADOX 4.x FILE FORMATS
  1400.  
  1401.  
  1402.                        The offset is measured from start of the 4k block.
  1403.                        If this is zero, then the blob was deleted and
  1404.                        the space has been reused for another blob (which
  1405.                        is associated with another entry in the array).
  1406.           000001 UB    Data length divided by 16 (rounded up)
  1407.           000002 US    Modification number from blob header
  1408.                        This is reset to 1 by a table restructure.
  1409.           000004 UB    Data length modulo 16.
  1410.                        If this is zero, then the associated blob has been
  1411.                        deleted and the space can be reused
  1412.                        For an active blob, this value will be between
  1413.                        01h and 10h
  1414.  
  1415.  
  1416.           Note: Suballocations are made in 16-byte chunks. The first
  1417.                available chunk is at offset 0150h in the block. Multiply
  1418.                the first byte of the pointer array entry by 16 to get the
  1419.                offset. The next byte is the number of chunks. The last byte
  1420.                tells you how many bytes of data there are in the last
  1421.                chunk. I don't know the purpose of the modification number.
  1422.  
  1423.           For example, if an array entry looks like: 25030F0007 then the
  1424.           data associated with the entry starts at offset 0250h (25h times
  1425.           10h) and has 10h times 03h bytes allocated (48 bytes). The actual
  1426.           data length is 27h (39 bytes) because there are only 7 bytes of
  1427.           data in the last 16 byte chunk. The modification number is in
  1428.           little endian format and is 000Fh (15).
  1429.  
  1430.  
  1431.           Free Block (Type 04)
  1432.           --------------------
  1433.  
  1434.           A free block can appear anywhere in the MB file.
  1435.  
  1436.           The block length is a multiple of 4k.
  1437.  
  1438.           If there are several contiguous free blocks, then the combined
  1439.           length is placed in the first one.
  1440.  
  1441.           The block header contains the following fields.
  1442.  
  1443.            Hex   Field
  1444.           Offset Type  Description
  1445.           ------ ----- --------------------------------------------------
  1446.           000000 UB    Record type = 04h (free block)
  1447.           000001 US    Size of block divided by 4k
  1448.  
  1449.  
  1450.  
  1451.  
  1452.           Revision 1.0                   23                    May 11, 1996
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458.                               PARADOX 4.x FILE FORMATS
  1459.  
  1460.  
  1461.           If the blob in a type 03h block (single blob block) is deleted,
  1462.           then its block becomes a free block. If all of the blobs in a
  1463.           type 02h (suballocated block) are deleted, then the block becomes
  1464.           a free block.
  1465.  
  1466.           Deletions happen more often than you might imagine. Whenever you
  1467.           modify a blob, the original blob is deleted and the modified
  1468.           version is saved as a new blob.
  1469.  
  1470.           Paradox never updates a blob "in-place".
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.           Revision 1.0                   24                    May 11, 1996
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.                               PARADOX 4.x FILE FORMATS
  1518.  
  1519.  
  1520.           X** File
  1521.           --------
  1522.  
  1523.           An X** file contains the data records for a secondary index.
  1524.           There is one record for each record in the DB file.
  1525.  
  1526.           An X** file has the same logical format as a DB file.
  1527.  
  1528.           The X** data record contains the secondary index fields followed
  1529.           by the primary index fields. An additional type S field named
  1530.           "Hint" is the last field in the record. All fields except "Hint"
  1531.           are included in the record key.
  1532.  
  1533.           For example, if your data record has the key "Custid" and you
  1534.           define a compound secondary index on "Last Name" and "First
  1535.           Name", then the X** record contains four fields: [Last Name],
  1536.           [First Name], [Custid], and [Hint]. The first three fields are in
  1537.           the primary index for the X** file.
  1538.  
  1539.           [Hint] contains the block number of the DB file block that
  1540.           contains the record associated with the index record. This means
  1541.           that the DB record can retrieved directly. It doesn't have to be
  1542.           located via the primary index in the PX file.
  1543.  
  1544.           Although [Hint] is defined as a type S field, it is treated as an
  1545.           unsigned integer by Paradox. Paradox knows it's a block number.
  1546.  
  1547.           Note: If you specify more than 16 secondary index fields, then
  1548.                only the first 16 fields are included in the index. Primary
  1549.                index fields may be included in the index but the first
  1550.                primary index field may not be the first secondary index
  1551.                field.
  1552.  
  1553.  
  1554.           X** File Header
  1555.           ---------------
  1556.  
  1557.           The X** file header is the first block in the X** file. It has
  1558.           the same format as the Table Header (DB File Header).
  1559.  
  1560.            Hex   Field
  1561.           Offset Type  Description
  1562.           ------ ----- --------------------------------------------------
  1563.           000000 US    Record length
  1564.           000002 US    Length of the header block.
  1565.                        Usually 2k (even if the data block is not 2k).
  1566.           000004 UB    File type
  1567.                        08 - Secondary index data file
  1568.  
  1569.  
  1570.           Revision 1.0                   25                    May 11, 1996
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.                               PARADOX 4.x FILE FORMATS
  1577.  
  1578.  
  1579.           000005 UB    Data block size code
  1580.                        01 - Block size is 1k
  1581.                        02 - Block size is 2k
  1582.                        03 - Block size is 3k (not used in 4.5)
  1583.                        04 - Block size is 4k
  1584.           000006 UL    Number of records in X**
  1585.           00000A US    Number of blocks in use
  1586.           00000C US    Total blocks in file
  1587.           00000E US    First data block (always 1)
  1588.           000010 US    Last block in use
  1589.           000021 UB    Number of fields
  1590.           000023 UB    Number of key fields
  1591.           00004D US    Block number of first free block
  1592.           000078       Start of field description array
  1593.  
  1594.           The field description array contains two bytes for each field.
  1595.  
  1596.           (See the Table Header description.)
  1597.  
  1598.           Field names start at offset 120 (78h) plus 83 plus 6 times the
  1599.           number of fields. Field names are in field number sequence. Each
  1600.           field name is a null-terminated string (00h marks the end of the
  1601.           string).
  1602.  
  1603.           For a compound index (more than one secondary index field), the
  1604.           fields have the same names that they have in the DB file.
  1605.  
  1606.           For a simple index (only one secondary index field), the name of
  1607.           the index field is replaced by the name "Sec Key". This is really
  1608.           stupid!
  1609.  
  1610.           The primary key field names follow the secondary index field
  1611.           names. "Hint" follows the primary key field names.
  1612.  
  1613.           Immediately after the terminating null for [Hint], there is a
  1614.           series of n unsigned short integers, where n is the number of
  1615.           fields in the record. The first m of these integers are the field
  1616.           numbers (in DB) of the secondary index fields, where m is the
  1617.           number of fields in the secondary index.
  1618.  
  1619.           Immediately after the integers, there is a null-terminated string
  1620.           that contains the name of the sort order, e.g., "ascii".
  1621.  
  1622.           The name of the index follows the sort order string. The index
  1623.           name (a.k.a., label) is a null-terminated string.
  1624.  
  1625.           No secondary index data records are stored in the X** header
  1626.           block.
  1627.  
  1628.  
  1629.           Revision 1.0                   26                    May 11, 1996
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.                               PARADOX 4.x FILE FORMATS
  1636.  
  1637.  
  1638.  
  1639.           X** Data Blocks
  1640.           ---------------
  1641.  
  1642.           The data blocks have the same format as a DB data block.
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.           Revision 1.0                   27                    May 11, 1996
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.                               PARADOX 4.x FILE FORMATS
  1695.  
  1696.  
  1697.           Y** File
  1698.           --------
  1699.  
  1700.           A Y** file is the primary index for an X** file.
  1701.  
  1702.           Its logical format is identical to the format of the PX file.
  1703.  
  1704.  
  1705.           Y** Header
  1706.           ----------
  1707.  
  1708.           The Y** header is the first block in the Y** file. It has the
  1709.           same format as the PX file header. Some of the fields in the
  1710.           header are described below. (The list is far from complete.)
  1711.  
  1712.  
  1713.            Hex   Field
  1714.           Offset Type  Description
  1715.           ------ ----- --------------------------------------------------
  1716.           000000 US    Index record length
  1717.           000002 US    Length of index header size (2k)
  1718.           000004 UB    File type
  1719.                        05 - Y** file
  1720.           000005 UB    Index block size code
  1721.                        01 - Block size is 1k
  1722.                        02 - Block size is 2k
  1723.                        03 - Block size is 3k (not used in 4.5)
  1724.                        04 - Block size is 4k
  1725.           000006 UL    Number of records in Y**
  1726.           00000A US    Number of blocks in use
  1727.           00000C US    Total blocks in file
  1728.           00000E US    First index data block (always 1)
  1729.           000010 US    Last block in use
  1730.           00001E US    Block number of index root
  1731.           000020 UB    Number of index levels
  1732.           000021 UB    Number of fields in index
  1733.  
  1734.           The index record length is six greater than the sum of the
  1735.           lengths of the key fields.
  1736.  
  1737.           No index records are stored in this block.
  1738.  
  1739.  
  1740.           Y** Index Blocks
  1741.           ----------------
  1742.  
  1743.           Same format as the index blocks in the PX file.
  1744.  
  1745.  
  1746.  
  1747.           Revision 1.0                   28                    May 11, 1996