1 /**
2 * libdmtx - Data Matrix Encoding/Decoding Library
3 * Copyright 2008, 2009 Mike Laughton. All rights reserved.
4 * Copyright 2012-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
5 *
6 * See LICENSE file in the main project directory for full
7 * terms of use and distribution.
8 *
9 * Contact:
10 * Vadim A. Misbakh-Soloviov <dmtx@mva.name>
11 * Mike Laughton <mike@dragonflylogic.com>
12 *
13 * \file dmtxmessage.c
14 * \brief Data message handling
15 */
16
17 /**
18 * \brief Allocate memory for message
19 * \param sizeIdx
20 * \param symbolFormat DmtxFormatMatrix | DmtxFormatMosaic
21 * \return Address of allocated memory
22 */
23 extern DmtxMessage *
dmtxMessageCreate(int sizeIdx,int symbolFormat)24 dmtxMessageCreate(int sizeIdx, int symbolFormat)
25 {
26 DmtxMessage *message;
27 int mappingRows, mappingCols;
28
29 assert(symbolFormat == DmtxFormatMatrix || symbolFormat == DmtxFormatMosaic);
30
31 mappingRows = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixRows, sizeIdx);
32 mappingCols = dmtxGetSymbolAttribute(DmtxSymAttribMappingMatrixCols, sizeIdx);
33
34 message = (DmtxMessage *)calloc(1, sizeof(DmtxMessage));
35 if(message == NULL)
36 return NULL;
37
38 message->arraySize = sizeof(unsigned char) * mappingRows * mappingCols;
39
40 message->array = (unsigned char *)calloc(1, message->arraySize);
41 if(message->array == NULL) {
42 perror("Calloc failed");
43 dmtxMessageDestroy(&message);
44 return NULL;
45 }
46
47 message->codeSize = sizeof(unsigned char) *
48 dmtxGetSymbolAttribute(DmtxSymAttribSymbolDataWords, sizeIdx) +
49 dmtxGetSymbolAttribute(DmtxSymAttribSymbolErrorWords, sizeIdx);
50
51 if(symbolFormat == DmtxFormatMosaic)
52 message->codeSize *= 3;
53
54 message->code = (unsigned char *)calloc(message->codeSize, sizeof(unsigned char));
55 if(message->code == NULL) {
56 perror("Calloc failed");
57 dmtxMessageDestroy(&message);
58 return NULL;
59 }
60
61 /* XXX not sure if this is the right place or even the right approach.
62 Trying to allocate memory for the decoded data stream and will
63 initially assume that decoded data will not be larger than 2x encoded data */
64 message->outputSize = sizeof(unsigned char) * message->codeSize * 10;
65 message->output = (unsigned char *)calloc(message->outputSize, sizeof(unsigned char));
66 if(message->output == NULL) {
67 perror("Calloc failed");
68 dmtxMessageDestroy(&message);
69 return NULL;
70 }
71
72 return message;
73 }
74
75 /**
76 * \brief Free memory previously allocated for message
77 * \param message
78 * \return void
79 */
80 extern DmtxPassFail
dmtxMessageDestroy(DmtxMessage ** msg)81 dmtxMessageDestroy(DmtxMessage **msg)
82 {
83 if(msg == NULL || *msg == NULL)
84 return DmtxFail;
85
86 if((*msg)->array != NULL)
87 free((*msg)->array);
88
89 if((*msg)->code != NULL)
90 free((*msg)->code);
91
92 if((*msg)->output != NULL)
93 free((*msg)->output);
94
95 free(*msg);
96
97 *msg = NULL;
98
99 return DmtxPass;
100 }
101