Class: Ucp::Util::UCP

Inherits:
Object
  • Object
show all
Defined in:
lib/ucp/util/ucp.rb

Class Method Summary collapse

Class Method Details

.add_char(value, char) ⇒ Object



37
38
39
40
# File 'lib/ucp/util/ucp.rb', line 37

def self.add_char(value,char)
  @gsmtable[char]=value
  @asciitable[value]=char
end

.add_extchar(value, char) ⇒ Object



44
45
46
47
# File 'lib/ucp/util/ucp.rb', line 44

def self.add_extchar(value,char)
  @extensiontable[char]=value
  @extensiontable_rev[value]=char
end

.ascii2ira(str, max_bytes = nil) ⇒ Object

convert standard string to IRA encoded hexstring



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
# File 'lib/ucp/util/ucp.rb', line 197

def self.ascii2ira(str,max_bytes=nil)

  tainted=false  # true if not able to convert a character
  s=""
  idx=0
  str.each_char  { |c|
    gsmchar=@gsmtable[c]

    ext=""
    if gsmchar.nil?
      if @extensiontable.has_key?(c)
        ext="1B"
        gsmchar=@extensiontable[c]
      else
        gsmchar=@gsmtable[" "]
        tainted=true
      end
    end


    tmp=int2hex(gsmchar)


    if !max_bytes.nil?
      # if adding this character exceeds the max allowed nr of bytes, break
      if ((tmp+ext+s).length*7.0/16).ceil>max_bytes
        break
      end
    end

    s+=ext+tmp
    idx+=(ext+tmp).length/2

    # if reached the max allowed nr of bytes, break
    #    if (s.length*7.0/16).floor==max_bytes
    #      break
    #    end

  }

  required_septets=s.length/2
  #puts "idx: #{idx}; req_sep: #{required_septets}"
  return GsmPackedMsg.new(s,UCP.utf8_substr(str,0,idx-1),idx,required_septets,tainted)
end

.decode7bitgsm(str) ⇒ Object

decode a 7bit packed GSM default alphabet hexstring



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
# File 'lib/ucp/util/ucp.rb', line 439

def self.decode7bitgsm(str)        
  unencoded=""
  #puts "#{str}"
  bstr=UCP.hextobin_reversed(str)
  #puts "#{bstr}"
  # 110 1111 110 1100 110 0001
  # 1000011 1011101 0110111   000

  i=bstr.length-7
  while i>=0
    value=bstr[i,7].to_i(2)
    if value==0x1B
      i-=7
      value=@extensiontable_rev[bstr[i,7].to_i(2)]
      value=" " if value.nil?
      unencoded+=value
    else
      val=@asciitable[value]
      #puts "value: #{value} ; val: #{val}"
      val=" " if val.nil?
      unencoded+=val
      #unencoded+=value.chr
    end


    

    i-=7
  end


  #    i=0
  #    while i<str.length
  #      hexv=str[i,2]
  #      if "1B".eql?(hexv)
  #        i+=2
  #        hexv=str[i,2]
  #      end
  #
  #      unencoded+=hexv.to_i(16).chr
  #
  #      i+=2
  #    end


  return unencoded
end

.decode_ira(str) ⇒ Object

decode an IRA5 hexadecimal represented string to string



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/ucp/util/ucp.rb', line 489

def self.decode_ira(str)
  unencoded=""

  i=0
  while i<str.length
    hexv=str[i,2]
    if "1B".eql?(hexv)
      i+=2
      hexv=str[i,2]

      value=@extensiontable_rev[hexv.to_i(16)]
      value=" " if value.nil?
      unencoded+=value
    else
      val=@asciitable[hexv.to_i(16)]
      val=" " if val.nil?
      unencoded+=val
    end

    i+=2
  end

  return unencoded
end

.decode_ucp_msg(ucp) ⇒ Object

given an UCP pdu object, decode message field



722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/ucp/util/ucp.rb', line 722

