static void
JPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength)
{
	if ((uint32)skiplength<=data->bufferbytesleft)
	{
		data->buffercurrentbyte+=skiplength;
		data->bufferbytesleft-=skiplength;
	}
	else
	{
		uint16 m;
		m=skiplength-data->bufferbytesleft;
		if (m<=data->filebytesleft)
		{
			data->bufferbytesleft=0;
			data->fileoffset+=m;
			data->filebytesleft-=m;
			data->filepositioned=0;
		}
		else
		{
			data->bufferbytesleft=0;
			data->filebytesleft=0;
		}
	}
}
['static int\nJPEGFixupTagsSubsamplingSec(struct JPEGFixupTagsSubsamplingData* data)\n{\n\tstatic const char module[] = "JPEGFixupTagsSubsamplingSec";\n\tuint8 m;\n\twhile (1)\n\t{\n\t\twhile (1)\n\t\t{\n\t\t\tif (!JPEGFixupTagsSubsamplingReadByte(data,&m))\n\t\t\t\treturn(0);\n\t\t\tif (m==255)\n\t\t\t\tbreak;\n\t\t}\n\t\twhile (1)\n\t\t{\n\t\t\tif (!JPEGFixupTagsSubsamplingReadByte(data,&m))\n\t\t\t\treturn(0);\n\t\t\tif (m!=255)\n\t\t\t\tbreak;\n\t\t}\n\t\tswitch (m)\n\t\t{\n\t\t\tcase JPEG_MARKER_SOI:\n\t\t\t\tbreak;\n\t\t\tcase JPEG_MARKER_COM:\n\t\t\tcase JPEG_MARKER_APP0:\n\t\t\tcase JPEG_MARKER_APP0+1:\n\t\t\tcase JPEG_MARKER_APP0+2:\n\t\t\tcase JPEG_MARKER_APP0+3:\n\t\t\tcase JPEG_MARKER_APP0+4:\n\t\t\tcase JPEG_MARKER_APP0+5:\n\t\t\tcase JPEG_MARKER_APP0+6:\n\t\t\tcase JPEG_MARKER_APP0+7:\n\t\t\tcase JPEG_MARKER_APP0+8:\n\t\t\tcase JPEG_MARKER_APP0+9:\n\t\t\tcase JPEG_MARKER_APP0+10:\n\t\t\tcase JPEG_MARKER_APP0+11:\n\t\t\tcase JPEG_MARKER_APP0+12:\n\t\t\tcase JPEG_MARKER_APP0+13:\n\t\t\tcase JPEG_MARKER_APP0+14:\n\t\t\tcase JPEG_MARKER_APP0+15:\n\t\t\tcase JPEG_MARKER_DQT:\n\t\t\tcase JPEG_MARKER_DHT:\n\t\t\tcase JPEG_MARKER_DRI:\n\t\t\t\t{\n\t\t\t\t\tuint16 n;\n\t\t\t\t\tif (!JPEGFixupTagsSubsamplingReadWord(data,&n))\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\tif (n<2)\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\tn-=2;\n\t\t\t\t\tif (n>0)\n\t\t\t\t\t\tJPEGFixupTagsSubsamplingSkip(data,n);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase JPEG_MARKER_SOF0:\n\t\t\t\t{\n\t\t\t\t\tuint16 n;\n\t\t\t\t\tuint16 o;\n\t\t\t\t\tuint8 p;\n\t\t\t\t\tuint8 ph,pv;\n\t\t\t\t\tif (!JPEGFixupTagsSubsamplingReadWord(data,&n))\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\tif (n!=8+data->tif->tif_dir.td_samplesperpixel*3)\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\tJPEGFixupTagsSubsamplingSkip(data,7);\n\t\t\t\t\tif (!JPEGFixupTagsSubsamplingReadByte(data,&p))\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\tph=(p>>4);\n\t\t\t\t\tpv=(p&15);\n\t\t\t\t\tJPEGFixupTagsSubsamplingSkip(data,1);\n\t\t\t\t\tfor (o=1; o<data->tif->tif_dir.td_samplesperpixel; o++)\n\t\t\t\t\t{\n\t\t\t\t\t\tJPEGFixupTagsSubsamplingSkip(data,1);\n\t\t\t\t\t\tif (!JPEGFixupTagsSubsamplingReadByte(data,&p))\n\t\t\t\t\t\t\treturn(0);\n\t\t\t\t\t\tif (p!=0x11)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tTIFFWarningExt(data->tif->tif_clientdata,module,\n\t\t\t\t\t\t\t    "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");\n\t\t\t\t\t\t\treturn(1);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tJPEGFixupTagsSubsamplingSkip(data,1);\n\t\t\t\t\t}\n\t\t\t\t\tif (((ph!=1)&&(ph!=2)&&(ph!=4))||((pv!=1)&&(pv!=2)&&(pv!=4)))\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFWarningExt(data->tif->tif_clientdata,module,\n\t\t\t\t\t\t    "Subsampling values inside JPEG compressed data have no TIFF equivalent, auto-correction of TIFF subsampling values failed");\n\t\t\t\t\t\treturn(1);\n\t\t\t\t\t}\n\t\t\t\t\tif ((ph!=data->tif->tif_dir.td_ycbcrsubsampling[0])||(pv!=data->tif->tif_dir.td_ycbcrsubsampling[1]))\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFWarningExt(data->tif->tif_clientdata,module,\n\t\t\t\t\t\t    "Auto-corrected former TIFF subsampling values [%d,%d] to match subsampling values inside JPEG compressed data [%d,%d]",\n\t\t\t\t\t\t    (int)data->tif->tif_dir.td_ycbcrsubsampling[0],\n\t\t\t\t\t\t    (int)data->tif->tif_dir.td_ycbcrsubsampling[1],\n\t\t\t\t\t\t    (int)ph,(int)pv);\n\t\t\t\t\t\tdata->tif->tif_dir.td_ycbcrsubsampling[0]=ph;\n\t\t\t\t\t\tdata->tif->tif_dir.td_ycbcrsubsampling[1]=pv;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn(1);\n\t\t\tdefault:\n\t\t\t\treturn(0);\n\t\t}\n\t}\n}', 'static int\nJPEGFixupTagsSubsamplingReadByte(struct JPEGFixupTagsSubsamplingData* data, uint8* result)\n{\n\tif (data->bufferbytesleft==0)\n\t{\n\t\tuint32 m;\n\t\tif (data->filebytesleft==0)\n\t\t\treturn(0);\n\t\tif (!data->filepositioned)\n\t\t{\n\t\t\tTIFFSeekFile(data->tif,data->fileoffset,SEEK_SET);\n\t\t\tdata->filepositioned=1;\n\t\t}\n\t\tm=data->buffersize;\n\t\tif ((uint64)m>data->filebytesleft)\n\t\t\tm=(uint32)data->filebytesleft;\n\t\tassert(m<0x80000000UL);\n\t\tif (TIFFReadFile(data->tif,data->buffer,(tmsize_t)m)!=(tmsize_t)m)\n\t\t\treturn(0);\n\t\tdata->buffercurrentbyte=data->buffer;\n\t\tdata->bufferbytesleft=m;\n\t\tdata->fileoffset+=m;\n\t\tdata->filebytesleft-=m;\n\t}\n\t*result=*data->buffercurrentbyte;\n\tdata->buffercurrentbyte++;\n\tdata->bufferbytesleft--;\n\treturn(1);\n}', 'static int\nJPEGFixupTagsSubsamplingReadWord(struct JPEGFixupTagsSubsamplingData* data, uint16* result)\n{\n\tuint8 ma;\n\tuint8 mb;\n\tif (!JPEGFixupTagsSubsamplingReadByte(data,&ma))\n\t\treturn(0);\n\tif (!JPEGFixupTagsSubsamplingReadByte(data,&mb))\n\t\treturn(0);\n\t*result=(ma<<8)|mb;\n\treturn(1);\n}', 'static void\nJPEGFixupTagsSubsamplingSkip(struct JPEGFixupTagsSubsamplingData* data, uint16 skiplength)\n{\n\tif ((uint32)skiplength<=data->bufferbytesleft)\n\t{\n\t\tdata->buffercurrentbyte+=skiplength;\n\t\tdata->bufferbytesleft-=skiplength;\n\t}\n\telse\n\t{\n\t\tuint16 m;\n\t\tm=skiplength-data->bufferbytesleft;\n\t\tif (m<=data->filebytesleft)\n\t\t{\n\t\t\tdata->bufferbytesleft=0;\n\t\t\tdata->fileoffset+=m;\n\t\t\tdata->filebytesleft-=m;\n\t\t\tdata->filepositioned=0;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tdata->bufferbytesleft=0;\n\t\t\tdata->filebytesleft=0;\n\t\t}\n\t}\n}']