This file is indexed.

/usr/share/pyshared/remuco/serial.py is in remuco-base 0.9.6-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# =============================================================================
#
#    Remuco - A remote control system for media players.
#    Copyright (C) 2006-2010 by the Remuco team, see AUTHORS.
#
#    This file is part of Remuco.
#
#    Remuco is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Remuco is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Remuco.  If not, see <http://www.gnu.org/licenses/>.
#
# =============================================================================

import inspect
import struct
import array

from remuco import log

TYPE_Y = 1
TYPE_I = 2
TYPE_B = 3
TYPE_S = 4
TYPE_AY = 5
TYPE_AI = 6
TYPE_AS = 7
TYPE_L = 8
TYPE_N = 9
TYPE_AN = 10
TYPE_AB = 11
TYPE_AL = 12

class Bin:
    
    NET_ENCODING = "UTF-8" # codec for data exchanged with clients
    NET_ENCODING_ALT = ("UTF-8", "UTF8", "utf-8", "utf8") # synonyms
    HOST_ENCODING = NET_ENCODING # will be updated with value from config file
    
    def __init__(self, buff=None):
        
        self.__data = buff or array.array('c')
        self.__off = 0
        
    def get_buff(self):
        if isinstance(self.__data, basestring):
            return self.__data
        elif isinstance(self.__data, array.array):
            return self.__data.tostring()
        else:
            log.error("** BUG ** unexpected buffer type")
        
    def read_boolean(self):
        
        b = self.read_byte()
        if b == 0:
            return False
        else:
            return True

    def read_byte(self):
        
        y = struct.unpack_from('b', self.__data, offset=self.__off)[0]
        self.__off += 1
        return y
        
    def read_short(self):
        
        n = struct.unpack_from('!h', self.__data, offset=self.__off)[0]
        self.__off += 2
        return n
    
    def read_int(self):
        
        i = struct.unpack_from('!i', self.__data, offset=self.__off)[0]
        self.__off += 4
        return i
    
    def read_long(self):
        
        l = struct.unpack_from('!q', self.__data, offset=self.__off)[0]
        self.__off += 8
        return l
    
    def read_string(self):
        """ Read a string.
        
        The read raw string will be converted from Bin.NET_ENCODING to
        Bin.HOST_ENCODING.
        """
        
        s = self.__read_string()
        
        if Bin.HOST_ENCODING not in Bin.NET_ENCODING_ALT:
            try:
                s = unicode(s, Bin.NET_ENCODING).encode(Bin.HOST_ENCODING)
            except UnicodeDecodeError, e:
                log.warning("could not decode '%s' with codec %s (%s)" %
                            (s, Bin.NET_ENCODING, e))
            except UnicodeEncodeError, e:
                log.warning("could not encode '%s' with codec %s (%s)" %
                            (s, Bin.HOST_ENCODING, e))
                
        return s

    def read_type(self, expected):
        
        type = self.read_byte()
         
        if type != expected:
            log.warning("bin data malformed (expected type %d, have %d)" %
                        (expected, type))
            return False
        else:
            return True
        
    def read_array_boolean(self):
        
        return self.__read_array(self.read_boolean)

    def read_array_byte(self):
        
        return self.__read_array(self.read_byte)

    def read_array_short(self):
        
        return self.__read_array(self.read_short)
    
    def read_array_int(self):
        
        return self.__read_array(self.read_int)
    
    def read_array_long(self):
        
        return self.__read_array(self.read_long)
    
    def read_array_string(self):
        
        return self.__read_array(self.read_string)
            
    def __read_string(self):
        """ Read a string as it is, i.e. without any codec conversion. """
        
        l = self.read_short()
        s = struct.unpack_from('%ds' % l, self.__data, offset=self.__off)[0]
        self.__off += l
        return s
        
    def __read_array(self, fn_read_element):
        
        num = self.read_int()
        
        a = []
        
        for i in range(num):
            
            a.append(fn_read_element())
            
        return a
    
    def get_unused_data(self):
        
        return len(self.__data) - self.__off
        
    def write_type(self, type):
        
        self.write_byte(type)
    
    def write_boolean(self, b):
        
        if b:
            self.write_byte(1)
        else:
            self.write_byte(0)
        
    def write_byte(self, y):
        
        if y is None: y = 0
        self.__data.extend(' ' * 1)
        struct.pack_into('b', self.__data, self.__off, y)
        self.__off += 1

    def write_short(self, n):
        
        if n is None: n = 0
        self.__data.extend(' ' * 2)
        struct.pack_into('!h', self.__data, self.__off, n)
        self.__off += 2

    def write_int(self, i):
        
        if i is None: i = 0
        self.__data.extend(' ' * 4)
        struct.pack_into('!i', self.__data, self.__off, i)
        self.__off += 4

    def write_long(self, l):
        
        if l is None: l = 0
        self.__data.extend(' ' * 8)
        struct.pack_into('!q', self.__data, self.__off, l)
        self.__off += 8

    def write_string(self, s):
        """ Write a string. 
        
        If the string is a unicode string, it will be encoded as a normal string
        in Bin.NET_ENCODING. If it already is a normal string it will be
        converted from Bin.HOST_ENCODING to Bin.NET_ENCODING.
        
        """
        if s is None:
            self.__write_string(s)
            return
        
        if isinstance(s, unicode):
            
            try:
                s = s.encode(Bin.NET_ENCODING)
            except UnicodeEncodeError, e:
                log.warning("could not encode '%s' with codec %s (%s)" %
                            (s, Bin.NET_ENCODING, e))
                s = str(s)
        
        elif Bin.HOST_ENCODING not in Bin.NET_ENCODING_ALT:
            log.debug("convert '%s' from %s to %s" %
                      (s, Bin.HOST_ENCODING, Bin.NET_ENCODING))
            try:
                s = unicode(s, Bin.HOST_ENCODING).encode(Bin.NET_ENCODING)
            except UnicodeDecodeError, e:
                log.warning("could not decode '%s' with codec %s (%s)" %
                            (s, Bin.HOST_ENCODING, e))
            except UnicodeEncodeError, e:
                log.warning("could not encode '%s' with codec %s (%s)" %
                            (s, Bin.NET_ENCODING, e))
            
        self.__write_string(s)

    def write_array_boolean(self, ba):
        
        self.__write_array(ba, self.write_boolean)

    def write_array_byte(self, ba):
        
        if isinstance(ba, str): # byte sequences often come as strings
            self.__write_string(ba, len_as_int=True)
        else:
            self.__write_array(ba, self.write_byte)

    def write_array_short(self, na):
        
        self.__write_array(na, self.write_short)

    def write_array_int(self, ia):
        
        self.__write_array(ia, self.write_int)

    def write_array_long(self, ia):
        
        self.__write_array(ia, self.write_long)

    def write_array_string(self, sa):
        
        self.__write_array(sa, self.write_string)

    def __write_string(self, s, len_as_int=False):
        """ Write a string. 
        
        The string is written as is, i.e. there is no codec conversion.
        """
        
        if s is None:
            s = ""
            
        if not isinstance(s, basestring):
            s = str(s)
        
        l = len(s)
        
        if len_as_int:
            self.write_int(l)
        else:
            self.write_short(l)
        
        self.__data.extend(' ' * l)
        struct.pack_into('%ds' % l, self.__data, self.__off, s)
        self.__off += l
        
    def __write_array(self, a, fn_element_write):
        
        if a is None:
            l = 0
        else:
            l = len(a)
        
        self.write_int(l)
        
        for i in range(l):
            
            fn_element_write(a[i])