def self.decode_ucp_msg(ucp)
  text=nil
  dcs=ucp.dcs.to_i(16)

  if (dcs & 0x0F == 0x01) || (dcs & 0x0F == 0x00)

    if ucp.operation.eql?("30")
      text=UCP.decode_ira(ucp.get_field(:amsg))
    else

      if ucp.operation.eql?("51") || ucp.operation.eql?("52") || ucp.operation.eql?("53")
        mt=ucp.get_field(:mt)
        if mt.eql?("2")
          # numeric message.. return it as it is
          text=ucp.get_field(:msg)
        elsif mt.eql?("3")
          text=UCP.decode_ira(ucp.get_field(:msg))
        else
          # unexpected "mt" value. return nil explicitely
          return nil
        end
      elsif ucp.operation.eql?("01")
        text=UCP.decode_ira(ucp.get_field(:msg))
      else
        # unexpected operation. return nil explicitely
        return nil
      end

    end
  elsif (dcs & 0x0F == 0x08) || (dcs & 0x0F == 0x09)
    str=UCP.hex2str(ucp.get_field(:msg))
    text=Iconv.iconv("utf-8","utf-16be",str).first
  else
    # cant decode text; unsupported DCS
    return nil
  end

  return text
end

.decode_ucp_oadc(ucp) ⇒ Object

given an UCP pdu object, decode the originator address



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/ucp/util/ucp.rb', line 763

def self.decode_ucp_oadc(ucp)
  otoa=nil
  oadc=ucp.get_field(:oadc)

  if ucp.is_a?(Ucp51Operation) || ucp.is_a?(Ucp52Operation) || ucp.is_a?(Ucp53Operation) || ucp.is_a?(Ucp54Operation) || ucp.is_a?(Ucp55Operation) || ucp.is_a?(Ucp56Operation) || ucp.is_a?(Ucp57Operation) || ucp.is_a?(Ucp58Operation)
    otoa=ucp.get_field(:otoa)
  end

  if otoa.nil? || otoa.eql?("1139")
    return oadc
  elsif otoa.eql?("5039")
    #useful_nibbles=oadc[0,2].to_i(16)
    return UCP.decode7bitgsm(oadc[2..-1])
  else
    #
  end

  # by default, return oadc
  return oadc
end

.hex2str(hexstr) ⇒ Object

convert hexadecimal represent string to string, assuming a byte per character



715
716
717
718
719
# File 'lib/ucp/util/ucp.rb', line 715

def self.hex2str(hexstr)
  str=""
  hexstr.scan(/../).each { | tuple | str+=tuple.hex.chr }
  return str
end

.hextobin(hstr) ⇒ Object

convert an hexadecimal string to a binary string



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/ucp/util/ucp.rb', line 403

def self.hextobin(hstr)
  bstr=""
  i=0
  while i<hstr.length
    tmp=hstr[i,2].to_i(16).to_s(2)

    nfillbits=8-tmp.length
    if nfillbits!=0
      tmp="0"*nfillbits+tmp
    end

    bstr+=tmp
    i+=2
  end
  return bstr
end

.hextobin_reversed(hstr) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/ucp/util/ucp.rb', line 421

def self.hextobin_reversed(hstr)
  bstr=""
  i=0
  while i<hstr.length
    tmp=hstr[i,2].to_i(16).to_s(2)

    nfillbits=8-tmp.length
    if nfillbits!=0
      tmp="0"*nfillbits+tmp
    end

    bstr=tmp+bstr
    i+=2
  end
  return bstr
end

.initialize_ascii2iraObject

build/initialize the GSM default alphabet mapping tables



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
# File 'lib/ucp/util/ucp.rb', line 50

