appendBits() публичный Метод

Appends the least-significant bits, from value, in order from most-significant to least-significant. For example, appending 6 bits from 0x000001E will append the bits 0, 1, 1, 1, 1, 0 in that order.
public appendBits ( int value, int numBits ) : void
value int containing bits to append
numBits int bits from value to append
Результат void
Пример #1
0
 public override void appendTo(BitArray bitArray, byte[] text)
 {
    for (int i = 0; i < binaryShiftByteCount; i++)
    {
       if (i == 0 || (i == 31 && binaryShiftByteCount <= 62))
       {
          // We need a header before the first character, and before
          // character 31 when the total byte code is <= 62
          bitArray.appendBits(31, 5);  // BINARY_SHIFT
          if (binaryShiftByteCount > 62)
          {
             bitArray.appendBits(binaryShiftByteCount - 31, 16);
          }
          else if (i == 0)
          {
             // 1 <= binaryShiftByteCode <= 62
             bitArray.appendBits(Math.Min(binaryShiftByteCount, (short) 31), 5);
          }
          else
          {
             // 32 <= binaryShiftCount <= 62 and i == 31
             bitArray.appendBits(binaryShiftByteCount - 31, 5);
          }
       }
       bitArray.appendBits(text[binaryShiftStart + i], 8);
    }
 }
Пример #2
0
        public static ByteMatrix CreateRawQR(byte[] rawData, ErrorCorrectionLevel errorCorrectionLevel)
        {
            int versionNumber = GetSmallestVersion(rawData.Length, errorCorrectionLevel);
            ZXing.QrCode.Internal.Version version = ZXing.QrCode.Internal.Version.getVersionForNumber(versionNumber);

            BitArray dataBits = new BitArray();
            foreach (byte b in rawData)
                dataBits.appendBits(b, 8);

            ZXing.QrCode.Internal.Version.ECBlocks ecBlocks = version.getECBlocksForLevel(errorCorrectionLevel);
            int bytesLength = version.TotalCodewords - ecBlocks.TotalECCodewords;
            terminateBits(bytesLength, dataBits);

            BitArray resultBits = interleaveWithECBytes(dataBits, version.TotalCodewords, bytesLength, ecBlocks.NumBlocks);

            ByteMatrix matrix = new ByteMatrix(version.DimensionForVersion, version.DimensionForVersion);
            int maskPattern = chooseMaskPattern(resultBits, errorCorrectionLevel, version, matrix);

            MatrixUtil.buildMatrix(resultBits, errorCorrectionLevel, version, maskPattern, matrix);
            return matrix;
        }
Пример #3
0
 internal static void append8BitBytes(String content, BitArray bits, String encoding)
 {
     byte[] bytes;
      try
      {
     bytes = Encoding.UTF8.GetBytes(content);
      }
      catch (Exception uee)
      {
     throw new WriterException(uee.Message, uee);
      }
      foreach (byte b in bytes)
      {
     bits.appendBits(b, 8);
      }
 }
Пример #4
0
      internal static void appendKanjiBytes(String content, BitArray bits)
      {
         byte[] bytes;
         try
         {
            bytes = Encoding.GetEncoding("Shift_JIS").GetBytes(content);
         }
         catch (Exception uee)
         {
            throw new WriterException(uee.Message, uee);
         }
         int length = bytes.Length;
         for (int i = 0; i < length; i += 2)
         {
            int byte1 = bytes[i] & 0xFF;
            int byte2 = bytes[i + 1] & 0xFF;
            int code = (byte1 << 8) | byte2;
            int subtracted = -1;
            if (code >= 0x8140 && code <= 0x9ffc)
            {

               subtracted = code - 0x8140;
            }
            else if (code >= 0xe040 && code <= 0xebbf)
            {
               subtracted = code - 0xc140;
            }
            if (subtracted == -1)
            {

               throw new WriterException("Invalid byte sequence");
            }
            int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
            bits.appendBits(encoded, 13);
         }
      }
