encode_bits Subroutine

private pure subroutine encode_bits(bits, padd, code)

Encode a bits stream (must be multiple of 24 bits) into base64 charcaters code (of length multiple of 4).

The bits stream are encoded in chunks of 24 bits as the following example (in little endian order)

 +--first octet--+-second octet--+--third octet--+
 |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
 +-----------+---+-------+-------+---+-----------+
 |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|
 +--1.index--+--2.index--+--3.index--+--4.index--+

Arguments

Type IntentOptional AttributesName
integer(kind=I1P), intent(in) :: bits(1:)

Bits to be encoded.

integer(kind=I4P), intent(in) :: padd

Number of padding characters ('=').

character(len=*), intent(out) :: code

Characters code.

Called By

proc~~encode_bits~~CalledByGraph proc~encode_bits encode_bits proc~b64_encode_r16 b64_encode_R16 proc~b64_encode_r16->proc~encode_bits proc~b64_encode_r4 b64_encode_R4 proc~b64_encode_r4->proc~encode_bits proc~b64_encode_r4_a b64_encode_R4_a proc~b64_encode_r4_a->proc~encode_bits proc~b64_encode_i8_a b64_encode_I8_a proc~b64_encode_i8_a->proc~encode_bits proc~b64_encode_i1 b64_encode_I1 proc~b64_encode_i1->proc~encode_bits proc~b64_encode_string_a b64_encode_string_a proc~b64_encode_string_a->proc~encode_bits proc~b64_encode_i2 b64_encode_I2 proc~b64_encode_i2->proc~encode_bits proc~b64_encode_string b64_encode_string proc~b64_encode_string->proc~encode_bits proc~b64_encode_i1_a b64_encode_I1_a proc~b64_encode_i1_a->proc~encode_bits proc~b64_encode_r8_a b64_encode_R8_a proc~b64_encode_r8_a->proc~encode_bits proc~b64_encode_i8 b64_encode_I8 proc~b64_encode_i8->proc~encode_bits proc~b64_encode_i2_a b64_encode_I2_a proc~b64_encode_i2_a->proc~encode_bits proc~b64_encode_i4_a b64_encode_I4_a proc~b64_encode_i4_a->proc~encode_bits proc~b64_encode_r8 b64_encode_R8 proc~b64_encode_r8->proc~encode_bits proc~b64_encode_i4 b64_encode_I4 proc~b64_encode_i4->proc~encode_bits proc~b64_encode_r16_a b64_encode_R16_a proc~b64_encode_r16_a->proc~encode_bits proc~b64_encode_up b64_encode_up proc~b64_encode_up->proc~b64_encode_r4 proc~b64_encode_up->proc~b64_encode_i1 proc~b64_encode_up->proc~b64_encode_i2 proc~b64_encode_up->proc~b64_encode_string proc~b64_encode_up->proc~b64_encode_i8 proc~b64_encode_up->proc~b64_encode_r8 proc~b64_encode_up->proc~b64_encode_i4 interface~b64_encode b64_encode interface~b64_encode->proc~b64_encode_r4 interface~b64_encode->proc~b64_encode_r4_a interface~b64_encode->proc~b64_encode_i8_a interface~b64_encode->proc~b64_encode_i1 interface~b64_encode->proc~b64_encode_string_a interface~b64_encode->proc~b64_encode_i2 interface~b64_encode->proc~b64_encode_string interface~b64_encode->proc~b64_encode_i1_a interface~b64_encode->proc~b64_encode_r8_a interface~b64_encode->proc~b64_encode_i8 interface~b64_encode->proc~b64_encode_i2_a interface~b64_encode->proc~b64_encode_i4_a interface~b64_encode->proc~b64_encode_r8 interface~b64_encode->proc~b64_encode_i4 program~volatile_doctest~11 volatile_doctest program~volatile_doctest~11->interface~b64_encode program~volatile_doctest~34 volatile_doctest program~volatile_doctest~34->interface~b64_encode program~volatile_doctest~4 volatile_doctest program~volatile_doctest~4->interface~b64_encode program~volatile_doctest~10 volatile_doctest program~volatile_doctest~10->interface~b64_encode program~volatile_doctest~37 volatile_doctest program~volatile_doctest~37->interface~b64_encode program~volatile_doctest~5 volatile_doctest program~volatile_doctest~5->interface~b64_encode program~volatile_doctest~13 volatile_doctest program~volatile_doctest~13->interface~b64_encode program~volatile_doctest~9 volatile_doctest program~volatile_doctest~9->interface~b64_encode program~volatile_doctest~2 volatile_doctest program~volatile_doctest~2->interface~b64_encode program~volatile_doctest~35 volatile_doctest program~volatile_doctest~35->interface~b64_encode program~volatile_doctest~6 volatile_doctest program~volatile_doctest~6->interface~b64_encode program~volatile_doctest~14 volatile_doctest program~volatile_doctest~14->interface~b64_encode program~volatile_doctest~3 volatile_doctest program~volatile_doctest~3->interface~b64_encode program~volatile_doctest~36 volatile_doctest program~volatile_doctest~36->interface~b64_encode program~volatile_doctest~7 volatile_doctest program~volatile_doctest~7->interface~b64_encode program~volatile_doctest~8 volatile_doctest program~volatile_doctest~8->interface~b64_encode proc~b64_encode_up_a b64_encode_up_a proc~b64_encode_up_a->proc~b64_encode_r4_a proc~b64_encode_up_a->proc~b64_encode_i8_a proc~b64_encode_up_a->proc~b64_encode_string_a proc~b64_encode_up_a->proc~b64_encode_i1_a proc~b64_encode_up_a->proc~b64_encode_r8_a proc~b64_encode_up_a->proc~b64_encode_i2_a proc~b64_encode_up_a->proc~b64_encode_i4_a interface~b64_encode_up b64_encode_up interface~b64_encode_up->proc~b64_encode_up_a interface~b64_encode_up->proc~b64_encode_up_a interface~b64_encode_up->interface~b64_encode_up
Help

