Decoding Varints: A Critical Skill in SQLite Forensics

Introduction

My first encounter with varints was many years ago during a digital forensic examination of an iPhone 3GS in a murder and arson investigation. Until then, my forensic work had primarily involved feature phones, particularly CDMA devices running BREW. Each text message was stored in its own file, with a consistent offset and a straightforward hex value indicating the length of the message. It was a predictable and familiar process.

However, the iPhone introduced me to an entirely new challenge. Messages were no longer stored as individual files but instead as records within a database. Unlike the simplicity of feature phones, the length of a message string wasn’t defined by a clear hex value. Instead, I found myself navigating SQLite databases and learning to parse record payloads. It was here that I encountered varints—variable-length integers—a compact and efficient way to encode data. Understanding varints became crucial as I unraveled the database structure and extracted key evidence. In this case, mastering varints led to incriminating findings that ultimately helped secure the conviction of the defendant for arson and murder.

Varints are a fundamental component of SQLite databases, enabling efficient encoding of integers that define the length of cells, records, and specific fields. For digital forensics practitioners, decoding varints is essential for uncovering critical information within database files. These compact integers play a significant role in mapping table structures, identifying record payloads, and recovering deleted or hidden data.

  • This blog post explores the vital role varints play in SQLite forensics. I’ll introduce VarInt Calculator, a free tool designed to simplify the decoding process, and guide you through a tutorial to help you apply this knowledge in your investigations. From parsing SQLite database entries to reconstructing deleted records, understanding varints is a skill every forensic examiner should have in their toolkit. If you want to apply these techniques in real-world cases, our Certified SQLite Forensics course covers varints, freelists, overflow pages, and more, through hands-on labs and practical challenges.

Understanding Varints: Spotting and Decoding Them

To dive deeper into SQLite and digital forensics, one key skill you’ll need is understanding variable-length integers (varints). These compact numbers are everywhere in SQLite databases, helping the system save space by using as few bytes as possible to store values. But they can also be tricky to spot and decode if you don’t know what to look for. Let’s break it down step by step.

What is a Varint?

A varint is a special way of storing numbers where the number’s size determines how many bytes are used. Unlike fixed-length integers (e.g., 4 bytes or 8 bytes), a varint can be as small as 1 byte or as large as 9 bytes. This flexibility makes varints space-efficient but also more challenging to interpret.

Where Will You Find Varints?

In SQLite databases, varints are used to encode:
  • The total record payload size in a table leaf cell.
  • Row IDs that uniquely identify records.
  • The length of data within a record’s payload.
  • Various other values related to the database structure.

The Key to Reading Varints: The Marker Bit

A SQLite varint can be one to nine bytes long. In the first eight bytes, the marker bit (the most significant bit, or MSB) tells you whether the varint continues:
  • If the marker bit is 1, the next byte is also part of the varint.
  • If the marker bit is 0, you’ve reached the end of the varint.
If the varint reaches a ninth byte, that byte always ends it. All eight bits of the ninth byte contribute to the value.

Two Ways to Decode the Length of Varints

There are two methods for figuring out how long a varint is: the long way (using binary) and the easy way (using the hex nibble). Let’s go over both. The Long Way: Using Binary
    1. Start with the first byte of the varint.
    2. Convert the byte into binary. Look at the most significant bit (the leftmost bit).
      • If the bit is 1, the byte to the right is part of the varint.
      • If the bit is 0, you’ve reached the end.
    3. Repeat for up to eight bytes. Stop at the first byte with an MSB of 0, or after reading the ninth byte if the first eight all have an MSB of 1.
Example:
  • Byte: 0xC2 → Binary: 11000010 → MSB: 1 → Keep going.
  • Byte: 0x7F → Binary: 01111111 → MSB: 0 → Stop here.
The Easy Way: Using the Left Nibble
  1. For each of the first eight bytes, look at the left nibble (the first hex digit of the byte).
  2. If the left nibble is 8 or higher (8, 9, A, B, C, D, E, or F), the next byte is part of the varint.
  3. If the left nibble is 7 or lower (0 to 7), you’ve reached the end of the varint.
  4. If you reach the ninth byte, stop after it regardless of its left nibble.
Why This Works: A left nibble of 8 or more means the MSB is 1, while a left nibble of 7 or less means the MSB is 0. Example:
  • Byte: 0xC2 → Left nibble: C (greater than 8) → Keep going.
  • Byte: 0x7F → Left nibble: 7 (below 8) → Stop here.
SQLite VarInt in hex view 03 — forensic analysis of variable-length integer encoding
The outlined varint gives the total record payload size for this SQLite table leaf cell.

Reading the Record Payload Size from a Varint

In a SQLite table leaf cell, the first varint gives the total record payload size, including any payload stored on overflow pages. The next varint stores the rowid.

Real-World Example: WAL File

In this WAL frame, the table leaf cell holds a record from the messages table. The high bit of 0x81 tells us to read another byte. 0x07 ends this varint.
  • Breakdown:
    • Byte 1: 0x81 → Binary: 10000001 → Marker bit: 1 → Continue.
    • Byte 2: 0x07 → Binary: 00000111 → Marker bit: 0 → Stop.

Decoding the Varint

Now, we need to decode the varint. If we simply convert the value 0x8107 to an integer, we’d mistakenly calculate a massive, 33,031, because that would treat the continuation bit as part of the value. Why? In this two-byte varint, the high bits indicate whether the varint continues. To decode the varint properly, we need to strip the marker bits and interpret only the meaningful portions of the binary representation. Here’s how it’s done:
  • Decode:
    • Remove marker bits: 0000001 and 0000111.
    • Combine the two septets: 00000000 10000111 → Decimal: 135.
  • The total record payload is 135 bytes. The payload-size varint and the rowid varint are stored separately from those 135 bytes.
Practice decoding varints and tracing the boundaries of SQLite records in the guided SQLite Forensics On-Demand course.

Using the Varint Calculator for Efficiency

The Varint Calculator simplifies this process. Enter the hex value (e.g., 8107) to quickly decode the varint. For the above example, the calculator returns 135, confirming the length.

Calculator for SQLite forensics — forensic hex value calculation and conversion tool
Converting the varint 0x8107 to decimal 135

Decoding Varints in Record Headers

In SQLite, record headers use varints to define:

  1. Length: How much data to read.
  2. Type: How to interpret the data (integer, string, BLOB, etc.).
SQLite record header string length VarInt — forensic analysis of text field encoding
Varint 0x25 within this record header indicates the length of the string in the payload

Example: String Length

Let’s decode a varint in a record header:
  • Varint: 0x25
    • Left nibble: 2 (less than 8) → Single-byte varint.
    • Decimal: 37.
  • Payload: String length = 12 bytes (+97444455667).
SQLite VarInt Calculator tool — decode variable-length integers in forensic analysis
Varint calculator converts varints to Strings and BLOB lengths

Closing Thoughts

Understanding and decoding varints is a cornerstone skill for any digital forensic investigator working with SQLite databases. These compact integers are key to interpreting database structures, determining cell sizes, and recovering data efficiently.

With tools like the Varint Calculator, the process becomes more accessible, allowing forensic practitioners to focus on uncovering the truth hidden in digital evidence. Mastering varints is more than a technical skill—it’s a vital capability that can directly impact the outcome of investigations.

You can read more, and download the Varint Calculator here:

You might also be interested in

Facebook
Twitter
Email
Print

Latest Post

Subscribe to stay updated!

Never miss a post – get notified whenever we publish a new blog article, forensic guide, or share important updates.