Tizen 2.0 Release
[external/epson-laser-printer-escpage.git] / lib / epson-escpr-mem.c
1 /*________________________________  epson-escpr-mem.c   ________________________________*/\r
2 \r
3 /*       1         2         3         4         5         6         7         8        */\r
4 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/\r
5 /*******************************************|********************************************/\r
6 /*\r
7  *   Copyright (c) 2009  Seiko Epson Corporation   All rights reserved.\r
8  *\r
9  *   Copyright protection claimed includes all forms and matters of copyrightable\r
10  *   material and information now allowed by statutory or judicial law or hereinafter\r
11  *   granted, including without limitation, material generated from the software\r
12  *   programs which are displayed on the screen such as icons, screen display looks,\r
13  *   etc.\r
14  */\r
15 /*******************************************|********************************************/\r
16 /*                                                                                      */\r
17 /*                                   memory Module                                      */\r
18 /*                                                                                      */\r
19 /*                                Public Function Calls                                 */\r
20 /*                              --------------------------                              */\r
21 /*      void*       memRealloc          (buffer, oldSizem, newSize              );      */\r
22 /*              EPS_INT8*   memStrStr           (strSrc, strFind, gotoEnd               );      */\r
23 /*      EPS_INT8*       memStrStrWithLen        (strSrc, nSrcLen, strFind               );      */\r
24 /*      void        memSetEndian        (Endianess, byteSize, endTag, value, array);    */\r
25 /*      void        memInspectEndian    (                                       );      */\r
26 /*      EPS_INT16   memHtoN16           (nSrc                                   );      */\r
27 /*      EPS_INT32   memHtoN32           (nSrc                                   );      */\r
28 /*      EPS_INT32       memGetBitCount          (bitfield                                                               );              */\r
29 /*                                                                                      */\r
30 /*******************************************|********************************************/\r
31 \r
32 /*------------------------------------  Includes   -------------------------------------*/\r
33 /*******************************************|********************************************/\r
34 #include "epson-escpr-def.h"\r
35 #include "epson-escpr-err.h"\r
36 #include "epson-escpr-mem.h"\r
37 \r
38 /*----------------------------  ESC/P-R Lib Global Variables  --------------------------*/\r
39 /*******************************************|********************************************/\r
40 \r
41         /*** Extern Function                                                                */\r
42         /*** -------------------------------------------------------------------------------*/\r
43 extern EPS_CMN_FUNC    epsCmnFnc;\r
44 \r
45 EPS_INT16   cpuEndian;                      /* Endian-ness                              */\r
46 \r
47 \r
48 /*--------------------------------  Local Definition   ---------------------------------*/\r
49 /*******************************************|********************************************/\r
50 \r
51 \r
52 /*--------------------------------  Local Functions   ----------------------------------*/\r
53 /*******************************************|********************************************/\r
54 \r
55 \r
56 \r
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
59 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/\r
60 /*--------------------              Public Functions               ---------------------*/\r
61 /*%%%%%%%%%%%%%%%%%%%%                                             %%%%%%%%%%%%%%%%%%%%%*/\r
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
64 \r
65 /*******************************************|********************************************/\r
66 /*                                                                                      */\r
67 /* Function name:     memRealloc()                                                                  */\r
68 /*                                                                                      */\r
69 /* Arguments                                                                            */\r
70 /* ---------                                                                            */\r
71 /* Name:        Type:               Description:                                        */\r
72 /* buffer               void*                           I/O: pointer of buffer                                                          */\r
73 /* oldSize              EPS_UINT32                      I:   original buffer size                                                       */\r
74 /* newSize              EPS_UINT32                      I:   new buffer size                                                            */\r
75 /*                                                                                      */\r
76 /* Return value:                                                                        */\r
77 /*                              EPS_INT8*                       pointer to finded string                            */\r
78 /*                                                                                      */\r
79 /* Description:                                                                         */\r
80 /*      Realocate buffer.                                                                                                                               */\r
81 /*                                                                                      */\r
82 /*******************************************|********************************************/\r
83 void* memRealloc (\r
84                                  \r
85                 void*       buffer, \r
86                 EPS_UINT32  oldSize, \r
87                 EPS_UINT32  newSize\r
88                 \r
89 ){\r
90         /* Create a temporary pointer to a new buffer of the desired size */\r
91         void* newBuffer = EPS_ALLOC(newSize);\r
92         if( NULL == newBuffer ){\r
93                 EPS_SAFE_RELEASE( buffer );\r
94                 return NULL;\r
95         }\r
96 \r
97         memset(newBuffer, 0, newSize);\r
98 \r
99         /* Copy the data from the old buffer to the new one */\r
100         if(oldSize < newSize)\r
101         {\r
102                 memcpy(newBuffer, buffer, oldSize);\r
103         }\r
104         else\r
105         {\r
106                 memcpy(newBuffer, buffer, newSize);\r
107         }\r
108         /* Free the original buffer */\r
109         EPS_SAFE_RELEASE( buffer );\r
110 \r
111         /* Return a pointer to the new block of memory */\r
112         return newBuffer;\r
113 }\r
114 \r
115 \r
116 /*******************************************|********************************************/\r
117 /*                                                                                      */\r
118 /* Function name:     memStrStr()                                                                   */\r
119 /*                                                                                      */\r
120 /* Arguments                                                                            */\r
121 /* ---------                                                                            */\r
122 /* Name:        Type:               Description:                                        */\r
123 /* strSrc               EPS_INT8*                       I: Source string                                                                        */\r
124 /* strFind              EPS_INT8*                       I: Find string. Size MUST be less than 64 byte.         */\r
125 /* gotoEnd              EPS_BOOL                        I: If TRUE, the return value indicates              */\r
126 /*                                     the end of found string.                         */\r
127 /*                                                                                      */\r
128 /* Return value:                                                                        */\r
129 /*                              EPS_INT8*                       pointer to finded string                            */\r
130 /*                                                                                      */\r
131 /* Description:                                                                         */\r
132 /*      Finds the first occurrence of a substring within a string.                                              */\r
133 /*              The comparison is NOT case sensitive.                                                                                   */\r
134 /*                                                                                      */\r
135 /*******************************************|********************************************/\r
136 EPS_INT8* memStrStr (\r
137 \r
138                 EPS_INT8*       strSrc, \r
139                 const EPS_INT8* strFind, \r
140                 EPS_BOOL        gotoEnd\r
141                 \r
142 ){\r
143         EPS_INT8        strShadow[64];\r
144         EPS_INT32       nSrcPos = 0;\r
145         EPS_INT32       nFindPos = 0;\r
146         EPS_INT32       nFindedTop = -1;\r
147         EPS_UINT32      nFindStrLen = 0;\r
148         \r
149         if(NULL == strSrc || 64 <= strlen(strFind)){\r
150                 return NULL;\r
151         }\r
152 \r
153         memset(strShadow, 0, sizeof(strShadow));\r
154 \r
155         /* create shadow string */\r
156         do{\r
157                 if( 0x41 <= strFind[nFindPos] && strFind[nFindPos] <= 0x5A){\r
158                         strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] + 0x20);\r
159                 } else if( 0x61 <= strFind[nFindPos] && strFind[nFindPos] <= 0x7A){\r
160                         strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] - 0x20);\r
161                 } else{\r
162                         strShadow[nFindPos] = strFind[nFindPos];\r
163                 }\r
164         }while('\0' != strFind[nFindPos++]);\r
165 \r
166         /* search by strFind & strShadow */\r
167         nFindPos = 0;\r
168         while( '\0' != strSrc[nSrcPos] && '\0' != strFind[nFindPos]){\r
169                 if( strSrc[nSrcPos] == strFind[nFindPos]\r
170                         || strSrc[nSrcPos] == strShadow[nFindPos] ){\r
171                         if(-1 == nFindedTop){\r
172                                 nFindedTop = nSrcPos;\r
173                         }\r
174 \r
175                         nSrcPos++;\r
176                         nFindPos++;\r
177 \r
178                 } else{\r
179                         if(-1 == nFindedTop){\r
180                                 nSrcPos++;\r
181                                 nFindPos = 0;\r
182                         } else{\r
183                                 nSrcPos = nFindedTop + 1;\r
184                         }\r
185                         nFindedTop = -1;\r
186                 }\r
187         }\r
188 \r
189         nFindStrLen = (EPS_UINT32)strlen(strFind);\r
190         if(0 <= nFindedTop && (EPS_INT32)strlen(strFind) == nFindPos){\r
191                 if(!gotoEnd){\r
192                         return &strSrc[nFindedTop];\r
193                 } else{\r
194                         return &strSrc[nFindedTop] + nFindStrLen;\r
195                 }\r
196         } else{\r
197                 return NULL;\r
198         }\r
199 }\r
200 \r
201 \r
202 /*******************************************|********************************************/\r
203 /*                                                                                      */\r
204 /* Function name:     memStrStrWithLen()                                                            */\r
205 /*                                                                                      */\r
206 /* Arguments                                                                            */\r
207 /* ---------                                                                            */\r
208 /* Name:        Type:               Description:                                        */\r
209 /* strSrc               EPS_INT8*                       I: Source string                                                                        */\r
210 /* nSrcLen              EPS_UINT32                      I: Source string length                                                         */\r
211 /* strFind              EPS_INT8*                       I: Find string. Size MUST be less than 64 byte.         */\r
212 /*                                                                                      */\r
213 /* Return value:                                                                        */\r
214 /*                              EPS_INT8*                       pointer to finded string                            */\r
215 /*                                                                                      */\r
216 /* Description:                                                                         */\r
217 /*      Finds the first occurrence of a substring within a string.                                              */\r
218 /*              The comparison is NOT case sensitive.                                                                                   */\r
219 /*              Limit nSrcLen characters.                                                                                                               */\r
220 /*                                                                                      */\r
221 /*******************************************|********************************************/\r
222 EPS_INT8* memStrStrWithLen (\r
223                                                         \r
224                 EPS_INT8*       strSrc, \r
225                 EPS_UINT32      nSrcLen, \r
226                 const EPS_INT8* strFind\r
227                 \r
228 ){\r
229         EPS_INT8        strShadow[64];\r
230         EPS_INT32       nSrcPos = 0;\r
231         EPS_INT32       nFindPos = 0;\r
232         EPS_INT32       nFindedTop = -1;\r
233 \r
234         if(NULL == strSrc || 64 <= strlen(strFind)){\r
235                 return NULL;\r
236         }\r
237 \r
238         memset(strShadow, 0, sizeof(strShadow));\r
239 \r
240         /* create shadow string */\r
241         do{\r
242                 if( 0x41 <= strFind[nFindPos] && strFind[nFindPos] <= 0x5A){\r
243                         strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] + 0x20);\r
244                 } else if( 0x61 <= strFind[nFindPos] && strFind[nFindPos] <= 0x7A){\r
245                         strShadow[nFindPos] = (EPS_INT8)(strFind[nFindPos] - 0x20);\r
246                 } else{\r
247                         strShadow[nFindPos] = strFind[nFindPos];\r
248                 }\r
249         }while('\0' != strFind[nFindPos++]);\r
250 \r
251         /* search by strFind & strShadow */\r
252         nFindPos = 0;\r
253         while( (EPS_INT32)nSrcLen > nSrcPos && '\0' != strFind[nFindPos]){\r
254                 if( strSrc[nSrcPos] == strFind[nFindPos]\r
255                         || strSrc[nSrcPos] == strShadow[nFindPos] ){\r
256                         if(-1 == nFindedTop){\r
257                                 nFindedTop = nSrcPos;\r
258                         }\r
259 \r
260                         nSrcPos++;\r
261                         nFindPos++;\r
262                 } else{\r
263                         if(-1 == nFindedTop){\r
264                                 nSrcPos++;\r
265                         } else{\r
266                                 nSrcPos = nFindedTop + 1;\r
267                         }\r
268                         nFindPos = 0;\r
269                         nFindedTop = -1;\r
270                 }\r
271         }\r
272 \r
273         if(0 <= nFindedTop && (EPS_INT32)strlen(strFind) == nFindPos){\r
274                 return &strSrc[nFindedTop];\r
275         } else{\r
276                 return NULL;\r
277         }\r
278 }\r
279 \r
280 \r
281 /*******************************************|********************************************/\r
282 /* Function name:   memSetEndian()                                                      */\r
283 /*                                                                                      */\r
284 /* Arguments                                                                            */\r
285 /* ---------                                                                            */\r
286 /* Name:        Type:               Description:                                        */\r
287 /* Endianess    EPS_ENDIAN          I: Desired Endianess (Big/Little)                   */\r
288 /* byteSize     EPS_BYTE_SIZE       I: 2-byte or 4 bytes to convert                     */\r
289 /* value        EPS_UINT32          I: 4 Bytes to be swapped if necesaray               */\r
290 /* array        EPS_UINT8*          O: Correct endian-ness bytes                        */\r
291 /*                                                                                      */\r
292 /* Return value:                                                                        */\r
293 /*      None                                                                            */\r
294 /*                                                                                      */\r
295 /* Description:                                                                         */\r
296 /*      Swap data depending on endian-ness.                                             */\r
297 /*                                                                                      */\r
298 /*******************************************|********************************************/\r
299 void     memSetEndian (\r
300 \r
301         EPS_ENDIAN      Endianess,          /* Desired Endianess (Big/Little)           */\r
302         EPS_BYTE_SIZE   byteSize,           /* 2-byte or 4 bytes to convert             */\r
303         EPS_UINT32      value,              /* 4 Bytes to be swapped if necesaray       */\r
304         EPS_UINT8*      array               /* Correct endian-ness bytes                */\r
305 \r
306 ){\r
307 \r
308 /*** Declare Variable Local to Routine                                                  */\r
309     EPS_UINT16  value2byte;\r
310     EPS_UINT32  value4byte;\r
311 \r
312 /*** Initialize Local Variables                                                         */\r
313 \r
314 /*** Based on desired Eniandess - Perform test and swap, if necessary                   */\r
315     switch (byteSize + Endianess) {\r
316         /*** Change 2 bytes value to the little endianness                              */\r
317         case (EPS_2_BYTES + EPS_ENDIAN_LITTLE):\r
318 #if 0  /* Not Used */\r
319             value2byte = (EPS_UINT16)value;\r
320             array[0]   = (EPS_UINT8)((value2byte     ) & 0x00ff);\r
321             array[1]   = (EPS_UINT8)((value2byte >> 8) & 0x00ff);\r
322 #endif\r
323             break;\r
324         /*** Change 2 bytes value to the big endianness                                 */\r
325         case (EPS_2_BYTES + EPS_ENDIAN_BIG):\r
326             value2byte = (EPS_UINT16)value;\r
327             array[0]   = (EPS_UINT8)((value2byte >> 8) & 0x00ff);\r
328             array[1]   = (EPS_UINT8)((value2byte     ) & 0x00ff);\r
329             break;\r
330         /*** Change 4 bytes value to the little endianness                              */\r
331         case (EPS_4_BYTES + EPS_ENDIAN_LITTLE):\r
332             value4byte = (EPS_UINT32)value;\r
333             array[0]   = (EPS_UINT8)((value4byte      ) & 0x000000ff);\r
334             array[1]   = (EPS_UINT8)((value4byte >>  8) & 0x000000ff);\r
335             array[2]   = (EPS_UINT8)((value4byte >> 16) & 0x000000ff);\r
336             array[3]   = (EPS_UINT8)((value4byte >> 24) & 0x000000ff);\r
337             break;\r
338         /*** Change 4 bytes value to the big endianness                                 */\r
339         case (EPS_4_BYTES + EPS_ENDIAN_BIG):\r
340             value4byte = (EPS_UINT32)value;\r
341             array[0]   = (EPS_UINT8)((value4byte >> 24) & 0x000000ff);\r
342             array[1]   = (EPS_UINT8)((value4byte >> 16) & 0x000000ff);\r
343             array[2]   = (EPS_UINT8)((value4byte >>  8) & 0x000000ff);\r
344             array[3]   = (EPS_UINT8)((value4byte      ) & 0x000000ff);\r
345             break;\r
346         default:\r
347             break;\r
348     }\r
349 }\r
350 \r
351 \r
352 /*******************************************|********************************************/\r
353 /* Function name:   memInspectEndian()                                                  */\r
354 /*                                                                                      */\r
355 /* Arguments                                                                            */\r
356 /* ---------                                                                            */\r
357 /* Name:        Type:               Description:                                        */\r
358 /* (None)                                                                               */\r
359 /*                                                                                      */\r
360 /* Return value:                                                                        */\r
361 /*      None                                                                            */\r
362 /*                                                                                      */\r
363 /* Description:                                                                         */\r
364 /*      Determine "Endian-ness" for the current cpu.                                    */\r
365 /*                                                                                      */\r
366 /*******************************************|********************************************/\r
367 void   memInspectEndian (\r
368 \r
369         void\r
370 \r
371 ){\r
372         union {\r
373                 EPS_INT8    array[2];                   /* Endian-ness test array               */\r
374                 EPS_INT16   chars;                      /* Endian-ness test string              */\r
375         } EndianTest;\r
376 \r
377     EndianTest.array[0] = 'a';    \r
378     EndianTest.array[1] = 'b';\r
379         if (EndianTest.chars == 0x6162) {\r
380                 cpuEndian = EPS_ENDIAN_BIG;\r
381         } else{\r
382         cpuEndian = EPS_ENDIAN_LITTLE;\r
383         }\r
384 }\r
385 \r
386 \r
387 EPS_INT32 memGetBitCount(\r
388 \r
389                 EPS_INT32 bitfield\r
390 \r
391 ){\r
392         EPS_INT32       i = 0;\r
393         EPS_INT32       cnt = 0;\r
394 \r
395         for(i = 0; i < sizeof(bitfield)*8; i++){\r
396                 if( (bitfield >> i) & 1 ){\r
397                         cnt++;\r
398                 }\r
399         }\r
400 \r
401         return cnt;\r
402 }\r
403 \r
404 \r
405 EPS_UINT8 memSearchWhiteColorVal(\r
406                 \r
407                 EPS_UINT8       colorPlane,\r
408                 EPS_UINT8*      paletteData,\r
409                 EPS_UINT16      paletteSize\r
410                 \r
411 ){\r
412         EPS_UINT8       whiteColorValue = 255;  /* Set default index for white to negative one  */\r
413         EPS_UINT16   jdx;                                       /* General indes/loop variable              */\r
414         EPS_UINT8*  bufPtr = NULL;                      /* Temporary buffer pointer                 */\r
415 \r
416         if( colorPlane == EPS_CP_256COLOR ){ \r
417         bufPtr = paletteData;\r
418         for ( jdx =  0; jdx < paletteSize/3; jdx++ ) {\r
419             if (*bufPtr == 255 && *(bufPtr+1) == 255 && *(bufPtr+2) == 255) {\r
420                 whiteColorValue = (EPS_UINT8)jdx;\r
421                 break;\r
422             }\r
423             bufPtr += 3;\r
424         }\r
425         } else /* if(EPS_CP_FULLCOLOR) */{\r
426                 whiteColorValue = 0xFF;\r
427         }\r
428 \r
429         return whiteColorValue;\r
430 }\r
431 \r
432 /*________________________________  epson-escpr-mem.c  _________________________________*/\r
433   \r
434 /*34567890123456789012345678901234567890123456789012345678901234567890123456789012345678*/\r
435 /*       1         2         3         4         5         6         7         8        */\r
436 /*******************************************|********************************************/\r
437 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r
438 /***** End of File *** End of File *** End of File *** End of File *** End of File ******/\r
439 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/\r