qail-pg 0.27.8

Rust PostgreSQL driver for typed AST queries with direct wire-protocol execution
Documentation
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! BackendMessage decoder — server-to-client wire format.

use super::types::*;

/// Maximum backend frame length accepted by the decoder.
///
/// Mirrors driver-side guards to keep standalone `BackendMessage::decode`
/// usage fail-closed against oversized frames.
pub(crate) const MAX_BACKEND_FRAME_LEN: usize = 64 * 1024 * 1024;

impl BackendMessage {
    /// Decode a message from wire bytes.
    pub fn decode(buf: &[u8]) -> Result<(Self, usize), String> {
        if buf.len() < 5 {
            return Err("Buffer too short".to_string());
        }

        let msg_type = buf[0];
        let len = u32::from_be_bytes([buf[1], buf[2], buf[3], buf[4]]) as usize;

        // PG protocol: length includes itself (4 bytes), so minimum valid length is 4.
        // Anything less is a malformed message.
        if len < 4 {
            return Err(format!("Invalid message length: {} (minimum is 4)", len));
        }
        if len > MAX_BACKEND_FRAME_LEN {
            return Err(format!(
                "Message too large: {} bytes (max {})",
                len, MAX_BACKEND_FRAME_LEN
            ));
        }

        let frame_len = len
            .checked_add(1)
            .ok_or_else(|| "Message length overflow".to_string())?;

        if buf.len() < frame_len {
            return Err("Incomplete message".to_string());
        }

        let payload = &buf[5..frame_len];

        let message = match msg_type {
            b'R' => Self::decode_auth(payload)?,
            b'S' => Self::decode_parameter_status(payload)?,
            b'K' => Self::decode_backend_key(payload)?,
            b'v' => Self::decode_negotiate_protocol_version(payload)?,
            b'Z' => Self::decode_ready_for_query(payload)?,
            b'T' => Self::decode_row_description(payload)?,
            b'D' => Self::decode_data_row(payload)?,
            b'C' => Self::decode_command_complete(payload)?,
            b'E' => Self::decode_error_response(payload)?,
            b'1' => {
                if !payload.is_empty() {
                    return Err("ParseComplete must have empty payload".to_string());
                }
                BackendMessage::ParseComplete
            }
            b'2' => {
                if !payload.is_empty() {
                    return Err("BindComplete must have empty payload".to_string());
                }
                BackendMessage::BindComplete
            }
            b'3' => {
                if !payload.is_empty() {
                    return Err("CloseComplete must have empty payload".to_string());
                }
                BackendMessage::CloseComplete
            }
            b'n' => {
                if !payload.is_empty() {
                    return Err("NoData must have empty payload".to_string());
                }
                BackendMessage::NoData
            }
            b's' => {
                if !payload.is_empty() {
                    return Err("PortalSuspended must have empty payload".to_string());
                }
                BackendMessage::PortalSuspended
            }
            b't' => Self::decode_parameter_description(payload)?,
            b'G' => Self::decode_copy_in_response(payload)?,
            b'H' => Self::decode_copy_out_response(payload)?,
            b'W' => Self::decode_copy_both_response(payload)?,
            b'd' => BackendMessage::CopyData(payload.to_vec()),
            b'c' => {
                if !payload.is_empty() {
                    return Err("CopyDone must have empty payload".to_string());
                }
                BackendMessage::CopyDone
            }
            b'A' => Self::decode_notification_response(payload)?,
            b'I' => {
                if !payload.is_empty() {
                    return Err("EmptyQueryResponse must have empty payload".to_string());
                }
                BackendMessage::EmptyQueryResponse
            }
            b'N' => BackendMessage::NoticeResponse(Self::parse_error_fields(payload)?),
            _ => return Err(format!("Unknown message type: {}", msg_type as char)),
        };

        Ok((message, frame_len))
    }