def self.initialize_ascii2ira
  
  ('A'..'Z').each { |c|
    add_char(c[0],c)
  }
  ('a'..'z').each { |c|
    #@gsmtable[c]=c[0]
    #@asciitable[c[0]]=c
    add_char(c[0],c)
  }
  ('0'..'9').each { |c|
    add_char(c[0],c)
  }


  add_char(0x00,"@")
  add_char(0x01,"£")
  add_char(0x02,"$")
  add_char(0x03,"¥")
  add_char(0x04,"è")
  add_char(0x05,"é")
  add_char(0x06,"ù")
  add_char(0x07,"ì")
  add_char(0x08,"ò")
  add_char(0x09,"Ç")
  add_char(0x0A,"\n")
  add_char(0x0B,"Ø")
  add_char(0x0C,"ø")
  add_char(0x0D,"\r")
  add_char(0x0E,"Å")
  add_char(0x0F,"å")

  add_char(0x10, "Δ")
  add_char(0x11, "_")
  add_char(0x12, "Φ")
  add_char(0x13, "Γ")
  add_char(0x14, "Λ")
  add_char(0x15, "Ω")
  add_char(0x16, "Π")
  add_char(0x17, "Ψ")
  add_char(0x18, "Σ")
  add_char(0x19, "Θ")
  add_char(0x1A, "Ξ")
  # 0x1B is escape sequence for extended characters
  add_char(0x1C, "Æ")
  add_char(0x1D, "æ")
  add_char(0x1E, "ß")
  add_char(0x1F,"É")



  add_char(0x20," ")
  add_char(0x21,"!")
  add_char(0x22,'"')
  add_char(0x23,"#")
  add_char(0x24,"¤")
  add_char(0x25,"%")
  add_char(0x26,"&")
  add_char(0x27,"'")
  add_char(0x28,"(")
  add_char(0x29,")")
  add_char(0x2A,"*")
  add_char(0x2B,"+")
  add_char(0x2C,",")
  add_char(0x2D,"-")
  add_char(0x2E,".")
  add_char(0x2F,"/")

  add_char(0x3A,":")
  add_char(0x3B,";")
  add_char(0x3C,"<")
  add_char(0x3D,"=")
  add_char(0x3E,">")
  add_char(0x3F,"?")

  add_extchar(0x65,"")
  add_extchar(0x14,"^")
  add_extchar(0x28,"{")
  add_extchar(0x29,"}")
  add_extchar(0x2F,"\\")
  add_extchar(0x3C,"[")
  add_extchar(0x3D,"~")
  add_extchar(0x3E,"]")
  add_extchar(0x40,"|")
end

.int2hex(i, max_nibbles = 2) ⇒ Object

convert an integer to an hex string, two (or given) nibbles wide



518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/ucp/util/ucp.rb', line 518

def self.int2hex(i,max_nibbles=2)
  tmp=i.to_s(16).upcase
  if tmp.length%2!=0
    tmp="0"+tmp
  end

  remaining_nibbles=max_nibbles-tmp.length
  if remaining_nibbles>0
    tmp="0"*remaining_nibbles+tmp;
  end

  return tmp
end

.make_multi_ucps(originator, recipient, message, mr = 0) ⇒ Object

automatically build the necessary UCP pdu’s in order to encode a given submit message returns an array of UCP51 pdu’s (for now, it does NOT select automatically the SM encoding; it forces/assumes 7bit GSM)



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
# File 'lib/ucp/util/ucp.rb', line 343

def self.make_multi_ucps(originator,recipient,message,mr=0)
  ucps=[]

  gsm7bit_encodable=false

  if gsm7bit_encodable
    gsmparts= UCP.multi_ascii2ira(message,134)
  else
    gsmparts= UCP.multi_ucs2(message,134)
  end


  part_nr=1
  gsmparts.each { |gsmpart|
    ucp=Ucp51Operation.new
    ucp.basic_submit(originator,recipient,nil)

    if gsmparts.length>1
      # concatenated xser
      ucp.add_xser("01","050003"+UCP.int2hex(mr)+UCP.int2hex(gsmparts.length)+UCP.int2hex(part_nr))
    end

    if gsm7bit_encodable
      ucp.set_fields({:mt=>3, :msg=>gsmpart.encoded})
      # DCS xser
      ucp.add_xser("02","01")
    else
      ucp.set_fields({:msg=>gsmpart.encoded,:mt=>4,:nb=>gsmpart.encoded.length*4})
      # DCS xser
      ucp.add_xser("02","08")
    end

    #ucp.trn=UCP.int2hex(initial_trn)
    #initial_trn+=1

    #puts "part: #{ucp.to_s}"
    ucps<<ucp
    part_nr+=1
  }
  return ucps