Пример #5
0
      internal static void appendAlphanumericBytes(String content, BitArray bits)
      {
         int length = content.Length;

         int i = 0;
         while (i < length)
         {
            int code1 = getAlphanumericCode(content[i]);
            if (code1 == -1)
            {
               throw new WriterException();
            }
            if (i + 1 < length)
            {
               int code2 = getAlphanumericCode(content[i + 1]);
               if (code2 == -1)
               {
                  throw new WriterException();
               }
               // Encode two alphanumeric letters in 11 bits.
               bits.appendBits(code1 * 45 + code2, 11);
               i += 2;
            }
            else
            {
               // Encode one alphanumeric letter in six bits.
               bits.appendBits(code1, 6);
               i++;
            }
         }
      }
Пример #6
0
 /// <summary>
 /// Append length info. On success, store the result in "bits".
 /// </summary>
 /// <param name="numLetters">The num letters.</param>
 /// <param name="version">The version.</param>
 /// <param name="mode">The mode.</param>
 /// <param name="bits">The bits.</param>
 internal static void appendLengthInfo(int numLetters, Version version, Mode mode, BitArray bits)
 {
    int numBits = mode.getCharacterCountBits(version);
    if (numLetters >= (1 << numBits))
    {
       throw new WriterException(numLetters + " is bigger than " + ((1 << numBits) - 1));
    }
    bits.appendBits(numLetters, numBits);
 }
Пример #7
0
      /// <summary>
      /// Interleave "bits" with corresponding error correction bytes. On success, store the result in
      /// "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details.
      /// </summary>
      /// <param name="bits">The bits.</param>
      /// <param name="numTotalBytes">The num total bytes.</param>
      /// <param name="numDataBytes">The num data bytes.</param>
      /// <param name="numRSBlocks">The num RS blocks.</param>
      /// <returns></returns>
      internal static BitArray interleaveWithECBytes(BitArray bits,
                                             int numTotalBytes,
                                             int numDataBytes,
                                             int numRSBlocks)
      {
         // "bits" must have "getNumDataBytes" bytes of data.
         if (bits.SizeInBytes != numDataBytes)
         {

            throw new WriterException("Number of bits and data bytes does not match");
         }

         // Step 1.  Divide data bytes into blocks and generate error correction bytes for them. We'll
         // store the divided data bytes blocks and error correction bytes blocks into "blocks".
         int dataBytesOffset = 0;
         int maxNumDataBytes = 0;
         int maxNumEcBytes = 0;

         // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number.
         var blocks = new List<BlockPair>(numRSBlocks);

         for (int i = 0; i < numRSBlocks; ++i)
         {

            int[] numDataBytesInBlock = new int[1];
            int[] numEcBytesInBlock = new int[1];
            getNumDataBytesAndNumECBytesForBlockID(
                numTotalBytes, numDataBytes, numRSBlocks, i,
                numDataBytesInBlock, numEcBytesInBlock);

            int size = numDataBytesInBlock[0];
            byte[] dataBytes = new byte[size];
            bits.toBytes(8 * dataBytesOffset, dataBytes, 0, size);
            byte[] ecBytes = generateECBytes(dataBytes, numEcBytesInBlock[0]);
            blocks.Add(new BlockPair(dataBytes, ecBytes));

            maxNumDataBytes = Math.Max(maxNumDataBytes, size);
            maxNumEcBytes = Math.Max(maxNumEcBytes, ecBytes.Length);
            dataBytesOffset += numDataBytesInBlock[0];
         }
         if (numDataBytes != dataBytesOffset)
         {

            throw new WriterException("Data bytes does not match offset");
         }

         BitArray result = new BitArray();

         // First, place data blocks.
         for (int i = 0; i < maxNumDataBytes; ++i)
         {
            foreach (BlockPair block in blocks)
            {
               byte[] dataBytes = block.DataBytes;
               if (i < dataBytes.Length)
               {
                  result.appendBits(dataBytes[i], 8);
               }
            }
         }
         // Then, place error correction blocks.
         for (int i = 0; i < maxNumEcBytes; ++i)
         {
            foreach (BlockPair block in blocks)
            {
               byte[] ecBytes = block.ErrorCorrectionBytes;
               if (i < ecBytes.Length)
               {
                  result.appendBits(ecBytes[i], 8);
               }
            }
         }
         if (numTotalBytes != result.SizeInBytes)
         {  // Should be same.
            throw new WriterException("Interleaving error: " + numTotalBytes + " and " +
                result.SizeInBytes + " differ.");
         }

         return result;
      }
