MCPcopy Index your code
hub / github.com/subtrace/subtrace / decodeRawProto

Function decodeRawProto

tracer/parser.go:100–152  ·  view source on GitHub ↗
(depth int, enc map[string]any, buf []byte)

Source from the content-addressed store, hash-verified

98}
99
100func decodeRawProto(depth int, enc map[string]any, buf []byte) error {
101 if depth > 100 {
102 return fmt.Errorf("recursion depth limit exceeded")
103 }
104 for len(buf) > 0 {
105 num, typ, tlen := protowire.ConsumeTag(buf)
106 if tlen < 0 {
107 return fmt.Errorf("consume tag: %v", protowire.ParseError(tlen))
108 }
109 if tlen > len(buf) {
110 return fmt.Errorf("consume num=%v, typ=%v: short buffer: %d < %d", num, typ, tlen, len(buf))
111 }
112 buf = buf[tlen:]
113
114 key := fmt.Sprintf("%d", num)
115 var vlen int
116 switch typ {
117 case protowire.VarintType:
118 enc[key], vlen = protowire.ConsumeVarint(buf)
119 case protowire.Fixed32Type:
120 enc[key], vlen = protowire.ConsumeFixed32(buf)
121 case protowire.Fixed64Type:
122 enc[key], vlen = protowire.ConsumeFixed64(buf)
123 case protowire.BytesType:
124 var tmp []byte
125 tmp, vlen = protowire.ConsumeBytes(buf)
126 if vlen >= 0 {
127 if _, _, size := protowire.ConsumeTag(tmp); size >= 0 {
128 m := make(map[string]any)
129 if err := decodeRawProto(depth+1, m, tmp); err == nil {
130 enc[key] = m
131 break
132 }
133 }
134 if utf8.Valid(tmp) {
135 enc[key] = string(tmp)
136 } else {
137 enc[key] = base64.RawStdEncoding.EncodeToString(tmp)
138 }
139 }
140 case protowire.StartGroupType:
141 enc[key], vlen = protowire.ConsumeGroup(num, buf)
142 case protowire.EndGroupType:
143 default:
144 return fmt.Errorf("consume num=%v, typ=%v: unknown type", num, typ)
145 }
146 if vlen < 0 {
147 return fmt.Errorf("consume num=%v, typ=%v: parse error: %w", num, typ, protowire.ParseError(vlen))
148 }
149 buf = buf[vlen:]
150 }
151 return nil
152}
153
154func jsonify(mime string, buf []byte) ([]byte, bool) {
155 switch mime {

Callers 1

jsonifyFunction · 0.85

Calls 1

ValidMethod · 0.80

Tested by

no test coverage detected