Source Code


Source Code

   pure subroutine encode_bits(bits, padd, code)
   !< Encode a bits stream (must be multiple of 24 bits) into base64 charcaters code (of length multiple of 4).
   !<
   !< The bits stream are encoded in chunks of 24 bits as the following example (in little endian order)
   !<```
   !< +--first octet--+-second octet--+--third octet--+
   !< |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
   !< +-----------+---+-------+-------+---+-----------+
   !< |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|
   !< +--1.index--+--2.index--+--3.index--+--4.index--+
   !<```
   !< @note The 4 indexes are stored into 4 elements 8 bits array, thus 2 bits of each array element are not used.
   !<
   !< @note The number of paddings must be computed outside this procedure, into the calling scope.
   !<
   !< @warning This procedure is the backend of encoding, thus it must be never called outside the module.
   integer(I1P), intent(in)  :: bits(1:)  !< Bits to be encoded.
   integer(I4P), intent(in)  :: padd      !< Number of padding characters ('=').
   character(*), intent(out) :: code      !< Characters code.
   integer(I1P)              :: sixb(1:4) !< 6 bits slices (stored into 8 bits integer) of 24 bits input.
   integer(I8P)              :: c         !< Counter.
   integer(I8P)              :: e         !< Counter.
   integer(I8P)              :: Nb        !< Length of bits array.

   Nb=size(bits,dim=1,kind=I8P)
   c = 1_I8P
   do e=1_I8P,Nb,3_I8P ! loop over array elements: 3 bytes (24 bits) scanning
      sixb = 0_I1P
         call mvbits(bits(e  ),2,6,sixb(1),0)
         call mvbits(bits(e  ),0,2,sixb(2),4)
      if (e+1<=Nb) then
         call mvbits(bits(e+1),4,4,sixb(2),0)
         call mvbits(bits(e+1),0,4,sixb(3),2)
      endif
      if (e+2<=Nb) then
         call mvbits(bits(e+2),6,2,sixb(3),0)
         call mvbits(bits(e+2),0,6,sixb(4),0)
      endif
      sixb = sixb + 1_I1P
      code(c  :c  ) = base64(sixb(1):sixb(1))
      code(c+1:c+1) = base64(sixb(2):sixb(2))
      code(c+2:c+2) = base64(sixb(3):sixb(3))
      code(c+3:c+3) = base64(sixb(4):sixb(4))
      c = c + 4_I8P
   enddo
   if (padd>0) code(len(code)-padd+1:)=repeat('=',padd)
   endsubroutine encode_bits