package xturbo // ExtInfo is a negotiated extension's wire opcode and event/error bases. type ExtInfo struct { Present bool Major, FirstEvent, FirstError uint8 } // ExtTable is populated during setup. Fixed arrays avoid lookup allocations. // Reverse maps store ExtID+1 so the zero value means "none". type ExtTable struct { info [extCount]ExtInfo byEvent [128]uint8 byError [256]uint8 byMajor [256]uint8 } // Set records an extension's negotiated opcodes and fills the reverse maps // for the event and error codes the extension declares in the pinned XML. func (t *ExtTable) Set(id ExtID, major, firstEvent, firstError uint8) { if id == ExtCore || id >= extCount { return } tag := uint8(id) + 1 for i := range t.byEvent { if t.byEvent[i] == tag { t.byEvent[i] = 0 } } for i := range t.byError { if t.byError[i] == tag { t.byError[i] = 0 } if t.byMajor[i] == tag { t.byMajor[i] = 0 } } t.info[id] = ExtInfo{true, major, firstEvent, firstError} if major != 0 { t.byMajor[major] = tag } if firstEvent != 0 { for i := 0; i < int(extEvents[id]) && int(firstEvent)+i < len(t.byEvent); i++ { t.byEvent[int(firstEvent)+i] = tag } } if firstError != 0 { for i := 0; i < int(extErrors[id]) && int(firstError)+i < len(t.byError); i++ { t.byError[int(firstError)+i] = tag } } } func (t *ExtTable) Info(id ExtID) ExtInfo { if id >= extCount { return ExtInfo{} } if id == ExtCore { return ExtInfo{Present: true} } return t.info[id] } func (t *ExtTable) Major(id ExtID) uint8 { return t.Info(id).Major } // ByEvent maps an event code (raw[0]&0x7f) to its extension and the code // relative to the extension's first event. func (t *ExtTable) ByEvent(code uint8) (id ExtID, rel uint8, ok bool) { if code >= 128 || t.byEvent[code] == 0 { return ExtCore, 0, false } id = ExtID(t.byEvent[code] - 1) return id, code - t.info[id].FirstEvent, true } // ByError maps a wire error code to its extension and relative error code. func (t *ExtTable) ByError(code uint8) (id ExtID, rel uint8, ok bool) { if t.byError[code] == 0 { return ExtCore, 0, false } id = ExtID(t.byError[code] - 1) return id, code - t.info[id].FirstError, true } // ByMajor maps a major opcode to its extension. func (t *ExtTable) ByMajor(major uint8) (id ExtID, ok bool) { if t.byMajor[major] == 0 { return ExtCore, false } return ExtID(t.byMajor[major] - 1), true }