end

.make_ucp_result(ucp) ⇒ Object

given an UCP pdu object, build a corresponding result pdu



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/ucp/util/ucp.rb', line 667

def self.make_ucp_result(ucp)
  #puts "make_ucp_result(#{ucp.to_s})"

  if ucp.nil?
    return nil
  end

  trn=ucp.trn
  operation_type=ucp.operation_type
  operation=ucp.operation

  ucpmsg=nil

  if operation.eql?("01")
    ucpmsg=Ucp01Result.new
  elsif operation.eql?("30")
    ucpmsg=Ucp30Result.new
  elsif operation.eql?("31")
    ucpmsg=Ucp31Result.new
  elsif operation.eql?("51")
    ucpmsg=Ucp51Result.new
  elsif operation.eql?("52")
    ucpmsg=Ucp52Result.new
  elsif operation.eql?("53")
    ucpmsg=Ucp53Result.new
  elsif operation.eql?("54")
    ucpmsg=Ucp54Result.new
  elsif operation.eql?("55")
    ucpmsg=Ucp55Result.new
  elsif operation.eql?("56")
    ucpmsg=Ucp56Result.new
  elsif operation.eql?("57")
    ucpmsg=Ucp57Result.new
  elsif operation.eql?("58")
    ucpmsg=Ucp58Result.new
  elsif operation.eql?("60")
    ucpmsg=Ucp60Result.new
  elsif operation.eql?("61")
    ucpmsg=Ucp61Result.new
  else
    return nil
  end

  ucpmsg.trn=trn
  return ucpmsg
end

.multi_ascii2ira(str, max_bytes) ⇒ Object

given a text, automatically split it if necessary and encode each part text in (IRA5) GSM default alphabet returns an array of IRA5 hexadecimal strings, one per part



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
# File 'lib/ucp/util/ucp.rb', line 257

def self.multi_ascii2ira(str,max_bytes)
  msgparts=[]
  idx=0

  if str.jlength<=160
    packedmsg=UCP.ascii2ira(UCP.utf8_substr(str,idx),140)
    #puts "pckd_chars: #{packedmsg.chars}; strlen: #{str.jlength}"
    if packedmsg.chars==str.jlength
      msgparts<<packedmsg
      return msgparts
    end
  end

  while true
    #packedmsg=UCP.ascii2ira(str[idx..-1],max_bytes)
    #puts "bla: #{UCP.utf8_substr(str,idx)}"
    packedmsg=UCP.ascii2ira(UCP.utf8_substr(str,idx),max_bytes)
    msgparts<<packedmsg
    #break
    #puts "idx: #{idx}; pckd_chars: #{packedmsg.chars}; strlen: #{str.jlength}"
    
    #puts "sL: #{UCP.utf8_substr(str,idx,idx+packedmsg.chars- 1)} ; #{str[0,idx+packedmsg.chars].length}#"

    #if idx+packedmsg.chars<str.length
    if idx+packedmsg.chars<str.jlength
      idx+=packedmsg.chars
    else
      break
    end
  end
  return msgparts
end

.multi_ucs2(str, max_bytes) ⇒ Object

given a text, automatically split it if necessary and encode each part text in UCS-2 returns an array of UCS-2 hexadecimal strings, one per part



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
# File 'lib/ucp/util/ucp.rb', line 309