Пример #8
0
      /// <summary>
      /// Make bit vector of version information. On success, store the result in "bits" and return true.
      /// See 8.10 of JISX0510:2004 (p.45) for details.
      /// </summary>
      /// <param name="version">The version.</param>
      /// <param name="bits">The bits.</param>
      public static void makeVersionInfoBits(Version version, BitArray bits)
      {
         bits.appendBits(version.VersionNumber, 6);
         int bchCode = calculateBCHCode(version.VersionNumber, VERSION_INFO_POLY);
         bits.appendBits(bchCode, 12);

         if (bits.Size != 18)
         {
            // Just in case.
            throw new WriterException("should not happen but we got: " + bits.Size);
         }
      }
Пример #9
0
 public void testInterleaveWithECBytes()
 {
    byte[] dataBytes = {32, 65, 205, 69, 41, 220, 46, 128, 236};
    var @in = new BitArray();
    foreach (byte dataByte in dataBytes)
    {
       @in.appendBits(dataByte, 8);
    }
    var @out = Encoder.interleaveWithECBytes(@in, 26, 9, 1);
    byte[] expected =
       {
          // Data bytes.
          32, 65, 205, 69, 41, 220, 46, 128, 236,
          // Error correction bytes.
          42, 159, 74, 221, 244, 169, 239, 150, 138, 70,
          237, 85, 224, 96, 74, 219, 61,
       };
    Assert.AreEqual(expected.Length, @out.SizeInBytes);
    var outArray = new byte[expected.Length];
    @out.toBytes(0, outArray, 0, expected.Length);
    // Can't use Arrays.equals(), because outArray may be longer than out.sizeInBytes()
    for (int x = 0; x < expected.Length; x++)
    {
       Assert.AreEqual(expected[x], outArray[x]);
    }
    // Numbers are from http://www.swetake.com/qr/qr8.html
    dataBytes = new byte[]
       {
          67, 70, 22, 38, 54, 70, 86, 102, 118, 134, 150, 166, 182,
          198, 214, 230, 247, 7, 23, 39, 55, 71, 87, 103, 119, 135,
          151, 166, 22, 38, 54, 70, 86, 102, 118, 134, 150, 166,
          182, 198, 214, 230, 247, 7, 23, 39, 55, 71, 87, 103, 119,
          135, 151, 160, 236, 17, 236, 17, 236, 17, 236,
          17
       };
    @in = new BitArray();
    foreach (byte dataByte in dataBytes)
    {
       @in.appendBits(dataByte, 8);
    }
    @out = Encoder.interleaveWithECBytes(@in, 134, 62, 4);
    expected = new byte[]
       {
          // Data bytes.
          67, 230, 54, 55, 70, 247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54, 39,
          118, 119, 70, 55, 134, 135, 86, 71, 150, 151, 102, 87, 166,
          160, 118, 103, 182, 236, 134, 119, 198, 17, 150,
          135, 214, 236, 166, 151, 230, 17, 182,
          166, 247, 236, 198, 22, 7, 17, 214, 38, 23, 236, 39,
          17,
          // Error correction bytes.
          175, 155, 245, 236, 80, 146, 56, 74, 155, 165,
          133, 142, 64, 183, 132, 13, 178, 54, 132, 108, 45,
          113, 53, 50, 214, 98, 193, 152, 233, 147, 50, 71, 65,
          190, 82, 51, 209, 199, 171, 54, 12, 112, 57, 113, 155, 117,
          211, 164, 117, 30, 158, 225, 31, 190, 242, 38,
          140, 61, 179, 154, 214, 138, 147, 87, 27, 96, 77, 47,
          187, 49, 156, 214,
       };
    Assert.AreEqual(expected.Length, @out.SizeInBytes);
    outArray = new byte[expected.Length];
    @out.toBytes(0, outArray, 0, expected.Length);
    for (int x = 0; x < expected.Length; x++)
    {
       Assert.AreEqual(expected[x], outArray[x]);
    }
 }
Пример #10
0
 public void testToString()
 {
    BitArray v = new BitArray();
    v.appendBits(0xdead, 16);  // 1101 1110 1010 1101
    Assert.AreEqual(" XX.XXXX. X.X.XX.X", v.ToString());
 }
