Ejemplo n.º 1
0
bool EncodeData(char *lpsSource)
{
	int i, j;
	int nEncodeVersion, ncDataCodeWord, ncTerminater;
	BYTE byPaddingCode = 0xec;
	int nDataCwIndex = 0, ncBlock1, ncBlock2, ncBlockSum; 
	int nBlockNo = 0, ncDataCw1, ncDataCw2;
	int ncRSCw1, ncRSCw2;
  
	int nVersion = 1;
	int bAutoExtent = 1;
	int ncLength =  strlen(lpsSource);;
	
	m_nLevel = 2;
	m_nMaskingNo = 0;

 //»ñÈ¡°æ±¾ºÅ
	nEncodeVersion = GetEncodeVersion(nVersion, lpsSource, ncLength);
	if(nEncodeVersion == 0) return FALSE; 

	
	if (nEncodeVersion <= nVersion) m_nVersion = nVersion;
  else if (bAutoExtent)           m_nVersion = nEncodeVersion; 
	
	
	ncDataCodeWord = QR_VersonInfo[m_nVersion].ncDataCodeWord[m_nLevel];

	ncTerminater = min(4, (ncDataCodeWord * 8) - m_ncDataCodeWordBit);

	if (ncTerminater > 0) m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, 0, ncTerminater);

	for (i = (m_ncDataCodeWordBit + 7) / 8; i < ncDataCodeWord; ++i)
	{
		m_byDataCodeWord[i] = byPaddingCode;

		byPaddingCode = (BYTE)(byPaddingCode == 0xec ? 0x11 : 0xec);
	}

	m_ncAllCodeWord = QR_VersonInfo[m_nVersion].ncAllCodeWord;
	memset(m_byAllCodeWord, 0, m_ncAllCodeWord);

	ncBlock1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[m_nLevel].ncRSBlock;
	ncBlock2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[m_nLevel].ncRSBlock;
	ncBlockSum = ncBlock1 + ncBlock2;

	ncDataCw1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[m_nLevel].ncDataCodeWord;
	ncDataCw2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[m_nLevel].ncDataCodeWord;

	for (i = 0; i < ncBlock1; ++i)
	{
		for (j = 0; j < ncDataCw1; ++j)
		{
			m_byAllCodeWord[(ncBlockSum * j) + nBlockNo] = m_byDataCodeWord[nDataCwIndex++];
		}

		++nBlockNo;
	}

	for (i = 0; i < ncBlock2; ++i)
	{
		for (j = 0; j < ncDataCw2; ++j)
		{
			if (j < ncDataCw1)
			{
				m_byAllCodeWord[(ncBlockSum * j) + nBlockNo] = m_byDataCodeWord[nDataCwIndex++];
			}
			else
			{
				m_byAllCodeWord[(ncBlockSum * ncDataCw1) + i]  = m_byDataCodeWord[nDataCwIndex++];
			}	
		}

		++nBlockNo;
	}

	ncRSCw1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[m_nLevel].ncAllCodeWord - ncDataCw1;
	ncRSCw2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[m_nLevel].ncAllCodeWord - ncDataCw2;

	nDataCwIndex = 0;
	nBlockNo = 0;

	for (i = 0; i < ncBlock1; ++i)
	{
		memset(m_byRSWork, 0, sizeof(m_byRSWork));

		memmove(m_byRSWork, m_byDataCodeWord + nDataCwIndex, ncDataCw1);

		GetRSCodeWord(m_byRSWork, ncDataCw1, ncRSCw1);

		for (j = 0; j < ncRSCw1; ++j)
		{
			m_byAllCodeWord[ncDataCodeWord + (ncBlockSum * j) + nBlockNo] = m_byRSWork[j];
		}

		nDataCwIndex += ncDataCw1;
		++nBlockNo;
	}

	for (i = 0; i < ncBlock2; ++i)
	{
		memset(m_byRSWork, 0, sizeof(m_byRSWork));

		memmove(m_byRSWork, m_byDataCodeWord + nDataCwIndex, ncDataCw2);

		GetRSCodeWord(m_byRSWork, ncDataCw2, ncRSCw2);

		for (j = 0; j < ncRSCw2; ++j)
		{
			m_byAllCodeWord[ncDataCodeWord + (ncBlockSum * j) + nBlockNo] = m_byRSWork[j];
		}

		nDataCwIndex += ncDataCw2;
		++nBlockNo;
	}

	m_nSymbleSize = m_nVersion * 4 + 17;

	FormatModule();

	return TRUE;
}
Ejemplo n.º 2
0
int qr_encode(int level, int version, const char *source, size_t source_len, uint8_t *result)
{
	int i, j;
	const bool auto_extent = 0;
	m_nLevel = level;
	m_nMaskingNo = -1;

	memset(result, 0, QR_MAX_BITDATA);
	// If the data length is not specified, acquired by lstrlen
	size_t ncLength = source_len > 0 ? source_len : strlen(source);

	if (ncLength == 0) {
		return -1; // No data
	}

	// Check version (model number)

	nEncodeVersion = GetEncodeVersion(version, source, ncLength);

	if (nEncodeVersion == 0) {
		return -1; // Over-capacity
	}
	if (version == 0) {
		// Auto Part
		m_nVersion = nEncodeVersion;
	} else {
		if (nEncodeVersion <= version) {
			m_nVersion = version;
		} else {
			if (auto_extent) {
				m_nVersion = nEncodeVersion; // Automatic extended version (model number)
			} else {
				return -1; // Over-capacity
			}
		}
	}

	// Terminator addition code "0000"
	int ncDataCodeWord = QR_VersonInfo[m_nVersion].ncDataCodeWord[level];

	int ncTerminater = (ncDataCodeWord * 8) - m_ncDataCodeWordBit;
	if (ncTerminater < 4) {
		ncTerminater = 4;
	}

	if (ncTerminater > 0) {
		m_ncDataCodeWordBit = SetBitStream(m_ncDataCodeWordBit, 0, ncTerminater);
	}

	// Additional padding code "11101100, 00010001"
	uint8_t byPaddingCode = 0xec;

	for (i = (m_ncDataCodeWordBit + 7) / 8; i < ncDataCodeWord; i++) {
		m_byDataCodeWord[i] = byPaddingCode;

		byPaddingCode = (uint8_t)(byPaddingCode == 0xec ? 0x11 : 0xec);
	}

	// Calculated the total clear area code word
	m_ncAllCodeWord = QR_VersonInfo[m_nVersion].ncAllCodeWord;

	memset(m_byAllCodeWord, 0, sizeof(m_byAllCodeWord));

	int nDataCwIndex = 0;	// Position data processing code word

	// Division number data block
	int ncBlock1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[level].ncRSBlock;

	int ncBlock2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[level].ncRSBlock;

	int ncBlockSum = ncBlock1 + ncBlock2;

	int nBlockNo = 0; // Block number in the process

	// The number of data code words by block
	int ncDataCw1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[level].ncDataCodeWord;

	int ncDataCw2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[level].ncDataCodeWord;

	// Code word interleaving data placement
	for (i = 0; i < ncBlock1; i++) {
		for (j = 0; j < ncDataCw1; j++) {
			m_byAllCodeWord[(ncBlockSum * j) + nBlockNo] = m_byDataCodeWord[nDataCwIndex++];
		}

		nBlockNo++;
	}

	for (i = 0; i < ncBlock2; i++) {
		for (j = 0; j < ncDataCw2; j++) {
			if (j < ncDataCw1) {
				m_byAllCodeWord[(ncBlockSum * j) + nBlockNo] = m_byDataCodeWord[nDataCwIndex++];
			} else {
				// 2 minute fraction block placement event
				m_byAllCodeWord[(ncBlockSum * ncDataCw1) + i] = m_byDataCodeWord[nDataCwIndex++];
			}
		}
		nBlockNo++;
	}

	// RS code words by block number (currently the same number)
	int ncRSCw1 = QR_VersonInfo[m_nVersion].RS_BlockInfo1[level].ncAllCodeWord - ncDataCw1;
	int ncRSCw2 = QR_VersonInfo[m_nVersion].RS_BlockInfo2[level].ncAllCodeWord - ncDataCw2;

	// RS code word is calculated

	nDataCwIndex = 0;
	nBlockNo = 0;

	for (i = 0; i < ncBlock1; i++) {
		memset(m_byRSWork, 0, sizeof(m_byRSWork));
		memmove(m_byRSWork, m_byDataCodeWord + nDataCwIndex, ncDataCw1);
		GetRSCodeWord(m_byRSWork, ncDataCw1, ncRSCw1);
		// RS code word placement
		for (j = 0; j < ncRSCw1; j++) {
			m_byAllCodeWord[ncDataCodeWord + (ncBlockSum * j) + nBlockNo] = m_byRSWork[j];
		}
		nDataCwIndex += ncDataCw1;
		nBlockNo++;
	}

	for (i = 0; i < ncBlock2; i++) {
		memset(m_byRSWork, 0, sizeof(m_byRSWork));
		memmove(m_byRSWork, m_byDataCodeWord + nDataCwIndex, ncDataCw2);
		GetRSCodeWord(m_byRSWork, ncDataCw2, ncRSCw2);
		// RS code word placement
		for (j = 0; j < ncRSCw2; j++) {
			m_byAllCodeWord[ncDataCodeWord + (ncBlockSum * j) + nBlockNo] = m_byRSWork[j];
		}
		nDataCwIndex += ncDataCw2;
		nBlockNo++;
	}

	m_nSymbleSize = m_nVersion * 4 + 17;

	// Module placement
	FormatModule();

	for (i = 0; i < m_nSymbleSize; i++) {
		for (j = 0; j < m_nSymbleSize; j++) {
			if (!m_byModuleData[i][j]) {
				putBitToPos((j * m_nSymbleSize) + i + 1, 0, result);
			} else {
				putBitToPos((j * m_nSymbleSize) + i + 1, 1, result);
			}
		}
	}
	return m_nSymbleSize;
}