blob: 078b6a9c4046f3edc8bb44ec3d02f7d3c35c0651 [file] [log] [blame]
Nigel Taofcb64162020-07-10 22:49:41 +10001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build ignore
16
17package main
18
19// extract-cbor-rfc-7049-examples.go extracts the examples (Appendix A) from
20// RFC 7049 "Concise Binary Object Representation (CBOR)". Each example is
21// written to a separate file under a "cbor-rfc-7049-examples" directory.
22//
23// Usage:
24// go run extract-cbor-rfc-7049-examples.go < rfc7049-errata-corrected.txt
25//
26// The rfc7049-errata-corrected.txt file comes from
27// https://github.com/cbor/spec-with-errata-fixed
28
29import (
30 "bytes"
31 "fmt"
32 "io/ioutil"
33 "os"
34)
35
36func main() {
37 if err := main1(); err != nil {
38 os.Stderr.WriteString(err.Error() + "\n")
39 os.Exit(1)
40 }
41}
42
43func main1() error {
44 src, err := readExampleTable()
45 if err != nil {
46 return err
47 }
48 if err := os.Mkdir("cbor-rfc-7049-examples", 0755); err != nil {
49 return err
50 }
51 for remaining := []byte(nil); len(src) > 0; src = remaining {
52 if i := bytes.IndexByte(src, '\n'); i >= 0 {
53 src, remaining = src[:i], src[i+1:]
54 } else {
55 remaining = nil
56 }
57 src = bytes.TrimSpace(src)
58 if (len(src) == 0) || (src[0] != '|') {
59 continue
60 }
61 if (len(src) != 69) || (src[31] != '|') || (src[68] != '|') {
62 return fmt.Errorf("main: unexpected table row: %q", string(src))
63 }
64 src = bytes.TrimSpace(src[33:68])
65 if len(src) == 0 {
66 if err := emit(); err != nil {
67 return err
68 }
69 continue
70 }
71 if (len(src) > 1) && (src[0] == '0') && (src[1] == 'x') {
72 src = src[2:]
73 }
74 for ; len(src) >= 2; src = src[2:] {
75 contents = append(contents, (unhex(src[0])<<4)|unhex(src[1]))
76 }
77 }
78 return emit()
79}
80
81var (
82 contents = []byte(nil)
83 fileNumber = 0
84)
85
86func emit() error {
87 if len(contents) == 0 {
88 return nil
89 }
90 filename := fmt.Sprintf("cbor-rfc-7049-examples/%02d.cbor", fileNumber)
91 fileNumber++
92 err := ioutil.WriteFile(filename, contents, 0644)
93 contents = nil
94 return err
95}
96
97func unhex(x uint8) uint8 {
98 if x >= 'a' {
99 return x + 10 - 'a'
100 }
101 return x - '0'
102}
103
104func readExampleTable() ([]byte, error) {
105 appendixA := []byte("Appendix A. Examples\n\n")
106 ruler := []byte("+------------------------------+------------------------------------+\n")
107
108 src, err := ioutil.ReadAll(os.Stdin)
109 if err != nil {
110 return nil, err
111 }
112 if i := bytes.Index(src, appendixA); i < 0 {
113 return nil, fmt.Errorf("main: couldn't find Appendix A")
114 } else {
115 src = src[i+len(appendixA):]
116 }
117 if i := bytes.Index(src, ruler); i < 0 {
118 return nil, fmt.Errorf("main: couldn't find ---- ruler")
119 } else {
120 src = src[i+len(ruler):]
121 }
122 if i := bytes.Index(src, ruler); i < 0 {
123 return nil, fmt.Errorf("main: couldn't find ---- ruler")
124 } else {
125 src = src[i+len(ruler):]
126 }
127 if i := bytes.Index(src, ruler); i < 0 {
128 return nil, fmt.Errorf("main: couldn't find Appendix B")
129 } else {
130 src = src[:i]
131 }
132 return bytes.TrimSpace(src), nil
133}