def self.multi_ucs2(str,max_bytes)
  msgparts=[]
  idx=0

  if str.jlength<=70
    packedmsg=UCP.str2ucs2(str,140)
    #puts "pckd_chars: #{packedmsg.chars}; strlen: #{str.jlength}"
    if packedmsg.chars==str.jlength
      msgparts<<packedmsg
      return msgparts
    end
  end

  while true

    #puts "bla: #{UCP.utf8_substr(str,idx)}"
    packedmsg=UCP.str2ucs2(UCP.utf8_substr(str,idx),max_bytes)
    msgparts<<packedmsg
    #break
    #puts "idx: #{idx}; pckd_chars: #{packedmsg.chars}; strlen: #{str.jlength}"
    #puts "sL: #{UCP.utf8_substr(str,idx,idx+packedmsg.chars- 1)} ; #{str[0,idx+packedmsg.chars].length}#"

    if idx+packedmsg.chars<str.jlength
      idx+=packedmsg.chars
    else
      break
    end
  end
  return msgparts
end

.pack7bits(str) ⇒ Object

pack a given text string in 7bit GSM default alphabet return it as an hexadecimal string



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
# File 'lib/ucp/util/ucp.rb', line 138

def self.pack7bits(str)
  
  s=""
  str.each_char  { |c|

    ext=""
    gsmchar=@gsmtable[c]

    if gsmchar.nil?
      if @extensiontable.has_key?(c)
        ext="0011011" # 1B
        gsmchar=@extensiontable[c]
      else
        gsmchar=@gsmtable[" "]
      end
    end

    #gsmchar=c[0]
    #puts "#{c} : #{gsmchar} xxx"
    #gsmchar=@gsmtable[" "] if gsmchar.nil?
    
    tmp= gsmchar.to_s(2)

    remainder=tmp.length%7
    if remainder!=0
      nfillbits=7-remainder
      tmp="0"*nfillbits+tmp
    end
    
    s=tmp+ext+s

  }

  remainder=s.length%8
  if remainder!=0
    nfillbits=8-remainder
    s="0"*nfillbits+s
  end

  #puts "S: #{s}"
  
  i=s.length-8
  hexstr=""
  while i>=0
    c=s[i,8]

    tmp=c.to_i(2).to_s(16).upcase
    if tmp.length==1
      tmp="0"+tmp
    end
    #      puts tmp
    hexstr+=tmp
    i-=8
  end

  return hexstr
end

.packoadc(oa) ⇒ Object

return an encoded originator alphanumeric address to be used in the “oadc” field, if alphanumeric



387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/ucp/util/ucp.rb', line 387

def self.packoadc(oa)
  packedoa=UCP.pack7bits(oa)

  # esta conta nao esta correcta... por causa das extensoes...
  #useful_nibbles=packedoa.length
  useful_nibbles=(oa.length*7.0/4).ceil

  tmp=useful_nibbles.to_s(16).upcase
  if tmp.length==1
    tmp="0"+tmp
  end

  return tmp+packedoa
end

.parse_str(ustr) ⇒ Object

given a UCP pdu string, return a UCP pdu object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/ucp/util/ucp.rb', line 535

