|
67 | 67 | </li> |
68 | 68 | <li class="toctree-l3"><a class="reference internal" href="#define-sizeof-and-eval">Define, Sizeof, and Eval</a> |
69 | 69 | </li> |
| 70 | + <li class="toctree-l3"><a class="reference internal" href="#accessing-field-definitions">Accessing Field Definitions</a> |
| 71 | + </li> |
70 | 72 | <li class="toctree-l3"><a class="reference internal" href="#ispect-memory">Ispect memory</a> |
71 | 73 | </li> |
72 | 74 | </ul> |
@@ -272,12 +274,15 @@ <h3 id="nested-structsunions">Nested structs/unions</h3> |
272 | 274 | """ |
273 | 275 | </code></pre> |
274 | 276 | <h3 id="byte-order-size-and-padding">Byte Order, Size, and Padding</h3> |
275 | | -<p>Suported byte orders:</p> |
| 277 | +<p>Python-CStruct supports big-endian, little-endian, and native byte orders, which you can specify using:</p> |
276 | 278 | <ul> |
277 | | -<li><code>cstruct.LITTLE_ENDIAN</code> - Little endian byte order, standard size, no padding</li> |
278 | | -<li><code>cstruct.BIG_ENDIAN</code> - Big endian byte order, standard size, no padding</li> |
279 | | -<li><code>cstruct.NATIVE_ORDER</code> - Native byte order, native size, padding</li> |
| 279 | +<li><code>cstruct.LITTLE_ENDIAN</code> - Little endian byte order, standard size, no padding.</li> |
| 280 | +<li><code>cstruct.BIG_ENDIAN</code> - Big endian byte order, standard size, no padding.</li> |
| 281 | +<li><code>cstruct.NATIVE_ORDER</code> - Native byte order, native size, padding. Native byte order is big-endian or little-endian, depending on the host system.</li> |
280 | 282 | </ul> |
| 283 | +<p>Standard size depends only on the format character while native size depends on the host system. |
| 284 | +For native order, padding is automatically added to align the structure members.</p> |
| 285 | +<p>For more information, see the <a href="https://docs.python.org/3/library/struct.html#byte-order-size-and-alignment">struct - Byte Order, Size, and Padding</a> section.</p> |
281 | 286 | <pre><code class="language-python">class Native(cstruct.MemCStruct): |
282 | 287 | __byte_order__ = cstruct.NATIVE_ORDER |
283 | 288 | __def__ = """ |
@@ -400,6 +405,52 @@ <h3 id="define-sizeof-and-eval">Define, Sizeof, and Eval</h3> |
400 | 405 | } |
401 | 406 | """ |
402 | 407 | </code></pre> |
| 408 | +<h3 id="accessing-field-definitions">Accessing Field Definitions</h3> |
| 409 | +<p>Python-CStruct provides the <code>__fields_types__</code> attribute at the class level, which allows you to inspect the metadata of each field. |
| 410 | +The dictionary contains each field's name as a key and its metadata as a value. |
| 411 | +Each field has the following attributes:</p> |
| 412 | +<table> |
| 413 | +<thead> |
| 414 | +<tr> |
| 415 | +<th>Attribute</th> |
| 416 | +<th>Description</th> |
| 417 | +</tr> |
| 418 | +</thead> |
| 419 | +<tbody> |
| 420 | +<tr> |
| 421 | +<td><code>kind</code></td> |
| 422 | +<td>Indicates whether the field is a primitive type or a nested struct.</td> |
| 423 | +</tr> |
| 424 | +<tr> |
| 425 | +<td><code>c_type</code></td> |
| 426 | +<td>The corresponding C type.</td> |
| 427 | +</tr> |
| 428 | +<tr> |
| 429 | +<td><code>ref</code></td> |
| 430 | +<td>If the field is a nested struct, this contains a reference to the class representing that struct.</td> |
| 431 | +</tr> |
| 432 | +<tr> |
| 433 | +<td><code>vlen_ex</code></td> |
| 434 | +<td>The number of elements in the field.</td> |
| 435 | +</tr> |
| 436 | +<tr> |
| 437 | +<td><code>flexible_array</code></td> |
| 438 | +<td>Indicates whether the field is a flexible array.</td> |
| 439 | +</tr> |
| 440 | +<tr> |
| 441 | +<td><code>byte_order</code></td> |
| 442 | +<td>The byte order of the field.</td> |
| 443 | +</tr> |
| 444 | +<tr> |
| 445 | +<td><code>offset</code></td> |
| 446 | +<td>The relative position of the field within the struct.</td> |
| 447 | +</tr> |
| 448 | +<tr> |
| 449 | +<td><code>padding</code></td> |
| 450 | +<td>The number of bytes added before this field for alignment.</td> |
| 451 | +</tr> |
| 452 | +</tbody> |
| 453 | +</table> |
403 | 454 | <h3 id="ispect-memory">Ispect memory</h3> |
404 | 455 | <p>The <a href="https://python-cstruct.readthedocs.io/en/latest/api/abstract/#cstruct.abstract.AbstractCStruct.inspect"><code>inspect</code></a> methods displays memory contents in hexadecimal.</p> |
405 | 456 | <pre><code class="language-python">print(mbr.inspect()) |
@@ -490,5 +541,5 @@ <h2 id="links">Links</h2> |
490 | 541 |
|
491 | 542 | <!-- |
492 | 543 | MkDocs version : 1.6.1 |
493 | | -Build Date UTC : 2025-01-16 19:35:50.915244+00:00 |
| 544 | +Build Date UTC : 2025-02-18 05:53:39.039107+00:00 |
494 | 545 | --> |
0 commit comments