    fn decode_auth(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 4 {
            return Err("Auth payload too short".to_string());
        }
        let auth_type = i32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]);
        match auth_type {
            0 => {
                if payload.len() != 4 {
                    return Err(format!(
                        "AuthenticationOk invalid payload length: {}",
                        payload.len()
                    ));
                }
                Ok(BackendMessage::AuthenticationOk)
            }
            2 => {
                if payload.len() != 4 {
                    return Err(format!(
                        "AuthenticationKerberosV5 invalid payload length: {}",
                        payload.len()
                    ));
                }
                Ok(BackendMessage::AuthenticationKerberosV5)
            }
            3 => {
                if payload.len() != 4 {
                    return Err(format!(
                        "AuthenticationCleartextPassword invalid payload length: {}",
                        payload.len()
                    ));
                }
                Ok(BackendMessage::AuthenticationCleartextPassword)
            }
            5 => {
                if payload.len() != 8 {
                    return Err("MD5 auth payload too short (need salt)".to_string());
                }
                let mut salt = [0u8; 4];
                salt.copy_from_slice(&payload[4..8]);
                Ok(BackendMessage::AuthenticationMD5Password(salt))
            }
            6 => {
                if payload.len() != 4 {
                    return Err(format!(
                        "AuthenticationSCMCredential invalid payload length: {}",
                        payload.len()
                    ));
                }
                Ok(BackendMessage::AuthenticationSCMCredential)
            }
            7 => {
                if payload.len() != 4 {
                    return Err(format!(
                        "AuthenticationGSS invalid payload length: {}",
                        payload.len()
                    ));
                }
                Ok(BackendMessage::AuthenticationGSS)
            }
            8 => Ok(BackendMessage::AuthenticationGSSContinue(
                payload[4..].to_vec(),
            )),
            9 => {
                if payload.len() != 4 {
                    return Err(format!(
                        "AuthenticationSSPI invalid payload length: {}",
                        payload.len()
                    ));
                }
                Ok(BackendMessage::AuthenticationSSPI)
            }
            10 => {
                // SASL - parse mechanism list
                let mut mechanisms = Vec::new();
                let mut pos = 4;
                while pos < payload.len() {
                    if payload[pos] == 0 {
                        break; // list terminator
                    }
                    let end = payload[pos..]
                        .iter()
                        .position(|&b| b == 0)
                        .map(|p| pos + p)
                        .ok_or("SASL mechanism list missing null terminator")?;
                    mechanisms.push(String::from_utf8_lossy(&payload[pos..end]).to_string());
                    pos = end + 1;
                }
                if pos >= payload.len() {
                    return Err("SASL mechanism list missing final terminator".to_string());
                }
                if pos + 1 != payload.len() {
                    return Err("SASL mechanism list has trailing bytes".to_string());
                }
                if mechanisms.is_empty() {
                    return Err("SASL mechanism list is empty".to_string());
                }
                Ok(BackendMessage::AuthenticationSASL(mechanisms))
            }
            11 => {
                // SASL Continue - server challenge
                Ok(BackendMessage::AuthenticationSASLContinue(
                    payload[4..].to_vec(),
                ))
            }
            12 => {
                // SASL Final - server signature
                Ok(BackendMessage::AuthenticationSASLFinal(
                    payload[4..].to_vec(),
                ))
            }
            _ => Err(format!("Unknown auth type: {}", auth_type)),
        }
    }

    fn decode_parameter_status(payload: &[u8]) -> Result<Self, String> {
        let name_end = payload
            .iter()
            .position(|&b| b == 0)
            .ok_or("ParameterStatus missing name terminator")?;
        let value_start = name_end + 1;
        if value_start > payload.len() {
            return Err("ParameterStatus missing value".to_string());
        }
        let value_end_rel = payload[value_start..]
            .iter()
            .position(|&b| b == 0)
            .ok_or("ParameterStatus missing value terminator")?;
        let value_end = value_start + value_end_rel;
        if value_end + 1 != payload.len() {
            return Err("ParameterStatus has trailing bytes".to_string());
        }
        Ok(BackendMessage::ParameterStatus {
            name: String::from_utf8_lossy(&payload[..name_end]).to_string(),
            value: String::from_utf8_lossy(&payload[value_start..value_end]).to_string(),
        })
    }

    fn decode_backend_key(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 8 {
            return Err("BackendKeyData payload too short".to_string());
        }
        let key_len = payload.len() - 4;
        if !(4..=256).contains(&key_len) {
            return Err(format!(
                "BackendKeyData invalid secret key length: {} (expected 4..=256)",
                key_len
            ));
        }
        Ok(BackendMessage::BackendKeyData {
            process_id: i32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]),
            secret_key: payload[4..].to_vec(),
        })
    }

    fn decode_negotiate_protocol_version(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 8 {
            return Err("NegotiateProtocolVersion payload too short".to_string());
        }

        let newest_minor_supported =
            i32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]);
        if newest_minor_supported < 0 {
            return Err("NegotiateProtocolVersion newest_minor_supported is negative".to_string());
        }

        let unrecognized_count =
            i32::from_be_bytes([payload[4], payload[5], payload[6], payload[7]]);
        if unrecognized_count < 0 {
            return Err(
                "NegotiateProtocolVersion unrecognized option count is negative".to_string(),
            );
        }
        let unrecognized_count = unrecognized_count as usize;
        let remaining = payload.len() - 8;
        // Each option is NUL-terminated, so even an empty option consumes one byte.
        // This bound keeps malformed counts from forcing huge pre-allocations.
        if unrecognized_count > remaining {
            return Err(format!(
                "NegotiateProtocolVersion unrecognized option count {} exceeds payload capacity {}",
                unrecognized_count, remaining
            ));
        }

        let mut options = Vec::with_capacity(unrecognized_count);
        let mut pos = 8usize;
        for _ in 0..unrecognized_count {
            if pos >= payload.len() {
                return Err("NegotiateProtocolVersion missing option string terminator".to_string());
            }
            let rel_end = payload[pos..]
                .iter()
                .position(|&b| b == 0)
                .ok_or("NegotiateProtocolVersion option missing null terminator")?;
            let end = pos + rel_end;
            options.push(String::from_utf8_lossy(&payload[pos..end]).to_string());
            pos = end + 1;
        }

        if pos != payload.len() {
            return Err("NegotiateProtocolVersion has trailing bytes".to_string());
        }

        Ok(BackendMessage::NegotiateProtocolVersion {
            newest_minor_supported,
            unrecognized_protocol_options: options,
        })
    }

    fn decode_ready_for_query(payload: &[u8]) -> Result<Self, String> {
        if payload.len() != 1 {
            return Err("ReadyForQuery payload empty".to_string());
        }
        let status = match payload[0] {
            b'I' => TransactionStatus::Idle,
            b'T' => TransactionStatus::InBlock,
            b'E' => TransactionStatus::Failed,
            _ => return Err("Unknown transaction status".to_string()),
        };
        Ok(BackendMessage::ReadyForQuery(status))
    }

    fn decode_row_description(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 2 {
            return Err("RowDescription payload too short".to_string());
        }

        let raw_count = i16::from_be_bytes([payload[0], payload[1]]);
        if raw_count < 0 {
            return Err(format!("RowDescription invalid field count: {}", raw_count));
        }
        let field_count = raw_count as usize;
        let mut fields = Vec::with_capacity(field_count);
        let mut pos = 2;

        for _ in 0..field_count {
            // Field name (null-terminated string)
            let name_end = payload[pos..]
                .iter()
                .position(|&b| b == 0)
                .ok_or("Missing null terminator in field name")?;
            let name = String::from_utf8_lossy(&payload[pos..pos + name_end]).to_string();
            pos += name_end + 1; // Skip null terminator

            // Ensure we have enough bytes for the fixed fields
            if pos + 18 > payload.len() {
                return Err("RowDescription field truncated".to_string());
            }

            let table_oid = u32::from_be_bytes([
                payload[pos],
                payload[pos + 1],
                payload[pos + 2],
                payload[pos + 3],
            ]);
            pos += 4;

            let column_attr = i16::from_be_bytes([payload[pos], payload[pos + 1]]);
            pos += 2;

            let type_oid = u32::from_be_bytes([
                payload[pos],
                payload[pos + 1],
                payload[pos + 2],
                payload[pos + 3],
            ]);
            pos += 4;

            let type_size = i16::from_be_bytes([payload[pos], payload[pos + 1]]);
            pos += 2;

            let type_modifier = i32::from_be_bytes([
                payload[pos],
                payload[pos + 1],
                payload[pos + 2],
                payload[pos + 3],
            ]);
            pos += 4;

            let format = i16::from_be_bytes([payload[pos], payload[pos + 1]]);
            if !(0..=1).contains(&format) {
                return Err(format!("RowDescription invalid format code: {}", format));
            }
            pos += 2;

            fields.push(FieldDescription {
                name,
                table_oid,
                column_attr,
                type_oid,
                type_size,
                type_modifier,
                format,
            });
        }

        if pos != payload.len() {
            return Err("RowDescription has trailing bytes".to_string());
        }

        Ok(BackendMessage::RowDescription(fields))
    }

    fn decode_data_row(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 2 {
            return Err("DataRow payload too short".to_string());
        }

        let raw_count = i16::from_be_bytes([payload[0], payload[1]]);
        if raw_count < 0 {
            return Err(format!("DataRow invalid column count: {}", raw_count));
        }
        let column_count = raw_count as usize;
        // Sanity check: each column needs at least 4 bytes (length field)
        if column_count > (payload.len() - 2) / 4 + 1 {
            return Err(format!(
                "DataRow claims {} columns but payload is only {} bytes",
                column_count,
                payload.len()
            ));
        }
        let mut columns = Vec::with_capacity(column_count);
        let mut pos = 2;

        for _ in 0..column_count {
            if pos + 4 > payload.len() {
                return Err("DataRow truncated".to_string());
            }

            let len = i32::from_be_bytes([
                payload[pos],
                payload[pos + 1],
                payload[pos + 2],
                payload[pos + 3],
            ]);
            pos += 4;

            if len == -1 {
                // NULL value
                columns.push(None);
            } else {
                if len < -1 {
                    return Err(format!("DataRow invalid column length: {}", len));
                }
                let len = len as usize;
                if len > payload.len().saturating_sub(pos) {
                    return Err("DataRow column data truncated".to_string());
                }
                let data = payload[pos..pos + len].to_vec();
                pos += len;
                columns.push(Some(data));
            }
        }

        if pos != payload.len() {
            return Err("DataRow has trailing bytes".to_string());
        }

        Ok(BackendMessage::DataRow(columns))
    }

    fn decode_command_complete(payload: &[u8]) -> Result<Self, String> {
        if payload.last().copied() != Some(0) {
            return Err("CommandComplete missing null terminator".to_string());
        }
        let tag_bytes = &payload[..payload.len() - 1];
        if tag_bytes.contains(&0) {
            return Err("CommandComplete contains interior null byte".to_string());
        }
        let tag = String::from_utf8_lossy(tag_bytes).to_string();
        Ok(BackendMessage::CommandComplete(tag))
    }

    fn decode_error_response(payload: &[u8]) -> Result<Self, String> {
        Ok(BackendMessage::ErrorResponse(Self::parse_error_fields(
            payload,
        )?))
    }

    fn parse_error_fields(payload: &[u8]) -> Result<ErrorFields, String> {
        if payload.last().copied() != Some(0) {
            return Err("ErrorResponse missing final terminator".to_string());
        }
        let mut fields = ErrorFields::default();
        let mut i = 0;
        while i < payload.len() && payload[i] != 0 {
            let field_type = payload[i];
            i += 1;
            let end = payload[i..]
                .iter()
                .position(|&b| b == 0)
                .map(|p| p + i)
                .ok_or("ErrorResponse field missing null terminator")?;
            let value = String::from_utf8_lossy(&payload[i..end]).to_string();
            i = end + 1;

            match field_type {
                b'S' => fields.severity = value,
                b'C' => fields.code = value,
                b'M' => fields.message = value,
                b'D' => fields.detail = Some(value),
                b'H' => fields.hint = Some(value),
                _ => {}
            }
        }
        if i + 1 != payload.len() {
            return Err("ErrorResponse has trailing bytes after terminator".to_string());
        }
        Ok(fields)
    }

    fn decode_parameter_description(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 2 {
            return Err("ParameterDescription payload too short".to_string());
        }
        let raw_count = i16::from_be_bytes([payload[0], payload[1]]);
        if raw_count < 0 {
            return Err(format!("ParameterDescription invalid count: {}", raw_count));
        }
        let count = raw_count as usize;
        let expected_len = 2 + count * 4;
        if payload.len() < expected_len {
            return Err(format!(
                "ParameterDescription truncated: expected {} bytes, got {}",
                expected_len,
                payload.len()
            ));
        }
        let mut oids = Vec::with_capacity(count);
        let mut pos = 2;
        for _ in 0..count {
            oids.push(u32::from_be_bytes([
                payload[pos],
                payload[pos + 1],
                payload[pos + 2],
                payload[pos + 3],
            ]));
            pos += 4;
        }
        if pos != payload.len() {
            return Err("ParameterDescription has trailing bytes".to_string());
        }
        Ok(BackendMessage::ParameterDescription(oids))
    }

    fn decode_copy_in_response(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 3 {
            return Err("CopyInResponse payload too short".to_string());
        }
        let format = payload[0];
        if format > 1 {
            return Err(format!(
                "CopyInResponse invalid overall format code: {}",
                format
            ));
        }
        let num_columns = if payload.len() >= 3 {
            let raw = i16::from_be_bytes([payload[1], payload[2]]);
            if raw < 0 {
                return Err(format!(
                    "CopyInResponse invalid negative column count: {}",
                    raw
                ));
            }
            raw as usize
        } else {
            0
        };
        let mut column_formats = Vec::with_capacity(num_columns);
        let mut pos = 3usize;
        for _ in 0..num_columns {
            if pos + 2 > payload.len() {
                return Err("CopyInResponse truncated column format list".to_string());
            }
            let raw = i16::from_be_bytes([payload[pos], payload[pos + 1]]);
            if !(0..=1).contains(&raw) {
                return Err(format!("CopyInResponse invalid format code: {}", raw));
            }
            column_formats.push(raw as u8);
            pos += 2;
        }
        if pos != payload.len() {
            return Err("CopyInResponse has trailing bytes".to_string());
        }
        Ok(BackendMessage::CopyInResponse {
            format,
            column_formats,
        })
    }

    fn decode_copy_out_response(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 3 {
            return Err("CopyOutResponse payload too short".to_string());
        }
        let format = payload[0];
        if format > 1 {
            return Err(format!(
                "CopyOutResponse invalid overall format code: {}",
                format
            ));
        }
        let num_columns = if payload.len() >= 3 {
            let raw = i16::from_be_bytes([payload[1], payload[2]]);
            if raw < 0 {
                return Err(format!(
                    "CopyOutResponse invalid negative column count: {}",
                    raw
                ));
            }
            raw as usize
        } else {
            0
        };
        let mut column_formats = Vec::with_capacity(num_columns);
        let mut pos = 3usize;
        for _ in 0..num_columns {
            if pos + 2 > payload.len() {
                return Err("CopyOutResponse truncated column format list".to_string());
            }
            let raw = i16::from_be_bytes([payload[pos], payload[pos + 1]]);
            if !(0..=1).contains(&raw) {
                return Err(format!("CopyOutResponse invalid format code: {}", raw));
            }
            column_formats.push(raw as u8);
            pos += 2;
        }
        if pos != payload.len() {
            return Err("CopyOutResponse has trailing bytes".to_string());
        }
        Ok(BackendMessage::CopyOutResponse {
            format,
            column_formats,
        })
    }

    fn decode_copy_both_response(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 3 {
            return Err("CopyBothResponse payload too short".to_string());
        }
        let format = payload[0];
        if format > 1 {
            return Err(format!(
                "CopyBothResponse invalid overall format code: {}",
                format
            ));
        }
        let num_columns = if payload.len() >= 3 {
            let raw = i16::from_be_bytes([payload[1], payload[2]]);
            if raw < 0 {
                return Err(format!(
                    "CopyBothResponse invalid negative column count: {}",
                    raw
                ));
            }
            raw as usize
        } else {
            0
        };
        let mut column_formats = Vec::with_capacity(num_columns);
        let mut pos = 3usize;
        for _ in 0..num_columns {
            if pos + 2 > payload.len() {
                return Err("CopyBothResponse truncated column format list".to_string());
            }
            let raw = i16::from_be_bytes([payload[pos], payload[pos + 1]]);
            if !(0..=1).contains(&raw) {
                return Err(format!("CopyBothResponse invalid format code: {}", raw));
            }
            column_formats.push(raw as u8);
            pos += 2;
        }
        if pos != payload.len() {
            return Err("CopyBothResponse has trailing bytes".to_string());
        }
        Ok(BackendMessage::CopyBothResponse {
            format,
            column_formats,
        })
    }

    fn decode_notification_response(payload: &[u8]) -> Result<Self, String> {
        if payload.len() < 6 {
            // Minimum: 4 (process_id) + 1 (channel NUL) + 1 (payload NUL)
            return Err("NotificationResponse too short".to_string());
        }
        let process_id = i32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]);

        // Channel name (null-terminated)
        let mut i = 4;
        let remaining = payload.get(i..).unwrap_or(&[]);
        let channel_end = remaining
            .iter()
            .position(|&b| b == 0)
            .ok_or("NotificationResponse: missing channel null terminator")?;
        let channel = String::from_utf8_lossy(&remaining[..channel_end]).to_string();
        i += channel_end + 1;

        // Payload (null-terminated)
        let remaining = payload.get(i..).unwrap_or(&[]);
        let payload_end = remaining
            .iter()
            .position(|&b| b == 0)
            .ok_or("NotificationResponse: missing payload null terminator")?;
        let notification_payload = String::from_utf8_lossy(&remaining[..payload_end]).to_string();
        if i + payload_end + 1 != payload.len() {
            return Err("NotificationResponse has trailing bytes".to_string());
        }

        Ok(BackendMessage::NotificationResponse {
            process_id,
            channel,
            payload: notification_payload,
        })
    }
}