code: def _copy_bitpacked_buffer_if_needed(
      buf: "pyarrow.Buffer",
      offset: int,
      length: int,
  ) -> "pyarrow.Buffer":
      """Copy bit-packed binary buffer, if needed."""
      bit_offset = offset % 8
      byte_offset = offset // 8
      byte_length = _bytes_for_bits(bit_offset + length) // 8
      if offset > 0 or byte_length < buf.size:
          buf = buf.slice(byte_offset, byte_length)
          if bit_offset != 0:
              # Need to manually shift the buffer to eliminate the bit offset.
              buf = _align_bit_offset(buf, bit_offset, byte_length)
      return buf
  
imports: from dataclasses import dataclass
  import logging
  import os
  import sys
  from typing import List, Tuple, Optional, TYPE_CHECKING
label_desc: Use image node_id 26 for calling a global function with example usage: _copy_bitpacked_buffer_if_needed(buf, offset, length) and returns: buf