File: TXT.rb

package info (click to toggle)
dnsruby 1.54-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,184 kB
  • sloc: ruby: 15,095; makefile: 3
file content (192 lines) | stat: -rw-r--r-- 5,787 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#--
#Copyright 2007 Nominet UK
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License. 
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0 
#
#Unless required by applicable law or agreed to in writing, software 
#distributed under the License is distributed on an "AS IS" BASIS, 
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
#See the License for the specific language governing permissions and 
#limitations under the License.
#++
begin
  require 'jcode'
rescue LoadError => e
end
module Dnsruby
  class RR
    #Class for DNS Text (TXT) resource records.
    #RFC 1035 Section 3.3.14
    class TXT < RR
      ClassValue = nil #:nodoc: all
      TypeValue = Types::TXT #:nodoc: all
      
      #List of the individual elements
      attr_accessor :strings
      
      def data
        @strings[0]
      end
      
      def from_data(data)
        @strings = data
      end
      
      def from_hash(hash)
        if (hash.has_key?:strings)
          from_string(hash[:strings])
        end
      end

      ESCAPE_CHARS = {"b" => 8, "t" => 9, "n" => 10, "v" => 11, "f" => 12, "r" => 13}
      ESCAPE_CODES = ESCAPE_CHARS.invert
      
      def from_string(input)
        @strings = TXT.parse(input)
      end

      def TXT.parse(input)
        # Need to look out for special characters.
        # Need to split the input up into strings (which are defined by non-escaped " characters)
        # Then need to fix up any \ escape characters (should just be " and ; and binary?)
        # Sadly, it's going to be easiest just to scan through this character by character...
        in_escaped = false
        in_string = false
        count = -1
        strings = []
        current_binary = ""
        current_quote_char = '"'
        unquoted = false
        seen_strings = false
        pos = 0
        input.sub!(/^\s*\(\s*/, "")
        input.sub!(/\s*\)\s*$/, "")
        input.each_char {|c|
          if (((c == "'") || (c == '"')) && (!in_escaped) && (!unquoted))
            if (!in_string)
              seen_strings = true
              current_quote_char = c
              in_string = true
              count+=1
              strings[count] = ""
            else
              if (c == current_quote_char)
                in_string = false
              else
                strings[count]+=c
              end
            end
          else
            if (seen_strings && !in_string)
              next
            end
            if (pos == 0)
              unquoted = true
              count+=1
              strings[count] = ""
            elsif (unquoted)
              if (c == " ")
                count+=1
                strings[count] = ""
                pos += 1
                next
              end
            end

            if (c == "\\")
              if (in_escaped)
                in_escaped = false
                strings[count]+=(c)
              else
                in_escaped = true
              end
            else
              if (in_escaped)
                # Build up the binary
                if (c == ";") || (c == '"')
                  strings[count]+=c
                  in_escaped = false
                elsif (ESCAPE_CHARS[c])
                  in_escaped=false
                  strings[count]+=ESCAPE_CHARS[c].chr
                elsif (c<"0" || c>"9")
                  in_escaped = false
                  strings[count]+=c
                else
                  # Must be building up three digit string to identify binary value?
#                  if (c >= "0" && c <= "9")
                    current_binary += c
#                  end
                  if ((current_binary.length == 3) ) # || (c < "0" || c > "9"))
                    strings[count]+=current_binary.to_i.chr
                    in_escaped = false
                    current_binary = ""
                  end
                end
              else
                strings[count]+=(c)
              end
            end
          end
          pos += 1
        }
        return strings
      end

      def TXT.display(str, do_escapes = true)
        output = ""
        # Probably need to scan through each string manually
        # Make sure to remember to escape binary characters.
        # Go through copying to output, and adding "\" characters as necessary?
        str.each_byte {|c|
          if (c == 34) || (c == 92) # || (c == 59)
            if (do_escapes)
            output+='\\'
            end
            output+=c.chr
          elsif (c < 32) # c is binary
            if (ESCAPE_CODES[c])
              output +=  c.chr
            else
              output+= '\\'
              num = c.to_i.to_s
              (3-num.length).times {|i|
                num="0"+num
              }
              output+= num # Need a 3 digit number here.
            end

          else
            output += c.chr
          end
        }
        return output
      end
      
      def rdata_to_string
        if (defined?@strings)
          temp = []
          @strings.each {|str|
            output = TXT.display(str)
            temp.push("\"#{output}\"")
          }
          return temp.join(' ')
        end
        return ''
      end
      
      def encode_rdata(msg, canonical=false) #:nodoc: all
        msg.put_string_list(@strings)
      end
      
      def self.decode_rdata(msg) #:nodoc: all
        strings = msg.get_string_list
        return self.new(strings)
      end
    end
  end
end