class Serializable(object):

    def get_fmt(self):
        
        raise NotImplementedError
        
    def get_data(self):

        raise NotImplementedError
        
    def set_data(self, data):

        raise NotImplementedError
    
def pack(serializable):

    fmt = serializable.get_fmt()
    
    data = serializable.get_data()
    
    if len(fmt) != len(data):
        log.error("** BUG ** format string and data differ in length")
        return None
        
    #log.debug("data to pack: %s" % str(data))

    bin = Bin()
    
    try:

        for i in range(0,len(fmt)):
            
            type = fmt[i]
            
            bin.write_byte(type)
            
            if type == TYPE_Y:
                
                bin.write_byte(data[i])
                
            elif type == TYPE_B:
                
                bin.write_boolean(data[i])
        
            elif type == TYPE_N:
                
                bin.write_short(data[i])
                
            elif type == TYPE_I:
                
                bin.write_int(data[i])
                
            elif type == TYPE_L:
                
                bin.write_long(data[i])
                
            elif type == TYPE_S:
                
                bin.write_string(data[i])
                
            elif type == TYPE_AB:
                
                bin.write_array_boolean(data[i])
                
            elif type == TYPE_AY:
                
                bin.write_array_byte(data[i])
                
            elif type == TYPE_AN:
                
                bin.write_array_short(data[i])

            elif type == TYPE_AI:
                
                bin.write_array_int(data[i])

            elif type == TYPE_AL:
                
                bin.write_array_long(data[i])

            elif type == TYPE_AS:
                
                bin.write_array_string(data[i])

            else:
                log.error("** BUG ** unknown type (%d) in format string" % type)
                return None
        
    except struct.error, e:
        
        log.exception("** BUG ** %s" % e)
        
        return None
    
    return bin.get_buff()

def unpack(serializable, bytes):
    """ Deserialize a Serializable.
    
    @param serializable:
        the Serializable to apply the binary data to (may be a class, in which
        case a new instance of this class is created)
    @param bytes:
        binary data (serialized Serializable)
    
    @return: 'serializable' itself if it is an instance of Serializable, a new
        instance of 'serializable' if it is a class or None if an error
        occurred
    """
    
    if inspect.isclass(serializable):
        serializable = serializable()
    
    fmt = serializable.get_fmt()
    
    if fmt and not bytes:
        log.warning("there is no data to unpack")
        return None
    
    data = []
    
    bin = Bin(buff=bytes)
    
    try:

        for type in fmt:
            
            if not bin.read_type(type):
                return None
            
            if type == TYPE_Y:
                
                data.append(bin.read_byte())
                
            elif type == TYPE_B:
                
                data.append(bin.read_boolean())
        
            elif type == TYPE_N:
                
                data.append(bin.read_short())
                
            elif type == TYPE_I:
                
                data.append(bin.read_int())
                
            elif type == TYPE_L:
                
                data.append(bin.read_long())
                
            elif type == TYPE_S:
                
                data.append(bin.read_string())
                
            elif type == TYPE_AB:
                
                data.append(bin.read_array_boolean())
                
            elif type == TYPE_AY:
                
                data.append(bin.read_array_byte())
                
            elif type == TYPE_AN:
                
                data.append(bin.read_array_short())

            elif type == TYPE_AI:
                
                data.append(bin.read_array_int())

            elif type == TYPE_AL:
                
                data.append(bin.read_array_long())

            elif type == TYPE_AS:
                
                data.append(bin.read_array_string())

            else:
                
                log.warning("bin data malformed (unknown data type: %d)" % type)
                return None
        
    except struct.error, e:
        
        log.warning("bin data malformed (%s)" % e)
        
        return None
    
    unused = bin.get_unused_data()
    if unused:
        log.warning("there are %d unused bytes" % unused)
        return None
    
    serializable.set_data(data)
    
    #log.debug("unpacked data  : %s" % str(data))

    return serializable