Пример #11
0
      public void testAt()
      {
         BitArray v = new BitArray();
         v.appendBits(0xdead, 16);  // 1101 1110 1010 1101
         Assert.IsTrue(v[0]);
         Assert.IsTrue(v[1]);
         Assert.IsFalse(v[2]);
         Assert.IsTrue(v[3]);

         Assert.IsTrue(v[4]);
         Assert.IsTrue(v[5]);
         Assert.IsTrue(v[6]);
         Assert.IsFalse(v[7]);

         Assert.IsTrue(v[8]);
         Assert.IsFalse(v[9]);
         Assert.IsTrue(v[10]);
         Assert.IsFalse(v[11]);

         Assert.IsTrue(v[12]);
         Assert.IsTrue(v[13]);
         Assert.IsFalse(v[14]);
         Assert.IsTrue(v[15]);
      }
Пример #12
0
 public void testXOR2()
 {
    var v1 = new BitArray();
    v1.appendBits(0x2a, 7); // 010 1010
    var v2 = new BitArray();
    v2.appendBits(0x55, 7); // 101 0101
    v1.xor(v2);
    Assert.AreEqual(0xfe000000L, getUnsignedInt(v1, 0)); // 1111 1110
 }
Пример #13
0
 public void testXOR()
 {
    var v1 = new BitArray();
    v1.appendBits(0x5555aaaa, 32);
    var v2 = new BitArray();
    v2.appendBits(-1431677611, 32); // 0xaaaa5555
    v1.xor(v2);
    Assert.AreEqual(0xffffffffL, getUnsignedInt(v1, 0));
 }
Пример #14
0
 public void testAppendBitVector()
 {
    BitArray v1 = new BitArray();
    v1.appendBits(0xbe, 8);
    BitArray v2 = new BitArray();
    v2.appendBits(0xef, 8);
    v1.appendBitArray(v2);
    // beef = 1011 1110 1110 1111
    Assert.AreEqual(" X.XXXXX. XXX.XXXX", v1.ToString());
 }
Пример #15
0
 public void testNumBytes()
 {
    BitArray v = new BitArray();
    Assert.AreEqual(0, v.SizeInBytes);
    v.appendBit(false);
    // 1 bit was added in the vector, so 1 byte should be consumed.
    Assert.AreEqual(1, v.SizeInBytes);
    v.appendBits(0, 7);
    Assert.AreEqual(1, v.SizeInBytes);
    v.appendBits(0, 8);
    Assert.AreEqual(2, v.SizeInBytes);
    v.appendBits(0, 1);
    // We now have 17 bits, so 3 bytes should be consumed.
    Assert.AreEqual(3, v.SizeInBytes);
 }
Пример #16
0
        public static byte[] Encode(byte[] Content, int Width, int Height)
        {
            var ecLevel = ErrorCorrectionLevel.L;
            var bits = new BitArray();

            var mode = Mode.BYTE;
            var bitsNeeded = 4 +
                mode.getCharacterCountBits(ZXing.QrCode.Internal.Version.getVersionForNumber(1)) +
                (Content.Length * 8);

            var version = ChooseVersion(bitsNeeded, out ecLevel);
            var ecBlocks = version.getECBlocksForLevel(ecLevel);
            var totalByteCapacity = version.TotalCodewords - ecBlocks.TotalECCodewords;
            var totalBitCapacity = totalByteCapacity << 3;

            // Write the mode marker (BYTE)
            bits.appendBits(mode.Bits, 4);

            // Write the number of bytes
            bits.appendBits(Content.Length, mode.getCharacterCountBits(version));

            // Write the bytes
            for (int i = 0; i < Content.Length; i++)
            {
                bits.appendBits(Content[i], 8);
            }

            // Terminate the bit stream
            //  - Write Termination Mode if space
            for (int i = 0; i < 4 && bits.Size < totalBitCapacity; ++i)
            {
                bits.appendBit(false);
            }

            // Add 8-bit alignment padding
            var bitsInLastByte = bits.Size & 0x07;
            if (bitsInLastByte > 0)
            {
                for (int i = bitsInLastByte; i < 8; i++)
                {
                    bits.appendBit(false);
                }
            }

            // Fill remain space with padding patterns
            var paddingBytes = totalByteCapacity - bits.SizeInBytes;
            for (int i = 0; i < paddingBytes; ++i)
            {
                bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8);
            }

            // Interleave data bits with error correction code.
            var finalBits = interleaveWithECBytes(bits, version.TotalCodewords, totalByteCapacity, ecBlocks.NumBlocks);

            //  Choose the mask pattern and set to "qrCode".
            int dimension = version.DimensionForVersion;
            ByteMatrix matrix = new ByteMatrix(dimension, dimension);
            int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);

            // Build the matrix and set it to "qrCode".
            MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);

            // Render matrix to bytes
            return scaleMatrix(matrix.Array, Width, Height);
        }