def self.parse_str(ustr)
  #puts "parse_str(#{ustr})"

  if ustr.nil?
    return nil
  end

  arr=ustr[(ustr.index(2.chr)+1)..-2].split("/")
  trn=arr[0]
  #puts "#{trn}"
  # length
  operation_type=arr[2]
  #puts "#{operation_type}"
  operation=arr[3]
  #puts "#{operation}"

  ucpmsg=nil

  if operation.eql?("01")
    if operation_type.eql?("O")
      # puts "parsing a ucp01"
      ucpmsg=Ucp01Operation.new(arr)
    elsif operation_type.eql?("R")
      # puts "parsing a result of a  ucp01"
      ucpmsg=Ucp01Result.new(arr)        
    end
  elsif operation.eql?("30")
    if operation_type.eql?("O")
      # puts "parsing a ucp30"
      ucpmsg=Ucp30Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp30"
      ucpmsg=Ucp30Result.new(arr)
    end
  elsif operation.eql?("31")
    if operation_type.eql?("O")
      #puts "parsing a ucp31"
      ucpmsg=Ucp31Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp31"
      ucpmsg=Ucp31Result.new(arr)
    end
  elsif operation.eql?("51")
    if operation_type.eql?("O")
      #puts "parsing a ucp51"
      ucpmsg=Ucp51Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp51"
      ucpmsg=Ucp51Result.new(arr)
    end
  elsif operation.eql?("52")
    if operation_type.eql?("O")
      #puts "parsing a ucp52"
      ucpmsg=Ucp52Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp52"
      ucpmsg=Ucp52Result.new(arr)
    end
  elsif operation.eql?("53")
    if operation_type.eql?("O")
      #puts "parsing a ucp53"
      ucpmsg=Ucp53Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp53"
      ucpmsg=Ucp53Result.new(arr)
    end
  elsif operation.eql?("54")
    if operation_type.eql?("O")
      #puts "parsing a ucp54"
      ucpmsg=Ucp54Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp54"
      ucpmsg=Ucp54Result.new(arr)
    end
  elsif operation.eql?("55")
    if operation_type.eql?("O")
      #puts "parsing a ucp55"
      ucpmsg=Ucp55Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp55"
      ucpmsg=Ucp55Result.new(arr)
    end
  elsif operation.eql?("56")
    if operation_type.eql?("O")
      #puts "parsing a ucp56"
      ucpmsg=Ucp56Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp56"
      ucpmsg=Ucp56Result.new(arr)
    end
  elsif operation.eql?("57")
    if operation_type.eql?("O")
      #puts "parsing a ucp57"
      ucpmsg=Ucp57Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp57"
      ucpmsg=Ucp57Result.new(arr)
    end
  elsif operation.eql?("58")
    if operation_type.eql?("O")
      #puts "parsing a ucp58"
      ucpmsg=Ucp58Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp58"
      ucpmsg=Ucp58Result.new(arr)
    end
  elsif operation.eql?("60")
    if operation_type.eql?("O")
      #puts "parsing a ucp60"
      ucpmsg=Ucp60Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp60"
      ucpmsg=Ucp60Result.new(arr)
    end
  elsif operation.eql?("61")
    if operation_type.eql?("O")
      #puts "parsing a ucp61"
      ucpmsg=Ucp61Operation.new(arr)
    elsif operation_type.eql?("R")
      #puts "parsing a result of a  ucp61"
      ucpmsg=Ucp61Result.new(arr)
    end
  end

  #ucpmsg.trn=trn
  return ucpmsg
end

.str2ucs2(str, max_bytes = nil) ⇒ Object

convert a given UTF-8 string to “UCS-2” returns an object representing the UCS-2 packed message



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/ucp/util/ucp.rb', line 292

def self.str2ucs2(str,max_bytes=nil)
  hexstr=""
  str=Iconv.iconv("utf-16be", "utf-8", str).first
  i=0
  str.each_byte{ |c|
    hexstr+=UCP.int2hex(c)
    i+=1
    if (!max_bytes.nil?) && (i == max_bytes)
      break
    end
  }

  return Ucs2PackedMsg.new(hexstr,UCP.utf8_substr(str,0,i-1),i/2,i)
end

.utf8_substr(str, idxs, idxe = nil) ⇒ Object

a dirty UTF-8 substring implementation returns a substring of an UTF-8 encoded string, given the string, start end end characters



244
245
246
247
248
249
250
251
252
253
# File 'lib/ucp/util/ucp.rb', line 244

def self.utf8_substr(str,idxs,idxe=nil)
  s=""
  i=0
  idxe=str.jlength-1 if idxe.nil?
  str.each_char{ |c|
    s+=c if i>=idxs and i<=idxe
    i+=1
  }
  return s
end