Debian Codegraph @

hgext/fsmonitor/pywatchman/load.py

raw
1 # Copyright 2016 Facebook, Inc.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 #
10 # * Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
13 #
14 # * Neither the name Facebook nor the names of its contributors may be used to
15 # endorse or promote products derived from this software without specific
16 # prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 from __future__ import annotations
30
31 import ctypes
32
33
34 try:
35 from . import bser
36 except ImportError:
37 from . import pybser as bser
38
39
40 EMPTY_HEADER = b"\x00\x01\x05\x00\x00\x00\x00"
41
42
43 def _read_bytes(fp, buf):
44 """Read bytes from a file-like object
45
46 @param fp: File-like object that implements read(int)
47 @type fp: file
48
49 @param buf: Buffer to read into
50 @type buf: bytes
51
52 @return: buf
53 """
54
55 # Do the first read without resizing the input buffer
56 offset = 0
57 remaining = len(buf)
58 while remaining > 0:
59 l = fp.readinto((ctypes.c_char * remaining).from_buffer(buf, offset))
60 if l is None or l == 0:
61 return offset
62 offset += l
63 remaining -= l
64 return offset
65
66
67 def load(fp, mutable=True, value_encoding=None, value_errors=None):
68 """Deserialize a BSER-encoded blob.
69
70 @param fp: The file-object to deserialize.
71 @type file:
72
73 @param mutable: Whether to return mutable results.
74 @type mutable: bool
75
76 @param value_encoding: Optional codec to use to decode values. If
77 unspecified or None, return values as bytestrings.
78 @type value_encoding: str
79
80 @param value_errors: Optional error handler for codec. 'strict' by default.
81 The other most common argument is 'surrogateescape' on
82 Python 3. If value_encoding is None, this is ignored.
83 @type value_errors: str
84 """
85 buf = ctypes.create_string_buffer(8192)
86 SNIFF_BUFFER_SIZE = len(EMPTY_HEADER)
87 header = (ctypes.c_char * SNIFF_BUFFER_SIZE).from_buffer(buf)
88 read_len = _read_bytes(fp, header)
89 if read_len < len(header):
90 return None
91
92 total_len = bser.pdu_len(buf)
93 if total_len > len(buf):
94 ctypes.resize(buf, total_len)
95
96 body = (ctypes.c_char * (total_len - len(header))).from_buffer(
97 buf, len(header)
98 )
99 read_len = _read_bytes(fp, body)
100 if read_len < len(body):
101 raise RuntimeError("bser data ended early")
102
103 return bser.loads(
104 (ctypes.c_char * total_len).from_buffer(buf, 0),
105 mutable,
106 value_encoding,
107 value_errors,
108 )