compare.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2015 Kurt Jung (Gmail: kurt.w.jung)
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. package gofpdf
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "sort"
  23. )
  24. type sortType struct {
  25. length int
  26. less func(int, int) bool
  27. swap func(int, int)
  28. }
  29. func (s *sortType) Len() int {
  30. return s.length
  31. }
  32. func (s *sortType) Less(i, j int) bool {
  33. return s.less(i, j)
  34. }
  35. func (s *sortType) Swap(i, j int) {
  36. s.swap(i, j)
  37. }
  38. func gensort(Len int, Less func(int, int) bool, Swap func(int, int)) {
  39. sort.Sort(&sortType{length: Len, less: Less, swap: Swap})
  40. }
  41. func writeBytes(leadStr string, startPos int, sl []byte) {
  42. var pos, max int
  43. var b byte
  44. fmt.Printf("%s %07x", leadStr, startPos)
  45. max = len(sl)
  46. for pos < max {
  47. fmt.Printf(" ")
  48. for k := 0; k < 8; k++ {
  49. if pos < max {
  50. fmt.Printf(" %02x", sl[pos])
  51. } else {
  52. fmt.Printf(" ")
  53. }
  54. pos++
  55. }
  56. }
  57. fmt.Printf(" |")
  58. pos = 0
  59. for pos < max {
  60. b = sl[pos]
  61. if b < 32 || b >= 128 {
  62. b = '.'
  63. }
  64. fmt.Printf("%c", b)
  65. pos++
  66. }
  67. fmt.Printf("|\n")
  68. }
  69. func checkBytes(pos int, sl1, sl2 []byte, printDiff bool) (eq bool) {
  70. eq = bytes.Equal(sl1, sl2)
  71. if !eq && printDiff {
  72. writeBytes("<", pos, sl1)
  73. writeBytes(">", pos, sl2)
  74. }
  75. return
  76. }
  77. // CompareBytes compares the bytes referred to by sl1 with those referred to by
  78. // sl2. Nil is returned if the buffers are equal, otherwise an error.
  79. func CompareBytes(sl1, sl2 []byte, printDiff bool) (err error) {
  80. var posStart, posEnd, len1, len2, length int
  81. var diffs bool
  82. len1 = len(sl1)
  83. len2 = len(sl2)
  84. length = len1
  85. if length > len2 {
  86. length = len2
  87. }
  88. for posStart < length-1 {
  89. posEnd = posStart + 16
  90. if posEnd > length {
  91. posEnd = length
  92. }
  93. if !checkBytes(posStart, sl1[posStart:posEnd], sl2[posStart:posEnd], printDiff) {
  94. diffs = true
  95. }
  96. posStart = posEnd
  97. }
  98. if diffs {
  99. err = fmt.Errorf("documents are different")
  100. }
  101. return
  102. }
  103. // ComparePDFs reads and compares the full contents of the two specified
  104. // readers byte-for-byte. Nil is returned if the buffers are equal, otherwise
  105. // an error.
  106. func ComparePDFs(rdr1, rdr2 io.Reader, printDiff bool) (err error) {
  107. var b1, b2 *bytes.Buffer
  108. _, err = b1.ReadFrom(rdr1)
  109. if err == nil {
  110. _, err = b2.ReadFrom(rdr2)
  111. if err == nil {
  112. err = CompareBytes(b1.Bytes(), b2.Bytes(), printDiff)
  113. }
  114. }
  115. return
  116. }
  117. // ComparePDFFiles reads and compares the full contents of the two specified
  118. // files byte-for-byte. Nil is returned if the file contents are equal, or if
  119. // the second file is missing, otherwise an error.
  120. func ComparePDFFiles(file1Str, file2Str string, printDiff bool) (err error) {
  121. var sl1, sl2 []byte
  122. sl1, err = ioutil.ReadFile(file1Str)
  123. if err == nil {
  124. sl2, err = ioutil.ReadFile(file2Str)
  125. if err == nil {
  126. err = CompareBytes(sl1, sl2, printDiff)
  127. } else {
  128. // Second file is missing; treat this as success
  129. err = nil
  130. }
  131. }
  132. return
  133. }