Skip to content

Commit

Permalink
QRcode_encodeData{,MQR,Structured}() have been added.
Browse files Browse the repository at this point in the history
Code refactoring.
  • Loading branch information
fukuchi committed Jan 16, 2010
1 parent 8ff21ca commit b44c5f9
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 35 deletions.
117 changes: 82 additions & 35 deletions qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ QRcode *QRcode_encodeInput(QRinput *input)
}
}

static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLevel level, int eightbit, int mqr, QRencodeMode hint, int casesensitive)
static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLevel level, int mqr, QRencodeMode hint, int casesensitive)
{
QRinput *input;
QRcode *code;
Expand All @@ -639,7 +639,7 @@ static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLeve
errno = EINVAL;
return NULL;
}
if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) {
if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
errno = EINVAL;
return NULL;
}
Expand All @@ -651,11 +651,46 @@ static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLeve
}
if(input == NULL) return NULL;

if(eightbit) {
ret = QRinput_append(input, QR_MODE_8, strlen(string), (unsigned char *)string);
ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
if(ret < 0) {
QRinput_free(input);
return NULL;
}
code = QRcode_encodeInput(input);
QRinput_free(input);

return code;
}

QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
{
return QRcode_encodeStringReal(string, version, level, 0, hint, casesensitive);
}

QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
{
return QRcode_encodeStringReal(string, version, level, 1, hint, casesensitive);
}

static QRcode *QRcode_encodeDataReal(const unsigned char *data, int length, int version, QRecLevel level, int mqr)
{
QRinput *input;
QRcode *code;
int ret;

if(data == NULL || length == 0) {
errno = EINVAL;
return NULL;
}

if(mqr) {
input = QRinput_newMQR(version, level);
} else {
ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
input = QRinput_new2(version, level);
}
if(input == NULL) return NULL;

ret = QRinput_append(input, QR_MODE_8, length, data);
if(ret < 0) {
QRinput_free(input);
return NULL;
Expand All @@ -666,26 +701,35 @@ static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLeve
return code;
}

QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level)
{
return QRcode_encodeStringReal(string, version, level, 1, 0, QR_MODE_NUL, 0);
return QRcode_encodeDataReal(data, size, version, level, 0);
}

QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
{
return QRcode_encodeStringReal(string, version, level, 0, 0, hint, casesensitive);
if(string == NULL) {
errno = EINVAL;
return NULL;
}
return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 0);
}

QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level)
QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level)
{
return QRcode_encodeStringReal(string, version, level, 1, 1, QR_MODE_NUL, 0);
return QRcode_encodeDataReal(data, size, version, level, 1);
}

QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level)
{
return QRcode_encodeStringReal(string, version, level, 0, 1, hint, casesensitive);
if(string == NULL) {
errno = EINVAL;
return NULL;
}
return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 1);
}


/******************************************************************************
* Structured QR-code encoding
*****************************************************************************/
Expand Down Expand Up @@ -795,7 +839,10 @@ static QRcode_List *QRcode_encodeInputToStructured(QRinput *input)
return codes;
}

QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level)
static QRcode_List *QRcode_encodeDataStructuredReal(
int size, const unsigned char *data,
int version, QRecLevel level,
int eightbit, QRencodeMode hint, int casesensitive)
{
QRinput *input;
QRcode_List *codes;
Expand All @@ -805,11 +852,19 @@ QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version,
errno = EINVAL;
return NULL;
}
if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) {
errno = EINVAL;
return NULL;
}

input = QRinput_new2(version, level);
if(input == NULL) return NULL;

ret = QRinput_append(input, QR_MODE_8, strlen(string), (unsigned char *)string);
if(eightbit) {
ret = QRinput_append(input, QR_MODE_8, size, data);
} else {
ret = Split_splitStringToQRinput((char *)data, input, hint, casesensitive);
}
if(ret < 0) {
QRinput_free(input);
return NULL;
Expand All @@ -820,33 +875,25 @@ QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version,
return codes;
}

QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
{
QRinput *input;
QRcode_List *codes;
int ret;
QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level) {
return QRcode_encodeDataStructuredReal(size, data, version, level, 1, QR_MODE_NUL, 0);
}

if(version <= 0) {
errno = EINVAL;
return NULL;
}
if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level) {
if(string == NULL) {
errno = EINVAL;
return NULL;
}
return QRcode_encodeDataStructured(strlen(string), (unsigned char *)string, version, level);
}

input = QRinput_new2(version, level);
if(input == NULL) return NULL;

ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
if(ret < 0) {
QRinput_free(input);
QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
{
if(string == NULL) {
errno = EINVAL;
return NULL;
}
codes = QRcode_encodeInputToStructured(input);
QRinput_free(input);

return codes;
return QRcode_encodeDataStructuredReal(strlen(string), (unsigned char *)string, version, level, 0, hint, casesensitive);
}

/******************************************************************************
Expand Down
35 changes: 35 additions & 0 deletions qrencode.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,26 @@ extern QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel
*/
extern QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level);

/**
* Encode byte stream (may include '\0') in 8-bit mode.
* @warning This function is THREAD UNSAFE.
* @param size size of the input data.
* @param data input data.
* @param version version of the symbol. If 0, the library chooses the minimum
* version for the given input data.
* @param level error correction level.
* @throw EINVAL invalid input object.
* @throw ENOMEM unable to allocate memory for input objects.
* @throw ERANGE input data is too large.
*/
extern QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level);

/**
* Micro QR Code version of QRcode_encodeData().
* @warning This function is THREAD UNSAFE.
*/
extern QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level);

/**
* Free the instance of QRcode class.
* @param qrcode an instance of QRcode class.
Expand Down Expand Up @@ -458,6 +478,21 @@ extern QRcode_List *QRcode_encodeStringStructured(const char *string, int versio
*/
extern QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level);

/**
* Create structured symbols from byte stream (may include '\0'). Wholde data
* are encoded in 8-bit mode.
* @warning This function is THREAD UNSAFE.
* @param size size of the input data.
* @param data input dat.
* @param version version of the symbol.
* @param level error correction level.
* @return a singly-linked list of QRcode. On error, NULL is returned, and
* errno is set to indicate the error. See Exceptions for the details.
* @throw EINVAL invalid input object.
* @throw ENOMEM unable to allocate memory for input objects.
*/
extern QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level);

/**
* Return the number of symbols included in a QRcode_List.
* @param qrlist a head entry of a QRcode_List.
Expand Down

0 comments on commit b44c5f9

Please sign in to comment.