Пример #17
0
 public void testTerminateBits()
 {
    var v = new BitArray();
    Encoder.terminateBits(0, v);
    Assert.AreEqual("", v.ToString());
    v = new BitArray();
    Encoder.terminateBits(1, v);
    Assert.AreEqual(" ........", v.ToString());
    v = new BitArray();
    v.appendBits(0, 3); // Append 000
    Encoder.terminateBits(1, v);
    Assert.AreEqual(" ........", v.ToString());
    v = new BitArray();
    v.appendBits(0, 5); // Append 00000
    Encoder.terminateBits(1, v);
    Assert.AreEqual(" ........", v.ToString());
    v = new BitArray();
    v.appendBits(0, 8); // Append 00000000
    Encoder.terminateBits(1, v);
    Assert.AreEqual(" ........", v.ToString());
    v = new BitArray();
    Encoder.terminateBits(2, v);
    Assert.AreEqual(" ........ XXX.XX..", v.ToString());
    v = new BitArray();
    v.appendBits(0, 1); // Append 0
    Encoder.terminateBits(3, v);
    Assert.AreEqual(" ........ XXX.XX.. ...X...X", v.ToString());
 }
Пример #18
0
 public void testAppendBits()
 {
    var v = new BitArray();
    v.appendBits(0x1, 1);
    Assert.AreEqual(1, v.Size);
    Assert.AreEqual(0x80000000L, getUnsignedInt(v, 0));
    v = new BitArray();
    v.appendBits(0xff, 8);
    Assert.AreEqual(8, v.Size);
    Assert.AreEqual(0xff000000L, getUnsignedInt(v, 0));
    v = new BitArray();
    v.appendBits(0xff7, 12);
    Assert.AreEqual(12, v.Size);
    Assert.AreEqual(0xff700000L, getUnsignedInt(v, 0));
 }
Пример #19
0
      /// <summary>
      /// Make bit vector of type information. On success, store the result in "bits" and return true.
      /// Encode error correction level and mask pattern. See 8.9 of
      /// JISX0510:2004 (p.45) for details.
      /// </summary>
      /// <param name="ecLevel">The ec level.</param>
      /// <param name="maskPattern">The mask pattern.</param>
      /// <param name="bits">The bits.</param>
      public static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
      {
         if (!QRCode.isValidMaskPattern(maskPattern))
         {
            throw new WriterException("Invalid mask pattern");
         }
         int typeInfo = (ecLevel.Bits << 3) | maskPattern;
         bits.appendBits(typeInfo, 5);

         int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
         bits.appendBits(bchCode, 10);

         BitArray maskBits = new BitArray();
         maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
         bits.xor(maskBits);

         if (bits.Size != 15)
         {
            // Just in case.
            throw new WriterException("should not happen but we got: " + bits.Size);
         }
      }
Пример #20
0
 public void testBuildMatrix()
 {
    // From http://www.swetake.com/qr/qr7.html
    String expected =
      " 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\n" +
      " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
      " 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\n" +
      " 1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\n" +
      " 1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\n" +
      " 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\n" +
      " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
      " 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\n" +
      " 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\n" +
      " 1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\n" +
      " 1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\n" +
      " 1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\n" +
      " 0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\n" +
      " 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\n" +
      " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\n" +
      " 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\n" +
      " 1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\n" +
      " 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\n" +
      " 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n" +
      " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n" +
      " 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n";
    int[] bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236,
   42, 159, 74, 221, 244, 169, 239, 150, 138,
   70, 237, 85, 224, 96, 74, 219 , 61};
    BitArray bits = new BitArray();
    foreach (char c in bytes)
    {
       bits.appendBits(c, 8);
    }
    ByteMatrix matrix = new ByteMatrix(21, 21);
    MatrixUtil.buildMatrix(bits,
                           ErrorCorrectionLevel.H,
                           Version.getVersionForNumber(1),  // Version 1
                           3,  // Mask pattern 3
                           matrix);
    Assert.AreEqual(expected, matrix.ToString());
 }
