⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdf417_prep.c

📁 PDF417编码的算法的源程序
💻 C
字号:
//////////////////////////////////////////////////////////////////////////////// File name    :pdf417_prep.c// Derived from :// Author       :M.A. Brand// Reviewer     ://// Description : Converts an arbitrary file into form usable by pdf417_encode//               by applying the Annex P algorithm of the ISO pdf417 specification.//// Copyright (c) 2004, 2005 N200.com B.V.// Marifoonweg 1// 1042 AV Amsterdam, The Netherlands///*    This file is part of pdf417_prep.    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with this program; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*///// History // ---------------------------------------------------------------------------// Date       | Who           |Action// ---------------------------------------------------------------------------// 2004-07-22 | M.A. Brand     |Created// 2004-08-02 | M.A. Brand     |Fixed a bug that could make pdf417_prep loop forever// 2004-08-02 | Niels Duineveld|Fixed bug in algorithm that would cause byte compaction //            |                |where text compaction would be the optimal choice// 2004-08-02 | M.A. Brand     |Replaced isdigit() with is_nc(). isdigit failed//            |                |failed to return the expected result for extended//            |                |characters in release builds. This was hard to find.// 2004-12-20 | M.A. Brand     |Increased PDF417_RECORD_LEN// 2005-01-05 | M.A. Brand     |Fixed is_tc() to give correct result for NULL.// 2005-01-06 | M.A. Brand     |Fixed processing when short numeric string appears//            |                |at end of buffer////////////////////////////////////////////////////////////////////////////////#include <stdio.h>#include <stdlib.h>#include <string.h>#include "pdf417_prep.h"//Line buffer size. Not sure how long lines in input//for pdf417_encode can be.#define PDF417_RECORD_LEN (512)//These functions output a sequence of bytes to a file in the format that //pdf417_enc understands. Each compaction type has an output function.static void output_bc(const char* buf, int len, FILE* fp);static void output_nc(const char* buf, int len, FILE* fp);static void output_tc(const char* buf, int len, FILE* fp);//These functions test a byte. They return true if the byte is a candidate for numeric compaction//or text compaction, respectively. The compaction mode that actually ends up being used is //context dependentstatic int is_nc(char c);static int is_tc(char c);/* Very literal implementation of Annex P algorithm */int pdf417_prep(const char* buffer, int len, FILE* fp){    const char* p; //ptr to current sequence of bytes    int n, t, b;  //counters for numeric, text, and byte compaction candidates             p = buffer;        while(p < buffer + len)    {        //STEP3: test for sequence for numeric compaction            n = 0;            while(p + n < buffer + len)        {            if (!is_nc(p[n]))            {                break;            }            n++;        }                if (n >= 13)        {            /* Use numeric compaction */            output_nc(p, n, fp);            p += n;            continue;        }                //STEP10: test for sequence for text compaction                t = n = 0;            while(p + n + t  < buffer + len)        {            if (is_nc(p[n + t]))            {                n++;            }            else if (is_tc(p[n + t]))            {                t++;                t += n;                n = 0;            }            else            {                break;            }            if (n == 13)            {                break;            }        }        if (n < 13)        {            //encountered BC byte or end of buffer reached. convert remaining n to t            t += n;            n = 0;        }                if (t >= 5)        {            /* Use text compaction */            output_tc(p, t, fp);            p += t;            continue;        }                //STEP17: test for sequence for byte compaction                b = t = n = 0;            while(p + n + t + b < buffer + len)        {            if (is_nc(p[n + t + b]))            {                n++;            }            else if (is_tc(p[n + t + b]))            {                t++;                t += n;                n = 0;            }            else            {                b++;                b += n + t;                n = 0;                t = 0;            }            if (n == 13)            {                b += t;                t = 0;                break;            }            if (t >= 5)            {                b += n;                n = 0;                break;            }        }                if (n < 13)        {            //end of buffer reached. convert remaining n to t            t += n;            n = 0;        }        if (t < 5)        {            //end of buffer reached. convert remaining t to b            b += t;            t = 0;        }        if (b >= 1)        {            // Use byte compaction            // Annex P Step 18 recommends:            // If b == 1 and current mode is text,            // then we should SHIFT into Byte Compaction Mode;            // otherwise, we should LATCH into Byte Compaction Mode.            // pdf417_encode should make this distinction since            // we cannot do it here.                        output_bc(p, b, fp);            p += b;        }    }    return 1;}void output_bc(const char* buf, int len, FILE* fp){    int i;    if (!buf || !len || !fp)    {        return;    }        for (i = 0; i < len; i++)    {        if (i % PDF417_RECORD_LEN == 0)        {            fputs("BC \"", fp);        }                fprintf(fp, "%02X", (unsigned char)buf[i]);                if ((i + 1) % PDF417_RECORD_LEN == 0 || i == len - 1)        {            fputs("\"\n", fp);        }            }}void output_nc(const char* buf, int len, FILE* fp){    int i;    if (!buf || !len || !fp)    {        return;    }    for (i = 0; i < len; i++)    {        if (i % PDF417_RECORD_LEN == 0)        {            fputs("NC \"", fp);        }                fputc(buf[i], fp);                if ((i + 1) % PDF417_RECORD_LEN == 0 || i == len - 1)        {            fputs("\"\n", fp);        }    }}void output_tc(const char* buf, int len, FILE* fp){    int i ;    if (!buf || !len || !fp)    {        return;    }        for (i = 0; i < len; i++)    {        if (i % PDF417_RECORD_LEN == 0)        {            fputs("TC \"", fp);        }                switch(buf[i])        {        case '\"':            fputs("\\DQ", fp);            break;        case '\n':            fputs("\\LF", fp);            break;        case '\r':            fputs("\\CR", fp);            break;        case '\\':            fputs("\\BS", fp);            break;                default:            fputc(buf[i], fp);            break;        }                if ((i + 1) % PDF417_RECORD_LEN == 0 || i == len - 1)        {            fputs("\"\n", fp);        }    }}int is_tc(char c){    if (c >= 32 && c <= 126)    {        return 1;    }    else if (c && strchr("\t\n\r", c))    {        return 1;    }        return 0;}int is_nc(char c){    return (c >= '0' && c <= '9');}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -