> ## Documentation Index
> Fetch the complete documentation index at: https://docs.directgvr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Data store

> Store data in a signed client executable with the data store.

## Overview

The data store allows for the delivery of custom per-user data or workflows that are delivered with the DLM. It is particularly useful for enterprise customers who don't want to transfer information from their webpage to the DLM through the use of filename parameters.

### Data format

The data store is an uninitialized buffer stored in an unsigned attribute when the executable or disk image is signed.

#### Version 1.0

| Field | Type | Description | Length (bytes) |
| - | - | - | - |
| Magic | char\[12] | SSNDATASTORE | 12 |
| Payload | string | Not to exceed 4096 bytes | Variable length padded to 4096 |

**Example:**

#### Version 2.0

| Field | Type | Description | Length (bytes) |
| - | - | - | - |
| Magic | char\[5] | SSNDS | 5 |
| Version | char | Version number of format (for example 2) | 1 |
| PaddedLength | uint16 | Multiple of 1024 | 2 |
| Length | uint32 | Length of payload | 4 |
| CRC32 | uint32 | Hash of datastore header and payload with padding, CRC32 value is 0 when calculating hash | 4 |
| Payload | byte\[] | Binary data | Padded to PaddedLength \* 1024 |

**Additional Notes:**

* All integer types are stored in big-endian

**Example:**

#### Version 3.0

| Field | Type | Description | Length (bytes) |
| - | - | - | - |
| Magic | char\[5] | SSNDS signature. | 5 |
| Version | char | Version number of format (for example 2). | 1 |
| PaddedLength | uint16 | Multiple of 1024. Written during signing and used by stamping service to determine maximum length of data it can write. | 2 |
| CompressedLength | uint32 | Compressed length of payload. | 4 |
| UncompressedLength | uint32 | Uncompressed length of payload. | 4 |
| CRC32 | uint32 | Hash of datastore header and payload with padding, CRC32 value is 0 when calculating hash. | 4 |
| Payload | byte\[] | Gzip compressed JSON or JWT token. | Padded to PaddedLength \* 1024 |

**Additional Notes:**

* All integer types are stored in big-endian
* Payloads must all be compressed using GZIP
* GZIP header begins with 0x1f 0x8b
* GZIP data contains its own CRC

### Building with a data store

To add a data store to your DLM, you have to use the DirectGVR signer console tool with the datastore argument.

The data store is supported on the following file types per platform:

* **Windows** : .exe, .dll
* **Macintosh** : .dmg
* **Linux** : elf binary

The version of the datastore used by the signer will depend on the version of the console tools being used and the arguments supplied to it:

| Signer Version | Argument(s) | DataStore Version |
| - | - | - |
| 5.1 | --datastore | V1 |
| 5.1 | --datastore --datastoresize=8kb | V2 |
| 5.2 | --datastore | V3 |
| 5.2 | --datastore --datastoresize=8kb | V3 |

### Delivering the binary

Normally when a web server responds to a request for a file, it will send the entire contents of the file as on disk. However, if we want to develop a service whereby the data store is changed per request with our own custom data, we have to respond to the request for the file using a server side script. The purpose of the script would be to intercept requests for the file and respond to the requests with the contents of the file as on disk that is also embedded with our own custom data. The custom data would be inserted into the data store section of the file and this amalgamation would then be sent as the response for the original request to our file.

From the perspective of a web server, this is what happens to a normal request for a file:

Request for file -> Read binary data from disk -> Respond with binary data

In our scenario, this is what we would have the web server do with our script:

Request for file -> Read binary data from disk -> Find and modify data store in binary data -> Respond with amalgamated binary data

Our single response would then consist of three parts:

The binary data from start of file to start of data store

The data store (padded)

The binary data from the end of the data store to the end of the file

One way to speed up the response times, is to cache the first and last parts of the response so that it isn't necessary to search for the data store each time.

### Access the data store in host JavaScript

#### DIRECT 3.6

To get the value of the data store in JavaScript, use the DataStore macro. Additionally, it supports specifying the file path we want to retrieve the data store from.

```javascript theme={null}
// Windows & Linux
app.expandString("{fileDataStore:{modulePath}}");
// Macintosh
app.expandString("{fileDataStore:{diskImage:{moduleAppPath}}}");
```

##### DIRECT 5.2

To get the value of the datastore, make an HTTP request to the internal web server datastore path. In the example below, the datastore would contain JSON.

```json theme={null}
$.getJSON(app.expandString("{contentUrl}datastore"), function(data) {
     console.log(data);
});
```

### View compressed data store

When the data store is compressed, it is possible to view the compressed data using a binary hex editor. In the example below the [010 Editor](https://www.sweetscape.com/010editor/) (available for Windows, macOS, and Linux) is used, however an alternative binary hex editor can be found [here](https://en.wikipedia.org/wiki/Comparison_of_hex_editors).

1. Search for SSNDS3
2. Search for GZIP header magic of 1F 8B
3. Delete all the bytes before the GZIP magic
4. Save the file with a .gz extension
5. View the GZIP archive in a viewer such as WinRAR
6. Right-click on the file and select View File

**Bash Script**

Alternatively, these commands can accomplish the same task on Linux and macOS.

```bash theme={null}
## Get the offset of "SSNDS3"
grep -a -b --only-matching SSNDS3 host
## Get the offset of the GZIP header magic after "SSNDS3"
dd if=host bs=1 skip=<SSNDS3 offset> | grep -a -b --only-matching $'\x1f\x8b'
## Save GZIP
dd if=host of=ds.gz bs=1 skip=$((<SSNDS3 offset>+<GZIP magic offset>))
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.