package git

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Pkt_line.DecoderSource

Module for decoding Git pkt lines, as specified at https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt

We define a "packet line" (aka a "packet") as

| 4 bytes || (enc-pkt-len)-4 | enc-pkt-len pkt-content |------- pkt-len ------|

Example: "0009done\n" where enc-pkt-len = 4 and pkt-content = "done" given we usually trim LF ("\n").

"Encoded" packet length, enc-pkt-len, is the first 4 bytes in the packet that encode the length of the packet in hex. It can have specific values of 0, 1, 2 to encode flush, delimiter, and message (response end) packets respectively. Otherwise, it should be >= 4, i.e., 4 length bytes + the length of the packet content.

In the docs, we define min_pkt_len = 4 as in specs.

Sourcetype decoder = {
  1. buffer : bytes;
  2. mutable pos : int;
  3. mutable max : int;
}
Sourceval io_buffer_size : int
Sourceval create : unit -> decoder
Sourceval of_string : string -> decoder
Sourceval end_of_input : decoder -> int
Sourcetype error = [
  1. | `End_of_input
  2. | `Expected_char of char
  3. | `Unexpected_char of char
  4. | `Expected_string of string
  5. | `Expected_eol
  6. | `Expected_eol_or_space
  7. | `Unexpected_end_of_input
  8. | `No_enough_space
  9. | `Assert_predicate of char -> bool
  10. | `Invalid_pkt_line of string
]
Sourceval pp_error : error Fmt.t
Sourcetype 'err info = {
  1. error : 'err;
  2. buffer : bytes;
  3. committed : int;
}
Sourceexception Leave of error info
Sourcetype ('v, 'err) state =
  1. | Done of 'v
  2. | Read of {
    1. buffer : bytes;
    2. off : int;
    3. len : int;
    4. continue : int -> ('v, 'err) state;
    5. eof : unit -> ('v, 'err) state;
    }
  3. | Error of 'err info
Sourceval return : 'v -> decoder -> ('v, 'err) state
Sourceval bind : ('a, 'b) state -> f:('a -> ('c, 'b) state) -> ('c, 'b) state
Sourceval (>>=) : ('a, 'b) state -> ('a -> ('c, 'b) state) -> ('c, 'b) state
Sourceval leave_with : decoder -> error -> 'never

leave_with d error raises Leave { error; buffer = d.buffer; committed = d.pos }

Sourceval safe : (decoder -> ('v, [> error ] as 'err) state) -> decoder -> ('v, 'err) state

safe k decoder wraps a call k decoder in a try-with block; if exception Leave err is raised, the function returns Error of err

Sourceval fail : decoder -> [> error ] as 'err -> ('v, 'err) state
Sourceval peek_char : decoder -> char option
Sourceval string : string -> decoder -> unit
Sourceval junk_char : decoder -> unit
Sourceval while1 : (char -> bool) -> decoder -> bytes * int * int
  • returns

    decoder.buffer, updated decoder.pos, # of bytes read

Sourceval at_least_one_pkt : decoder -> bool

returns true if decoder.max - decoder.pos is >= min_pkt_len and >= pkt_len, where pkt_len is the length of a pkt line starting at decoder.pos.

Sourceval at_least_one_line : decoder -> bool
Sourceval prompt : ?strict:bool -> (decoder -> ('v, [> error ] as 'err) state) -> decoder -> ('v, 'err) state
Sourceval peek_while_eol : decoder -> bytes * int * int
Sourceval peek_while_eol_or_space : decoder -> bytes * int * int
Sourceval peek_pkt : decoder -> bytes * int * int
Sourceval junk_pkt : decoder -> unit

increase decoder.pos by max min_pkt_len pkt_len, where pkt_len is the length of the pkt line starting at the current value of decoder.pos (before increasing) and min_pkt_len = 4.

  • raises Invalid_argument

    if there aren't 4 bytes representing the length

Sourceval prompt_pkt : ?strict:bool -> (decoder -> ('a, [> error ] as 'b) state) -> decoder -> ('a, 'b) state

/

Sourceval pkt_len_unsafe : decoder -> int
OCaml

Innovation. Community. Security.