Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

278 строки
6.9 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. package main
  15. import (
  16. "bufio"
  17. "bytes"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "os"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. )
  26. // libraryNames must be kept in sync with the enum in err.h. The generated code
  27. // will contain static assertions to enforce this.
  28. var libraryNames = []string{
  29. "NONE",
  30. "SYS",
  31. "BN",
  32. "RSA",
  33. "DH",
  34. "EVP",
  35. "BUF",
  36. "OBJ",
  37. "PEM",
  38. "DSA",
  39. "X509",
  40. "ASN1",
  41. "CONF",
  42. "CRYPTO",
  43. "EC",
  44. "SSL",
  45. "BIO",
  46. "PKCS7",
  47. "PKCS8",
  48. "X509V3",
  49. "RAND",
  50. "ENGINE",
  51. "OCSP",
  52. "UI",
  53. "COMP",
  54. "ECDSA",
  55. "ECDH",
  56. "HMAC",
  57. "DIGEST",
  58. "CIPHER",
  59. "HKDF",
  60. "USER",
  61. }
  62. // stringList is a map from uint32 -> string which can output data for a sorted
  63. // list as C literals.
  64. type stringList struct {
  65. // entries is an array of keys and offsets into |stringData|. The
  66. // offsets are in the bottom 15 bits of each uint32 and the key is the
  67. // top 17 bits.
  68. entries []uint32
  69. // internedStrings contains the same strings as are in |stringData|,
  70. // but allows for easy deduplication. It maps a string to its offset in
  71. // |stringData|.
  72. internedStrings map[string]uint32
  73. stringData []byte
  74. }
  75. func newStringList() *stringList {
  76. return &stringList{
  77. internedStrings: make(map[string]uint32),
  78. }
  79. }
  80. // offsetMask is the bottom 15 bits. It's a mask that selects the offset from a
  81. // uint32 in entries.
  82. const offsetMask = 0x7fff
  83. func (st *stringList) Add(key uint32, value string) error {
  84. if key&offsetMask != 0 {
  85. return errors.New("need bottom 15 bits of the key for the offset")
  86. }
  87. offset, ok := st.internedStrings[value]
  88. if !ok {
  89. offset = uint32(len(st.stringData))
  90. if offset&offsetMask != offset {
  91. return errors.New("stringList overflow")
  92. }
  93. st.stringData = append(st.stringData, []byte(value)...)
  94. st.stringData = append(st.stringData, 0)
  95. st.internedStrings[value] = offset
  96. }
  97. for _, existing := range st.entries {
  98. if existing>>15 == key>>15 {
  99. panic("duplicate entry")
  100. }
  101. }
  102. st.entries = append(st.entries, key|offset)
  103. return nil
  104. }
  105. // keySlice is a type that implements sorting of entries values.
  106. type keySlice []uint32
  107. func (ks keySlice) Len() int {
  108. return len(ks)
  109. }
  110. func (ks keySlice) Less(i, j int) bool {
  111. return (ks[i] >> 15) < (ks[j] >> 15)
  112. }
  113. func (ks keySlice) Swap(i, j int) {
  114. ks[i], ks[j] = ks[j], ks[i]
  115. }
  116. func (st *stringList) buildList() []uint32 {
  117. sort.Sort(keySlice(st.entries))
  118. return st.entries
  119. }
  120. type stringWriter interface {
  121. io.Writer
  122. WriteString(string) (int, error)
  123. }
  124. func (st *stringList) WriteTo(out stringWriter, name string) {
  125. list := st.buildList()
  126. fmt.Fprintf(os.Stderr, "%s: %d bytes of list and %d bytes of string data.\n", name, 4*len(list), len(st.stringData))
  127. values := "kOpenSSL" + name + "Values"
  128. out.WriteString("const uint32_t " + values + "[] = {\n")
  129. for _, v := range list {
  130. fmt.Fprintf(out, " 0x%x,\n", v)
  131. }
  132. out.WriteString("};\n\n")
  133. out.WriteString("const size_t " + values + "Len = sizeof(" + values + ") / sizeof(" + values + "[0]);\n\n")
  134. stringData := "kOpenSSL" + name + "StringData"
  135. out.WriteString("const char " + stringData + "[] =\n \"")
  136. for i, c := range st.stringData {
  137. if c == 0 {
  138. out.WriteString("\\0\"\n \"")
  139. continue
  140. }
  141. out.Write(st.stringData[i : i+1])
  142. }
  143. out.WriteString("\";\n\n")
  144. }
  145. type errorData struct {
  146. reasons *stringList
  147. libraryMap map[string]uint32
  148. }
  149. func (e *errorData) readErrorDataFile(filename string) error {
  150. inFile, err := os.Open(filename)
  151. if err != nil {
  152. return err
  153. }
  154. defer inFile.Close()
  155. scanner := bufio.NewScanner(inFile)
  156. comma := []byte(",")
  157. lineNo := 0
  158. for scanner.Scan() {
  159. lineNo++
  160. line := scanner.Bytes()
  161. if len(line) == 0 {
  162. continue
  163. }
  164. parts := bytes.Split(line, comma)
  165. if len(parts) != 3 {
  166. return fmt.Errorf("bad line %d in %s: found %d values but want 3", lineNo, filename, len(parts))
  167. }
  168. libNum, ok := e.libraryMap[string(parts[0])]
  169. if !ok {
  170. return fmt.Errorf("bad line %d in %s: unknown library", lineNo, filename)
  171. }
  172. if libNum >= 64 {
  173. return fmt.Errorf("bad line %d in %s: library value too large", lineNo, filename)
  174. }
  175. key, err := strconv.ParseUint(string(parts[1]), 10 /* base */, 32 /* bit size */)
  176. if err != nil {
  177. return fmt.Errorf("bad line %d in %s: %s", lineNo, filename, err)
  178. }
  179. if key >= 2048 {
  180. return fmt.Errorf("bad line %d in %s: key too large", lineNo, filename)
  181. }
  182. value := string(parts[2])
  183. listKey := libNum<<26 | uint32(key)<<15
  184. err = e.reasons.Add(listKey, value)
  185. if err != nil {
  186. return err
  187. }
  188. }
  189. return scanner.Err()
  190. }
  191. func main() {
  192. e := &errorData{
  193. reasons: newStringList(),
  194. libraryMap: make(map[string]uint32),
  195. }
  196. for i, name := range libraryNames {
  197. e.libraryMap[name] = uint32(i) + 1
  198. }
  199. cwd, err := os.Open(".")
  200. if err != nil {
  201. panic(err)
  202. }
  203. names, err := cwd.Readdirnames(-1)
  204. if err != nil {
  205. panic(err)
  206. }
  207. sort.Strings(names)
  208. for _, name := range names {
  209. if !strings.HasSuffix(name, ".errordata") {
  210. continue
  211. }
  212. if err := e.readErrorDataFile(name); err != nil {
  213. panic(err)
  214. }
  215. }
  216. out := os.Stdout
  217. out.WriteString(`/* Copyright (c) 2015, Google Inc.
  218. *
  219. * Permission to use, copy, modify, and/or distribute this software for any
  220. * purpose with or without fee is hereby granted, provided that the above
  221. * copyright notice and this permission notice appear in all copies.
  222. *
  223. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  224. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  225. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  226. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  227. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  228. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  229. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  230. /* This file was generated by err_data_generate.go. */
  231. #include <openssl/base.h>
  232. #include <openssl/err.h>
  233. #include <openssl/type_check.h>
  234. `)
  235. for i, name := range libraryNames {
  236. fmt.Fprintf(out, "OPENSSL_COMPILE_ASSERT(ERR_LIB_%s == %d, library_values_changed_%d);\n", name, i+1, i+1)
  237. }
  238. fmt.Fprintf(out, "OPENSSL_COMPILE_ASSERT(ERR_NUM_LIBS == %d, library_values_changed_num);\n", len(libraryNames)+1)
  239. out.WriteString("\n")
  240. e.reasons.WriteTo(out, "Reason")
  241. }