Пример #21
0
 /// <summary>
 /// Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
 /// </summary>
 /// <param name="numDataBytes">The num data bytes.</param>
 /// <param name="bits">The bits.</param>
 internal static void terminateBits(int numDataBytes, BitArray bits)
 {
    int capacity = numDataBytes << 3;
    if (bits.Size > capacity)
    {
       throw new WriterException("data bits cannot fit in the QR Code" + bits.Size + " > " +
           capacity);
    }
    for (int i = 0; i < 4 && bits.Size < capacity; ++i)
    {
       bits.appendBit(false);
    }
    // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
    // If the last byte isn't 8-bit aligned, we'll add padding bits.
    int numBitsInLastByte = bits.Size & 0x07;
    if (numBitsInLastByte > 0)
    {
       for (int i = numBitsInLastByte; i < 8; i++)
       {
          bits.appendBit(false);
       }
    }
    // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
    int numPaddingBytes = numDataBytes - bits.SizeInBytes;
    for (int i = 0; i < numPaddingBytes; ++i)
    {
       bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8);
    }
    if (bits.Size != capacity)
    {
       throw new WriterException("Bits size does not equal capacity");
    }
 }
Пример #22
0
 public override void appendTo(BitArray bitArray, byte[] text)
 {
    bitArray.appendBits(value, bitCount);
 }
Пример #23
0
 /// <summary>
 /// Append mode info. On success, store the result in "bits".
 /// </summary>
 /// <param name="mode">The mode.</param>
 /// <param name="bits">The bits.</param>
 internal static void appendModeInfo(Mode mode, BitArray bits)
 {
    bits.appendBits(mode.Bits, 4);
 }
Пример #24
0
 internal static BitArray generateModeMessage(bool compact, int layers, int messageSizeInWords)
 {
    var modeMessage = new BitArray();
    if (compact)
    {
       modeMessage.appendBits(layers - 1, 2);
       modeMessage.appendBits(messageSizeInWords - 1, 6);
       modeMessage = generateCheckWords(modeMessage, 28, 4);
    }
    else
    {
       modeMessage.appendBits(layers - 1, 5);
       modeMessage.appendBits(messageSizeInWords - 1, 11);
       modeMessage = generateCheckWords(modeMessage, 40, 4);
    }
    return modeMessage;
 }
Пример #25
0
      internal static void appendNumericBytes(String content, BitArray bits)
      {
         int length = content.Length;

         int i = 0;
         while (i < length)
         {
            int num1 = content[i] - '0';
            if (i + 2 < length)
            {
               // Encode three numeric letters in ten bits.
               int num2 = content[i + 1] - '0';
               int num3 = content[i + 2] - '0';
               bits.appendBits(num1 * 100 + num2 * 10 + num3, 10);
               i += 3;
            }
            else if (i + 1 < length)
            {
               // Encode two numeric letters in seven bits.
               int num2 = content[i + 1] - '0';
               bits.appendBits(num1 * 10 + num2, 7);
               i += 2;
            }
            else
            {
               // Encode one numeric letter in four bits.
               bits.appendBits(num1, 4);
               i++;
            }
         }
      }
Пример #26
0
      private static BitArray generateCheckWords(BitArray bitArray, int totalBits, int wordSize)
      {
         if (bitArray.Size % wordSize != 0)
            throw new InvalidOperationException("size of bit array is not a multiple of the word size");

         // bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
         int messageSizeInWords = bitArray.Size / wordSize;

         var rs = new ReedSolomonEncoder(getGF(wordSize));
         var totalWords = totalBits / wordSize;
         var messageWords = bitsToWords(bitArray, wordSize, totalWords);
         rs.encode(messageWords, totalWords - messageSizeInWords);

         var startPad = totalBits % wordSize;
         var messageBits = new BitArray();
         messageBits.appendBits(0, startPad);
         foreach (var messageWord in messageWords)
         {
            messageBits.appendBits(messageWord, wordSize);
         }
         return messageBits;
      }
