Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. ; pngmem.asm - stub functions for memory allocation
  3.  
  4. ; Last changed in libpng 1.6.24 [August 4, 2016%]
  5. ; Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson
  6. ; (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7. ; (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8.  
  9. ; This code is released under the libpng license.
  10. ; For conditions of distribution and use, see the disclaimer
  11. ; and license in png.inc
  12.  
  13. ; This file provides a location for all memory allocation.  Users who
  14. ; need special memory handling are expected to supply replacement
  15. ; functions for png_malloc() and png_free(), and to use
  16. ; png_create_read_struct_2() and png_create_write_struct_2() to
  17. ; identify the replacement functions.
  18.  
  19. ; Free a png_struct
  20. ;void (png_structrp png_ptr)
  21. align 4
  22. proc png_destroy_png_struct uses eax ecx edi esi, png_ptr:dword
  23. locals
  24.         dummy_struct png_struct
  25. endl
  26.         mov edi,[png_ptr]
  27.         cmp edi,0
  28.         je @f ;if (..!=0)
  29.                 ; png_free might call png_error and may certainly call
  30.                 ; png_get_mem_ptr, so fake a temporary png_struct to support this.
  31.  
  32.                 mov ecx,sizeof.png_struct
  33.                 mov esi,edi
  34.                 mov edi,ebp
  35.                 sub edi,ecx
  36.                 rep movsb ;dummy_struct = *png_ptr
  37.                 mov edi,[png_ptr]
  38.                 xor eax,eax
  39.                 mov ecx,sizeof.png_struct
  40.                 rep stosb ;memset(png_ptr, 0, (sizeof *png_ptr))
  41.                 mov esi,ebp
  42.                 sub esi,sizeof.png_struct
  43.                 stdcall png_free, esi, [png_ptr]
  44.  
  45. if PNG_SETJMP_SUPPORTED eq 1
  46.                 ; We may have a jmp_buf left to deallocate.
  47.                 stdcall png_free_jmpbuf, esi
  48. end if
  49.         @@:
  50.         ret
  51. endp
  52.  
  53. ; Allocate memory.  For reasonable files, size should never exceed
  54. ; 64K.  However, zlib may allocate more than 64K if you don't tell
  55. ; it not to.  See zconf.h and png.h for more information.  zlib does
  56. ; need to allocate exactly 64K, so whatever you call here must
  57. ; have the ability to do that.
  58.  
  59. ;voidp (const_structrp png_ptr, png_alloc_size_t size)
  60. align 4
  61. proc png_calloc uses ebx ecx edi, png_ptr:dword, size:dword
  62.         stdcall png_malloc, [png_ptr], [size]
  63.  
  64.         cmp eax,0
  65.         je @f ;if (..!=0)
  66.                 mov ebx,eax
  67.                 mov edi,eax
  68.                 mov ecx,[size]
  69.                 xor eax,eax
  70.                 rep stosb ;memset(ret, 0, size)
  71.                 mov eax,ebx
  72.         @@:
  73.         ret
  74. endp
  75.  
  76. ; png_malloc_base, an internal function added at libpng 1.6.0, does the work of
  77. ; allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
  78. ; Checking and error handling must happen outside this routine; it returns NULL
  79. ; if the allocation cannot be done (for any reason.)
  80.  
  81. ;voidp (const_structrp png_ptr, png_alloc_size_t size)
  82. align 4
  83. proc png_malloc_base uses ebx ecx, png_ptr:dword, size:dword
  84.         ; Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
  85.         ; allocators have also been removed in 1.6.0, so any 16-bit system now has
  86.         ; to implement a user memory handler.  This checks to be sure it isn't
  87.         ; called with big numbers.
  88.  
  89.         ; Some compilers complain that this is always true.  However, it
  90.         ; can be false when integer overflow happens.
  91.  
  92.         cmp dword[size],0
  93.         jle .end0
  94.         cmp dword[size],PNG_SIZE_MAX
  95.         jg .end0 ;   if (..>.. && ..<=..)
  96. if PNG_MAX_MALLOC_64K eq 1
  97.         cmp dword[size],65536
  98.         jg .end0
  99. end if
  100. if PNG_USER_MEM_SUPPORTED eq 1
  101.         mov ebx,[png_ptr]
  102.         cmp ebx,0
  103.         je @f
  104.         cmp dword[ebx+png_struct.malloc_fn],0
  105.         je @f ;if (..!=0 && ..!=0)
  106.                 stdcall [ebx+png_struct.malloc_fn], ebx, [size]
  107.                 jmp .end_f
  108.         @@: ;else
  109. end if
  110.                 ;stdcall [mem.alloc], [size]
  111.                 mcall SF_SYS_MISC, SSF_MEM_ALLOC, [size]
  112.                 jmp .end_f ;checked for truncation above
  113.         .end0: ;else
  114.         xor eax,eax
  115. .end_f:
  116.         ret
  117. endp
  118.  
  119. ; This is really here only to work round a spurious warning in GCC 4.6 and 4.7
  120. ; that arises because of the checks in png_realloc_array that are repeated in
  121. ; png_malloc_array.
  122.  
  123. ;voidp (const_structrp png_ptr, int nelements, size_t element_size)
  124. align 4
  125. proc png_malloc_array_checked, png_ptr:dword, nelements:dword, element_size:dword
  126. ;   png_alloc_size_t req = nelements; /* known to be > 0 */
  127.  
  128. ;   if (req <= PNG_SIZE_MAX/element_size)
  129. ;      return png_malloc_base(png_ptr, req * element_size);
  130.  
  131.         ; The failure case when the request is too large
  132.         xor eax,eax
  133. .end_f:
  134.         ret
  135. endp
  136.  
  137. ;voidp (const_structrp png_ptr, int nelements, size_t element_size)
  138. align 4
  139. proc png_malloc_array, png_ptr:dword, nelements:dword, element_size:dword
  140. ;   if (nelements <= 0 || element_size == 0)
  141. ;      png_error(png_ptr, "internal error: array alloc");
  142.  
  143.         stdcall png_malloc_array_checked, [png_ptr], [nelements], [element_size]
  144.         ret
  145. endp
  146.  
  147. ;voidp (const_structrp png_ptr, const_voidp old_array,
  148. ;    int old_elements, int add_elements, size_t element_size)
  149. align 4
  150. proc png_realloc_array, png_ptr:dword, old_array:dword, old_elements:dword, add_elements:dword, element_size:dword
  151.         ; These are internal errors:
  152. ;   if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
  153. ;      (old_array == NULL && old_elements > 0))
  154. ;      png_error(png_ptr, "internal error: array realloc");
  155.  
  156.         ; Check for overflow on the elements count (so the caller does not have to
  157.         ; check.)
  158.  
  159. ;   if (add_elements <= INT_MAX - old_elements)
  160. ;   {
  161. ;      voidp new_array = png_malloc_array_checked(png_ptr,
  162. ;          old_elements+add_elements, element_size);
  163. ;
  164. ;      if (new_array != NULL)
  165. ;      {
  166.         ; Because png_malloc_array worked the size calculations below cannot
  167.         ; overflow.
  168.  
  169. ;         if (old_elements > 0)
  170. ;            memcpy(new_array, old_array, element_size*(unsigned)old_elements);
  171. ;
  172. ;         memset((char*)new_array + element_size*(unsigned)old_elements, 0,
  173. ;             element_size*(unsigned)add_elements);
  174. ;
  175. ;         return new_array;
  176. ;      }
  177. ;   }
  178.  
  179.         xor eax,eax ;error
  180. .end_f:
  181.         ret
  182. endp
  183.  
  184. ; Various functions that have different error handling are derived from this.
  185. ; png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
  186. ; function png_malloc_default is also provided.
  187.  
  188. ;voidp (const_structrp png_ptr, png_alloc_size_t size)
  189. align 4
  190. proc png_malloc uses edi, png_ptr:dword, size:dword
  191.         xor eax,eax
  192.         mov edi,[png_ptr]
  193.         cmp edi,0
  194.         je @f ;if (..==0) return 0
  195.  
  196.         stdcall png_malloc_base, edi, [size]
  197.  
  198.         cmp eax,0
  199.         jne @f ;if (..==0)
  200.                 png_error edi, 'Out of memory' ;'m' means png_malloc
  201.         @@:
  202.         ret
  203. endp
  204.  
  205. ;voidp (const_structrp png_ptr, png_alloc_size_t size)
  206. align 4
  207. proc png_malloc_default uses edi, png_ptr:dword, size:dword
  208.         xor eax,eax
  209.         mov edi,[png_ptr]
  210.         cmp edi,0
  211.         je @f ;if (..==0) return 0
  212.  
  213.         ; Passing 'NULL' here bypasses the application provided memory handler.
  214.         stdcall png_malloc_base, 0, [size] ;0 - use malloc
  215.  
  216.         cmp eax,0
  217.         jne @f ;if (..==0)
  218.                 png_error edi, 'Out of Memory' ;'M' means png_malloc_default
  219.         @@:
  220.         ret
  221. endp
  222.  
  223. ; This function was added at libpng version 1.2.3.  The png_malloc_warn()
  224. ; function will issue a png_warning and return NULL instead of issuing a
  225. ; png_error, if it fails to allocate the requested memory.
  226.  
  227. ;voidp (const_structrp png_ptr, png_alloc_size_t size)
  228. align 4
  229. proc png_malloc_warn uses edi, png_ptr:dword, size:dword
  230.         mov edi,[png_ptr]
  231.         cmp edi,0
  232.         je .end0 ;if (..!=0)
  233.                 stdcall png_malloc_base, edi, [size]
  234.                 cmp eax,0
  235.                 jne .end_f ;if (..!=0) return ret
  236.  
  237.                 png_warning edi, 'Out of memory'
  238.         .end0:
  239.         xor eax,eax
  240. .end_f:
  241.         ret
  242. endp
  243.  
  244. ; Free a pointer allocated by png_malloc().  If ptr is NULL, return
  245. ; without taking any action.
  246.  
  247. ;void (const_structrp png_ptr, voidp ptr)
  248. align 4
  249. proc png_free uses eax ebx ecx, png_ptr:dword, p2ptr:dword
  250.         mov ebx,[png_ptr]
  251.         cmp ebx,0
  252.         je .end_f
  253.         mov ecx,[p2ptr]
  254.         cmp ecx,0
  255.         je .end_f ;if (..==0 || ..==0) return
  256.  
  257. if PNG_USER_MEM_SUPPORTED eq 1
  258.         cmp dword[ebx+png_struct.free_fn],0
  259.         je @f ;if (..!=0)
  260.                 stdcall dword[ebx+png_struct.free_fn], ebx, [p2ptr]
  261.                 jmp .end_f
  262.         @@: ;else
  263. end if
  264.                 mcall SF_SYS_MISC, SSF_MEM_FREE, [p2ptr]
  265. .end_f:
  266.         ret
  267. endp
  268.  
  269. ; This function is called when the application wants to use another method
  270. ; of allocating and freeing memory.
  271.  
  272. ;void (png_structrp png_ptr, voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn)
  273. align 4
  274. proc png_set_mem_fn uses eax edi, png_ptr:dword, mem_ptr:dword, malloc_fn:dword, free_fn:dword
  275.         mov edi,[png_ptr]
  276.         cmp edi,0
  277.         je @f ;if (..!=0)
  278.                 mov eax,[mem_ptr]
  279.                 mov [edi+png_struct.mem_ptr],eax
  280.                 mov eax,[malloc_fn]
  281.                 mov [edi+png_struct.malloc_fn],eax
  282.                 mov eax,[free_fn]
  283.                 mov [edi+png_struct.free_fn],eax
  284.         @@:
  285.         ret
  286. endp
  287.  
  288. ; This function returns a pointer to the mem_ptr associated with the user
  289. ; functions.  The application should free any memory associated with this
  290. ; pointer before png_write_destroy and png_read_destroy are called.
  291.  
  292. ;voidp (const_structrp png_ptr)
  293. align 4
  294. proc png_get_mem_ptr uses edi, png_ptr:dword
  295.         xor eax,eax
  296.         mov edi,[png_ptr]
  297.         cmp edi,0
  298.         je @f ;if (..==0) return 0
  299.                 mov eax,[edi+png_struct.mem_ptr]
  300.         @@:
  301.         ret
  302. endp
  303.  
  304.