Пример #27
0
      internal static void append8BitBytes(String content, BitArray bits, String encoding)
      {
         byte[] bytes;
         try
         {
            bytes = Encoding.GetEncoding(encoding).GetBytes(content);
         }
#if WindowsCE
         catch (PlatformNotSupportedException)
         {
            try
            {
               // WindowsCE doesn't support all encodings. But it is device depended.
               // So we try here the some different ones
               if (encoding == "ISO-8859-1")
               {
                  bytes = Encoding.GetEncoding(1252).GetBytes(content);
               }
               else
               {
                  bytes = Encoding.GetEncoding("UTF-8").GetBytes(content);
               }
            }
            catch (Exception uee)
            {
               throw new WriterException(uee.Message, uee);
            }
         }
#endif
         catch (Exception uee)
         {
            throw new WriterException(uee.Message, uee);
         }
         foreach (byte b in bytes)
         {
            bits.appendBits(b, 8);
         }
      }
Пример #28
0
      internal static BitArray stuffBits(BitArray bits, int wordSize)
      {
         var @out = new BitArray();

         int n = bits.Size;
         int mask = (1 << wordSize) - 2;
         for (int i = 0; i < n; i += wordSize)
         {
            int word = 0;
            for (int j = 0; j < wordSize; j++)
            {
               if (i + j >= n || bits[i + j])
               {
                  word |= 1 << (wordSize - 1 - j);
               }
            }
            if ((word & mask) == mask)
            {
               @out.appendBits(word & mask, wordSize);
               i--;
            }
            else if ((word & mask) == 0)
            {
               @out.appendBits(word | 1, wordSize);
               i--;
            }
            else
            {
               @out.appendBits(word, wordSize);
            }
         }

         return @out;
      }
Пример #29
0
      private static void appendECI(CharacterSetECI eci, BitArray bits)
      {
         bits.appendBits(Mode.ECI.Bits, 4);

         // This is correct for values up to 127, which is all we need now.
         bits.appendBits(eci.Value, 8);
      }
Пример #30
0
        private static BitArray interleaveWithECBytes(BitArray bits, int numTotalBytes, int numDataBytes, int numRSBlocks)
        {
            // Step 1.  Divide data bytes into blocks and generate error correction bytes for them. We'll
            // store the divided data bytes blocks and error correction bytes blocks into "blocks".
            int dataBytesOffset = 0;
            int maxNumDataBytes = 0;
            int maxNumEcBytes = 0;

            // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number.
            var blocks = new List<Tuple<byte[], byte[]>>(numRSBlocks);

            for (int i = 0; i < numRSBlocks; ++i)
            {

                int numDataBytesInBlock;
                int numEcBytesInBlock;
                getNumDataBytesAndNumECBytesForBlockID(
                    numTotalBytes, numDataBytes, numRSBlocks, i,
                    out numDataBytesInBlock, out numEcBytesInBlock);

                byte[] dataBytes = new byte[numDataBytesInBlock];
                bits.toBytes(8 * dataBytesOffset, dataBytes, 0, numDataBytesInBlock);
                byte[] ecBytes = generateECBytes(dataBytes, numEcBytesInBlock);
                blocks.Add(new Tuple<byte[], byte[]>(dataBytes, ecBytes));

                maxNumDataBytes = Math.Max(maxNumDataBytes, numDataBytesInBlock);
                maxNumEcBytes = Math.Max(maxNumEcBytes, ecBytes.Length);
                dataBytesOffset += numEcBytesInBlock;
            }

            BitArray result = new BitArray();

            // First, place data blocks.
            for (int i = 0; i < maxNumDataBytes; ++i)
            {
                foreach (Tuple<byte[], byte[]> block in blocks)
                {
                    byte[] dataBytes = block.Item1;
                    if (i < dataBytes.Length)
                    {
                        result.appendBits(dataBytes[i], 8);
                    }
                }
            }
            // Then, place error correction blocks.
            for (int i = 0; i < maxNumEcBytes; ++i)
            {
                foreach (Tuple<byte[], byte[]> block in blocks)
                {
                    byte[] ecBytes = block.Item2;
                    if (i < ecBytes.Length)
                    {
                        result.appendBits(ecBytes[i], 8);
                    }
                }
            }

            return result;
        }